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:13:06 +0300
committerAndrew Dolgov <fox@fakecake.org>2026-05-27 22:13:06 +0300
commit8d22861efda3b41e5fffce8b12638019b438587c (patch)
treefdc6aee813d9610a52be051b43015b903aa33800 /tests_integration
parentc900cc4fcc0a603a1a8887d4ff2919533cf22fdb (diff)
downloadthe-epube-8d22861efda3b41e5fffce8b12638019b438587c.tar.gz
the-epube-8d22861efda3b41e5fffce8b12638019b438587c.tar.xz
add tests for getinfo
Diffstat (limited to 'tests_integration')
-rw-r--r--tests_integration/GetinfoHandlerTest.php167
1 files changed, 167 insertions, 0 deletions
diff --git a/tests_integration/GetinfoHandlerTest.php b/tests_integration/GetinfoHandlerTest.php
new file mode 100644
index 0000000..1145d9e
--- /dev/null
+++ b/tests_integration/GetinfoHandlerTest.php
@@ -0,0 +1,167 @@
+<?php
+use PHPUnit\Framework\TestCase;
+
+final class GetinfoHandlerTest extends ApiTestCase
+{
+	// ── Authentication ───────────────────────────────────────────────────────
+
+	public function test_getinfo_requires_authentication(): void
+	{
+		[$status, $body] = $this->post($this->apiUrl, [
+			'op' => 'getinfo',
+			'id' => 1,
+		]);
+
+		$this->assertEquals(401, $status);
+		$this->assertArrayHasKey('error', $body);
+	}
+
+	// ── Valid book ───────────────────────────────────────────────────────────
+
+	public function test_getinfo_returns_book(): 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' => 'getinfo',
+			'id' => 2, // Adventures of Huckleberry Finn
+		]);
+
+		$this->assertEquals(200, $status);
+		$this->assertEquals(2, $body['id'] ?? null);
+		$this->assertEquals('Adventures of Huckleberry Finn', $body['title'] ?? null);
+		$this->assertEquals('Twain, Mark', $body['author_sort'] ?? null);
+		$this->assertArrayHasKey('series_name', $body);
+		$this->assertArrayHasKey('comment', $body);
+		$this->assertArrayHasKey('epub_id', $body);
+	}
+
+	public function test_getinfo_returns_all_book_fields(): 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' => 'getinfo',
+			'id' => 3,
+		]);
+
+		$this->assertEquals(200, $status);
+		// Verify all expected Calibre fields are present
+		$expectedFields = ['id', 'title', 'sort', 'timestamp', 'pubdate', 'series_index',
+			'author_sort', 'isbn', 'lccn', 'path', 'flags', 'uuid', 'has_cover', 'last_modified',
+			'series_name', 'comment', 'epub_id'];
+		foreach ($expectedFields as $field) {
+			$this->assertArrayHasKey($field, $body, "Missing field: $field");
+		}
+	}
+
+	// ── Non-existent book ────────────────────────────────────────────────────
+
+	public function test_getinfo_returns_404_for_missing_book(): 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' => 'getinfo',
+			'id' => 99999, // Non-existent book
+		]);
+
+		$this->assertEquals(404, $status);
+		$this->assertEquals('Not found.', $body['error']);
+	}
+
+	// ── Missing/invalid ID ───────────────────────────────────────────────────
+
+	public function test_getinfo_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' => 'getinfo',
+		]);
+
+		$this->assertEquals(404, $status);
+	}
+
+	public function test_getinfo_with_zero_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' => 'getinfo',
+			'id' => 0,
+		]);
+
+		$this->assertEquals(404, $status);
+	}
+
+	public function test_getinfo_with_negative_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' => 'getinfo',
+			'id' => -1,
+		]);
+
+		$this->assertEquals(404, $status);
+	}
+
+	// ── Sanitization ─────────────────────────────────────────────────────────
+
+	public function test_getinfo_sanitizes_book_comment(): 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' => 'getinfo',
+			'id' => 2,
+		]);
+
+		$this->assertEquals(200, $status);
+		// comment field exists and is a string (empty in test data, but Sanitizer::sanitize was called)
+		$this->assertIsString($body['comment'] ?? null);
+	}
+}