1
0
Fork 0
mirror of synced 2024-06-01 18:39:57 +12:00

Refactor code and add more assertions

This commit is contained in:
Khushboo Verma 2023-10-23 15:03:24 +05:30
parent eac911a006
commit 97c1099961
4 changed files with 95 additions and 98 deletions

1
.env
View file

@ -96,7 +96,6 @@ _APP_VCS_GITHUB_APP_ID=
_APP_VCS_GITHUB_CLIENT_ID=
_APP_VCS_GITHUB_CLIENT_SECRET=
_APP_VCS_GITHUB_WEBHOOK_SECRET=
_APP_VCS_TEST_GITHUB_INSTALLATION_ID=
_APP_MIGRATIONS_FIREBASE_CLIENT_ID=
_APP_MIGRATIONS_FIREBASE_CLIENT_SECRET=
_APP_ASSISTANT_OPENAI_API_KEY=

View file

@ -251,8 +251,7 @@ App::get('/v1/mock/tests/general/download')
->addHeader('Content-Disposition', 'attachment; filename="test.txt"')
->addHeader('Expires', \date('D, d M Y H:i:s', \time() + (60 * 60 * 24 * 45)) . ' GMT') // 45 days cache
->addHeader('X-Peak', \memory_get_peak_usage())
->send("GET:/v1/mock/tests/general/download:passed")
;
->send("GET:/v1/mock/tests/general/download:passed");
});
App::post('/v1/mock/tests/general/upload')
@ -334,7 +333,7 @@ App::post('/v1/mock/tests/general/upload')
}
if ($file['size'] !== 38756) {
throw new Exception(Exception::GENERAL_MOCK, 'Wrong file size');
throw new Exception(Exception::GENERAL_MOCK, 'Wrong file size');
}
if (\md5(\file_get_contents($file['tmp_name'])) !== 'd80e7e6999a3eb2ae0d631a96fe135a4') {
@ -509,8 +508,7 @@ App::get('/v1/mock/tests/general/502-error')
$response
->setStatusCode(502)
->text('This is a text error')
;
->text('This is a text error');
});
App::get('/v1/mock/tests/general/oauth2')
@ -653,16 +651,18 @@ App::get('/v1/mock/github/callback')
->desc('Create installation document using GitHub installation id')
->groups(['mock', 'api', 'vcs'])
->label('scope', 'public')
->param('installation_id', '', new Text(256, 0), 'GitHub installation ID', true)
->param('projectId', '', new Text(2048), 'Project ID of the project where app is to be installed', true)
->label('docs', false)
->param('providerInstallationId', '', new UID(), 'GitHub installation ID')
->param('projectId', '', new UID(), 'Project ID of the project where app is to be installed')
->inject('gitHub')
->inject('project')
->inject('response')
->inject('dbForConsole')
->action(function (string $providerInstallationId, string $projectId, GitHub $github, Document $project, Response $response, Database $dbForConsole) {
if (empty($projectId)) {
$error = 'Installation requests from organisation members for the Appwrite GitHub App are currently unsupported. To proceed with the installation, login to the Appwrite Console and install the GitHub App.';
throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, $error);
$isDevelopment = App::getEnv('_APP_ENV', 'development') === 'development';
if (!$isDevelopment) {
throw new Exception(Exception::GENERAL_NOT_IMPLEMENTED);
}
$project = $dbForConsole->getDocument('projects', $projectId);
@ -672,7 +672,6 @@ App::get('/v1/mock/github/callback')
throw new Exception(Exception::PROJECT_NOT_FOUND, $error);
}
// Create / Update installation
if (!empty($providerInstallationId)) {
$privateKey = App::getEnv('_APP_VCS_GITHUB_PRIVATE_KEY');
$githubAppId = App::getEnv('_APP_VCS_GITHUB_APP_ID');
@ -701,10 +700,6 @@ App::get('/v1/mock/github/callback')
]);
$installation = $dbForConsole->createDocument('installations', $installation);
} else {
$error = 'Installation of the Appwrite GitHub App on organization accounts is restricted to organization owners. As a member of the organization, you do not have the necessary permissions to install this GitHub App. Please contact the organization owner to create the installation from the Appwrite console.';
throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, $error);
}
$response->json([

31
composer.lock generated
View file

@ -156,11 +156,11 @@
},
{
"name": "appwrite/php-runtimes",
"version": "0.13.0",
"version": "0.13.1",
"source": {
"type": "git",
"url": "https://github.com/appwrite/runtimes.git",
"reference": "5ab496b3908992b39275994a23783701c4b3de84"
"reference": "b584d19cdcd82737d0ee5c34d23de791f5ed3610"
},
"require": {
"php": ">=8.0",
@ -195,7 +195,7 @@
"php",
"runtimes"
],
"time": "2023-09-12T19:38:43+00:00"
"time": "2023-10-16T15:39:53+00:00"
},
{
"name": "chillerlan/php-qrcode",
@ -2463,22 +2463,23 @@
},
{
"name": "utopia-php/logger",
"version": "0.3.1",
"version": "0.3.2",
"source": {
"type": "git",
"url": "https://github.com/utopia-php/logger.git",
"reference": "de623f1ec1c672c795d113dd25c5bf212f7ef4fc"
"reference": "9151b7d16eab18d4c37c34643041cc0f33ca4a6c"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/utopia-php/logger/zipball/de623f1ec1c672c795d113dd25c5bf212f7ef4fc",
"reference": "de623f1ec1c672c795d113dd25c5bf212f7ef4fc",
"url": "https://api.github.com/repos/utopia-php/logger/zipball/9151b7d16eab18d4c37c34643041cc0f33ca4a6c",
"reference": "9151b7d16eab18d4c37c34643041cc0f33ca4a6c",
"shasum": ""
},
"require": {
"php": ">=8.0"
},
"require-dev": {
"laravel/pint": "1.2.*",
"phpstan/phpstan": "1.9.x-dev",
"phpunit/phpunit": "^9.3",
"vimeo/psalm": "4.0.1"
@ -2510,9 +2511,9 @@
],
"support": {
"issues": "https://github.com/utopia-php/logger/issues",
"source": "https://github.com/utopia-php/logger/tree/0.3.1"
"source": "https://github.com/utopia-php/logger/tree/0.3.2"
},
"time": "2023-02-10T15:52:50+00:00"
"time": "2023-10-16T08:16:19+00:00"
},
{
"name": "utopia-php/messaging",
@ -3151,16 +3152,16 @@
},
{
"name": "utopia-php/vcs",
"version": "0.6.0",
"version": "0.6.1",
"source": {
"type": "git",
"url": "https://github.com/utopia-php/vcs.git",
"reference": "be866170c42912f0d371421346a69f4f575eb116"
"reference": "d161d1156ef336d197a8d45384b531e5ec31243d"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/utopia-php/vcs/zipball/be866170c42912f0d371421346a69f4f575eb116",
"reference": "be866170c42912f0d371421346a69f4f575eb116",
"url": "https://api.github.com/repos/utopia-php/vcs/zipball/d161d1156ef336d197a8d45384b531e5ec31243d",
"reference": "d161d1156ef336d197a8d45384b531e5ec31243d",
"shasum": ""
},
"require": {
@ -3194,9 +3195,9 @@
],
"support": {
"issues": "https://github.com/utopia-php/vcs/issues",
"source": "https://github.com/utopia-php/vcs/tree/0.6.0"
"source": "https://github.com/utopia-php/vcs/tree/0.6.1"
},
"time": "2023-10-13T13:07:13+00:00"
"time": "2023-10-19T07:43:31+00:00"
},
{
"name": "utopia-php/websocket",

View file

@ -18,13 +18,9 @@ class VCSConsoleClientTest extends Scope
use ProjectCustom;
use SideConsole;
public ?string $providerRepositoryId = null;
protected function setUp(): void
{
parent::setUp();
$this->providerRepositoryId = '705764267';
}
public string $providerInstallationId = '42954928';
public string $providerRepositoryId = '705764267';
public string $providerRepositoryId2 = '708688544';
public function testGitHubAuthorize()
{
@ -32,10 +28,9 @@ class VCSConsoleClientTest extends Scope
* Test for SUCCESS
*/
$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'),
'providerInstallationId' => $this->providerInstallationId,
'projectId' => $this->getProject()['$id'],
]);
@ -54,13 +49,12 @@ class VCSConsoleClientTest extends Scope
*/
$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->getHeaders()));
$this->assertEquals(200, $installation['headers']['status-code']);
$this->assertEquals('github', $installation['body']['provider']);
$this->assertEquals('appwrite-test', $installation['body']['organization']);
}
/**
@ -73,12 +67,8 @@ class VCSConsoleClientTest extends Scope
*/
$runtime = $this->client->call(Client::METHOD_POST, '/vcs/github/installations/' . $installationId . '/providerRepositories/' . $this->providerRepositoryId . '/detection', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'installationId' => $installationId,
'providerRepositoryId' => $this->providerRepositoryId
]);
], $this->getHeaders()));
$this->assertEquals(200, $runtime['headers']['status-code']);
$this->assertEquals($runtime['body']['runtime'], 'ruby-3.1');
@ -88,12 +78,8 @@ class VCSConsoleClientTest extends Scope
*/
$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->getHeaders()));
$this->assertEquals(404, $runtime['headers']['status-code']);
}
@ -108,36 +94,35 @@ class VCSConsoleClientTest extends Scope
*/
$repositories = $this->client->call(Client::METHOD_GET, '/vcs/github/installations/' . $installationId . '/providerRepositories', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'installationId' => $installationId
]);
], $this->getHeaders()));
$this->assertEquals(200, $repositories['headers']['status-code']);
$this->assertEquals($repositories['body']['total'], 3);
$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');
$searchedRepositories = $this->client->call(Client::METHOD_GET, '/vcs/github/installations/' . $installationId . '/providerRepositories', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'installationId' => $installationId,
'search' => 'func'
]);
$this->assertEquals(200, $searchedRepositories['headers']['status-code']);
$this->assertEquals($searchedRepositories['body']['total'], 1);
$this->assertEquals($searchedRepositories['body']['providerRepositories'][0]['name'], 'function1.4');
/**
* Test for FAILURE
*/
$repositories = $this->client->call(Client::METHOD_GET, '/vcs/github/installations/randomInstallationId/providerRepositories', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'installationId' => 'randomInstallationId'
]);
], $this->getHeaders()));
$this->assertEquals(404, $repositories['headers']['status-code']);
}
@ -145,45 +130,37 @@ class VCSConsoleClientTest extends Scope
/**
* @depends testGitHubAuthorize
*/
public function testGetRepository(string $installationId, string $providerRepositoryId2 = '705764449')
public function testGetRepository(string $installationId)
{
/**
* Test for SUCCESS
*/
$repository = $this->client->call(Client::METHOD_GET, '/vcs/github/installations/' . $installationId . '/providerRepositories/' . $this->providerRepositoryId, array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'installationId' => $installationId,
'providerRepositoryId' => $this->providerRepositoryId
]);
], $this->getHeaders()));
$this->assertEquals(200, $repository['headers']['status-code']);
$this->assertEquals($repository['body']['name'], 'ruby-starter');
$this->assertEquals($repository['body']['organization'], 'appwrite-test');
$this->assertEquals($repository['body']['private'], false);
$repository = $this->client->call(Client::METHOD_GET, '/vcs/github/installations/' . $installationId . '/providerRepositories/' . $providerRepositoryId2, array_merge([
'content-type' => 'application/json',
$repository = $this->client->call(Client::METHOD_GET, '/vcs/github/installations/' . $installationId . '/providerRepositories/' . $this->providerRepositoryId2, array_merge([
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'installationId' => $installationId,
'providerRepositoryId' => $providerRepositoryId2
]);
], $this->getHeaders()));
$this->assertEquals(200, $repository['headers']['status-code']);
$this->assertEquals($repository['body']['name'], 'function-1.4');
$this->assertEquals($repository['body']['name'], 'function1.4');
$this->assertEquals($repository['body']['organization'], 'appwrite-test');
$this->assertEquals($repository['body']['private'], true);
/**
* Test for FAILURE
*/
$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'
]);
], $this->getHeaders()));
$this->assertEquals(404, $repository['headers']['status-code']);
}
@ -198,12 +175,8 @@ class VCSConsoleClientTest extends Scope
*/
$repositoryBranches = $this->client->call(Client::METHOD_GET, '/vcs/github/installations/' . $installationId . '/providerRepositories/' . $this->providerRepositoryId . '/branches', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'installationId' => $installationId,
'providerRepositoryId' => $this->providerRepositoryId
]);
], $this->getHeaders()));
$this->assertEquals(200, $repositoryBranches['headers']['status-code']);
$this->assertEquals($repositoryBranches['body']['total'], 2);
@ -215,12 +188,8 @@ class VCSConsoleClientTest extends Scope
*/
$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'
]);
], $this->getHeaders()));
$this->assertEquals(404, $repositoryBranches['headers']['status-code']);
}
@ -251,6 +220,11 @@ class VCSConsoleClientTest extends Scope
]);
$this->assertEquals(201, $function['headers']['status-code']);
$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']);
return [
'installationId' => $installationId,
@ -261,7 +235,7 @@ class VCSConsoleClientTest extends Scope
/**
* @depends testCreateFunctionUsingVCS
*/
public function testUpdateFunctionUsingVCS(array $data, string $providerRepositoryId2 = '705764449')
public function testUpdateFunctionUsingVCS(array $data)
{
$function = $this->client->call(Client::METHOD_PUT, '/functions/' . $data['functionId'], array_merge([
'content-type' => 'application/json',
@ -279,11 +253,16 @@ class VCSConsoleClientTest extends Scope
'schedule' => '0 0 1 1 *',
'timeout' => 10,
'installationId' => $data['installationId'],
'providerRepositoryId' => $providerRepositoryId2,
'providerRepositoryId' => $this->providerRepositoryId2,
'providerBranch' => 'main',
]);
$this->assertEquals(200, $function['headers']['status-code']);
$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']);
return $function['body']['$id'];
}
@ -293,24 +272,47 @@ class VCSConsoleClientTest extends Scope
*/
public function testCreateRepository(string $installationId)
{
/**
* Test for SUCCESS
*/
$github = new GitHub(new Cache(new None()));
$githubInstallationId = App::getEnv('_APP_VCS_TEST_GITHUB_INSTALLATION_ID');
$privateKey = App::getEnv('_APP_VCS_GITHUB_PRIVATE_KEY');
$githubAppId = App::getEnv('_APP_VCS_GITHUB_APP_ID');
$github->initializeVariables($githubInstallationId, $privateKey, $githubAppId);
$github->initializeVariables($this->providerInstallationId, $privateKey, $githubAppId);
$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()), [
'installationId' => $installationId,
'name' => 'test-repo-1',
'private' => true
]);
$this->assertEquals('test-repo-1', $repository['body']['name']);
$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
*/
$result = $github->deleteRepository('appwrite-test', 'test-repo-1');
// $this->assertEquals($result, true);
$this->assertEquals($result, true);
}
}