-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathauthorization_code_refresh_tokens_projection_test.go
More file actions
162 lines (143 loc) · 6.06 KB
/
authorization_code_refresh_tokens_projection_test.go
File metadata and controls
162 lines (143 loc) · 6.06 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
package goauth2_test
import (
"testing"
"github.com/inklabs/rangedb/rangedbtest"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/inklabs/goauth2"
)
func TestAuthorizationCodeRefreshTokens(t *testing.T) {
// Given
const (
authorizationCode = "4da3044aa1ce4ef7a37c6a32375cf39a"
userID = "e2c27a06cd7c4003878979641e9e8288"
clientID = "17608d60710947d9803abc596184484b"
refreshToken1 = "3afb212eef99411d91f888cf826c289f"
refreshToken2 = "3754ba23f805431e83e0d50491311539"
scope = "read_write"
)
t.Run("authorization code is associated with one refresh token via authorization code grant", func(t *testing.T) {
// Given
authorizationCodeRefreshTokens := goauth2.NewAuthorizationCodeRefreshTokens()
record := rangedbtest.DummyRecordFromEvent(&goauth2.RefreshTokenWasIssuedToUserViaAuthorizationCodeGrant{
AuthorizationCode: authorizationCode,
ClientID: clientID,
UserID: userID,
RefreshToken: refreshToken1,
Scope: scope,
})
// When
authorizationCodeRefreshTokens.Accept(record)
// Then
tokens := authorizationCodeRefreshTokens.GetTokens(authorizationCode)
assert.Equal(t, []string{refreshToken1}, tokens)
actualAuthorizationCode, err := authorizationCodeRefreshTokens.GetAuthorizationCode(refreshToken1)
require.NoError(t, err)
assert.Equal(t, authorizationCode, actualAuthorizationCode)
})
t.Run("authorization code is associated with another refresh token via refresh token grant", func(t *testing.T) {
// Given
authorizationCodeRefreshTokens := goauth2.NewAuthorizationCodeRefreshTokens()
record1 := rangedbtest.DummyRecordFromEvent(&goauth2.RefreshTokenWasIssuedToUserViaAuthorizationCodeGrant{
AuthorizationCode: authorizationCode,
ClientID: clientID,
UserID: userID,
RefreshToken: refreshToken1,
Scope: scope,
})
record2 := rangedbtest.DummyRecordFromEvent(&goauth2.RefreshTokenWasIssuedToUserViaRefreshTokenGrant{
RefreshToken: refreshToken1,
UserID: userID,
ClientID: clientID,
NextRefreshToken: refreshToken2,
Scope: scope,
})
// When
authorizationCodeRefreshTokens.Accept(record1)
authorizationCodeRefreshTokens.Accept(record2)
// Then
assert.Equal(t, []string{refreshToken1, refreshToken2}, authorizationCodeRefreshTokens.GetTokens(authorizationCode))
actualAuthorizationCode1, err := authorizationCodeRefreshTokens.GetAuthorizationCode(refreshToken1)
require.NoError(t, err)
actualAuthorizationCode2, err := authorizationCodeRefreshTokens.GetAuthorizationCode(refreshToken2)
require.NoError(t, err)
assert.Equal(t, authorizationCode, actualAuthorizationCode1)
assert.Equal(t, authorizationCode, actualAuthorizationCode2)
})
t.Run("remove one revoked token to avoid memory leak", func(t *testing.T) {
// Given
authorizationCodeRefreshTokens := goauth2.NewAuthorizationCodeRefreshTokens()
record1 := rangedbtest.DummyRecordFromEvent(&goauth2.RefreshTokenWasIssuedToUserViaAuthorizationCodeGrant{
AuthorizationCode: authorizationCode,
ClientID: clientID,
UserID: userID,
RefreshToken: refreshToken1,
Scope: scope,
})
record2 := rangedbtest.DummyRecordFromEvent(&goauth2.RefreshTokenWasRevokedFromUser{
RefreshToken: refreshToken1,
UserID: userID,
ClientID: clientID,
})
// When
authorizationCodeRefreshTokens.Accept(record1)
authorizationCodeRefreshTokens.Accept(record2)
// Then
assert.Equal(t, []string(nil), authorizationCodeRefreshTokens.GetTokens(authorizationCode))
actualAuthorizationCode, err := authorizationCodeRefreshTokens.GetAuthorizationCode(refreshToken1)
assert.Equal(t, "", actualAuthorizationCode)
assert.Equal(t, goauth2.ErrAuthorizationCodeNotFound, err)
})
t.Run("remove two revoked tokens to avoid memory leak", func(t *testing.T) {
// Given
authorizationCodeRefreshTokens := goauth2.NewAuthorizationCodeRefreshTokens()
record1 := rangedbtest.DummyRecordFromEvent(&goauth2.RefreshTokenWasIssuedToUserViaAuthorizationCodeGrant{
AuthorizationCode: authorizationCode,
ClientID: clientID,
UserID: userID,
RefreshToken: refreshToken1,
Scope: scope,
})
record2 := rangedbtest.DummyRecordFromEvent(&goauth2.RefreshTokenWasIssuedToUserViaRefreshTokenGrant{
RefreshToken: refreshToken1,
UserID: userID,
ClientID: clientID,
NextRefreshToken: refreshToken2,
Scope: scope,
})
record3 := rangedbtest.DummyRecordFromEvent(&goauth2.RefreshTokenWasRevokedFromUser{
RefreshToken: refreshToken1,
UserID: userID,
ClientID: clientID,
})
record4 := rangedbtest.DummyRecordFromEvent(&goauth2.RefreshTokenWasRevokedFromUser{
RefreshToken: refreshToken2,
UserID: userID,
ClientID: clientID,
})
// When
authorizationCodeRefreshTokens.Accept(record1)
authorizationCodeRefreshTokens.Accept(record2)
authorizationCodeRefreshTokens.Accept(record3)
authorizationCodeRefreshTokens.Accept(record4)
// Then
assert.Equal(t, []string(nil), authorizationCodeRefreshTokens.GetTokens(authorizationCode))
actualAuthorizationCode1, err := authorizationCodeRefreshTokens.GetAuthorizationCode(refreshToken1)
assert.Equal(t, goauth2.ErrAuthorizationCodeNotFound, err)
actualAuthorizationCode2, err := authorizationCodeRefreshTokens.GetAuthorizationCode(refreshToken2)
assert.Equal(t, goauth2.ErrAuthorizationCodeNotFound, err)
assert.Equal(t, "", actualAuthorizationCode1)
assert.Equal(t, "", actualAuthorizationCode2)
})
t.Run("no events", func(t *testing.T) {
// Given
authorizationCodeRefreshTokens := goauth2.NewAuthorizationCodeRefreshTokens()
// When
tokens := authorizationCodeRefreshTokens.GetTokens(authorizationCode)
// Then
assert.Equal(t, []string(nil), tokens)
actualAuthorizationCode, err := authorizationCodeRefreshTokens.GetAuthorizationCode(refreshToken1)
assert.Equal(t, goauth2.ErrAuthorizationCodeNotFound, err)
assert.Equal(t, "", actualAuthorizationCode)
})
}