-
Notifications
You must be signed in to change notification settings - Fork 1
/
token_store_test.go
112 lines (93 loc) · 2.9 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
package oauth2xorm
import (
"context"
"github.com/go-oauth2/oauth2/v4/models"
_ "github.com/go-sql-driver/mysql"
. "github.com/smartystreets/goconvey/convey"
"testing"
"time"
)
const (
dsn = "root:secret@(127.0.0.1:33060)/oauth2?parseTime=true&charset=utf8mb4&collation=utf8mb4_unicode_ci"
)
func TestTokenStore(t *testing.T) {
Convey("Test mysql token store", t, func() {
store := NewDefaultStore(NewConfig(dsn), true)
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)
})
})
}