Files
breadcrumb-the-shire/tests/Service/Auth/MicrosoftOidcStateStoreServiceTest.php

72 lines
2.2 KiB
PHP
Raw Normal View History

2026-02-23 12:58:19 +01:00
<?php
namespace MintyPHP\Tests\Service\Auth;
2026-03-06 00:44:52 +01:00
use MintyPHP\Http\SessionStore;
2026-02-23 12:58:19 +01:00
use MintyPHP\Service\Auth\MicrosoftOidcStateStoreService;
use PHPUnit\Framework\TestCase;
class MicrosoftOidcStateStoreServiceTest extends TestCase
{
private array $previousSession = [];
protected function setUp(): void
{
parent::setUp();
$this->previousSession = $_SESSION ?? [];
$_SESSION = [];
}
protected function tearDown(): void
{
$_SESSION = $this->previousSession;
parent::tearDown();
}
public function testStoreAndConsumeIsSingleUse(): void
{
2026-03-06 00:44:52 +01:00
$store = new MicrosoftOidcStateStoreService(new SessionStore(), 600, 50);
2026-02-23 12:58:19 +01:00
$store->store('state-a', ['tenant_id' => 5, 'nonce' => 'n1']);
$first = $store->consume('state-a');
2026-03-04 15:56:58 +01:00
$this->assertTrue($first['ok']);
2026-02-23 12:58:19 +01:00
$this->assertSame(5, (int) (($first['entry']['tenant_id'] ?? 0)));
$second = $store->consume('state-a');
2026-03-04 15:56:58 +01:00
$this->assertFalse($second['ok']);
$this->assertSame('state_invalid', (string) $second['error']);
2026-02-23 12:58:19 +01:00
}
public function testConsumeReturnsExpiredForOldState(): void
{
2026-03-06 00:44:52 +01:00
$store = new MicrosoftOidcStateStoreService(new SessionStore(), 1, 50);
2026-02-23 12:58:19 +01:00
$store->store('state-old', [
'tenant_id' => 9,
'created_at' => time() - 120,
]);
$result = $store->consume('state-old');
2026-03-04 15:56:58 +01:00
$this->assertFalse($result['ok']);
$this->assertSame('state_expired', (string) $result['error']);
2026-02-23 12:58:19 +01:00
}
public function testStoreRespectsMaxEntriesAndPrunesOldStates(): void
{
2026-03-06 00:44:52 +01:00
$store = new MicrosoftOidcStateStoreService(new SessionStore(), 600, 2);
2026-02-23 12:58:19 +01:00
$store->store('state-1', ['created_at' => time() - 3]);
$store->store('state-2', ['created_at' => time() - 2]);
$store->store('state-3', ['created_at' => time() - 1]);
$invalid = $store->consume('state-1');
2026-03-04 15:56:58 +01:00
$this->assertFalse($invalid['ok']);
$this->assertSame('state_invalid', (string) $invalid['error']);
2026-02-23 12:58:19 +01:00
$valid2 = $store->consume('state-2');
2026-03-04 15:56:58 +01:00
$this->assertTrue($valid2['ok']);
2026-02-23 12:58:19 +01:00
$valid3 = $store->consume('state-3');
2026-03-04 15:56:58 +01:00
$this->assertTrue($valid3['ok']);
2026-02-23 12:58:19 +01:00
}
}