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 be41875
Showing 1 changed file with 144 additions and 0 deletions.
144 changes: 144 additions & 0 deletions organizationinvitation/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,147 @@ 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"
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,
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",
},
}
client := NewClient(config)
response, err := client.List(context.Background(), organizationID, params)
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)
}

0 comments on commit be41875

Please sign in to comment.