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:24:45 +0300
committerAndrew Dolgov <fox@fakecake.org>2026-05-27 22:24:45 +0300
commit69b46e2fd39294d347206edd8b4ea01b749bdb51 (patch)
tree2425f9aadac012bc8f39a16ecf52af16c074f43b /tests_integration
parent8d22861efda3b41e5fffce8b12638019b438587c (diff)
downloadthe-epube-69b46e2fd39294d347206edd8b4ea01b749bdb51.tar.gz
the-epube-69b46e2fd39294d347206edd8b4ea01b749bdb51.tar.xz
update some error messages, add tests for cover
Diffstat (limited to 'tests_integration')
-rw-r--r--tests_integration/ApiTestCase.php54
-rw-r--r--tests_integration/CoverHandlerTest.php144
2 files changed, 198 insertions, 0 deletions
diff --git a/tests_integration/ApiTestCase.php b/tests_integration/ApiTestCase.php
index 8ce01e9..e1cba9f 100644
--- a/tests_integration/ApiTestCase.php
+++ b/tests_integration/ApiTestCase.php
@@ -58,4 +58,58 @@ abstract class ApiTestCase extends TestCase {
 
 		return [$status, $decoded ?? []];
 	}
+
+	/**
+	 * Make an HTTP POST request and return [status_code, raw_body, headers].
+	 * Useful for endpoints that return non-JSON (e.g., binary image data).
+	 * @param array<string, string|int> $data
+	 * @return array{0: int, 1: string, 2: array<string, string>}
+	 */
+	protected function postRaw(string $endpoint, array $data): array
+	{
+		// allow overriding 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);
+		curl_setopt_array($ch, [
+			CURLOPT_RETURNTRANSFER => true,
+			CURLOPT_POST           => true,
+			CURLOPT_POSTFIELDS     => http_build_query($data),
+			CURLOPT_TIMEOUT        => 10,
+			CURLOPT_CONNECTTIMEOUT => 5,
+			CURLOPT_COOKIEFILE     => $this->cookieFile,
+			CURLOPT_COOKIEJAR      => $this->cookieFile,
+			CURLOPT_HEADER         => true,
+		]);
+		$response = 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");
+		}
+
+		// Split headers and body
+		$headerEnd = strpos($response, "\r\n\r\n");
+		if ($headerEnd === false) {
+			return [$status, '', []];
+		}
+
+		$headerText = substr($response, 0, $headerEnd);
+		$body       = substr($response, $headerEnd + 4);
+
+		// Parse headers into associative array
+		$headers = [];
+		foreach (explode("\r\n", trim($headerText)) as $line) {
+			if (strpos($line, ':') !== false) {
+				[$key, $value] = explode(':', $line, 2);
+				$headers[trim($key)] = trim($value);
+			}
+		}
+
+		return [$status, $body, $headers];
+	}
 }
diff --git a/tests_integration/CoverHandlerTest.php b/tests_integration/CoverHandlerTest.php
new file mode 100644
index 0000000..d0fc37c
--- /dev/null
+++ b/tests_integration/CoverHandlerTest.php
@@ -0,0 +1,144 @@
+<?php
+use PHPUnit\Framework\TestCase;
+
+final class CoverHandlerTest extends ApiTestCase
+{
+	// ── Authentication ───────────────────────────────────────────────────────
+
+	public function test_cover_requires_authentication(): void
+	{
+		[$status, $body] = $this->post($this->apiUrl, [
+			'op' => 'cover',
+			'id' => 2,
+		]);
+
+		$this->assertEquals(401, $status);
+		$this->assertArrayHasKey('error', $body);
+	}
+
+	// ── Valid cover ──────────────────────────────────────────────────────────
+
+	public function test_cover_returns_200_for_existing_cover(): void
+	{
+		// Login
+		[$status, $body] = $this->post($this->apiUrl, [
+			'op'       => 'perform-login',
+			'user'     => 'test',
+			'password' => 'test',
+		]);
+		$this->assertEquals(200, $status);
+
+		[$status, $body, $headers] = $this->postRaw($this->apiUrl, [
+			'op' => 'cover',
+			'id' => 2, // Adventures of Huckleberry Finn
+		]);
+
+		$this->assertEquals(200, $status);
+		$this->assertStringContainsString('image/jpeg', $headers['Content-type'] ?? '');
+		$this->assertNotEmpty($body);
+		// JPEG magic bytes
+		$this->assertEquals('ffd8', bin2hex(substr($body, 0, 2)));
+	}
+
+	public function test_cover_returns_cache_headers(): void
+	{
+		// Login
+		[$status, $body] = $this->post($this->apiUrl, [
+			'op'       => 'perform-login',
+			'user'     => 'test',
+			'password' => 'test',
+		]);
+		$this->assertEquals(200, $status);
+
+		[$status, $body, $headers] = $this->postRaw($this->apiUrl, [
+			'op' => 'cover',
+			'id' => 3,
+		]);
+
+		print_r($headers);
+
+		$this->assertEquals(200, $status);
+		$this->assertArrayHasKey('Cache-control', $headers);
+		$this->assertStringContainsString('max-age=', $headers['Cache-control'] ?? '');
+		$this->assertArrayHasKey('Expires', $headers);
+		$this->assertArrayHasKey('Last-Modified', $headers);
+	}
+
+	// ── Missing cover file ───────────────────────────────────────────────────
+
+	public function test_cover_returns_404_for_missing_cover_file(): void
+	{
+		// Login
+		[$status, $body] = $this->post($this->apiUrl, [
+			'op'       => 'perform-login',
+			'user'     => 'test',
+			'password' => 'test',
+		]);
+		$this->assertEquals(200, $status);
+
+		// Book ID 1 doesn't exist in the Calibre DB, so the handler returns 404
+		[$status, $body] = $this->post($this->apiUrl, [
+			'op' => 'cover',
+			'id' => 1,
+		]);
+
+		$this->assertEquals(404, $status);
+		$this->assertEquals('Not found.', $body['error']);
+	}
+
+	// ── Missing/invalid ID ───────────────────────────────────────────────────
+
+	public function test_cover_without_id(): void
+	{
+		// Login
+		[$status, $body] = $this->post($this->apiUrl, [
+			'op'       => 'perform-login',
+			'user'     => 'test',
+			'password' => 'test',
+		]);
+		$this->assertEquals(200, $status);
+
+		[$status, $body] = $this->post($this->apiUrl, [
+			'op' => 'cover',
+		]);
+
+		$this->assertEquals(404, $status);
+		$this->assertEquals('Not found.', $body['error']);
+	}
+
+	public function test_cover_with_zero_id(): void
+	{
+		// Login
+		[$status, $body] = $this->post($this->apiUrl, [
+			'op'       => 'perform-login',
+			'user'     => 'test',
+			'password' => 'test',
+		]);
+		$this->assertEquals(200, $status);
+
+		[$status, $body, $headers] = $this->postRaw($this->apiUrl, [
+			'op' => 'cover',
+			'id' => 0,
+		]);
+
+		$this->assertEquals(404, $status);
+	}
+
+	public function test_cover_with_negative_id(): void
+	{
+		// Login
+		[$status, $body] = $this->post($this->apiUrl, [
+			'op'       => 'perform-login',
+			'user'     => 'test',
+			'password' => 'test',
+		]);
+		$this->assertEquals(200, $status);
+
+		[$status, $body, $headers] = $this->postRaw($this->apiUrl, [
+			'op' => 'cover',
+			'id' => -1,
+		]);
+
+		$this->assertEquals(404, $status);
+	}
+}