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 support for invitations #314

Merged
Merged
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
22 changes: 22 additions & 0 deletions invitation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package clerk

import "encoding/json"

type Invitation struct {
APIResource
Object string `json:"object"`
ID string `json:"id"`
EmailAddress string `json:"email_address"`
PublicMetadata json.RawMessage `json:"public_metadata"`
Revoked bool `json:"revoked,omitempty"`
Status string `json:"status"`
URL string `json:"url,omitempty"`
CreatedAt int64 `json:"created_at"`
UpdatedAt int64 `json:"updated_at"`
}

type InvitationList struct {
APIResource
Invitations []*Invitation `json:"data"`
TotalCount int64 `json:"total_count"`
}
30 changes: 30 additions & 0 deletions invitation/api.go

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

67 changes: 67 additions & 0 deletions invitation/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package invitation

import (
"context"
"encoding/json"
"fmt"
"net/http"

"github.com/clerk/clerk-sdk-go/v2"
)

//go:generate go run ../cmd/gen/main.go

const path = "/invitations"

// Client is used to invoke the Invitations API.
type Client struct {
Backend clerk.Backend
}

func NewClient(config *clerk.ClientConfig) *Client {
return &Client{
Backend: clerk.NewBackend(&config.BackendConfig),
}
}

type ListParams struct {
clerk.APIParams
}

// List returns all invitations.
func (c *Client) List(ctx context.Context, params *ListParams) (*clerk.InvitationList, error) {
req := clerk.NewAPIRequest(http.MethodGet, fmt.Sprintf("%s?paginated=true", path))
list := &clerk.InvitationList{}
err := c.Backend.Call(ctx, req, list)
return list, err
}

type CreateParams struct {
clerk.APIParams
EmailAddress string `json:"email_address"`
PublicMetadata *json.RawMessage `json:"public_metadata,omitempty"`
RedirectURL *string `json:"redirect_url,omitempty"`
Notify *bool `json:"notify,omitempty"`
IgnoreExisting *bool `json:"ignore_existing,omitempty"`
}

// Create adds a new identifier to the allowlist.
func (c *Client) Create(ctx context.Context, params *CreateParams) (*clerk.Invitation, error) {
req := clerk.NewAPIRequest(http.MethodPost, path)
req.SetParams(params)
invitation := &clerk.Invitation{}
err := c.Backend.Call(ctx, req, invitation)
return invitation, err
}

// Revoke revokes a pending invitation.
func (c *Client) Revoke(ctx context.Context, id string) (*clerk.Invitation, error) {
path, err := clerk.JoinPath(path, id, "revoke")
if err != nil {
return nil, err
}
req := clerk.NewAPIRequest(http.MethodPost, path)
invitation := &clerk.Invitation{}
err = c.Backend.Call(ctx, req, invitation)
return invitation, err
}
130 changes: 130 additions & 0 deletions invitation/invitation_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package invitation

import (
"context"
"encoding/json"
"fmt"
"net/http"
"testing"

"github.com/clerk/clerk-sdk-go/v2"
"github.com/clerk/clerk-sdk-go/v2/clerktest"
"github.com/stretchr/testify/require"
)

func TestInvitationList(t *testing.T) {
clerk.SetBackend(clerk.NewBackend(&clerk.BackendConfig{
HTTPClient: &http.Client{
Transport: &clerktest.RoundTripper{
T: t,
Out: json.RawMessage(`{
"data": [{"id":"inv_123","email_address":"[email protected]"}],
"total_count": 1
}`),
Path: "/v1/invitations",
Method: http.MethodGet,
},
},
}))

list, err := List(context.Background(), &ListParams{})
require.NoError(t, err)
require.Equal(t, int64(1), list.TotalCount)
require.Equal(t, 1, len(list.Invitations))
require.Equal(t, "inv_123", list.Invitations[0].ID)
require.Equal(t, "[email protected]", list.Invitations[0].EmailAddress)
}

func TestInvitationCreate(t *testing.T) {
emailAddress := "[email protected]"
id := "inv_123"
clerk.SetBackend(clerk.NewBackend(&clerk.BackendConfig{
HTTPClient: &http.Client{
Transport: &clerktest.RoundTripper{
T: t,
In: json.RawMessage(fmt.Sprintf(`{"email_address":"%s"}`, emailAddress)),
Out: json.RawMessage(fmt.Sprintf(`{"id":"%s","email_address":"%s"}`, id, emailAddress)),
Method: http.MethodPost,
Path: "/v1/invitations",
},
},
}))

invitation, err := Create(context.Background(), &CreateParams{
EmailAddress: emailAddress,
})
require.NoError(t, err)
require.Equal(t, id, invitation.ID)
require.Equal(t, emailAddress, invitation.EmailAddress)
}

func TestInvitationCreate_Error(t *testing.T) {
clerk.SetBackend(clerk.NewBackend(&clerk.BackendConfig{
HTTPClient: &http.Client{
Transport: &clerktest.RoundTripper{
T: t,
Status: http.StatusBadRequest,
Out: json.RawMessage(`{
"errors":[{
"code":"create-error-code"
}],
"clerk_trace_id":"create-trace-id"
}`),
},
},
}))

_, err := Create(context.Background(), &CreateParams{})
require.Error(t, err)
apiErr, ok := err.(*clerk.APIErrorResponse)
require.True(t, ok)
require.Equal(t, "create-trace-id", apiErr.TraceID)
require.Equal(t, 1, len(apiErr.Errors))
require.Equal(t, "create-error-code", apiErr.Errors[0].Code)
}

func TestInvitationRevoke(t *testing.T) {
id := "inv_123"
clerk.SetBackend(clerk.NewBackend(&clerk.BackendConfig{
HTTPClient: &http.Client{
Transport: &clerktest.RoundTripper{
T: t,
Out: json.RawMessage(fmt.Sprintf(`{"id":"%s","revoked":true,"status":"revoked"}`, id)),
Method: http.MethodPost,
Path: "/v1/invitations/" + id + "/revoke",
},
},
}))

invitation, err := Revoke(context.Background(), id)
require.NoError(t, err)
require.Equal(t, id, invitation.ID)
require.True(t, invitation.Revoked)
require.Equal(t, "revoked", invitation.Status)
}

func TestInvitationRevoke_Error(t *testing.T) {
id := "inv_123"
clerk.SetBackend(clerk.NewBackend(&clerk.BackendConfig{
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"
}`),
},
},
}))

_, err := Revoke(context.Background(), 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)
}
Loading