Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add ListInvitations and DeleteInvitation methods to OrganizationsService #307

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions clerk/organizations.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,11 @@ type OrganizationInvitation struct {
UpdatedAt int64 `json:"updated_at"`
}

type OrganizationInvitationResponse struct {
Data []OrganizationInvitation `json:"data"`
TotalCount int64 `json:"total_count"`
}

type CreateOrganizationInvitationParams struct {
EmailAddress string `json:"email_address"`
InviterUserID string `json:"inviter_user_id"`
Expand All @@ -232,6 +237,62 @@ func (s *OrganizationsService) CreateInvitation(params CreateOrganizationInvitat
return &organizationInvitation, err
}

type ListAllOrganizationInvitationParams struct {
OrganizationID string `json:"organization_id"`
Limit *int `json:"limit,omitempty"`
Offset *int `json:"offset,omitempty"`
Status []string `json:"status,omitempty"`
}

func (s *OrganizationsService) ListInvitations(params ListAllOrganizationInvitationParams) (*OrganizationInvitationResponse, error) {
endpoint := fmt.Sprintf("%s/%s/%s", OrganizationsUrl, params.OrganizationID, InvitationsURL)
req, _ := s.client.NewRequest(http.MethodGet, endpoint)

query := req.URL.Query()
if params.Limit != nil {
query.Set("limit", strconv.Itoa(*params.Limit))
}
if params.Offset != nil {
query.Set("offset", strconv.Itoa(*params.Offset))
}
for _, status := range params.Status {
query.Add("status", status)
}
req.URL.RawQuery = query.Encode()

var organizationInvitations OrganizationInvitationResponse
_, err := s.client.Do(req, &organizationInvitations)
if err != nil {
return nil, err
}
return &organizationInvitations, nil
}

type DeleteOrganizationInvitationParams struct {
OrganizationID string `json:"organization_id"`
InvitationID string `json:"invitation_id"`
RequestingUserID string `json:"requesting_user_id"`
}

func (s *OrganizationsService) DeleteInvitation(params DeleteOrganizationInvitationParams) (*OrganizationInvitation, error) {
endpoint := fmt.Sprintf("%s/%s/%s/%s", OrganizationsUrl, params.OrganizationID, InvitationsURL, params.InvitationID)
req, err := s.client.NewRequest(http.MethodDelete, endpoint)
if err != nil {
return nil, err
}

q := req.URL.Query()
q.Add("requesting_user_id", params.RequestingUserID)
req.URL.RawQuery = q.Encode()

var organizationInvitation OrganizationInvitation
_, err = s.client.Do(req, &organizationInvitation)
if err != nil {
return nil, err
}
return &organizationInvitation, nil
}

type ListOrganizationMembershipsParams struct {
OrganizationID string
Limit *int
Expand Down
120 changes: 120 additions & 0 deletions clerk/organizations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,87 @@ func TestOrganizationsService_ListAll_invalidServer(t *testing.T) {
t.Errorf("Was not expecting any organizations to be returned, instead got %v", organizations)
}
}
func TestOrganizationsService_ListAllInvitations_happyPath(t *testing.T) {
client, mux, _, teardown := setup("token")
defer teardown()

expectedResponse := fmt.Sprintf(`{
"data": %s,
"total_count": 2
}`, dummyOrganizationInvitationListJson)

organizationID := "org_1mebQggrD3xO5JfuHk7clQ94ysA"

mux.HandleFunc(fmt.Sprintf("/organizations/%s/invitations", organizationID), func(w http.ResponseWriter, req *http.Request) {
testHttpMethod(t, req, "GET")
testHeader(t, req, "Authorization", "Bearer token")
fmt.Fprint(w, expectedResponse)
})

got, err := client.Organizations().ListInvitations(ListAllOrganizationInvitationParams{
OrganizationID: organizationID,
})
if err != nil {
t.Fatalf("Organizations.ListInvitations returned error: %v", err)
}

var want OrganizationInvitationResponse
err = json.Unmarshal([]byte(expectedResponse), &want)
if err != nil {
t.Fatalf("Error unmarshaling expected response: %v", err)
}

if len(got.Data) != len(want.Data) {
t.Errorf("Organizations.ListInvitations returned %d invitations, want %d", len(got.Data), len(want.Data))
}

if got.TotalCount != want.TotalCount {
t.Errorf("Organizations.ListInvitations returned total_count %d, want %d", got.TotalCount, want.TotalCount)
}
}

func TestOrganizationsService_DeleteInvitation(t *testing.T) {
client, mux, _, teardown := setup("token")
defer teardown()

organizationID := "org_123"
invitationID := "inv_456"
requestingUserID := "user_789"

expectedResponse := dummyOrganizationRevokedInvitationJson

mux.HandleFunc(fmt.Sprintf("/organizations/%s/invitations/%s", organizationID, invitationID), func(w http.ResponseWriter, r *http.Request) {
testHttpMethod(t, r, "DELETE")
testHeader(t, r, "Authorization", "Bearer token")

if got := r.URL.Query().Get("requesting_user_id"); got != requestingUserID {
t.Errorf("Request URL query 'requesting_user_id' = %v, want %v", got, requestingUserID)
}

fmt.Fprint(w, expectedResponse)
})

params := DeleteOrganizationInvitationParams{
OrganizationID: organizationID,
InvitationID: invitationID,
RequestingUserID: requestingUserID,
}

invitation, err := client.Organizations().DeleteInvitation(params)
if err != nil {
t.Errorf("Organizations.DeleteInvitation returned error: %v", err)
}

want := &OrganizationInvitation{}
err = json.Unmarshal([]byte(expectedResponse), want)
if err != nil {
t.Fatalf("Error unmarshaling expected response: %v", err)
}

if !reflect.DeepEqual(invitation, want) {
t.Errorf("Organizations.DeleteInvitation returned %+v, want %+v", invitation, want)
}
}
func TestOrganizationsService_UpdateLogo(t *testing.T) {
client, mux, _, teardown := setup("token")
defer teardown()
Expand Down Expand Up @@ -341,3 +421,43 @@ const dummyUpdateOrganizationJson = `{
"app_id": 8,
}
}`

const dummyOrganizationInvitationListJson = `[
{
"object": "organization_invitation",
"id": "orginv_1mebQggrD3xO5JfuHk7clQ94ysA",
"email_address": "[email protected]",
"role": "admin",
"organization_id": "org_1mebQggrD3xO5JfuHk7clQ94ysA",
"status": "pending",
"public_metadata": { },
"private_metadata": { },
"created_at": 1721806882553,
"updated_at": 1721806882553
},
{
"object": "organization_invitation",
"id": "orginv_1mebQggrD3xO5JfuHk7clQ94ysA",
"email_address": "[email protected]",
"role": "admin",
"organization_id": "org_1mebQggrD3xO5JfuHk7clQ94ysA",
"status": "pending",
"public_metadata": { },
"private_metadata": { },
"created_at": 1721806882553,
"updated_at": 1721806882553
}
]`

const dummyOrganizationRevokedInvitationJson = `{
"object": "organization_invitation",
"id": "orginv_1mebQggrD3xO5JfuHk7clQ94ysA",
"email_address": "[email protected]",
"role": "admin",
"organization_id": "org_1mebQggrD3xO5JfuHk7clQ94ysA",
"status": "revoked",
"public_metadata": {},
"private_metadata": {},
"created_at": 1621234567890,
"updated_at": 1621234567890
}`
Loading