Skip to content

Commit

Permalink
feat: Implement Testing Tokens API
Browse files Browse the repository at this point in the history
  • Loading branch information
agis committed Apr 24, 2024
1 parent 33b843a commit 8d15444
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 0 deletions.
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)
}

0 comments on commit 8d15444

Please sign in to comment.