diff options
| author | Andrew Dolgov <fox@fakecake.org> | 2026-05-27 20:45:59 +0300 |
|---|---|---|
| committer | Andrew Dolgov <fox@fakecake.org> | 2026-05-27 20:45:59 +0300 |
| commit | 2b28a847f97395d756d16400ce3fb29b8c8bb75e (patch) | |
| tree | cf3677ab97f9f9a984a1ee7fd5e5a4fd9853df96 /tests_integration | |
| parent | ac7b51679ca4df5fbb70d0ac796935de191b5e0d (diff) | |
| download | the-epube-2b28a847f97395d756d16400ce3fb29b8c8bb75e.tar.gz the-epube-2b28a847f97395d756d16400ce3fb29b8c8bb75e.tar.xz | |
add tests for logout
Diffstat (limited to 'tests_integration')
| -rw-r--r-- | tests_integration/DbTestCase.php | 6 | ||||
| -rw-r--r-- | tests_integration/LogoutHandlerTest.php | 146 |
2 files changed, 151 insertions, 1 deletions
diff --git a/tests_integration/DbTestCase.php b/tests_integration/DbTestCase.php index 5654017..a6ed51d 100644 --- a/tests_integration/DbTestCase.php +++ b/tests_integration/DbTestCase.php @@ -4,10 +4,14 @@ use PHPUnit\Framework\TestCase; abstract class DbTestCase extends TestCase { protected function setUp(): void { - + $_SESSION["owner"] = "test"; } protected function tearDown(): void { + if (session_status() == PHP_SESSION_ACTIVE) + session_destroy(); + // reset any possible local config changes + Config::get_instance()->reload(); } } diff --git a/tests_integration/LogoutHandlerTest.php b/tests_integration/LogoutHandlerTest.php new file mode 100644 index 0000000..4495872 --- /dev/null +++ b/tests_integration/LogoutHandlerTest.php @@ -0,0 +1,146 @@ +<?php +use PHPUnit\Framework\TestCase; + +final class LogoutHandlerTest extends TestCase +{ + private string $apiUrl; + + protected function setUp(): void + { + $this->apiUrl = getenv('API_URL') ?: 'http://localhost:9080/backend.php'; + } + + /** + * Make an HTTP POST request and return [status_code, decoded_body]. + * @param array<string, string> $data + * @return array<int, mixed> + */ + private function post(string $endpoint, array $data, ?string $cookieFile = null): array + { + $ch = curl_init($endpoint); + curl_setopt_array($ch, [ + CURLOPT_RETURNTRANSFER => true, + CURLOPT_POST => true, + CURLOPT_POSTFIELDS => http_build_query($data), + CURLOPT_TIMEOUT => 10, + CURLOPT_CONNECTTIMEOUT => 5, + CURLOPT_COOKIEFILE => $cookieFile ?? '', + CURLOPT_COOKIEJAR => $cookieFile ?? '', + ]); + $body = curl_exec($ch); + $status = curl_getinfo($ch, CURLINFO_HTTP_CODE); + $errno = curl_errno($ch); + $error = curl_error($ch); + curl_close($ch); + + if ($errno) { + $this->fail("cURL error ($errno): $error"); + } + + /** @var array<string, mixed>|null $decoded */ + $decoded = json_decode($body, true); + return [$status, $decoded ?? []]; + } + + // ── Successful logout ───────────────────────────────────────────────────── + + public function test_logout_returns_ok(): void + { + $cookieFile = tempnam(sys_get_temp_dir(), 'epube_cookie_'); + + // Step 1: Login + [$status, $body] = $this->post($this->apiUrl, [ + 'op' => 'perform-login', + 'user' => 'test', + 'password' => 'test', + ], $cookieFile); + + $this->assertEquals(200, $status); + $this->assertEquals('ok', $body['result'] ?? null); + + // Step 2: Logout + [$status, $body] = $this->post($this->apiUrl, [ + 'op' => 'logout', + ], $cookieFile); + + $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, [ + 'op' => 'perform-login', + 'user' => 'test', + 'password' => 'test', + ], $cookieFile); + + // Step 2: Logout + $this->post($this->apiUrl, [ + 'op' => 'logout', + ], $cookieFile); + + // 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, [ + 'op' => 'perform-login', + 'user' => 'test', + 'password' => 'test', + ], $cookieFile); + + // First logout + [$status1, $body1] = $this->post($this->apiUrl, [ + 'op' => 'logout', + ], $cookieFile); + + $this->assertEquals(200, $status1); + $this->assertEquals('OK', $body1['result'] ?? null); + + // Second logout — session already destroyed → 401 + [$status2, $body2] = $this->post($this->apiUrl, [ + 'op' => 'logout', + ], $cookieFile); + + $this->assertEquals(401, $status2); + + unlink($cookieFile); + } +} |
