about summary refs log tree commit diff stats
path: root/tests_integration
diff options
context:
space:
mode:
authorAndrew Dolgov <fox@fakecake.org>2026-05-27 22:04:38 +0300
committerAndrew Dolgov <fox@fakecake.org>2026-05-27 22:04:38 +0300
commitc900cc4fcc0a603a1a8887d4ff2919533cf22fdb (patch)
tree13d00c417b691e8c6027d15c3d70d2669d08d7d3 /tests_integration
parent496d8a72e4e652543d8cca11b2a105c00744ae13 (diff)
downloadthe-epube-c900cc4fcc0a603a1a8887d4ff2919533cf22fdb.tar.gz
the-epube-c900cc4fcc0a603a1a8887d4ff2919533cf22fdb.tar.xz
add csrf middleware tests
Diffstat (limited to 'tests_integration')
-rw-r--r--tests_integration/ApiTestCase.php3
-rw-r--r--tests_integration/CsrfMiddlewareTest.php133
2 files changed, 135 insertions, 1 deletions
diff --git a/tests_integration/ApiTestCase.php b/tests_integration/ApiTestCase.php
index d36f4ea..8ce01e9 100644
--- a/tests_integration/ApiTestCase.php
+++ b/tests_integration/ApiTestCase.php
@@ -26,7 +26,8 @@ abstract class ApiTestCase extends TestCase {
 	protected function post(string $endpoint, array $data): array
 	{
 
-		if ($this->csrfToken)
+		// allow overring automatic csrf_token to fail requests or w/e
+		if ($this->csrfToken && !isset($data['csrf_token']))
 			$data['csrf_token'] = $this->csrfToken;
 
 		$ch = curl_init($endpoint);
diff --git a/tests_integration/CsrfMiddlewareTest.php b/tests_integration/CsrfMiddlewareTest.php
new file mode 100644
index 0000000..dcf7ed8
--- /dev/null
+++ b/tests_integration/CsrfMiddlewareTest.php
@@ -0,0 +1,133 @@
+<?php
+use PHPUnit\Framework\TestCase;
+
+final class CsrfMiddlewareTest extends ApiTestCase
+{
+	// ── Ignored operations — no CSRF check needed ────────────────────────────
+
+	public function test_ignored_op_list_without_csrf(): void
+	{
+		// Login to establish session (list is ignored, so no CSRF check)
+		[$status, $body] = $this->post($this->apiUrl, [
+			'op'       => 'perform-login',
+			'user'     => 'test',
+			'password' => 'test',
+		]);
+		$this->assertEquals(200, $status);
+
+		// list is in csrfIgnore — should work without csrf_token
+		[$status, $body] = $this->post($this->apiUrl, [
+			'op' => 'list',
+		]);
+
+		$this->assertEquals(200, $status);
+		$this->assertArrayHasKey('books', $body);
+	}
+
+	public function test_ignored_op_getinfo_without_csrf(): void
+	{
+		// Login to establish session
+		[$status, $body] = $this->post($this->apiUrl, [
+			'op'       => 'perform-login',
+			'user'     => 'test',
+			'password' => 'test',
+		]);
+		$this->assertEquals(200, $status);
+
+		// getinfo is in csrfIgnore — should work without csrf_token
+		[$status, $body] = $this->post($this->apiUrl, [
+			'op' => 'getinfo',
+			'id' => 2,
+			'csrf_token' => '',
+		]);
+
+		$this->assertEquals(200, $status);
+		$this->assertArrayHasKey('id', $body);
+	}
+
+	// ── Non-ignored operations — CSRF required ───────────────────────────────
+
+	public function test_non_ignored_op_requires_csrf(): void
+	{
+		// Login to establish session
+		[$status, $body] = $this->post($this->apiUrl, [
+			'op'       => 'perform-login',
+			'user'     => 'test',
+			'password' => 'test',
+		]);
+		$this->assertEquals(200, $status);
+
+		// togglefav is NOT in csrfIgnore — should require csrf_token
+		[$status, $body] = $this->post($this->apiUrl, [
+			'op' => 'togglefav',
+			'id' => 1,
+			'csrf_token' => '',
+		]);
+
+		$this->assertEquals(401, $status);
+		$this->assertArrayHasKey('error', $body);
+		$this->assertStringContainsString('CSRF', $body['error'] ?? '');
+	}
+
+	public function test_non_ignored_op_with_valid_csrf(): void
+	{
+		// Login to establish session and get csrf_token
+		[$status, $body] = $this->post($this->apiUrl, [
+			'op'       => 'perform-login',
+			'user'     => 'test',
+			'password' => 'test',
+		]);
+		$this->assertEquals(200, $status);
+		$csrfToken = $body['csrf_token'] ?? '';
+		$this->assertNotEmpty($csrfToken);
+
+		// togglefav with valid csrf_token should succeed
+		[$status, $body] = $this->post($this->apiUrl, [
+			'op'       => 'togglefav',
+			'id'       => 1,
+			'csrf_token' => $csrfToken,
+		]);
+
+		$this->assertEquals(200, $status);
+		$this->assertArrayHasKey('id', $body);
+	}
+
+	public function test_non_ignored_op_with_invalid_csrf(): void
+	{
+		// Login to establish session
+		[$status, $body] = $this->post($this->apiUrl, [
+			'op'       => 'perform-login',
+			'user'     => 'test',
+			'password' => 'test',
+		]);
+		$this->assertEquals(200, $status);
+
+		// togglefav with wrong csrf_token should fail
+		[$status, $body] = $this->post($this->apiUrl, [
+			'op'       => 'togglefav',
+			'id'       => 1,
+			'csrf_token' => 'invalid_token_12345',
+		]);
+
+		$this->assertEquals(401, $status);
+		$this->assertArrayHasKey('error', $body);
+		$this->assertStringContainsString('CSRF', $body['error'] ?? '');
+	}
+
+	// ── Unauthenticated — no CSRF check ─────────────────────────────────────
+
+	public function test_unauthenticated_no_csrf_check(): void
+	{
+		// Without a session, CSRF middleware should skip its check
+		// and let the handler deal with auth
+		[$status, $body] = $this->post($this->apiUrl, [
+			'op' => 'togglefav',
+			'id' => 1,
+		]);
+
+		$this->assertEquals(401, $status);
+		// Error should be about authentication, not CSRF
+		$this->assertArrayHasKey('error', $body);
+		$this->assertStringNotContainsString('CSRF', $body['error'] ?? '');
+	}
+}