/
permissions_test.cljc
146 lines (105 loc) · 5.13 KB
/
permissions_test.cljc
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
(ns agynamix.permissions-test
(:require [clojure.test :refer :all]
[agynamix.permissions :refer :all]))
(deftest create-permissions
(testing "create wildcard permission"
(is (= (make-permission "*") wildcard-permission)))
(testing "create wildcard permission"
(is (= (make-permission "company")
(->Permission "company" #{wildcard-token} #{wildcard-token}))))
(testing "create permission"
(is (= (make-permission "company" "edit")
(->Permission "company" #{"edit"} #{wildcard-token}))))
(testing "create permission"
(is (= (make-permission "company" "edit")
(->Permission "company" #{"edit"} #{wildcard-token}))))
(testing "create permission"
(is (= (make-permission "company" "edit" "abcd1234")
(->Permission "company" #{"edit"} #{"abcd1234"}))))
(testing "create permission"
(is (= (make-permission "company" "edit" "abcd1234")
(->Permission "company" #{"edit"} #{"abcd1234"}))))
)
(deftest simple-permissions-test
(let [perm1 (make-permission "perm1")]
(testing "test against wildcard permission"
(is (implied-by? perm1 wildcard-permission)))
(testing "test against empty permission"
(is (not (implied-by? perm1 empty-permission))))
(testing "test against perm1 domain"
(is (implied-by? perm1 (make-permission "perm1"))))
(testing "test against different domain"
(is (not (implied-by? perm1 (make-permission "a")))))
))
(deftest domain-and-actions-test
(testing "if company:* includes company:read"
(let [user-perm (make-permission "company")
res (make-permission "company" #{"read"})]
(is (implied-by? res user-perm))))
(testing "if company:read is included in wildcard permission"
(let [res (make-permission "company" #{"read"})]
(is (implied-by? res wildcard-permission))))
(testing "if company:read is NOT included in empty permission"
(let [res (make-permission "company" #{"read"})]
(is (not (implied-by? res empty-permission)))))
(testing "if company:* includes company:read"
(let [user-perm (make-permission "company:*")
res (make-permission "company:read")]
(is (implied-by? res user-perm))))
(testing "if company:* includes company:read"
(let [user-perm (make-permission "company")
res (make-permission "company:read")]
(is (implied-by? res user-perm))))
(testing "if company:read includes company:*"
(let [user-perm (make-permission "company" #{"read"})
res (make-permission "company")]
(is (not (implied-by? res user-perm)))))
(testing "if company:read includes company:*"
(let [user-perm (make-permission "company:read")
res (make-permission "company")]
(is (not (implied-by? res user-perm)))))
(testing "if multiple entities work. company:read,write includes company:upload,write"
(let [user-perm (make-permission "company" #{"read" "write"})
res (make-permission "company" #{"write" "upload"})]
(is (implied-by? res user-perm))))
(testing "if all wildcard works. * includes company:upload,write"
(let [user-perm (make-permission)
res (make-permission "company" #{"write" "upload"})]
(is (implied-by? res user-perm))))
)
(deftest actions-and-entities-test
(let [edit (make-permission "users:edit:abcd1234")]
(testing "checking actions and entities in wildcard permission"
(is (implied-by? edit wildcard-permission)))
(testing "checking actions and entities in empty permission"
(is (not (implied-by? edit empty-permission))))
(testing "checking actions and entities in empty permission"
(is (not (implied-by? edit (make-permission "users" "create")))))
(testing "checking actions and entities in empty permission"
(is (not (implied-by? edit (make-permission "users" "update")))))
(testing "checking actions and entities in empty permission"
(is (not (implied-by? edit (make-permission "users" "view")))))
(testing "checking actions and entities in empty permission"
(is (implied-by? edit (make-permission "users" wildcard-token))))
(testing "checking actions and entities in empty permission"
(is (implied-by? edit (make-permission "users:*"))))
(testing "checking actions and entities in empty permission"
(is (implied-by? edit (make-permission "users:edit"))))
(testing "checking actions and entities in empty permission"
(is (not (implied-by? edit (make-permission "users:edit:abc")))))
(testing "checking actions and entities in empty permission"
(is (not (implied-by? edit (make-permission "users:edit:1234abc")))))
(testing "checking actions and entities in empty permission"
(is (implied-by? edit (make-permission "users:edit:abcd1234"))))
)
)
(deftest test-against-permission-list
(let [perms [(make-permission "company:read")
(make-permission "company:update")
(make-permission "company:*")
(make-permission "users:*")]]
(testing
(is (implied-by? (make-permission "company:write") perms)))
(testing
(is (not (implied-by? (make-permission "tickets:write") perms))))
))