summaryrefslogtreecommitdiff
path: root/tests/units/Model/AclTest.php
blob: fef03990d3817313d411b7eae583c23dbe6e9cc7 (plain)
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
<?php

require_once __DIR__.'/../Base.php';

use Core\Session;
use Model\Acl;
use Model\Project;
use Model\ProjectPermission;
use Model\User;

class AclTest extends Base
{
    public function testMatchAcl()
    {
        $acl_rules = array(
            'controller1' => array('action1', 'action3'),
            'controller3' => '*',
            'controller5' => '-',
            'controller6' => array(),
        );

        $acl = new Acl($this->container);
        $this->assertTrue($acl->matchAcl($acl_rules, 'controller1', 'aCtiOn1'));
        $this->assertTrue($acl->matchAcl($acl_rules, 'controller1', 'action1'));
        $this->assertTrue($acl->matchAcl($acl_rules, 'controller1', 'action3'));
        $this->assertFalse($acl->matchAcl($acl_rules, 'controller1', 'action2'));
        $this->assertFalse($acl->matchAcl($acl_rules, 'controller2', 'action2'));
        $this->assertFalse($acl->matchAcl($acl_rules, 'controller2', 'action3'));
        $this->assertTrue($acl->matchAcl($acl_rules, 'controller3', 'anything'));
        $this->assertFalse($acl->matchAcl($acl_rules, 'controller4', 'anything'));
        $this->assertFalse($acl->matchAcl($acl_rules, 'controller5', 'anything'));
        $this->assertFalse($acl->matchAcl($acl_rules, 'controller6', 'anything'));
    }

    public function testPublicActions()
    {
        $acl = new Acl($this->container);
        $this->assertTrue($acl->isPublicAction('task', 'readonly'));
        $this->assertTrue($acl->isPublicAction('board', 'readonly'));
        $this->assertFalse($acl->isPublicAction('board', 'show'));
        $this->assertTrue($acl->isPublicAction('feed', 'project'));
        $this->assertTrue($acl->isPublicAction('feed', 'user'));
        $this->assertTrue($acl->isPublicAction('ical', 'project'));
        $this->assertTrue($acl->isPublicAction('ical', 'user'));
        $this->assertTrue($acl->isPublicAction('oauth', 'github'));
        $this->assertTrue($acl->isPublicAction('oauth', 'google'));
        $this->assertTrue($acl->isPublicAction('auth', 'login'));
        $this->assertTrue($acl->isPublicAction('auth', 'check'));
        $this->assertTrue($acl->isPublicAction('auth', 'captcha'));
    }

    public function testAdminActions()
    {
        $acl = new Acl($this->container);
        $this->assertFalse($acl->isAdminAction('board', 'show'));
        $this->assertFalse($acl->isAdminAction('task', 'show'));
        $this->assertTrue($acl->isAdminAction('config', 'api'));
        $this->assertTrue($acl->isAdminAction('config', 'anything'));
        $this->assertTrue($acl->isAdminAction('config', 'anything'));
        $this->assertTrue($acl->isAdminAction('user', 'save'));
    }

    public function testProjectAdminActions()
    {
        $acl = new Acl($this->container);
        $this->assertFalse($acl->isProjectAdminAction('config', 'save'));
        $this->assertFalse($acl->isProjectAdminAction('user', 'index'));
        $this->assertTrue($acl->isProjectAdminAction('project', 'remove'));
    }

    public function testProjectManagerActions()
    {
        $acl = new Acl($this->container);
        $this->assertFalse($acl->isProjectManagerAction('board', 'readonly'));
        $this->assertFalse($acl->isProjectManagerAction('project', 'remove'));
        $this->assertFalse($acl->isProjectManagerAction('project', 'show'));
        $this->assertTrue($acl->isProjectManagerAction('project', 'disable'));
        $this->assertTrue($acl->isProjectManagerAction('category', 'index'));
        $this->assertTrue($acl->isProjectManagerAction('project', 'users'));
        $this->assertFalse($acl->isProjectManagerAction('app', 'index'));
    }

    public function testPageAccessNoSession()
    {
        $acl = new Acl($this->container);
        $session = new Session;
        $session = array();

        $this->assertFalse($acl->isAllowed('board', 'readonly'));
        $this->assertFalse($acl->isAllowed('task', 'show'));
        $this->assertFalse($acl->isAllowed('config', 'application'));
        $this->assertFalse($acl->isAllowed('project', 'users'));
        $this->assertFalse($acl->isAllowed('task', 'remove'));
        $this->assertTrue($acl->isAllowed('app', 'index'));
    }

    public function testPageAccessEmptySession()
    {
        $acl = new Acl($this->container);
        $session = new Session;
        $session['user'] = array();

        $this->assertFalse($acl->isAllowed('board', 'readonly'));
        $this->assertFalse($acl->isAllowed('task', 'show'));
        $this->assertFalse($acl->isAllowed('config', 'application'));
        $this->assertFalse($acl->isAllowed('project', 'users'));
        $this->assertFalse($acl->isAllowed('task', 'remove'));
        $this->assertTrue($acl->isAllowed('app', 'index'));
    }

    public function testPageAccessAdminUser()
    {
        $acl = new Acl($this->container);
        $session = new Session;

        $session['user'] = array(
            'is_admin' => true,
        );

        $this->assertTrue($acl->isAllowed('board', 'readonly'));
        $this->assertTrue($acl->isAllowed('task', 'readonly'));
        $this->assertTrue($acl->isAllowed('webhook', 'github'));
        $this->assertTrue($acl->isAllowed('task', 'show'));
        $this->assertTrue($acl->isAllowed('task', 'update'));
        $this->assertTrue($acl->isAllowed('config', 'application'));
        $this->assertTrue($acl->isAllowed('project', 'show'));
        $this->assertTrue($acl->isAllowed('project', 'users'));
        $this->assertTrue($acl->isAllowed('project', 'remove'));
        $this->assertTrue($acl->isAllowed('category', 'edit'));
        $this->assertTrue($acl->isAllowed('task', 'remove'));
        $this->assertTrue($acl->isAllowed('app', 'index'));
    }

    public function testPageAccessProjectAdmin()
    {
        $acl = new Acl($this->container);
        $p = new Project($this->container);
        $pp = new ProjectPermission($this->container);
        $u = new User($this->container);
        $session = new Session;

        // We create our user
        $this->assertEquals(2, $u->create(array('username' => 'unittest', 'password' => 'unittest')));

        // We create a project and set our user as project manager
        $this->assertEquals(1, $p->create(array('name' => 'UnitTest')));
        $this->assertTrue($pp->addMember(1, 2));
        $this->assertTrue($pp->isMember(1, 2));
        $this->assertFalse($pp->isManager(1, 2));

        // We fake a session for him
        $session['user'] = array(
            'id' => 2,
            'is_admin' => false,
            'is_project_admin' => true,
        );

        $this->assertTrue($acl->isAllowed('board', 'readonly', 1));
        $this->assertTrue($acl->isAllowed('task', 'readonly', 1));
        $this->assertTrue($acl->isAllowed('webhook', 'github', 1));
        $this->assertTrue($acl->isAllowed('task', 'show', 1));
        $this->assertFalse($acl->isAllowed('task', 'show', 2));
        $this->assertTrue($acl->isAllowed('task', 'update', 1));
        $this->assertTrue($acl->isAllowed('project', 'show', 1));
        $this->assertFalse($acl->isAllowed('config', 'application', 1));

        $this->assertTrue($acl->isAllowed('project', 'users', 1));
        $this->assertFalse($acl->isAllowed('project', 'users', 2));

        $this->assertTrue($acl->isAllowed('project', 'remove', 1));
        $this->assertFalse($acl->isAllowed('project', 'remove', 2));

        $this->assertTrue($acl->isAllowed('category', 'edit', 1));
        $this->assertTrue($acl->isAllowed('task', 'remove', 1));
        $this->assertTrue($acl->isAllowed('app', 'index', 1));
    }

    public function testPageAccessProjectManager()
    {
        $acl = new Acl($this->container);
        $p = new Project($this->container);
        $pp = new ProjectPermission($this->container);
        $u = new User($this->container);
        $session = new Session;

        // We create our user
        $this->assertEquals(2, $u->create(array('username' => 'unittest', 'password' => 'unittest')));

        // We create a project and set our user as project manager
        $this->assertEquals(1, $p->create(array('name' => 'UnitTest'), 2, true));
        $this->assertTrue($pp->isMember(1, 2));
        $this->assertTrue($pp->isManager(1, 2));

        // We fake a session for him
        $session['user'] = array(
            'id' => 2,
            'is_admin' => false,
        );

        $this->assertTrue($acl->isAllowed('board', 'readonly', 1));
        $this->assertTrue($acl->isAllowed('task', 'readonly', 1));
        $this->assertTrue($acl->isAllowed('webhook', 'github', 1));
        $this->assertTrue($acl->isAllowed('task', 'show', 1));
        $this->assertFalse($acl->isAllowed('task', 'show', 2));
        $this->assertTrue($acl->isAllowed('task', 'update', 1));
        $this->assertTrue($acl->isAllowed('project', 'show', 1));
        $this->assertFalse($acl->isAllowed('config', 'application', 1));

        $this->assertTrue($acl->isAllowed('project', 'users', 1));
        $this->assertFalse($acl->isAllowed('project', 'users', 2));

        $this->assertFalse($acl->isAllowed('project', 'remove', 1));
        $this->assertFalse($acl->isAllowed('project', 'remove', 2));

        $this->assertTrue($acl->isAllowed('category', 'edit', 1));
        $this->assertTrue($acl->isAllowed('task', 'remove', 1));
        $this->assertTrue($acl->isAllowed('app', 'index', 1));
    }

    public function testPageAccessMember()
    {
        $acl = new Acl($this->container);
        $p = new Project($this->container);
        $pp = new ProjectPermission($this->container);
        $u = new User($this->container);

        // We create our user
        $this->assertEquals(2, $u->create(array('username' => 'unittest', 'password' => 'unittest')));

        // We create a project and set our user as member
        $this->assertEquals(1, $p->create(array('name' => 'UnitTest1')));
        $this->assertEquals(2, $p->create(array('name' => 'UnitTest2')));
        $this->assertTrue($pp->addMember(1, 2));
        $this->assertTrue($pp->isMember(1, 2));
        $this->assertFalse($pp->isManager(1, 2));

        $session = new Session;

        $session['user'] = array(
            'id' => 2,
            'is_admin' => false,
        );

        $this->assertTrue($acl->isAllowed('board', 'readonly', 1));
        $this->assertTrue($acl->isAllowed('task', 'readonly', 1));
        $this->assertTrue($acl->isAllowed('webhook', 'github', 1));
        $this->assertFalse($acl->isAllowed('board', 'show', 2));
        $this->assertTrue($acl->isAllowed('board', 'show', 1));
        $this->assertFalse($acl->isAllowed('task', 'show', 2));
        $this->assertTrue($acl->isAllowed('task', 'show', 1));
        $this->assertTrue($acl->isAllowed('task', 'update', 1));
        $this->assertTrue($acl->isAllowed('project', 'show', 1));
        $this->assertFalse($acl->isAllowed('config', 'application', 1));
        $this->assertFalse($acl->isAllowed('project', 'users', 1));
        $this->assertTrue($acl->isAllowed('task', 'remove', 1));
        $this->assertFalse($acl->isAllowed('task', 'remove', 2));
        $this->assertTrue($acl->isAllowed('app', 'index', 1));
    }

    public function testPageAccessNotMember()
    {
        $acl = new Acl($this->container);
        $p = new Project($this->container);
        $pp = new ProjectPermission($this->container);
        $u = new User($this->container);

        // We create our user
        $this->assertEquals(2, $u->create(array('username' => 'unittest', 'password' => 'unittest')));

        // We create a project and set our user as member
        $this->assertEquals(1, $p->create(array('name' => 'UnitTest1')));
        $this->assertEquals(2, $p->create(array('name' => 'UnitTest2')));
        $this->assertFalse($pp->isMember(1, 2));
        $this->assertFalse($pp->isManager(1, 2));

        $session = new Session;

        $session['user'] = array(
            'id' => 2,
            'is_admin' => false,
        );

        $this->assertFalse($acl->isAllowed('board', 'show', 2));
        $this->assertFalse($acl->isAllowed('board', 'show', 1));
        $this->assertFalse($acl->isAllowed('task', 'show', 1));
        $this->assertFalse($acl->isAllowed('task', 'update', 1));
        $this->assertFalse($acl->isAllowed('project', 'show', 1));
        $this->assertFalse($acl->isAllowed('config', 'application', 1));
        $this->assertFalse($acl->isAllowed('project', 'users', 1));
        $this->assertFalse($acl->isAllowed('task', 'remove', 1));
        $this->assertTrue($acl->isAllowed('app', 'index', 1));
    }
}