-
Notifications
You must be signed in to change notification settings - Fork 10
/
mysql_test.go
145 lines (119 loc) · 3.95 KB
/
mysql_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
package mysql
import (
"context"
"regexp"
"testing"
"time"
"github.com/DATA-DOG/go-sqlmock"
"github.com/go-oauth2/oauth2/v4/models"
_ "github.com/go-sql-driver/mysql"
. "github.com/smartystreets/goconvey/convey"
"github.com/stretchr/testify/assert"
"gopkg.in/gorp.v2"
)
const (
dsn = "root:@tcp(127.0.0.1:3306)/myapp_test?charset=utf8"
)
func TestTokenStore(t *testing.T) {
Convey("Test mysql token store", t, func() {
store := NewDefaultStore(NewConfig(dsn))
defer store.clean()
ctx := context.Background()
Convey("Test authorization code store", func() {
info := &models.Token{
ClientID: "1",
UserID: "1_1",
RedirectURI: "http://localhost/",
Scope: "all",
Code: "11_11_11",
CodeCreateAt: time.Now(),
CodeExpiresIn: time.Second * 5,
}
err := store.Create(ctx, info)
So(err, ShouldBeNil)
cinfo, err := store.GetByCode(ctx, info.Code)
So(err, ShouldBeNil)
So(cinfo.GetUserID(), ShouldEqual, info.UserID)
err = store.RemoveByCode(ctx, info.Code)
So(err, ShouldBeNil)
cinfo, err = store.GetByCode(ctx, info.Code)
So(err, ShouldBeNil)
So(cinfo, ShouldBeNil)
})
Convey("Test access token store", func() {
info := &models.Token{
ClientID: "1",
UserID: "1_1",
RedirectURI: "http://localhost/",
Scope: "all",
Access: "1_1_1",
AccessCreateAt: time.Now(),
AccessExpiresIn: time.Second * 5,
}
err := store.Create(ctx, info)
So(err, ShouldBeNil)
ainfo, err := store.GetByAccess(ctx, info.GetAccess())
So(err, ShouldBeNil)
So(ainfo.GetUserID(), ShouldEqual, info.GetUserID())
err = store.RemoveByAccess(ctx, info.GetAccess())
So(err, ShouldBeNil)
ainfo, err = store.GetByAccess(ctx, info.GetAccess())
So(err, ShouldBeNil)
So(ainfo, ShouldBeNil)
})
Convey("Test refresh token store", func() {
info := &models.Token{
ClientID: "1",
UserID: "1_2",
RedirectURI: "http://localhost/",
Scope: "all",
Access: "1_2_1",
AccessCreateAt: time.Now(),
AccessExpiresIn: time.Second * 5,
Refresh: "1_2_2",
RefreshCreateAt: time.Now(),
RefreshExpiresIn: time.Second * 15,
}
err := store.Create(ctx, info)
So(err, ShouldBeNil)
ainfo, err := store.GetByAccess(ctx, info.GetAccess())
So(err, ShouldBeNil)
So(ainfo.GetUserID(), ShouldEqual, info.GetUserID())
err = store.RemoveByAccess(ctx, info.GetAccess())
So(err, ShouldBeNil)
ainfo, err = store.GetByAccess(ctx, info.GetAccess())
So(err, ShouldBeNil)
So(ainfo, ShouldBeNil)
rinfo, err := store.GetByRefresh(ctx, info.GetRefresh())
So(err, ShouldBeNil)
So(rinfo.GetUserID(), ShouldEqual, info.GetUserID())
err = store.RemoveByRefresh(ctx, info.GetRefresh())
So(err, ShouldBeNil)
rinfo, err = store.GetByRefresh(ctx, info.GetRefresh())
So(err, ShouldBeNil)
So(rinfo, ShouldBeNil)
})
})
}
func TestNewStoreWithOpts_ShouldReturnStoreNotNil(t *testing.T) {
// ARRANGE
db, mockDB, _ := sqlmock.New()
tableName := "custom_table_name"
// Mock sql exec create table
mockDB.ExpectExec(regexp.QuoteMeta("create table if not exists `custom_table_name` (`id` bigint not null primary key auto_increment, `expired_at` bigint, `code` varchar(255), `access` varchar(255), `refresh` varchar(255), `data` text) engine=InnoDB charset=UTF8;")).
WillReturnResult(sqlmock.NewResult(0, 0))
// Mock query:
mockDB.ExpectQuery(regexp.QuoteMeta("SELECT COUNT(*) FROM custom_table_name WHERE expired_at<=? OR (code='' AND access='' AND refresh='')")).
WillReturnRows(sqlmock.NewRows([]string{"count(*)"}).AddRow(0))
// ACTION
store := NewStoreWithOpts(db,
WithTableName(tableName),
WithSQLDialect(gorp.MySQLDialect{Engine: "InnoDB", Encoding: "UTF8"}),
WithGCTimeInterval(1000),
)
defer store.clean()
// ASSERT
assert.NotNil(t, store)
assert.NotNil(t, store.ticker)
assert.Equal(t, store.tableName, tableName)
}