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;
|
2023-10-13 10:41:31 +13:00
|
|
|
use Utopia\Database\Helpers\ID;
|
|
|
|
use Utopia\Database\Helpers\Role;
|
2023-10-18 20:54:32 +13:00
|
|
|
use Utopia\VCS\Adapter\Git\GitHub;
|
|
|
|
use Utopia\Cache\Adapter\None;
|
|
|
|
use Utopia\Cache\Cache;
|
2023-10-09 22:45:36 +13:00
|
|
|
|
|
|
|
class VCSConsoleClientTest extends Scope
|
|
|
|
{
|
2023-10-27 23:57:26 +13:00
|
|
|
use VCSBase;
|
2023-10-09 22:45:36 +13:00
|
|
|
use ProjectCustom;
|
|
|
|
use SideConsole;
|
|
|
|
|
2023-10-23 22:33:24 +13:00
|
|
|
public string $providerInstallationId = '42954928';
|
|
|
|
public string $providerRepositoryId = '705764267';
|
|
|
|
public string $providerRepositoryId2 = '708688544';
|
2023-10-11 09:01:22 +13:00
|
|
|
|
2023-10-28 03:08:33 +13:00
|
|
|
public function testGitHubAuthorize(): string
|
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([
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
], $this->getHeaders()), [
|
2023-10-23 22:33:24 +13:00
|
|
|
'providerInstallationId' => $this->providerInstallationId,
|
2023-10-13 09:15:27 +13:00
|
|
|
'projectId' => $this->getProject()['$id'],
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertNotEmpty($response['body']['installationId']);
|
|
|
|
$installationId = $response['body']['installationId'];
|
|
|
|
return $installationId;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testGitHubAuthorize
|
|
|
|
*/
|
2023-10-28 03:08:33 +13:00
|
|
|
public function testGetInstallation(string $installationId): void
|
2023-10-13 09:15:27 +13:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Test for SUCCESS
|
|
|
|
*/
|
|
|
|
|
|
|
|
$installation = $this->client->call(Client::METHOD_GET, '/vcs/installations/' . $installationId, array_merge([
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
2023-10-23 22:33:24 +13:00
|
|
|
], $this->getHeaders()));
|
2023-10-13 09:15:27 +13:00
|
|
|
|
|
|
|
$this->assertEquals(200, $installation['headers']['status-code']);
|
2023-10-23 22:33:24 +13:00
|
|
|
$this->assertEquals('github', $installation['body']['provider']);
|
|
|
|
$this->assertEquals('appwrite-test', $installation['body']['organization']);
|
2023-10-13 09:15:27 +13:00
|
|
|
}
|
2023-10-09 22:45:36 +13:00
|
|
|
|
2023-10-13 09:15:27 +13:00
|
|
|
/**
|
|
|
|
* @depends testGitHubAuthorize
|
|
|
|
*/
|
2023-10-28 03:08:33 +13:00
|
|
|
public function testDetectRuntime(string $installationId): void
|
2023-10-13 09:15:27 +13:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
2023-10-23 22:33:24 +13:00
|
|
|
], $this->getHeaders()));
|
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([
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
2023-10-23 22:33:24 +13:00
|
|
|
], $this->getHeaders()));
|
2023-10-14 02:20:58 +13:00
|
|
|
|
|
|
|
$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
|
|
|
|
*/
|
2023-10-28 03:08:33 +13:00
|
|
|
public function testListRepositories(string $installationId): void
|
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
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
2023-10-23 22:33:24 +13:00
|
|
|
], $this->getHeaders()));
|
2023-10-09 22:45:36 +13:00
|
|
|
|
|
|
|
$this->assertEquals(200, $repositories['headers']['status-code']);
|
|
|
|
$this->assertEquals($repositories['body']['total'], 3);
|
2023-10-23 22:33:24 +13:00
|
|
|
$this->assertEquals($repositories['body']['providerRepositories'][0]['name'], 'function1.4');
|
|
|
|
$this->assertEquals($repositories['body']['providerRepositories'][0]['organization'], 'appwrite-test');
|
|
|
|
$this->assertEquals($repositories['body']['providerRepositories'][0]['provider'], 'github');
|
|
|
|
$this->assertEquals($repositories['body']['providerRepositories'][1]['name'], 'appwrite');
|
|
|
|
$this->assertEquals($repositories['body']['providerRepositories'][2]['name'], 'ruby-starter');
|
|
|
|
|
2023-10-09 22:45:36 +13:00
|
|
|
|
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
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
], $this->getHeaders()), [
|
|
|
|
'search' => 'func'
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertEquals(200, $searchedRepositories['headers']['status-code']);
|
|
|
|
$this->assertEquals($searchedRepositories['body']['total'], 1);
|
2023-10-23 22:33:24 +13:00
|
|
|
$this->assertEquals($searchedRepositories['body']['providerRepositories'][0]['name'], 'function1.4');
|
2023-10-11 10:26:15 +13:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test for FAILURE
|
|
|
|
*/
|
|
|
|
|
2023-10-13 10:41:31 +13:00
|
|
|
$repositories = $this->client->call(Client::METHOD_GET, '/vcs/github/installations/randomInstallationId/providerRepositories', array_merge([
|
2023-10-11 10:26:15 +13:00
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
2023-10-23 22:33:24 +13:00
|
|
|
], $this->getHeaders()));
|
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
|
|
|
|
*/
|
2023-10-28 03:08:33 +13:00
|
|
|
public function testGetRepository(string $installationId): void
|
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
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
2023-10-23 22:33:24 +13:00
|
|
|
], $this->getHeaders()));
|
2023-10-09 22:45:36 +13:00
|
|
|
|
|
|
|
$this->assertEquals(200, $repository['headers']['status-code']);
|
|
|
|
$this->assertEquals($repository['body']['name'], 'ruby-starter');
|
2023-10-23 22:33:24 +13:00
|
|
|
$this->assertEquals($repository['body']['organization'], 'appwrite-test');
|
|
|
|
$this->assertEquals($repository['body']['private'], false);
|
2023-10-09 22:45:36 +13:00
|
|
|
|
2023-10-23 22:33:24 +13:00
|
|
|
$repository = $this->client->call(Client::METHOD_GET, '/vcs/github/installations/' . $installationId . '/providerRepositories/' . $this->providerRepositoryId2, array_merge([
|
2023-10-09 22:45:36 +13:00
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
2023-10-23 22:33:24 +13:00
|
|
|
], $this->getHeaders()));
|
2023-10-09 22:45:36 +13:00
|
|
|
|
|
|
|
$this->assertEquals(200, $repository['headers']['status-code']);
|
2023-10-23 22:33:24 +13:00
|
|
|
$this->assertEquals($repository['body']['name'], 'function1.4');
|
|
|
|
$this->assertEquals($repository['body']['organization'], 'appwrite-test');
|
|
|
|
$this->assertEquals($repository['body']['private'], true);
|
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([
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
2023-10-23 22:33:24 +13:00
|
|
|
], $this->getHeaders()));
|
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
|
|
|
|
*/
|
2023-10-28 03:08:33 +13:00
|
|
|
public function testListRepositoryBranches(string $installationId): void
|
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
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
2023-10-23 22:33:24 +13:00
|
|
|
], $this->getHeaders()));
|
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([
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
2023-10-23 22:33:24 +13:00
|
|
|
], $this->getHeaders()));
|
2023-10-11 09:01:22 +13:00
|
|
|
|
2023-10-14 02:20:58 +13:00
|
|
|
$this->assertEquals(404, $repositoryBranches['headers']['status-code']);
|
2023-10-13 10:41:31 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testGitHubAuthorize
|
|
|
|
*/
|
2023-10-28 03:08:33 +13:00
|
|
|
public function testCreateFunctionUsingVCS(string $installationId): array
|
2023-10-13 10:41:31 +13:00
|
|
|
{
|
|
|
|
$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']);
|
2023-10-23 22:33:24 +13:00
|
|
|
$this->assertEquals('Test', $function['body']['name']);
|
|
|
|
$this->assertEquals('php-8.0', $function['body']['runtime']);
|
|
|
|
$this->assertEquals('index.php', $function['body']['entrypoint']);
|
|
|
|
$this->assertEquals('705764267', $function['body']['providerRepositoryId']);
|
|
|
|
$this->assertEquals('main', $function['body']['providerBranch']);
|
2023-10-13 10:41:31 +13:00
|
|
|
|
|
|
|
return [
|
|
|
|
'installationId' => $installationId,
|
|
|
|
'functionId' => $function['body']['$id']
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testCreateFunctionUsingVCS
|
|
|
|
*/
|
2023-10-28 03:08:33 +13:00
|
|
|
public function testUpdateFunctionUsingVCS(array $data): string
|
2023-10-13 10:41:31 +13:00
|
|
|
{
|
|
|
|
$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'],
|
2023-10-23 22:33:24 +13:00
|
|
|
'providerRepositoryId' => $this->providerRepositoryId2,
|
2023-10-13 10:41:31 +13:00
|
|
|
'providerBranch' => 'main',
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertEquals(200, $function['headers']['status-code']);
|
2023-10-23 22:33:24 +13:00
|
|
|
$this->assertEquals('Test', $function['body']['name']);
|
|
|
|
$this->assertEquals('php-8.0', $function['body']['runtime']);
|
|
|
|
$this->assertEquals('index.php', $function['body']['entrypoint']);
|
|
|
|
$this->assertEquals('708688544', $function['body']['providerRepositoryId']);
|
|
|
|
$this->assertEquals('main', $function['body']['providerBranch']);
|
2023-10-13 10:41:31 +13:00
|
|
|
|
|
|
|
return $function['body']['$id'];
|
2023-10-11 09:01:22 +13:00
|
|
|
}
|
2023-10-18 20:54:32 +13:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testGitHubAuthorize
|
|
|
|
*/
|
2023-10-28 03:08:33 +13:00
|
|
|
public function testCreateRepository(string $installationId): void
|
2023-10-18 20:54:32 +13:00
|
|
|
{
|
2023-10-23 22:33:24 +13:00
|
|
|
/**
|
|
|
|
* Test for SUCCESS
|
|
|
|
*/
|
|
|
|
|
2023-10-18 20:54:32 +13:00
|
|
|
$github = new GitHub(new Cache(new None()));
|
|
|
|
$privateKey = App::getEnv('_APP_VCS_GITHUB_PRIVATE_KEY');
|
|
|
|
$githubAppId = App::getEnv('_APP_VCS_GITHUB_APP_ID');
|
2023-10-23 22:33:24 +13:00
|
|
|
$github->initializeVariables($this->providerInstallationId, $privateKey, $githubAppId);
|
2023-10-18 20:54:32 +13:00
|
|
|
|
|
|
|
$repository = $this->client->call(Client::METHOD_POST, '/vcs/github/installations/' . $installationId . '/providerRepositories', array_merge([
|
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
], $this->getHeaders()), [
|
|
|
|
'name' => 'test-repo-1',
|
|
|
|
'private' => true
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertEquals('test-repo-1', $repository['body']['name']);
|
2023-10-23 22:33:24 +13:00
|
|
|
$this->assertEquals('appwrite-test', $repository['body']['organization']);
|
|
|
|
$this->assertEquals('github', $repository['body']['provider']);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test for FAILURE
|
|
|
|
*/
|
|
|
|
|
|
|
|
$repository = $this->client->call(Client::METHOD_POST, '/vcs/github/installations/' . $installationId . '/providerRepositories', array_merge([
|
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
], $this->getHeaders()), [
|
|
|
|
'name' => 'test-repo-1',
|
|
|
|
'private' => true
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertEquals(400, $repository['headers']['status-code']);
|
|
|
|
$this->assertEquals('Provider Error: Repository creation failed. name already exists on this account', $repository['body']['message']);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test for SUCCESS
|
|
|
|
*/
|
2023-10-18 20:54:32 +13:00
|
|
|
|
|
|
|
$result = $github->deleteRepository('appwrite-test', 'test-repo-1');
|
2023-10-23 22:33:24 +13:00
|
|
|
$this->assertEquals($result, true);
|
2023-10-18 20:54:32 +13:00
|
|
|
}
|
2023-10-09 22:45:36 +13:00
|
|
|
}
|