Skip to content

Commit

Permalink
memberships endpoint
Browse files Browse the repository at this point in the history
Signed-off-by: Harsh Thakur <harshthakur9030@gmail.com>
  • Loading branch information
RealHarshThakur committed Nov 6, 2024
1 parent 38c04a8 commit cbc5511
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 0 deletions.
9 changes: 9 additions & 0 deletions fake_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ type Clienter interface {

// Ping
Ping() error

ListMemberships() (*MembershipResponse, error)
}

// NewFakeClient initializes a Client that doesn't attach to a
Expand Down Expand Up @@ -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 {
Expand Down
27 changes: 27 additions & 0 deletions membership.go
Original file line number Diff line number Diff line change
@@ -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
}
45 changes: 45 additions & 0 deletions membership_test.go
Original file line number Diff line number Diff line change
@@ -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)
}

}

0 comments on commit cbc5511

Please sign in to comment.