Repository navigation
Expand file tree
/
Copy pathclient_test.go
More file actions
78 lines (68 loc) · 1.55 KB
/
Copy pathclient_test.go
File metadata and controls
78 lines (68 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package widgets
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/require"
)
func TestGetToken(t *testing.T) {
tests := []struct {
scenario string
client *Client
options GetTokenOpts
expected string
err bool
}{
{
scenario: "Request without API Key returns an error",
client: &Client{},
err: true,
},
{
scenario: "Request returns widget token",
client: &Client{
APIKey: "test",
},
options: GetTokenOpts{
OrganizationId: "organization_id",
UserId: "user_id",
Scopes: []WidgetScope{UsersTableManage},
},
expected: "abc123456",
},
}
for _, test := range tests {
t.Run(test.scenario, func(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(getTokenTestHandler))
defer server.Close()
client := test.client
client.Endpoint = server.URL
client.HTTPClient = server.Client()
token, err := client.GetToken(context.Background(), test.options)
if test.err {
require.Error(t, err)
return
}
require.NoError(t, err)
require.Equal(t, test.expected, token)
})
}
}
func getTokenTestHandler(w http.ResponseWriter, r *http.Request) {
auth := r.Header.Get("Authorization")
if auth != "Bearer test" {
http.Error(w, "bad auth", http.StatusUnauthorized)
return
}
body, err := json.Marshal(struct {
GetTokenResponse
}{GetTokenResponse{Token: "abc123456"}})
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
w.Write(body)
}