1
+ <?php
2
+
3
+ namespace Lauthz \Tests ;
4
+
5
+ use Lauthz \Facades \Enforcer ;
6
+ use InvalidArgumentException ;
7
+ use RuntimeException ;
8
+
9
+
10
+ class ModelLoaderTest extends TestCase
11
+ {
12
+ public function testUrlLoader (): void
13
+ {
14
+ $ this ->initUrlConfig ();
15
+
16
+ $ this ->assertFalse (Enforcer::enforce ('alice ' , 'data ' , 'read ' ));
17
+
18
+ Enforcer::addPolicy ('data_admin ' , 'data ' , 'read ' );
19
+ Enforcer::addRoleForUser ('alice ' , 'data_admin ' );
20
+ $ this ->assertTrue (Enforcer::enforce ('alice ' , 'data ' , 'read ' ));
21
+ }
22
+
23
+ public function testTextLoader (): void
24
+ {
25
+ $ this ->initTextConfig ();
26
+
27
+ Enforcer::addPolicy ('data_admin ' , 'data ' , 'read ' );
28
+ $ this ->assertFalse (Enforcer::enforce ('alice ' , 'data ' , 'read ' ));
29
+ $ this ->assertTrue (Enforcer::enforce ('data_admin ' , 'data ' , 'read ' ));
30
+ }
31
+
32
+ public function testFileLoader (): void
33
+ {
34
+ $ this ->assertFalse (Enforcer::enforce ('alice ' , 'data ' , 'read ' ));
35
+
36
+ Enforcer::addPolicy ('data_admin ' , 'data ' , 'read ' );
37
+ Enforcer::addRoleForUser ('alice ' , 'data_admin ' );
38
+ $ this ->assertTrue (Enforcer::enforce ('alice ' , 'data ' , 'read ' ));
39
+ }
40
+
41
+ public function testCustomLoader (): void
42
+ {
43
+ $ this ->initCustomConfig ();
44
+ Enforcer::guard ('second ' )->addPolicy ('data_admin ' , 'data ' , 'read ' );
45
+ $ this ->assertFalse (Enforcer::guard ('second ' )->enforce ('alice ' , 'data ' , 'read ' ));
46
+ $ this ->assertTrue (Enforcer::guard ('second ' )->enforce ('data_admin ' , 'data ' , 'read ' ));
47
+ }
48
+
49
+ public function testMultipleLoader (): void
50
+ {
51
+ $ this ->testFileLoader ();
52
+ $ this ->testCustomLoader ();
53
+ }
54
+
55
+ public function testEmptyModel (): void
56
+ {
57
+ Enforcer::shouldUse ('third ' );
58
+ $ this ->expectException (InvalidArgumentException::class);
59
+ $ this ->assertFalse (Enforcer::enforce ('alice ' , 'data ' , 'read ' ));
60
+ }
61
+
62
+ public function testEmptyLoaderType (): void
63
+ {
64
+ $ this ->app ['config ' ]->set ('lauthz.basic.model.config_type ' , '' );
65
+ $ this ->expectException (InvalidArgumentException::class);
66
+
67
+ $ this ->assertFalse (Enforcer::enforce ('alice ' , 'data ' , 'read ' ));
68
+ }
69
+
70
+ public function testBadUlrConnection (): void
71
+ {
72
+ $ this ->initUrlConfig ();
73
+ $ this ->app ['config ' ]->set ('lauthz.basic.model.config_url ' , 'http://filenoexists ' );
74
+ $ this ->expectException (RuntimeException::class);
75
+
76
+ $ this ->assertFalse (Enforcer::enforce ('alice ' , 'data ' , 'read ' ));
77
+ }
78
+
79
+ protected function initUrlConfig (): void
80
+ {
81
+ $ this ->app ['config ' ]->set ('lauthz.basic.model.config_type ' , 'url ' );
82
+ $ this ->app ['config ' ]->set (
83
+ 'lauthz.basic.model.config_url ' ,
84
+ 'https://raw.githubusercontent.com/casbin/casbin/master/examples/rbac_model.conf '
85
+ );
86
+ }
87
+
88
+ protected function initTextConfig (): void
89
+ {
90
+ $ this ->app ['config ' ]->set ('lauthz.basic.model.config_type ' , 'text ' );
91
+ $ this ->app ['config ' ]->set (
92
+ 'lauthz.basic.model.config_text ' ,
93
+ $ this ->getModelText ()
94
+ );
95
+ }
96
+
97
+ protected function initCustomConfig (): void {
98
+ $ this ->app ['config ' ]->set ('lauthz.second.model.config_loader_class ' , '\Lauthz\Loaders\TextLoader ' );
99
+ $ this ->app ['config ' ]->set (
100
+ 'lauthz.second.model.config_text ' ,
101
+ $ this ->getModelText ()
102
+ );
103
+ }
104
+
105
+ protected function getModelText (): string
106
+ {
107
+ return <<<EOT
108
+ [request_definition]
109
+ r = sub, obj, act
110
+
111
+ [policy_definition]
112
+ p = sub, obj, act
113
+
114
+ [policy_effect]
115
+ e = some(where (p.eft == allow))
116
+
117
+ [matchers]
118
+ m = r.sub == p.sub && r.obj == p.obj && r.act == p.act
119
+ EOT ;
120
+ }
121
+ }
0 commit comments