1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* File containing the RestLogoutHandlerTest class. |
5
|
|
|
* |
6
|
|
|
* @copyright Copyright (C) 1999-2014 eZ Systems AS. All rights reserved. |
7
|
|
|
* @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2 |
8
|
|
|
*/ |
9
|
|
|
namespace eZ\Publish\Core\REST\Server\Tests\Security; |
10
|
|
|
|
11
|
|
|
use eZ\Publish\Core\Base\Tests\PHPUnit5CompatTrait; |
12
|
|
|
use eZ\Publish\Core\REST\Server\Security\CsrfTokenManager; |
13
|
|
|
use PHPUnit\Framework\TestCase; |
14
|
|
|
use Symfony\Component\HttpFoundation\Request; |
15
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
16
|
|
|
use Symfony\Component\Security\Csrf\TokenGenerator\TokenGeneratorInterface; |
17
|
|
|
use Symfony\Component\Security\Csrf\TokenStorage\TokenStorageInterface; |
18
|
|
|
|
19
|
|
|
class CsrfTokenManagerTest extends TestCase |
20
|
|
|
{ |
21
|
|
|
use PHPUnit5CompatTrait; |
22
|
|
|
|
23
|
|
|
const CSRF_TOKEN_INTENTION = 'csrf'; |
24
|
|
|
|
25
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject|\Symfony\Component\Security\Csrf\TokenStorage\TokenStorageInterface */ |
26
|
|
|
private $tokenStorage; |
27
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject|\Symfony\Component\HttpFoundation\RequestStack */ |
28
|
|
|
private $requestStack; |
29
|
|
|
|
30
|
|
|
protected function setUp() |
31
|
|
|
{ |
32
|
|
|
parent::setUp(); |
33
|
|
|
|
34
|
|
|
$this->tokenStorage = $this->createMock(TokenStorageInterface::class); |
35
|
|
|
$this->requestStack = $this->createMock(RequestStack::class); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
View Code Duplication |
public function testHasTokenForHttp() |
39
|
|
|
{ |
40
|
|
|
$csrfTokenManager = $this->createCsrfTokenManager(false); |
41
|
|
|
|
42
|
|
|
$this->tokenStorage |
43
|
|
|
->expects($this->once()) |
44
|
|
|
->method('hasToken') |
45
|
|
|
->with(self::CSRF_TOKEN_INTENTION); |
46
|
|
|
|
47
|
|
|
$csrfTokenManager->hasToken(self::CSRF_TOKEN_INTENTION); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
View Code Duplication |
public function testHasTokenForHttps() |
51
|
|
|
{ |
52
|
|
|
$csrfTokenManager = $this->createCsrfTokenManager(true); |
53
|
|
|
|
54
|
|
|
$this->tokenStorage |
55
|
|
|
->expects($this->once()) |
56
|
|
|
->method('hasToken') |
57
|
|
|
->with('https-' . self::CSRF_TOKEN_INTENTION); |
58
|
|
|
|
59
|
|
|
$csrfTokenManager->hasToken(self::CSRF_TOKEN_INTENTION); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
private function createCsrfTokenManager($https = false) |
63
|
|
|
{ |
64
|
|
|
$request = new Request(); |
65
|
|
|
if ($https) { |
66
|
|
|
$request->server->set('HTTPS', 'ON'); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$this->requestStack |
70
|
|
|
->expects($this->once()) |
71
|
|
|
->method('getMasterRequest') |
72
|
|
|
->willReturn($request); |
73
|
|
|
|
74
|
|
|
return new CsrfTokenManager( |
75
|
|
|
$this->createMock(TokenGeneratorInterface::class), |
76
|
|
|
$this->tokenStorage, |
77
|
|
|
$this->requestStack |
78
|
|
|
); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|