-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathoauth_test.go
104 lines (92 loc) · 2.18 KB
/
oauth_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
package golang_oauth
import (
_ "github.com/go-sql-driver/mysql"
"github.com/gobeam/golang-oauth/model"
"github.com/gobeam/golang-oauth/util"
"github.com/google/uuid"
"testing"
"time"
)
var dbStore *Store
var accessTokenString string
var refreshTokenString string
var accessId uuid.UUID
var userID int64 = 1
var clientDetail *model.Clients
func init() {
store := NewDefaultStore(
NewConfig(util.DbConfig),
)
dbStore = store
}
func TestCreateClient(t *testing.T) {
client, err := dbStore.CreateClient(1, "test app")
if err != nil {
t.Error(err.Error())
}
clientDetail = &client
if client.ID == uuid.Nil {
t.Errorf("Client uuid invalid client not expected to be %s", uuid.Nil)
}
}
func TestCreate(t *testing.T) {
accessToken := &model.Token{
ClientID: clientDetail.ID,
ClientSecret: clientDetail.Secret,
UserID: userID,
Scope: "*",
AccessCreateAt: time.Now(),
AccessExpiresIn: time.Second * 15,
RefreshCreateAt: time.Now(),
}
resp, err := dbStore.Create(accessToken)
if err != nil {
t.Error(err.Error())
}
if resp.RefreshToken == "" {
t.Error("refresh token cannot be nil")
}
refreshTokenString = resp.RefreshToken
if resp.AccessToken == "" {
t.Error("access token cannot be nil")
}
accessTokenString = resp.AccessToken
}
func TestGetByAccess(t *testing.T) {
resp, err := dbStore.GetByAccess(accessTokenString)
if err != nil {
t.Error(err.Error())
}
if resp.ID == uuid.Nil {
t.Errorf("token info uuid is not expected to be %s", uuid.Nil)
}
}
func TestGetByRefresh(t *testing.T) {
resp, err := dbStore.GetByRefresh(refreshTokenString)
if err != nil {
t.Error(err.Error())
}
if resp.ID == uuid.Nil {
t.Errorf("token info uuid is not expected to be %s", uuid.Nil)
}
accessId = resp.ID
}
func TestRevokeByAccessTokens(t *testing.T) {
err := dbStore.RevokeByAccessTokens(userID)
if err != nil {
t.Error(err.Error())
}
}
func TestRevokeRefreshToken(t *testing.T) {
err := dbStore.RevokeRefreshToken(accessId.String())
if err != nil {
t.Error(err.Error())
}
}
func TestClearByAccessToken(t *testing.T) {
err := dbStore.ClearByAccessToken(userID)
if err != nil {
t.Error(err.Error())
}
defer dbStore.Close()
}