-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware_test.go
121 lines (95 loc) · 2.8 KB
/
middleware_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
package auth_test
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/golang-jwt/jwt/v5"
"github.com/stretchr/testify/require"
"github.com/tinh-tinh/auth"
"github.com/tinh-tinh/tinhtinh/common"
"github.com/tinh-tinh/tinhtinh/core"
)
func Test_Guard(t *testing.T) {
authController := func(module *core.DynamicModule) *core.DynamicController {
ctrl := module.NewController("test")
jwtService := auth.InjectJwt(module)
ctrl.Get("", func(ctx core.Ctx) error {
data, err := jwtService.Generate(jwt.MapClaims{
"roles": []string{"admin", "user"},
})
if err != nil {
return common.BadRequestException(ctx.Res(), err.Error())
}
return ctx.JSON(core.Map{
"data": data,
})
})
ctrl.Guard(auth.Guard).Post("", func(ctx core.Ctx) error {
return ctx.JSON(core.Map{
"data": "ok",
})
})
return ctrl
}
authModule := func(module *core.DynamicModule) *core.DynamicModule {
mod := module.New(core.NewModuleOptions{
Controllers: []core.Controller{authController},
})
return mod
}
appModule := func() *core.DynamicModule {
appMod := core.NewModule(core.NewModuleOptions{
Imports: []core.Module{
auth.Register(auth.JwtOptions{
Alg: jwt.SigningMethodHS256,
Secret: "secret",
Exp: time.Hour,
}),
authModule,
},
})
return appMod
}
app := core.CreateFactory(appModule)
app.SetGlobalPrefix("api")
testServer := httptest.NewServer(app.PrepareBeforeListen())
defer testServer.Close()
testClient := testServer.Client()
req, err := http.NewRequest("GET", testServer.URL+"/api/test", nil)
require.Nil(t, err)
resp, err := testClient.Do(req)
require.Nil(t, err)
require.Equal(t, 200, resp.StatusCode)
data, err := io.ReadAll(resp.Body)
require.Nil(t, err)
type Response struct {
Data interface{} `json:"data"`
}
var res Response
require.Nil(t, json.Unmarshal(data, &res))
req, err = http.NewRequest("POST", testServer.URL+"/api/test", nil)
require.Nil(t, err)
req.Header.Set("Authorization", fmt.Sprintf("Bearer %v", res.Data))
resp, err = testClient.Do(req)
require.Nil(t, err)
require.Equal(t, http.StatusOK, resp.StatusCode)
resp, err = testClient.Post(testServer.URL+"/api/test", "application/json", nil)
require.Nil(t, err)
require.Equal(t, http.StatusForbidden, resp.StatusCode)
req, err = http.NewRequest("POST", testServer.URL+"/api/test", nil)
require.Nil(t, err)
req.Header.Set("Authorization", "Bearer")
resp, err = testClient.Do(req)
require.Nil(t, err)
require.Equal(t, http.StatusForbidden, resp.StatusCode)
req, err = http.NewRequest("POST", testServer.URL+"/api/test", nil)
require.Nil(t, err)
req.Header.Set("Authorization", "Bearer kfhdfkbkh")
resp, err = testClient.Do(req)
require.Nil(t, err)
require.Equal(t, http.StatusForbidden, resp.StatusCode)
}