-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstore_test.go
106 lines (90 loc) · 2.68 KB
/
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
package wework_test
import (
"context"
"fmt"
"testing"
"time"
"github.com/jasonwwl/go-wework"
)
func TestBuildKey(t *testing.T) {
client := newTestClient()
openCorpCfg := client.GetOpenCorpConfig()
internalCorpCfg := client.GetInternalCorpConfig()
tests := []struct {
tokenType wework.TokenType
want string
}{
{
tokenType: wework.AccessToken.TokenType,
want: fmt.Sprintf("%s:%s", wework.AccessToken.TokenType, internalCorpCfg.CorpID),
},
{
tokenType: wework.ProviderToken.TokenType,
want: fmt.Sprintf("%s:%s", wework.ProviderToken.TokenType, openCorpCfg.ProviderCorpID),
},
{
tokenType: wework.SuiteToken.TokenType,
want: fmt.Sprintf("%s:%s", wework.SuiteToken.TokenType, openCorpCfg.SuiteID),
},
{
tokenType: wework.AuthCorpAccessToken.TokenType,
want: fmt.Sprintf("%s:%s", wework.AuthCorpAccessToken.TokenType, openCorpCfg.AuthCorpID),
},
{
tokenType: wework.PermanentCode.TokenType,
want: fmt.Sprintf("%s:%s", wework.PermanentCode.TokenType, openCorpCfg.AuthCorpID),
},
{
tokenType: wework.SuiteTicket.TokenType,
want: fmt.Sprintf("%s:%s", wework.SuiteTicket.TokenType, openCorpCfg.SuiteID),
},
}
for _, test := range tests {
got, err := wework.BuildKey(client, test.tokenType)
if err != nil {
t.Errorf("BuildKey returned an error: %v", err)
}
if got != test.want {
t.Errorf("BuildKey returned unexpected key: got %v want %v", got, test.want)
}
}
}
func TestSetToken(t *testing.T) {
store := wework.NewMemoryStore()
client := newTestClient()
ctx := context.TODO()
tokenType := wework.AccessToken.TokenType
token := "testToken"
expiresIn := time.Second * 5
err := store.SetToken(client, ctx, tokenType, token, expiresIn)
if err != nil {
t.Errorf("SetToken returned an error: %v", err)
}
}
func TestGetToken(t *testing.T) {
store := wework.NewMemoryStore()
client := newTestClient()
ctx := context.TODO()
tokenType := wework.AccessToken.TokenType
token := "testToken"
expiresIn := time.Second * 1 // 5 seconds
// 首先设置一个令牌
err := store.SetToken(client, ctx, tokenType, token, expiresIn)
if err != nil {
t.Fatalf("SetToken returned an error: %v", err)
}
// 尝试获取同一个令牌
retrievedToken, err := store.GetToken(client, ctx, tokenType)
if err != nil {
t.Fatalf("GetToken returned an error: %v", err)
}
if retrievedToken != token {
t.Errorf("GetToken returned unexpected token: got %v want %v", retrievedToken, token)
}
// 测试过期的令牌
time.Sleep(time.Second * 1) // 等待超过令牌的过期时间
_, err = store.GetToken(client, ctx, tokenType)
if err == nil {
t.Errorf("Expected an error for expired token, but got none")
}
}