diff options
| author | Andrew Dolgov <fox@fakecake.org> | 2026-05-27 21:39:01 +0300 |
|---|---|---|
| committer | Andrew Dolgov <fox@fakecake.org> | 2026-05-27 21:39:01 +0300 |
| commit | 37f9ded993c7f01293167a705bffea1a7c8fa1d6 (patch) | |
| tree | 2196b474d93b7329cf15856283fd0894173146a4 /tests_integration/LogoutHandlerTest.php | |
| parent | d2c2453e4854d55e5ceaacd4203197e560c38073 (diff) | |
| download | the-epube-37f9ded993c7f01293167a705bffea1a7c8fa1d6.tar.gz the-epube-37f9ded993c7f01293167a705bffea1a7c8fa1d6.tar.xz | |
get csrf token from login response
Diffstat (limited to 'tests_integration/LogoutHandlerTest.php')
| -rw-r--r-- | tests_integration/LogoutHandlerTest.php | 47 |
1 files changed, 18 insertions, 29 deletions
diff --git a/tests_integration/LogoutHandlerTest.php b/tests_integration/LogoutHandlerTest.php index 6742875..35289f2 100644 --- a/tests_integration/LogoutHandlerTest.php +++ b/tests_integration/LogoutHandlerTest.php @@ -7,90 +7,81 @@ final class LogoutHandlerTest extends ApiTestCase public function test_logout_returns_ok(): void { - $cookieFile = tempnam(sys_get_temp_dir(), 'epube_cookie_'); - - // Step 1: Login + // Step 1: Login (response includes csrf_token) [$status, $body] = $this->post($this->apiUrl, [ 'op' => 'perform-login', 'user' => 'test', 'password' => 'test', - ], $cookieFile); + ]); $this->assertEquals(200, $status); $this->assertEquals('ok', $body['result'] ?? null); + $csrfToken = $body['csrf_token'] ?? null; + $this->assertNotNull($csrfToken); // Step 2: Logout [$status, $body] = $this->post($this->apiUrl, [ 'op' => 'logout', - ], $cookieFile); + ], $csrfToken); $this->assertEquals(200, $status); $this->assertEquals('ok', $body['result'] ?? null); - - unlink($cookieFile); } // ── Session is destroyed after logout ───────────────────────────────────── public function test_logout_destroys_session(): void { - $cookieFile = tempnam(sys_get_temp_dir(), 'epube_cookie_'); - // Step 1: Login - $this->post($this->apiUrl, [ + [$status, $body] = $this->post($this->apiUrl, [ 'op' => 'perform-login', 'user' => 'test', - 'password' => 'test1', - ], $cookieFile); + 'password' => 'test', + ]); + $this->assertEquals(200, $status); + $csrfToken = $body['csrf_token'] ?? null; // Step 2: Logout $this->post($this->apiUrl, [ 'op' => 'logout', - ], $cookieFile); + ], $csrfToken); // Step 3: Try an auth-required operation → should be 401 [$status, $body] = $this->post($this->apiUrl, [ 'op' => 'list', - ], $cookieFile); + ]); $this->assertEquals(401, $status); - - unlink($cookieFile); } // ── Logout without authentication ───────────────────────────────────────── public function test_logout_without_auth(): void { - $cookieFile = tempnam(sys_get_temp_dir(), 'epube_cookie_'); - [$status, $body] = $this->post($this->apiUrl, [ 'op' => 'logout', - ], $cookieFile); + ]); $this->assertEquals(401, $status); $this->assertArrayHasKey('error', $body); - - unlink($cookieFile); } // ── Double logout ───────────────────────────────────────────────────────── public function test_double_logout_second_fails(): void { - $cookieFile = tempnam(sys_get_temp_dir(), 'epube_cookie_'); - // Login - $this->post($this->apiUrl, [ + [$status, $body] = $this->post($this->apiUrl, [ 'op' => 'perform-login', 'user' => 'test', 'password' => 'test', - ], $cookieFile); + ]); + $csrfToken = $body['csrf_token'] ?? null; // First logout [$status1, $body1] = $this->post($this->apiUrl, [ 'op' => 'logout', - ], $cookieFile); + ], $csrfToken); $this->assertEquals(200, $status1); $this->assertEquals('ok', $body1['result'] ?? null); @@ -98,10 +89,8 @@ final class LogoutHandlerTest extends ApiTestCase // Second logout — session already destroyed → 401 [$status2, $body2] = $this->post($this->apiUrl, [ 'op' => 'logout', - ], $cookieFile); + ], $csrfToken); $this->assertEquals(401, $status2); - - unlink($cookieFile); } } |
