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: Implement Testing Tokens API #288

Merged
merged 1 commit into from
Apr 24, 2024
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
8 changes: 8 additions & 0 deletions testing_token.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package clerk

type TestingToken struct {
APIResource
Object string `json:"object"`
Token string `json:"token"`
ExpiresAt int64 `json:"expires_at"`
}
20 changes: 20 additions & 0 deletions testingtoken/api.go

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

34 changes: 34 additions & 0 deletions testingtoken/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Package testingtoken provides the Testing Tokens API.
//
// https://clerk.com/docs/reference/backend-api/tag/Testing-Tokens
package testingtoken

import (
"context"
"net/http"

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

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

const path = "/testing_tokens"

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

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

// Create creates a new testing token.
func (c *Client) Create(ctx context.Context) (*clerk.TestingToken, error) {
req := clerk.NewAPIRequest(http.MethodPost, path)
token := &clerk.TestingToken{}
err := c.Backend.Call(ctx, req, token)
return token, err
}
35 changes: 35 additions & 0 deletions testingtoken/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package testingtoken

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

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

func TestTestingTokenClientCreate(t *testing.T) {
t.Parallel()

config := &clerk.ClientConfig{}
config.HTTPClient = &http.Client{
Transport: &clerktest.RoundTripper{
T: t,
In: nil,
Out: json.RawMessage(
`{"object":"testing_token","token":"1713877200-c_3G2MvPu9PnXcuhbPZNao0LOXqK9A7YrnBn0HmIWxt","expires_at":1713880800}`,
),
Method: http.MethodPost,
Path: "/v1/testing_tokens",
},
}
client := NewClient(config)
token, err := client.Create(context.Background())
require.NoError(t, err)
require.Equal(t, "testing_token", token.Object)
require.Equal(t, "1713877200-c_3G2MvPu9PnXcuhbPZNao0LOXqK9A7YrnBn0HmIWxt", token.Token)
require.Equal(t, int64(1713880800), token.ExpiresAt)
}
Loading