|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\Security\Http\Tests\EventListener; |
| 13 | + |
| 14 | +use PHPUnit\Framework\MockObject\MockObject; |
| 15 | +use PHPUnit\Framework\TestCase; |
| 16 | +use Symfony\Component\HttpFoundation\Exception\UnsignedUriException; |
| 17 | +use Symfony\Component\HttpFoundation\Request; |
| 18 | +use Symfony\Component\HttpFoundation\UriSigner; |
| 19 | +use Symfony\Component\HttpKernel\Event\ControllerArgumentsEvent; |
| 20 | +use Symfony\Component\HttpKernel\Exception\HttpException; |
| 21 | +use Symfony\Component\HttpKernel\HttpKernelInterface; |
| 22 | +use Symfony\Component\Security\Http\EventListener\IsSignatureValidAttributeListener; |
| 23 | +use Symfony\Component\Security\Http\Tests\Fixtures\IsSignatureValidAttributeController; |
| 24 | +use Symfony\Component\Security\Http\Tests\Fixtures\IsSignatureValidAttributeMethodsController; |
| 25 | + |
| 26 | +class IsSignatureValidAttributeListenerTest extends TestCase |
| 27 | +{ |
| 28 | + public function testInvokableControllerWithValidSignature() |
| 29 | + { |
| 30 | + $request = new Request(); |
| 31 | + |
| 32 | + /** @var UriSigner&MockObject $signer */ |
| 33 | + $signer = $this->createMock(UriSigner::class); |
| 34 | + $signer->expects($this->once())->method('checkRequest')->with($request)->willReturn(true); |
| 35 | + |
| 36 | + /** @var HttpKernelInterface&MockObject $kernel */ |
| 37 | + $kernel = $this->createMock(HttpKernelInterface::class); |
| 38 | + |
| 39 | + $event = new ControllerArgumentsEvent( |
| 40 | + $kernel, |
| 41 | + new IsSignatureValidAttributeController(), |
| 42 | + [], |
| 43 | + $request, |
| 44 | + null |
| 45 | + ); |
| 46 | + |
| 47 | + $listener = new IsSignatureValidAttributeListener($signer); |
| 48 | + $listener->onKernelControllerArguments($event); |
| 49 | + } |
| 50 | + |
| 51 | + public function testNoAttributeSkipsValidation() |
| 52 | + { |
| 53 | + /** @var UriSigner&MockObject $signer */ |
| 54 | + $signer = $this->createMock(UriSigner::class); |
| 55 | + $signer->expects($this->never())->method('checkRequest'); |
| 56 | + |
| 57 | + /** @var HttpKernelInterface&MockObject $kernel */ |
| 58 | + $kernel = $this->createMock(HttpKernelInterface::class); |
| 59 | + |
| 60 | + $event = new ControllerArgumentsEvent( |
| 61 | + $kernel, |
| 62 | + [new IsSignatureValidAttributeMethodsController(), 'noAttribute'], |
| 63 | + [], |
| 64 | + new Request(), |
| 65 | + null |
| 66 | + ); |
| 67 | + |
| 68 | + $listener = new IsSignatureValidAttributeListener($signer); |
| 69 | + $listener->onKernelControllerArguments($event); |
| 70 | + } |
| 71 | + |
| 72 | + public function testDefaultCheckRequestSucceeds() |
| 73 | + { |
| 74 | + $request = new Request(); |
| 75 | + /** @var UriSigner&MockObject $signer */ |
| 76 | + $signer = $this->createMock(UriSigner::class); |
| 77 | + $signer->expects($this->once())->method('checkRequest')->with($request)->willReturn(true); |
| 78 | + |
| 79 | + /** @var HttpKernelInterface&MockObject $kernel */ |
| 80 | + $kernel = $this->createMock(HttpKernelInterface::class); |
| 81 | + |
| 82 | + $event = new ControllerArgumentsEvent( |
| 83 | + $kernel, |
| 84 | + [new IsSignatureValidAttributeMethodsController(), 'withDefaultBehavior'], |
| 85 | + [], |
| 86 | + $request, |
| 87 | + null |
| 88 | + ); |
| 89 | + |
| 90 | + $listener = new IsSignatureValidAttributeListener($signer); |
| 91 | + $listener->onKernelControllerArguments($event); |
| 92 | + } |
| 93 | + |
| 94 | + public function testCheckRequestFailsThrowsHttpException() |
| 95 | + { |
| 96 | + $request = new Request(); |
| 97 | + /** @var UriSigner&MockObject $signer */ |
| 98 | + $signer = $this->createMock(UriSigner::class); |
| 99 | + $signer->expects($this->once())->method('checkRequest')->willReturn(false); |
| 100 | + |
| 101 | + /** @var HttpKernelInterface&MockObject $kernel */ |
| 102 | + $kernel = $this->createMock(HttpKernelInterface::class); |
| 103 | + |
| 104 | + $event = new ControllerArgumentsEvent( |
| 105 | + $kernel, |
| 106 | + [new IsSignatureValidAttributeMethodsController(), 'withDefaultBehavior'], |
| 107 | + [], |
| 108 | + $request, |
| 109 | + null |
| 110 | + ); |
| 111 | + |
| 112 | + $listener = new IsSignatureValidAttributeListener($signer); |
| 113 | + |
| 114 | + try { |
| 115 | + $listener->onKernelControllerArguments($event); |
| 116 | + } catch (HttpException $e) { |
| 117 | + $this->assertSame(404, $e->getStatusCode()); |
| 118 | + $this->assertSame('The URI signature is invalid.', $e->getMessage()); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + public function testVerifyThrowsCustomException() |
| 123 | + { |
| 124 | + $request = new Request(); |
| 125 | + $signer = new UriSigner('foobar'); |
| 126 | + |
| 127 | + /** @var HttpKernelInterface&MockObject $kernel */ |
| 128 | + $kernel = $this->createMock(HttpKernelInterface::class); |
| 129 | + |
| 130 | + $event = new ControllerArgumentsEvent( |
| 131 | + $kernel, |
| 132 | + [new IsSignatureValidAttributeMethodsController(), 'withExceptionThrowing'], |
| 133 | + [], |
| 134 | + $request, |
| 135 | + null |
| 136 | + ); |
| 137 | + |
| 138 | + $listener = new IsSignatureValidAttributeListener($signer); |
| 139 | + |
| 140 | + $this->expectException(UnsignedUriException::class); |
| 141 | + $listener->onKernelControllerArguments($event); |
| 142 | + } |
| 143 | + |
| 144 | + public function testCustomStatusCodeReturnedOnInvalidSignature() |
| 145 | + { |
| 146 | + $request = new Request(); |
| 147 | + |
| 148 | + /** @var UriSigner&MockObject $signer */ |
| 149 | + $signer = $this->createMock(UriSigner::class); |
| 150 | + $signer->expects($this->once())->method('checkRequest')->with($request)->willReturn(false); |
| 151 | + |
| 152 | + /** @var HttpKernelInterface&MockObject $kernel */ |
| 153 | + $kernel = $this->createMock(HttpKernelInterface::class); |
| 154 | + |
| 155 | + $event = new ControllerArgumentsEvent( |
| 156 | + $kernel, |
| 157 | + [new IsSignatureValidAttributeMethodsController(), 'withCustomStatusCode'], |
| 158 | + [], |
| 159 | + $request, |
| 160 | + null |
| 161 | + ); |
| 162 | + |
| 163 | + $listener = new IsSignatureValidAttributeListener($signer); |
| 164 | + |
| 165 | + try { |
| 166 | + $listener->onKernelControllerArguments($event); |
| 167 | + } catch (HttpException $e) { |
| 168 | + $this->assertSame(401, $e->getStatusCode()); |
| 169 | + $this->assertSame('The URI signature is invalid.', $e->getMessage()); |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + public function testWithThrowAndCustomStatusFails() |
| 174 | + { |
| 175 | + $request = new Request(); |
| 176 | + $signer = new UriSigner('foobar'); |
| 177 | + |
| 178 | + /** @var HttpKernelInterface&MockObject $kernel */ |
| 179 | + $kernel = $this->createMock(HttpKernelInterface::class); |
| 180 | + |
| 181 | + $event = new ControllerArgumentsEvent( |
| 182 | + $kernel, |
| 183 | + [new IsSignatureValidAttributeMethodsController(), 'withThrowAndCustomStatus'], |
| 184 | + [], |
| 185 | + $request, |
| 186 | + null |
| 187 | + ); |
| 188 | + |
| 189 | + $listener = new IsSignatureValidAttributeListener($signer); |
| 190 | + |
| 191 | + $this->expectException(UnsignedUriException::class); |
| 192 | + $listener->onKernelControllerArguments($event); |
| 193 | + } |
| 194 | + |
| 195 | + public function testWithExplicitNoThrowIgnoresSignatureFailure() |
| 196 | + { |
| 197 | + $request = new Request(); |
| 198 | + |
| 199 | + /** @var UriSigner&MockObject $signer */ |
| 200 | + $signer = $this->createMock(UriSigner::class); |
| 201 | + $signer->expects($this->once())->method('checkRequest')->with($request)->willReturn(false); |
| 202 | + |
| 203 | + /** @var HttpKernelInterface&MockObject $kernel */ |
| 204 | + $kernel = $this->createMock(HttpKernelInterface::class); |
| 205 | + |
| 206 | + $event = new ControllerArgumentsEvent( |
| 207 | + $kernel, |
| 208 | + [new IsSignatureValidAttributeMethodsController(), 'withExplicitNoThrow'], |
| 209 | + [], |
| 210 | + $request, |
| 211 | + null |
| 212 | + ); |
| 213 | + |
| 214 | + $listener = new IsSignatureValidAttributeListener($signer); |
| 215 | + $this->expectException(HttpException::class); |
| 216 | + $listener->onKernelControllerArguments($event); |
| 217 | + } |
| 218 | + |
| 219 | + public function testMultipleAttributesAllValid() |
| 220 | + { |
| 221 | + $request = new Request(); |
| 222 | + |
| 223 | + /** @var UriSigner&MockObject $signer */ |
| 224 | + $signer = $this->createMock(UriSigner::class); |
| 225 | + $signer->expects($this->exactly(2))->method('checkRequest')->with($request)->willReturn(true); |
| 226 | + |
| 227 | + /** @var HttpKernelInterface&MockObject $kernel */ |
| 228 | + $kernel = $this->createMock(HttpKernelInterface::class); |
| 229 | + |
| 230 | + $event = new ControllerArgumentsEvent( |
| 231 | + $kernel, |
| 232 | + [new IsSignatureValidAttributeMethodsController(), 'withMultiple'], |
| 233 | + [], |
| 234 | + $request, |
| 235 | + null |
| 236 | + ); |
| 237 | + |
| 238 | + $listener = new IsSignatureValidAttributeListener($signer); |
| 239 | + $listener->onKernelControllerArguments($event); |
| 240 | + } |
| 241 | +} |
0 commit comments