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 DatabasesPermissionsGuestTest 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
|
|
|
|
|
|
|
public function createCollection(): array
|
|
|
|
{
|
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']
|
|
|
|
]), [
|
|
|
|
'databaseId' => 'unique()',
|
|
|
|
'name' => 'InvalidDocumentDatabase',
|
|
|
|
]);
|
|
|
|
$this->assertEquals(201, $database['headers']['status-code']);
|
|
|
|
$this->assertEquals('InvalidDocumentDatabase', $database['body']['name']);
|
|
|
|
|
|
|
|
$databaseId = $database['body']['$id'];
|
|
|
|
$movies = $this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/collections', $this->getServerHeader(), [
|
2021-10-09 01:49:45 +13:00
|
|
|
'collectionId' => 'unique()',
|
|
|
|
'name' => 'Movies',
|
|
|
|
'read' => ['role:all'],
|
|
|
|
'write' => ['role:all'],
|
|
|
|
'permission' => 'document',
|
|
|
|
]);
|
|
|
|
|
|
|
|
$collection = ['id' => $movies['body']['$id']];
|
|
|
|
|
2022-06-22 22:51:49 +12:00
|
|
|
$this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/collections/' . $collection['id'] . '/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,
|
|
|
|
]);
|
|
|
|
|
|
|
|
sleep(2);
|
|
|
|
|
2022-06-22 22:51:49 +12:00
|
|
|
return ['collectionId' => $collection['id'], 'databaseId' => $databaseId];
|
2021-10-09 01:49:45 +13:00
|
|
|
}
|
|
|
|
|
2021-10-22 13:38:27 +13:00
|
|
|
/**
|
|
|
|
* [string[] $read, string[] $write]
|
|
|
|
*/
|
|
|
|
public function readDocumentsProvider()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
[['role:all'], []],
|
|
|
|
[['role:member'], []],
|
|
|
|
[[] ,['role:all']],
|
|
|
|
[['role:all'], ['role:all']],
|
|
|
|
[['role:member'], ['role:member']],
|
|
|
|
[['role:all'], ['role:member']],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider readDocumentsProvider
|
|
|
|
*/
|
|
|
|
public function testReadDocuments($read, $write)
|
2021-10-09 01:49:45 +13:00
|
|
|
{
|
2022-06-22 22:51:49 +12:00
|
|
|
$data = $this->createCollection();
|
|
|
|
$collectionId = $data['collectionId'];
|
|
|
|
$databaseId = $data['databaseId'];
|
|
|
|
$response = $this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/collections/' . $collectionId . '/documents', $this->getServerHeader(), [
|
2021-10-22 13:38:27 +13:00
|
|
|
'documentId' => 'unique()',
|
|
|
|
'data' => [
|
|
|
|
'title' => 'Lorem',
|
|
|
|
],
|
|
|
|
'read' => $read,
|
|
|
|
'write' => $write,
|
|
|
|
]);
|
|
|
|
$this->assertEquals(201, $response['headers']['status-code']);
|
2021-10-09 01:49:45 +13:00
|
|
|
|
2022-06-22 22:51:49 +12:00
|
|
|
$documents = $this->client->call(Client::METHOD_GET, '/databases/' . $databaseId . '/collections/' . $collectionId . '/documents', [
|
2021-10-09 01:49:45 +13:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
]);
|
|
|
|
|
|
|
|
foreach ($documents['body']['documents'] as $document) {
|
|
|
|
$this->assertContains('role:all', $document['$read']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|