-
Notifications
You must be signed in to change notification settings - Fork 0
/
common_test.go
133 lines (110 loc) · 2.92 KB
/
common_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
package authcontrol_test
import (
"context"
"encoding/json"
"errors"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/0xsequence/authcontrol"
"github.com/0xsequence/authcontrol/proto"
)
const HeaderKey = "Test-Key"
func keyFunc(r *http.Request) string {
return r.Header.Get(HeaderKey)
}
type requestOption func(r *http.Request)
func accessKey(v string) requestOption {
return func(r *http.Request) {
r.Header.Set(HeaderKey, v)
}
}
func jwt(v string) requestOption {
return func(r *http.Request) {
r.Header.Set("Authorization", "Bearer "+v)
}
}
func origin(v string) requestOption {
return func(r *http.Request) {
r.Header.Set("Origin", v)
}
}
func executeRequest(t *testing.T, ctx context.Context, handler http.Handler, path string, options ...requestOption) (bool, error) {
req, err := http.NewRequest("POST", path, nil)
require.NoError(t, err)
req.Header.Set("X-Real-IP", "127.0.0.1")
for _, opt := range options {
opt(req)
}
rr := httptest.NewRecorder()
handler.ServeHTTP(rr, req.WithContext(ctx))
if status := rr.Result().StatusCode; status < http.StatusOK || status >= http.StatusBadRequest {
w := proto.WebRPCError{}
err = json.Unmarshal(rr.Body.Bytes(), &w)
require.NoError(t, err)
return false, w
}
return true, nil
}
func TestVerify(t *testing.T) {
services := map[string][]string{
"Service1": {
"Method1",
"Method2",
"Method3",
},
"Service2": {
"Method1",
},
}
// Valid ACL config
acl := authcontrol.Config[any]{
"Service1": {
"Method1": authcontrol.NewACL(proto.SessionType_User),
"Method2": authcontrol.NewACL(proto.SessionType_User),
"Method3": authcontrol.NewACL(proto.SessionType_User),
},
"Service2": {
"Method1": authcontrol.NewACL(proto.SessionType_User),
},
}
err := acl.Verify(services)
assert.NoError(t, err)
// Wrong Service
acl = authcontrol.Config[any]{
"WrongService1": {
"Method1": authcontrol.NewACL(proto.SessionType_User),
"Method2": authcontrol.NewACL(proto.SessionType_User),
"Method3": authcontrol.NewACL(proto.SessionType_User),
},
"Service2": {
"Method1": authcontrol.NewACL(proto.SessionType_User),
},
}
err = acl.Verify(services)
require.Error(t, err)
expectedErrors := []error{
errors.New("Service1.Method1 not found"),
errors.New("Service1.Method2 not found"),
errors.New("Service1.Method3 not found"),
}
assert.Equal(t, errors.Join(expectedErrors...).Error(), err.Error())
// Wrong Methods
acl = authcontrol.Config[any]{
"Service1": {
"Method1": authcontrol.NewACL(proto.SessionType_User),
},
"Service2": {
"Method1": authcontrol.NewACL(proto.SessionType_User),
},
}
err = acl.Verify(services)
require.Error(t, err)
expectedErrors = []error{
errors.New("Service1.Method2 not found"),
errors.New("Service1.Method3 not found"),
}
assert.Equal(t, errors.Join(expectedErrors...).Error(), err.Error())
}