2019-09-16 08:17:10 +12:00
|
|
|
<?php
|
|
|
|
|
2020-01-12 02:58:02 +13:00
|
|
|
namespace Tests\E2E\Scopes;
|
2019-09-16 08:17:10 +12:00
|
|
|
|
|
|
|
use Tests\E2E\Client;
|
2023-02-06 09:07:46 +13:00
|
|
|
use Utopia\Database\Helpers\ID;
|
2019-09-16 08:17:10 +12:00
|
|
|
|
2020-01-12 02:58:02 +13:00
|
|
|
trait ProjectCustom
|
2019-09-16 08:17:10 +12:00
|
|
|
{
|
2020-01-12 02:58:02 +13:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2020-01-12 10:53:57 +13:00
|
|
|
protected static $project = [];
|
2019-09-16 08:17:10 +12:00
|
|
|
|
2020-01-12 02:58:02 +13:00
|
|
|
/**
|
2023-08-24 05:12:43 +12:00
|
|
|
* @param bool $fresh
|
2020-01-12 02:58:02 +13:00
|
|
|
* @return array
|
|
|
|
*/
|
2022-06-30 16:27:11 +12:00
|
|
|
public function getProject(bool $fresh = false): array
|
2019-09-16 08:17:10 +12:00
|
|
|
{
|
2023-08-24 05:12:43 +12:00
|
|
|
if (! empty(self::$project) && ! $fresh) {
|
2020-01-12 10:53:57 +13:00
|
|
|
return self::$project;
|
2020-01-12 02:58:02 +13:00
|
|
|
}
|
2019-09-16 08:17:10 +12:00
|
|
|
|
2019-10-21 19:01:07 +13:00
|
|
|
$team = $this->client->call(Client::METHOD_POST, '/teams', [
|
|
|
|
'origin' => 'http://localhost',
|
|
|
|
'content-type' => 'application/json',
|
2023-08-24 05:12:43 +12:00
|
|
|
'cookie' => 'a_session_console='.$this->getRoot()['session'],
|
2019-11-30 07:26:06 +13:00
|
|
|
'x-appwrite-project' => 'console',
|
2019-10-21 19:01:07 +13:00
|
|
|
], [
|
2022-08-14 22:33:36 +12:00
|
|
|
'teamId' => ID::unique(),
|
2019-10-21 19:01:07 +13:00
|
|
|
'name' => 'Demo Project Team',
|
|
|
|
]);
|
|
|
|
$this->assertEquals(201, $team['headers']['status-code']);
|
|
|
|
$this->assertEquals('Demo Project Team', $team['body']['name']);
|
2020-02-17 20:16:11 +13:00
|
|
|
$this->assertNotEmpty($team['body']['$id']);
|
2019-10-21 19:01:07 +13:00
|
|
|
|
|
|
|
$project = $this->client->call(Client::METHOD_POST, '/projects', [
|
|
|
|
'origin' => 'http://localhost',
|
|
|
|
'content-type' => 'application/json',
|
2023-08-24 05:12:43 +12:00
|
|
|
'cookie' => 'a_session_console='.$this->getRoot()['session'],
|
2019-11-30 07:26:06 +13:00
|
|
|
'x-appwrite-project' => 'console',
|
2019-10-21 19:01:07 +13:00
|
|
|
], [
|
2022-08-14 22:33:36 +12:00
|
|
|
'projectId' => ID::unique(),
|
2022-10-01 01:22:11 +13:00
|
|
|
'region' => 'default',
|
2019-10-21 19:01:07 +13:00
|
|
|
'name' => 'Demo Project',
|
2020-02-17 20:16:11 +13:00
|
|
|
'teamId' => $team['body']['$id'],
|
2019-10-21 19:01:07 +13:00
|
|
|
'description' => 'Demo Project Description',
|
|
|
|
'url' => 'https://appwrite.io',
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertEquals(201, $project['headers']['status-code']);
|
|
|
|
$this->assertNotEmpty($project['body']);
|
|
|
|
|
2023-08-24 05:12:43 +12:00
|
|
|
$key = $this->client->call(Client::METHOD_POST, '/projects/'.$project['body']['$id'].'/keys', [
|
2019-10-21 19:01:07 +13:00
|
|
|
'origin' => 'http://localhost',
|
|
|
|
'content-type' => 'application/json',
|
2023-08-24 05:12:43 +12:00
|
|
|
'cookie' => 'a_session_console='.$this->getRoot()['session'],
|
2019-11-30 07:26:06 +13:00
|
|
|
'x-appwrite-project' => 'console',
|
2019-10-21 19:01:07 +13:00
|
|
|
], [
|
|
|
|
'name' => 'Demo Project Key',
|
2020-01-12 02:58:02 +13:00
|
|
|
'scopes' => [
|
2020-01-13 00:46:39 +13:00
|
|
|
'users.read',
|
|
|
|
'users.write',
|
|
|
|
'teams.read',
|
|
|
|
'teams.write',
|
2022-06-22 22:51:49 +12:00
|
|
|
'databases.read',
|
|
|
|
'databases.write',
|
2020-01-13 00:46:39 +13:00
|
|
|
'collections.read',
|
|
|
|
'collections.write',
|
|
|
|
'documents.read',
|
|
|
|
'documents.write',
|
2020-01-12 10:53:57 +13:00
|
|
|
'files.read',
|
2020-01-13 00:46:39 +13:00
|
|
|
'files.write',
|
2021-06-16 18:07:35 +12:00
|
|
|
'buckets.read',
|
2021-06-15 18:38:05 +12:00
|
|
|
'buckets.write',
|
2020-05-06 08:37:59 +12:00
|
|
|
'functions.read',
|
|
|
|
'functions.write',
|
2020-12-30 20:26:01 +13:00
|
|
|
'execution.read',
|
|
|
|
'execution.write',
|
2020-01-13 00:46:39 +13:00
|
|
|
'locale.read',
|
|
|
|
'avatars.read',
|
2020-06-07 18:01:35 +12:00
|
|
|
'health.read',
|
2023-08-23 22:01:29 +12:00
|
|
|
'providers.read',
|
|
|
|
'providers.write',
|
|
|
|
'messages.read',
|
|
|
|
'messages.write',
|
2020-01-12 02:58:02 +13:00
|
|
|
],
|
2019-10-21 19:01:07 +13:00
|
|
|
]);
|
|
|
|
|
2020-11-21 01:35:16 +13:00
|
|
|
$this->assertEquals(201, $key['headers']['status-code']);
|
2019-10-21 19:01:07 +13:00
|
|
|
$this->assertNotEmpty($key['body']);
|
|
|
|
$this->assertNotEmpty($key['body']['secret']);
|
|
|
|
|
2023-08-24 05:12:43 +12:00
|
|
|
$webhook = $this->client->call(Client::METHOD_POST, '/projects/'.$project['body']['$id'].'/webhooks', [
|
2020-11-21 10:03:14 +13:00
|
|
|
'origin' => 'http://localhost',
|
2020-11-21 01:35:16 +13:00
|
|
|
'content-type' => 'application/json',
|
2023-08-24 05:12:43 +12:00
|
|
|
'cookie' => 'a_session_console='.$this->getRoot()['session'],
|
2020-11-21 10:03:14 +13:00
|
|
|
'x-appwrite-project' => 'console',
|
|
|
|
], [
|
2020-11-21 01:35:16 +13:00
|
|
|
'name' => 'Webhook Test',
|
|
|
|
'events' => [
|
2022-06-22 22:51:49 +12:00
|
|
|
'databases.*',
|
2023-06-15 23:06:04 +12:00
|
|
|
// 'functions.*', TODO @christyjacob4 : enable test once we allow functions.* events
|
2022-04-04 18:30:07 +12:00
|
|
|
'buckets.*',
|
|
|
|
'teams.*',
|
2023-08-24 05:12:43 +12:00
|
|
|
'users.*',
|
2020-11-21 01:35:16 +13:00
|
|
|
],
|
|
|
|
'url' => 'http://request-catcher:5000/webhook',
|
|
|
|
'security' => false,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertEquals(201, $webhook['headers']['status-code']);
|
|
|
|
$this->assertNotEmpty($webhook['body']);
|
2020-01-12 02:58:02 +13:00
|
|
|
|
2022-06-30 16:27:11 +12:00
|
|
|
$project = [
|
2020-02-17 20:16:11 +13:00
|
|
|
'$id' => $project['body']['$id'],
|
2020-01-12 02:58:02 +13:00
|
|
|
'name' => $project['body']['name'],
|
|
|
|
'apiKey' => $key['body']['secret'],
|
2020-12-04 06:56:07 +13:00
|
|
|
'webhookId' => $webhook['body']['$id'],
|
2022-06-08 03:11:07 +12:00
|
|
|
'signatureKey' => $webhook['body']['signatureKey'],
|
2020-01-12 02:58:02 +13:00
|
|
|
];
|
2022-07-03 13:09:11 +12:00
|
|
|
if ($fresh) {
|
2022-06-30 16:27:11 +12:00
|
|
|
return $project;
|
|
|
|
}
|
|
|
|
self::$project = $project;
|
2019-11-04 08:05:26 +13:00
|
|
|
|
2020-01-12 10:53:57 +13:00
|
|
|
return self::$project;
|
2019-10-21 19:01:07 +13:00
|
|
|
}
|
2021-05-14 02:47:35 +12:00
|
|
|
|
2022-05-24 02:54:50 +12:00
|
|
|
public function getNewKey(array $scopes)
|
|
|
|
{
|
2021-05-14 02:47:35 +12:00
|
|
|
$projectId = self::$project['$id'];
|
|
|
|
|
2023-08-24 05:12:43 +12:00
|
|
|
$key = $this->client->call(Client::METHOD_POST, '/projects/'.$projectId.'/keys', [
|
2021-05-14 02:47:35 +12:00
|
|
|
'origin' => 'http://localhost',
|
|
|
|
'content-type' => 'application/json',
|
2023-08-24 05:12:43 +12:00
|
|
|
'cookie' => 'a_session_console='.$this->getRoot()['session'],
|
2021-05-14 02:47:35 +12:00
|
|
|
'x-appwrite-project' => 'console',
|
|
|
|
], [
|
|
|
|
'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'];
|
|
|
|
}
|
2019-09-30 19:13:40 +13:00
|
|
|
}
|