diff options
Diffstat (limited to 'tests_integration/LoginTest.php')
| -rw-r--r-- | tests_integration/LoginTest.php | 133 |
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); + } +} |
