diff options
| author | Andrew Dolgov <fox@fakecake.org> | 2026-05-26 10:48:27 +0300 |
|---|---|---|
| committer | Andrew Dolgov <fox@fakecake.org> | 2026-05-26 10:48:27 +0300 |
| commit | 7585cd2047345487e062a86ea7b3fee1acf52d68 (patch) | |
| tree | a2e35963b108e2c0126fa091ac5be4cf8aa01c3c /tests_integration | |
| parent | 2846cabf1d0c447e4f4c0aa429ac0381cbec3dd4 (diff) | |
| download | the-epube-7585cd2047345487e062a86ea7b3fee1acf52d68.tar.gz the-epube-7585cd2047345487e062a86ea7b3fee1acf52d68.tar.xz | |
add integration tests for list
Diffstat (limited to 'tests_integration')
| -rw-r--r-- | tests_integration/ListTest.php | 237 |
1 files changed, 237 insertions, 0 deletions
diff --git a/tests_integration/ListTest.php b/tests_integration/ListTest.php new file mode 100644 index 0000000..95985fc --- /dev/null +++ b/tests_integration/ListTest.php @@ -0,0 +1,237 @@ +<?php +use PHPUnit\Framework\TestCase; + +final class ListTest extends TestCase +{ + private string $apiUrl; + private ?string $cookieFile = null; + + protected function setUp(): void + { + $this->apiUrl = getenv('API_URL') ?: 'http://localhost:9080/backend.php'; + $this->cookieFile = tempnam(sys_get_temp_dir(), 'epube_cookie_'); + } + + protected function tearDown(): void + { + if ($this->cookieFile && file_exists($this->cookieFile)) { + unlink($this->cookieFile); + } + } + + /** + * Login and return a cookie file path ready for authenticated requests. + * @return string Path to the cookie jar file. + */ + private function loginAs(string $username, string $password): string + { + $ch = curl_init($this->apiUrl); + curl_setopt_array($ch, [ + CURLOPT_RETURNTRANSFER => true, + CURLOPT_POST => true, + CURLOPT_POSTFIELDS => http_build_query([ + 'op' => 'perform-login', + 'user' => $username, + 'password' => $password, + ]), + CURLOPT_COOKIEJAR => $this->cookieFile, + CURLOPT_COOKIEFILE => $this->cookieFile, + CURLOPT_TIMEOUT => 10, + CURLOPT_CONNECTTIMEOUT => 5, + ]); + $body = curl_exec($ch); + $status = curl_getinfo($ch, CURLINFO_HTTP_CODE); + curl_close($ch); + + $this->assertEquals(200, $status, "Login failed: $body"); + + $decoded = json_decode($body, true); + $this->assertArrayHasKey('result', $decoded); + $this->assertEquals('ok', $decoded['result']); + + return $this->cookieFile; + } + + /** + * Make an authenticated HTTP POST request and return [status_code, decoded_body]. + * @param array<string, string|int> $data + * @return array<int, mixed> + */ + private function authenticatedPost(string $endpoint, array $data): array + { + $ch = curl_init($endpoint); + curl_setopt_array($ch, [ + CURLOPT_RETURNTRANSFER => true, + CURLOPT_POST => true, + CURLOPT_POSTFIELDS => http_build_query($data), + CURLOPT_COOKIEFILE => $this->cookieFile, + CURLOPT_COOKIEJAR => $this->cookieFile, + CURLOPT_TIMEOUT => 10, + CURLOPT_CONNECTTIMEOUT => 5, + ]); + $body = 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"); + } + + /** @var array<string, mixed>|null $decoded */ + $decoded = json_decode($body, true); + return [$status, $decoded ?? []]; + } + + // ── Unauthenticated access ────────────────────────────────────────────── + + public function test_list_requires_authentication(): void + { + $ch = curl_init($this->apiUrl); + curl_setopt_array($ch, [ + CURLOPT_RETURNTRANSFER => true, + CURLOPT_POST => true, + CURLOPT_POSTFIELDS => http_build_query(['op' => 'list']), + CURLOPT_TIMEOUT => 10, + CURLOPT_CONNECTTIMEOUT => 5, + ]); + $body = curl_exec($ch); + $status = curl_getinfo($ch, CURLINFO_HTTP_CODE); + curl_close($ch); + + $this->assertEquals(401, $status); + } + + // ── Basic list (authenticated) ────────────────────────────────────────── + + public function test_list_returns_books(): void + { + $this->loginAs('test', 'test'); + + [$status, $body] = $this->authenticatedPost($this->apiUrl, [ + 'op' => 'list', + ]); + + $this->assertEquals(200, $status); + $this->assertArrayHasKey('books', $body); + $this->assertArrayHasKey('total', $body); + $this->assertIsArray($body['books']); + $this->assertGreaterThan(0, $body['total']); + } + + public function test_list_book_fields(): void + { + $this->loginAs('test', 'test'); + + [$status, $body] = $this->authenticatedPost($this->apiUrl, [ + 'op' => 'list', + 'limit' => 1, + ]); + + $this->assertEquals(200, $status); + $this->assertCount(1, $body['books']); + + $book = $body['books'][0]; + $this->assertArrayHasKey('id', $book); + $this->assertArrayHasKey('title', $book); + $this->assertArrayHasKey('author_sort', $book); + $this->assertArrayHasKey('series_name', $book); + $this->assertArrayHasKey('series_index', $book); + $this->assertArrayHasKey('cover_link', $book); + $this->assertArrayHasKey('read_link', $book); + $this->assertArrayHasKey('is_fav', $book); + $this->assertArrayHasKey('download_formats', $book); + } + + // ── Pagination ────────────────────────────────────────────────────────── + + public function test_list_pagination(): void + { + $this->loginAs('test', 'test'); + + [$status1, $body1] = $this->authenticatedPost($this->apiUrl, [ + 'op' => 'list', + 'limit' => 2, + 'offset' => 0, + ]); + + [$status2, $body2] = $this->authenticatedPost($this->apiUrl, [ + 'op' => 'list', + 'limit' => 2, + 'offset' => 2, + ]); + + $this->assertEquals(200, $status1); + $this->assertEquals(200, $status2); + $this->assertCount(2, $body1['books']); + $this->assertCount(2, $body2['books']); + $this->assertNotEquals($body1['books'][0]['id'], $body2['books'][0]['id']); + } + + // ── Search / query ────────────────────────────────────────────────────── + + public function test_list_search_by_title(): void + { + $this->loginAs('test', 'test'); + + [$status, $body] = $this->authenticatedPost($this->apiUrl, [ + 'op' => 'list', + 'query' => 'Huckleberry', + ]); + + $this->assertEquals(200, $status); + $this->assertGreaterThan(0, $body['total']); + + foreach ($body['books'] as $book) { + $this->assertStringContainsString('Huckleberry', $book['title']); + } + } + + public function test_list_search_by_author(): void + { + $this->loginAs('test', 'test'); + + [$status, $body] = $this->authenticatedPost($this->apiUrl, [ + 'op' => 'list', + 'query' => 'Kafka', + ]); + + $this->assertEquals(200, $status); + $this->assertGreaterThan(0, $body['total']); + + foreach ($body['books'] as $book) { + $this->assertStringContainsString('Kafka', $book['author_sort']); + } + } + + public function test_list_search_no_match(): void + { + $this->loginAs('test', 'test'); + + [$status, $body] = $this->authenticatedPost($this->apiUrl, [ + 'op' => 'list', + 'query' => 'xyznonexistent', + ]); + + $this->assertEquals(200, $status); + $this->assertEquals(0, $body['total']); + $this->assertCount(0, $body['books']); + } + + // ── Favorites mode ────────────────────────────────────────────────────── + + public function test_list_favorites_empty(): void + { + $this->loginAs('test', 'test'); + + [$status, $body] = $this->authenticatedPost($this->apiUrl, [ + 'op' => 'list', + 'mode' => 'favorites', + ]); + + $this->assertEquals(200, $status); + $this->assertEquals(0, $body['total']); + $this->assertCount(0, $body['books']); + } +} |
