From a35e0b6a2bcf12a950e547bc873d64191874c8f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Thu, 23 Jul 2026 16:17:23 +0200 Subject: [PATCH] Add E2E gitea tests --- tests/e2e/Services/VCSGitea/VCSGiteaBase.php | 17 +++ .../VCSGitea/VCSGiteaConsoleClientTest.php | 144 ++++++++++++++++++ 2 files changed, 161 insertions(+) create mode 100644 tests/e2e/Services/VCSGitea/VCSGiteaBase.php create mode 100644 tests/e2e/Services/VCSGitea/VCSGiteaConsoleClientTest.php diff --git a/tests/e2e/Services/VCSGitea/VCSGiteaBase.php b/tests/e2e/Services/VCSGitea/VCSGiteaBase.php new file mode 100644 index 0000000000..34a63e1010 --- /dev/null +++ b/tests/e2e/Services/VCSGitea/VCSGiteaBase.php @@ -0,0 +1,17 @@ +markTestSkipped('Gitea VCS is not configured.'); + } + } +} diff --git a/tests/e2e/Services/VCSGitea/VCSGiteaConsoleClientTest.php b/tests/e2e/Services/VCSGitea/VCSGiteaConsoleClientTest.php new file mode 100644 index 0000000000..a719709576 --- /dev/null +++ b/tests/e2e/Services/VCSGitea/VCSGiteaConsoleClientTest.php @@ -0,0 +1,144 @@ +getProject()['$id']; + + $authorize = $this->client->call(Client::METHOD_GET, '/vcs/gitea/authorize', array_merge([ + 'x-appwrite-project' => $projectId, + ], $this->getHeaders()), [], true, false); + + $this->assertEquals(301, $authorize['headers']['status-code']); + + $loginUrl = $authorize['headers']['location'] ?? ''; + $this->assertStringContainsString('/login/oauth/authorize', (string) $loginUrl); + + $query = []; + \parse_str(\parse_url($loginUrl, PHP_URL_QUERY) ?: '', $query); + $this->assertNotEmpty($query['client_id'] ?? ''); + $this->assertNotEmpty($query['redirect_uri'] ?? ''); + $this->assertNotEmpty($query['state'] ?? ''); + $this->assertSame('code', $query['response_type'] ?? ''); + + // Location targets the browser-facing endpoint, unreachable in-container; reuse only its path and query + $gitea = new Client(); + $gitea->setEndpoint(System::getEnv('_APP_VCS_GITEA_ENDPOINT', 'http://gitea:3000')); + + $this->giteaCall($gitea, Client::METHOD_GET, '/user/login'); + $this->assertNotEmpty($this->giteaCookies['_csrf'] ?? '', 'Gitea did not issue a CSRF cookie.'); + + $login = $this->giteaCall($gitea, Client::METHOD_POST, '/user/login', [ + '_csrf' => $this->giteaCookies['_csrf'], + 'user_name' => self::GITEA_USERNAME, + 'password' => self::GITEA_PASSWORD, + ]); + $this->assertContains($login['headers']['status-code'], [302, 303], 'Gitea login failed.'); + $this->assertNotEmpty($this->giteaCookies['i_like_gitea'] ?? '', 'Gitea did not issue a session cookie.'); + + // Gitea stores client_id, state and redirect_uri in the session here; the grant must match them + $authorizePath = \parse_url($loginUrl, PHP_URL_PATH) . '?' . \parse_url($loginUrl, PHP_URL_QUERY); + $consent = $this->giteaCall($gitea, Client::METHOD_GET, $authorizePath); + + if (\in_array($consent['headers']['status-code'], [302, 303])) { + // Already granted: Gitea redirects straight back with a code + $redirect = $consent['headers']['location'] ?? ''; + } else { + $this->assertEquals(200, $consent['headers']['status-code']); + + $grant = $this->giteaCall($gitea, Client::METHOD_POST, '/login/oauth/grant', [ + '_csrf' => $this->giteaCookies['_csrf'], + 'client_id' => $query['client_id'], + 'state' => $query['state'], + 'scope' => $query['scope'] ?? '', + 'nonce' => '', + 'redirect_uri' => $query['redirect_uri'], + ]); + $this->assertEquals(303, $grant['headers']['status-code']); + + $redirect = $grant['headers']['location'] ?? ''; + } + + $this->assertStringContainsString('/v1/vcs/gitea/callback', (string) $redirect); + + $callbackQuery = []; + \parse_str(\parse_url($redirect, PHP_URL_QUERY) ?: '', $callbackQuery); + $this->assertNotEmpty($callbackQuery['code'] ?? ''); + $this->assertNotEmpty($callbackQuery['state'] ?? ''); + + $callback = $this->client->call(Client::METHOD_GET, '/vcs/gitea/callback', array_merge([ + 'x-appwrite-project' => $projectId, + ], $this->getHeaders()), [ + 'code' => $callbackQuery['code'], + 'state' => $callbackQuery['state'], + ], true, false); + + $this->assertEquals(301, $callback['headers']['status-code']); + $this->assertStringContainsString('git-installations', (string) ($callback['headers']['location'] ?? '')); + + $installations = $this->client->call(Client::METHOD_GET, '/vcs/installations', array_merge([ + 'x-appwrite-project' => $projectId, + ], $this->getHeaders())); + + $this->assertEquals(200, $installations['headers']['status-code']); + $this->assertGreaterThanOrEqual(1, $installations['body']['total']); + + $installation = null; + foreach ($installations['body']['installations'] as $candidate) { + if ($candidate['provider'] === 'gitea') { + $installation = $candidate; + break; + } + } + + $this->assertNotNull($installation, 'Gitea installation not found in listInstallations.'); + $this->assertNotEmpty($installation['$id']); + $this->assertEquals('gitea', $installation['provider']); + $this->assertEquals(self::GITEA_USERNAME, $installation['organization']); + $this->assertTrue($installation['personal']); + } + + // Client does not persist cookies between calls, so carry them manually and never follow redirects + private function giteaCall(Client $gitea, string $method, string $path, array $params = []): array + { + $headers = []; + + if (!empty($this->giteaCookies)) { + $headers['cookie'] = \implode('; ', \array_map( + fn (string $name) => $name . '=' . $this->giteaCookies[$name], + \array_keys($this->giteaCookies) + )); + } + + if ($method !== Client::METHOD_GET) { + $headers['content-type'] = 'application/x-www-form-urlencoded'; + } + + $response = $gitea->call($method, $path, $headers, $params, false, false); + + $this->giteaCookies = \array_merge($this->giteaCookies, $response['cookies']); + + return $response; + } +}