Skip to content

Commit

Permalink
update deprecated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
olegbaturin committed Nov 8, 2024
1 parent 7e15fee commit 9e56915
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 7 deletions.
2 changes: 2 additions & 0 deletions tests/DeprecatedCsrfMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Yiisoft\Csrf\Synchronizer\Generator\RandomCsrfTokenGenerator;
use Yiisoft\Csrf\Synchronizer\SynchronizerCsrfToken;
use Yiisoft\Csrf\Tests\Synchronizer\Storage\MockCsrfTokenStorage;
use Yiisoft\Http\Method;

final class DeprecatedCsrfMiddlewareTest extends TestCase
{
Expand Down Expand Up @@ -46,6 +47,7 @@ public function testImmutability(): void
$original = $this->createMiddleware();
$this->assertNotSame($original, $original->withHeaderName('csrf'));
$this->assertNotSame($original, $original->withParameterName('csrf'));
$this->assertNotSame($original, $original->withSafeMethods([Method::HEAD]));
}

private function createMiddleware(): CsrfMiddleware
Expand Down
64 changes: 57 additions & 7 deletions tests/DeprecatedTokenCsrfMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,27 @@ abstract class DeprecatedTokenCsrfMiddlewareTest extends TestCase

private string $token;

public function testGetIsAlwaysAllowed(): void
{
$middleware = $this->createCsrfMiddleware();
$response = $middleware->process($this->createServerRequest(Method::GET), $this->createRequestHandler());
$this->assertEquals(200, $response->getStatusCode());
}

public function testHeadIsAlwaysAllowed(): void
{
$middleware = $this->createCsrfMiddleware();
$response = $middleware->process($this->createServerRequest(Method::HEAD), $this->createRequestHandler());
$this->assertEquals(200, $response->getStatusCode());
}

public function testOptionsIsAlwaysAllowed(): void
{
$middleware = $this->createCsrfMiddleware();
$response = $middleware->process($this->createServerRequest(Method::OPTIONS), $this->createRequestHandler());
$this->assertEquals(200, $response->getStatusCode());
}

public function testValidTokenInBodyPostRequestResultIn200(): void
{
$middleware = $this->createCsrfMiddleware();
Expand Down Expand Up @@ -79,13 +100,6 @@ public function testValidTokenInCustomHeaderResultIn200(): void
$this->assertEquals(200, $response->getStatusCode());
}

public function testGetIsAlwaysAllowed(): void
{
$middleware = $this->createCsrfMiddleware();
$response = $middleware->process($this->createServerRequest(Method::GET), $this->createRequestHandler());
$this->assertEquals(200, $response->getStatusCode());
}

public function testInvalidTokenResultIn422(): void
{
$middleware = $this->createCsrfMiddleware();
Expand Down Expand Up @@ -130,6 +144,42 @@ public function testEmptyTokenInRequestResultIn422(): void
$this->assertEquals(Status::UNPROCESSABLE_ENTITY, $response->getStatusCode());
}

public function testUnsafeMethodPostRequestResultIn422(): void
{
$middleware = $this->createCsrfMiddleware();
$response = $middleware->process(
$this->createServerRequest(Method::POST),
$this->createRequestHandler()
);
$this->assertEquals(Status::TEXTS[Status::UNPROCESSABLE_ENTITY], $response->getBody());
$this->assertEquals(Status::UNPROCESSABLE_ENTITY, $response->getStatusCode());
}

public function testCustomSafeOptionsRequestResultIn200(): void
{
$middleware = $this
->createCsrfMiddleware()
->withSafeMethods([Method::OPTIONS]);
$response = $middleware->process(
$this->createServerRequest(Method::OPTIONS),
$this->createRequestHandler()
);
$this->assertEquals(200, $response->getStatusCode());
}

public function testCustomUnsafeMethodGetRequestResultIn422(): void
{
$middleware = $this
->createCsrfMiddleware()
->withSafeMethods([Method::OPTIONS]);
$response = $middleware->process(
$this->createServerRequest(Method::GET),
$this->createRequestHandler()
);
$this->assertEquals(Status::TEXTS[Status::UNPROCESSABLE_ENTITY], $response->getBody());
$this->assertEquals(Status::UNPROCESSABLE_ENTITY, $response->getStatusCode());
}

private function createServerRequest(
string $method = Method::POST,
array $bodyParams = [],
Expand Down

0 comments on commit 9e56915

Please sign in to comment.