-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdynamicUserAuth_test.go
81 lines (69 loc) · 2.05 KB
/
dynamicUserAuth_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
package dynamicUserAuth_test
import (
"errors"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/fino-digital/dynamicUserAuth"
"github.com/labstack/echo"
)
func TestAuthMiddleware(t *testing.T) {
// Init TestServer
targetFunc := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "Hello, client")
})
testServer := httptest.NewServer(targetFunc)
host := "fino.digital"
testServer.URL = "http://" + host
defer testServer.Close()
// build up a testStrategy
testStrategy := dynamicUserAuth.Strategy{
AuthorizeUser: func(c echo.Context) error {
return nil
},
}
// new middleware
authMiddleware := dynamicUserAuth.NewAuthMiddleware(&dynamicUserAuth.DynamicUserAuth{Stragegies: dynamicUserAuth.Stragegies{host: testStrategy}})
// build request
router := echo.New()
request := httptest.NewRequest(echo.GET, testServer.URL, nil)
rec := httptest.NewRecorder()
context := router.NewContext(request, rec)
// TEST
err := authMiddleware.Handle(echo.WrapHandler(targetFunc))(context)
if err != nil {
t.Error(err)
}
}
func TestAllowedException(t *testing.T) {
// Init TestServer
targetFunc := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "Hello, client")
})
testServer := httptest.NewServer(targetFunc)
host := "fino.digital"
testServer.URL = "http://" + host
defer testServer.Close()
// build up a testStrategy
testStrategy := dynamicUserAuth.Strategy{
AuthorizeUser: func(c echo.Context) error {
return errors.New("it shouldn't pass this function")
},
Exception: func(c echo.Context) bool {
return true
},
}
// new middleware
authMiddleware := dynamicUserAuth.NewAuthMiddleware(&dynamicUserAuth.DynamicUserAuth{Stragegies: dynamicUserAuth.Stragegies{host: testStrategy}})
// build request
router := echo.New()
request := httptest.NewRequest(echo.GET, testServer.URL, nil)
rec := httptest.NewRecorder()
context := router.NewContext(request, rec)
// TEST
err := authMiddleware.Handle(echo.WrapHandler(targetFunc))(context)
if err != nil {
t.Error(err)
}
}