diff --git a/fake_client.go b/fake_client.go index 3283e57..0364ec4 100644 --- a/fake_client.go +++ b/fake_client.go @@ -192,6 +192,8 @@ type Clienter interface { // Ping Ping() error + + ListMemberships() (*MembershipResponse, error) } // NewFakeClient initializes a Client that doesn't attach to a @@ -270,6 +272,13 @@ func NewFakeClient() (*FakeClient, error) { }, nil } +// ListMemberships implemented in a fake way for automated tests +func (c *FakeClient) ListMemberships() (*MembershipResponse, error) { + return &MembershipResponse{ + Accounts: c.OrganisationAccounts, + }, nil +} + // Ping implemented in a fake way for automated tests func (c *FakeClient) Ping() error { if c.PingErr != nil { diff --git a/membership.go b/membership.go new file mode 100644 index 0000000..80faccc --- /dev/null +++ b/membership.go @@ -0,0 +1,27 @@ +package civogo + +import ( + "bytes" + "encoding/json" +) + +// MembershipResponse is the response for the memberships of a user +type MembershipResponse struct { + Accounts []Account + Organisations []Organisation +} + +// ListMemberships returns all the memberships(to accounts and organisations) for the user +func (c *Client) ListMemberships() (*MembershipResponse, error) { + resp, err := c.SendGetRequest("/v2/memberships") + if err != nil { + return nil, decodeError(err) + } + + mrs := &MembershipResponse{} + if err := json.NewDecoder(bytes.NewReader(resp)).Decode(&mrs); err != nil { + return nil, err + } + + return mrs, nil +} diff --git a/membership_test.go b/membership_test.go new file mode 100644 index 0000000..d3a33b9 --- /dev/null +++ b/membership_test.go @@ -0,0 +1,45 @@ +package civogo + +import "testing" + +func TestListMemberships(t *testing.T) { + client, server, _ := NewClientForTesting(map[string]string{ + "/v2/memberships": `{ + "accounts": [ + { + "id": "4f229791-6088-42dd-8fbe-3f64ec47567f", + "api_key": "75e521bc74d34b21b42827fb58fcd590", + "email_address": "team@civo.com", + "label": "team testing", + "flags": null, + "salt": null, + "timezone": "Europe/London", + "organisation_id": "63bd4fe4-eeff-421b-aa24-1518decc5464" + } + ], + + "organisations": [ + { + "id": "63bd4fe4-eeff-421b-aa24-1518decc5464", + "name": "Our-group Inc" + } + ] + + }`, + }) + defer server.Close() + got, err := client.ListMemberships() + + if err != nil { + t.Errorf("Request returned an error: %s", err) + return + } + + if got.Accounts[0].ID != "4f229791-6088-42dd-8fbe-3f64ec47567f" { + t.Errorf("Expected User ID %s, got %s", "4f229791-6088-42dd-8fbe-3f64ec47567f", got.Accounts[0].ID) + } + if got.Organisations[0].ID != "63bd4fe4-eeff-421b-aa24-1518decc5464" { + t.Errorf("Expected User ID %s, got %s", "63bd4fe4-eeff-421b-aa24-1518decc5464", got.Organisations[0].ID) + } + +}