-
Notifications
You must be signed in to change notification settings - Fork 0
/
authz_test.go
155 lines (131 loc) · 3.91 KB
/
authz_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
package main_test
import (
"testing"
main "github.com/m-mizutani/strix"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestAuthzService(t *testing.T) {
raw := `{
"users": [
{"user_id": "alpha@example.com", "role":"blue"},
{"user_id": "bravo@example.com", "role":"orange"},
{"user_id": "charlie@example.com", "role":"orange"}
],
"roles": [
{"name":"blue", "allowed_tags":[]},
{"name":"orange", "allowed_tags":["spell.1"]}
],
"rules": [
{"user_regex":"^delta@", "role":"blue"},
{"user_regex":"@example.com$", "role":"orange"}
]
}`
authz, err := main.NewAuthzService([]byte(raw))
require.NoError(t, err)
// Test default users and roles
userA := main.AuthzServiceLookup(authz, "alpha@example.com")
assert.NotNil(t, userA)
assert.NotContains(t, main.AuthzUserAllowed(userA), "spell.1")
userB := main.AuthzServiceLookup(authz, "bravo@example.com")
assert.NotNil(t, userB)
assert.Contains(t, main.AuthzUserAllowed(userB), "spell.1")
userC := main.AuthzServiceLookup(authz, "charlie@example.com")
assert.NotNil(t, userC)
assert.Contains(t, main.AuthzUserAllowed(userC), "spell.1")
// Test rules
userD1 := main.AuthzServiceLookup(authz, "delta@example.com")
assert.NotNil(t, userD1)
assert.Equal(t, 0, len(main.AuthzUserAllowed(userD1)))
userD2 := main.AuthzServiceLookup(authz, "delta@example.org")
assert.NotNil(t, userD2)
assert.Equal(t, 0, len(main.AuthzUserAllowed(userD2)))
userD3 := main.AuthzServiceLookup(authz, "xxxxdelta@example.org")
assert.Nil(t, userD3)
userE1 := main.AuthzServiceLookup(authz, "echo@example.com")
assert.NotNil(t, userE1)
assert.Contains(t, main.AuthzUserAllowed(userE1), "spell.1")
userF1 := main.AuthzServiceLookup(authz, "foxtrot@example.org")
assert.Nil(t, userF1)
userF2 := main.AuthzServiceLookup(authz, "foxtrot@example.comcom")
assert.Nil(t, userF2)
}
func TestAuthzServiceInvalidJSON(t *testing.T) {
raw := `{
"users": [
{"user_id": "alpha@example.com", "role":"blue"}
],
"roles": [
{"name":"blue", "allowed_tags":[]}
],
"rules": [
{"user_regex":"^delta@", "role":"blue"}
],
}` // ^^^ invalid comma
_, err := main.NewAuthzService([]byte(raw))
require.Error(t, err)
}
func TestAuthzServiceDuplicatedRole(t *testing.T) {
raw := `{
"users": [
{"user_id": "alpha@example.com", "role":"blue"}
],
"roles": [
{"name":"blue", "allowed_tags":[]},
{"name":"blue", "allowed_tags":["spell.1"]}
]
}`
_, err := main.NewAuthzService([]byte(raw))
assert.EqualError(t, err, "Role 'blue' is duplicated")
}
func TestAuthzServiceDuplicatedUser(t *testing.T) {
raw := `{
"users": [
{"user_id": "alpha@example.com", "role":"blue"},
{"user_id": "alpha@example.com", "role":"orange"}
],
"roles": [
{"name":"blue", "allowed_tags":[]},
{"name":"orange", "allowed_tags":["spell.1"]}
]
}`
_, err := main.NewAuthzService([]byte(raw))
assert.EqualError(t, err, "User 'alpha@example.com' is duplicated")
}
func TestAuthzServiceUserRoleNotFound(t *testing.T) {
raw := `{
"users": [
{"user_id": "alpha@example.com", "role":"blue"},
{"user_id": "bravo@example.com", "role":"orange"}
],
"roles": [
{"name":"blue", "allowed_tags":[]}
]
}`
_, err := main.NewAuthzService([]byte(raw))
assert.EqualError(t, err, "Role 'orange' of User 'bravo@example.com' is not found")
}
func TestAuthzServiceRuleRoleNotFound(t *testing.T) {
raw := `{
"roles": [
{"name":"blue", "allowed_tags":[]}
],
"rules": [
{"user_regex":"^delta@", "role":"orange"}
]
}`
_, err := main.NewAuthzService([]byte(raw))
assert.EqualError(t, err, "Role 'orange' of Rule '^delta@' is not found")
}
func TestAuthzServiceRuleInavlidRegex(t *testing.T) {
raw := `{
"roles": [
{"name":"orange", "allowed_tags":[]}
],
"rules": [
{"user_regex":"^[delta@", "role":"orange"}
]
}`
_, err := main.NewAuthzService([]byte(raw))
assert.EqualError(t, err, "Fail to compile regex of a rule: ^[delta@")
}