appwrite/tests/e2e/Services/Databases/Permissions/LegacyPermissionsMemberTest.php

290 lines
11 KiB
PHP
Raw Permalink Normal View History

2021-10-08 12:49:45 +00:00
<?php
namespace Tests\E2E\Services\Databases\Permissions;
2021-10-08 12:49:45 +00:00
2026-01-15 03:14:53 +00:00
use PHPUnit\Framework\Attributes\DataProvider;
2021-10-08 12:49:45 +00:00
use Tests\E2E\Client;
use Tests\E2E\Scopes\ApiLegacy;
2021-10-08 12:49:45 +00:00
use Tests\E2E\Scopes\ProjectCustom;
2026-02-24 01:00:07 +00:00
use Tests\E2E\Scopes\SchemaPolling;
2022-08-24 13:32:52 +00:00
use Tests\E2E\Scopes\Scope;
2021-10-08 12:49:45 +00:00
use Tests\E2E\Scopes\SideClient;
2022-12-14 15:42:25 +00:00
use Utopia\Database\Helpers\ID;
2022-12-14 16:04:06 +00:00
use Utopia\Database\Helpers\Permission;
use Utopia\Database\Helpers\Role;
2021-10-08 12:49:45 +00:00
class LegacyPermissionsMemberTest extends Scope
2021-10-08 12:49:45 +00:00
{
use DatabasesPermissionsBase;
2021-10-08 12:49:45 +00:00
use ProjectCustom;
use SideClient;
use ApiLegacy;
2026-02-24 01:00:07 +00:00
use SchemaPolling;
2021-10-08 12:49:45 +00:00
public array $collections = [];
2021-10-08 12:49:45 +00:00
public function createUsers(): array
2021-10-08 12:49:45 +00:00
{
return [
'user1' => $this->createUser('user1', 'lorem@ipsum.com'),
'user2' => $this->createUser('user2', 'dolor@ipsum.com'),
];
}
2026-01-15 03:14:53 +00:00
public static function permissionsProvider(): array
{
return [
2026-01-16 08:09:46 +00:00
[[Permission::read(Role::any())], 1, 1, 1],
[[Permission::read(Role::users())], 1, 1, 1],
[[Permission::read(Role::user(ID::custom('random')))], 1, 1, 0],
[[Permission::read(Role::user(ID::custom('lorem'))), Permission::update(Role::user('lorem')), Permission::delete(Role::user('lorem'))], 1, 1, 0],
[[Permission::read(Role::user(ID::custom('dolor'))), Permission::update(Role::user('dolor')), Permission::delete(Role::user('dolor'))], 1, 1, 0],
[[Permission::read(Role::user(ID::custom('dolor'))), Permission::read(Role::user('lorem')), Permission::update(Role::user('dolor')), Permission::delete(Role::user('dolor'))], 1, 1, 0],
[[Permission::update(Role::any()), Permission::delete(Role::any())], 1, 1, 0],
[[Permission::read(Role::any()), Permission::update(Role::any()), Permission::delete(Role::any())], 1, 1, 1],
[[Permission::read(Role::any()), Permission::update(Role::users()), Permission::delete(Role::users())], 1, 1, 1],
[[Permission::read(Role::user(ID::custom('user1')))], 1, 1, 1],
[[Permission::read(Role::user(ID::custom('user1'))), Permission::read(Role::user(ID::custom('user1')))], 1, 1, 1],
[[Permission::read(Role::users()), Permission::update(Role::users()), Permission::delete(Role::users())], 1, 1, 1],
];
}
/**
* Setup database helper with caching
*/
protected function setupDatabase(): array
{
$cacheKey = $this->getProject()['$id'] . '_' . static::class;
if (!empty(self::$setupDatabaseCache[$cacheKey])) {
return self::$setupDatabaseCache[$cacheKey];
}
$this->createUsers();
$db = $this->client->call(
Client::METHOD_POST,
$this->getDatabaseUrl(),
$this->getServerHeader(),
[
'databaseId' => ID::unique(),
'name' => 'Test Database',
]
);
Database layer (#3338) * database response model * database collection config * new database scopes * database service update * database execption codes * remove read write permission from database model * updating tests and fixing some bugs * server side tests are now passing * databases api * tests for database endpoint * composer update * fix error * formatting * formatting fixes * get database test * more updates to events and usage * more usage updates * fix delete type * fix test * delete database * more fixes * databaseId in attributes and indexes * more fixes * fix issues * fix index subquery * fix console scope and index query * updating tests as required * fix phpcs errors and warnings * updates to review suggestions * UI progress * ui updates and cleaning up * fix type * rework database events * update tests * update types * event generation fixed * events config updated * updating context to support multiple * realtime updates * fix ids * update context * validator updates * fix naming conflict * fix tests * fix lint errors * fix wprler and realtime tests * fix webhooks test * fix event validator and other tests * formatting fixes * removing leftover var_dumps * remove leftover comment * update usage params * usage metrics updates * update database usage * fix usage * specs update * updates to usage * fix UI and usage * fix lints * internal id fixes * fixes for internal Id * renaming services and related files * rename tests * rename doc link * rename readme * fix test name * tests: fixes for 0.15.x sync Co-authored-by: Torsten Dittmann <torsten.dittmann@googlemail.com>
2022-06-22 10:51:49 +00:00
$this->assertEquals(201, $db['headers']['status-code']);
$databaseId = $db['body']['$id'];
$public = $this->client->call(
Client::METHOD_POST,
$this->getContainerUrl($databaseId),
$this->getServerHeader(),
[
$this->getContainerIdParam() => ID::unique(),
'name' => 'Movies',
'permissions' => [
Permission::read(Role::any()),
Permission::create(Role::any()),
Permission::update(Role::any()),
Permission::delete(Role::any()),
],
$this->getSecurityParam() => true,
]
);
$this->assertEquals(201, $public['headers']['status-code']);
$this->collections = ['public' => $public['body']['$id']];
2021-10-08 12:49:45 +00:00
$response = $this->client->call(
Client::METHOD_POST,
$this->getSchemaUrl($databaseId, $this->collections['public'], 'string'),
$this->getServerHeader(),
[
'key' => 'title',
'size' => 256,
'required' => true,
]
);
2022-07-18 13:22:23 +00:00
$this->assertEquals(202, $response['headers']['status-code']);
2021-10-08 12:49:45 +00:00
$private = $this->client->call(
Client::METHOD_POST,
$this->getContainerUrl($databaseId),
$this->getServerHeader(),
[
$this->getContainerIdParam() => ID::unique(),
'name' => 'Private Movies',
'permissions' => [
Permission::read(Role::users()),
Permission::create(Role::users()),
Permission::update(Role::users()),
Permission::delete(Role::users()),
],
$this->getSecurityParam() => true,
]
);
$this->assertEquals(201, $private['headers']['status-code']);
$this->collections['private'] = $private['body']['$id'];
2021-10-08 12:49:45 +00:00
$response = $this->client->call(
Client::METHOD_POST,
$this->getSchemaUrl($databaseId, $this->collections['private'], 'string'),
$this->getServerHeader(),
[
'key' => 'title',
'size' => 256,
'required' => true,
]
);
2022-08-24 13:32:52 +00:00
$this->assertEquals(202, $response['headers']['status-code']);
$doconly = $this->client->call(
Client::METHOD_POST,
$this->getContainerUrl($databaseId),
$this->getServerHeader(),
[
$this->getContainerIdParam() => ID::unique(),
'name' => 'Document Only Movies',
'permissions' => [],
$this->getSecurityParam() => true,
]
);
$this->assertEquals(201, $doconly['headers']['status-code']);
2022-08-24 13:32:52 +00:00
$this->collections['doconly'] = $doconly['body']['$id'];
$response = $this->client->call(
Client::METHOD_POST,
$this->getSchemaUrl($databaseId, $this->collections['doconly'], 'string'),
$this->getServerHeader(),
[
'key' => 'title',
'size' => 256,
'required' => true,
]
);
2022-07-18 13:22:23 +00:00
$this->assertEquals(202, $response['headers']['status-code']);
2021-10-08 12:49:45 +00:00
2026-02-24 01:00:07 +00:00
$this->waitForAttribute($databaseId, $this->collections['public'], 'title');
$this->waitForAttribute($databaseId, $this->collections['private'], 'title');
$this->waitForAttribute($databaseId, $this->collections['doconly'], 'title');
2021-10-08 12:49:45 +00:00
self::$setupDatabaseCache[$cacheKey] = [
'users' => $this->users,
2025-05-08 14:23:31 +00:00
'collections' => $this->collections,
Database layer (#3338) * database response model * database collection config * new database scopes * database service update * database execption codes * remove read write permission from database model * updating tests and fixing some bugs * server side tests are now passing * databases api * tests for database endpoint * composer update * fix error * formatting * formatting fixes * get database test * more updates to events and usage * more usage updates * fix delete type * fix test * delete database * more fixes * databaseId in attributes and indexes * more fixes * fix issues * fix index subquery * fix console scope and index query * updating tests as required * fix phpcs errors and warnings * updates to review suggestions * UI progress * ui updates and cleaning up * fix type * rework database events * update tests * update types * event generation fixed * events config updated * updating context to support multiple * realtime updates * fix ids * update context * validator updates * fix naming conflict * fix tests * fix lint errors * fix wprler and realtime tests * fix webhooks test * fix event validator and other tests * formatting fixes * removing leftover var_dumps * remove leftover comment * update usage params * usage metrics updates * update database usage * fix usage * specs update * updates to usage * fix UI and usage * fix lints * internal id fixes * fixes for internal Id * renaming services and related files * rename tests * rename doc link * rename readme * fix test name * tests: fixes for 0.15.x sync Co-authored-by: Torsten Dittmann <torsten.dittmann@googlemail.com>
2022-06-22 10:51:49 +00:00
'databaseId' => $databaseId
];
return self::$setupDatabaseCache[$cacheKey];
}
/**
* Setup database test
*/
public function testSetupDatabase(): void
{
$data = $this->setupDatabase();
$this->assertNotEmpty($data['databaseId']);
2021-10-08 12:49:45 +00:00
}
2026-01-15 03:14:53 +00:00
#[DataProvider('permissionsProvider')]
public function testReadDocuments($permissions, $anyCount, $usersCount, $docOnlyCount)
2021-10-08 12:49:45 +00:00
{
$data = $this->setupDatabase();
$users = $data['users'];
2025-05-08 14:23:31 +00:00
$collections = $data['collections'];
Database layer (#3338) * database response model * database collection config * new database scopes * database service update * database execption codes * remove read write permission from database model * updating tests and fixing some bugs * server side tests are now passing * databases api * tests for database endpoint * composer update * fix error * formatting * formatting fixes * get database test * more updates to events and usage * more usage updates * fix delete type * fix test * delete database * more fixes * databaseId in attributes and indexes * more fixes * fix issues * fix index subquery * fix console scope and index query * updating tests as required * fix phpcs errors and warnings * updates to review suggestions * UI progress * ui updates and cleaning up * fix type * rework database events * update tests * update types * event generation fixed * events config updated * updating context to support multiple * realtime updates * fix ids * update context * validator updates * fix naming conflict * fix tests * fix lint errors * fix wprler and realtime tests * fix webhooks test * fix event validator and other tests * formatting fixes * removing leftover var_dumps * remove leftover comment * update usage params * usage metrics updates * update database usage * fix usage * specs update * updates to usage * fix UI and usage * fix lints * internal id fixes * fixes for internal Id * renaming services and related files * rename tests * rename doc link * rename readme * fix test name * tests: fixes for 0.15.x sync Co-authored-by: Torsten Dittmann <torsten.dittmann@googlemail.com>
2022-06-22 10:51:49 +00:00
$databaseId = $data['databaseId'];
$response = $this->client->call(
Client::METHOD_POST,
$this->getRecordUrl($databaseId, $collections['public']),
$this->getServerHeader(),
[
$this->getRecordIdParam() => ID::unique(),
'data' => [
'title' => 'Lorem',
],
'permissions' => $permissions
]
);
$this->assertEquals(201, $response['headers']['status-code']);
$response = $this->client->call(
Client::METHOD_POST,
$this->getRecordUrl($databaseId, $collections['private']),
$this->getServerHeader(),
[
$this->getRecordIdParam() => ID::unique(),
'data' => [
'title' => 'Lorem',
],
'permissions' => $permissions
]
);
$this->assertEquals(201, $response['headers']['status-code']);
2021-10-08 12:49:45 +00:00
$response = $this->client->call(
Client::METHOD_POST,
$this->getRecordUrl($databaseId, $collections['doconly']),
$this->getServerHeader(),
[
$this->getRecordIdParam() => ID::unique(),
'data' => [
'title' => 'Lorem',
],
'permissions' => $permissions
]
);
2022-08-24 13:32:52 +00:00
$this->assertEquals(201, $response['headers']['status-code']);
/**
* Check "any" permission collection
*/
$documents = $this->client->call(
Client::METHOD_GET,
$this->getRecordUrl($databaseId, $collections['public']),
[
'origin' => 'http://localhost',
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'cookie' => 'a_session_' . $this->getProject()['$id'] . '=' . $users['user1']['session'],
]
);
2022-08-24 13:32:52 +00:00
$this->assertEquals(200, $documents['headers']['status-code']);
$this->assertGreaterThanOrEqual($anyCount, $documents['body']['total']);
2022-08-24 13:32:52 +00:00
2021-10-08 12:49:45 +00:00
/**
2022-08-24 13:32:52 +00:00
* Check "users" permission collection
2021-10-08 12:49:45 +00:00
*/
$documents = $this->client->call(
Client::METHOD_GET,
$this->getRecordUrl($databaseId, $collections['private']),
[
'origin' => 'http://localhost',
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'cookie' => 'a_session_' . $this->getProject()['$id'] . '=' . $users['user1']['session'],
]
);
2021-10-08 12:49:45 +00:00
2022-08-24 13:32:52 +00:00
$this->assertEquals(200, $documents['headers']['status-code']);
$this->assertGreaterThanOrEqual($usersCount, $documents['body']['total']);
2021-10-08 12:49:45 +00:00
/**
2022-08-24 13:32:52 +00:00
* Check "user:user1" document only permission collection
2021-10-08 12:49:45 +00:00
*/
$documents = $this->client->call(
Client::METHOD_GET,
$this->getRecordUrl($databaseId, $collections['doconly']),
[
'origin' => 'http://localhost',
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'cookie' => 'a_session_' . $this->getProject()['$id'] . '=' . $users['user1']['session'],
]
);
2021-10-08 12:49:45 +00:00
2022-08-24 13:32:52 +00:00
$this->assertEquals(200, $documents['headers']['status-code']);
$this->assertGreaterThanOrEqual($docOnlyCount, $documents['body']['total']);
2021-10-08 12:49:45 +00:00
}
}