2021-10-09 01:49:45 +13:00
|
|
|
<?php
|
|
|
|
|
2022-06-22 22:51:49 +12:00
|
|
|
namespace Tests\E2E\Services\Databases;
|
2021-10-09 01:49:45 +13:00
|
|
|
|
|
|
|
use Tests\E2E\Client;
|
|
|
|
use Tests\E2E\Scopes\ProjectCustom;
|
2022-08-25 01:32:52 +12:00
|
|
|
use Tests\E2E\Scopes\Scope;
|
2021-10-09 01:49:45 +13:00
|
|
|
use Tests\E2E\Scopes\SideClient;
|
2022-08-14 22:33:36 +12:00
|
|
|
use Utopia\Database\ID;
|
2022-08-14 17:21:11 +12:00
|
|
|
use Utopia\Database\Permission;
|
|
|
|
use Utopia\Database\Role;
|
2021-10-09 01:49:45 +13:00
|
|
|
|
2022-06-22 22:51:49 +12:00
|
|
|
class DatabasesPermissionsMemberTest extends Scope
|
2021-10-09 01:49:45 +13:00
|
|
|
{
|
|
|
|
use ProjectCustom;
|
|
|
|
use SideClient;
|
2022-06-22 22:51:49 +12:00
|
|
|
use DatabasesPermissionsScope;
|
2021-10-09 01:49:45 +13:00
|
|
|
|
2021-10-22 13:38:17 +13:00
|
|
|
public array $collections = [];
|
2021-10-09 01:49:45 +13:00
|
|
|
|
2021-10-22 13:38:17 +13:00
|
|
|
public function createUsers(): array
|
2021-10-09 01:49:45 +13:00
|
|
|
{
|
2021-10-22 13:38:17 +13:00
|
|
|
return [
|
|
|
|
'user1' => $this->createUser('user1', 'lorem@ipsum.com'),
|
|
|
|
'user2' => $this->createUser('user2', 'dolor@ipsum.com'),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2022-08-09 14:48:56 +12:00
|
|
|
public function permissionsProvider(): array
|
2021-10-22 13:38:17 +13:00
|
|
|
{
|
|
|
|
return [
|
2022-08-25 01:32:52 +12:00
|
|
|
[
|
|
|
|
'permissions' => [Permission::read(Role::any())],
|
|
|
|
'any' => 1,
|
|
|
|
'users' => 1,
|
|
|
|
'doconly' => 1,
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'permissions' => [Permission::read(Role::users())],
|
|
|
|
'any' => 2,
|
|
|
|
'users' => 2,
|
|
|
|
'doconly' => 2,
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'permissions' => [Permission::read(Role::user(ID::custom('random')))],
|
|
|
|
'any' => 3,
|
|
|
|
'users' => 3,
|
|
|
|
'doconly' => 2,
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'permissions' => [Permission::read(Role::user(ID::custom('lorem'))), Permission::update(Role::user('lorem')), Permission::delete(Role::user('lorem'))],
|
|
|
|
'any' => 4,
|
|
|
|
'users' => 4,
|
|
|
|
'doconly' => 2,
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'permissions' => [Permission::read(Role::user(ID::custom('dolor'))), Permission::update(Role::user('dolor')), Permission::delete(Role::user('dolor'))],
|
|
|
|
'any' => 5,
|
|
|
|
'users' => 5,
|
|
|
|
'doconly' => 2,
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'permissions' => [Permission::read(Role::user(ID::custom('dolor'))), Permission::read(Role::user('lorem')), Permission::update(Role::user('dolor')), Permission::delete(Role::user('dolor'))],
|
|
|
|
'any' => 6,
|
|
|
|
'users' => 6,
|
|
|
|
'doconly' => 2,
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'permissions' => [Permission::update(Role::any()), Permission::delete(Role::any())],
|
|
|
|
'any' => 7,
|
|
|
|
'users' => 7,
|
|
|
|
'doconly' => 2,
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'permissions' => [Permission::read(Role::any()), Permission::update(Role::any()), Permission::delete(Role::any())],
|
|
|
|
'any' => 8,
|
|
|
|
'users' => 8,
|
|
|
|
'doconly' => 3,
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'permissions' => [Permission::read(Role::any()), Permission::update(Role::users()), Permission::delete(Role::users())],
|
|
|
|
'any' => 9,
|
|
|
|
'users' => 9,
|
|
|
|
'doconly' => 4,
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'permissions' => [Permission::read(Role::user(ID::custom('user1')))],
|
|
|
|
'any' => 10,
|
|
|
|
'users' => 10,
|
|
|
|
'doconly' => 5,
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'permissions' => [Permission::read(Role::user(ID::custom('user1'))), Permission::read(Role::user(ID::custom('user1')))],
|
|
|
|
'any' => 11,
|
|
|
|
'users' => 11,
|
|
|
|
'doconly' => 6,
|
|
|
|
],
|
2022-08-27 15:16:37 +12:00
|
|
|
[
|
|
|
|
'permissions' => [Permission::read(Role::users()), Permission::update(Role::users()), Permission::delete(Role::users())],
|
|
|
|
'any' => 12,
|
|
|
|
'users' => 12,
|
|
|
|
'doconly' => 7,
|
|
|
|
],
|
2021-10-22 13:38:17 +13:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Setup database
|
|
|
|
*
|
2022-08-09 14:48:56 +12:00
|
|
|
* Data providers lose object state so explicitly pass [$users, $collections] to each iteration
|
|
|
|
*
|
2021-10-22 13:38:17 +13:00
|
|
|
* @return array
|
2022-08-09 14:48:56 +12:00
|
|
|
* @throws \Exception
|
2021-10-22 13:38:17 +13:00
|
|
|
*/
|
|
|
|
public function testSetupDatabase(): array
|
|
|
|
{
|
|
|
|
$this->createUsers();
|
|
|
|
|
2022-06-22 22:51:49 +12:00
|
|
|
$db = $this->client->call(Client::METHOD_POST, '/databases', $this->getServerHeader(), [
|
2022-08-14 22:33:36 +12:00
|
|
|
'databaseId' => ID::unique(),
|
2022-06-22 22:51:49 +12:00
|
|
|
'name' => 'Test Database',
|
|
|
|
]);
|
|
|
|
$this->assertEquals(201, $db['headers']['status-code']);
|
|
|
|
|
|
|
|
$databaseId = $db['body']['$id'];
|
|
|
|
|
|
|
|
$public = $this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/collections', $this->getServerHeader(), [
|
2022-08-14 22:33:36 +12:00
|
|
|
'collectionId' => ID::unique(),
|
2021-10-09 01:49:45 +13: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-10-09 01:49:45 +13:00
|
|
|
]);
|
2021-10-22 13:38:17 +13:00
|
|
|
$this->assertEquals(201, $public['headers']['status-code']);
|
|
|
|
$this->collections = ['public' => $public['body']['$id']];
|
2021-10-09 01:49:45 +13:00
|
|
|
|
2022-06-22 22:51:49 +12:00
|
|
|
$response = $this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/collections/' . $this->collections['public'] . '/attributes/string', $this->getServerHeader(), [
|
2021-12-17 04:04:30 +13:00
|
|
|
'key' => 'title',
|
2021-10-09 01:49:45 +13:00
|
|
|
'size' => 256,
|
|
|
|
'required' => true,
|
|
|
|
]);
|
2022-07-19 01:22:23 +12:00
|
|
|
$this->assertEquals(202, $response['headers']['status-code']);
|
2021-10-09 01:49:45 +13:00
|
|
|
|
2022-06-22 22:51:49 +12:00
|
|
|
$private = $this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/collections', $this->getServerHeader(), [
|
2022-08-14 22:33:36 +12:00
|
|
|
'collectionId' => ID::unique(),
|
2021-10-09 01:49:45 +13:00
|
|
|
'name' => 'Private Movies',
|
2022-08-03 16:17:49 +12:00
|
|
|
'permissions' => [
|
2022-08-14 17:21:11 +12:00
|
|
|
Permission::read(Role::users()),
|
|
|
|
Permission::create(Role::users()),
|
|
|
|
Permission::update(Role::users()),
|
|
|
|
Permission::delete(Role::users()),
|
2022-08-03 16:17:49 +12:00
|
|
|
],
|
|
|
|
'documentSecurity' => true,
|
2021-10-09 01:49:45 +13:00
|
|
|
]);
|
2021-10-22 13:38:17 +13:00
|
|
|
$this->assertEquals(201, $private['headers']['status-code']);
|
|
|
|
$this->collections['private'] = $private['body']['$id'];
|
2021-10-09 01:49:45 +13:00
|
|
|
|
2022-08-25 01:32:52 +12:00
|
|
|
$response = $this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/collections/' . $this->collections['private'] . '/attributes/string', $this->getServerHeader(), [
|
|
|
|
'key' => 'title',
|
|
|
|
'size' => 256,
|
|
|
|
'required' => true,
|
|
|
|
]);
|
|
|
|
$this->assertEquals(202, $response['headers']['status-code']);
|
|
|
|
|
|
|
|
$doconly = $this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/collections', $this->getServerHeader(), [
|
|
|
|
'collectionId' => ID::unique(),
|
|
|
|
'name' => 'Document Only Movies',
|
|
|
|
'permissions' => [],
|
|
|
|
'documentSecurity' => true,
|
|
|
|
]);
|
|
|
|
$this->assertEquals(201, $private['headers']['status-code']);
|
|
|
|
$this->collections['doconly'] = $doconly['body']['$id'];
|
|
|
|
|
|
|
|
$response = $this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/collections/' . $this->collections['doconly'] . '/attributes/string', $this->getServerHeader(), [
|
2021-12-17 04:04:30 +13:00
|
|
|
'key' => 'title',
|
2021-10-09 01:49:45 +13:00
|
|
|
'size' => 256,
|
|
|
|
'required' => true,
|
|
|
|
]);
|
2022-07-19 01:22:23 +12:00
|
|
|
$this->assertEquals(202, $response['headers']['status-code']);
|
2021-10-09 01:49:45 +13:00
|
|
|
|
|
|
|
sleep(2);
|
|
|
|
|
2021-10-22 13:38:17 +13:00
|
|
|
return [
|
|
|
|
'users' => $this->users,
|
2022-06-22 22:51:49 +12:00
|
|
|
'collections' => $this->collections,
|
|
|
|
'databaseId' => $databaseId
|
2021-10-22 13:38:17 +13:00
|
|
|
];
|
2021-10-09 01:49:45 +13:00
|
|
|
}
|
|
|
|
|
2021-10-22 13:38:17 +13:00
|
|
|
/**
|
|
|
|
* Data provider params are passed before test dependencies
|
2022-08-09 14:48:56 +12:00
|
|
|
* @dataProvider permissionsProvider
|
2022-08-25 01:32:52 +12:00
|
|
|
* @depends testSetupDatabase
|
2021-10-22 13:38:17 +13:00
|
|
|
*/
|
2022-08-25 01:32:52 +12:00
|
|
|
public function testReadDocuments($permissions, $anyCount, $usersCount, $docOnlyCount, $data)
|
2021-10-09 01:49:45 +13:00
|
|
|
{
|
2021-10-22 13:38:17 +13:00
|
|
|
$users = $data['users'];
|
|
|
|
$collections = $data['collections'];
|
2022-06-22 22:51:49 +12:00
|
|
|
$databaseId = $data['databaseId'];
|
2021-10-22 13:38:17 +13:00
|
|
|
|
2022-06-22 22:51:49 +12:00
|
|
|
$response = $this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/collections/' . $collections['public'] . '/documents', $this->getServerHeader(), [
|
2022-08-14 22:33:36 +12:00
|
|
|
'documentId' => ID::unique(),
|
2021-10-22 13:38:17 +13:00
|
|
|
'data' => [
|
|
|
|
'title' => 'Lorem',
|
|
|
|
],
|
2022-08-09 14:48:56 +12:00
|
|
|
'permissions' => $permissions
|
2021-10-22 13:38:17 +13:00
|
|
|
]);
|
|
|
|
$this->assertEquals(201, $response['headers']['status-code']);
|
|
|
|
|
2022-06-22 22:51:49 +12:00
|
|
|
$response = $this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/collections/' . $collections['private'] . '/documents', $this->getServerHeader(), [
|
2022-08-14 22:33:36 +12:00
|
|
|
'documentId' => ID::unique(),
|
2021-10-22 13:38:17 +13:00
|
|
|
'data' => [
|
|
|
|
'title' => 'Lorem',
|
|
|
|
],
|
2022-08-09 14:48:56 +12:00
|
|
|
'permissions' => $permissions
|
2021-10-22 13:38:17 +13:00
|
|
|
]);
|
|
|
|
$this->assertEquals(201, $response['headers']['status-code']);
|
2021-10-09 01:49:45 +13:00
|
|
|
|
2022-08-25 01:32:52 +12:00
|
|
|
$response = $this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/collections/' . $collections['doconly'] . '/documents', $this->getServerHeader(), [
|
|
|
|
'documentId' => ID::unique(),
|
|
|
|
'data' => [
|
|
|
|
'title' => 'Lorem',
|
|
|
|
],
|
|
|
|
'permissions' => $permissions
|
|
|
|
]);
|
|
|
|
$this->assertEquals(201, $response['headers']['status-code']);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check "any" permission collection
|
|
|
|
*/
|
|
|
|
$documents = $this->client->call(Client::METHOD_GET, '/databases/' . $databaseId . '/collections/' . $collections['public'] . '/documents', [
|
|
|
|
'origin' => 'http://localhost',
|
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
'cookie' => 'a_session_' . $this->getProject()['$id'] . '=' . $users['user1']['session'],
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertEquals(200, $documents['headers']['status-code']);
|
|
|
|
$this->assertEquals($anyCount, $documents['body']['total']);
|
|
|
|
|
2021-10-09 01:49:45 +13:00
|
|
|
/**
|
2022-08-25 01:32:52 +12:00
|
|
|
* Check "users" permission collection
|
2021-10-09 01:49:45 +13:00
|
|
|
*/
|
2022-08-25 01:32:52 +12:00
|
|
|
$documents = $this->client->call(Client::METHOD_GET, '/databases/' . $databaseId . '/collections/' . $collections['private'] . '/documents', [
|
2021-10-09 01:49:45 +13:00
|
|
|
'origin' => 'http://localhost',
|
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
2021-10-22 13:38:17 +13:00
|
|
|
'cookie' => 'a_session_' . $this->getProject()['$id'] . '=' . $users['user1']['session'],
|
2021-10-09 01:49:45 +13:00
|
|
|
]);
|
|
|
|
|
2022-08-25 01:32:52 +12:00
|
|
|
$this->assertEquals(200, $documents['headers']['status-code']);
|
|
|
|
$this->assertEquals($usersCount, $documents['body']['total']);
|
2021-10-09 01:49:45 +13:00
|
|
|
|
|
|
|
/**
|
2022-08-25 01:32:52 +12:00
|
|
|
* Check "user:user1" document only permission collection
|
2021-10-09 01:49:45 +13:00
|
|
|
*/
|
2022-08-25 01:32:52 +12:00
|
|
|
$documents = $this->client->call(Client::METHOD_GET, '/databases/' . $databaseId . '/collections/' . $collections['doconly'] . '/documents', [
|
2021-10-09 01:49:45 +13:00
|
|
|
'origin' => 'http://localhost',
|
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
2021-10-22 13:38:17 +13:00
|
|
|
'cookie' => 'a_session_' . $this->getProject()['$id'] . '=' . $users['user1']['session'],
|
2021-10-09 01:49:45 +13:00
|
|
|
]);
|
|
|
|
|
2022-08-25 01:32:52 +12:00
|
|
|
$this->assertEquals(200, $documents['headers']['status-code']);
|
|
|
|
$this->assertEquals($docOnlyCount, $documents['body']['total']);
|
2021-10-09 01:49:45 +13:00
|
|
|
}
|
|
|
|
}
|