Passed
Push — issue#767 ( 0909e2...b3675b )
by Guilherme
05:08
created

testGetUserNotPersonInterface()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 15
nc 1
nop 0
dl 0
loc 22
rs 9.2
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of the login-cidadao project or it's bundles.
4
 *
5
 * (c) Guilherme Donato <guilhermednt on github>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace LoginCidadao\CoreBundle\Tests\Helper;
12
13
use LoginCidadao\APIBundle\Entity\ActionLogRepository;
14
use LoginCidadao\CoreBundle\Entity\Person;
15
use LoginCidadao\CoreBundle\Helper\ExtremeNotificationsHelper;
16
use LoginCidadao\CoreBundle\Helper\SecurityHelper;
17
use LoginCidadao\CoreBundle\Model\PersonInterface;
18
use Symfony\Component\HttpFoundation\HeaderBag;
19
use Symfony\Component\HttpFoundation\Request;
20
use Symfony\Component\HttpFoundation\Response;
21
use Symfony\Component\HttpFoundation\Session\SessionInterface;
22
use Symfony\Component\Routing\RouterInterface;
23
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
24
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
25
use Symfony\Component\Security\Core\User\User;
26
27
class SecurityHelperTest extends \PHPUnit_Framework_TestCase
28
{
29
    public function testGetLoggedInUserLevelNonDefault()
30
    {
31
        $authChecker = $this->getAuthChecker();
32
        $authChecker->expects($this->atLeastOnce())
33
            ->method('isGranted')
34
            ->willReturnMap([
35
                ['ROLE_SUPER_ADMIN', null, false],
36
                ['ROLE_ADMIN', null, false],
37
                ['ROLE_SUPER_USER', null, true],
38
                ['ROLE_DEV', null, false],
39
                ['ROLE_USER', null, false],
40
            ]);
41
42
        $helper = new SecurityHelper(
43
            $authChecker,
44
            $this->getTokenStorage(),
45
            $this->getActionLogRepository(),
46
            $this->getExtremeNotificationsHelper(),
47
            $this->getRouter(),
48
            'cookieName'
49
        );
50
51
        $this->assertSame(2, $helper->getLoggedInUserLevel());
52
    }
53
54
    public function testGetLoggedInUserLevelDefault()
55
    {
56
        $authChecker = $this->getAuthChecker();
57
        $authChecker->expects($this->atLeastOnce())
58
            ->method('isGranted');
59
60
        $helper = new SecurityHelper(
61
            $authChecker,
62
            $this->getTokenStorage(),
63
            $this->getActionLogRepository(),
64
            $this->getExtremeNotificationsHelper(),
65
            $this->getRouter(),
66
            'cookieName'
67
        );
68
69
        $this->assertSame(0, $helper->getLoggedInUserLevel());
70
    }
71
72
    public function testCheckNoPendingImpersonateReport()
73
    {
74
        $person = new Person();
75
76
        $repo = $this->getActionLogRepository();
77
        $repo->expects($this->once())
78
            ->method('countImpersonatonsWithoutReports')->with($person)
79
            ->willReturn(0);
80
81
        $helper = new SecurityHelper(
82
            $this->getAuthChecker(),
83
            $this->getTokenStorage(),
84
            $repo,
85
            $this->getExtremeNotificationsHelper(),
86
            $this->getRouter(),
87
            'cookieName'
88
        );
89
90
        $helper->checkPendingImpersonateReport($person);
91
    }
92
93
    public function testCheckPendingImpersonateReport()
94
    {
95
        $count = 2;
96
        $url = 'https://example.com';
97
        $person = new Person();
98
99
        $repo = $this->getActionLogRepository();
100
        $repo->expects($this->once())
101
            ->method('countImpersonatonsWithoutReports')
102
            ->willReturn($count);
103
104
        $router = $this->getRouter();
105
        $router->expects($this->once())
106
            ->method('generate')->with('lc_admin_impersonation_report_index')
107
            ->willReturn($url);
108
109
        $parameters = ['%url%' => $url, '%count%' => $count];
110
111
        $extremeNotifHelper = $this->getExtremeNotificationsHelper();
112
        $extremeNotifHelper->expects($this->once())
113
            ->method('addTransChoice')->with('admin.impersonation_report.pending.notification', $count, $parameters);
114
115
        $helper = new SecurityHelper(
116
            $this->getAuthChecker(),
117
            $this->getTokenStorage(),
118
            $repo,
119
            $extremeNotifHelper,
120
            $router,
121
            'cookieName'
122
        );
123
124
        $helper->checkPendingImpersonateReport($person);
125
    }
126
127
    public function testGetRoleLevel()
128
    {
129
        $helper = new SecurityHelper(
130
            $this->getAuthChecker(),
131
            $this->getTokenStorage(),
132
            $this->getActionLogRepository(),
133
            $this->getExtremeNotificationsHelper(),
134
            $this->getRouter(),
135
            'cookieName'
136
        );
137
138
        $roles = [
139
            'ROLE_SUPER_ADMIN' => 4,
140
            'ROLE_ADMIN' => 3,
141
            'ROLE_SUPER_USER' => 2,
142
            'ROLE_DEV' => 1,
143
            'ROLE_USER' => 0,
144
        ];
145
146
        foreach ($roles as $role => $expected) {
147
            $this->assertSame($expected, $helper->getRoleLevel($role));
148
        }
149
150
        $this->assertSame(4, $helper->getRoleLevel('OTHER_ROLE'));
151
    }
152
153
    public function testGetUser()
154
    {
155
        $person = new Person();
156
157
        $token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
158
        $token->expects($this->once())->method('getUser')->willReturn($person);
159
160
        $tokenStorage = $this->getTokenStorage();
161
        $tokenStorage->expects($this->once())
162
            ->method('getToken')
163
            ->willReturn($token);
164
165
        $helper = new SecurityHelper(
166
            $this->getAuthChecker(),
167
            $tokenStorage,
168
            $this->getActionLogRepository(),
169
            $this->getExtremeNotificationsHelper(),
170
            $this->getRouter(),
171
            'cookieName'
172
        );
173
174
        $this->assertSame($person, $helper->getUser());
175
    }
176
177
    public function testGetUserNotPersonInterface()
178
    {
179
        $user = new User('username', 'password');
180
181
        $token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
182
        $token->expects($this->once())->method('getUser')->willReturn($user);
183
184
        $tokenStorage = $this->getTokenStorage();
185
        $tokenStorage->expects($this->once())
186
            ->method('getToken')
187
            ->willReturn($token);
188
189
        $helper = new SecurityHelper(
190
            $this->getAuthChecker(),
191
            $tokenStorage,
192
            $this->getActionLogRepository(),
193
            $this->getExtremeNotificationsHelper(),
194
            $this->getRouter(),
195
            'cookieName'
196
        );
197
198
        $this->assertNull($helper->getUser());
199
    }
200
201
    public function testGetUserException()
202
    {
203
        $token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
204
        $token->expects($this->once())->method('getUser')->willThrowException(new \RuntimeException());
205
206
        $tokenStorage = $this->getTokenStorage();
207
        $tokenStorage->expects($this->once())
208
            ->method('getToken')
209
            ->willReturn($token);
210
211
        $helper = new SecurityHelper(
212
            $this->getAuthChecker(),
213
            $tokenStorage,
214
            $this->getActionLogRepository(),
215
            $this->getExtremeNotificationsHelper(),
216
            $this->getRouter(),
217
            'cookieName'
218
        );
219
220
        $this->assertNull($helper->getUser());
221
    }
222
223
    public function testGetTargetPersonLevel()
224
    {
225
        $personLvl3 = new Person();
226
        $personLvl3->setRoles([
227
            'ROLE_DEV',
228
            'ROLE_ADMIN',
229
        ]);
230
231
        /** @var PersonInterface|\PHPUnit_Framework_MockObject_MockObject $personLvlDefault */
232
        $personLvlDefault = $this->getMock('LoginCidadao\CoreBundle\Model\PersonInterface');
233
        $personLvlDefault->expects($this->once())
234
            ->method('getRoles')
235
            ->willReturn(['OTHER_ROLE']);
236
237
        $helper = new SecurityHelper(
238
            $this->getAuthChecker(),
239
            $this->getTokenStorage(),
240
            $this->getActionLogRepository(),
241
            $this->getExtremeNotificationsHelper(),
242
            $this->getRouter(),
243
            'cookieName'
244
        );
245
246
        $this->assertSame(3, $helper->getTargetPersonLevel($personLvl3));
247
        $this->assertSame(0, $helper->getTargetPersonLevel($personLvlDefault));
248
    }
249
250
    public function testLogout()
251
    {
252
        $rememberMe = 'cookieName';
253
254
        /** @var SessionInterface|\PHPUnit_Framework_MockObject_MockObject $session */
255
        $session = $this->getMock('Symfony\Component\HttpFoundation\Session\SessionInterface');
256
        $session->expects($this->once())->method('invalidate');
257
258
        $request = new Request();
259
        $request->setSession($session);
260
261
        $headers = $this->getMock('Symfony\Component\HttpFoundation\ResponseHeaderBag');
262
        $headers->expects($this->once())->method('clearCookie')->with($rememberMe);
263
264
        $response = new Response();
265
        $response->headers = $headers;
0 ignored issues
show
Documentation Bug introduced by
It seems like $headers of type PHPUnit_Framework_MockObject_MockObject is incompatible with the declared type Symfony\Component\HttpFoundation\ResponseHeaderBag of property $headers.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
266
267
        $tokenStorage = $this->getTokenStorage();
268
        $tokenStorage->expects($this->once())->method('setToken')->with(null);
269
270
        $helper = new SecurityHelper(
271
            $this->getAuthChecker(),
272
            $tokenStorage,
273
            $this->getActionLogRepository(),
274
            $this->getExtremeNotificationsHelper(),
275
            $this->getRouter(),
276
            $rememberMe
277
        );
278
279
        $helper->logout($request, $response);
280
    }
281
282
    public function testIsGranted()
283
    {
284
        $attributes = ['THE_ROLE'];
285
        $object = new \stdClass();
286
287
        $authChecker = $this->getAuthChecker();
288
        $authChecker->expects($this->atLeastOnce())
289
            ->method('isGranted')->with($attributes, $object)
290
            ->willReturn(true);
291
292
        $helper = new SecurityHelper(
293
            $authChecker,
294
            $this->getTokenStorage(),
295
            $this->getActionLogRepository(),
296
            $this->getExtremeNotificationsHelper(),
297
            $this->getRouter(),
298
            'cookieName'
299
        );
300
301
        $this->assertTrue($helper->isGranted($attributes, $object));
302
    }
303
304
    /**
305
     * @return AuthorizationCheckerInterface|\PHPUnit_Framework_MockObject_MockObject
306
     */
307
    private function getAuthChecker()
308
    {
309
        return $this->getMock('Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface');
310
    }
311
312
    /**
313
     * @return TokenStorageInterface|\PHPUnit_Framework_MockObject_MockObject
314
     */
315
    private function getTokenStorage()
316
    {
317
        return $this->getMock('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface');
318
    }
319
320
    /**
321
     * @return ActionLogRepository|\PHPUnit_Framework_MockObject_MockObject
322
     */
323
    private function getActionLogRepository()
324
    {
325
        return $this->getMockBuilder('LoginCidadao\APIBundle\Entity\ActionLogRepository')
326
            ->disableOriginalConstructor()->getMock();
327
    }
328
329
    /**
330
     * @return ExtremeNotificationsHelper|\PHPUnit_Framework_MockObject_MockObject
331
     */
332
    private function getExtremeNotificationsHelper()
333
    {
334
        return $this->getMockBuilder('LoginCidadao\CoreBundle\Helper\ExtremeNotificationsHelper')
335
            ->disableOriginalConstructor()->getMock();
336
    }
337
338
    /**
339
     * @return RouterInterface|\PHPUnit_Framework_MockObject_MockObject
340
     */
341
    private function getRouter()
342
    {
343
        return $this->getMock('Symfony\Component\Routing\RouterInterface');
344
    }
345
}
346