-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext_test.go
60 lines (49 loc) · 1.43 KB
/
context_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
package biu
import (
"net/http/httptest"
"testing"
"github.com/golang-jwt/jwt/v5"
"github.com/stretchr/testify/assert"
"github.com/tuotoo/biu/auth"
"github.com/tuotoo/biu/box"
"github.com/tuotoo/biu/expect"
)
type MockAuthTokenManager struct {
}
func (m MockAuthTokenManager) SignWithClaims(uid string, claims map[string]any) (token string, err error) {
tok := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
"uid": uid,
})
return tok.SignedString([]byte(""))
}
func (m MockAuthTokenManager) ParseToken(token string) (*jwt.Token, error) {
return jwt.Parse(token, func(token *jwt.Token) (any, error) {
return []byte(""), nil
})
}
func (m MockAuthTokenManager) RefreshToken(token string) (newToken string, err error) {
panic("implement me")
}
func TestAuthFilter(t *testing.T) {
e := New()
authInstance := &auth.Instance{
ITokenManager: MockAuthTokenManager{},
}
e.Filter(AuthFilter(100, authInstance))
ws := e.NewWS()
ws.Route(ws.POST("/auth"), ws.RouteAPI(func(ctx box.Ctx, api struct {
Return func(string)
}) {
assert.Equal(t, "1", ctx.UserID())
api.Return("OK")
}))
e.Add(ws.WebService)
s := httptest.NewServer(e)
defer s.Close()
expect.Default(t, s.URL).POST("/auth").
Expect().JSON().Object().HasValue("code", 100)
token, err := authInstance.Sign("1")
assert.NoError(t, err)
expect.Default(t, s.URL).POST("/auth").WithHeader("Authorization", token).
Expect().JSON().Object().HasValue("code", 0)
}