-
Notifications
You must be signed in to change notification settings - Fork 3
/
client_test.go
118 lines (93 loc) · 2.71 KB
/
client_test.go
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package solus
import (
"net/http"
"net/http/httptest"
"net/url"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestAllowInsecure(t *testing.T) {
c := &Client{
HTTPClient: &http.Client{
Transport: &http.Transport{},
},
}
AllowInsecure()(c)
require.True(t, c.HTTPClient.Transport.(*http.Transport).TLSClientConfig.InsecureSkipVerify)
}
func TestSetRetryPolicy(t *testing.T) {
c := &Client{}
SetRetryPolicy(1, time.Second)(c)
assert.Equal(t, 1, c.Retries)
assert.Equal(t, time.Second, c.RetryAfter)
}
type fakeLogger struct{}
func (fakeLogger) Debugf(string, ...interface{}) {}
func (fakeLogger) Errorf(string, ...interface{}) {}
func TestWithLogger(t *testing.T) {
c := &Client{}
l := fakeLogger{}
WithLogger(l)(c)
assert.Equal(t, l, c.Logger)
}
func TestEmailAndPasswordAuthenticator_Authenticate(t *testing.T) {
authenticator := EmailAndPasswordAuthenticator{
Email: "[email protected]",
Password: "Pass80rd",
}
t.Run("positive", func(t *testing.T) {
credentials := Credentials{
AccessToken: "access token",
TokenType: "token type",
ExpiresAt: "expires at",
}
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodPost, r.Method)
assert.Equal(t, "/auth/login", r.URL.Path)
assertRequestBody(t, r, AuthLoginRequest{
Email: "[email protected]",
Password: "Pass80rd",
})
writeResponse(t, w, http.StatusOK, AuthLoginResponse{
Credentials: credentials,
})
}))
defer s.Close()
u, err := url.Parse(s.URL)
require.NoError(t, err)
c, err := NewClient(u, authenticator)
require.NoError(t, err)
require.Equal(t, credentials, c.Credentials)
})
t.Run("negative", func(t *testing.T) {
t.Run("failed to make request", func(t *testing.T) {
_, err := NewClient(&url.URL{}, authenticator, SetRetryPolicy(0, 0))
require.EqualError(t, err, `authenticate: Post "/auth/login": unsupported protocol scheme ""`)
})
t.Run("invalid status", func(t *testing.T) {
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusBadRequest)
}))
defer s.Close()
u, err := url.Parse(s.URL)
require.NoError(t, err)
_, err = NewClient(u, authenticator)
require.EqualError(t, err, "authenticate: HTTP POST auth/login returns 400 status code")
})
})
}
func TestAPITokenAuthenticator_Authenticate(t *testing.T) {
const token = "foo"
authenticator := APITokenAuthenticator{
Token: token,
}
c, err := NewClient(&url.URL{}, authenticator)
require.NoError(t, err)
require.Equal(t, Credentials{
AccessToken: token,
TokenType: "Bearer",
ExpiresAt: "",
}, c.Credentials)
}