33namespace Tests \Unit \Authorization ;
44
55use PHPUnit \Framework \TestCase ;
6+ use Utopia \Database \PermissionType ;
67use Utopia \Database \Validator \Authorization ;
78use Utopia \Database \Validator \Authorization \Input ;
89
@@ -25,26 +26,26 @@ public function testDefaultRolesContainAny(): void
2526 public function testIsValidWithMatchingRole (): void
2627 {
2728 $ this ->auth ->addRole ('user:123 ' );
28- $ input = new Input (' read ' , ['user:123 ' ]);
29+ $ input = new Input (PermissionType::Read , ['user:123 ' ]);
2930 $ this ->assertTrue ($ this ->auth ->isValid ($ input ));
3031 }
3132
3233 public function testIsValidWithNonMatchingRole (): void
3334 {
3435 $ this ->auth ->addRole ('user:123 ' );
35- $ input = new Input (' read ' , ['user:456 ' ]);
36+ $ input = new Input (PermissionType::Read , ['user:456 ' ]);
3637 $ this ->assertFalse ($ this ->auth ->isValid ($ input ));
3738 }
3839
3940 public function testIsValidWithAnyRoleMatchesAllPermissions (): void
4041 {
41- $ input = new Input (' read ' , ['any ' ]);
42+ $ input = new Input (PermissionType::Read , ['any ' ]);
4243 $ this ->assertTrue ($ this ->auth ->isValid ($ input ));
4344 }
4445
4546 public function testIsValidReturnsFalseWithEmptyPermissions (): void
4647 {
47- $ input = new Input (' read ' , []);
48+ $ input = new Input (PermissionType::Read , []);
4849 $ this ->assertFalse ($ this ->auth ->isValid ($ input ));
4950 $ this ->assertStringContainsString ('No permissions provided ' , $ this ->auth ->getDescription ());
5051 }
@@ -89,7 +90,7 @@ public function testSkipBypassesAuthorization(): void
8990 {
9091 $ this ->auth ->cleanRoles ();
9192
92- $ input = new Input (' read ' , ['user:999 ' ]);
93+ $ input = new Input (PermissionType::Read , ['user:999 ' ]);
9394 $ this ->assertFalse ($ this ->auth ->isValid ($ input ));
9495
9596 $ result = $ this ->auth ->skip (function () use ($ input ) {
@@ -129,63 +130,63 @@ public function testIsValidWithMultipleRoles(): void
129130 $ this ->auth ->addRole ('user:123 ' );
130131 $ this ->auth ->addRole ('team:456 ' );
131132
132- $ input = new Input (' read ' , ['team:456 ' ]);
133+ $ input = new Input (PermissionType::Read , ['team:456 ' ]);
133134 $ this ->assertTrue ($ this ->auth ->isValid ($ input ));
134135 }
135136
136137 public function testIsValidWithMultiplePermissionsMatchesFirst (): void
137138 {
138139 $ this ->auth ->addRole ('user:123 ' );
139140
140- $ input = new Input (' read ' , ['user:123 ' , 'team:456 ' ]);
141+ $ input = new Input (PermissionType::Read , ['user:123 ' , 'team:456 ' ]);
141142 $ this ->assertTrue ($ this ->auth ->isValid ($ input ));
142143 }
143144
144145 public function testIsValidWithMultiplePermissionsMatchesLast (): void
145146 {
146147 $ this ->auth ->addRole ('team:456 ' );
147148
148- $ input = new Input (' read ' , ['user:123 ' , 'team:456 ' ]);
149+ $ input = new Input (PermissionType::Read , ['user:123 ' , 'team:456 ' ]);
149150 $ this ->assertTrue ($ this ->auth ->isValid ($ input ));
150151 }
151152
152153 public function testIsValidWithGuestsRole (): void
153154 {
154155 $ this ->auth ->addRole ('guests ' );
155156
156- $ input = new Input (' read ' , ['guests ' ]);
157+ $ input = new Input (PermissionType::Read , ['guests ' ]);
157158 $ this ->assertTrue ($ this ->auth ->isValid ($ input ));
158159 }
159160
160161 public function testIsValidWithUsersRole (): void
161162 {
162163 $ this ->auth ->addRole ('users ' );
163164
164- $ input = new Input (' read ' , ['users ' ]);
165+ $ input = new Input (PermissionType::Read , ['users ' ]);
165166 $ this ->assertTrue ($ this ->auth ->isValid ($ input ));
166167 }
167168
168169 public function testIsValidWithDimensionalRole (): void
169170 {
170171 $ this ->auth ->addRole ('user:123/admin ' );
171172
172- $ input = new Input (' read ' , ['user:123/admin ' ]);
173+ $ input = new Input (PermissionType::Read , ['user:123/admin ' ]);
173174 $ this ->assertTrue ($ this ->auth ->isValid ($ input ));
174175 }
175176
176177 public function testDimensionalRoleDoesNotMatchWithoutDimension (): void
177178 {
178179 $ this ->auth ->addRole ('user:123/admin ' );
179180
180- $ input = new Input (' read ' , ['user:123 ' ]);
181+ $ input = new Input (PermissionType::Read , ['user:123 ' ]);
181182 $ this ->assertFalse ($ this ->auth ->isValid ($ input ));
182183 }
183184
184185 public function testNonDimensionalRoleDoesNotMatchWithDimension (): void
185186 {
186187 $ this ->auth ->addRole ('user:123 ' );
187188
188- $ input = new Input (' read ' , ['user:123/admin ' ]);
189+ $ input = new Input (PermissionType::Read , ['user:123/admin ' ]);
189190 $ this ->assertFalse ($ this ->auth ->isValid ($ input ));
190191 }
191192
@@ -194,7 +195,7 @@ public function testGetDescriptionOnFailure(): void
194195 $ this ->auth ->cleanRoles ();
195196 $ this ->auth ->addRole ('user:123 ' );
196197
197- $ input = new Input (' read ' , ['team:456 ' ]);
198+ $ input = new Input (PermissionType::Read , ['team:456 ' ]);
198199 $ this ->assertFalse ($ this ->auth ->isValid ($ input ));
199200
200201 $ description = $ this ->auth ->getDescription ();
@@ -204,7 +205,7 @@ public function testGetDescriptionOnFailure(): void
204205
205206 public function testGetDescriptionOnEmptyPermissions (): void
206207 {
207- $ input = new Input (' write ' , []);
208+ $ input = new Input (PermissionType::Write , []);
208209 $ this ->assertFalse ($ this ->auth ->isValid ($ input ));
209210 $ this ->assertStringContainsString ("No permissions provided for action 'write' " , $ this ->auth ->getDescription ());
210211 }
@@ -236,7 +237,7 @@ public function testDisabledAuthorizationBypassesAllChecks(): void
236237 $ this ->auth ->disable ();
237238 $ this ->auth ->cleanRoles ();
238239
239- $ input = new Input (' read ' , ['user:999 ' ]);
240+ $ input = new Input (PermissionType::Read , ['user:999 ' ]);
240241 $ this ->assertTrue ($ this ->auth ->isValid ($ input ));
241242 }
242243
@@ -263,39 +264,39 @@ public function testPermissionTypeMatchingRead(): void
263264 {
264265 $ this ->auth ->addRole ('user:123 ' );
265266
266- $ input = new Input (' read ' , ['user:123 ' ]);
267+ $ input = new Input (PermissionType::Read , ['user:123 ' ]);
267268 $ this ->assertTrue ($ this ->auth ->isValid ($ input ));
268269 }
269270
270271 public function testPermissionTypeMatchingCreate (): void
271272 {
272273 $ this ->auth ->addRole ('user:123 ' );
273274
274- $ input = new Input (' create ' , ['user:123 ' ]);
275+ $ input = new Input (PermissionType::Create , ['user:123 ' ]);
275276 $ this ->assertTrue ($ this ->auth ->isValid ($ input ));
276277 }
277278
278279 public function testPermissionTypeMatchingUpdate (): void
279280 {
280281 $ this ->auth ->addRole ('user:123 ' );
281282
282- $ input = new Input (' update ' , ['user:123 ' ]);
283+ $ input = new Input (PermissionType::Update , ['user:123 ' ]);
283284 $ this ->assertTrue ($ this ->auth ->isValid ($ input ));
284285 }
285286
286287 public function testPermissionTypeMatchingDelete (): void
287288 {
288289 $ this ->auth ->addRole ('user:123 ' );
289290
290- $ input = new Input (' delete ' , ['user:123 ' ]);
291+ $ input = new Input (PermissionType::Delete , ['user:123 ' ]);
291292 $ this ->assertTrue ($ this ->auth ->isValid ($ input ));
292293 }
293294
294295 public function testPermissionTypeMatchingWrite (): void
295296 {
296297 $ this ->auth ->addRole ('user:123 ' );
297298
298- $ input = new Input (' write ' , ['user:123 ' ]);
299+ $ input = new Input (PermissionType::Write , ['user:123 ' ]);
299300 $ this ->assertTrue ($ this ->auth ->isValid ($ input ));
300301 }
301302
@@ -320,11 +321,11 @@ public function testGetType(): void
320321
321322 public function testInputSettersAndGetters (): void
322323 {
323- $ input = new Input (' read ' , ['user:123 ' ]);
324+ $ input = new Input (PermissionType::Read , ['user:123 ' ]);
324325 $ this ->assertEquals ('read ' , $ input ->getAction ());
325326 $ this ->assertEquals (['user:123 ' ], $ input ->getPermissions ());
326327
327- $ input ->setAction (' write ' );
328+ $ input ->setAction (PermissionType::Write );
328329 $ this ->assertEquals ('write ' , $ input ->getAction ());
329330
330331 $ input ->setPermissions (['team:456 ' ]);
@@ -335,10 +336,10 @@ public function testIsValidWithTeamDimensionRole(): void
335336 {
336337 $ this ->auth ->addRole ('team:abc/owner ' );
337338
338- $ input = new Input (' read ' , ['team:abc/owner ' ]);
339+ $ input = new Input (PermissionType::Read , ['team:abc/owner ' ]);
339340 $ this ->assertTrue ($ this ->auth ->isValid ($ input ));
340341
341- $ input = new Input (' read ' , ['team:abc/member ' ]);
342+ $ input = new Input (PermissionType::Read , ['team:abc/member ' ]);
342343 $ this ->assertFalse ($ this ->auth ->isValid ($ input ));
343344 }
344345
@@ -361,21 +362,21 @@ public function testLabelRole(): void
361362 {
362363 $ this ->auth ->addRole ('label:vip ' );
363364
364- $ input = new Input (' read ' , ['label:vip ' ]);
365+ $ input = new Input (PermissionType::Read , ['label:vip ' ]);
365366 $ this ->assertTrue ($ this ->auth ->isValid ($ input ));
366367
367- $ input = new Input (' read ' , ['label:premium ' ]);
368+ $ input = new Input (PermissionType::Read , ['label:premium ' ]);
368369 $ this ->assertFalse ($ this ->auth ->isValid ($ input ));
369370 }
370371
371372 public function testMemberRole (): void
372373 {
373374 $ this ->auth ->addRole ('member:abc123 ' );
374375
375- $ input = new Input (' read ' , ['member:abc123 ' ]);
376+ $ input = new Input (PermissionType::Read , ['member:abc123 ' ]);
376377 $ this ->assertTrue ($ this ->auth ->isValid ($ input ));
377378
378- $ input = new Input (' read ' , ['member:def456 ' ]);
379+ $ input = new Input (PermissionType::Read , ['member:def456 ' ]);
379380 $ this ->assertFalse ($ this ->auth ->isValid ($ input ));
380381 }
381382}
0 commit comments