about summary refs log tree commit diff stats
path: root/tests_integration
diff options
context:
space:
mode:
authorAndrew Dolgov <fox@fakecake.org>2026-05-26 10:35:09 +0300
committerAndrew Dolgov <fox@fakecake.org>2026-05-26 10:35:09 +0300
commit2846cabf1d0c447e4f4c0aa429ac0381cbec3dd4 (patch)
tree8b18fa4b4d2fd433518dc8d321e94c1e21c1bc62 /tests_integration
parent42743863414ce4a34646268f2a468c7da476eb09 (diff)
downloadthe-epube-2846cabf1d0c447e4f4c0aa429ac0381cbec3dd4.tar.gz
the-epube-2846cabf1d0c447e4f4c0aa429ac0381cbec3dd4.tar.xz
add test for login
Diffstat (limited to 'tests_integration')
-rw-r--r--tests_integration/LoginTest.php133
1 files changed, 133 insertions, 0 deletions
diff --git a/tests_integration/LoginTest.php b/tests_integration/LoginTest.php
new file mode 100644
index 0000000..f6480bf
--- /dev/null
+++ b/tests_integration/LoginTest.php
@@ -0,0 +1,133 @@
+<?php
+use PHPUnit\Framework\TestCase;
+
+final class LoginTest extends TestCase
+{
+    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
+    {
+        [$status, $body] = $this->post($this->apiUrl, [
+            'op'       => 'perform-login',
+            'user'     => 'test',
+            'password' => 'test',
+        ]);
+
+        $this->assertEquals(200, $status);
+        $this->assertEquals('ok', $body['result'] ?? null);
+        $this->assertEquals('test', $body['user'] ?? null);
+    }
+
+    // ── Missing parameters ────────────────────────────────────────────────────
+
+    public function test_perform_login_missing_password(): void
+    {
+        [$status, $body] = $this->post($this->apiUrl, [
+            'op'   => 'perform-login',
+            'user' => 'test',
+        ]);
+
+        $this->assertEquals(400, $status);
+        $this->assertArrayHasKey('error', $body);
+    }
+
+    public function test_perform_login_missing_username(): void
+    {
+        [$status, $body] = $this->post($this->apiUrl, [
+            'op'       => 'perform-login',
+            'password' => 'test',
+        ]);
+
+        $this->assertEquals(400, $status);
+        $this->assertArrayHasKey('error', $body);
+    }
+
+    public function test_perform_login_no_params(): void
+    {
+        [$status, $body] = $this->post($this->apiUrl, [
+            'op' => 'perform-login',
+        ]);
+
+        $this->assertEquals(400, $status);
+        $this->assertArrayHasKey('error', $body);
+    }
+
+    // ── Invalid credentials ───────────────────────────────────────────────────
+
+    public function test_perform_login_wrong_password(): void
+    {
+        [$status, $body] = $this->post($this->apiUrl, [
+            'op'       => 'perform-login',
+            'user'     => 'test',
+            'password' => 'wrongpassword',
+        ]);
+
+        $this->assertEquals(401, $status);
+        $this->assertArrayHasKey('error', $body);
+    }
+
+    public function test_perform_login_nonexistent_user(): void
+    {
+        [$status, $body] = $this->post($this->apiUrl, [
+            'op'       => 'perform-login',
+            'user'     => 'nonexistent',
+            'password' => 'test',
+        ]);
+
+        $this->assertEquals(401, $status);
+        $this->assertArrayHasKey('error', $body);
+    }
+
+    // ── Username case-insensitivity ───────────────────────────────────────────
+
+    public function test_perform_login_uppercase_username(): void
+    {
+        [$status, $body] = $this->post($this->apiUrl, [
+            'op'       => 'perform-login',
+            'user'     => 'TEST',
+            'password' => 'test',
+        ]);
+
+        $this->assertEquals(200, $status);
+        $this->assertEquals('ok', $body['result'] ?? null);
+        // username is normalised to lowercase
+        $this->assertEquals('test', $body['user'] ?? null);
+    }
+}