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 21:19:36 +0300
committerAndrew Dolgov <fox@fakecake.org>2026-05-27 21:19:39 +0300
commitb434f9127fc67d97ed224b22594ecbd232fa4da3 (patch)
tree90449be52f3d9b85f449cc65707a75e82cc81b3a /tests_integration
parent2b28a847f97395d756d16400ce3fb29b8c8bb75e (diff)
downloadthe-epube-b434f9127fc67d97ed224b22594ecbd232fa4da3.tar.gz
the-epube-b434f9127fc67d97ed224b22594ecbd232fa4da3.tar.xz
cleanup return values a bit (more json), move common API test stuff to an ancestor class
Diffstat (limited to 'tests_integration')
-rw-r--r--tests_integration/ApiTestCase.php47
-rw-r--r--tests_integration/LoginTest.php39
-rw-r--r--tests_integration/LogoutHandlerTest.php47
-rw-r--r--tests_integration/TogglefavHandlerTest.php237
-rw-r--r--tests_integration/bootstrap.php1
5 files changed, 291 insertions, 80 deletions
diff --git a/tests_integration/ApiTestCase.php b/tests_integration/ApiTestCase.php
new file mode 100644
index 0000000..0cb7a3d
--- /dev/null
+++ b/tests_integration/ApiTestCase.php
@@ -0,0 +1,47 @@
+<?php
+use PHPUnit\Framework\TestCase;
+
+abstract class ApiTestCase extends TestCase {
+	protected string $apiUrl;
+
+	protected function setUp(): void
+	{
+		$this->apiUrl = getenv('API_URL') ?: 'http://localhost:9080/backend.php';
+	}
+
+	protected function tearDown(): void {
+
+	}
+
+	/**
+	 * Make an HTTP POST request and return [status_code, decoded_body].
+	 * @param array<string, string> $data
+	 * @return array<int, mixed>
+	 */
+	protected function post(string $endpoint, array $data, ?string $cookieFile = null): array
+	{
+		$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     => $cookieFile ?? '',
+			CURLOPT_COOKIEJAR      => $cookieFile ?? '',
+		]);
+		$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 ?? []];
+	}
+}
diff --git a/tests_integration/LoginTest.php b/tests_integration/LoginTest.php
index 3e155d8..6d685c7 100644
--- a/tests_integration/LoginTest.php
+++ b/tests_integration/LoginTest.php
@@ -1,45 +1,8 @@
 <?php
 use PHPUnit\Framework\TestCase;
 
-final class LoginTest extends TestCase
+final class LoginTest extends ApiTestCase
 {
-	private string $apiUrl;
-
-	protected function setUp(): void
-	{
-		$this->apiUrl = getenv('API_URL') ?: 'http://localhost:9080/backend.php';
-	}
-
-	/**
-	 * Make an HTTP POST request and return [status_code, decoded_body].
-	 * @param array<string, string> $data
-	 * @return array<int, mixed>
-	 */
-	private function post(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_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 ?? []];
-	}
-
 	// ── Successful login ──────────────────────────────────────────────────────
 
 	public function test_perform_login_success(): void
diff --git a/tests_integration/LogoutHandlerTest.php b/tests_integration/LogoutHandlerTest.php
index 4495872..93ee596 100644
--- a/tests_integration/LogoutHandlerTest.php
+++ b/tests_integration/LogoutHandlerTest.php
@@ -1,47 +1,8 @@
 <?php
 use PHPUnit\Framework\TestCase;
 
-final class LogoutHandlerTest extends TestCase
+final class LogoutHandlerTest extends ApiTestCase
 {
-	private string $apiUrl;
-
-	protected function setUp(): void
-	{
-		$this->apiUrl = getenv('API_URL') ?: 'http://localhost:9080/backend.php';
-	}
-
-	/**
-	 * Make an HTTP POST request and return [status_code, decoded_body].
-	 * @param array<string, string> $data
-	 * @return array<int, mixed>
-	 */
-	private function post(string $endpoint, array $data, ?string $cookieFile = null): array
-	{
-		$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     => $cookieFile ?? '',
-			CURLOPT_COOKIEJAR      => $cookieFile ?? '',
-		]);
-		$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 ?? []];
-	}
-
 	// ── Successful logout ─────────────────────────────────────────────────────
 
 	public function test_logout_returns_ok(): void
@@ -63,8 +24,10 @@ final class LogoutHandlerTest extends TestCase
 			'op' => 'logout',
 		], $cookieFile);
 
+		print_r($body);
+
 		$this->assertEquals(200, $status);
-		$this->assertEquals('OK', $body['result'] ?? null);
+		$this->assertEquals('ok', $body['result'] ?? null);
 
 		unlink($cookieFile);
 	}
@@ -79,7 +42,7 @@ final class LogoutHandlerTest extends TestCase
 		$this->post($this->apiUrl, [
 			'op'       => 'perform-login',
 			'user'     => 'test',
-			'password' => 'test',
+			'password' => 'test1',
 		], $cookieFile);
 
 		// Step 2: Logout
diff --git a/tests_integration/TogglefavHandlerTest.php b/tests_integration/TogglefavHandlerTest.php
new file mode 100644
index 0000000..c55d3e1
--- /dev/null
+++ b/tests_integration/TogglefavHandlerTest.php
@@ -0,0 +1,237 @@
+<?php
+use PHPUnit\Framework\TestCase;
+
+final class TogglefavHandlerTest 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);
+		$errno   = curl_errno($ch);
+		$error   = curl_error($ch);
+		curl_close($ch);
+
+		if ($errno) {
+			$this->fail("cURL error ($errno): $error");
+		}
+
+		$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 ?? []];
+	}
+
+	/**
+	 * Query the favorites table via PDO to check if a favorite exists.
+	 */
+	private function favoriteExists(int $bookId): bool
+	{
+		$pdo = Db::pdo();
+		$sth = $pdo->prepare("SELECT 1 FROM epube_favorites WHERE bookid = ? AND owner = 'test'");
+		$sth->execute([$bookId]);
+		return (bool) $sth->fetch();
+	}
+
+	/**
+	 * Remove any existing favorite for a book to ensure clean state.
+	 */
+	private function removeFavorite(int $bookId): void
+	{
+		$pdo = Db::pdo();
+		$sth = $pdo->prepare("DELETE FROM epube_favorites WHERE bookid = ? AND owner = 'test'");
+		$sth->execute([$bookId]);
+	}
+
+	// ── Unauthenticated access ──────────────────────────────────────────────
+
+	public function test_togglefav_requires_authentication(): void
+	{
+		$ch = curl_init($this->apiUrl);
+		curl_setopt_array($ch, [
+			CURLOPT_RETURNTRANSFER => true,
+			CURLOPT_POST           => true,
+			CURLOPT_POSTFIELDS     => http_build_query([
+				'op'   => 'togglefav',
+				'id'   => 2,
+			]),
+			CURLOPT_TIMEOUT        => 10,
+			CURLOPT_CONNECTTIMEOUT => 5,
+		]);
+		$body    = curl_exec($ch);
+		$status  = curl_getinfo($ch, CURLINFO_HTTP_CODE);
+		curl_close($ch);
+
+		$this->assertEquals(401, $status);
+	}
+
+	// ── Toggle on ────────────────────────────────────────────────────────────
+
+	public function test_togglefav_adds_favorite(): void
+	{
+		$this->loginAs('test', 'test');
+		$bookId = 2; // Adventures of Huckleberry Finn
+
+		// Ensure clean state
+		$this->removeFavorite($bookId);
+
+		[$status, $body] = $this->authenticatedPost($this->apiUrl, [
+			'op' => 'togglefav',
+			'id' => $bookId,
+		]);
+
+		$this->assertEquals(200, $status);
+		$this->assertEquals($bookId, $body['id'] ?? null);
+		$this->assertEquals(1, $body['status'] ?? null);
+		$this->assertTrue($this->favoriteExists($bookId));
+	}
+
+	// ── Toggle off ───────────────────────────────────────────────────────────
+
+	public function test_togglefav_removes_favorite(): void
+	{
+		$this->loginAs('test', 'test');
+		$bookId = 3; // The Adventures of Sherlock Holmes
+
+		// Ensure favorite exists
+		$this->removeFavorite($bookId);
+		$this->authenticatedPost($this->apiUrl, [
+			'op' => 'togglefav',
+			'id' => $bookId,
+		]);
+
+		// Toggle off
+		[$status, $body] = $this->authenticatedPost($this->apiUrl, [
+			'op' => 'togglefav',
+			'id' => $bookId,
+		]);
+
+		$this->assertEquals(200, $status);
+		$this->assertEquals($bookId, $body['id'] ?? null);
+		$this->assertEquals(0, $body['status'] ?? null);
+		$this->assertFalse($this->favoriteExists($bookId));
+	}
+
+	// ── Toggle again (re-add) ────────────────────────────────────────────────
+
+	public function test_togglefav_re_adds_after_removal(): void
+	{
+		$this->loginAs('test', 'test');
+		$bookId = 4; // Metamorphosis
+
+		// Ensure favorite exists, then remove it
+		$this->removeFavorite($bookId);
+		$this->authenticatedPost($this->apiUrl, [
+			'op' => 'togglefav',
+			'id' => $bookId,
+		]);
+		$this->authenticatedPost($this->apiUrl, [
+			'op' => 'togglefav',
+			'id' => $bookId,
+		]);
+
+		// Toggle back on
+		[$status, $body] = $this->authenticatedPost($this->apiUrl, [
+			'op' => 'togglefav',
+			'id' => $bookId,
+		]);
+
+		$this->assertEquals(200, $status);
+		$this->assertEquals($bookId, $body['id'] ?? null);
+		$this->assertEquals(1, $body['status'] ?? null);
+		$this->assertTrue($this->favoriteExists($bookId));
+	}
+
+	// ── Invalid book ID ──────────────────────────────────────────────────────
+
+	public function test_togglefav_with_zero_id(): void
+	{
+		$this->loginAs('test', 'test');
+		$bookId = 0;
+
+		// Ensure clean state
+		$this->removeFavorite($bookId);
+
+		[$status, $body] = $this->authenticatedPost($this->apiUrl, [
+			'op' => 'togglefav',
+			'id' => $bookId,
+		]);
+
+		// Handler casts to int, so id=0 is valid — creates a favorite for bookid=0
+		$this->assertEquals(200, $status);
+		$this->assertEquals(0, $body['id'] ?? null);
+		$this->assertEquals(1, $body['status'] ?? null);
+		$this->assertTrue($this->favoriteExists($bookId));
+
+		// Clean up
+		$this->removeFavorite($bookId);
+	}
+}
diff --git a/tests_integration/bootstrap.php b/tests_integration/bootstrap.php
index d5fd639..0a7e8d0 100644
--- a/tests_integration/bootstrap.php
+++ b/tests_integration/bootstrap.php
@@ -4,6 +4,7 @@
 
 	require_once "common.php";
 
+	require_once __DIR__ . "/ApiTestCase.php";
 	require_once __DIR__ . "/DbTestCase.php";
 
 	// Force Config singleton to re-read the environment variables