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\Scope;
|
|
|
|
use Tests\E2E\Scopes\ProjectCustom;
|
|
|
|
use Tests\E2E\Scopes\SideClient;
|
|
|
|
|
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-09 14:48:56 +12:00
|
|
|
[['read(any)']],
|
|
|
|
[['read(users)']],
|
|
|
|
[['read(user:random)']],
|
|
|
|
[['read(user:lorem)', 'create(user:lorem)', 'update(user:lorem)', 'delete(user:lorem)',]],
|
|
|
|
[['read(user:dolor)', 'create(user:dolor)', 'update(user:dolor)', 'delete(user:dolor)',]],
|
2022-08-13 00:49:57 +12:00
|
|
|
[['read(user:dolor)', 'read(user:lorem)', 'create(user:dolor)', 'update(user:dolor)', 'delete(user:dolor)',]],
|
2022-08-09 14:48:56 +12:00
|
|
|
[['create(any)', 'update(any)', 'delete(any)']],
|
|
|
|
[['read(any)', 'create(any)', 'update(any)', 'delete(any)']],
|
|
|
|
[['read(users)', 'create(users)', 'update(users)', 'delete(users)']],
|
|
|
|
[['read(any)', 'create(users)', 'update(users)', 'delete(users)']],
|
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(), [
|
|
|
|
'databaseId' => 'unique()',
|
|
|
|
'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(), [
|
2021-10-09 01:49:45 +13:00
|
|
|
'collectionId' => 'unique()',
|
|
|
|
'name' => 'Movies',
|
2022-08-03 16:17:49 +12:00
|
|
|
'permissions' => [
|
|
|
|
'read(any)',
|
2022-08-05 17:23:18 +12:00
|
|
|
'create(any)',
|
|
|
|
'update(any)',
|
|
|
|
'delete(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']);
|
2021-10-09 01:49:45 +13:00
|
|
|
|
2021-10-22 13:38:17 +13:00
|
|
|
$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(), [
|
2021-10-09 01:49:45 +13:00
|
|
|
'collectionId' => 'unique()',
|
|
|
|
'name' => 'Private Movies',
|
2022-08-03 16:17:49 +12:00
|
|
|
'permissions' => [
|
|
|
|
'read(users)',
|
2022-08-09 14:48:56 +12:00
|
|
|
'create(users)',
|
|
|
|
'update(users)',
|
|
|
|
'delete(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']);
|
2021-10-09 01:49:45 +13:00
|
|
|
|
2021-10-22 13:38:17 +13:00
|
|
|
$this->collections['private'] = $private['body']['$id'];
|
2021-10-09 01:49:45 +13:00
|
|
|
|
2022-06-22 22:51:49 +12:00
|
|
|
$this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/collections/' . $this->collections['private'] . '/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
|
2021-10-22 13:38:17 +13:00
|
|
|
* @depends testSetupDatabase
|
|
|
|
*/
|
2022-08-09 14:48:56 +12:00
|
|
|
public function testReadDocuments($permissions, $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(), [
|
2021-10-22 13:38:17 +13:00
|
|
|
'documentId' => 'unique()',
|
|
|
|
'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(), [
|
2021-10-22 13:38:17 +13:00
|
|
|
'documentId' => 'unique()',
|
|
|
|
'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
|
|
|
|
|
|
|
/**
|
|
|
|
* Check role:all collection
|
|
|
|
*/
|
2022-06-22 22:51:49 +12:00
|
|
|
$documents = $this->client->call(Client::METHOD_GET, '/databases/' . $databaseId . '/collections/' . $collections['public'] . '/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
|
|
|
]);
|
|
|
|
|
|
|
|
foreach ($documents['body']['documents'] as $document) {
|
2022-08-09 14:48:56 +12:00
|
|
|
$hasPermissions = \array_reduce(['any', 'users', 'user:' . $users['user1']['$id']], function (bool $carry, string $role) use ($document) {
|
2022-08-03 16:17:49 +12:00
|
|
|
if ($carry) {
|
2022-08-09 14:48:56 +12:00
|
|
|
return true;
|
2022-08-03 16:17:49 +12:00
|
|
|
}
|
|
|
|
foreach ($document['$permissions'] as $permission) {
|
2022-08-09 14:48:56 +12:00
|
|
|
if (\str_starts_with($permission, 'read') && \str_contains($permission, $role)) {
|
2022-08-03 16:17:49 +12:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2021-10-09 01:49:45 +13:00
|
|
|
}, false);
|
2022-08-09 14:48:56 +12:00
|
|
|
|
2021-10-09 01:49:45 +13:00
|
|
|
$this->assertTrue($hasPermissions);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check role:member collection
|
|
|
|
*/
|
2022-06-22 22:51:49 +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
|
|
|
]);
|
|
|
|
|
|
|
|
foreach ($documents['body']['documents'] as $document) {
|
2022-08-09 14:48:56 +12:00
|
|
|
$hasPermissions = \array_reduce(['any', 'users', 'user:' . $users['user1']['$id']], function (bool $carry, string $role) use ($document) {
|
2022-08-03 16:17:49 +12:00
|
|
|
if ($carry) {
|
2022-08-09 14:48:56 +12:00
|
|
|
return true;
|
2022-08-03 16:17:49 +12:00
|
|
|
}
|
|
|
|
foreach ($document['$permissions'] as $permission) {
|
2022-08-09 14:48:56 +12:00
|
|
|
if (\str_starts_with($permission, 'read') && \str_contains($permission, $role)) {
|
2022-08-03 16:17:49 +12:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2021-10-09 01:49:45 +13:00
|
|
|
}, false);
|
2022-08-09 14:48:56 +12:00
|
|
|
|
2021-10-09 01:49:45 +13:00
|
|
|
$this->assertTrue($hasPermissions);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|