github.com/clerkinc/clerk-sdk-go@v1.49.1/clerk/allowlists_test.go (about) 1 package clerk 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "net/http" 7 "testing" 8 9 "github.com/stretchr/testify/assert" 10 ) 11 12 func TestAllowlistService_CreateIdentifier_happyPath(t *testing.T) { 13 token := "token" 14 var allowlistIdentifier AllowlistIdentifierResponse 15 _ = json.Unmarshal([]byte(dummyAllowlistIdentifierJson), &allowlistIdentifier) 16 17 client, mux, _, teardown := setup(token) 18 defer teardown() 19 20 mux.HandleFunc("/allowlist_identifiers", func(w http.ResponseWriter, req *http.Request) { 21 testHttpMethod(t, req, http.MethodPost) 22 testHeader(t, req, "Authorization", "Bearer "+token) 23 fmt.Fprint(w, dummyAllowlistIdentifierJson) 24 }) 25 26 got, _ := client.Allowlists().CreateIdentifier(CreateAllowlistIdentifierParams{ 27 Identifier: allowlistIdentifier.Identifier, 28 }) 29 30 assert.Equal(t, &allowlistIdentifier, got) 31 } 32 33 func TestAllowlistService_CreateIdentifier_invalidServer(t *testing.T) { 34 client, _ := NewClient("token") 35 36 _, err := client.Allowlists().CreateIdentifier(CreateAllowlistIdentifierParams{ 37 Identifier: "dummy@example.com", 38 }) 39 if err == nil { 40 t.Errorf("Expected error to be returned") 41 } 42 } 43 44 func TestAllowlistService_DeleteIdentifier_happyPath(t *testing.T) { 45 token := "token" 46 var allowlistIdentifier BlocklistIdentifierResponse 47 _ = json.Unmarshal([]byte(dummyBlocklistIdentifierJson), &allowlistIdentifier) 48 49 client, mux, _, teardown := setup(token) 50 defer teardown() 51 52 mux.HandleFunc("/allowlist_identifiers/"+allowlistIdentifier.ID, func(w http.ResponseWriter, req *http.Request) { 53 testHttpMethod(t, req, http.MethodDelete) 54 testHeader(t, req, "Authorization", "Bearer "+token) 55 response := fmt.Sprintf(`{ "deleted": true, "id": "%s", "object": "allowlist_identifier" }`, allowlistIdentifier.ID) 56 _, _ = fmt.Fprint(w, response) 57 }) 58 59 got, _ := client.Allowlists().DeleteIdentifier(allowlistIdentifier.ID) 60 assert.Equal(t, allowlistIdentifier.ID, got.ID) 61 assert.True(t, got.Deleted) 62 } 63 64 func TestAllowlistService_DeleteIdentifier_invalidServer(t *testing.T) { 65 client, _ := NewClient("token") 66 67 _, err := client.Allowlists().DeleteIdentifier("alid_1mvFol71HiKCcypBd6xxg0IpMBN") 68 if err == nil { 69 t.Errorf("Expected error to be returned") 70 } 71 } 72 73 func TestAllowlistService_ListAllIdentifiers_happyPath(t *testing.T) { 74 token := "token" 75 var allowlistIdentifier AllowlistIdentifierResponse 76 _ = json.Unmarshal([]byte(dummyAllowlistIdentifierJson), &allowlistIdentifier) 77 78 client, mux, _, teardown := setup(token) 79 defer teardown() 80 81 mux.HandleFunc("/allowlist_identifiers", func(w http.ResponseWriter, req *http.Request) { 82 testHttpMethod(t, req, http.MethodGet) 83 testHeader(t, req, "Authorization", "Bearer "+token) 84 fmt.Fprint(w, fmt.Sprintf(`[%s]`, dummyAllowlistIdentifierJson)) 85 }) 86 87 got, _ := client.Allowlists().ListAllIdentifiers() 88 89 assert.Len(t, got.Data, 1) 90 assert.Equal(t, int64(1), got.TotalCount) 91 assert.Equal(t, &allowlistIdentifier, got.Data[0]) 92 } 93 94 func TestAllowlistService_ListAllIdentifiers_invalidServer(t *testing.T) { 95 client, _ := NewClient("token") 96 97 _, err := client.Allowlists().ListAllIdentifiers() 98 if err == nil { 99 t.Errorf("Expected error to be returned") 100 } 101 } 102 103 const dummyAllowlistIdentifierJson = `{ 104 "id": "alid_1mvFol71HiKCcypBd6xxg0IpMBN", 105 "object": "allowlist_identifier", 106 "identifier": "dummy@example.com", 107 "identifier_type": "email_address", 108 "invitation_id": "inv_1mvFol71PeRCcypBd628g0IuRmF", 109 "created_at": 1610783813, 110 "updated_at": 1610783813 111 }`