-
Notifications
You must be signed in to change notification settings - Fork 9
/
user_policies_test.go
183 lines (169 loc) · 3.84 KB
/
user_policies_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
package authz
import (
"testing"
"github.com/dzungtran/echo-rest-api/modules/core/domains"
"github.com/stretchr/testify/assert"
)
type TestEndpoint struct {
Method string
Endpoint string
}
var (
getUserEndpoint = TestEndpoint{"GET", "/admin/users/:userId"}
updateUserEndpoint = TestEndpoint{"PUT", "/admin/users/:userId"}
deleteUserEndpoint = TestEndpoint{"DELETE", "/admin/users/:userId"}
getListUserEndpoint = TestEndpoint{"GET", "/admin/users"}
createUserEndpoint = TestEndpoint{"POST", "/admin/users"}
)
func TestPoliciesForUserEndpoint(t *testing.T) {
loggedInUser := &domains.UserWithRoles{
User: domains.User{Id: 8},
OrgRole: map[int64]string{
8: "viewer",
},
}
superAdmin := &domains.UserWithRoles{
User: domains.User{Id: 99, Email: "[email protected]"},
OrgRole: map[int64]string{},
}
tcs := []struct {
name string
loggedInUser *domains.UserWithRoles
requestedUser *domains.User
hasError bool
denyMsg []string
endpoint TestEndpoint
}{
{
"super admin fetch user info then return no error",
superAdmin,
&domains.User{Id: 8},
false,
[]string{},
getUserEndpoint,
},
{
"logged in users fetch info by them self then return no error",
loggedInUser,
&domains.User{Id: 8},
false,
[]string{},
getUserEndpoint,
},
{
"logged in users fetch info of other then return forbidden error",
loggedInUser,
&domains.User{Id: 9},
true,
[]string{"user id is invalid"},
getUserEndpoint,
},
{
"logged in users fetch null info then return forbidden error",
loggedInUser,
nil,
true,
[]string{"user id is invalid"},
getUserEndpoint,
},
{
"super admin update user info then return no error",
superAdmin,
&domains.User{Id: 8},
false,
[]string{},
updateUserEndpoint,
},
{
"logged in users update info by them self then return no error",
loggedInUser,
&domains.User{Id: 8},
false,
[]string{},
updateUserEndpoint,
},
{
"logged in users update info of other then return forbidden error",
loggedInUser,
&domains.User{Id: 9},
true,
[]string{"user id is invalid"},
updateUserEndpoint,
},
{
"logged in users update null info then return forbidden error",
loggedInUser,
nil,
true,
[]string{"user id is invalid"},
updateUserEndpoint,
},
{
"super admin delete an user then return no error",
superAdmin,
&domains.User{Id: 99},
false,
[]string{},
deleteUserEndpoint,
},
{
"logged in users try to delete an user then return forbidden error",
loggedInUser,
&domains.User{Id: 99},
true,
[]string{"you don't have the permission"},
deleteUserEndpoint,
},
{
"super admin create an user then return no error",
superAdmin,
&domains.User{Id: 99},
false,
[]string{},
createUserEndpoint,
},
{
"logged in users try to create an user then return forbidden error",
loggedInUser,
&domains.User{Id: 99},
true,
[]string{"you don't have the permission"},
createUserEndpoint,
},
{
"super admin get list user then return no error",
superAdmin,
&domains.User{Id: 99},
false,
[]string{},
getListUserEndpoint,
},
{
"logged in users try to get list user then return forbidden error",
loggedInUser,
&domains.User{Id: 99},
true,
[]string{"you don't have the permission"},
getListUserEndpoint,
},
}
for _, tc := range tcs {
tc := tc
t.Run(tc.name, func(t *testing.T) {
msg, err := CheckPolicies(tc.loggedInUser,
WithInputRequestMethod(tc.endpoint.Method),
WithInputRequestEndpoint(tc.endpoint.Endpoint),
WithInputExtraData("user_info", tc.requestedUser),
)
if tc.hasError {
assert.NotNil(t, err)
} else {
assert.Nil(t, err)
}
if len(tc.denyMsg) > 0 {
assert.Equal(t, tc.denyMsg, msg)
}
assert.Equal(t, len(tc.denyMsg), len(msg))
})
}
}