diff options
| author | Andrew Dolgov <fox@fakecake.org> | 2026-05-27 21:43:15 +0300 |
|---|---|---|
| committer | Andrew Dolgov <fox@fakecake.org> | 2026-05-27 21:43:15 +0300 |
| commit | 03d1c9a1f0bd65cd7fb64a9bcb1c8c8a8961ff8e (patch) | |
| tree | efb792b888653d7fdb9e63c2699cd90fa80e339a /tests_integration | |
| parent | 37f9ded993c7f01293167a705bffea1a7c8fa1d6 (diff) | |
| download | the-epube-03d1c9a1f0bd65cd7fb64a9bcb1c8c8a8961ff8e.tar.gz the-epube-03d1c9a1f0bd65cd7fb64a9bcb1c8c8a8961ff8e.tar.xz | |
remove unnecessary csrf token testing / local vars
Diffstat (limited to 'tests_integration')
| -rw-r--r-- | tests_integration/ApiTestCase.php | 13 | ||||
| -rw-r--r-- | tests_integration/LogoutHandlerTest.php | 16 |
2 files changed, 17 insertions, 12 deletions
diff --git a/tests_integration/ApiTestCase.php b/tests_integration/ApiTestCase.php index 1997653..e5cebee 100644 --- a/tests_integration/ApiTestCase.php +++ b/tests_integration/ApiTestCase.php @@ -5,6 +5,7 @@ abstract class ApiTestCase extends TestCase { protected string $apiUrl; private string $cookieFile; + private string $csrfToken = ""; protected function setUp(): void { @@ -23,11 +24,11 @@ abstract class ApiTestCase extends TestCase { * @param string|null $csrfToken CSRF token to include in the POST body. * @return array<int, mixed> */ - protected function post(string $endpoint, array $data, ?string $csrfToken = null): array + protected function post(string $endpoint, array $data): array { - if ($csrfToken !== null) { - $data['csrf_token'] = $csrfToken; - } + + if ($this->csrfToken) + $data['csrf_token'] = $this->csrfToken; $ch = curl_init($endpoint); curl_setopt_array($ch, [ @@ -51,6 +52,10 @@ abstract class ApiTestCase extends TestCase { /** @var array<string, mixed>|null $decoded */ $decoded = json_decode($body, true); + + if (isset($decoded["csrf_token"])) + $this->csrfToken = $decoded["csrf_token"]; + return [$status, $decoded ?? []]; } } diff --git a/tests_integration/LogoutHandlerTest.php b/tests_integration/LogoutHandlerTest.php index 35289f2..3eff109 100644 --- a/tests_integration/LogoutHandlerTest.php +++ b/tests_integration/LogoutHandlerTest.php @@ -16,13 +16,12 @@ final class LogoutHandlerTest extends ApiTestCase $this->assertEquals(200, $status); $this->assertEquals('ok', $body['result'] ?? null); - $csrfToken = $body['csrf_token'] ?? null; - $this->assertNotNull($csrfToken); + $this->assertNotNull($body['csrf_token']); // Step 2: Logout [$status, $body] = $this->post($this->apiUrl, [ 'op' => 'logout', - ], $csrfToken); + ]); $this->assertEquals(200, $status); $this->assertEquals('ok', $body['result'] ?? null); @@ -39,12 +38,12 @@ final class LogoutHandlerTest extends ApiTestCase 'password' => 'test', ]); $this->assertEquals(200, $status); - $csrfToken = $body['csrf_token'] ?? null; + $this->assertNotNull($body['csrf_token']); // Step 2: Logout $this->post($this->apiUrl, [ 'op' => 'logout', - ], $csrfToken); + ]); // Step 3: Try an auth-required operation → should be 401 [$status, $body] = $this->post($this->apiUrl, [ @@ -76,12 +75,13 @@ final class LogoutHandlerTest extends ApiTestCase 'user' => 'test', 'password' => 'test', ]); - $csrfToken = $body['csrf_token'] ?? null; + + $this->assertNotNull($body['csrf_token']); // First logout [$status1, $body1] = $this->post($this->apiUrl, [ 'op' => 'logout', - ], $csrfToken); + ]); $this->assertEquals(200, $status1); $this->assertEquals('ok', $body1['result'] ?? null); @@ -89,7 +89,7 @@ final class LogoutHandlerTest extends ApiTestCase // Second logout — session already destroyed → 401 [$status2, $body2] = $this->post($this->apiUrl, [ 'op' => 'logout', - ], $csrfToken); + ]); $this->assertEquals(401, $status2); } |
