about summary refs log tree commit diff stats
path: root/tests_integration/DownloadHandlerTest.php
diff options
context:
space:
mode:
authorAndrew Dolgov <fox@fakecake.org>2026-05-28 07:27:12 +0300
committerAndrew Dolgov <fox@fakecake.org>2026-05-28 07:27:12 +0300
commit8b453f2c6741e5a0ac18f73c0470f00ed6e91ed6 (patch)
treef0a07901f985221cd305358a1be884b1415ef642 /tests_integration/DownloadHandlerTest.php
parent5007508dc8e461553aeaf47e49a1f538da881335 (diff)
downloadthe-epube-8b453f2c6741e5a0ac18f73c0470f00ed6e91ed6.tar.gz
the-epube-8b453f2c6741e5a0ac18f73c0470f00ed6e91ed6.tar.xz
add download handler test
Diffstat (limited to 'tests_integration/DownloadHandlerTest.php')
-rw-r--r--tests_integration/DownloadHandlerTest.php165
1 files changed, 165 insertions, 0 deletions
diff --git a/tests_integration/DownloadHandlerTest.php b/tests_integration/DownloadHandlerTest.php
new file mode 100644
index 0000000..edf9e46
--- /dev/null
+++ b/tests_integration/DownloadHandlerTest.php
@@ -0,0 +1,165 @@
+<?php
+use PHPUnit\Framework\TestCase;
+
+final class DownloadHandlerTest extends ApiTestCase
+{
+	// ── Authentication ───────────────────────────────────────────────────────
+
+	public function test_download_requires_authentication(): void
+	{
+		[$status, $body] = $this->post($this->apiUrl, [
+			'op' => 'download',
+			'id' => 6,
+		]);
+
+		$this->assertEquals(401, $status);
+		$this->assertArrayHasKey('error', $body);
+	}
+
+	// ── Valid downloads ──────────────────────────────────────────────────────
+
+	public function test_download_returns_200_for_existing_file(): void
+	{
+		// Login
+		[$status, $body] = $this->post($this->apiUrl, [
+			'op'       => 'perform-login',
+			'user'     => 'test',
+			'password' => 'test',
+		]);
+		$this->assertEquals(200, $status);
+
+		// data.id 6 = Huckleberry Finn EPUB
+		[$status, $body, $headers] = $this->post($this->apiUrl, [
+			'op' => 'download',
+			'id' => 6,
+		], false);
+
+		$this->assertEquals(200, $status);
+		$this->assertStringContainsString('application/epub+zip', $headers['Content-type'] ?? '');
+		$this->assertStringContainsString('attachment', $headers['Content-Disposition'] ?? '');
+		$this->assertStringContainsString('epub', $headers['Content-Disposition'] ?? '');
+		$this->assertNotEmpty($body);
+	}
+
+	public function test_download_original_epub_format(): void
+	{
+		// Login
+		[$status, $body] = $this->post($this->apiUrl, [
+			'op'       => 'perform-login',
+			'user'     => 'test',
+			'password' => 'test',
+		]);
+		$this->assertEquals(200, $status);
+
+		// data.id 5 = Huckleberry Finn ORIGINAL_EPUB
+		[$status, $body, $headers] = $this->post($this->apiUrl, [
+			'op' => 'download',
+			'id' => 5,
+		], false);
+
+		$this->assertEquals(200, $status);
+		$this->assertStringContainsString('application/epub+zip', $headers['Content-type'] ?? '');
+		$this->assertStringContainsString('attachment', $headers['Content-Disposition'] ?? '');
+		$this->assertNotEmpty($body);
+	}
+
+	public function test_download_different_book(): void
+	{
+		// Login
+		[$status, $body] = $this->post($this->apiUrl, [
+			'op'       => 'perform-login',
+			'user'     => 'test',
+			'password' => 'test',
+		]);
+		$this->assertEquals(200, $status);
+
+		// data.id 9 = Sherlock Holmes EPUB
+		[$status, $body, $headers] = $this->post($this->apiUrl, [
+			'op' => 'download',
+			'id' => 9,
+		], false);
+
+		$this->assertEquals(200, $status);
+		$this->assertStringContainsString('application/epub+zip', $headers['Content-type'] ?? '');
+		$this->assertStringContainsString('attachment', $headers['Content-Disposition'] ?? '');
+		$this->assertNotEmpty($body);
+	}
+
+	// ── Missing/invalid ID ───────────────────────────────────────────────────
+
+	public function test_download_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' => 'download',
+		]);
+
+		$this->assertEquals(404, $status);
+		$this->assertEquals('Not found.', $body['error']);
+	}
+
+	public function test_download_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' => 'download',
+			'id' => 0,
+		]);
+
+		$this->assertEquals(404, $status);
+		$this->assertEquals('Not found.', $body['error']);
+	}
+
+	public function test_download_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' => 'download',
+			'id' => -1,
+		]);
+
+		$this->assertEquals(404, $status);
+		$this->assertEquals('Not found.', $body['error']);
+	}
+
+	public function test_download_with_nonexistent_data_id(): void
+	{
+		// Login
+		[$status, $body] = $this->post($this->apiUrl, [
+			'op'       => 'perform-login',
+			'user'     => 'test',
+			'password' => 'test',
+		]);
+		$this->assertEquals(200, $status);
+
+		// data.id 99999 doesn't exist in the DB
+		[$status, $body] = $this->post($this->apiUrl, [
+			'op' => 'download',
+			'id' => 99999,
+		]);
+
+		$this->assertEquals(404, $status);
+		$this->assertEquals('Not found.', $body['error']);
+	}
+}