appwrite/tests/e2e/Scopes/ProjectCustom.php

286 lines
10 KiB
PHP
Raw Normal View History

2019-09-15 20:17:10 +00:00
<?php
2020-01-11 13:58:02 +00:00
namespace Tests\E2E\Scopes;
2019-09-15 20:17:10 +00:00
use Tests\E2E\Client;
2024-12-15 09:22:05 +00:00
use Utopia\Database\DateTime;
2023-02-05 20:07:46 +00:00
use Utopia\Database\Helpers\ID;
2025-03-19 07:32:48 +00:00
use Utopia\System\System;
2019-09-15 20:17:10 +00:00
2020-01-11 13:58:02 +00:00
trait ProjectCustom
2019-09-15 20:17:10 +00:00
{
2020-01-11 13:58:02 +00:00
/**
* @var array
*/
2020-01-11 21:53:57 +00:00
protected static $project = [];
2019-09-15 20:17:10 +00:00
2020-01-11 13:58:02 +00:00
/**
2022-06-30 04:27:11 +00:00
* @param bool $fresh
2020-01-11 13:58:02 +00:00
* @return array
*/
2022-06-30 04:27:11 +00:00
public function getProject(bool $fresh = false): array
2019-09-15 20:17:10 +00:00
{
2022-06-30 04:27:11 +00:00
if (!empty(self::$project) && !$fresh) {
2020-01-11 21:53:57 +00:00
return self::$project;
2020-01-11 13:58:02 +00:00
}
2019-09-15 20:17:10 +00:00
if ($fresh) {
return $this->createNewProject();
}
self::$project = $this->createNewProject();
return self::$project;
}
/**
* Create a new project with team, API key, dev key, webhook, and SMTP config.
*/
protected function createNewProject(): array
{
// Small delay to ensure session is fully propagated under parallel load
usleep(100000); // 100ms
$maxRetries = 5;
$team = null;
$teamId = ID::unique();
for ($i = 0; $i < $maxRetries; $i++) {
$team = $this->client->call(Client::METHOD_POST, '/teams', [
'origin' => 'http://localhost',
'content-type' => 'application/json',
'cookie' => 'a_session_console=' . $this->getRoot()['session'],
'x-appwrite-project' => 'console',
], [
'teamId' => $teamId,
'name' => 'Demo Project Team',
]);
if ($team['headers']['status-code'] === 201 || $team['headers']['status-code'] === 409) {
break;
}
if ($team['headers']['status-code'] === 401 && $i < $maxRetries - 1) {
2026-02-24 01:00:07 +00:00
\usleep(500000); // 500ms delay before retry
continue;
}
}
$this->assertContains($team['headers']['status-code'], [201, 409], 'Team creation failed with status: ' . $team['headers']['status-code']);
if ($team['headers']['status-code'] === 201) {
$this->assertEquals('Demo Project Team', $team['body']['name']);
$this->assertNotEmpty($team['body']['$id']);
$teamId = $team['body']['$id'];
}
$project = null;
for ($i = 0; $i < $maxRetries; $i++) {
$project = $this->client->call(Client::METHOD_POST, '/projects', [
'origin' => 'http://localhost',
'content-type' => 'application/json',
'cookie' => 'a_session_console=' . $this->getRoot()['session'],
'x-appwrite-project' => 'console',
], [
'projectId' => ID::unique(),
'region' => System::getEnv('_APP_REGION', 'default'),
'name' => 'Demo Project',
'teamId' => $teamId,
'description' => 'Demo Project Description',
'url' => 'https://appwrite.io',
]);
if ($project['headers']['status-code'] === 201) {
break;
}
if ($project['headers']['status-code'] === 401 && $i < $maxRetries - 1) {
2026-02-24 01:00:07 +00:00
\usleep(500000); // 500ms delay before retry
continue;
}
}
$this->assertEquals(201, $project['headers']['status-code'], 'Project creation failed with status: ' . $project['headers']['status-code']);
2019-10-21 06:01:07 +00:00
$this->assertNotEmpty($project['body']);
$key = null;
for ($i = 0; $i < $maxRetries; $i++) {
$key = $this->client->call(Client::METHOD_POST, '/projects/' . $project['body']['$id'] . '/keys', [
'origin' => 'http://localhost',
'content-type' => 'application/json',
'cookie' => 'a_session_console=' . $this->getRoot()['session'],
'x-appwrite-project' => 'console',
], [
'keyId' => ID::unique(),
'name' => 'Demo Project Key',
'scopes' => [
'users.read',
'users.write',
'teams.read',
'teams.write',
'databases.read',
'databases.write',
'collections.read',
'collections.write',
'tables.read',
'tables.write',
'documents.read',
'documents.write',
'rows.read',
'rows.write',
'files.read',
'files.write',
'buckets.read',
'buckets.write',
'sites.read',
'sites.write',
'functions.read',
'functions.write',
'sites.read',
'sites.write',
'execution.read',
'execution.write',
'log.read',
'log.write',
'locale.read',
'avatars.read',
'health.read',
'rules.read',
'rules.write',
'sessions.write',
'targets.read',
'targets.write',
'providers.read',
'providers.write',
'messages.read',
'messages.write',
'topics.write',
'topics.read',
'subscribers.write',
'subscribers.read',
'migrations.write',
'migrations.read',
'tokens.read',
'tokens.write',
2026-03-17 10:03:18 +00:00
'webhooks.read',
'webhooks.write',
2026-03-18 13:48:31 +00:00
'project.read',
2026-04-08 08:01:52 +00:00
'project.write',
'keys.read',
'keys.write',
'platforms.read',
'platforms.write',
],
]);
if ($key['headers']['status-code'] === 201) {
break;
}
if ($key['headers']['status-code'] === 401 && $i < $maxRetries - 1) {
\usleep(500000);
continue;
}
}
2019-10-21 06:01:07 +00:00
$this->assertEquals(201, $key['headers']['status-code'], 'Key creation failed with status: ' . $key['headers']['status-code']);
2019-10-21 06:01:07 +00:00
$this->assertNotEmpty($key['body']);
$this->assertNotEmpty($key['body']['secret']);
2024-12-15 10:31:55 +00:00
$devKey = $this->client->call(Client::METHOD_POST, '/projects/' . $project['body']['$id'] . '/dev-keys', [
2024-12-15 09:33:17 +00:00
'origin' => 'http://localhost',
2024-12-15 09:22:05 +00:00
'content-type' => 'application/json',
2024-12-15 09:33:17 +00:00
'cookie' => 'a_session_console=' . $this->getRoot()['session'],
'x-appwrite-project' => 'console',
2024-12-15 10:31:55 +00:00
], [
2024-12-15 09:22:05 +00:00
'name' => 'Key Test',
'expire' => DateTime::addSeconds(new \DateTime(), 3600),
]);
$this->assertEquals(201, $devKey['headers']['status-code']);
$this->assertNotEmpty($devKey['body']);
$this->assertNotEmpty($devKey['body']['secret']);
2026-03-19 10:27:13 +00:00
$webhook = $this->client->call(Client::METHOD_POST, '/webhooks', [
2020-11-20 21:03:14 +00:00
'origin' => 'http://localhost',
2020-11-20 12:35:16 +00:00
'content-type' => 'application/json',
2020-11-20 21:03:14 +00:00
'cookie' => 'a_session_console=' . $this->getRoot()['session'],
2026-03-19 10:27:13 +00:00
'x-appwrite-project' => $project['body']['$id'],
'x-appwrite-mode' => 'admin'
2020-11-20 21:03:14 +00:00
], [
2026-03-19 10:27:13 +00:00
'webhookId' => 'unique()',
2020-11-20 12:35:16 +00:00
'name' => 'Webhook Test',
'events' => [
Database layer (#3338) * database response model * database collection config * new database scopes * database service update * database execption codes * remove read write permission from database model * updating tests and fixing some bugs * server side tests are now passing * databases api * tests for database endpoint * composer update * fix error * formatting * formatting fixes * get database test * more updates to events and usage * more usage updates * fix delete type * fix test * delete database * more fixes * databaseId in attributes and indexes * more fixes * fix issues * fix index subquery * fix console scope and index query * updating tests as required * fix phpcs errors and warnings * updates to review suggestions * UI progress * ui updates and cleaning up * fix type * rework database events * update tests * update types * event generation fixed * events config updated * updating context to support multiple * realtime updates * fix ids * update context * validator updates * fix naming conflict * fix tests * fix lint errors * fix wprler and realtime tests * fix webhooks test * fix event validator and other tests * formatting fixes * removing leftover var_dumps * remove leftover comment * update usage params * usage metrics updates * update database usage * fix usage * specs update * updates to usage * fix UI and usage * fix lints * internal id fixes * fixes for internal Id * renaming services and related files * rename tests * rename doc link * rename readme * fix test name * tests: fixes for 0.15.x sync Co-authored-by: Torsten Dittmann <torsten.dittmann@googlemail.com>
2022-06-22 10:51:49 +00:00
'databases.*',
'functions.*',
2022-04-04 06:30:07 +00:00
'buckets.*',
'teams.*',
'users.*'
2020-11-20 12:35:16 +00:00
],
2025-06-26 16:07:59 +00:00
'url' => 'http://request-catcher-webhook:5000/',
2020-11-20 12:35:16 +00:00
'security' => false,
]);
$this->assertEquals(201, $webhook['headers']['status-code']);
$this->assertNotEmpty($webhook['body']);
2020-01-11 13:58:02 +00:00
2023-08-30 04:30:44 +00:00
$this->client->call(Client::METHOD_PATCH, '/projects/' . $project['body']['$id'] . '/smtp', [
'origin' => 'http://localhost',
'content-type' => 'application/json',
'cookie' => 'a_session_console=' . $this->getRoot()['session'],
'x-appwrite-project' => 'console',
], [
'enabled' => true,
'senderEmail' => 'mailer@appwrite.io',
'senderName' => 'Mailer',
'host' => 'maildev',
'port' => intval(System::getEnv('_APP_SMTP_PORT', "1025")),
'username' => System::getEnv('_APP_SMTP_USERNAME', 'user'),
'password' => System::getEnv('_APP_SMTP_PASSWORD', 'password'),
2023-08-30 04:30:44 +00:00
]);
return [
2020-02-17 07:16:11 +00:00
'$id' => $project['body']['$id'],
2020-01-11 13:58:02 +00:00
'name' => $project['body']['name'],
'apiKey' => $key['body']['secret'],
2024-12-15 09:22:05 +00:00
'devKey' => $devKey['body']['secret'],
2020-12-03 17:56:07 +00:00
'webhookId' => $webhook['body']['$id'],
2022-06-07 15:11:07 +00:00
'signatureKey' => $webhook['body']['signatureKey'],
2020-01-11 13:58:02 +00:00
];
2019-10-21 06:01:07 +00:00
}
2021-05-13 14:47:35 +00:00
2022-05-23 14:54:50 +00:00
public function getNewKey(array $scopes)
{
2021-05-13 14:47:35 +00:00
$projectId = self::$project['$id'];
$key = $this->client->call(Client::METHOD_POST, '/projects/' . $projectId . '/keys', [
'origin' => 'http://localhost',
'content-type' => 'application/json',
'cookie' => 'a_session_console=' . $this->getRoot()['session'],
'x-appwrite-project' => 'console',
], [
2026-02-09 13:34:05 +00:00
'keyId' => ID::unique(),
2021-05-13 14:47:35 +00:00
'name' => 'Demo Project Key',
'scopes' => $scopes,
]);
$this->assertEquals(201, $key['headers']['status-code']);
$this->assertNotEmpty($key['body']);
$this->assertNotEmpty($key['body']['secret']);
return $key['body']['secret'];
}
public function updateProjectinvalidateSessionsProperty(bool $value)
{
$response = $this->client->call(Client::METHOD_PATCH, '/projects/' . self::$project['$id'] . '/auth/session-invalidation', array_merge([
'origin' => 'http://localhost',
'content-type' => 'application/json',
'cookie' => 'a_session_console=' . $this->getRoot()['session'],
'x-appwrite-project' => 'console',
]), [
'enabled' => $value,
]);
return $response['headers']['status-code'];
}
}