2021-08-29 19:50:52 +12:00
|
|
|
<?php
|
|
|
|
|
2022-06-22 22:51:49 +12:00
|
|
|
namespace Tests\E2E\Services\Databases;
|
2021-08-29 19:50:52 +12:00
|
|
|
|
|
|
|
use Tests\E2E\Scopes\Scope;
|
|
|
|
use Tests\E2E\Scopes\ProjectCustom;
|
|
|
|
use Tests\E2E\Client;
|
|
|
|
use Tests\E2E\Scopes\SideConsole;
|
2022-12-15 04:42:25 +13:00
|
|
|
use Utopia\Database\Helpers\ID;
|
2022-12-15 05:04:06 +13:00
|
|
|
use Utopia\Database\Helpers\Permission;
|
|
|
|
use Utopia\Database\Helpers\Role;
|
2021-08-29 19:50:52 +12:00
|
|
|
|
2022-06-22 22:51:49 +12:00
|
|
|
class DatabasesConsoleClientTest extends Scope
|
2021-08-29 19:50:52 +12:00
|
|
|
{
|
|
|
|
use ProjectCustom;
|
|
|
|
use SideConsole;
|
|
|
|
|
2021-10-27 02:19:28 +13:00
|
|
|
public function testCreateCollection(): array
|
2021-08-29 19:50:52 +12:00
|
|
|
{
|
2022-06-22 22:51:49 +12:00
|
|
|
$database = $this->client->call(Client::METHOD_POST, '/databases', array_merge([
|
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
]), [
|
2022-08-14 22:33:36 +12:00
|
|
|
'databaseId' => ID::unique(),
|
2022-06-22 22:51:49 +12:00
|
|
|
'name' => 'invalidDocumentDatabase',
|
|
|
|
]);
|
|
|
|
$this->assertEquals(201, $database['headers']['status-code']);
|
|
|
|
$this->assertEquals('invalidDocumentDatabase', $database['body']['name']);
|
2023-05-25 05:40:12 +12:00
|
|
|
$this->assertTrue($database['body']['enabled']);
|
2022-06-22 22:51:49 +12:00
|
|
|
|
|
|
|
$databaseId = $database['body']['$id'];
|
2021-08-29 19:50:52 +12:00
|
|
|
/**
|
|
|
|
* Test for SUCCESS
|
|
|
|
*/
|
2022-06-22 22:51:49 +12:00
|
|
|
$movies = $this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/collections', array_merge([
|
2021-08-29 19:50:52 +12:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
], $this->getHeaders()), [
|
2022-08-14 22:33:36 +12:00
|
|
|
'collectionId' => ID::unique(),
|
2021-08-29 19:50:52 +12:00
|
|
|
'name' => 'Movies',
|
2022-08-03 16:17:49 +12:00
|
|
|
'permissions' => [
|
2022-08-14 17:21:11 +12:00
|
|
|
Permission::read(Role::any()),
|
|
|
|
Permission::create(Role::any()),
|
|
|
|
Permission::update(Role::any()),
|
|
|
|
Permission::delete(Role::any()),
|
2022-08-03 16:17:49 +12:00
|
|
|
],
|
|
|
|
'documentSecurity' => true,
|
2021-08-29 19:50:52 +12:00
|
|
|
]);
|
|
|
|
|
2022-08-08 23:00:03 +12:00
|
|
|
$this->assertEquals(201, $movies['headers']['status-code']);
|
2021-08-29 19:50:52 +12:00
|
|
|
$this->assertEquals($movies['body']['name'], 'Movies');
|
|
|
|
|
2023-05-25 05:40:12 +12:00
|
|
|
/**
|
|
|
|
* Test When database is disabled but can still create collections
|
|
|
|
*/
|
|
|
|
$database = $this->client->call(Client::METHOD_PUT, '/databases/' . $databaseId, [
|
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
], [
|
|
|
|
'name' => 'invalidDocumentDatabase Updated',
|
|
|
|
'enabled' => false,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertFalse($database['body']['enabled']);
|
|
|
|
|
|
|
|
$movies = $this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/collections', array_merge([
|
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
], $this->getHeaders()), [
|
|
|
|
'collectionId' => ID::unique(),
|
|
|
|
'name' => 'TvShows',
|
|
|
|
'permissions' => [
|
|
|
|
Permission::read(Role::any()),
|
|
|
|
Permission::create(Role::any()),
|
|
|
|
Permission::update(Role::any()),
|
|
|
|
Permission::delete(Role::any()),
|
|
|
|
],
|
|
|
|
'documentSecurity' => true,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertEquals(201, $movies['headers']['status-code']);
|
|
|
|
$this->assertEquals($movies['body']['name'], 'TvShows');
|
|
|
|
|
2022-06-22 22:51:49 +12:00
|
|
|
return ['moviesId' => $movies['body']['$id'], 'databaseId' => $databaseId];
|
2021-08-29 19:50:52 +12:00
|
|
|
}
|
2021-10-27 02:19:28 +13:00
|
|
|
|
2022-06-22 22:51:49 +12:00
|
|
|
/**
|
|
|
|
* @depends testCreateCollection
|
|
|
|
*/
|
|
|
|
// public function testGetDatabaseUsage(array $data)
|
|
|
|
// {
|
|
|
|
// $databaseId = $data['databaseId'];
|
|
|
|
// /**
|
|
|
|
// * Test for FAILURE
|
|
|
|
// */
|
|
|
|
|
|
|
|
// $response = $this->client->call(Client::METHOD_GET, '/databases/' . $databaseId . '/usage', array_merge([
|
|
|
|
// 'content-type' => 'application/json',
|
|
|
|
// 'x-appwrite-project' => $this->getProject()['$id']
|
|
|
|
// ], $this->getHeaders()), [
|
|
|
|
// 'range' => '32h'
|
|
|
|
// ]);
|
|
|
|
|
2022-08-08 23:00:03 +12:00
|
|
|
// $this->assertEquals(400, $response['headers']['status-code']);
|
2022-06-22 22:51:49 +12:00
|
|
|
|
|
|
|
// /**
|
|
|
|
// * Test for SUCCESS
|
|
|
|
// */
|
|
|
|
|
|
|
|
// $response = $this->client->call(Client::METHOD_GET, '/databases/' . $databaseId . '/usage', array_merge([
|
|
|
|
// 'content-type' => 'application/json',
|
|
|
|
// 'x-appwrite-project' => $this->getProject()['$id']
|
|
|
|
// ], $this->getHeaders()), [
|
|
|
|
// 'range' => '24h'
|
|
|
|
// ]);
|
|
|
|
|
2022-08-08 23:00:03 +12:00
|
|
|
// $this->assertEquals(200, $response['headers']['status-code']);
|
2022-06-22 22:51:49 +12:00
|
|
|
// $this->assertEquals(count($response['body']), 11);
|
|
|
|
// $this->assertEquals($response['body']['range'], '24h');
|
|
|
|
// $this->assertIsArray($response['body']['documentsCount']);
|
|
|
|
// $this->assertIsArray($response['body']['collectionsCount']);
|
|
|
|
// $this->assertIsArray($response['body']['documentsCreate']);
|
|
|
|
// $this->assertIsArray($response['body']['documentsRead']);
|
|
|
|
// $this->assertIsArray($response['body']['documentsUpdate']);
|
|
|
|
// $this->assertIsArray($response['body']['documentsDelete']);
|
|
|
|
// $this->assertIsArray($response['body']['collectionsCreate']);
|
|
|
|
// $this->assertIsArray($response['body']['collectionsRead']);
|
|
|
|
// $this->assertIsArray($response['body']['collectionsUpdate']);
|
|
|
|
// $this->assertIsArray($response['body']['collectionsDelete']);
|
|
|
|
// }
|
2021-08-29 19:50:52 +12:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testCreateCollection
|
|
|
|
*/
|
|
|
|
public function testGetCollectionUsage(array $data)
|
|
|
|
{
|
2022-06-22 22:51:49 +12:00
|
|
|
$databaseId = $data['databaseId'];
|
2021-08-29 19:50:52 +12:00
|
|
|
/**
|
2021-08-29 19:59:08 +12:00
|
|
|
* Test for FAILURE
|
2021-08-29 19:50:52 +12:00
|
|
|
*/
|
|
|
|
|
2022-06-22 22:51:49 +12:00
|
|
|
$response = $this->client->call(Client::METHOD_GET, '/databases/' . $databaseId . '/collections/' . $data['moviesId'] . '/usage', array_merge([
|
2021-08-29 19:59:08 +12:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id']
|
|
|
|
], $this->getHeaders()), [
|
|
|
|
'range' => '32h'
|
|
|
|
]);
|
|
|
|
|
2022-08-08 23:00:03 +12:00
|
|
|
$this->assertEquals(400, $response['headers']['status-code']);
|
2021-08-29 19:59:08 +12:00
|
|
|
|
2022-06-22 22:51:49 +12:00
|
|
|
$response = $this->client->call(Client::METHOD_GET, '/databases/' . $databaseId . '/collections/randomCollectionId/usage', array_merge([
|
2021-08-29 19:59:08 +12:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id']
|
|
|
|
], $this->getHeaders()), [
|
|
|
|
'range' => '24h'
|
|
|
|
]);
|
|
|
|
|
2022-08-08 23:00:03 +12:00
|
|
|
$this->assertEquals(404, $response['headers']['status-code']);
|
2021-08-29 19:59:08 +12:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test for SUCCESS
|
|
|
|
*/
|
2022-06-22 22:51:49 +12:00
|
|
|
$response = $this->client->call(Client::METHOD_GET, '/databases/' . $databaseId . '/collections/' . $data['moviesId'] . '/usage', array_merge([
|
2021-08-29 19:50:52 +12:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id']
|
|
|
|
], $this->getHeaders()), [
|
|
|
|
'range' => '24h'
|
|
|
|
]);
|
|
|
|
|
2022-08-08 23:00:03 +12:00
|
|
|
$this->assertEquals(200, $response['headers']['status-code']);
|
2021-08-29 20:11:29 +12:00
|
|
|
$this->assertEquals(count($response['body']), 6);
|
2021-08-29 19:50:52 +12:00
|
|
|
$this->assertEquals($response['body']['range'], '24h');
|
2021-10-27 02:19:28 +13:00
|
|
|
$this->assertIsArray($response['body']['documentsCount']);
|
|
|
|
$this->assertIsArray($response['body']['documentsCreate']);
|
|
|
|
$this->assertIsArray($response['body']['documentsRead']);
|
|
|
|
$this->assertIsArray($response['body']['documentsUpdate']);
|
|
|
|
$this->assertIsArray($response['body']['documentsDelete']);
|
2021-08-29 19:50:52 +12:00
|
|
|
}
|
2021-11-15 22:49:11 +13:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testCreateCollection
|
|
|
|
*/
|
|
|
|
public function testGetCollectionLogs(array $data)
|
|
|
|
{
|
2022-06-22 22:51:49 +12:00
|
|
|
$databaseId = $data['databaseId'];
|
2021-11-15 22:49:11 +13:00
|
|
|
/**
|
|
|
|
* Test for SUCCESS
|
|
|
|
*/
|
2022-06-22 22:51:49 +12:00
|
|
|
$logs = $this->client->call(Client::METHOD_GET, '/databases/' . $databaseId . '/collections/' . $data['moviesId'] . '/logs', array_merge([
|
2021-11-15 22:49:11 +13:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
], $this->getHeaders()));
|
|
|
|
|
2022-08-08 23:00:03 +12:00
|
|
|
$this->assertEquals(200, $logs['headers']['status-code']);
|
2021-11-15 22:49:11 +13:00
|
|
|
$this->assertIsArray($logs['body']['logs']);
|
2022-02-27 22:57:09 +13:00
|
|
|
$this->assertIsNumeric($logs['body']['total']);
|
2021-11-15 22:49:11 +13:00
|
|
|
|
2022-06-22 22:51:49 +12:00
|
|
|
$logs = $this->client->call(Client::METHOD_GET, '/databases/' . $databaseId . '/collections/' . $data['moviesId'] . '/logs', array_merge([
|
2021-11-15 22:49:11 +13:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
], $this->getHeaders()), [
|
|
|
|
'limit' => 1
|
|
|
|
]);
|
|
|
|
|
2022-08-08 23:00:03 +12:00
|
|
|
$this->assertEquals(200, $logs['headers']['status-code']);
|
2021-11-15 22:49:11 +13:00
|
|
|
$this->assertIsArray($logs['body']['logs']);
|
|
|
|
$this->assertLessThanOrEqual(1, count($logs['body']['logs']));
|
2022-02-27 22:57:09 +13:00
|
|
|
$this->assertIsNumeric($logs['body']['total']);
|
2021-11-15 22:49:11 +13:00
|
|
|
|
2022-06-22 22:51:49 +12:00
|
|
|
$logs = $this->client->call(Client::METHOD_GET, '/databases/' . $databaseId . '/collections/' . $data['moviesId'] . '/logs', array_merge([
|
2021-11-15 22:49:11 +13:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
], $this->getHeaders()), [
|
|
|
|
'offset' => 1
|
|
|
|
]);
|
|
|
|
|
2022-08-08 23:00:03 +12:00
|
|
|
$this->assertEquals(200, $logs['headers']['status-code']);
|
2021-11-15 22:49:11 +13:00
|
|
|
$this->assertIsArray($logs['body']['logs']);
|
2022-02-27 22:57:09 +13:00
|
|
|
$this->assertIsNumeric($logs['body']['total']);
|
2021-11-15 22:49:11 +13:00
|
|
|
|
2022-06-22 22:51:49 +12:00
|
|
|
$logs = $this->client->call(Client::METHOD_GET, '/databases/' . $databaseId . '/collections/' . $data['moviesId'] . '/logs', array_merge([
|
2021-11-15 22:49:11 +13:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
], $this->getHeaders()), [
|
|
|
|
'offset' => 1,
|
|
|
|
'limit' => 1
|
|
|
|
]);
|
|
|
|
|
2022-08-08 23:00:03 +12:00
|
|
|
$this->assertEquals(200, $logs['headers']['status-code']);
|
2021-11-15 22:49:11 +13:00
|
|
|
$this->assertIsArray($logs['body']['logs']);
|
|
|
|
$this->assertLessThanOrEqual(1, count($logs['body']['logs']));
|
2022-02-27 22:57:09 +13:00
|
|
|
$this->assertIsNumeric($logs['body']['total']);
|
2021-11-15 22:49:11 +13:00
|
|
|
}
|
2021-10-27 02:19:28 +13:00
|
|
|
}
|