github.com/clerkinc/clerk-sdk-go@v1.49.1/clerk/clients_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 TestClientsService_ListAll_happyPath(t *testing.T) { 12 client, mux, _, teardown := setup("token") 13 defer teardown() 14 15 expectedResponse := "[" + dummyClientResponseJson + "]" 16 17 mux.HandleFunc("/clients", func(w http.ResponseWriter, req *http.Request) { 18 testHttpMethod(t, req, "GET") 19 testHeader(t, req, "Authorization", "Bearer token") 20 fmt.Fprint(w, expectedResponse) 21 }) 22 23 var want []ClientResponse 24 _ = json.Unmarshal([]byte(expectedResponse), &want) 25 26 got, _ := client.Clients().ListAll() 27 if len(got) != len(want) { 28 t.Errorf("Was expecting %d clients to be returned, instead got %d", len(want), len(got)) 29 } 30 31 if !reflect.DeepEqual(got, want) { 32 t.Errorf("Response = %v, want %v", got, want) 33 } 34 } 35 36 func TestClientsService_ListAll_invalidServer(t *testing.T) { 37 client, _ := NewClient("token") 38 39 response, err := client.Clients().ListAll() 40 if err == nil { 41 t.Errorf("Expected error to be returned") 42 } 43 if response != nil { 44 t.Errorf("Was not expecting any clients to be returned, instead got %v", response) 45 } 46 } 47 48 func TestClientsService_Read_happyPath(t *testing.T) { 49 token := "token" 50 clientId := "someClientId" 51 expectedResponse := dummyClientResponseJson 52 53 client, mux, _, teardown := setup(token) 54 defer teardown() 55 56 mux.HandleFunc("/clients/"+clientId, func(w http.ResponseWriter, req *http.Request) { 57 testHttpMethod(t, req, "GET") 58 testHeader(t, req, "Authorization", "Bearer "+token) 59 fmt.Fprint(w, expectedResponse) 60 }) 61 62 var want ClientResponse 63 _ = json.Unmarshal([]byte(expectedResponse), &want) 64 65 got, _ := client.Clients().Read(clientId) 66 if !reflect.DeepEqual(*got, want) { 67 t.Errorf("Response = %v, want %v", *got, want) 68 } 69 } 70 71 func TestClientsService_Read_invalidServer(t *testing.T) { 72 client, _ := NewClient("token") 73 74 response, err := client.Clients().Read("someClientId") 75 if err == nil { 76 t.Errorf("Expected error to be returned") 77 } 78 if response != nil { 79 t.Errorf("Was not expecting any client to be returned, instead got %v", response) 80 } 81 } 82 83 func TestClientsService_Verify_happyPath(t *testing.T) { 84 token := "token" 85 sessionToken := "sessionToken" 86 expectedResponse := dummyClientResponseJson 87 88 client, mux, _, teardown := setup(token) 89 defer teardown() 90 91 mux.HandleFunc("/clients/verify", func(w http.ResponseWriter, req *http.Request) { 92 testHttpMethod(t, req, "POST") 93 testHeader(t, req, "Authorization", "Bearer "+token) 94 fmt.Fprint(w, expectedResponse) 95 }) 96 97 var want ClientResponse 98 _ = json.Unmarshal([]byte(expectedResponse), &want) 99 100 got, _ := client.Clients().Verify(sessionToken) 101 if !reflect.DeepEqual(*got, want) { 102 t.Errorf("Response = %v, want %v", *got, want) 103 } 104 } 105 106 func TestClientsService_Verify_invalidServer(t *testing.T) { 107 client, _ := NewClient("token") 108 109 response, err := client.Clients().Verify("someSessionToken") 110 if err == nil { 111 t.Errorf("Expected error to be returned") 112 } 113 if response != nil { 114 t.Errorf("Was not expecting any client to be returned, instead got %v", response) 115 } 116 } 117 118 const dummyClientResponseJson = `{ 119 "ended": false, 120 "id": "client_1mvnkzXhKhn9pDjp1f4x1id6pQZ", 121 "last_active_session_id": "sess_1mebQdHlQI14cjxln4e2eXNzwzi", 122 "session_ids": ["sess_1mebQdHlQI14cjxln4e2eXNzwzi"], 123 "sessions": [{ 124 "id": "sess_1mebQdHlQI14cjxln4e2eXNzwzi", 125 "abandon_at": 1612448988, 126 "client_id": "client_1mebPYz8NFNA17fi7NemNXIwp1p", 127 "expire_at": 1610461788, 128 "last_active_at": 1609857251, 129 "object": "session", 130 "status": "ended", 131 "user_id": "user_1mebQggrD3xO5JfuHk7clQ94ysA" 132 }], 133 "object": "client" 134 }`