1
0
Fork 0
mirror of synced 2024-09-17 01:47:57 +12:00
appwrite/tests/e2e/Services/VCS/VCSConsoleClientTest.php

288 lines
10 KiB
PHP
Raw Normal View History

2023-10-09 22:45:36 +13:00
<?php
namespace Tests\E2E\Services\VCS;
use Tests\E2E\Scopes\Scope;
use Tests\E2E\Client;
use Tests\E2E\Scopes\ProjectCustom;
use Tests\E2E\Scopes\SideConsole;
2023-10-11 09:01:22 +13:00
use Utopia\App;
use Utopia\Database\Helpers\ID;
use Utopia\Database\Helpers\Role;
2023-10-09 22:45:36 +13:00
class VCSConsoleClientTest extends Scope
{
use ProjectCustom;
use SideConsole;
2023-10-11 09:01:22 +13:00
public ?string $providerRepositoryId = null;
protected function setUp(): void
{
parent::setUp();
$this->providerRepositoryId = App::getEnv('_APP_VCS_TEST_PROVIDER_REPOSITORY_ID');
}
2023-10-13 09:15:27 +13:00
public function testGitHubAuthorize()
2023-10-09 22:45:36 +13:00
{
/**
* Test for SUCCESS
*/
2023-10-13 09:15:27 +13:00
$response = $this->client->call(Client::METHOD_GET, '/mock/github/callback', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'installation_id' => App::getEnv('_APP_VCS_TEST_GITHUB_INSTALLATION_ID'),
'projectId' => $this->getProject()['$id'],
]);
$this->assertNotEmpty($response['body']['installationId']);
$installationId = $response['body']['installationId'];
return $installationId;
}
/**
* @depends testGitHubAuthorize
*/
public function testGetInstallation(string $installationId)
{
/**
* Test for SUCCESS
*/
$installation = $this->client->call(Client::METHOD_GET, '/vcs/installations/' . $installationId, array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'installationId' => $installationId
]);
$this->assertEquals(200, $installation['headers']['status-code']);
}
2023-10-09 22:45:36 +13:00
2023-10-13 09:15:27 +13:00
/**
* @depends testGitHubAuthorize
*/
public function testDetectRuntime(string $installationId)
{
/**
* Test for SUCCESS
*/
$runtime = $this->client->call(Client::METHOD_POST, '/vcs/github/installations/' . $installationId . '/providerRepositories/' . $this->providerRepositoryId . '/detection', array_merge([
2023-10-09 22:45:36 +13:00
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
2023-10-13 09:15:27 +13:00
'installationId' => $installationId,
2023-10-11 09:01:22 +13:00
'providerRepositoryId' => $this->providerRepositoryId
2023-10-09 22:45:36 +13:00
]);
$this->assertEquals(200, $runtime['headers']['status-code']);
$this->assertEquals($runtime['body']['runtime'], 'ruby-3.1');
/**
* Test for FAILURE
*/
2023-10-14 02:20:58 +13:00
$runtime = $this->client->call(Client::METHOD_POST, '/vcs/github/installations/' . $installationId . '/providerRepositories/randomRepositoryId/detection', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'installationId' => $installationId,
'providerRepositoryId' => 'randomRepositoryId'
]);
$this->assertEquals(404, $runtime['headers']['status-code']);
2023-10-09 22:45:36 +13:00
}
2023-10-13 09:15:27 +13:00
/**
* @depends testGitHubAuthorize
*/
public function testListRepositories(string $installationId)
2023-10-09 22:45:36 +13:00
{
/**
* Test for SUCCESS
*/
2023-10-13 09:15:27 +13:00
$repositories = $this->client->call(Client::METHOD_GET, '/vcs/github/installations/' . $installationId . '/providerRepositories', array_merge([
2023-10-09 22:45:36 +13:00
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
2023-10-13 09:15:27 +13:00
'installationId' => $installationId
2023-10-09 22:45:36 +13:00
]);
$this->assertEquals(200, $repositories['headers']['status-code']);
$this->assertEquals($repositories['body']['total'], 3);
2023-10-13 09:15:27 +13:00
$searchedRepositories = $this->client->call(Client::METHOD_GET, '/vcs/github/installations/' . $installationId . '/providerRepositories', array_merge([
2023-10-09 22:45:36 +13:00
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
2023-10-13 09:15:27 +13:00
'installationId' => $installationId,
2023-10-09 22:45:36 +13:00
'search' => 'func'
]);
$this->assertEquals(200, $searchedRepositories['headers']['status-code']);
$this->assertEquals($searchedRepositories['body']['total'], 1);
2023-10-11 10:26:15 +13:00
/**
* Test for FAILURE
*/
$repositories = $this->client->call(Client::METHOD_GET, '/vcs/github/installations/randomInstallationId/providerRepositories', array_merge([
2023-10-11 10:26:15 +13:00
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'installationId' => 'randomInstallationId'
2023-10-11 10:26:15 +13:00
]);
$this->assertEquals(404, $repositories['headers']['status-code']);
2023-10-09 22:45:36 +13:00
}
2023-10-13 09:15:27 +13:00
/**
* @depends testGitHubAuthorize
*/
public function testGetRepository(string $installationId, string $providerRepositoryId2 = '700020051')
2023-10-09 22:45:36 +13:00
{
/**
* Test for SUCCESS
*/
2023-10-13 09:15:27 +13:00
$repository = $this->client->call(Client::METHOD_GET, '/vcs/github/installations/' . $installationId . '/providerRepositories/' . $this->providerRepositoryId, array_merge([
2023-10-09 22:45:36 +13:00
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
2023-10-13 09:15:27 +13:00
'installationId' => $installationId,
2023-10-11 09:01:22 +13:00
'providerRepositoryId' => $this->providerRepositoryId
2023-10-09 22:45:36 +13:00
]);
$this->assertEquals(200, $repository['headers']['status-code']);
$this->assertEquals($repository['body']['name'], 'ruby-starter');
2023-10-13 09:15:27 +13:00
$repository = $this->client->call(Client::METHOD_GET, '/vcs/github/installations/' . $installationId . '/providerRepositories/' . $providerRepositoryId2, array_merge([
2023-10-09 22:45:36 +13:00
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
2023-10-13 09:15:27 +13:00
'installationId' => $installationId,
2023-10-09 22:45:36 +13:00
'providerRepositoryId' => $providerRepositoryId2
]);
$this->assertEquals(200, $repository['headers']['status-code']);
$this->assertEquals($repository['body']['name'], 'function-1.4');
2023-10-11 10:26:15 +13:00
/**
* Test for FAILURE
*/
2023-10-14 02:20:58 +13:00
$repository = $this->client->call(Client::METHOD_GET, '/vcs/github/installations/' . $installationId . '/providerRepositories/randomRepositoryId', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'installationId' => $installationId,
'providerRepositoryId' => 'randomRepositoryId'
]);
2023-10-11 10:26:15 +13:00
2023-10-14 02:20:58 +13:00
$this->assertEquals(404, $repository['headers']['status-code']);
2023-10-09 22:45:36 +13:00
}
2023-10-13 09:15:27 +13:00
/**
* @depends testGitHubAuthorize
*/
public function testListRepositoryBranches(string $installationId)
2023-10-09 22:45:36 +13:00
{
/**
* Test for SUCCESS
*/
2023-10-13 09:15:27 +13:00
$repositoryBranches = $this->client->call(Client::METHOD_GET, '/vcs/github/installations/' . $installationId . '/providerRepositories/' . $this->providerRepositoryId . '/branches', array_merge([
2023-10-09 22:45:36 +13:00
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
2023-10-13 09:15:27 +13:00
'installationId' => $installationId,
2023-10-11 09:01:22 +13:00
'providerRepositoryId' => $this->providerRepositoryId
2023-10-09 22:45:36 +13:00
]);
$this->assertEquals(200, $repositoryBranches['headers']['status-code']);
$this->assertEquals($repositoryBranches['body']['total'], 2);
$this->assertEquals($repositoryBranches['body']['branches'][0]['name'], 'main');
$this->assertEquals($repositoryBranches['body']['branches'][1]['name'], 'test');
2023-10-11 09:01:22 +13:00
/**
2023-10-11 10:26:15 +13:00
* Test for FAILURE
2023-10-11 09:01:22 +13:00
*/
2023-10-14 02:20:58 +13:00
$repositoryBranches = $this->client->call(Client::METHOD_GET, '/vcs/github/installations/' . $installationId . '/providerRepositories/randomRepositoryId/branches', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'installationId' => $installationId,
'providerRepositoryId' => 'randomRepositoryId'
]);
2023-10-11 09:01:22 +13:00
2023-10-14 02:20:58 +13:00
$this->assertEquals(404, $repositoryBranches['headers']['status-code']);
}
/**
* @depends testGitHubAuthorize
*/
public function testCreateFunctionUsingVCS(string $installationId)
{
$function = $this->client->call(Client::METHOD_POST, '/functions', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'functionId' => ID::unique(),
'name' => 'Test',
'execute' => [Role::user($this->getUser()['$id'])->toString()],
'runtime' => 'php-8.0',
'entrypoint' => 'index.php',
'events' => [
'users.*.create',
'users.*.delete',
],
'schedule' => '0 0 1 1 *',
'timeout' => 10,
'installationId' => $installationId,
'providerRepositoryId' => $this->providerRepositoryId,
'providerBranch' => 'main',
]);
$this->assertEquals(201, $function['headers']['status-code']);
return [
'installationId' => $installationId,
'functionId' => $function['body']['$id']
];
}
/**
* @depends testCreateFunctionUsingVCS
*/
public function testUpdateFunctionUsingVCS(array $data, string $providerRepositoryId2 = '700020051')
{
$function = $this->client->call(Client::METHOD_PUT, '/functions/' . $data['functionId'], array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'functionId' => ID::unique(),
'name' => 'Test',
'execute' => [Role::user($this->getUser()['$id'])->toString()],
'runtime' => 'php-8.0',
'entrypoint' => 'index.php',
'events' => [
'users.*.create',
'users.*.delete',
],
'schedule' => '0 0 1 1 *',
'timeout' => 10,
'installationId' => $data['installationId'],
'providerRepositoryId' => $providerRepositoryId2,
'providerBranch' => 'main',
]);
$this->assertEquals(200, $function['headers']['status-code']);
return $function['body']['$id'];
2023-10-11 09:01:22 +13:00
}
2023-10-09 22:45:36 +13:00
}