From a2abbc37939c1b6c8f936e0e192f82f6c317e842 Mon Sep 17 00:00:00 2001 From: Luther Monson Date: Thu, 31 Aug 2023 01:46:41 -0700 Subject: [PATCH] adding tests for domains (#91) --- access_test.go | 53 +++++++++++++++++++++++++++++++++++++ proxmox.go | 4 +++ tests/mocks/pve7x/access.go | 19 +++++++++++++ 3 files changed, 76 insertions(+) diff --git a/access_test.go b/access_test.go index ab370c8..ad8472c 100644 --- a/access_test.go +++ b/access_test.go @@ -165,3 +165,56 @@ func TestACL(t *testing.T) { assert.Nil(t, err) assert.Len(t, acls, 1) } + +func TestNewDomain(t *testing.T) { + mocks.On(mockConfig) + defer mocks.Off() + client := mockClient() + + assert.Nil(t, client.NewDomain("test", "t")) +} + +func TestDomain_Update(t *testing.T) { + mocks.On(mockConfig) + defer mocks.Off() + client := mockClient() + + // no realm name + domain := Domain{ + client: client, + } + + assert.Error(t, domain.Update()) + domain.Realm = "test" + assert.Nil(t, domain.Update()) +} + +func TestDomain_Delete(t *testing.T) { + mocks.On(mockConfig) + defer mocks.Off() + client := mockClient() + + // no realm name + domain := Domain{ + client: client, + } + + assert.Error(t, domain.Delete()) + domain.Realm = "test" + assert.Nil(t, domain.Delete()) +} + +func TestDomain_Sync(t *testing.T) { + mocks.On(mockConfig) + defer mocks.Off() + client := mockClient() + + // no realm name + domain := Domain{ + client: client, + } + + assert.Error(t, domain.Sync(DomainSyncOptions{})) + domain.Realm = "test" + assert.Nil(t, domain.Sync(DomainSyncOptions{})) +} diff --git a/proxmox.go b/proxmox.go index 5f25a23..140cbec 100644 --- a/proxmox.go +++ b/proxmox.go @@ -275,6 +275,10 @@ func (c *Client) handleResponse(res *http.Response, v interface{}) error { return fmt.Errorf("bad request: %s - %s", res.Status, string(body)) } + // if nil passed dont bother to do any unmarshalling + if nil == v { + return nil + } // account for everything being in a data key var datakey map[string]json.RawMessage if err := json.Unmarshal(body, &datakey); err != nil { diff --git a/tests/mocks/pve7x/access.go b/tests/mocks/pve7x/access.go index 782cefc..134f203 100644 --- a/tests/mocks/pve7x/access.go +++ b/tests/mocks/pve7x/access.go @@ -830,4 +830,23 @@ func access() { ] }`) + gock.New(config.C.URI). + Post("^/access/domains"). + Reply(200). + JSON(``) + + gock.New(config.C.URI). + Put("^/access/domains/test$"). + Reply(200). + JSON(``) + + gock.New(config.C.URI). + Delete("^/access/domains/test$"). + Reply(200). + JSON(``) + + gock.New(config.C.URI). + Post("^/access/domains/test$"). + Reply(200). + JSON(``) }