From 9bb3e8d811513830123a9d823458dbb9d28c3298 Mon Sep 17 00:00:00 2001 From: Nicolas Lopes Date: Tue, 1 Oct 2024 11:47:15 -0300 Subject: [PATCH] test: create tests for organization invitations CRUD --- organization_invitation.go | 14 +-- organizationinvitation/api.go | 12 +- organizationinvitation/client.go | 25 ++-- organizationinvitation/client_test.go | 166 ++++++++++++++++++++++++++ 4 files changed, 197 insertions(+), 20 deletions(-) diff --git a/organization_invitation.go b/organization_invitation.go index 844faf46..ad89767f 100644 --- a/organization_invitation.go +++ b/organization_invitation.go @@ -2,12 +2,12 @@ package clerk import "encoding/json" -type publicOrganizationData struct { - ID string `json:"id"` - Name string `json:"name"` - Slug string `json:"slug"` - ImageURL string `json:"image_url,omitempty"` - HasImage bool `json:"has_image"` +type PublicOrganizationData struct { + ID string `json:"id"` + Name string `json:"name"` + Slug string `json:"slug"` + ImageURL *string `json:"image_url,omitempty"` + HasImage bool `json:"has_image"` } type OrganizationInvitation struct { @@ -17,7 +17,7 @@ type OrganizationInvitation struct { EmailAddress string `json:"email_address"` Role string `json:"role"` OrganizationID string `json:"organization_id"` - PublicOrganizationData *publicOrganizationData `json:"public_organization_data,omitempty"` + PublicOrganizationData *PublicOrganizationData `json:"public_organization_data,omitempty"` Status string `json:"status"` PublicMetadata json.RawMessage `json:"public_metadata"` PrivateMetadata json.RawMessage `json:"private_metadata"` diff --git a/organizationinvitation/api.go b/organizationinvitation/api.go index ca79202d..29340217 100644 --- a/organizationinvitation/api.go +++ b/organizationinvitation/api.go @@ -14,18 +14,18 @@ func Create(ctx context.Context, params *CreateParams) (*clerk.OrganizationInvit } // List returns a list of organization invitations -func List(ctx context.Context, organizationID string, params *ListParams) (*clerk.OrganizationInvitationList, error) { - return getClient().List(ctx, organizationID, params) +func List(ctx context.Context, params *ListParams) (*clerk.OrganizationInvitationList, error) { + return getClient().List(ctx, params) } // Get retrieves the detail for an organization invitation. -func Get(ctx context.Context, organizationID, id string) (*clerk.OrganizationInvitation, error) { - return getClient().Get(ctx, organizationID, id) +func Get(ctx context.Context, params *GetParams) (*clerk.OrganizationInvitation, error) { + return getClient().Get(ctx, params) } // Revoke marks the organization invitation as revoked. -func Revoke(ctx context.Context, organizationID, id string) (*clerk.OrganizationInvitation, error) { - return getClient().Revoke(ctx, organizationID, id) +func Revoke(ctx context.Context, params *RevokeParams) (*clerk.OrganizationInvitation, error) { + return getClient().Revoke(ctx, params) } func getClient() *Client { diff --git a/organizationinvitation/client.go b/organizationinvitation/client.go index 65da182c..d5154eb2 100644 --- a/organizationinvitation/client.go +++ b/organizationinvitation/client.go @@ -52,7 +52,8 @@ func (c *Client) Create(ctx context.Context, params *CreateParams) (*clerk.Organ type ListParams struct { clerk.APIParams clerk.ListParams - Statuses *[]string + OrganizationID string + Statuses *[]string } func (p *ListParams) ToQuery() url.Values { @@ -66,8 +67,8 @@ func (p *ListParams) ToQuery() url.Values { } // List returns a list of organization invitations -func (c *Client) List(ctx context.Context, organizationID string, params *ListParams) (*clerk.OrganizationInvitationList, error) { - path, err := clerk.JoinPath(path, organizationID, "/invitations") +func (c *Client) List(ctx context.Context, params *ListParams) (*clerk.OrganizationInvitationList, error) { + path, err := clerk.JoinPath(path, params.OrganizationID, "/invitations") if err != nil { return nil, err } @@ -78,9 +79,14 @@ func (c *Client) List(ctx context.Context, organizationID string, params *ListPa return invitation, err } +type GetParams struct { + OrganizationID string + ID string +} + // Get retrieves the detail for an organization invitation. -func (c *Client) Get(ctx context.Context, organizationID, id string) (*clerk.OrganizationInvitation, error) { - path, err := clerk.JoinPath(path, organizationID, "/invitations", id) +func (c *Client) Get(ctx context.Context, params *GetParams) (*clerk.OrganizationInvitation, error) { + path, err := clerk.JoinPath(path, params.OrganizationID, "/invitations", params.ID) if err != nil { return nil, err } @@ -90,9 +96,14 @@ func (c *Client) Get(ctx context.Context, organizationID, id string) (*clerk.Org return invitation, err } +type RevokeParams struct { + OrganizationID string + ID string +} + // Revoke marks the organization invitation as revoked. -func (c *Client) Revoke(ctx context.Context, organizationID, id string) (*clerk.OrganizationInvitation, error) { - path, err := clerk.JoinPath(path, organizationID, "/invitations", id, "/revoke") +func (c *Client) Revoke(ctx context.Context, params *RevokeParams) (*clerk.OrganizationInvitation, error) { + path, err := clerk.JoinPath(path, params.OrganizationID, "/invitations", params.ID, "/revoke") if err != nil { return nil, err } diff --git a/organizationinvitation/client_test.go b/organizationinvitation/client_test.go index f36b1815..2078c7d3 100644 --- a/organizationinvitation/client_test.go +++ b/organizationinvitation/client_test.go @@ -5,6 +5,7 @@ import ( "encoding/json" "fmt" "net/http" + "net/url" "testing" "github.com/clerk/clerk-sdk-go/v2" @@ -62,3 +63,168 @@ func TestOrganizationInvitationClientCreate_Error(t *testing.T) { require.Equal(t, 1, len(apiErr.Errors)) require.Equal(t, "create-error-code", apiErr.Errors[0].Code) } + +func TestOrganizationInvitationClientList(t *testing.T) { + t.Parallel() + organizationID := "org_123" + id := "orginv_123" + statuses := []string{"pending", "accepted"} + limit := int64(10) + config := &clerk.ClientConfig{} + config.HTTPClient = &http.Client{ + Transport: &clerktest.RoundTripper{ + T: t, + Out: json.RawMessage(fmt.Sprintf(`{"data":[{"id":"%s","object":"organization_invitation","email_address":"string","role":"string","organization_id":"%s","status":"string","public_metadata":{},"private_metadata":{},"created_at":0,"updated_at":0}],"total_count":1}`, id, organizationID)), + Method: http.MethodGet, + Path: "/v1/organizations/" + organizationID + "/invitations", + Query: &url.Values{ + "limit": []string{fmt.Sprintf("%d", limit)}, + "status": statuses, + }, + }, + } + client := NewClient(config) + response, err := client.List(context.Background(), &ListParams{ + OrganizationID: organizationID, + ListParams: clerk.ListParams{ + Limit: clerk.Int64(limit), + }, + Statuses: &statuses, + }) + require.NoError(t, err) + require.Len(t, response.OrganizationInvitations, 1) + require.Equal(t, id, response.OrganizationInvitations[0].ID) + require.Equal(t, organizationID, response.OrganizationInvitations[0].OrganizationID) + require.Equal(t, int64(1), response.TotalCount) +} + +func TestOrganizationInvitationClientList_Error(t *testing.T) { + t.Parallel() + config := &clerk.ClientConfig{} + config.HTTPClient = &http.Client{ + Transport: &clerktest.RoundTripper{ + T: t, + Status: http.StatusBadRequest, + Out: json.RawMessage(`{ + "errors":[{ + "code":"list-error-code" + }], + "clerk_trace_id":"list-trace-id" + }`), + }, + } + client := NewClient(config) + _, err := client.List(context.Background(), &ListParams{OrganizationID: "org_123"}) + require.Error(t, err) + apiErr, ok := err.(*clerk.APIErrorResponse) + require.True(t, ok) + require.Equal(t, "list-trace-id", apiErr.TraceID) + require.Equal(t, 1, len(apiErr.Errors)) + require.Equal(t, "list-error-code", apiErr.Errors[0].Code) +} + +func TestOrganizationInvitationClientGet(t *testing.T) { + t.Parallel() + organizationID := "org_123" + id := "orginv_123" + config := &clerk.ClientConfig{} + config.HTTPClient = &http.Client{ + Transport: &clerktest.RoundTripper{ + T: t, + Out: json.RawMessage(fmt.Sprintf(`{"id":"%s","object":"organization_invitation","email_address":"string","role":"string","organization_id":"%s","status":"string","public_metadata":{},"private_metadata":{},"created_at":0,"updated_at":0}`, id, organizationID)), + Method: http.MethodGet, + Path: "/v1/organizations/" + organizationID + "/invitations/" + id, + }, + } + client := NewClient(config) + response, err := client.Get(context.Background(), &GetParams{ + OrganizationID: organizationID, + ID: id, + }) + require.NoError(t, err) + require.Equal(t, id, response.ID) + require.Equal(t, organizationID, response.OrganizationID) +} + +func TestOrganizationInvitationClientGet_Error(t *testing.T) { + t.Parallel() + organizationID := "org_123" + id := "orginv_123" + config := &clerk.ClientConfig{} + config.HTTPClient = &http.Client{ + Transport: &clerktest.RoundTripper{ + T: t, + Status: http.StatusBadRequest, + Out: json.RawMessage(`{ + "errors":[{ + "code":"get-error-code" + }], + "clerk_trace_id":"get-trace-id" + }`), + }, + } + client := NewClient(config) + _, err := client.Get(context.Background(), &GetParams{ + OrganizationID: organizationID, + ID: id, + }) + require.Error(t, err) + apiErr, ok := err.(*clerk.APIErrorResponse) + require.True(t, ok) + require.Equal(t, "get-trace-id", apiErr.TraceID) + require.Equal(t, 1, len(apiErr.Errors)) + require.Equal(t, "get-error-code", apiErr.Errors[0].Code) +} + +func TestOrganizationInvitationClientRevoke(t *testing.T) { + t.Parallel() + organizationID := "org_123" + id := "orginv_123" + config := &clerk.ClientConfig{} + config.HTTPClient = &http.Client{ + Transport: &clerktest.RoundTripper{ + T: t, + Out: json.RawMessage(fmt.Sprintf(`{"id":"%s","object":"organization_invitation","email_address":"string","role":"string","organization_id":"%s","status":"string","public_metadata":{},"private_metadata":{},"created_at":0,"updated_at":0}`, id, organizationID)), + Method: http.MethodPost, + Path: "/v1/organizations/" + organizationID + "/invitations/" + id + "/revoke", + }, + } + client := NewClient(config) + response, err := client.Revoke(context.Background(), &RevokeParams{ + OrganizationID: organizationID, + ID: id, + }) + require.NoError(t, err) + require.Equal(t, id, response.ID) + require.Equal(t, organizationID, response.OrganizationID) +} + +func TestOrganizationInvitationClientRevoke_Error(t *testing.T) { + t.Parallel() + organizationID := "org_123" + id := "orginv_123" + config := &clerk.ClientConfig{} + config.HTTPClient = &http.Client{ + Transport: &clerktest.RoundTripper{ + T: t, + Status: http.StatusBadRequest, + Out: json.RawMessage(`{ + "errors":[{ + "code":"revoke-error-code" + }], + "clerk_trace_id":"revoke-trace-id" + }`), + }, + } + client := NewClient(config) + _, err := client.Revoke(context.Background(), &RevokeParams{ + OrganizationID: organizationID, + ID: id, + }) + require.Error(t, err) + apiErr, ok := err.(*clerk.APIErrorResponse) + require.True(t, ok) + require.Equal(t, "revoke-trace-id", apiErr.TraceID) + require.Equal(t, 1, len(apiErr.Errors)) + require.Equal(t, "revoke-error-code", apiErr.Errors[0].Code) +}