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
|
|
|
|
2026-02-20 12:47:20 +00:00
|
|
|
if ($fresh) {
|
|
|
|
|
return $this->createNewProject();
|
|
|
|
|
}
|
|
|
|
|
|
fix: scope file caching to Database tests only, revert for non-Database tests
The previous file caching approach cached getRoot(), getUser(), getProject(),
and getConsoleVariables() globally. This caused all test methods in a class
to share the same project, breaking non-Database tests that expect isolated
state (Account 401s, Storage 500s, Users 404s, etc.).
Now file caching is only applied in Database/Transaction test setup chains:
- ensureSharedProject() in DatabasesBase, TransactionsBase, TransactionPermissionsBase
creates and file-caches both the project AND user so all methods share
consistent project + user state (needed for collection permissions)
- Non-Database tests (Account, Storage, Users, etc.) create their own
isolated projects per-process as before
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-20 13:46:56 +00:00
|
|
|
self::$project = $this->createNewProject();
|
2026-02-20 12:47:20 +00:00
|
|
|
|
|
|
|
|
return self::$project;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create a new project with team, API key, dev key, webhook, and SMTP config.
|
|
|
|
|
*/
|
|
|
|
|
protected function createNewProject(): array
|
|
|
|
|
{
|
2026-02-13 07:05:49 +00:00
|
|
|
// Small delay to ensure session is fully propagated under parallel load
|
|
|
|
|
usleep(100000); // 100ms
|
|
|
|
|
|
2026-02-13 08:46:24 +00:00
|
|
|
$maxRetries = 5;
|
2026-02-13 07:05:49 +00:00
|
|
|
$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
|
2026-02-13 07:05:49 +00:00
|
|
|
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
|
2026-02-13 07:05:49 +00:00
|
|
|
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']);
|
|
|
|
|
|
2026-03-05 14:01:23 +00:00
|
|
|
$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-05 14:01:23 +00:00
|
|
|
],
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
2026-03-05 14:01:23 +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']);
|
|
|
|
|
|
2022-05-23 14:54:50 +00:00
|
|
|
$webhook = $this->client->call(Client::METHOD_POST, '/projects/' . $project['body']['$id'] . '/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'],
|
|
|
|
|
'x-appwrite-project' => 'console',
|
|
|
|
|
], [
|
2020-11-20 12:35:16 +00:00
|
|
|
'name' => 'Webhook Test',
|
|
|
|
|
'events' => [
|
2022-06-22 10:51:49 +00:00
|
|
|
'databases.*',
|
2026-02-24 02:15:59 +00:00
|
|
|
'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',
|
2025-12-12 06:02:17 +00:00
|
|
|
'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
|
|
|
]);
|
|
|
|
|
|
2026-02-20 12:47:20 +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'];
|
|
|
|
|
}
|
2025-06-16 17:35:52 +00:00
|
|
|
public function updateProjectinvalidateSessionsProperty(bool $value)
|
2025-06-14 12:37:42 +00:00
|
|
|
{
|
2025-06-16 17:35:52 +00:00
|
|
|
$response = $this->client->call(Client::METHOD_PATCH, '/projects/' . self::$project['$id'] . '/auth/session-invalidation', array_merge([
|
2025-06-14 12:37:42 +00:00
|
|
|
'origin' => 'http://localhost',
|
|
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'cookie' => 'a_session_console=' . $this->getRoot()['session'],
|
|
|
|
|
'x-appwrite-project' => 'console',
|
|
|
|
|
]), [
|
2025-06-16 19:10:32 +00:00
|
|
|
'enabled' => $value,
|
2025-06-14 12:37:42 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
return $response['headers']['status-code'];
|
|
|
|
|
}
|
2019-09-30 06:13:40 +00:00
|
|
|
}
|