|
12 | 12 | namespace Symfony\Component\Security\Csrf\Tests;
|
13 | 13 |
|
14 | 14 | use PHPUnit\Framework\TestCase;
|
| 15 | +use Symfony\Component\HttpFoundation\Request; |
| 16 | +use Symfony\Component\HttpFoundation\RequestStack; |
15 | 17 | use Symfony\Component\Security\Csrf\CsrfToken;
|
16 | 18 | use Symfony\Component\Security\Csrf\CsrfTokenManager;
|
17 | 19 |
|
|
21 | 23 | class CsrfTokenManagerTest extends TestCase
|
22 | 24 | {
|
23 | 25 | /**
|
24 |
| - * @var \PHPUnit_Framework_MockObject_MockObject |
| 26 | + * @dataProvider getManagerGeneratorAndStorage |
25 | 27 | */
|
26 |
| - private $generator; |
27 |
| - |
28 |
| - /** |
29 |
| - * @var \PHPUnit_Framework_MockObject_MockObject |
30 |
| - */ |
31 |
| - private $storage; |
32 |
| - |
33 |
| - /** |
34 |
| - * @var CsrfTokenManager |
35 |
| - */ |
36 |
| - private $manager; |
37 |
| - |
38 |
| - protected function setUp() |
39 |
| - { |
40 |
| - $this->generator = $this->getMockBuilder('Symfony\Component\Security\Csrf\TokenGenerator\TokenGeneratorInterface')->getMock(); |
41 |
| - $this->storage = $this->getMockBuilder('Symfony\Component\Security\Csrf\TokenStorage\TokenStorageInterface')->getMock(); |
42 |
| - $this->manager = new CsrfTokenManager($this->generator, $this->storage); |
43 |
| - } |
44 |
| - |
45 |
| - protected function tearDown() |
46 |
| - { |
47 |
| - $this->generator = null; |
48 |
| - $this->storage = null; |
49 |
| - $this->manager = null; |
50 |
| - } |
51 |
| - |
52 |
| - public function testGetNonExistingToken() |
| 28 | + public function testGetNonExistingToken($namespace, $manager, $storage, $generator) |
53 | 29 | {
|
54 |
| - $this->storage->expects($this->once()) |
| 30 | + $storage->expects($this->once()) |
55 | 31 | ->method('hasToken')
|
56 |
| - ->with('token_id') |
| 32 | + ->with($namespace.'token_id') |
57 | 33 | ->will($this->returnValue(false));
|
58 | 34 |
|
59 |
| - $this->generator->expects($this->once()) |
| 35 | + $generator->expects($this->once()) |
60 | 36 | ->method('generateToken')
|
61 | 37 | ->will($this->returnValue('TOKEN'));
|
62 | 38 |
|
63 |
| - $this->storage->expects($this->once()) |
| 39 | + $storage->expects($this->once()) |
64 | 40 | ->method('setToken')
|
65 |
| - ->with('token_id', 'TOKEN'); |
| 41 | + ->with($namespace.'token_id', 'TOKEN'); |
66 | 42 |
|
67 |
| - $token = $this->manager->getToken('token_id'); |
| 43 | + $token = $manager->getToken('token_id'); |
68 | 44 |
|
69 | 45 | $this->assertInstanceOf('Symfony\Component\Security\Csrf\CsrfToken', $token);
|
70 | 46 | $this->assertSame('token_id', $token->getId());
|
71 | 47 | $this->assertSame('TOKEN', $token->getValue());
|
72 | 48 | }
|
73 | 49 |
|
74 |
| - public function testUseExistingTokenIfAvailable() |
| 50 | + /** |
| 51 | + * @dataProvider getManagerGeneratorAndStorage |
| 52 | + */ |
| 53 | + public function testUseExistingTokenIfAvailable($namespace, $manager, $storage) |
75 | 54 | {
|
76 |
| - $this->storage->expects($this->once()) |
| 55 | + $storage->expects($this->once()) |
77 | 56 | ->method('hasToken')
|
78 |
| - ->with('token_id') |
| 57 | + ->with($namespace.'token_id') |
79 | 58 | ->will($this->returnValue(true));
|
80 | 59 |
|
81 |
| - $this->storage->expects($this->once()) |
| 60 | + $storage->expects($this->once()) |
82 | 61 | ->method('getToken')
|
83 |
| - ->with('token_id') |
| 62 | + ->with($namespace.'token_id') |
84 | 63 | ->will($this->returnValue('TOKEN'));
|
85 | 64 |
|
86 |
| - $token = $this->manager->getToken('token_id'); |
| 65 | + $token = $manager->getToken('token_id'); |
87 | 66 |
|
88 | 67 | $this->assertInstanceOf('Symfony\Component\Security\Csrf\CsrfToken', $token);
|
89 | 68 | $this->assertSame('token_id', $token->getId());
|
90 | 69 | $this->assertSame('TOKEN', $token->getValue());
|
91 | 70 | }
|
92 | 71 |
|
93 |
| - public function testRefreshTokenAlwaysReturnsNewToken() |
| 72 | + /** |
| 73 | + * @dataProvider getManagerGeneratorAndStorage |
| 74 | + */ |
| 75 | + public function testRefreshTokenAlwaysReturnsNewToken($namespace, $manager, $storage, $generator) |
94 | 76 | {
|
95 |
| - $this->storage->expects($this->never()) |
| 77 | + $storage->expects($this->never()) |
96 | 78 | ->method('hasToken');
|
97 | 79 |
|
98 |
| - $this->generator->expects($this->once()) |
| 80 | + $generator->expects($this->once()) |
99 | 81 | ->method('generateToken')
|
100 | 82 | ->will($this->returnValue('TOKEN'));
|
101 | 83 |
|
102 |
| - $this->storage->expects($this->once()) |
| 84 | + $storage->expects($this->once()) |
103 | 85 | ->method('setToken')
|
104 |
| - ->with('token_id', 'TOKEN'); |
| 86 | + ->with($namespace.'token_id', 'TOKEN'); |
105 | 87 |
|
106 |
| - $token = $this->manager->refreshToken('token_id'); |
| 88 | + $token = $manager->refreshToken('token_id'); |
107 | 89 |
|
108 | 90 | $this->assertInstanceOf('Symfony\Component\Security\Csrf\CsrfToken', $token);
|
109 | 91 | $this->assertSame('token_id', $token->getId());
|
110 | 92 | $this->assertSame('TOKEN', $token->getValue());
|
111 | 93 | }
|
112 | 94 |
|
113 |
| - public function testMatchingTokenIsValid() |
| 95 | + /** |
| 96 | + * @dataProvider getManagerGeneratorAndStorage |
| 97 | + */ |
| 98 | + public function testMatchingTokenIsValid($namespace, $manager, $storage) |
114 | 99 | {
|
115 |
| - $this->storage->expects($this->once()) |
| 100 | + $storage->expects($this->once()) |
116 | 101 | ->method('hasToken')
|
117 |
| - ->with('token_id') |
| 102 | + ->with($namespace.'token_id') |
118 | 103 | ->will($this->returnValue(true));
|
119 | 104 |
|
120 |
| - $this->storage->expects($this->once()) |
| 105 | + $storage->expects($this->once()) |
121 | 106 | ->method('getToken')
|
122 |
| - ->with('token_id') |
| 107 | + ->with($namespace.'token_id') |
123 | 108 | ->will($this->returnValue('TOKEN'));
|
124 | 109 |
|
125 |
| - $this->assertTrue($this->manager->isTokenValid(new CsrfToken('token_id', 'TOKEN'))); |
| 110 | + $this->assertTrue($manager->isTokenValid(new CsrfToken('token_id', 'TOKEN'))); |
126 | 111 | }
|
127 | 112 |
|
128 |
| - public function testNonMatchingTokenIsNotValid() |
| 113 | + /** |
| 114 | + * @dataProvider getManagerGeneratorAndStorage |
| 115 | + */ |
| 116 | + public function testNonMatchingTokenIsNotValid($namespace, $manager, $storage) |
129 | 117 | {
|
130 |
| - $this->storage->expects($this->once()) |
| 118 | + $storage->expects($this->once()) |
131 | 119 | ->method('hasToken')
|
132 |
| - ->with('token_id') |
| 120 | + ->with($namespace.'token_id') |
133 | 121 | ->will($this->returnValue(true));
|
134 | 122 |
|
135 |
| - $this->storage->expects($this->once()) |
| 123 | + $storage->expects($this->once()) |
136 | 124 | ->method('getToken')
|
137 |
| - ->with('token_id') |
| 125 | + ->with($namespace.'token_id') |
138 | 126 | ->will($this->returnValue('TOKEN'));
|
139 | 127 |
|
140 |
| - $this->assertFalse($this->manager->isTokenValid(new CsrfToken('token_id', 'FOOBAR'))); |
| 128 | + $this->assertFalse($manager->isTokenValid(new CsrfToken('token_id', 'FOOBAR'))); |
141 | 129 | }
|
142 | 130 |
|
143 |
| - public function testNonExistingTokenIsNotValid() |
| 131 | + /** |
| 132 | + * @dataProvider getManagerGeneratorAndStorage |
| 133 | + */ |
| 134 | + public function testNonExistingTokenIsNotValid($namespace, $manager, $storage) |
144 | 135 | {
|
145 |
| - $this->storage->expects($this->once()) |
| 136 | + $storage->expects($this->once()) |
146 | 137 | ->method('hasToken')
|
147 |
| - ->with('token_id') |
| 138 | + ->with($namespace.'token_id') |
148 | 139 | ->will($this->returnValue(false));
|
149 | 140 |
|
150 |
| - $this->storage->expects($this->never()) |
| 141 | + $storage->expects($this->never()) |
151 | 142 | ->method('getToken');
|
152 | 143 |
|
153 |
| - $this->assertFalse($this->manager->isTokenValid(new CsrfToken('token_id', 'FOOBAR'))); |
| 144 | + $this->assertFalse($manager->isTokenValid(new CsrfToken('token_id', 'FOOBAR'))); |
154 | 145 | }
|
155 | 146 |
|
156 |
| - public function testRemoveToken() |
| 147 | + /** |
| 148 | + * @dataProvider getManagerGeneratorAndStorage |
| 149 | + */ |
| 150 | + public function testRemoveToken($namespace, $manager, $storage) |
157 | 151 | {
|
158 |
| - $this->storage->expects($this->once()) |
| 152 | + $storage->expects($this->once()) |
159 | 153 | ->method('removeToken')
|
160 |
| - ->with('token_id') |
| 154 | + ->with($namespace.'token_id') |
161 | 155 | ->will($this->returnValue('REMOVED_TOKEN'));
|
162 | 156 |
|
163 |
| - $this->assertSame('REMOVED_TOKEN', $this->manager->removeToken('token_id')); |
| 157 | + $this->assertSame('REMOVED_TOKEN', $manager->removeToken('token_id')); |
| 158 | + } |
| 159 | + |
| 160 | + public function testNamespaced() |
| 161 | + { |
| 162 | + $generator = $this->getMockBuilder('Symfony\Component\Security\Csrf\TokenGenerator\TokenGeneratorInterface')->getMock(); |
| 163 | + $storage = $this->getMockBuilder('Symfony\Component\Security\Csrf\TokenStorage\TokenStorageInterface')->getMock(); |
| 164 | + |
| 165 | + $requestStack = new RequestStack(); |
| 166 | + $requestStack->push(new Request(array(), array(), array(), array(), array(), array('HTTPS' => 'on'))); |
| 167 | + |
| 168 | + $manager = new CsrfTokenManager($generator, $storage, null, $requestStack); |
| 169 | + |
| 170 | + $token = $manager->getToken('foo'); |
| 171 | + $this->assertSame('foo', $token->getId()); |
| 172 | + } |
| 173 | + |
| 174 | + public function getManagerGeneratorAndStorage() |
| 175 | + { |
| 176 | + $data = array(); |
| 177 | + |
| 178 | + list($generator, $storage) = $this->getGeneratorAndStorage(); |
| 179 | + $data[] = array('', new CsrfTokenManager($generator, $storage, ''), $storage, $generator); |
| 180 | + |
| 181 | + list($generator, $storage) = $this->getGeneratorAndStorage(); |
| 182 | + $data[] = array('https-', new CsrfTokenManager($generator, $storage), $storage, $generator); |
| 183 | + |
| 184 | + list($generator, $storage) = $this->getGeneratorAndStorage(); |
| 185 | + $data[] = array('aNamespace-', new CsrfTokenManager($generator, $storage, 'aNamespace-'), $storage, $generator); |
| 186 | + |
| 187 | + $requestStack = new RequestStack(); |
| 188 | + $requestStack->push(new Request(array(), array(), array(), array(), array(), array('HTTPS' => 'on'))); |
| 189 | + list($generator, $storage) = $this->getGeneratorAndStorage(); |
| 190 | + $data[] = array('https-', new CsrfTokenManager($generator, $storage, $requestStack), $storage, $generator); |
| 191 | + |
| 192 | + list($generator, $storage) = $this->getGeneratorAndStorage(); |
| 193 | + $data[] = array('generated-', new CsrfTokenManager($generator, $storage, function () { |
| 194 | + return 'generated-'; |
| 195 | + }), $storage, $generator); |
| 196 | + |
| 197 | + $requestStack = new RequestStack(); |
| 198 | + $requestStack->push(new Request()); |
| 199 | + list($generator, $storage) = $this->getGeneratorAndStorage(); |
| 200 | + $data[] = array('', new CsrfTokenManager($generator, $storage, $requestStack), $storage, $generator); |
| 201 | + |
| 202 | + return $data; |
| 203 | + } |
| 204 | + |
| 205 | + private function getGeneratorAndStorage() |
| 206 | + { |
| 207 | + return array( |
| 208 | + $this->getMockBuilder('Symfony\Component\Security\Csrf\TokenGenerator\TokenGeneratorInterface')->getMock(), |
| 209 | + $this->getMockBuilder('Symfony\Component\Security\Csrf\TokenStorage\TokenStorageInterface')->getMock(), |
| 210 | + ); |
| 211 | + } |
| 212 | + |
| 213 | + public function setUp() |
| 214 | + { |
| 215 | + $_SERVER['HTTPS'] = 'on'; |
| 216 | + } |
| 217 | + |
| 218 | + public function tearDown() |
| 219 | + { |
| 220 | + parent::tearDown(); |
| 221 | + |
| 222 | + unset($_SERVER['HTTPS']); |
164 | 223 | }
|
165 | 224 | }
|
0 commit comments