-
Notifications
You must be signed in to change notification settings - Fork 315
Expand file tree
/
Copy pathaccess_token_test.go
More file actions
187 lines (162 loc) · 5.31 KB
/
access_token_test.go
File metadata and controls
187 lines (162 loc) · 5.31 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
package oauth_test
import (
"time"
"github.com/RichardKnop/go-oauth2-server/models"
"github.com/RichardKnop/uuid"
"github.com/stretchr/testify/assert"
)
func (suite *OauthTestSuite) TestGrantAccessToken() {
var (
accessToken *models.OauthAccessToken
err error
tokens []*models.OauthAccessToken
)
// Grant a client only access token
accessToken, err = suite.service.GrantAccessToken(
suite.clients[0], // client
nil, // user
3600, // expires in
"scope doesn't matter", // scope
)
// Error should be Nil
assert.Nil(suite.T(), err)
// Correct access token object should be returned
if assert.NotNil(suite.T(), accessToken) {
// Fetch all access tokens
models.OauthAccessTokenPreload(suite.db).Order("created_at").Find(&tokens)
// There should be just one right now
assert.Equal(suite.T(), 1, len(tokens))
// And the token should match the one returned by the grant method
assert.Equal(suite.T(), tokens[0].Token, accessToken.Token)
// Client id should be set
assert.True(suite.T(), tokens[0].ClientID.Valid)
assert.Equal(suite.T(), string(suite.clients[0].ID), tokens[0].ClientID.String)
// User id should be nil
assert.False(suite.T(), tokens[0].UserID.Valid)
}
// Grant a user specific access token
accessToken, err = suite.service.GrantAccessToken(
suite.clients[0], // client
suite.users[0], // user
3600, // expires in
"scope doesn't matter", // scope
)
// Error should be Nil
assert.Nil(suite.T(), err)
// Correct access token object should be returned
if assert.NotNil(suite.T(), accessToken) {
// Fetch all access tokens
models.OauthAccessTokenPreload(suite.db).Order("created_at").Find(&tokens)
// There should be 2 tokens now
assert.Equal(suite.T(), 2, len(tokens))
// And the second token should match the one returned by the grant method
assert.Equal(suite.T(), tokens[1].Token, accessToken.Token)
// Client id should be set
assert.True(suite.T(), tokens[1].ClientID.Valid)
assert.Equal(suite.T(), string(suite.clients[0].ID), tokens[1].ClientID.String)
// User id should be set
assert.True(suite.T(), tokens[1].UserID.Valid)
assert.Equal(suite.T(), string(suite.users[0].ID), tokens[1].UserID.String)
}
}
func (suite *OauthTestSuite) TestGrantAccessTokenDeletesExpiredTokens() {
var (
testAccessTokens = []*models.OauthAccessToken{
// Expired access token with a user
{
MyGormModel: models.MyGormModel{
ID: uuid.New(),
CreatedAt: time.Now().UTC(),
},
Token: "test_token_1",
ExpiresAt: time.Now().UTC().Add(-10 * time.Second),
Client: suite.clients[0],
User: suite.users[0],
},
// Expired access token without a user
{
MyGormModel: models.MyGormModel{
ID: uuid.New(),
CreatedAt: time.Now().UTC(),
},
Token: "test_token_2",
ExpiresAt: time.Now().UTC().Add(-10 * time.Second),
Client: suite.clients[0],
},
// Access token with a user
{
MyGormModel: models.MyGormModel{
ID: uuid.New(),
CreatedAt: time.Now().UTC(),
},
Token: "test_token_3",
ExpiresAt: time.Now().UTC().Add(+10 * time.Second),
Client: suite.clients[0],
User: suite.users[0],
},
// Access token without a user
{
MyGormModel: models.MyGormModel{
ID: uuid.New(),
CreatedAt: time.Now().UTC(),
},
Token: "test_token_4",
ExpiresAt: time.Now().UTC().Add(+10 * time.Second),
Client: suite.clients[0],
},
}
err error
notFound bool
existingTokens []string
)
// Insert test access tokens
for _, testAccessToken := range testAccessTokens {
err = suite.db.Create(testAccessToken).Error
assert.NoError(suite.T(), err, "Inserting test data failed")
}
// This should only delete test_token_1
_, err = suite.service.GrantAccessToken(
suite.clients[0], // client
suite.users[0], // user
3600, // expires in
"scope doesn't matter", // scope
)
assert.NoError(suite.T(), err)
// Check the test_token_1 was deleted
notFound = suite.db.Unscoped().Where("token = ?", "test_token_1").
First(new(models.OauthAccessToken)).RecordNotFound()
assert.True(suite.T(), notFound)
// Check the other three tokens are still around
existingTokens = []string{
"test_token_2",
"test_token_3",
"test_token_4",
}
for _, token := range existingTokens {
notFound = suite.db.Unscoped().Where("token = ?", token).
First(new(models.OauthAccessToken)).RecordNotFound()
assert.False(suite.T(), notFound)
}
// This should only delete test_token_2
_, err = suite.service.GrantAccessToken(
suite.clients[0], // client
nil, // user
3600, // expires in
"scope doesn't matter", // scope
)
assert.NoError(suite.T(), err)
// Check the test_token_2 was deleted
notFound = suite.db.Unscoped().Where("token = ?", "test_token_2").
First(new(models.OauthAccessToken)).RecordNotFound()
assert.True(suite.T(), notFound)
// Check that last two tokens are still around
existingTokens = []string{
"test_token_3",
"test_token_4",
}
for _, token := range existingTokens {
notFound := suite.db.Unscoped().Where("token = ?", token).
First(new(models.OauthAccessToken)).RecordNotFound()
assert.False(suite.T(), notFound)
}
}