Skip to content

Commit

Permalink
feat: Proxy checks service
Browse files Browse the repository at this point in the history
Added a ProxyChecksService with a method to Create a proxy check.
Triggers a request to the Backend API POST /v1/proxy_checks endpoint.
  • Loading branch information
gkats committed Oct 25, 2023
1 parent d5e4e06 commit 8a58d46
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 0 deletions.
8 changes: 8 additions & 0 deletions clerk/clerk.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const (
InvitationsURL = "invitations"
OrganizationsUrl = "organizations"
PhoneNumbersURL = "phone_numbers"
ProxyChecksURL = "proxy_checks"
RedirectURLsUrl = "redirect_urls"
SAMLConnectionsUrl = "saml_connections"
SessionsUrl = "sessions"
Expand Down Expand Up @@ -62,6 +63,7 @@ type Client interface {
JWTTemplates() *JWTTemplatesService
Organizations() *OrganizationsService
PhoneNumbers() *PhoneNumbersService
ProxyChecks() *ProxyChecksService
RedirectURLs() *RedirectURLsService
SAMLConnections() *SAMLConnectionsService
Sessions() *SessionsService
Expand Down Expand Up @@ -97,6 +99,7 @@ type client struct {
jwtTemplates *JWTTemplatesService
organizations *OrganizationsService
phoneNumbers *PhoneNumbersService
proxyChecks *ProxyChecksService
redirectURLs *RedirectURLsService
samlConnections *SAMLConnectionsService
sessions *SessionsService
Expand Down Expand Up @@ -145,6 +148,7 @@ func NewClient(token string, options ...ClerkOption) (Client, error) {
client.jwtTemplates = (*JWTTemplatesService)(commonService)
client.organizations = (*OrganizationsService)(commonService)
client.phoneNumbers = (*PhoneNumbersService)(commonService)
client.proxyChecks = (*ProxyChecksService)(commonService)
client.redirectURLs = (*RedirectURLsService)(commonService)
client.samlConnections = (*SAMLConnectionsService)(commonService)
client.sessions = (*SessionsService)(commonService)
Expand Down Expand Up @@ -309,6 +313,10 @@ func (c *client) PhoneNumbers() *PhoneNumbersService {
return c.phoneNumbers
}

func (c *client) ProxyChecks() *ProxyChecksService {
return c.proxyChecks
}

func (c *client) RedirectURLs() *RedirectURLsService {
return c.redirectURLs
}
Expand Down
31 changes: 31 additions & 0 deletions clerk/proxy_checks.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package clerk

import "net/http"

type ProxyChecksService service

type ProxyCheck struct {
Object string `json:"object"`
ID string `json:"id"`
DomainID string `json:"domain_id"`
ProxyURL string `json:"proxy_url"`
Successful bool `json:"successful"`
LastRunAt *int64 `json:"last_run_at"`
CreatedAt int64 `json:"created_at"`
UpdatedAt int64 `json:"updated_at"`
}

type CreateProxyCheckParams struct {
DomainID string `json:"domain_id"`
ProxyURL string `json:"proxy_url"`
}

func (s *ProxyChecksService) Create(params CreateProxyCheckParams) (*ProxyCheck, error) {
req, _ := s.client.NewRequest(http.MethodPost, ProxyChecksURL, &params)
var proxyCheck ProxyCheck
_, err := s.client.Do(req, &proxyCheck)
if err != nil {
return nil, err
}
return &proxyCheck, nil
}
52 changes: 52 additions & 0 deletions clerk/proxy_checks_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package clerk

import (
"encoding/json"
"fmt"
"net/http"
"reflect"
"testing"
)

func TestProxyChecksService_Create(t *testing.T) {
client, mux, _, teardown := setup("token")
defer teardown()
var params CreateProxyCheckParams
payload := `{
"proxy_url":"https://example.com/__clerk",
"domain_id": "dmn_1mebQggrD3xO5JfuHk7clQ94ysA"
}`
_ = json.Unmarshal([]byte(payload), &params)

mux.HandleFunc("/proxy_checks", func(w http.ResponseWriter, req *http.Request) {
testHttpMethod(t, req, "POST")
testHeader(t, req, "Authorization", "Bearer token")
fmt.Fprint(w, dummyProxyCheckJSON)
})

got, err := client.ProxyChecks().Create(params)
if err != nil {
t.Fatal(err)
}

var want ProxyCheck
err = json.Unmarshal([]byte(dummyProxyCheckJSON), &want)
if err != nil {
t.Fatal(err)
}

if !reflect.DeepEqual(got, &want) {
t.Errorf("Response = %v, want %v", got, &want)
}
}

const dummyProxyCheckJSON = `{
"object": "proxy_check",
"id": "proxychk_1mebQggrD3xO5JfuHk7clQ94ysA",
"successful": true,
"domain_id": "dmn_1mebQggrD3xO5JfuHk7clQ94ysA",
"proxy_url": "https://example.com/__clerk",
"last_run_at": 1610783813,
"created_at": 1610783813,
"updated_at": 1610783813
}`
31 changes: 31 additions & 0 deletions tests/integration/proxy_checks_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//go:build integration
// +build integration

package integration

import (
"testing"

"github.com/brianvoe/gofakeit/v6"
"github.com/clerkinc/clerk-sdk-go/clerk"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestProxyChecks(t *testing.T) {
client := createClient()
// Create a domain first
domainName := gofakeit.DomainName()
domain, err := client.Domains().Create(clerk.CreateDomainParams{
Name: domainName,
IsSatellite: true,
})
require.Nil(t, err)

// Now trigger a proxy check. Most likely a proxy is not configured.
res, err := client.ProxyChecks().Create(clerk.CreateProxyCheckParams{

Check failure on line 26 in tests/integration/proxy_checks_test.go

View workflow job for this annotation

GitHub Actions / test (1.16.x, ubuntu-latest)

res declared but not used
DomainID: domain.ID,
ProxyURL: "https://" + domainName + "/__clerk",
})
assert.NotNil(t, err)
}

0 comments on commit 8a58d46

Please sign in to comment.