From cbc55116a2ff9a9602ca85edcf633841a28485e2 Mon Sep 17 00:00:00 2001 From: Harsh Thakur Date: Wed, 6 Nov 2024 14:52:34 +0530 Subject: [PATCH] memberships endpoint Signed-off-by: Harsh Thakur --- fake_client.go | 9 +++++++++ membership.go | 27 +++++++++++++++++++++++++++ membership_test.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+) create mode 100644 membership.go create mode 100644 membership_test.go 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) + } + +}