-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Blocklist Identifiers API (#222)
Added support for the blocklist identifiers API. Supported operations are Create, Delete and List.
- Loading branch information
Showing
9 changed files
with
251 additions
and
55 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,14 @@ | ||
package allowlistidentifier | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/clerk/clerk-sdk-go/v2" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/clerk/clerk-sdk-go/v2/clerktest" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
|
@@ -19,10 +17,12 @@ func TestAllowlistIdentifierCreate(t *testing.T) { | |
id := "alid_123" | ||
clerk.SetBackend(clerk.NewBackend(&clerk.BackendConfig{ | ||
HTTPClient: &http.Client{ | ||
Transport: &mockRoundTripper{ | ||
T: t, | ||
in: json.RawMessage(fmt.Sprintf(`{"identifier":"%s"}`, identifier)), | ||
out: json.RawMessage(fmt.Sprintf(`{"id":"%s","identifier":"%s"}`, id, identifier)), | ||
Transport: &clerktest.RoundTripper{ | ||
T: t, | ||
In: json.RawMessage(fmt.Sprintf(`{"identifier":"%s"}`, identifier)), | ||
Out: json.RawMessage(fmt.Sprintf(`{"id":"%s","identifier":"%s"}`, id, identifier)), | ||
Method: http.MethodPost, | ||
Path: "/v1/allowlist_identifiers", | ||
}, | ||
}, | ||
})) | ||
|
@@ -31,17 +31,17 @@ func TestAllowlistIdentifierCreate(t *testing.T) { | |
Identifier: clerk.String(identifier), | ||
}) | ||
require.NoError(t, err) | ||
assert.Equal(t, id, allowlistIdentifier.ID) | ||
assert.Equal(t, identifier, allowlistIdentifier.Identifier) | ||
require.Equal(t, id, allowlistIdentifier.ID) | ||
require.Equal(t, identifier, allowlistIdentifier.Identifier) | ||
} | ||
|
||
func TestAllowlistIdentifierCreate_Error(t *testing.T) { | ||
clerk.SetBackend(clerk.NewBackend(&clerk.BackendConfig{ | ||
HTTPClient: &http.Client{ | ||
Transport: &mockRoundTripper{ | ||
Transport: &clerktest.RoundTripper{ | ||
T: t, | ||
status: http.StatusBadRequest, | ||
out: json.RawMessage(`{ | ||
Status: http.StatusBadRequest, | ||
Out: json.RawMessage(`{ | ||
"errors":[{ | ||
"code":"create-error-code" | ||
}], | ||
|
@@ -55,70 +55,49 @@ func TestAllowlistIdentifierCreate_Error(t *testing.T) { | |
require.Error(t, err) | ||
apiErr, ok := err.(*clerk.APIErrorResponse) | ||
require.True(t, ok) | ||
assert.Equal(t, "create-trace-id", apiErr.TraceID) | ||
require.Equal(t, "create-trace-id", apiErr.TraceID) | ||
require.Equal(t, 1, len(apiErr.Errors)) | ||
assert.Equal(t, "create-error-code", apiErr.Errors[0].Code) | ||
require.Equal(t, "create-error-code", apiErr.Errors[0].Code) | ||
} | ||
|
||
func TestAllowlistIdentifierDelete(t *testing.T) { | ||
id := "alid_456" | ||
clerk.SetBackend(clerk.NewBackend(&clerk.BackendConfig{ | ||
HTTPClient: &http.Client{ | ||
Transport: &mockRoundTripper{ | ||
T: t, | ||
out: json.RawMessage(fmt.Sprintf(`{"id":"%s","deleted":true}`, id)), | ||
Transport: &clerktest.RoundTripper{ | ||
T: t, | ||
Out: json.RawMessage(fmt.Sprintf(`{"id":"%s","deleted":true}`, id)), | ||
Method: http.MethodDelete, | ||
Path: "/v1/allowlist_identifiers/" + id, | ||
}, | ||
}, | ||
})) | ||
|
||
allowlistIdentifier, err := Delete(context.Background(), id) | ||
require.NoError(t, err) | ||
assert.Equal(t, id, allowlistIdentifier.ID) | ||
assert.True(t, allowlistIdentifier.Deleted) | ||
require.Equal(t, id, allowlistIdentifier.ID) | ||
require.True(t, allowlistIdentifier.Deleted) | ||
} | ||
|
||
func TestAllowlistIdentifierList(t *testing.T) { | ||
clerk.SetBackend(clerk.NewBackend(&clerk.BackendConfig{ | ||
HTTPClient: &http.Client{ | ||
Transport: &mockRoundTripper{ | ||
Transport: &clerktest.RoundTripper{ | ||
T: t, | ||
out: json.RawMessage(`{ | ||
Out: json.RawMessage(`{ | ||
"data": [{"id":"alid_123","identifier":"[email protected]"}], | ||
"total_count": 1 | ||
}`), | ||
Method: http.MethodGet, | ||
Path: "/v1/allowlist_identifiers", | ||
}, | ||
}, | ||
})) | ||
|
||
list, err := List(context.Background(), &ListParams{}) | ||
require.NoError(t, err) | ||
assert.Equal(t, int64(1), list.TotalCount) | ||
assert.Equal(t, 1, len(list.AllowlistIdentifiers)) | ||
assert.Equal(t, "alid_123", list.AllowlistIdentifiers[0].ID) | ||
assert.Equal(t, "[email protected]", list.AllowlistIdentifiers[0].Identifier) | ||
} | ||
|
||
type mockRoundTripper struct { | ||
T *testing.T | ||
status int | ||
in json.RawMessage | ||
out json.RawMessage | ||
} | ||
|
||
func (rt *mockRoundTripper) RoundTrip(r *http.Request) (*http.Response, error) { | ||
if rt.status == 0 { | ||
rt.status = http.StatusOK | ||
} | ||
if rt.in != nil { | ||
body, err := io.ReadAll(r.Body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer r.Body.Close() | ||
assert.JSONEq(rt.T, string(rt.in), string(body)) | ||
} | ||
return &http.Response{ | ||
StatusCode: rt.status, | ||
Body: io.NopCloser(bytes.NewReader(rt.out)), | ||
}, nil | ||
require.Equal(t, int64(1), list.TotalCount) | ||
require.Equal(t, 1, len(list.AllowlistIdentifiers)) | ||
require.Equal(t, "alid_123", list.AllowlistIdentifiers[0].ID) | ||
require.Equal(t, "[email protected]", list.AllowlistIdentifiers[0].Identifier) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package clerk | ||
|
||
type BlocklistIdentifier struct { | ||
APIResource | ||
Object string `json:"object"` | ||
ID string `json:"id"` | ||
Identifier string `json:"identifier"` | ||
IdentifierType string `json:"identifier_type"` | ||
CreatedAt int64 `json:"created_at"` | ||
UpdatedAt int64 `json:"updated_at"` | ||
} | ||
|
||
type BlocklistIdentifierList struct { | ||
APIResource | ||
BlocklistIdentifiers []*BlocklistIdentifier `json:"data"` | ||
TotalCount int64 `json:"total_count"` | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Package blocklistidentifier provides the Blocklist Identifiers API. | ||
package blocklistidentifier | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/clerk/clerk-sdk-go/v2" | ||
) | ||
|
||
//go:generate go run ../cmd/gen/main.go | ||
|
||
const path = "/blocklist_identifiers" | ||
|
||
// Client is used to invoke the Blocklist Identifiers API. | ||
type Client struct { | ||
Backend clerk.Backend | ||
} | ||
|
||
type ClientConfig struct { | ||
clerk.BackendConfig | ||
} | ||
|
||
func NewClient(config *ClientConfig) *Client { | ||
return &Client{ | ||
Backend: clerk.NewBackend(&config.BackendConfig), | ||
} | ||
} | ||
|
||
type CreateParams struct { | ||
clerk.APIParams | ||
Identifier *string `json:"identifier,omitempty"` | ||
} | ||
|
||
// Create adds a new identifier to the blocklist. | ||
func (c *Client) Create(ctx context.Context, params *CreateParams) (*clerk.BlocklistIdentifier, error) { | ||
req := clerk.NewAPIRequest(http.MethodPost, path) | ||
req.SetParams(params) | ||
identifier := &clerk.BlocklistIdentifier{} | ||
err := c.Backend.Call(ctx, req, identifier) | ||
return identifier, err | ||
} | ||
|
||
// Delete removes an identifier from the blocklist. | ||
func (c *Client) Delete(ctx context.Context, id string) (*clerk.DeletedResource, error) { | ||
path, err := clerk.JoinPath(path, id) | ||
if err != nil { | ||
return nil, err | ||
} | ||
req := clerk.NewAPIRequest(http.MethodDelete, path) | ||
identifier := &clerk.DeletedResource{} | ||
err = c.Backend.Call(ctx, req, identifier) | ||
return identifier, err | ||
} | ||
|
||
type ListParams struct { | ||
clerk.APIParams | ||
} | ||
|
||
// List returns all the identifiers in the blocklist. | ||
func (c *Client) List(ctx context.Context, params *ListParams) (*clerk.BlocklistIdentifierList, error) { | ||
req := clerk.NewAPIRequest(http.MethodGet, fmt.Sprintf("%s?paginated=true", path)) | ||
list := &clerk.BlocklistIdentifierList{} | ||
err := c.Backend.Call(ctx, req, list) | ||
return list, err | ||
} |
Oops, something went wrong.