Skip to content

Commit

Permalink
test: create tests for organization invitations CRUD
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolasLopes7 committed Oct 1, 2024
1 parent 49d07bd commit 9bb3e8d
Show file tree
Hide file tree
Showing 4 changed files with 197 additions and 20 deletions.
14 changes: 7 additions & 7 deletions organization_invitation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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"`
Expand Down
12 changes: 6 additions & 6 deletions organizationinvitation/api.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 18 additions & 7 deletions organizationinvitation/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down
166 changes: 166 additions & 0 deletions organizationinvitation/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"testing"

"github.com/clerk/clerk-sdk-go/v2"
Expand Down Expand Up @@ -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)
}

0 comments on commit 9bb3e8d

Please sign in to comment.