diff --git a/clerk/clerk.go b/clerk/clerk.go index 0def6797..b8646579 100644 --- a/clerk/clerk.go +++ b/clerk/clerk.go @@ -30,6 +30,7 @@ const ( InvitationsURL = "invitations" OrganizationsUrl = "organizations" PhoneNumbersURL = "phone_numbers" + ProxyChecksURL = "proxy_checks" RedirectURLsUrl = "redirect_urls" SAMLConnectionsUrl = "saml_connections" SessionsUrl = "sessions" @@ -62,6 +63,7 @@ type Client interface { JWTTemplates() *JWTTemplatesService Organizations() *OrganizationsService PhoneNumbers() *PhoneNumbersService + ProxyChecks() *ProxyChecksService RedirectURLs() *RedirectURLsService SAMLConnections() *SAMLConnectionsService Sessions() *SessionsService @@ -97,6 +99,7 @@ type client struct { jwtTemplates *JWTTemplatesService organizations *OrganizationsService phoneNumbers *PhoneNumbersService + proxyChecks *ProxyChecksService redirectURLs *RedirectURLsService samlConnections *SAMLConnectionsService sessions *SessionsService @@ -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) @@ -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 } diff --git a/clerk/proxy_checks.go b/clerk/proxy_checks.go new file mode 100644 index 00000000..2202b615 --- /dev/null +++ b/clerk/proxy_checks.go @@ -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, ¶ms) + var proxyCheck ProxyCheck + _, err := s.client.Do(req, &proxyCheck) + if err != nil { + return nil, err + } + return &proxyCheck, nil +} diff --git a/clerk/proxy_checks_test.go b/clerk/proxy_checks_test.go new file mode 100644 index 00000000..db8473c4 --- /dev/null +++ b/clerk/proxy_checks_test.go @@ -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), ¶ms) + + 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 +}` diff --git a/tests/integration/proxy_checks_test.go b/tests/integration/proxy_checks_test.go new file mode 100644 index 00000000..549c314c --- /dev/null +++ b/tests/integration/proxy_checks_test.go @@ -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.NoError(t, err) + + // Now trigger a proxy check. Most likely a proxy is not configured. + _, err = client.ProxyChecks().Create(clerk.CreateProxyCheckParams{ + DomainID: domain.ID, + ProxyURL: "https://" + domainName + "/__clerk", + }) + assert.Error(t, err) +}