Skip to content

Commit

Permalink
Use data providers
Browse files Browse the repository at this point in the history
  • Loading branch information
paulbalandan committed Jan 18, 2025
1 parent 9d24d4b commit d11d76a
Showing 1 changed file with 37 additions and 28 deletions.
65 changes: 37 additions & 28 deletions tests/system/Security/SecurityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use CodeIgniter\Test\Mock\MockSecurity;
use Config\Security as SecurityConfig;
use PHPUnit\Framework\Attributes\BackupGlobals;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;

/**
Expand All @@ -42,13 +43,23 @@ protected function setUp(): void
$this->resetServices();
}

private function createMockSecurity(?SecurityConfig $config = null): MockSecurity
private static function createMockSecurity(SecurityConfig $config = new SecurityConfig()): MockSecurity
{
$config ??= new SecurityConfig();

return new MockSecurity($config);
}

private static function createIncomingRequest(): IncomingRequest
{
$config = new MockAppConfig();

return new IncomingRequest(
$config,
new SiteURI($config),
null,
new UserAgent(),
);
}

public function testBasicConfigIsSaved(): void
{
$security = $this->createMockSecurity();
Expand Down Expand Up @@ -108,18 +119,6 @@ public function testCSRFVerifyPostThrowsExceptionOnNoMatch(): void
$security->verify($request);
}

private function createIncomingRequest(): IncomingRequest
{
$config = new MockAppConfig();

return new IncomingRequest(
$config,
new SiteURI($config),
null,
new UserAgent(),
);
}

public function testCSRFVerifyPostReturnsSelfOnMatch(): void
{
$_SERVER['REQUEST_METHOD'] = 'POST';
Expand Down Expand Up @@ -354,24 +353,34 @@ public function testGetPostedTokenReturnsTokenFromFormBody(): void
$this->assertSame('8b9218a55906f9dcc1dc263dce7f005a', $method($request));
}

public function testGetPostedTokenReturnsNullForInvalidInputs(): void
#[DataProvider('provideGetPostedTokenReturnsNullForInvalidInputs')]
public function testGetPostedTokenReturnsNullForInvalidInputs(string $case, IncomingRequest $request): void
{
$method = $this->getPrivateMethodInvoker($this->createMockSecurity(), 'getPostedToken');

$this->assertNull(
$method($request),
sprintf('Failed asserting that %s returns null on invalid input.', $case),
);
}

/**
* @return iterable<string, array{string, IncomingRequest}>
*/
public static function provideGetPostedTokenReturnsNullForInvalidInputs(): iterable
{
$method = $this->getPrivateMethodInvoker($this->createMockSecurity(), 'getPostedToken');
$testCases = [
'empty_post' => $this->createIncomingRequest(),
'invalid_post_data' => $this->createIncomingRequest()->setGlobal('post', ['csrf_test_name' => ['invalid' => 'data']]),
'empty_header' => $this->createIncomingRequest()->setHeader('X-CSRF-TOKEN', ''),
'invalid_json_data' => $this->createIncomingRequest()->setBody(json_encode(['csrf_test_name' => ['invalid' => 'data']])),
'invalid_json' => $this->createIncomingRequest()->setBody('{invalid json}'),
'missing_token_in_body' => $this->createIncomingRequest()->setBody('other=value&another=test'),
'invalid_form_data' => $this->createIncomingRequest()->setBody('csrf_test_name[]=invalid'),
'empty_post' => self::createIncomingRequest(),
'invalid_post_data' => self::createIncomingRequest()->setGlobal('post', ['csrf_test_name' => ['invalid' => 'data']]),
'empty_header' => self::createIncomingRequest()->setHeader('X-CSRF-TOKEN', ''),
'invalid_json_data' => self::createIncomingRequest()->setBody(json_encode(['csrf_test_name' => ['invalid' => 'data']])),
'invalid_json' => self::createIncomingRequest()->setBody('{invalid json}'),
'missing_token_in_body' => self::createIncomingRequest()->setBody('other=value&another=test'),
'invalid_form_data' => self::createIncomingRequest()->setBody('csrf_test_name[]=invalid'),
];

foreach ($testCases as $case => $request) {
$this->assertNull(
$method($request),
"Failed asserting that {$case} returns null"
);
yield $case => [$case, $request];
}
}
}

0 comments on commit d11d76a

Please sign in to comment.