about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAndrew Dolgov <fox@fakecake.org>2026-05-27 21:29:30 +0300
committerAndrew Dolgov <fox@fakecake.org>2026-05-27 21:29:30 +0300
commit6851640586a06986f704f9182b3cc175c9ebde17 (patch)
treeae1011f4ee99254ca112eecd247f4c1db2cf5d96
parentb434f9127fc67d97ed224b22594ecbd232fa4da3 (diff)
downloadthe-epube-6851640586a06986f704f9182b3cc175c9ebde17.tar.gz
the-epube-6851640586a06986f704f9182b3cc175c9ebde17.tar.xz
fix tests via cookie jar parsing
-rw-r--r--tests_integration/ApiTestCase.php31
-rw-r--r--tests_integration/LogoutHandlerTest.php4
2 files changed, 32 insertions, 3 deletions
diff --git a/tests_integration/ApiTestCase.php b/tests_integration/ApiTestCase.php
index 0cb7a3d..e4a5f1f 100644
--- a/tests_integration/ApiTestCase.php
+++ b/tests_integration/ApiTestCase.php
@@ -14,12 +14,43 @@ abstract class ApiTestCase extends TestCase {
 	}
 
 	/**
+	 * Extract the epube_csrf_token cookie value from a cookie jar file.
+	 */
+	private function extractCsrfToken(?string $cookieFile): ?string
+	{
+		if (!$cookieFile || !file_exists($cookieFile)) {
+			return null;
+		}
+
+		$content = file_get_contents($cookieFile);
+
+		// localhost      FALSE   /       FALSE   1782498271      epube_csrf_token        xyztest123
+		$lines = explode("\n", $content);
+		foreach ($lines as $line) {
+			$parts = explode("\t", $line);
+
+			if (isset($parts[5]) && $parts[5] === 'epube_csrf_token') {
+				return $parts[6] ?? null;
+			}
+		}
+		return null;
+	}
+
+	/**
 	 * Make an HTTP POST request and return [status_code, decoded_body].
+	 * Automatically includes the epube_csrf_token cookie as a POST parameter.
 	 * @param array<string, string> $data
 	 * @return array<int, mixed>
 	 */
 	protected function post(string $endpoint, array $data, ?string $cookieFile = null): array
 	{
+		// Include CSRF token from cookie in POST body if available
+		$csrfToken = $this->extractCsrfToken($cookieFile);
+
+		if ($csrfToken !== null) {
+			$data['csrf_token'] = $csrfToken;
+		}
+
 		$ch = curl_init($endpoint);
 		curl_setopt_array($ch, [
 			CURLOPT_RETURNTRANSFER => true,
diff --git a/tests_integration/LogoutHandlerTest.php b/tests_integration/LogoutHandlerTest.php
index 93ee596..6742875 100644
--- a/tests_integration/LogoutHandlerTest.php
+++ b/tests_integration/LogoutHandlerTest.php
@@ -24,8 +24,6 @@ final class LogoutHandlerTest extends ApiTestCase
 			'op' => 'logout',
 		], $cookieFile);
 
-		print_r($body);
-
 		$this->assertEquals(200, $status);
 		$this->assertEquals('ok', $body['result'] ?? null);
 
@@ -95,7 +93,7 @@ final class LogoutHandlerTest extends ApiTestCase
 		], $cookieFile);
 
 		$this->assertEquals(200, $status1);
-		$this->assertEquals('OK', $body1['result'] ?? null);
+		$this->assertEquals('ok', $body1['result'] ?? null);
 
 		// Second logout — session already destroyed → 401
 		[$status2, $body2] = $this->post($this->apiUrl, [