-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser_test.go
179 lines (174 loc) · 4.78 KB
/
user_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
package auth
import (
"testing"
"github.com/stretchr/testify/assert"
)
var policies = map[string]map[string]string{
// Default policies
// users are limited to admin user
"users": {
"all": "auth_user.is_admin",
},
// policies operations are limited to admin user
"policies": {
"all": "auth_user.is_admin",
},
// all tables are limited to filter by user_id by default
"all": {
"all": "user_id = auth_user.id",
},
// Custom policies
// todos operations are limited by `author_id` field
"todos": {
"all": "author_id = auth_user.id",
},
"articles": {
"read": "",
"all": "author_id = auth_user.id",
},
"comments": {
"read": "",
},
"reviews": {
"read": "auth_user.is_authenticated",
},
"notes": {
"read": "invalid policy",
},
}
//nolint:funlen
func TestUser_HasPerm(t *testing.T) {
for _, test := range []struct {
name string
user User
table string
action Action
hasPerm bool
withUserIDColumn string
}{
{
name: "non-admin users don't have permission on users table",
user: User{IsAdmin: false, ID: 1},
table: "users",
action: ActionRead,
hasPerm: false,
withUserIDColumn: "",
},
{
name: "admin users have permission on users tables",
user: User{IsAdmin: true, ID: 1},
table: "users",
action: ActionUpdate,
hasPerm: true,
withUserIDColumn: "",
},
{
name: "non-admin users don't have permission on policies",
user: User{IsAdmin: false, ID: 1},
table: "policies",
action: ActionRead,
hasPerm: false,
withUserIDColumn: "",
},
{
name: "admin users have permission on policies",
user: User{IsAdmin: true, ID: 1},
table: "policies",
action: ActionRead,
hasPerm: true,
withUserIDColumn: "",
},
{
name: "limit by `user_id` field by default",
user: User{IsAdmin: false, ID: 1},
table: "some_table",
action: ActionRead,
hasPerm: true,
withUserIDColumn: "user_id",
},
{
name: "users have read permission on todos with custom column",
user: User{IsAdmin: false, ID: 1},
table: "todos",
action: ActionRead,
hasPerm: true,
withUserIDColumn: "author_id",
},
{
name: "users have write permission on todos with custom column",
user: User{IsAdmin: false, ID: 1},
table: "todos",
action: ActionCreate,
hasPerm: true,
withUserIDColumn: "author_id",
},
{
name: "users have permission on read articles",
user: User{IsAdmin: false, ID: 1},
table: "articles",
action: ActionRead,
hasPerm: true,
withUserIDColumn: "",
},
{
name: "limit to current auth user when read mine",
user: User{IsAdmin: false, ID: 1},
table: "articles",
action: ActionReadMine,
hasPerm: true,
withUserIDColumn: "author_id",
},
{
name: "comments has public read perm",
user: User{IsAdmin: false, ID: 1},
table: "comments",
action: ActionRead,
hasPerm: true,
withUserIDColumn: "",
},
{
name: "comments has default other perm",
user: User{ID: 0},
table: "comments",
action: ActionCreate,
hasPerm: false,
withUserIDColumn: "user_id",
},
{
name: "reviews has public read perm for authenticated user",
user: User{ID: 1},
table: "reviews",
action: ActionRead,
hasPerm: true,
withUserIDColumn: "",
},
{
name: "reviews has not allow read perm for anonymous user",
user: User{ID: 0},
table: "reviews",
action: ActionRead,
hasPerm: false,
withUserIDColumn: "",
},
{
name: "notes has invalid policy, return false",
user: User{ID: 1, IsAdmin: true},
table: "notes",
action: ActionRead,
hasPerm: false,
withUserIDColumn: "",
},
} {
t.Run(test.name, func(t *testing.T) {
hasPerm, userIDColumn := test.user.HasPerm(test.table, test.action, policies)
assert.Equal(t, test.hasPerm, hasPerm)
assert.Equal(t, test.withUserIDColumn, userIDColumn)
})
}
t.Run("nil policies, return false", func(t *testing.T) {
user := User{ID: 1, IsAdmin: true}
hasPerm, userIDColumn := user.HasPerm("users", ActionRead, nil)
assert.False(t, hasPerm)
assert.Equal(t, "", userIDColumn)
})
}