1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* File containing the RestSessionBasedAuthenticatorTest class. |
5
|
|
|
* |
6
|
|
|
* @copyright Copyright (C) eZ Systems AS. All rights reserved. |
7
|
|
|
* @license For full copyright and license information view LICENSE file distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
namespace eZ\Publish\Core\REST\Server\Tests\Security; |
10
|
|
|
|
11
|
|
|
use eZ\Publish\Core\REST\Server\Security\RestAuthenticator; |
12
|
|
|
use eZ\Publish\Core\MVC\Symfony\Security\User as EzUser; |
13
|
|
|
use PHPUnit\Framework\TestCase; |
14
|
|
|
use Symfony\Component\HttpFoundation\Request; |
15
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken; |
16
|
|
|
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent; |
17
|
|
|
use Symfony\Component\Security\Http\SecurityEvents; |
18
|
|
|
|
19
|
|
|
class RestSessionBasedAuthenticatorTest extends TestCase |
20
|
|
|
{ |
21
|
|
|
const PROVIDER_KEY = 'test_key'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject |
25
|
|
|
*/ |
26
|
|
|
private $tokenStorage; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject |
30
|
|
|
*/ |
31
|
|
|
private $authenticationManager; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject |
35
|
|
|
*/ |
36
|
|
|
private $eventDispatcher; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject |
40
|
|
|
*/ |
41
|
|
|
private $configResolver; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject |
45
|
|
|
*/ |
46
|
|
|
private $sessionStorage; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject |
50
|
|
|
*/ |
51
|
|
|
private $logger; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var \eZ\Publish\Core\REST\Server\Security\RestAuthenticator |
55
|
|
|
*/ |
56
|
|
|
private $authenticator; |
57
|
|
|
|
58
|
|
|
protected function setUp() |
59
|
|
|
{ |
60
|
|
|
parent::setUp(); |
61
|
|
|
$this->tokenStorage = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface'); |
62
|
|
|
$this->authenticationManager = $this->getMock('Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface'); |
63
|
|
|
$this->eventDispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface'); |
64
|
|
|
$this->configResolver = $this->getMock('eZ\Publish\Core\MVC\ConfigResolverInterface'); |
65
|
|
|
$this->sessionStorage = $this->getMock('Symfony\Component\HttpFoundation\Session\Storage\SessionStorageInterface'); |
66
|
|
|
$this->logger = $this->getMock('Psr\Log\LoggerInterface'); |
67
|
|
|
$this->authenticator = new RestAuthenticator( |
68
|
|
|
$this->tokenStorage, |
69
|
|
|
$this->authenticationManager, |
70
|
|
|
self::PROVIDER_KEY, |
71
|
|
|
$this->eventDispatcher, |
72
|
|
|
$this->configResolver, |
73
|
|
|
$this->sessionStorage, |
74
|
|
|
$this->logger |
75
|
|
|
); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function testAuthenticateAlreadyHaveSessionToken() |
79
|
|
|
{ |
80
|
|
|
$username = 'foo_user'; |
81
|
|
|
$password = 'publish'; |
82
|
|
|
|
83
|
|
|
$existingToken = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface'); |
84
|
|
|
$this->tokenStorage |
85
|
|
|
->expects($this->once()) |
86
|
|
|
->method('getToken') |
87
|
|
|
->will($this->returnValue($existingToken)); |
88
|
|
|
|
89
|
|
|
$existingToken |
90
|
|
|
->expects($this->once()) |
91
|
|
|
->method('getUsername') |
92
|
|
|
->will($this->returnValue($username)); |
93
|
|
|
$existingToken |
94
|
|
|
->expects($this->once()) |
95
|
|
|
->method('setAttribute') |
96
|
|
|
->with('isFromSession', true); |
97
|
|
|
|
98
|
|
|
$request = new Request(); |
99
|
|
|
$request->attributes->set('username', $username); |
100
|
|
|
$request->attributes->set('password', $password); |
101
|
|
|
|
102
|
|
|
$this->assertSame($existingToken, $this->authenticator->authenticate($request)); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @expectedException \Symfony\Component\Security\Core\Exception\TokenNotFoundException |
107
|
|
|
*/ |
108
|
|
|
public function testAuthenticateNoTokenFound() |
109
|
|
|
{ |
110
|
|
|
$username = 'foo_user'; |
111
|
|
|
$password = 'publish'; |
112
|
|
|
|
113
|
|
|
$existingToken = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface'); |
114
|
|
|
$this->tokenStorage |
115
|
|
|
->expects($this->once()) |
116
|
|
|
->method('getToken') |
117
|
|
|
->will($this->returnValue($existingToken)); |
118
|
|
|
|
119
|
|
|
$existingToken |
120
|
|
|
->expects($this->once()) |
121
|
|
|
->method('getUsername') |
122
|
|
|
->will($this->returnValue(__METHOD__)); |
123
|
|
|
|
124
|
|
|
$request = new Request(); |
125
|
|
|
$request->attributes->set('username', $username); |
126
|
|
|
$request->attributes->set('password', $password); |
127
|
|
|
|
128
|
|
|
$usernamePasswordToken = new UsernamePasswordToken($username, $password, self::PROVIDER_KEY); |
129
|
|
|
$this->authenticationManager |
130
|
|
|
->expects($this->once()) |
131
|
|
|
->method('authenticate') |
132
|
|
|
->with($this->equalTo($usernamePasswordToken)) |
133
|
|
|
->will($this->returnValue(null)); |
134
|
|
|
|
135
|
|
|
$this->logger |
136
|
|
|
->expects($this->once()) |
137
|
|
|
->method('error'); |
138
|
|
|
|
139
|
|
|
$this->authenticator->authenticate($request); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @expectedException \eZ\Publish\Core\REST\Server\Exceptions\InvalidUserTypeException |
144
|
|
|
*/ |
145
|
|
|
public function testAuthenticateInvalidUser() |
146
|
|
|
{ |
147
|
|
|
$username = 'foo_user'; |
148
|
|
|
$password = 'publish'; |
149
|
|
|
|
150
|
|
|
$existingToken = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface'); |
151
|
|
|
$existingToken |
152
|
|
|
->expects($this->once()) |
153
|
|
|
->method('getUsername') |
154
|
|
|
->will($this->returnValue(__METHOD__)); |
155
|
|
|
|
156
|
|
|
$request = new Request(); |
157
|
|
|
$request->attributes->set('username', $username); |
158
|
|
|
$request->attributes->set('password', $password); |
159
|
|
|
|
160
|
|
|
$usernamePasswordToken = new UsernamePasswordToken($username, $password, self::PROVIDER_KEY); |
161
|
|
|
$authenticatedToken = $this |
162
|
|
|
->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken') |
163
|
|
|
->disableOriginalConstructor() |
164
|
|
|
->getMock(); |
165
|
|
|
$this->authenticationManager |
166
|
|
|
->expects($this->once()) |
167
|
|
|
->method('authenticate') |
168
|
|
|
->with($this->equalTo($usernamePasswordToken)) |
169
|
|
|
->will($this->returnValue($authenticatedToken)); |
170
|
|
|
|
171
|
|
|
$this->tokenStorage |
172
|
|
|
->expects($this->once()) |
173
|
|
|
->method('setToken') |
174
|
|
|
->with($authenticatedToken); |
175
|
|
|
|
176
|
|
|
$this->eventDispatcher |
177
|
|
|
->expects($this->once()) |
178
|
|
|
->method('dispatch') |
179
|
|
|
->with( |
180
|
|
|
SecurityEvents::INTERACTIVE_LOGIN, |
181
|
|
|
$this->equalTo(new InteractiveLoginEvent($request, $authenticatedToken)) |
182
|
|
|
); |
183
|
|
|
|
184
|
|
|
$this->tokenStorage |
185
|
|
|
->expects($this->exactly(2)) |
186
|
|
|
->method('getToken') |
187
|
|
|
->will( |
188
|
|
|
$this->onConsecutiveCalls($existingToken, $authenticatedToken) |
189
|
|
|
); |
190
|
|
|
|
191
|
|
|
$authenticatedToken |
192
|
|
|
->expects($this->once()) |
193
|
|
|
->method('getUser') |
194
|
|
|
->will($this->returnValue('not_an_ez_user')); |
195
|
|
|
|
196
|
|
|
$this->logger |
197
|
|
|
->expects($this->once()) |
198
|
|
|
->method('error'); |
199
|
|
|
|
200
|
|
|
$this->authenticator->authenticate($request); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @param $userId |
205
|
|
|
* |
206
|
|
|
* @return EzUser |
207
|
|
|
*/ |
208
|
|
|
private function createUser($userId) |
209
|
|
|
{ |
210
|
|
|
$apiUser = $this->getMock('eZ\Publish\API\Repository\Values\User\User'); |
211
|
|
|
$apiUser |
212
|
|
|
->expects($this->any()) |
213
|
|
|
->method('__get') |
214
|
|
|
->with('id') |
215
|
|
|
->will($this->returnValue($userId)); |
216
|
|
|
|
217
|
|
|
return new EzUser($apiUser); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* @expectedException \eZ\Publish\Core\REST\Server\Exceptions\UserConflictException |
222
|
|
|
*/ |
223
|
|
|
public function testAuthenticateUserConflict() |
224
|
|
|
{ |
225
|
|
|
$username = 'foo_user'; |
226
|
|
|
$password = 'publish'; |
227
|
|
|
|
228
|
|
|
$existingUser = $this->createUser(123); |
229
|
|
|
$existingToken = $this |
230
|
|
|
->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken') |
231
|
|
|
->disableOriginalConstructor() |
232
|
|
|
->getMock(); |
233
|
|
|
$existingToken |
234
|
|
|
->expects($this->once()) |
235
|
|
|
->method('getUsername') |
236
|
|
|
->will($this->returnValue(__METHOD__)); |
237
|
|
|
$existingToken |
238
|
|
|
->expects($this->once()) |
239
|
|
|
->method('getUser') |
240
|
|
|
->will($this->returnValue($existingUser)); |
241
|
|
|
|
242
|
|
|
$request = new Request(); |
243
|
|
|
$request->attributes->set('username', $username); |
244
|
|
|
$request->attributes->set('password', $password); |
245
|
|
|
|
246
|
|
|
$usernamePasswordToken = new UsernamePasswordToken($username, $password, self::PROVIDER_KEY); |
247
|
|
|
$authenticatedToken = $this |
248
|
|
|
->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken') |
249
|
|
|
->disableOriginalConstructor() |
250
|
|
|
->getMock(); |
251
|
|
|
$this->authenticationManager |
252
|
|
|
->expects($this->once()) |
253
|
|
|
->method('authenticate') |
254
|
|
|
->with($this->equalTo($usernamePasswordToken)) |
255
|
|
|
->will($this->returnValue($authenticatedToken)); |
256
|
|
|
|
257
|
|
|
$this->eventDispatcher |
258
|
|
|
->expects($this->once()) |
259
|
|
|
->method('dispatch') |
260
|
|
|
->with( |
261
|
|
|
SecurityEvents::INTERACTIVE_LOGIN, |
262
|
|
|
$this->equalTo(new InteractiveLoginEvent($request, $authenticatedToken)) |
263
|
|
|
); |
264
|
|
|
|
265
|
|
|
$this->tokenStorage |
266
|
|
|
->expects($this->at(0)) |
267
|
|
|
->method('getToken') |
268
|
|
|
->will($this->returnValue($existingToken)); |
269
|
|
|
$this->tokenStorage |
270
|
|
|
->expects($this->at(1)) |
271
|
|
|
->method('setToken') |
272
|
|
|
->with($authenticatedToken); |
273
|
|
|
$this->tokenStorage |
274
|
|
|
->expects($this->at(2)) |
275
|
|
|
->method('getToken') |
276
|
|
|
->will($this->returnValue($authenticatedToken)); |
277
|
|
|
$this->tokenStorage |
278
|
|
|
->expects($this->at(3)) |
279
|
|
|
->method('setToken') |
280
|
|
|
->with($existingToken); |
281
|
|
|
|
282
|
|
|
$authenticatedUser = $this->createUser(456); |
283
|
|
|
$authenticatedToken |
284
|
|
|
->expects($this->once()) |
285
|
|
|
->method('getUser') |
286
|
|
|
->will($this->returnValue($authenticatedUser)); |
287
|
|
|
|
288
|
|
|
$this->configResolver |
289
|
|
|
->expects($this->once()) |
290
|
|
|
->method('getParameter') |
291
|
|
|
->with('anonymous_user_id') |
292
|
|
|
->will($this->returnValue(10)); |
293
|
|
|
|
294
|
|
|
$this->authenticator->authenticate($request); |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
public function testAuthenticatePreviouslyAnonymous() |
298
|
|
|
{ |
299
|
|
|
$username = 'foo_user'; |
300
|
|
|
$password = 'publish'; |
301
|
|
|
|
302
|
|
|
$anonymousUserId = 10; |
303
|
|
|
$existingUser = $this->createUser($anonymousUserId); |
304
|
|
|
$existingToken = $this |
305
|
|
|
->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken') |
306
|
|
|
->disableOriginalConstructor() |
307
|
|
|
->getMock(); |
308
|
|
|
$existingToken |
309
|
|
|
->expects($this->once()) |
310
|
|
|
->method('getUsername') |
311
|
|
|
->will($this->returnValue(__METHOD__)); |
312
|
|
|
$existingToken |
313
|
|
|
->expects($this->once()) |
314
|
|
|
->method('getUser') |
315
|
|
|
->will($this->returnValue($existingUser)); |
316
|
|
|
|
317
|
|
|
$request = new Request(); |
318
|
|
|
$request->attributes->set('username', $username); |
319
|
|
|
$request->attributes->set('password', $password); |
320
|
|
|
|
321
|
|
|
$usernamePasswordToken = new UsernamePasswordToken($username, $password, self::PROVIDER_KEY); |
322
|
|
|
$authenticatedToken = $this |
323
|
|
|
->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken') |
324
|
|
|
->disableOriginalConstructor() |
325
|
|
|
->getMock(); |
326
|
|
|
$this->authenticationManager |
327
|
|
|
->expects($this->once()) |
328
|
|
|
->method('authenticate') |
329
|
|
|
->with($this->equalTo($usernamePasswordToken)) |
330
|
|
|
->will($this->returnValue($authenticatedToken)); |
331
|
|
|
|
332
|
|
|
$this->eventDispatcher |
333
|
|
|
->expects($this->once()) |
334
|
|
|
->method('dispatch') |
335
|
|
|
->with( |
336
|
|
|
SecurityEvents::INTERACTIVE_LOGIN, |
337
|
|
|
$this->equalTo(new InteractiveLoginEvent($request, $authenticatedToken)) |
338
|
|
|
); |
339
|
|
|
|
340
|
|
|
$this->tokenStorage |
341
|
|
|
->expects($this->at(0)) |
342
|
|
|
->method('getToken') |
343
|
|
|
->will($this->returnValue($existingToken)); |
344
|
|
|
$this->tokenStorage |
345
|
|
|
->expects($this->at(1)) |
346
|
|
|
->method('setToken') |
347
|
|
|
->with($authenticatedToken); |
348
|
|
|
$this->tokenStorage |
349
|
|
|
->expects($this->at(2)) |
350
|
|
|
->method('getToken') |
351
|
|
|
->will($this->returnValue($authenticatedToken)); |
352
|
|
|
|
353
|
|
|
$authenticatedUser = $this->createUser(456); |
354
|
|
|
$authenticatedToken |
355
|
|
|
->expects($this->once()) |
356
|
|
|
->method('getUser') |
357
|
|
|
->will($this->returnValue($authenticatedUser)); |
358
|
|
|
|
359
|
|
|
$this->configResolver |
360
|
|
|
->expects($this->once()) |
361
|
|
|
->method('getParameter') |
362
|
|
|
->with('anonymous_user_id') |
363
|
|
|
->will($this->returnValue($anonymousUserId)); |
364
|
|
|
|
365
|
|
|
$this->assertSame($authenticatedToken, $this->authenticator->authenticate($request)); |
366
|
|
|
} |
367
|
|
|
|
368
|
|
View Code Duplication |
public function testAuthenticate() |
369
|
|
|
{ |
370
|
|
|
$username = 'foo_user'; |
371
|
|
|
$password = 'publish'; |
372
|
|
|
|
373
|
|
|
$existingToken = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface'); |
374
|
|
|
$existingToken |
375
|
|
|
->expects($this->once()) |
376
|
|
|
->method('getUsername') |
377
|
|
|
->will($this->returnValue(__METHOD__)); |
378
|
|
|
|
379
|
|
|
$request = new Request(); |
380
|
|
|
$request->attributes->set('username', $username); |
381
|
|
|
$request->attributes->set('password', $password); |
382
|
|
|
|
383
|
|
|
$usernamePasswordToken = new UsernamePasswordToken($username, $password, self::PROVIDER_KEY); |
384
|
|
|
$authenticatedToken = $this |
385
|
|
|
->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken') |
386
|
|
|
->disableOriginalConstructor() |
387
|
|
|
->getMock(); |
388
|
|
|
$this->authenticationManager |
389
|
|
|
->expects($this->once()) |
390
|
|
|
->method('authenticate') |
391
|
|
|
->with($this->equalTo($usernamePasswordToken)) |
392
|
|
|
->will($this->returnValue($authenticatedToken)); |
393
|
|
|
|
394
|
|
|
$this->eventDispatcher |
395
|
|
|
->expects($this->once()) |
396
|
|
|
->method('dispatch') |
397
|
|
|
->with( |
398
|
|
|
SecurityEvents::INTERACTIVE_LOGIN, |
399
|
|
|
$this->equalTo(new InteractiveLoginEvent($request, $authenticatedToken)) |
400
|
|
|
); |
401
|
|
|
|
402
|
|
|
$this->tokenStorage |
403
|
|
|
->expects($this->at(0)) |
404
|
|
|
->method('getToken') |
405
|
|
|
->will($this->returnValue($existingToken)); |
406
|
|
|
$this->tokenStorage |
407
|
|
|
->expects($this->at(1)) |
408
|
|
|
->method('setToken') |
409
|
|
|
->with($authenticatedToken); |
410
|
|
|
$this->tokenStorage |
411
|
|
|
->expects($this->at(2)) |
412
|
|
|
->method('getToken') |
413
|
|
|
->will($this->returnValue($authenticatedToken)); |
414
|
|
|
|
415
|
|
|
$authenticatedUser = $this->createUser(456); |
416
|
|
|
$authenticatedToken |
417
|
|
|
->expects($this->once()) |
418
|
|
|
->method('getUser') |
419
|
|
|
->will($this->returnValue($authenticatedUser)); |
420
|
|
|
|
421
|
|
|
$this->assertSame($authenticatedToken, $this->authenticator->authenticate($request)); |
422
|
|
|
} |
423
|
|
|
|
424
|
|
|
public function testAuthenticatePreviousUserNonEz() |
425
|
|
|
{ |
426
|
|
|
$username = 'foo_user'; |
427
|
|
|
$password = 'publish'; |
428
|
|
|
|
429
|
|
|
$existingUser = $this->getMock('Symfony\Component\Security\Core\User\UserInterface'); |
430
|
|
|
$existingToken = $this |
431
|
|
|
->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken') |
432
|
|
|
->disableOriginalConstructor() |
433
|
|
|
->getMock(); |
434
|
|
|
$existingToken |
435
|
|
|
->expects($this->once()) |
436
|
|
|
->method('getUsername') |
437
|
|
|
->will($this->returnValue(__METHOD__)); |
438
|
|
|
$existingToken |
439
|
|
|
->expects($this->once()) |
440
|
|
|
->method('getUser') |
441
|
|
|
->will($this->returnValue($existingUser)); |
442
|
|
|
|
443
|
|
|
$request = new Request(); |
444
|
|
|
$request->attributes->set('username', $username); |
445
|
|
|
$request->attributes->set('password', $password); |
446
|
|
|
|
447
|
|
|
$usernamePasswordToken = new UsernamePasswordToken($username, $password, self::PROVIDER_KEY); |
448
|
|
|
$authenticatedToken = $this |
449
|
|
|
->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken') |
450
|
|
|
->disableOriginalConstructor() |
451
|
|
|
->getMock(); |
452
|
|
|
$this->authenticationManager |
453
|
|
|
->expects($this->once()) |
454
|
|
|
->method('authenticate') |
455
|
|
|
->with($this->equalTo($usernamePasswordToken)) |
456
|
|
|
->will($this->returnValue($authenticatedToken)); |
457
|
|
|
|
458
|
|
|
$this->eventDispatcher |
459
|
|
|
->expects($this->once()) |
460
|
|
|
->method('dispatch') |
461
|
|
|
->with( |
462
|
|
|
SecurityEvents::INTERACTIVE_LOGIN, |
463
|
|
|
$this->equalTo(new InteractiveLoginEvent($request, $authenticatedToken)) |
464
|
|
|
); |
465
|
|
|
|
466
|
|
|
$this->tokenStorage |
467
|
|
|
->expects($this->at(0)) |
468
|
|
|
->method('getToken') |
469
|
|
|
->will($this->returnValue($existingToken)); |
470
|
|
|
$this->tokenStorage |
471
|
|
|
->expects($this->at(1)) |
472
|
|
|
->method('setToken') |
473
|
|
|
->with($authenticatedToken); |
474
|
|
|
$this->tokenStorage |
475
|
|
|
->expects($this->at(2)) |
476
|
|
|
->method('getToken') |
477
|
|
|
->will($this->returnValue($authenticatedToken)); |
478
|
|
|
|
479
|
|
|
$authenticatedUser = $this->createUser(456); |
480
|
|
|
$authenticatedToken |
481
|
|
|
->expects($this->once()) |
482
|
|
|
->method('getUser') |
483
|
|
|
->will($this->returnValue($authenticatedUser)); |
484
|
|
|
|
485
|
|
|
$this->assertSame($authenticatedToken, $this->authenticator->authenticate($request)); |
486
|
|
|
} |
487
|
|
|
|
488
|
|
View Code Duplication |
public function testAuthenticatePreviousTokenNotUsernamePassword() |
489
|
|
|
{ |
490
|
|
|
$username = 'foo_user'; |
491
|
|
|
$password = 'publish'; |
492
|
|
|
|
493
|
|
|
$existingToken = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface'); |
494
|
|
|
$existingToken |
495
|
|
|
->expects($this->once()) |
496
|
|
|
->method('getUsername') |
497
|
|
|
->will($this->returnValue(__METHOD__)); |
498
|
|
|
|
499
|
|
|
$request = new Request(); |
500
|
|
|
$request->attributes->set('username', $username); |
501
|
|
|
$request->attributes->set('password', $password); |
502
|
|
|
|
503
|
|
|
$usernamePasswordToken = new UsernamePasswordToken($username, $password, self::PROVIDER_KEY); |
504
|
|
|
$authenticatedToken = $this |
505
|
|
|
->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken') |
506
|
|
|
->disableOriginalConstructor() |
507
|
|
|
->getMock(); |
508
|
|
|
$this->authenticationManager |
509
|
|
|
->expects($this->once()) |
510
|
|
|
->method('authenticate') |
511
|
|
|
->with($this->equalTo($usernamePasswordToken)) |
512
|
|
|
->will($this->returnValue($authenticatedToken)); |
513
|
|
|
|
514
|
|
|
$this->eventDispatcher |
515
|
|
|
->expects($this->once()) |
516
|
|
|
->method('dispatch') |
517
|
|
|
->with( |
518
|
|
|
SecurityEvents::INTERACTIVE_LOGIN, |
519
|
|
|
$this->equalTo(new InteractiveLoginEvent($request, $authenticatedToken)) |
520
|
|
|
); |
521
|
|
|
|
522
|
|
|
$this->tokenStorage |
523
|
|
|
->expects($this->at(0)) |
524
|
|
|
->method('getToken') |
525
|
|
|
->will($this->returnValue($existingToken)); |
526
|
|
|
$this->tokenStorage |
527
|
|
|
->expects($this->at(1)) |
528
|
|
|
->method('setToken') |
529
|
|
|
->with($authenticatedToken); |
530
|
|
|
$this->tokenStorage |
531
|
|
|
->expects($this->at(2)) |
532
|
|
|
->method('getToken') |
533
|
|
|
->will($this->returnValue($authenticatedToken)); |
534
|
|
|
|
535
|
|
|
$authenticatedUser = $this->createUser(456); |
536
|
|
|
$authenticatedToken |
537
|
|
|
->expects($this->once()) |
538
|
|
|
->method('getUser') |
539
|
|
|
->will($this->returnValue($authenticatedUser)); |
540
|
|
|
|
541
|
|
|
$this->assertSame($authenticatedToken, $this->authenticator->authenticate($request)); |
542
|
|
|
} |
543
|
|
|
|
544
|
|
|
public function testLogout() |
545
|
|
|
{ |
546
|
|
|
$sessionLogoutHandler = $this->getMock('Symfony\Component\Security\Http\Logout\SessionLogoutHandler'); |
547
|
|
|
$sessionLogoutHandler |
548
|
|
|
->expects($this->never()) |
549
|
|
|
->method('logout'); |
550
|
|
|
|
551
|
|
|
$token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface'); |
552
|
|
|
$this->tokenStorage |
553
|
|
|
->expects($this->once()) |
554
|
|
|
->method('getToken') |
555
|
|
|
->will($this->returnValue($token)); |
556
|
|
|
|
557
|
|
|
$request = new Request(); |
558
|
|
|
$logoutHandler1 = $this->getMock('Symfony\Component\Security\Http\Logout\LogoutHandlerInterface'); |
559
|
|
|
$logoutHandler1 |
560
|
|
|
->expects($this->once()) |
561
|
|
|
->method('logout') |
562
|
|
|
->with( |
563
|
|
|
$request, |
564
|
|
|
$this->isInstanceOf('Symfony\Component\HttpFoundation\Response'), |
565
|
|
|
$token |
566
|
|
|
); |
567
|
|
|
$logoutHandler2 = $this->getMock('Symfony\Component\Security\Http\Logout\LogoutHandlerInterface'); |
568
|
|
|
$logoutHandler2 |
569
|
|
|
->expects($this->once()) |
570
|
|
|
->method('logout') |
571
|
|
|
->with( |
572
|
|
|
$request, |
573
|
|
|
$this->isInstanceOf('Symfony\Component\HttpFoundation\Response'), |
574
|
|
|
$token |
575
|
|
|
); |
576
|
|
|
|
577
|
|
|
$this->authenticator->addLogoutHandler($sessionLogoutHandler); |
578
|
|
|
$this->authenticator->addLogoutHandler($logoutHandler1); |
579
|
|
|
$this->authenticator->addLogoutHandler($logoutHandler2); |
580
|
|
|
|
581
|
|
|
$this->assertInstanceOf( |
582
|
|
|
'Symfony\Component\HttpFoundation\Response', |
583
|
|
|
$this->authenticator->logout($request) |
584
|
|
|
); |
585
|
|
|
} |
586
|
|
|
} |
587
|
|
|
|