-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoken_test.go
72 lines (61 loc) · 1.98 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
package gooauth2gorm_test
import (
"context"
"testing"
"time"
"github.com/go-oauth2/oauth2/v4/models"
gooauth2gorm "github.com/nouhoum/go-oauth2-gorm"
"github.com/stretchr/testify/assert"
)
func TestTokenStore(t *testing.T) {
store := gooauth2gorm.NewTokenStoreWithDB(&gooauth2gorm.Config{DBType: gooauth2gorm.PostgresSQL}, db, "", 0)
ctx := context.Background()
info := &models.Token{
ClientID: "client1",
UserID: "user1",
RedirectURI: "http://localhost",
Scope: "all",
Code: "code1",
CodeCreateAt: time.Now(),
CodeExpiresIn: time.Second * 5,
}
err := store.Create(ctx, info)
assert.Nil(t, err, "store.Create")
token, err := store.GetByCode(ctx, info.GetCode())
assert.Nil(t, err, "store.GetByCode")
assert.Equal(t, info.Code, token.GetCode())
access, err := store.GetByAccess(ctx, info.GetAccess())
assert.Nil(t, err)
assert.Nil(t, access, "store.GetByAccess")
//Remove by code
err = store.RemoveByCode(ctx, info.GetCode())
assert.Nil(t, err)
unknownToken, err := store.GetByCode(ctx, info.GetCode())
assert.Nil(t, err, "store.GetByCode")
assert.Nil(t, unknownToken, "store.GetByCode")
//access token
info2 := &models.Token{
ClientID: "client1",
UserID: "user1",
RedirectURI: "http://localhost",
Scope: "all",
Access: "access1",
CodeCreateAt: time.Now(),
CodeExpiresIn: time.Second * 5,
}
err = store.Create(ctx, info2)
assert.Nil(t, err, "store.Create")
token, err = store.GetByAccess(ctx, info2.GetAccess())
assert.Nil(t, err, "store.GetByAccess")
assert.NotNil(t, token.GetAccess())
assert.Equal(t, info2.Access, token.GetAccess())
code, err := store.GetByCode(ctx, "...")
assert.Nil(t, err)
assert.Nil(t, code, "store.GetByCode")
//Remove access code
err = store.RemoveByAccess(ctx, info2.GetAccess())
//assert.Nil(t, err)
unknownAccess, err := store.GetByAccess(ctx, info2.GetAccess())
assert.Nil(t, err, "store.GetAccess")
assert.Nil(t, unknownAccess, "store.GetAccess")
}