|
|
@ -2,6 +2,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
namespace Engelsystem\Test\Unit\Middleware;
|
|
|
|
namespace Engelsystem\Test\Unit\Middleware;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
use Engelsystem\Http\Exceptions\HttpAuthExpired;
|
|
|
|
use Engelsystem\Middleware\VerifyCsrfToken;
|
|
|
|
use Engelsystem\Middleware\VerifyCsrfToken;
|
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
@ -33,13 +34,9 @@ class VerifyCsrfTokenTest extends TestCase
|
|
|
|
/** @var VerifyCsrfToken|MockObject $middleware */
|
|
|
|
/** @var VerifyCsrfToken|MockObject $middleware */
|
|
|
|
$middleware = $this->getMockBuilder(VerifyCsrfToken::class)
|
|
|
|
$middleware = $this->getMockBuilder(VerifyCsrfToken::class)
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->setMethods(['notAuthorizedResponse', 'tokensMatch'])
|
|
|
|
->setMethods(['tokensMatch'])
|
|
|
|
->getMock();
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
|
|
|
|
$middleware->expects($this->exactly(1))
|
|
|
|
|
|
|
|
->method('notAuthorizedResponse')
|
|
|
|
|
|
|
|
->willReturn($response);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$middleware->expects($this->exactly(2))
|
|
|
|
$middleware->expects($this->exactly(2))
|
|
|
|
->method('tokensMatch')
|
|
|
|
->method('tokensMatch')
|
|
|
|
->willReturnOnConsecutiveCalls(true, false);
|
|
|
|
->willReturnOnConsecutiveCalls(true, false);
|
|
|
@ -47,10 +44,15 @@ class VerifyCsrfTokenTest extends TestCase
|
|
|
|
// Results in true, false, false
|
|
|
|
// Results in true, false, false
|
|
|
|
$request->expects($this->exactly(3))
|
|
|
|
$request->expects($this->exactly(3))
|
|
|
|
->method('getMethod')
|
|
|
|
->method('getMethod')
|
|
|
|
->willReturnOnConsecutiveCalls('GET', 'POST', 'DELETE');
|
|
|
|
->willReturnOnConsecutiveCalls('GET', 'DELETE', 'POST');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Is reading
|
|
|
|
$middleware->process($request, $handler);
|
|
|
|
$middleware->process($request, $handler);
|
|
|
|
|
|
|
|
// Tokens match
|
|
|
|
$middleware->process($request, $handler);
|
|
|
|
$middleware->process($request, $handler);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// No match
|
|
|
|
|
|
|
|
$this->expectException(HttpAuthExpired::class);
|
|
|
|
$middleware->process($request, $handler);
|
|
|
|
$middleware->process($request, $handler);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -66,23 +68,18 @@ class VerifyCsrfTokenTest extends TestCase
|
|
|
|
$handler = $this->getMockForAbstractClass(RequestHandlerInterface::class);
|
|
|
|
$handler = $this->getMockForAbstractClass(RequestHandlerInterface::class);
|
|
|
|
/** @var ResponseInterface|MockObject $response */
|
|
|
|
/** @var ResponseInterface|MockObject $response */
|
|
|
|
$response = $this->getMockForAbstractClass(ResponseInterface::class);
|
|
|
|
$response = $this->getMockForAbstractClass(ResponseInterface::class);
|
|
|
|
/** @var ResponseInterface|MockObject $noAuthResponse */
|
|
|
|
|
|
|
|
$noAuthResponse = $this->getMockForAbstractClass(ResponseInterface::class);
|
|
|
|
|
|
|
|
/** @var SessionInterface|MockObject $session */
|
|
|
|
/** @var SessionInterface|MockObject $session */
|
|
|
|
$session = $this->getMockForAbstractClass(SessionInterface::class);
|
|
|
|
$session = $this->getMockForAbstractClass(SessionInterface::class);
|
|
|
|
|
|
|
|
|
|
|
|
/** @var VerifyCsrfToken|MockObject $middleware */
|
|
|
|
/** @var VerifyCsrfToken|MockObject $middleware */
|
|
|
|
$middleware = $this->getMockBuilder(VerifyCsrfToken::class)
|
|
|
|
$middleware = $this->getMockBuilder(VerifyCsrfToken::class)
|
|
|
|
->setConstructorArgs([$session])
|
|
|
|
->setConstructorArgs([$session])
|
|
|
|
->setMethods(['isReading', 'notAuthorizedResponse'])
|
|
|
|
->setMethods(['isReading'])
|
|
|
|
->getMock();
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
|
|
|
|
$middleware->expects($this->atLeastOnce())
|
|
|
|
$middleware->expects($this->atLeastOnce())
|
|
|
|
->method('isReading')
|
|
|
|
->method('isReading')
|
|
|
|
->willReturn(false);
|
|
|
|
->willReturn(false);
|
|
|
|
$middleware->expects($this->exactly(1))
|
|
|
|
|
|
|
|
->method('notAuthorizedResponse')
|
|
|
|
|
|
|
|
->willReturn($noAuthResponse);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$handler->expects($this->exactly(3))
|
|
|
|
$handler->expects($this->exactly(3))
|
|
|
|
->method('handle')
|
|
|
|
->method('handle')
|
|
|
@ -91,38 +88,39 @@ class VerifyCsrfTokenTest extends TestCase
|
|
|
|
$request->expects($this->exactly(4))
|
|
|
|
$request->expects($this->exactly(4))
|
|
|
|
->method('getParsedBody')
|
|
|
|
->method('getParsedBody')
|
|
|
|
->willReturnOnConsecutiveCalls(
|
|
|
|
->willReturnOnConsecutiveCalls(
|
|
|
|
null,
|
|
|
|
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
['_token' => 'PostFooToken'],
|
|
|
|
['_token' => 'PostFooToken'],
|
|
|
|
['_token' => 'PostBarToken']
|
|
|
|
['_token' => 'PostBarToken'],
|
|
|
|
|
|
|
|
null
|
|
|
|
);
|
|
|
|
);
|
|
|
|
$request->expects($this->exactly(4))
|
|
|
|
$request->expects($this->exactly(4))
|
|
|
|
->method('getHeader')
|
|
|
|
->method('getHeader')
|
|
|
|
->with('X-CSRF-TOKEN')
|
|
|
|
->with('X-CSRF-TOKEN')
|
|
|
|
->willReturnOnConsecutiveCalls(
|
|
|
|
->willReturnOnConsecutiveCalls(
|
|
|
|
[],
|
|
|
|
|
|
|
|
['HeaderFooToken'],
|
|
|
|
['HeaderFooToken'],
|
|
|
|
[],
|
|
|
|
[],
|
|
|
|
['HeaderBarToken']
|
|
|
|
['HeaderBarToken'],
|
|
|
|
|
|
|
|
[]
|
|
|
|
);
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
$session->expects($this->exactly(4))
|
|
|
|
$session->expects($this->exactly(4))
|
|
|
|
->method('get')
|
|
|
|
->method('get')
|
|
|
|
->with('_token')
|
|
|
|
->with('_token')
|
|
|
|
->willReturnOnConsecutiveCalls(
|
|
|
|
->willReturnOnConsecutiveCalls(
|
|
|
|
'NotAvailableToken',
|
|
|
|
|
|
|
|
'HeaderFooToken',
|
|
|
|
'HeaderFooToken',
|
|
|
|
'PostFooToken',
|
|
|
|
'PostFooToken',
|
|
|
|
'PostBarToken'
|
|
|
|
'PostBarToken',
|
|
|
|
|
|
|
|
'NotAvailableToken'
|
|
|
|
);
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
// Not tokens
|
|
|
|
|
|
|
|
$this->assertEquals($noAuthResponse, $middleware->process($request, $handler));
|
|
|
|
|
|
|
|
// Header token
|
|
|
|
// Header token
|
|
|
|
$this->assertEquals($response, $middleware->process($request, $handler));
|
|
|
|
$middleware->process($request, $handler);
|
|
|
|
// POST token
|
|
|
|
// POST token
|
|
|
|
$this->assertEquals($response, $middleware->process($request, $handler));
|
|
|
|
$middleware->process($request, $handler);
|
|
|
|
// Header and POST tokens
|
|
|
|
// Header and POST tokens
|
|
|
|
$this->assertEquals($response, $middleware->process($request, $handler));
|
|
|
|
$middleware->process($request, $handler);
|
|
|
|
|
|
|
|
// No tokens
|
|
|
|
|
|
|
|
$this->expectException(HttpAuthExpired::class);
|
|
|
|
|
|
|
|
$middleware->process($request, $handler);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|