-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoken_api_test.go
More file actions
220 lines (182 loc) · 5.35 KB
/
token_api_test.go
File metadata and controls
220 lines (182 loc) · 5.35 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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package moneyloverapi
import (
"context"
"errors"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
clock "go.nhat.io/clock/mock"
"github.com/nhatthm/moneyloverapi/pkg/auth"
"github.com/nhatthm/moneyloverapi/pkg/testkit"
authMock "github.com/nhatthm/moneyloverapi/pkg/testkit/auth"
)
func TestApiTokenProvider_GetToken(t *testing.T) {
t.Parallel()
username := "user@example.org"
password := "123456"
cred := Credentials(username, password)
storageKey := username
testCases := []struct {
scenario string
mockServer testkit.ServerMocker
configure func(t *testing.T, p *apiTokenProvider)
expectedError string
}{
{
scenario: "could not get token from storage",
mockServer: testkit.MockEmptyServer(),
configure: func(t *testing.T, p *apiTokenProvider) { //nolint: thelper
s := authMock.MockTokenStorage(func(s *authMock.TokenStorage) {
s.On("Get", context.Background(), storageKey).
Return(auth.OAuthToken{}, errors.New("get token error"))
})(t)
p.WithStorage(s)
},
expectedError: "could not get token from storage: get token error",
},
{
scenario: "could not get login url",
mockServer: testkit.MockEmptyServer(
testkit.WithAuthLoginURLFailure(),
),
expectedError: "unexpected response: unexpected response status: 500 Internal Server Error",
},
{
scenario: "could not login",
mockServer: testkit.MockEmptyServer(
testkit.WithAuthLoginURLSuccess(),
testkit.WithAuthTokenFailure(username, password),
),
expectedError: "unexpected response: unexpected response status: 401 Unauthorized",
},
{
scenario: "could not set token",
mockServer: testkit.MockEmptyServer(
testkit.WithAuthLoginURLSuccess(),
testkit.WithAuthTokenSuccess(username, password),
),
configure: func(t *testing.T, p *apiTokenProvider) { //nolint: thelper
s := authMock.MockTokenStorage(func(s *authMock.TokenStorage) {
s.On("Get", context.Background(), storageKey).
Return(auth.OAuthToken{}, nil)
s.On("Set", context.Background(), storageKey, mock.Anything).
Return(errors.New("set token error"))
})(t)
p.WithStorage(s)
},
expectedError: "could not persist token to storage: set token error",
},
{
scenario: "success",
mockServer: testkit.MockEmptyServer(
testkit.WithAuthLoginURLSuccess(),
testkit.WithAuthTokenSuccess(username, password),
),
},
}
for _, tc := range testCases {
tc := tc
t.Run(tc.scenario, func(t *testing.T) {
t.Parallel()
s := tc.mockServer(t)
p := newAPITokenProvider(cred).
WithBaseURL(s.URL()).
WithTimeout(time.Second)
if tc.configure != nil {
tc.configure(t, p)
}
token, err := p.Token(context.Background())
if tc.expectedError == "" {
assert.Equal(t, s.AccessToken(), token)
assert.NoError(t, err)
} else {
assert.Empty(t, token)
assert.EqualError(t, err, tc.expectedError)
}
})
}
}
func TestApiTokenProvider_GetToken_MissingCredentials(t *testing.T) {
t.Parallel()
testCases := []struct {
scenario string
credentials CredentialsProvider
expectedError string
}{
{
scenario: "missing username",
credentials: Credentials("", "password"),
expectedError: "could not get token: missing username",
},
{
scenario: "missing password",
credentials: Credentials("username", ""),
expectedError: "unexpected response: unexpected response status: 403 Forbidden",
},
}
for _, tc := range testCases {
tc := tc
t.Run(tc.scenario, func(t *testing.T) {
t.Parallel()
p := newAPITokenProvider(tc.credentials)
token, err := p.Token(context.Background())
assert.Empty(t, token)
assert.EqualError(t, err, tc.expectedError)
})
}
}
func TestApiTokenProvider_GetTokenFromCache(t *testing.T) {
t.Parallel()
username := "user@example.org"
password := "123456"
cred := Credentials(username, password)
timestamp := time.Now()
s := testkit.MockServer(username, password)(t)
c := clock.Mock(func(c *clock.Clock) {
c.On("Now").Return(timestamp.Add(4 * time.Minute)).Once()
})(t)
p := newAPITokenProvider(cred).
WithBaseURL(s.URL()).
WithTimeout(time.Second).
WithClock(c)
// 1st try.
token, err := p.Token(context.Background())
assert.Equal(t, s.AccessToken(), token)
assert.NotEmpty(t, string(token))
assert.NoError(t, err)
// 2nd try.
token, err = p.Token(context.Background())
assert.Equal(t, s.AccessToken(), token)
assert.NotEmpty(t, string(token))
assert.NoError(t, err)
}
func TestApiTokenProvider_TokenExpired(t *testing.T) {
t.Parallel()
username := "user@example.org"
password := "123456"
cred := Credentials(username, password)
timestamp := time.Now()
s := testkit.MockEmptyServer(
testkit.WithAuthSuccess(username, password),
testkit.WithAuthSuccess(username, password),
)(t)
c := clock.Mock(func(c *clock.Clock) {
c.On("Now").Return(timestamp.AddDate(0, 0, 30)).Once()
})(t)
p := newAPITokenProvider(cred).
WithBaseURL(s.URL()).
WithTimeout(time.Second).
WithClock(c)
// 1st try.
token1, err := p.Token(context.Background())
assert.Equal(t, s.AccessToken(), token1)
assert.NotEmpty(t, string(token1))
assert.NoError(t, err)
// 2nd try.
token2, err := p.Token(context.Background())
assert.Equal(t, s.AccessToken(), token2)
assert.NotEqual(t, token1, token2)
assert.NotEmpty(t, string(token2))
assert.NoError(t, err)
}