2018-09-03 16:33:13 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Engelsystem\Test\Unit\Middleware;
|
|
|
|
|
2018-11-27 13:34:55 +01:00
|
|
|
use Engelsystem\Http\Exceptions\HttpAuthExpired;
|
2018-09-03 16:33:13 +02:00
|
|
|
use Engelsystem\Middleware\VerifyCsrfToken;
|
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
use Psr\Http\Message\ResponseInterface;
|
|
|
|
use Psr\Http\Message\ServerRequestInterface;
|
|
|
|
use Psr\Http\Server\RequestHandlerInterface;
|
|
|
|
use Symfony\Component\HttpFoundation\Session\SessionInterface;
|
|
|
|
|
|
|
|
class VerifyCsrfTokenTest extends TestCase
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Middleware\VerifyCsrfToken::isReading
|
2019-04-24 11:01:37 +02:00
|
|
|
* @covers \Engelsystem\Middleware\VerifyCsrfToken::process
|
2018-09-03 16:33:13 +02:00
|
|
|
*/
|
|
|
|
public function testProcess(): void
|
|
|
|
{
|
|
|
|
/** @var ServerRequestInterface|MockObject $request */
|
|
|
|
$request = $this->getMockForAbstractClass(ServerRequestInterface::class);
|
|
|
|
/** @var RequestHandlerInterface|MockObject $handler */
|
|
|
|
$handler = $this->getMockForAbstractClass(RequestHandlerInterface::class);
|
|
|
|
/** @var ResponseInterface|MockObject $response */
|
|
|
|
$response = $this->getMockForAbstractClass(ResponseInterface::class);
|
|
|
|
|
|
|
|
$handler->expects($this->exactly(2))
|
|
|
|
->method('handle')
|
|
|
|
->with($request)
|
|
|
|
->willReturn($response);
|
|
|
|
|
|
|
|
/** @var VerifyCsrfToken|MockObject $middleware */
|
|
|
|
$middleware = $this->getMockBuilder(VerifyCsrfToken::class)
|
|
|
|
->disableOriginalConstructor()
|
2019-11-06 12:29:58 +01:00
|
|
|
->onlyMethods(['tokensMatch'])
|
2018-09-03 16:33:13 +02:00
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$middleware->expects($this->exactly(2))
|
|
|
|
->method('tokensMatch')
|
|
|
|
->willReturnOnConsecutiveCalls(true, false);
|
|
|
|
|
|
|
|
// Results in true, false, false
|
|
|
|
$request->expects($this->exactly(3))
|
|
|
|
->method('getMethod')
|
2018-11-27 13:34:55 +01:00
|
|
|
->willReturnOnConsecutiveCalls('GET', 'DELETE', 'POST');
|
2018-09-03 16:33:13 +02:00
|
|
|
|
2018-11-27 13:34:55 +01:00
|
|
|
// Is reading
|
2018-09-03 16:33:13 +02:00
|
|
|
$middleware->process($request, $handler);
|
2018-11-27 13:34:55 +01:00
|
|
|
// Tokens match
|
2018-09-03 16:33:13 +02:00
|
|
|
$middleware->process($request, $handler);
|
2018-11-27 13:34:55 +01:00
|
|
|
|
|
|
|
// No match
|
|
|
|
$this->expectException(HttpAuthExpired::class);
|
2018-09-03 16:33:13 +02:00
|
|
|
$middleware->process($request, $handler);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Middleware\VerifyCsrfToken::__construct
|
|
|
|
* @covers \Engelsystem\Middleware\VerifyCsrfToken::tokensMatch
|
|
|
|
*/
|
|
|
|
public function testTokensMatch(): void
|
|
|
|
{
|
|
|
|
/** @var ServerRequestInterface|MockObject $request */
|
|
|
|
$request = $this->getMockForAbstractClass(ServerRequestInterface::class);
|
|
|
|
/** @var RequestHandlerInterface|MockObject $handler */
|
|
|
|
$handler = $this->getMockForAbstractClass(RequestHandlerInterface::class);
|
|
|
|
/** @var ResponseInterface|MockObject $response */
|
|
|
|
$response = $this->getMockForAbstractClass(ResponseInterface::class);
|
|
|
|
/** @var SessionInterface|MockObject $session */
|
|
|
|
$session = $this->getMockForAbstractClass(SessionInterface::class);
|
|
|
|
|
|
|
|
/** @var VerifyCsrfToken|MockObject $middleware */
|
|
|
|
$middleware = $this->getMockBuilder(VerifyCsrfToken::class)
|
|
|
|
->setConstructorArgs([$session])
|
2019-11-06 12:29:58 +01:00
|
|
|
->onlyMethods(['isReading'])
|
2018-09-03 16:33:13 +02:00
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$middleware->expects($this->atLeastOnce())
|
|
|
|
->method('isReading')
|
|
|
|
->willReturn(false);
|
|
|
|
|
|
|
|
$handler->expects($this->exactly(3))
|
|
|
|
->method('handle')
|
|
|
|
->willReturn($response);
|
|
|
|
|
|
|
|
$request->expects($this->exactly(4))
|
|
|
|
->method('getParsedBody')
|
|
|
|
->willReturnOnConsecutiveCalls(
|
|
|
|
null,
|
|
|
|
['_token' => 'PostFooToken'],
|
2018-11-27 13:34:55 +01:00
|
|
|
['_token' => 'PostBarToken'],
|
|
|
|
null
|
2018-09-03 16:33:13 +02:00
|
|
|
);
|
|
|
|
$request->expects($this->exactly(4))
|
|
|
|
->method('getHeader')
|
|
|
|
->with('X-CSRF-TOKEN')
|
|
|
|
->willReturnOnConsecutiveCalls(
|
|
|
|
['HeaderFooToken'],
|
|
|
|
[],
|
2018-11-27 13:34:55 +01:00
|
|
|
['HeaderBarToken'],
|
|
|
|
[]
|
2018-09-03 16:33:13 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
$session->expects($this->exactly(4))
|
|
|
|
->method('get')
|
|
|
|
->with('_token')
|
|
|
|
->willReturnOnConsecutiveCalls(
|
|
|
|
'HeaderFooToken',
|
|
|
|
'PostFooToken',
|
2018-11-27 13:34:55 +01:00
|
|
|
'PostBarToken',
|
|
|
|
'NotAvailableToken'
|
2018-09-03 16:33:13 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
// Header token
|
2018-11-27 13:34:55 +01:00
|
|
|
$middleware->process($request, $handler);
|
2018-09-03 16:33:13 +02:00
|
|
|
// POST token
|
2018-11-27 13:34:55 +01:00
|
|
|
$middleware->process($request, $handler);
|
2018-09-03 16:33:13 +02:00
|
|
|
// Header and POST tokens
|
2018-11-27 13:34:55 +01:00
|
|
|
$middleware->process($request, $handler);
|
|
|
|
// No tokens
|
|
|
|
$this->expectException(HttpAuthExpired::class);
|
|
|
|
$middleware->process($request, $handler);
|
2018-09-03 16:33:13 +02:00
|
|
|
}
|
|
|
|
}
|