/
OAuth2AccessTokenSpecs.groovy
107 lines (75 loc) · 3.9 KB
/
OAuth2AccessTokenSpecs.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
package de.rheinfabrik.heimdall2
import com.google.gson.Gson
import spock.lang.Specification
import spock.lang.Title
@Title("Specs for serialization in the OAuth2AccessToken class.")
class OAuth2AccessTokenSerializationSpecs extends Specification {
// Setup
def setup() {
// Set default locale and time zone
Locale.setDefault(Locale.GERMANY)
TimeZone.setDefault(TimeZone.getTimeZone("CEST"))
}
// Scenarios
def "It should create the correct JSON for a given OAuth2AccessToken"() {
given: "An OAuth2AccessToken"
OAuth2AccessToken accessToken = new OAuth2AccessToken(refreshToken: "rt", expiresIn: 3600, accessToken: "at", tokenType: "bearer")
accessToken.expirationDate = Calendar.getInstance()
accessToken.expirationDate.setTimeInMillis(0)
when: "I serialize it with Gson"
String json = new Gson().toJson(accessToken)
then: "The JSON should be as expected"
json == "{\"token_type\":\"bearer\",\"access_token\":\"at\",\"refresh_token\":\"rt\",\"expires_in\":3600,\"heimdall_expiration_date\":{\"year\":1970,\"month\":0,\"dayOfMonth\":1,\"hourOfDay\":0,\"minute\":0,\"second\":0}}"
}
def "It should create the correct OAuth2AccessToken for a given JSON"() {
given: "Some JSON representing an OAuth2AccessToken"
String json = "{\"access_token\":\"at\",\"heimdall_expiration_date\":{\"year\":1970,\"month\":0,\"dayOfMonth\":1,\"hourOfDay\":0,\"minute\":0,\"second\":0},\"expires_in\":3600,\"refresh_token\":\"rt\",\"token_type\":\"bearer\"}"
when: "I deserialize it with Gson"
OAuth2AccessToken accessToken = new Gson().fromJson(json, OAuth2AccessToken.class)
then: "The OAuth2AccessToken should be as expected"
accessToken.refreshToken == "rt"
accessToken.expiresIn == 3600
accessToken.accessToken == "at"
accessToken.tokenType == "bearer"
Calendar calendar = Calendar.getInstance()
calendar.setTimeInMillis(0)
accessToken.expirationDate.timeInMillis == calendar.timeInMillis
}
}
@Title("Specs for the isExpired() function in the OAuth2AccessToken class.")
class OAuth2AccessTokenIsExpiredSpecs extends Specification {
// Scenarios
@SuppressWarnings("GroovyPointlessBoolean")
def "It should return false if the expirationDate is null"() {
given: "An OAuth2AccessToken with null as expirationDate"
OAuth2AccessToken accessToken = new OAuth2AccessToken(expirationDate: null)
when: "I check if the access token is expired"
boolean isExpired = accessToken.isExpired()
then: "It should be true"
isExpired == false
}
@SuppressWarnings("GroovyPointlessBoolean")
def "It should return false if the expirationDate is in the future"() {
given: "A date which is in the future"
Calendar future = Calendar.getInstance()
future.add(Calendar.YEAR, 1)
and: "An OAuth2AccessToken with that future as expirationDate"
OAuth2AccessToken accessToken = new OAuth2AccessToken(expirationDate: future)
when: "I check if the access token is expired"
boolean isExpired = accessToken.isExpired()
then: "It should be false"
isExpired == false
}
@SuppressWarnings("GroovyPointlessBoolean")
def "It should return true if the expirationDate is in the past"() {
given: "A date which is in the past"
Calendar past = Calendar.getInstance()
past.add(Calendar.YEAR, -1)
and: "An OAuth2AccessToken with that past as expirationDate"
OAuth2AccessToken accessToken = new OAuth2AccessToken(expirationDate: past)
when: "I check if the access token is expired"
boolean isExpired = accessToken.isExpired()
then: "It should be true"
isExpired == true
}
}