-
Notifications
You must be signed in to change notification settings - Fork 14
/
token_store_test.go
166 lines (130 loc) · 4.23 KB
/
token_store_test.go
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
package mysql_test
import (
"fmt"
"os"
"testing"
"time"
_ "github.com/go-sql-driver/mysql"
"github.com/jmoiron/sqlx"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
pgadapter "github.com/vgarvardt/go-pg-adapter"
"gopkg.in/oauth2.v3/models"
. "github.com/imrenagi/go-oauth2-mysql"
)
func generateTokenTableName() string {
return fmt.Sprintf("token_%d", time.Now().UnixNano())
}
func generateClientTableName() string {
return fmt.Sprintf("client_%d", time.Now().UnixNano())
}
func TestMYSQLConn(t *testing.T) {
db, err := sqlx.Connect("mysql", os.Getenv("MYSQL_URI"))
require.NoError(t, err)
defer func() {
assert.NoError(t, db.Close())
}()
tokenStore, err := NewTokenStore(
db,
WithTokenStoreTableName(generateTokenTableName()),
WithTokenStoreGCInterval(time.Second),
)
require.NoError(t, err)
defer func() {
assert.NoError(t, tokenStore.Close())
}()
clientStore, err := NewClientStore(
db,
WithClientStoreTableName(generateClientTableName()),
)
require.NoError(t, err)
runTokenStoreTest(t, tokenStore)
runClientStoreTest(t, clientStore)
}
func TestSQL(t *testing.T) {
db, err := sqlx.Connect("mysql", os.Getenv("MYSQL_URI"))
require.NoError(t, err)
defer func() {
assert.NoError(t, db.Close())
}()
tokenStore, err := NewTokenStore(
db,
WithTokenStoreTableName(generateTokenTableName()),
WithTokenStoreGCInterval(time.Second),
)
require.NoError(t, err)
defer func() {
assert.NoError(t, tokenStore.Close())
}()
clientStore, err := NewClientStore(
db,
WithClientStoreTableName(generateClientTableName()),
)
require.NoError(t, err)
runTokenStoreTest(t, tokenStore)
runClientStoreTest(t, clientStore)
}
func runTokenStoreTest(t *testing.T, store *TokenStore) {
runTokenStoreCodeTest(t, store)
runTokenStoreAccessTest(t, store)
runTokenStoreRefreshTest(t, store)
// sleep for a while just to wait for GC run for sure to ensure there were no errors there
time.Sleep(3 * time.Second)
}
func runTokenStoreCodeTest(t *testing.T, store *TokenStore) {
code := fmt.Sprintf("code %s", time.Now().String())
tokenCode := models.NewToken()
tokenCode.SetCode(code)
tokenCode.SetCodeCreateAt(time.Now())
tokenCode.SetCodeExpiresIn(time.Minute)
require.NoError(t, store.Create(tokenCode))
token, err := store.GetByCode(code)
require.NoError(t, err)
assert.Equal(t, code, token.GetCode())
require.NoError(t, store.RemoveByCode(code))
_, err = store.GetByCode(code)
assert.Equal(t, pgadapter.ErrNoRows, err)
}
func runTokenStoreAccessTest(t *testing.T, store *TokenStore) {
code := fmt.Sprintf("access %s", time.Now().String())
tokenCode := models.NewToken()
tokenCode.SetAccess(code)
tokenCode.SetAccessCreateAt(time.Now())
tokenCode.SetAccessExpiresIn(time.Minute)
require.NoError(t, store.Create(tokenCode))
token, err := store.GetByAccess(code)
require.NoError(t, err)
assert.Equal(t, code, token.GetAccess())
require.NoError(t, store.RemoveByAccess(code))
_, err = store.GetByAccess(code)
assert.Equal(t, pgadapter.ErrNoRows, err)
}
func runTokenStoreRefreshTest(t *testing.T, store *TokenStore) {
code := fmt.Sprintf("refresh %s", time.Now().String())
tokenCode := models.NewToken()
tokenCode.SetRefresh(code)
tokenCode.SetRefreshCreateAt(time.Now())
tokenCode.SetRefreshExpiresIn(time.Minute)
require.NoError(t, store.Create(tokenCode))
token, err := store.GetByRefresh(code)
require.NoError(t, err)
assert.Equal(t, code, token.GetRefresh())
require.NoError(t, store.RemoveByRefresh(code))
_, err = store.GetByRefresh(code)
assert.Equal(t, pgadapter.ErrNoRows, err)
}
func runClientStoreTest(t *testing.T, store *ClientStore) {
originalClient := &models.Client{
ID: fmt.Sprintf("id %s", time.Now().String()),
Secret: fmt.Sprintf("secret %s", time.Now().String()),
Domain: fmt.Sprintf("domain %s", time.Now().String()),
UserID: fmt.Sprintf("user id %s", time.Now().String()),
}
require.NoError(t, store.Create(originalClient))
client, err := store.GetByID(originalClient.GetID())
require.NoError(t, err)
assert.Equal(t, originalClient.GetID(), client.GetID())
assert.Equal(t, originalClient.GetSecret(), client.GetSecret())
assert.Equal(t, originalClient.GetDomain(), client.GetDomain())
assert.Equal(t, originalClient.GetUserID(), client.GetUserID())
}