/
OAuth2AccessTokenManagerSpecs.groovy
301 lines (211 loc) · 11.4 KB
/
OAuth2AccessTokenManagerSpecs.groovy
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package de.rheinfabrik.heimdall
import com.andrewreitz.spock.android.AndroidSpecification
import de.rheinfabrik.heimdall.grants.OAuth2Grant
import de.rheinfabrik.heimdall.grants.OAuth2RefreshAccessTokenGrant
import spock.lang.Title
import java.util.concurrent.TimeUnit
import static rx.Single.just
@Title("Tests for the constructor of the OAuth2AccessTokenManager class")
class OAuth2AccessTokenManagerConstructorSpecs extends AndroidSpecification {
// Scenarios
@SuppressWarnings(["GroovyResultOfObjectAllocationIgnored", "GroovyAssignabilityCheck"])
def "it should throw an exception if the storage argument is null"() {
given: "A null storage"
OAuth2AccessTokenStorage storage = null
when: "I initialize an OAuth2AccessTokenManager with a that storage"
new OAuth2AccessTokenManager(storage)
then: "An IllegalArgumentException is thrown"
thrown(IllegalArgumentException)
}
}
@Title("Tests for the grantNewAccessToken() function of the OAuth2AccessTokenManager class")
class OAuth2AccessTokenManagerGrantNewAccessTokenSpecs extends AndroidSpecification {
// Scenarios
def "it should throw an IllegalArgumentException when the grant parameter is null"() {
given: "A null grant"
OAuth2Grant grant = null
and: "An OAuth2AccessTokenManager"
OAuth2AccessTokenManager tokenManager = new OAuth2AccessTokenManager<OAuth2AccessToken>(Mock(OAuth2AccessTokenStorage))
when: "I ask for a new access token"
tokenManager.grantNewAccessToken(grant)
then: "An IllegalArgumentException is thrown"
thrown(IllegalArgumentException)
}
def "it should generate and set the correct expiration date"() {
given: "An OAuth2AccessToken"
OAuth2AccessToken accessToken = new OAuth2AccessToken(expirationDate: null)
accessToken.expiresIn = 3
and: "A grant emitting that token"
OAuth2Grant grant = Mock(OAuth2Grant)
grant.grantNewAccessToken() >> just(accessToken)
and: "An OAuth2AccessTokenManager"
OAuth2AccessTokenManager tokenManager = new OAuth2AccessTokenManager<OAuth2AccessToken>(Mock(OAuth2AccessTokenStorage))
and: "A calendar instance"
Calendar calendar = Calendar.getInstance()
when: "I ask for a new access token"
OAuth2AccessToken newToken = tokenManager.grantNewAccessToken(grant, calendar).toBlocking().value()
then: "The access token should have the correct expiration date"
newToken.expirationDate.timeInMillis == calendar.getTimeInMillis() + 3000
}
def "it should NOT generate and set the correct expiration date if expiresIn is null"() {
given: "An OAuth2AccessToken"
OAuth2AccessToken accessToken = new OAuth2AccessToken(expirationDate: null)
accessToken.expiresIn = null
and: "A grant emitting that token"
OAuth2Grant grant = Mock(OAuth2Grant)
grant.grantNewAccessToken() >> just(accessToken)
and: "An OAuth2AccessTokenManager"
OAuth2AccessTokenManager tokenManager = new OAuth2AccessTokenManager<OAuth2AccessToken>(Mock(OAuth2AccessTokenStorage))
when: "I ask for a new access token"
OAuth2AccessToken newToken = tokenManager.grantNewAccessToken(grant).toBlocking().value()
then: "The access token should have the NO expiration date"
newToken.expirationDate == null
}
def "it should store the access token"() {
given: "An OAuth2AccessToken"
OAuth2AccessToken accessToken = new OAuth2AccessToken()
and: "A grant emitting that token"
OAuth2Grant grant = Mock(OAuth2Grant)
grant.grantNewAccessToken() >> just(accessToken)
and: "A mock storage"
OAuth2AccessTokenStorage storage = Mock(OAuth2AccessTokenStorage)
and: "An OAuth2AccessTokenManager"
OAuth2AccessTokenManager tokenManager = new OAuth2AccessTokenManager<OAuth2AccessToken>(storage)
when: "I ask for a new access token"
tokenManager.grantNewAccessToken(grant).toBlocking().value()
then: "The storage is asked to save the token"
1 * storage.storeAccessToken(accessToken)
}
}
@Title("Tests for the getStorage() function of the OAuth2AccessTokenManager class")
class OAuth2AccessTokenManagerGetStorageSpecs extends AndroidSpecification {
// Scenarios
def "it should return the correct storage"() {
given: "A mock storage"
OAuth2AccessTokenStorage storage = Mock(OAuth2AccessTokenStorage)
and: "An OAuth2AccessTokenManager"
OAuth2AccessTokenManager tokenManager = new OAuth2AccessTokenManager<OAuth2AccessToken>(storage)
when: "I ask for the storage"
OAuth2AccessTokenStorage receivedStorage = tokenManager.getStorage()
then: "I have the storage I once passed via the constructor"
receivedStorage == storage
}
}
@Title("Tests for the getValidAccessToken() function of the OAuth2AccessTokenManager class")
class OAuth2AccessTokenManagerGetValidAccessTokenSpecs extends AndroidSpecification {
// Scenarios
def "it should only request a new valid token ONCE"() {
given: "An expired OAuth2AccessToken"
OAuth2AccessToken accessToken = Mock(OAuth2AccessToken)
accessToken.refreshToken = "rt"
accessToken.isExpired() >> true
and: "A mock storage emitting that token"
OAuth2AccessTokenStorage storage = Mock(OAuth2AccessTokenStorage)
storage.getStoredAccessToken() >> just(accessToken)
and: "An OAuth2AccessTokenManager with that storage"
OAuth2AccessTokenManager tokenManager = new OAuth2AccessTokenManager<OAuth2AccessToken>(storage)
and: "A mock grant"
OAuth2RefreshAccessTokenGrant grant = Mock(OAuth2RefreshAccessTokenGrant)
def counter = 0
grant.grantNewAccessToken() >> { it ->
return just(accessToken).doOnSuccess({ x -> counter++ }).delay(1, TimeUnit.SECONDS)
}
when: "I ask for a valid access token"
tokenManager.getValidAccessToken(grant).subscribe()
and: "I ask again"
tokenManager.getValidAccessToken(grant).subscribe()
and: "I wait 2 seconds"
sleep(2000)
then: "The refresh grant is asked for a new token ONCE"
counter == 1
}
def "it should clear the current request once done"() {
given: "An expired OAuth2AccessToken"
OAuth2AccessToken accessToken = Mock(OAuth2AccessToken)
accessToken.refreshToken = "rt"
accessToken.isExpired() >> true
and: "A mock storage emitting that token"
OAuth2AccessTokenStorage storage = Mock(OAuth2AccessTokenStorage)
storage.getStoredAccessToken() >> just(accessToken)
and: "An OAuth2AccessTokenManager with that storage"
OAuth2AccessTokenManager tokenManager = new OAuth2AccessTokenManager<OAuth2AccessToken>(storage)
and: "A mock grant"
OAuth2RefreshAccessTokenGrant grant = Mock(OAuth2RefreshAccessTokenGrant)
def counter = 0
grant.grantNewAccessToken() >> { it ->
return just(accessToken).doOnSuccess({ x -> counter++ }).delay(1, TimeUnit.SECONDS)
}
when: "I ask for a valid access token"
tokenManager.getValidAccessToken(grant).subscribe()
and: "I ask again"
tokenManager.getValidAccessToken(grant).subscribe()
and: "I wait 2 seconds"
sleep(2000)
and: "I ask again"
tokenManager.getValidAccessToken(grant).subscribe()
then: "The refresh grant is asked for a new token TWICE"
counter == 2
}
def "it should throw an IllegalArgumentException when the refreshAccessTokenGrant parameter is null"() {
given: "A null grant"
OAuth2RefreshAccessTokenGrant grant = null
and: "An OAuth2AccessTokenManager"
OAuth2AccessTokenManager tokenManager = new OAuth2AccessTokenManager<OAuth2AccessToken>(Mock(OAuth2AccessTokenStorage))
when: "I ask for a valid access token"
tokenManager.getValidAccessToken(grant)
then: "An IllegalArgumentException is thrown"
thrown(IllegalArgumentException)
}
def "it should emit the non-expired stored access token"() {
given: "A non-expired OAuth2AccessToken"
OAuth2AccessToken accessToken = Mock(OAuth2AccessToken)
accessToken.isExpired() >> false
and: "A mock storage emitting that token"
OAuth2AccessTokenStorage storage = Mock(OAuth2AccessTokenStorage)
storage.getStoredAccessToken() >> just(accessToken)
and: "A mock grant"
OAuth2RefreshAccessTokenGrant grant = Mock(OAuth2RefreshAccessTokenGrant)
grant.grantNewAccessToken() >> just(accessToken)
and: "An OAuth2AccessTokenManager with that storage"
OAuth2AccessTokenManager tokenManager = new OAuth2AccessTokenManager<OAuth2AccessToken>(storage)
when: "I ask for a valid access token"
OAuth2AccessToken validToken = tokenManager.getValidAccessToken(grant).toBlocking().value()
then: "The I receive the non-expired token"
validToken == accessToken
}
@SuppressWarnings("GroovyAssignabilityCheck")
def "it should ask to refresh the token if the token is expired"() {
given: "An expired OAuth2AccessToken"
OAuth2AccessToken accessToken = Mock(OAuth2AccessToken)
accessToken.isExpired() >> true
and: "A mock storage emitting that token"
OAuth2AccessTokenStorage storage = Mock(OAuth2AccessTokenStorage)
storage.getStoredAccessToken() >> just(accessToken)
and: "An OAuth2AccessTokenManager with that storage"
OAuth2AccessTokenManager tokenManager = new OAuth2AccessTokenManager<OAuth2AccessToken>(storage)
and: "A mock grant"
OAuth2RefreshAccessTokenGrant grant = Mock(OAuth2RefreshAccessTokenGrant)
when: "I ask for a valid access token"
tokenManager.getValidAccessToken(grant).subscribe()
then: "The refresh grant is asked for a new token"
1 * grant.grantNewAccessToken() >> just(accessToken)
}
def "it should set the refresh token to the grant if the token is expired"() {
given: "An expired OAuth2AccessToken"
OAuth2AccessToken accessToken = Mock(OAuth2AccessToken)
accessToken.refreshToken = "rt"
accessToken.isExpired() >> true
and: "A mock storage emitting that token"
OAuth2AccessTokenStorage storage = Mock(OAuth2AccessTokenStorage)
storage.getStoredAccessToken() >> just(accessToken)
and: "An OAuth2AccessTokenManager with that storage"
OAuth2AccessTokenManager tokenManager = new OAuth2AccessTokenManager<OAuth2AccessToken>(storage)
and: "A mock grant"
OAuth2RefreshAccessTokenGrant grant = Mock(OAuth2RefreshAccessTokenGrant)
grant.grantNewAccessToken() >> just(accessToken)
when: "I ask for a valid access token"
tokenManager.getValidAccessToken(grant).subscribe()
then: "The refresh grant is asked for a new token"
grant.refreshToken == accessToken.refreshToken
}
}