-
Notifications
You must be signed in to change notification settings - Fork 9
/
token_test.go
105 lines (90 loc) · 2.59 KB
/
token_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
package dynamo_test
import (
"os"
"testing"
"time"
dynamo "github.com/contamobi/go-oauth2-dynamodb"
"github.com/contamobi/oauth2/models"
. "github.com/smartystreets/goconvey/convey"
)
func TestTokenStore(t *testing.T) {
Convey("Test dynamodb token store", t, func() {
mcfg, err := dynamo.NewConfig(
os.Getenv("AWS_REGION"),
os.Getenv("DYNAMODB_ENDPOINT"),
os.Getenv("AWS_ACCESS_KEY"),
os.Getenv("AWS_SECRET"),
"oauth2_basic",
"oauth2_access",
"oauth2_refresh",
)
store := dynamo.NewTokenStore(mcfg)
So(err, ShouldBeNil)
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(info)
So(err, ShouldBeNil)
cinfo, err := store.GetByCode(info.Code)
So(err, ShouldBeNil)
So(cinfo.GetUserID(), ShouldEqual, info.UserID)
err = store.RemoveByCode(info.Code)
So(err, ShouldBeNil)
cinfo, err = store.GetByCode(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(info)
So(err, ShouldBeNil)
ainfo, err := store.GetByAccess(info.GetAccess())
So(err, ShouldBeNil)
So(ainfo.GetUserID(), ShouldEqual, info.GetUserID())
err = store.RemoveByAccess(info.GetAccess())
So(err, ShouldBeNil)
ainfo, err = store.GetByAccess(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(info)
So(err, ShouldBeNil)
rinfo, err := store.GetByRefresh(info.GetRefresh())
So(err, ShouldBeNil)
So(rinfo.GetUserID(), ShouldEqual, info.GetUserID())
err = store.RemoveByRefresh(info.GetRefresh())
So(err, ShouldBeNil)
rinfo, err = store.GetByRefresh(info.GetRefresh())
So(err, ShouldBeNil)
So(rinfo, ShouldBeNil)
})
})
}