-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathauth_token_test.go
185 lines (160 loc) · 6.08 KB
/
auth_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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package coze
import (
"context"
"net/http"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestTokenAuth(t *testing.T) {
t.Run("Token returns fixed access token", func(t *testing.T) {
expectedToken := "test_access_token"
auth := NewTokenAuth(expectedToken)
token, err := auth.Token(context.Background())
require.NoError(t, err)
assert.Equal(t, expectedToken, token)
})
}
func TestJWTAuth(t *testing.T) {
const testPrivateKey = `-----BEGIN PRIVATE KEY-----
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCj1Mlf7zfg/kx4
DHogPkN7gTkAYi7FM6TktFZFHDm8Zs1KiL6WrpU+UTqBiHHhlMVB3RiaJxWH40ia
9OWJvIpM5lCaMnzGNX/4wC+4Pxc3KNoUhijP6ofS4yI5xSpUyMrjl9q95ePBNmmP
Tv+s4uTa2y0e1ZlDHwIWC8InZ5NX65RO+yIF+95gclFkANgp5l7aBHaLiSebYRJT
aluZmS4ZUH06Y9LHkS+QvuvOPaQu3Y+xdgHnzEYtNn83tTmLCBAt2ZYcJi0WIeJZ
acaLsi59N1LH+2ZFtMc6+l7qlB0i4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apl0bVKK1CqvdzYKVeRR72BEH5CwG2qoP6pAoGAUpvD0LSVh171UwQFkT6K
b2mWBz1LV7EYLg4bfmi7wRBUCeEuK16+PEJ63yYUg8cSGTZqOFyRbc4tNf2Ow8BL
gpsiuY9Mn2TnbscpeK841s68IHx4l90Je4tbbjK4E/yv+vgARkiiWQbG0BZSkBjO
qI39/arl6ZhTeQMv7TrpQ6Q=
-----END PRIVATE KEY-----`
t.Run("NewJWTAuth with default TTL", func(t *testing.T) {
client, err := NewJWTOAuthClient(NewJWTOAuthClientParam{
ClientID: "test_client_id",
PublicKey: "test_public_key",
PrivateKeyPEM: testPrivateKey,
}, WithAuthBaseURL(ComBaseURL))
require.NoError(t, err)
auth := NewJWTAuth(client, nil)
jwtAuth, ok := auth.(*jwtOAuthImpl)
require.True(t, ok)
assert.Equal(t, 900, jwtAuth.TTL)
assert.Nil(t, jwtAuth.SessionName)
assert.Nil(t, jwtAuth.Scope)
})
t.Run("NewJWTAuth with custom options", func(t *testing.T) {
client, err := NewJWTOAuthClient(NewJWTOAuthClientParam{
ClientID: "test_client_id",
PublicKey: "test_public_key",
PrivateKeyPEM: testPrivateKey,
}, WithAuthBaseURL(ComBaseURL))
require.NoError(t, err)
sessionName := "test_session"
scope := BuildBotChat([]string{"bot_id"}, []string{"permission"})
auth := NewJWTAuth(client, &GetJWTAccessTokenReq{
TTL: 1800,
SessionName: &sessionName,
Scope: scope,
})
jwtAuth, ok := auth.(*jwtOAuthImpl)
require.True(t, ok)
assert.Equal(t, 1800, jwtAuth.TTL)
assert.Equal(t, &sessionName, jwtAuth.SessionName)
assert.Equal(t, scope, jwtAuth.Scope)
})
t.Run("Token returns cached token when not expired", func(t *testing.T) {
mockTransport := &mockTransport{
roundTripFunc: func(req *http.Request) (*http.Response, error) {
return mockResponse(http.StatusOK, &OAuthToken{
AccessToken: "test_access_token",
ExpiresIn: 3600,
})
},
}
client, err := NewJWTOAuthClient(NewJWTOAuthClientParam{
ClientID: "test_client_id",
PublicKey: "test_public_key",
PrivateKeyPEM: testPrivateKey,
}, WithAuthBaseURL(ComBaseURL),
WithAuthHttpClient(&http.Client{Transport: mockTransport}))
require.NoError(t, err)
auth := NewJWTAuth(client, nil)
// 第一次调用,获取新 token
token1, err := auth.Token(context.Background())
require.NoError(t, err)
assert.Equal(t, "test_access_token", token1)
// 第二次调用,使用缓存的 token
token2, err := auth.Token(context.Background())
require.NoError(t, err)
assert.Equal(t, token1, token2)
})
t.Run("Token refreshes when expired", func(t *testing.T) {
callCount := 0
mockTransport := &mockTransport{
roundTripFunc: func(req *http.Request) (*http.Response, error) {
callCount++
return mockResponse(http.StatusOK, &OAuthToken{
AccessToken: "test_access_token_" + string(rune(callCount+'0')),
ExpiresIn: 1, // 设置为1秒后过期
})
},
}
client, err := NewJWTOAuthClient(NewJWTOAuthClientParam{
ClientID: "test_client_id",
PublicKey: "test_public_key",
PrivateKeyPEM: testPrivateKey,
}, WithAuthBaseURL(ComBaseURL),
WithAuthHttpClient(&http.Client{Transport: mockTransport}))
require.NoError(t, err)
auth := NewJWTAuth(client, nil)
// 第一次调用,获取新 token
token1, err := auth.Token(context.Background())
require.NoError(t, err)
assert.Equal(t, "test_access_token_1", token1)
// 等待 token 过期
time.Sleep(2 * time.Second)
// 第二次调用,token 已过期,获取新 token
token2, err := auth.Token(context.Background())
require.NoError(t, err)
assert.Equal(t, "test_access_token_2", token2)
assert.NotEqual(t, token1, token2)
})
t.Run("Token handles error", func(t *testing.T) {
mockTransport := &mockTransport{
roundTripFunc: func(req *http.Request) (*http.Response, error) {
return mockResponse(http.StatusBadRequest, &OAuthToken{
AccessToken: "",
ExpiresIn: 0,
})
},
}
client, err := NewJWTOAuthClient(NewJWTOAuthClientParam{
ClientID: "test_client_id",
PublicKey: "test_public_key",
PrivateKeyPEM: testPrivateKey,
}, WithAuthBaseURL(ComBaseURL),
WithAuthHttpClient(&http.Client{Transport: mockTransport}))
require.NoError(t, err)
auth := NewJWTAuth(client, nil)
token, err := auth.Token(context.Background())
assert.Error(t, err)
assert.Empty(t, token)
})
}