diff --git a/organization_invitation.go b/organization_invitation.go index 844faf4..5ce9855 100644 --- a/organization_invitation.go +++ b/organization_invitation.go @@ -3,11 +3,11 @@ 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"` + 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 { diff --git a/organizationinvitation/client_test.go b/organizationinvitation/client_test.go index f36b181..2e7c3ca 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,157 @@ 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(), organizationID, &ListParams{ + 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() + organizationID := "org_123" + params := &ListParams{ListParams: clerk.ListParams{Limit: clerk.Int64(10)}, Statuses: &[]string{"pending", "accepted"}} + 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(), organizationID, params) + 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(), organizationID, 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(), organizationID, 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(), organizationID, 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(), organizationID, 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) +}