diff options
| author | Andrew Dolgov <fox@fakecake.org> | 2026-05-27 22:04:38 +0300 |
|---|---|---|
| committer | Andrew Dolgov <fox@fakecake.org> | 2026-05-27 22:04:38 +0300 |
| commit | c900cc4fcc0a603a1a8887d4ff2919533cf22fdb (patch) | |
| tree | 13d00c417b691e8c6027d15c3d70d2669d08d7d3 /tests_integration/CsrfMiddlewareTest.php | |
| parent | 496d8a72e4e652543d8cca11b2a105c00744ae13 (diff) | |
| download | the-epube-c900cc4fcc0a603a1a8887d4ff2919533cf22fdb.tar.gz the-epube-c900cc4fcc0a603a1a8887d4ff2919533cf22fdb.tar.xz | |
add csrf middleware tests
Diffstat (limited to 'tests_integration/CsrfMiddlewareTest.php')
| -rw-r--r-- | tests_integration/CsrfMiddlewareTest.php | 133 |
1 files changed, 133 insertions, 0 deletions
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'] ?? ''); + } +} |
