github.com/clerkinc/clerk-sdk-go@v1.49.1/clerk/webhooks_test.go (about) 1 package clerk 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "net/http" 7 "reflect" 8 "testing" 9 ) 10 11 func TestWebhooksService_CreateSvix_happyPath(t *testing.T) { 12 client, mux, _, teardown := setup("token") 13 defer teardown() 14 15 expectedResponse := dummySvixResponseJson 16 17 mux.HandleFunc("/webhooks/svix", func(w http.ResponseWriter, req *http.Request) { 18 testHttpMethod(t, req, "POST") 19 testHeader(t, req, "Authorization", "Bearer token") 20 fmt.Fprint(w, expectedResponse) 21 }) 22 23 var want SvixResponse 24 _ = json.Unmarshal([]byte(expectedResponse), &want) 25 26 got, _ := client.Webhooks().CreateSvix() 27 if !reflect.DeepEqual(*got, want) { 28 t.Errorf("response = %v, want %v", got, want) 29 } 30 } 31 32 func TestWebhooksService_CreateSvix_invalidServer(t *testing.T) { 33 client, _ := NewClient("token") 34 35 svixResponse, err := client.Webhooks().CreateSvix() 36 if err == nil { 37 t.Errorf("expected error to be returned") 38 } 39 if svixResponse != nil { 40 t.Errorf("was not expecting any users to be returned, instead got %v", svixResponse) 41 } 42 } 43 44 func TestWebhooksService_DeleteSvix_happyPath(t *testing.T) { 45 client, mux, _, teardown := setup("token") 46 defer teardown() 47 48 mux.HandleFunc("/webhooks/svix", func(w http.ResponseWriter, req *http.Request) { 49 testHttpMethod(t, req, "DELETE") 50 testHeader(t, req, "Authorization", "Bearer token") 51 w.WriteHeader(http.StatusNoContent) 52 }) 53 54 err := client.Webhooks().DeleteSvix() 55 if err != nil { 56 t.Errorf("was not expecting error, found %v instead", err) 57 } 58 } 59 60 func TestWebhooksService_DeleteSvix_invalidServer(t *testing.T) { 61 client, _ := NewClient("token") 62 63 err := client.Webhooks().DeleteSvix() 64 if err == nil { 65 t.Errorf("expected error to be returned") 66 } 67 } 68 69 func TestWebhooksService_RefreshSvixURL_happyPath(t *testing.T) { 70 client, mux, _, teardown := setup("token") 71 defer teardown() 72 73 expectedResponse := dummySvixResponseJson 74 75 mux.HandleFunc("/webhooks/svix_url", func(w http.ResponseWriter, req *http.Request) { 76 testHttpMethod(t, req, "POST") 77 testHeader(t, req, "Authorization", "Bearer token") 78 fmt.Fprint(w, expectedResponse) 79 }) 80 81 var want SvixResponse 82 _ = json.Unmarshal([]byte(expectedResponse), &want) 83 84 got, _ := client.Webhooks().RefreshSvixURL() 85 if !reflect.DeepEqual(*got, want) { 86 t.Errorf("response = %v, want %v", got, want) 87 } 88 } 89 90 func TestWebhooksService_RefreshSvixURL_invalidServer(t *testing.T) { 91 client, _ := NewClient("token") 92 93 svixResponse, err := client.Webhooks().RefreshSvixURL() 94 if err == nil { 95 t.Errorf("expected error to be returned") 96 } 97 if svixResponse != nil { 98 t.Errorf("was not expecting any users to be returned, instead got %v", svixResponse) 99 } 100 } 101 102 const dummySvixResponseJson = `{ 103 "svix_url": "http://example.svix.com" 104 }`