1
0
Fork 0
mirror of synced 2024-06-02 10:54:44 +12:00
appwrite/tests/e2e/Services/Database/DatabaseBase.php

968 lines
43 KiB
PHP
Raw Normal View History

2020-01-13 21:46:09 +13:00
<?php
namespace Tests\E2E\Services\Database;
use Tests\E2E\Client;
trait DatabaseBase
{
public function testCreateCollection():array
{
/**
* Test for SUCCESS
*/
$movies = $this->client->call(Client::METHOD_POST, '/database/collections', array_merge([
2020-01-13 21:46:09 +13:00
'content-type' => 'application/json',
2020-02-17 20:16:11 +13:00
'x-appwrite-project' => $this->getProject()['$id'],
2020-01-13 21:46:09 +13:00
'x-appwrite-key' => $this->getProject()['apiKey']
]), [
'name' => 'Movies',
'read' => ['role:all'],
'write' => ['role:all'],
2020-01-13 21:46:09 +13:00
]);
$this->assertEquals($movies['headers']['status-code'], 201);
$this->assertEquals($movies['body']['name'], 'Movies');
2020-01-13 21:46:09 +13:00
return ['moviesId' => $movies['body']['$id']];
}
/**
* @depends testCreateCollection
*/
public function testCreateAttributes(array $data): array
{
2021-07-22 03:05:18 +12:00
$title = $this->client->call(Client::METHOD_POST, '/database/collections/' . $data['moviesId'] . '/attributes/string', array_merge([
2020-01-13 21:46:09 +13:00
'content-type' => 'application/json',
2020-02-17 20:16:11 +13:00
'x-appwrite-project' => $this->getProject()['$id'],
2020-01-13 21:46:09 +13:00
'x-appwrite-key' => $this->getProject()['apiKey']
]), [
2021-07-22 03:05:18 +12:00
'attributeId' => 'title',
'size' => 256,
'required' => true,
2020-01-13 21:46:09 +13:00
]);
2021-07-22 03:05:18 +12:00
$releaseYear = $this->client->call(Client::METHOD_POST, '/database/collections/' . $data['moviesId'] . '/attributes/integer', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]), [
2021-07-22 03:05:18 +12:00
'attributeId' => 'releaseYear',
'size' => 0,
'required' => true,
]);
2020-01-13 21:46:09 +13:00
2021-07-22 03:05:18 +12:00
$actors = $this->client->call(Client::METHOD_POST, '/database/collections/' . $data['moviesId'] . '/attributes/string', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]), [
2021-07-22 03:05:18 +12:00
'attributeId' => 'actors',
'size' => 256,
'required' => false,
2021-07-03 05:29:03 +12:00
'default' => null,
'array' => true,
]);
2021-06-15 07:54:19 +12:00
$this->assertEquals($title['headers']['status-code'], 201);
$this->assertEquals($title['body']['$collection'], $data['moviesId']);
$this->assertEquals($title['body']['$id'], 'title');
$this->assertEquals($title['body']['type'], 'string');
$this->assertEquals($title['body']['size'], 256);
$this->assertEquals($title['body']['required'], true);
$this->assertEquals($releaseYear['headers']['status-code'], 201);
$this->assertEquals($releaseYear['body']['$collection'], $data['moviesId']);
$this->assertEquals($releaseYear['body']['$id'], 'releaseYear');
$this->assertEquals($releaseYear['body']['type'], 'integer');
$this->assertEquals($releaseYear['body']['size'], 0);
$this->assertEquals($releaseYear['body']['required'], true);
$this->assertEquals($actors['headers']['status-code'], 201);
$this->assertEquals($actors['body']['$collection'], $data['moviesId']);
$this->assertEquals($actors['body']['$id'], 'actors');
$this->assertEquals($actors['body']['type'], 'string');
$this->assertEquals($actors['body']['size'], 256);
$this->assertEquals($actors['body']['required'], false);
$this->assertEquals($actors['body']['array'], true);
// wait for database worker to create attributes
2021-07-03 10:22:36 +12:00
sleep(10);
$movies = $this->client->call(Client::METHOD_GET, '/database/collections/' . $data['moviesId'], array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]), []);
$this->assertEquals($movies['body']['$id'], $title['body']['$collection']);
$this->assertEquals($movies['body']['$id'], $releaseYear['body']['$collection']);
$this->assertEquals($movies['body']['$id'], $actors['body']['$collection']);
2021-07-03 10:22:36 +12:00
$this->assertIsArray($movies['body']['attributesInQueue']);
$this->assertCount(0, $movies['body']['attributesInQueue']);
$this->assertIsArray($movies['body']['attributes']);
$this->assertCount(3, $movies['body']['attributes']);
$this->assertEquals($movies['body']['attributes'][0]['$id'], $title['body']['$id']);
$this->assertEquals($movies['body']['attributes'][1]['$id'], $releaseYear['body']['$id']);
$this->assertEquals($movies['body']['attributes'][2]['$id'], $actors['body']['$id']);
return $data;
2020-01-13 21:46:09 +13:00
}
/**
* @depends testCreateAttributes
2020-01-13 21:46:09 +13:00
*/
2021-06-15 07:54:49 +12:00
public function testCreateIndexes(array $data): array
{
$titleIndex = $this->client->call(Client::METHOD_POST, '/database/collections/' . $data['moviesId'] . '/indexes', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]), [
'id' => 'titleIndex',
'type' => 'fulltext',
'attributes' => ['title'],
]);
$this->assertEquals($titleIndex['headers']['status-code'], 201);
$this->assertEquals($titleIndex['body']['$collection'], $data['moviesId']);
$this->assertEquals($titleIndex['body']['$id'], 'titleIndex');
$this->assertEquals($titleIndex['body']['type'], 'fulltext');
$this->assertCount(1, $titleIndex['body']['attributes']);
$this->assertEquals($titleIndex['body']['attributes'][0], 'title');
// wait for database worker to create index
sleep(5);
$movies = $this->client->call(Client::METHOD_GET, '/database/collections/' . $data['moviesId'], array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]), []);
$this->assertEquals($movies['body']['$id'], $titleIndex['body']['$collection']);
$this->assertIsArray($movies['body']['indexes']);
$this->assertCount(1, $movies['body']['indexes']);
$this->assertEquals($movies['body']['indexes'][0]['$id'], $titleIndex['body']['$id']);
2021-06-15 07:54:49 +12:00
return $data;
}
/**
* @depends testCreateIndexes
*/
2020-01-13 21:46:09 +13:00
public function testCreateDocument(array $data):array
{
2020-01-31 06:22:58 +13:00
$document1 = $this->client->call(Client::METHOD_POST, '/database/collections/' . $data['moviesId'] . '/documents', array_merge([
2020-01-13 21:46:09 +13:00
'content-type' => 'application/json',
2020-02-17 20:16:11 +13:00
'x-appwrite-project' => $this->getProject()['$id'],
2020-01-13 21:46:09 +13:00
], $this->getHeaders()), [
'data' => [
'title' => 'Captain America',
2020-01-13 21:46:09 +13:00
'releaseYear' => 1944,
'actors' => [
'Chris Evans',
'Samuel Jackson',
2020-01-13 21:46:09 +13:00
]
2020-01-14 06:53:22 +13:00
],
2020-02-17 20:16:11 +13:00
'read' => ['user:'.$this->getUser()['$id']],
'write' => ['user:'.$this->getUser()['$id']],
2020-01-13 21:46:09 +13:00
]);
2020-01-31 06:22:58 +13:00
$document2 = $this->client->call(Client::METHOD_POST, '/database/collections/' . $data['moviesId'] . '/documents', array_merge([
2020-01-13 21:46:09 +13:00
'content-type' => 'application/json',
2020-02-17 20:16:11 +13:00
'x-appwrite-project' => $this->getProject()['$id'],
2020-01-13 21:46:09 +13:00
], $this->getHeaders()), [
'data' => [
'title' => 'Spider-Man: Far From Home',
2020-01-13 21:46:09 +13:00
'releaseYear' => 2019,
'actors' => [
'Tom Holland',
'Zendaya Maree Stoermer',
'Samuel Jackson',
2020-01-13 21:46:09 +13:00
]
2020-01-14 06:53:22 +13:00
],
2020-02-17 20:16:11 +13:00
'read' => ['user:'.$this->getUser()['$id']],
'write' => ['user:'.$this->getUser()['$id']],
2020-01-13 21:46:09 +13:00
]);
2020-01-31 06:22:58 +13:00
$document3 = $this->client->call(Client::METHOD_POST, '/database/collections/' . $data['moviesId'] . '/documents', array_merge([
2020-01-13 21:46:09 +13:00
'content-type' => 'application/json',
2020-02-17 20:16:11 +13:00
'x-appwrite-project' => $this->getProject()['$id'],
2020-01-13 21:46:09 +13:00
], $this->getHeaders()), [
'data' => [
'title' => 'Spider-Man: Homecoming',
2020-01-13 21:46:09 +13:00
'releaseYear' => 2017,
'actors' => [
'Tom Holland',
'Zendaya Maree Stoermer',
2020-01-13 21:46:09 +13:00
],
2020-01-14 06:53:22 +13:00
],
2020-02-17 20:16:11 +13:00
'read' => ['user:'.$this->getUser()['$id']],
'write' => ['user:'.$this->getUser()['$id']],
2020-01-13 21:46:09 +13:00
]);
2020-01-31 06:22:58 +13:00
$document4 = $this->client->call(Client::METHOD_POST, '/database/collections/' . $data['moviesId'] . '/documents', array_merge([
2020-01-13 21:46:09 +13:00
'content-type' => 'application/json',
2020-02-17 20:16:11 +13:00
'x-appwrite-project' => $this->getProject()['$id'],
2020-01-13 21:46:09 +13:00
], $this->getHeaders()), [
'data' => [
'releaseYear' => 2020, // Missing title, expect an 400 error
2020-01-14 06:53:22 +13:00
],
2020-02-17 20:16:11 +13:00
'read' => ['user:'.$this->getUser()['$id']],
'write' => ['user:'.$this->getUser()['$id']],
2020-01-13 21:46:09 +13:00
]);
$this->assertEquals($document1['headers']['status-code'], 201);
$this->assertEquals($document1['body']['$collection'], $data['moviesId']);
$this->assertEquals($document1['body']['title'], 'Captain America');
2020-01-13 21:46:09 +13:00
$this->assertEquals($document1['body']['releaseYear'], 1944);
$this->assertIsArray($document1['body']['$read']);
$this->assertIsArray($document1['body']['$write']);
$this->assertCount(1, $document1['body']['$read']);
$this->assertCount(1, $document1['body']['$write']);
2020-01-13 21:46:09 +13:00
$this->assertCount(2, $document1['body']['actors']);
$this->assertEquals($document1['body']['actors'][0], 'Chris Evans');
$this->assertEquals($document1['body']['actors'][1], 'Samuel Jackson');
2020-01-13 21:46:09 +13:00
$this->assertEquals($document2['headers']['status-code'], 201);
$this->assertEquals($document2['body']['$collection'], $data['moviesId']);
$this->assertEquals($document2['body']['title'], 'Spider-Man: Far From Home');
2020-01-13 21:46:09 +13:00
$this->assertEquals($document2['body']['releaseYear'], 2019);
$this->assertIsArray($document2['body']['$read']);
$this->assertIsArray($document2['body']['$write']);
$this->assertCount(1, $document2['body']['$read']);
$this->assertCount(1, $document2['body']['$write']);
2020-01-13 21:46:09 +13:00
$this->assertCount(3, $document2['body']['actors']);
$this->assertEquals($document2['body']['actors'][0], 'Tom Holland');
$this->assertEquals($document2['body']['actors'][1], 'Zendaya Maree Stoermer');
$this->assertEquals($document2['body']['actors'][2], 'Samuel Jackson');
2020-01-13 21:46:09 +13:00
$this->assertEquals($document3['headers']['status-code'], 201);
$this->assertEquals($document3['body']['$collection'], $data['moviesId']);
$this->assertEquals($document3['body']['title'], 'Spider-Man: Homecoming');
2020-01-13 21:46:09 +13:00
$this->assertEquals($document3['body']['releaseYear'], 2017);
$this->assertIsArray($document3['body']['$read']);
$this->assertIsArray($document3['body']['$write']);
$this->assertCount(1, $document3['body']['$read']);
$this->assertCount(1, $document3['body']['$write']);
2020-01-13 21:46:09 +13:00
$this->assertCount(2, $document3['body']['actors']);
$this->assertEquals($document2['body']['actors'][0], 'Tom Holland');
$this->assertEquals($document2['body']['actors'][1], 'Zendaya Maree Stoermer');
2020-01-13 21:46:09 +13:00
$this->assertEquals($document4['headers']['status-code'], 400);
return $data;
}
/**
* @depends testCreateDocument
*/
public function testListDocuments(array $data):array
{
2020-01-31 06:22:58 +13:00
$documents = $this->client->call(Client::METHOD_GET, '/database/collections/' . $data['moviesId'] . '/documents', array_merge([
2020-01-13 21:46:09 +13:00
'content-type' => 'application/json',
2020-02-17 20:16:11 +13:00
'x-appwrite-project' => $this->getProject()['$id'],
2020-01-13 21:46:09 +13:00
], $this->getHeaders()), [
2021-06-15 07:55:36 +12:00
'orderAttributes' => ['releaseYear'],
'orderTypes' => ['ASC'],
2020-01-13 21:46:09 +13:00
]);
$this->assertEquals(1944, $documents['body']['documents'][0]['releaseYear']);
$this->assertEquals(2017, $documents['body']['documents'][1]['releaseYear']);
$this->assertEquals(2019, $documents['body']['documents'][2]['releaseYear']);
$this->assertCount(3, $documents['body']['documents']);
2020-01-31 06:22:58 +13:00
$documents = $this->client->call(Client::METHOD_GET, '/database/collections/' . $data['moviesId'] . '/documents', array_merge([
2020-01-13 21:46:09 +13:00
'content-type' => 'application/json',
2020-02-17 20:16:11 +13:00
'x-appwrite-project' => $this->getProject()['$id'],
2020-01-13 21:46:09 +13:00
], $this->getHeaders()), [
2021-06-15 07:55:36 +12:00
'orderAttributes' => ['releaseYear'],
'orderTypes' => ['DESC'],
2020-01-13 21:46:09 +13:00
]);
$this->assertEquals(1944, $documents['body']['documents'][2]['releaseYear']);
$this->assertEquals(2017, $documents['body']['documents'][1]['releaseYear']);
$this->assertEquals(2019, $documents['body']['documents'][0]['releaseYear']);
$this->assertCount(3, $documents['body']['documents']);
return [];
}
/**
* @depends testCreateDocument
*/
public function testListDocumentsLimitAndOffset(array $data):array
{
2020-01-31 06:22:58 +13:00
$documents = $this->client->call(Client::METHOD_GET, '/database/collections/' . $data['moviesId'] . '/documents', array_merge([
2020-01-13 21:46:09 +13:00
'content-type' => 'application/json',
2020-02-17 20:16:11 +13:00
'x-appwrite-project' => $this->getProject()['$id'],
2020-01-13 21:46:09 +13:00
], $this->getHeaders()), [
'limit' => 1,
2021-06-15 07:55:36 +12:00
'orderAttributes' => ['releaseYear'],
'orderTypes' => ['ASC'],
2020-01-13 21:46:09 +13:00
]);
$this->assertEquals(1944, $documents['body']['documents'][0]['releaseYear']);
$this->assertCount(1, $documents['body']['documents']);
2020-01-31 06:22:58 +13:00
$documents = $this->client->call(Client::METHOD_GET, '/database/collections/' . $data['moviesId'] . '/documents', array_merge([
2020-01-13 21:46:09 +13:00
'content-type' => 'application/json',
2020-02-17 20:16:11 +13:00
'x-appwrite-project' => $this->getProject()['$id'],
2020-01-13 21:46:09 +13:00
], $this->getHeaders()), [
'limit' => 2,
'offset' => 1,
2021-06-15 07:55:36 +12:00
'orderAttributes' => ['releaseYear'],
'orderTypes' => ['ASC'],
2020-01-13 21:46:09 +13:00
]);
$this->assertEquals(2017, $documents['body']['documents'][0]['releaseYear']);
$this->assertEquals(2019, $documents['body']['documents'][1]['releaseYear']);
$this->assertCount(2, $documents['body']['documents']);
return [];
}
/**
* @depends testCreateDocument
*/
public function testDocumentsListSuccessSearch(array $data):array
{
2020-01-31 06:22:58 +13:00
$documents = $this->client->call(Client::METHOD_GET, '/database/collections/' . $data['moviesId'] . '/documents', array_merge([
2020-01-13 21:46:09 +13:00
'content-type' => 'application/json',
2020-02-17 20:16:11 +13:00
'x-appwrite-project' => $this->getProject()['$id'],
2020-01-13 21:46:09 +13:00
], $this->getHeaders()), [
2021-06-15 07:55:36 +12:00
'queries' => ['title.search("Captain America")'],
2020-01-13 21:46:09 +13:00
]);
$this->assertEquals(1944, $documents['body']['documents'][0]['releaseYear']);
$this->assertCount(1, $documents['body']['documents']);
2020-01-31 06:22:58 +13:00
$documents = $this->client->call(Client::METHOD_GET, '/database/collections/' . $data['moviesId'] . '/documents', array_merge([
2020-01-13 21:46:09 +13:00
'content-type' => 'application/json',
2020-02-17 20:16:11 +13:00
'x-appwrite-project' => $this->getProject()['$id'],
2020-01-13 21:46:09 +13:00
], $this->getHeaders()), [
2021-06-15 07:55:36 +12:00
'queries' => ['title.search("Homecoming")'],
2020-01-13 21:46:09 +13:00
]);
$this->assertEquals(2017, $documents['body']['documents'][0]['releaseYear']);
$this->assertCount(1, $documents['body']['documents']);
2020-01-31 06:22:58 +13:00
$documents = $this->client->call(Client::METHOD_GET, '/database/collections/' . $data['moviesId'] . '/documents', array_merge([
2020-01-13 21:46:09 +13:00
'content-type' => 'application/json',
2020-02-17 20:16:11 +13:00
'x-appwrite-project' => $this->getProject()['$id'],
2020-01-13 21:46:09 +13:00
], $this->getHeaders()), [
2021-06-15 07:55:36 +12:00
'queries' => ['title.search("spider")'],
2020-01-13 21:46:09 +13:00
]);
$this->assertEquals(2019, $documents['body']['documents'][0]['releaseYear']);
$this->assertEquals(2017, $documents['body']['documents'][1]['releaseYear']);
$this->assertCount(2, $documents['body']['documents']);
return [];
}
2021-06-15 07:55:36 +12:00
// TODO@kodumbeats test for empty searches and misformatted queries
2020-01-13 21:46:09 +13:00
/**
* @depends testCreateDocument
*/
2021-06-15 07:55:36 +12:00
// public function testListDocumentsFilters(array $data):array
// {
// $documents = $this->client->call(Client::METHOD_GET, '/database/collections/' . $data['moviesId'] . '/documents', array_merge([
// 'content-type' => 'application/json',
// 'x-appwrite-project' => $this->getProject()['$id'],
// ], $this->getHeaders()), [
// 'filters' => [
// 'actors.firstName=Tom'
// ],
// ]);
// $this->assertCount(2, $documents['body']['documents']);
// $this->assertEquals('Spider-Man: Far From Home', $documents['body']['documents'][0]['name']);
// $this->assertEquals('Spider-Man: Homecoming', $documents['body']['documents'][1]['name']);
// $documents = $this->client->call(Client::METHOD_GET, '/database/collections/' . $data['moviesId'] . '/documents', array_merge([
// 'content-type' => 'application/json',
// 'x-appwrite-project' => $this->getProject()['$id'],
// ], $this->getHeaders()), [
// 'filters' => [
// 'releaseYear=1944'
// ],
// ]);
// $this->assertCount(1, $documents['body']['documents']);
// $this->assertEquals('Captain America', $documents['body']['documents'][0]['name']);
// $documents = $this->client->call(Client::METHOD_GET, '/database/collections/' . $data['moviesId'] . '/documents', array_merge([
// 'content-type' => 'application/json',
// 'x-appwrite-project' => $this->getProject()['$id'],
// ], $this->getHeaders()), [
// 'filters' => [
// 'releaseYear!=1944'
// ],
// ]);
// $this->assertCount(2, $documents['body']['documents']);
// $this->assertEquals('Spider-Man: Far From Home', $documents['body']['documents'][0]['name']);
// $this->assertEquals('Spider-Man: Homecoming', $documents['body']['documents'][1]['name']);
// return [];
// }
2020-01-13 21:46:09 +13:00
/**
* @depends testCreateDocument
*/
public function testUpdateDocument(array $data):array
{
2020-01-31 06:22:58 +13:00
$document = $this->client->call(Client::METHOD_POST, '/database/collections/' . $data['moviesId'] . '/documents', array_merge([
2020-01-13 21:46:09 +13:00
'content-type' => 'application/json',
2020-02-17 20:16:11 +13:00
'x-appwrite-project' => $this->getProject()['$id'],
2020-01-13 21:46:09 +13:00
], $this->getHeaders()), [
'data' => [
'title' => 'Thor: Ragnaroc',
2020-01-13 21:46:09 +13:00
'releaseYear' => 2017,
'actors' => [],
2020-01-14 06:53:22 +13:00
],
2021-07-06 08:27:20 +12:00
'read' => ['user:'.$this->getUser()['$id'], 'user:testx'],
'write' => ['user:'.$this->getUser()['$id'], 'user:testy'],
2020-01-13 21:46:09 +13:00
]);
2020-02-17 20:16:11 +13:00
$id = $document['body']['$id'];
2020-01-13 21:46:09 +13:00
$collection = $document['body']['$collection'];
$this->assertEquals($document['headers']['status-code'], 201);
$this->assertEquals($document['body']['title'], 'Thor: Ragnaroc');
2020-01-13 21:46:09 +13:00
$this->assertEquals($document['body']['releaseYear'], 2017);
2021-07-06 08:27:20 +12:00
$this->assertEquals($document['body']['$read'][1], 'user:testx');
$this->assertEquals($document['body']['$write'][1], 'user:testy');
2020-01-13 21:46:09 +13:00
2020-01-31 06:22:58 +13:00
$document = $this->client->call(Client::METHOD_PATCH, '/database/collections/' . $collection . '/documents/' . $id, array_merge([
2020-01-13 21:46:09 +13:00
'content-type' => 'application/json',
2020-02-17 20:16:11 +13:00
'x-appwrite-project' => $this->getProject()['$id'],
2020-01-13 21:46:09 +13:00
], $this->getHeaders()), [
'data' => [
'title' => 'Thor: Ragnarok',
2021-03-16 00:44:11 +13:00
],
2020-01-13 21:46:09 +13:00
]);
$this->assertEquals($document['headers']['status-code'], 200);
$this->assertEquals($document['body']['title'], 'Thor: Ragnarok');
2020-01-13 21:46:09 +13:00
$this->assertEquals($document['body']['releaseYear'], 2017);
2020-01-31 06:22:58 +13:00
$document = $this->client->call(Client::METHOD_GET, '/database/collections/' . $collection . '/documents/' . $id, array_merge([
2020-01-13 21:46:09 +13:00
'content-type' => 'application/json',
2020-02-17 20:16:11 +13:00
'x-appwrite-project' => $this->getProject()['$id'],
2020-01-13 21:46:09 +13:00
], $this->getHeaders()));
2020-02-17 20:16:11 +13:00
$id = $document['body']['$id'];
2020-01-13 21:46:09 +13:00
$collection = $document['body']['$collection'];
$this->assertEquals($document['headers']['status-code'], 200);
$this->assertEquals($document['body']['title'], 'Thor: Ragnarok');
2020-01-13 21:46:09 +13:00
$this->assertEquals($document['body']['releaseYear'], 2017);
return [];
}
/**
* @depends testCreateDocument
*/
public function testDeleteDocument(array $data):array
{
2020-01-31 06:22:58 +13:00
$document = $this->client->call(Client::METHOD_POST, '/database/collections/' . $data['moviesId'] . '/documents', array_merge([
2020-01-13 21:46:09 +13:00
'content-type' => 'application/json',
2020-02-17 20:16:11 +13:00
'x-appwrite-project' => $this->getProject()['$id'],
2020-01-13 21:46:09 +13:00
], $this->getHeaders()), [
'data' => [
'title' => 'Thor: Ragnarok',
2020-01-13 21:46:09 +13:00
'releaseYear' => 2017,
'actors' => [],
2020-01-14 06:53:22 +13:00
],
2020-02-17 20:16:11 +13:00
'read' => ['user:'.$this->getUser()['$id']],
'write' => ['user:'.$this->getUser()['$id']],
2020-01-13 21:46:09 +13:00
]);
2020-02-17 20:16:11 +13:00
$id = $document['body']['$id'];
2020-01-13 21:46:09 +13:00
$collection = $document['body']['$collection'];
$this->assertEquals($document['headers']['status-code'], 201);
2020-01-31 06:22:58 +13:00
$document = $this->client->call(Client::METHOD_GET, '/database/collections/' . $collection . '/documents/' . $id, array_merge([
2020-01-13 21:46:09 +13:00
'content-type' => 'application/json',
2020-02-17 20:16:11 +13:00
'x-appwrite-project' => $this->getProject()['$id'],
2020-01-13 21:46:09 +13:00
], $this->getHeaders()));
$this->assertEquals($document['headers']['status-code'], 200);
2020-01-31 06:22:58 +13:00
$document = $this->client->call(Client::METHOD_DELETE, '/database/collections/' . $collection . '/documents/' . $id, array_merge([
2020-01-13 21:46:09 +13:00
'content-type' => 'application/json',
2020-02-17 20:16:11 +13:00
'x-appwrite-project' => $this->getProject()['$id'],
2020-01-13 21:46:09 +13:00
], $this->getHeaders()));
$this->assertEquals($document['headers']['status-code'], 204);
2020-01-31 06:22:58 +13:00
$document = $this->client->call(Client::METHOD_GET, '/database/collections/' . $collection . '/documents/' . $id, array_merge([
2020-01-13 21:46:09 +13:00
'content-type' => 'application/json',
2020-02-17 20:16:11 +13:00
'x-appwrite-project' => $this->getProject()['$id'],
2020-01-13 21:46:09 +13:00
], $this->getHeaders()));
$this->assertEquals($document['headers']['status-code'], 404);
2021-03-22 20:34:51 +13:00
return $data;
}
public function testInvalidDocumentStructure()
{
$collection = $this->client->call(Client::METHOD_POST, '/database/collections', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]), [
'name' => 'invalidDocumentStructure',
'read' => ['role:all'],
'write' => ['role:all'],
]);
2021-07-24 03:04:31 +12:00
$this->assertEquals(201, $collection['headers']['status-code']);
$this->assertEquals('invalidDocumentStructure', $collection['body']['name']);
$collectionId = $collection['body']['$id'];
$email = $this->client->call(Client::METHOD_POST, '/database/collections/' . $collectionId . '/attributes/string', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]), [
'attributeId' => 'email',
'size' => 256,
'required' => false,
'format' => 'email',
]);
$ip = $this->client->call(Client::METHOD_POST, '/database/collections/' . $collectionId . '/attributes/string', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]), [
'attributeId' => 'ip',
'size' => 64,
'required' => false,
'format' => 'ip',
]);
$url = $this->client->call(Client::METHOD_POST, '/database/collections/' . $collectionId . '/attributes/string', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]), [
'attributeId' => 'url',
'size' => 256,
'required' => false,
'format' => 'url',
]);
$range = $this->client->call(Client::METHOD_POST, '/database/collections/' . $collectionId . '/attributes/integer', array_merge([
'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]), [
'attributeId' => 'range',
'required' => false,
'min' => 1,
'max' => 10,
]);
// TODO@kodumbeats float validator rejects 0.0 and 1.0 as floats
$floatRange = $this->client->call(Client::METHOD_POST, '/database/collections/' . $collectionId . '/attributes/float', array_merge([
'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]), [
'attributeId' => 'floatRange',
'required' => false,
'min' => 0.5,
'max' => 1.5,
]);
$upperBound = $this->client->call(Client::METHOD_POST, '/database/collections/' . $collectionId . '/attributes/integer', array_merge([
'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]), [
'attributeId' => 'upperBound',
'required' => false,
'max' => 10,
]);
$lowerBound = $this->client->call(Client::METHOD_POST, '/database/collections/' . $collectionId . '/attributes/integer', array_merge([
'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]), [
'attributeId' => 'lowerBound',
'required' => false,
'min' => 5,
]);
/**
* Test for failure
*/
2021-07-24 03:04:31 +12:00
$unsupported = $this->client->call(Client::METHOD_POST, '/database/collections/' . $collectionId . '/attributes/string', array_merge([
'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]), [
'attributeId' => 'unsupported',
'size' => 256,
'required' => false,
'format' => 'unsupported',
]);
// TODO@kodumbeats troubleshoot
// $invalidRange = $this->client->call(Client::METHOD_POST, '/database/collections/' . $collectionId . '/attributes/integer', array_merge([
// 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'],
// 'x-appwrite-key' => $this->getProject()['apiKey']
// ]), [
// 'attributeId' => 'invalidRange',
// 'required' => false,
// 'min' => 4,
// 'max' => 3,
// ]);
2021-07-24 03:04:31 +12:00
$this->assertEquals(201, $email['headers']['status-code']);
$this->assertEquals(201, $ip['headers']['status-code']);
$this->assertEquals(201, $url['headers']['status-code']);
$this->assertEquals(201, $range['headers']['status-code']);
$this->assertEquals(201, $floatRange['headers']['status-code']);
$this->assertEquals(201, $upperBound['headers']['status-code']);
$this->assertEquals(201, $lowerBound['headers']['status-code']);
2021-07-24 03:04:31 +12:00
$this->assertEquals(400, $unsupported['headers']['status-code']);
// $this->assertEquals(400, $invalidRange['headers']['status-code']);
2021-07-24 03:04:31 +12:00
$this->assertEquals('Invalid format: Value must be one of (email, ip, url)', $unsupported['body']['message']);
// $this->assertEquals('Minimum value must be lesser than maximum value', $invalidRange['body']['message']);
// wait for worker to add attributes
sleep(10);
$collection = $this->client->call(Client::METHOD_GET, '/database/collections/' . $collectionId, array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]), []);
// var_dump($collection);
$this->assertCount(7, $collection['body']['attributes']);
$this->assertCount(0, $collection['body']['attributesInQueue']);
/**
* Test for successful validation
*/
$goodEmail = $this->client->call(Client::METHOD_POST, '/database/collections/' . $collectionId . '/documents', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'data' => [
'email' => 'user@example.com',
],
'read' => ['user:'.$this->getUser()['$id']],
'write' => ['user:'.$this->getUser()['$id']],
]);
$goodIp = $this->client->call(Client::METHOD_POST, '/database/collections/' . $collectionId . '/documents', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'data' => [
'ip' => '1.1.1.1',
],
'read' => ['user:'.$this->getUser()['$id']],
'write' => ['user:'.$this->getUser()['$id']],
]);
$goodUrl = $this->client->call(Client::METHOD_POST, '/database/collections/' . $collectionId . '/documents', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'data' => [
'url' => 'http://www.example.com',
],
'read' => ['user:'.$this->getUser()['$id']],
'write' => ['user:'.$this->getUser()['$id']],
]);
$goodRange = $this->client->call(Client::METHOD_POST, '/database/collections/' . $collectionId . '/documents', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'data' => [
'range' => 3,
],
'read' => ['user:'.$this->getUser()['$id']],
'write' => ['user:'.$this->getUser()['$id']],
]);
$goodFloatRange = $this->client->call(Client::METHOD_POST, '/database/collections/' . $collectionId . '/documents', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'data' => [
'floatRange' => 0.8,
],
'read' => ['user:'.$this->getUser()['$id']],
'write' => ['user:'.$this->getUser()['$id']],
]);
$notTooHigh = $this->client->call(Client::METHOD_POST, '/database/collections/' . $collectionId . '/documents', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'data' => [
'upperBound' => 8,
],
'read' => ['user:'.$this->getUser()['$id']],
'write' => ['user:'.$this->getUser()['$id']],
]);
$notTooLow = $this->client->call(Client::METHOD_POST, '/database/collections/' . $collectionId . '/documents', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'data' => [
'lowerBound' => 8,
],
'read' => ['user:'.$this->getUser()['$id']],
'write' => ['user:'.$this->getUser()['$id']],
]);
// var_dump($notTooLow);
$this->assertEquals(201, $goodEmail['headers']['status-code']);
$this->assertEquals(201, $goodIp['headers']['status-code']);
$this->assertEquals(201, $goodUrl['headers']['status-code']);
$this->assertEquals(201, $goodRange['headers']['status-code']);
$this->assertEquals(201, $goodFloatRange['headers']['status-code']);
$this->assertEquals(201, $notTooHigh['headers']['status-code']);
$this->assertEquals(201, $notTooLow['headers']['status-code']);
/*
* Test that custom validators reject documents
*/
$badEmail = $this->client->call(Client::METHOD_POST, '/database/collections/' . $collectionId . '/documents', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'data' => [
'email' => 'user@@example.com',
],
'read' => ['user:'.$this->getUser()['$id']],
'write' => ['user:'.$this->getUser()['$id']],
]);
$badIp = $this->client->call(Client::METHOD_POST, '/database/collections/' . $collectionId . '/documents', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'data' => [
'ip' => '1.1.1.1.1',
],
'read' => ['user:'.$this->getUser()['$id']],
'write' => ['user:'.$this->getUser()['$id']],
]);
$badUrl = $this->client->call(Client::METHOD_POST, '/database/collections/' . $collectionId . '/documents', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'data' => [
'url' => 'example...com',
],
'read' => ['user:'.$this->getUser()['$id']],
'write' => ['user:'.$this->getUser()['$id']],
]);
$badRange = $this->client->call(Client::METHOD_POST, '/database/collections/' . $collectionId . '/documents', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'data' => [
'range' => 11,
],
'read' => ['user:'.$this->getUser()['$id']],
'write' => ['user:'.$this->getUser()['$id']],
]);
$badProbability = $this->client->call(Client::METHOD_POST, '/database/collections/' . $collectionId . '/documents', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'data' => [
'floatRange' => 2.5,
],
'read' => ['user:'.$this->getUser()['$id']],
'write' => ['user:'.$this->getUser()['$id']],
]);
$tooHigh = $this->client->call(Client::METHOD_POST, '/database/collections/' . $collectionId . '/documents', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'data' => [
'upperBound' => 11,
],
'read' => ['user:'.$this->getUser()['$id']],
'write' => ['user:'.$this->getUser()['$id']],
]);
$tooLow = $this->client->call(Client::METHOD_POST, '/database/collections/' . $collectionId . '/documents', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'data' => [
'lowerBound' => 3,
],
'read' => ['user:'.$this->getUser()['$id']],
'write' => ['user:'.$this->getUser()['$id']],
]);
$this->assertEquals(400, $badEmail['headers']['status-code']);
$this->assertEquals(400, $badIp['headers']['status-code']);
$this->assertEquals(400, $badUrl['headers']['status-code']);
$this->assertEquals(400, $badRange['headers']['status-code']);
$this->assertEquals(400, $badProbability['headers']['status-code']);
$this->assertEquals(400, $tooHigh['headers']['status-code']);
$this->assertEquals(400, $tooLow['headers']['status-code']);
$this->assertEquals('Invalid document structure: Attribute "email" has invalid format. Value must be a valid email address', $badEmail['body']['message']);
$this->assertEquals('Invalid document structure: Attribute "ip" has invalid format. Value must be a valid IP address', $badIp['body']['message']);
$this->assertEquals('Invalid document structure: Attribute "url" has invalid format. Value must be a valid URL', $badUrl['body']['message']);
$this->assertEquals('Invalid document structure: Attribute "range" has invalid format. Value must be a valid range between 1 and 10', $badRange['body']['message']);
$this->assertEquals('Invalid document structure: Attribute "floatRange" has invalid format. Value must be a valid range between 1 and 2', $badProbability['body']['message']);
$this->assertEquals('Invalid document structure: Attribute "upperBound" has invalid format. Value must be a valid range between inf and 10', $tooHigh['body']['message']);
$this->assertEquals('Invalid document structure: Attribute "lowerBound" has invalid format. Value must be a valid range between 5 and inf', $tooLow['body']['message']);
}
2021-03-22 20:34:51 +13:00
/**
* @depends testDeleteDocument
*/
public function testDefaultPermissions(array $data):array
{
$document = $this->client->call(Client::METHOD_POST, '/database/collections/' . $data['moviesId'] . '/documents', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'data' => [
2021-06-16 02:24:51 +12:00
'title' => 'Captain America',
2021-03-22 20:34:51 +13:00
'releaseYear' => 1944,
'actors' => [],
],
]);
$id = $document['body']['$id'];
$this->assertEquals($document['headers']['status-code'], 201);
$this->assertEquals($document['body']['$collection'], $data['moviesId']);
2021-06-16 02:24:51 +12:00
$this->assertEquals($document['body']['title'], 'Captain America');
2021-03-22 20:34:51 +13:00
$this->assertEquals($document['body']['releaseYear'], 1944);
2021-06-16 02:24:51 +12:00
$this->assertIsArray($document['body']['$read']);
$this->assertIsArray($document['body']['$write']);
2021-03-22 20:34:51 +13:00
if($this->getSide() == 'client') {
2021-06-16 02:24:51 +12:00
$this->assertCount(1, $document['body']['$read']);
$this->assertCount(1, $document['body']['$write']);
$this->assertEquals(['user:'.$this->getUser()['$id']], $document['body']['$read']);
$this->assertEquals(['user:'.$this->getUser()['$id']], $document['body']['$write']);
2021-03-22 20:34:51 +13:00
}
if($this->getSide() == 'server') {
2021-06-16 02:24:51 +12:00
$this->assertCount(0, $document['body']['$read']);
$this->assertCount(0, $document['body']['$write']);
$this->assertEquals([], $document['body']['$read']);
$this->assertEquals([], $document['body']['$write']);
2021-03-22 20:34:51 +13:00
}
// Updated and Inherit Permissions
$document = $this->client->call(Client::METHOD_PATCH, '/database/collections/' . $data['moviesId'] . '/documents/' . $id, array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'data' => [
2021-06-16 02:24:51 +12:00
'title' => 'Captain America 2',
2021-03-22 20:34:51 +13:00
'releaseYear' => 1945,
'actors' => [],
],
2021-06-12 06:23:16 +12:00
'read' => ['role:all'],
2021-03-22 20:34:51 +13:00
]);
$this->assertEquals($document['headers']['status-code'], 200);
2021-06-16 02:24:51 +12:00
$this->assertEquals($document['body']['title'], 'Captain America 2');
2021-03-22 20:34:51 +13:00
$this->assertEquals($document['body']['releaseYear'], 1945);
if($this->getSide() == 'client') {
2021-06-16 02:24:51 +12:00
$this->assertCount(1, $document['body']['$read']);
$this->assertCount(1, $document['body']['$write']);
$this->assertEquals(['role:all'], $document['body']['$read']);
$this->assertEquals(['user:'.$this->getUser()['$id']], $document['body']['$write']);
2021-03-22 20:34:51 +13:00
}
if($this->getSide() == 'server') {
2021-06-16 02:24:51 +12:00
$this->assertCount(1, $document['body']['$read']);
$this->assertCount(0, $document['body']['$write']);
$this->assertEquals(['role:all'], $document['body']['$read']);
$this->assertEquals([], $document['body']['$write']);
2021-03-22 20:34:51 +13:00
}
$document = $this->client->call(Client::METHOD_GET, '/database/collections/' . $data['moviesId'] . '/documents/' . $id, array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()));
$this->assertEquals($document['headers']['status-code'], 200);
2021-06-16 02:24:51 +12:00
$this->assertEquals($document['body']['title'], 'Captain America 2');
2021-03-22 20:34:51 +13:00
$this->assertEquals($document['body']['releaseYear'], 1945);
if($this->getSide() == 'client') {
2021-06-16 02:24:51 +12:00
$this->assertCount(1, $document['body']['$read']);
$this->assertCount(1, $document['body']['$write']);
$this->assertEquals(['role:all'], $document['body']['$read']);
$this->assertEquals(['user:'.$this->getUser()['$id']], $document['body']['$write']);
2021-03-22 20:34:51 +13:00
}
if($this->getSide() == 'server') {
2021-06-16 02:24:51 +12:00
$this->assertCount(1, $document['body']['$read']);
$this->assertCount(0, $document['body']['$write']);
$this->assertEquals(['role:all'], $document['body']['$read']);
$this->assertEquals([], $document['body']['$write']);
2021-03-22 20:34:51 +13:00
}
// Reset Permissions
$document = $this->client->call(Client::METHOD_PATCH, '/database/collections/' . $data['moviesId'] . '/documents/' . $id, array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'data' => [
2021-06-16 02:24:51 +12:00
'title' => 'Captain America 3',
2021-03-22 20:34:51 +13:00
'releaseYear' => 1946,
'actors' => [],
],
'read' => [],
'write' => [],
]);
if($this->getSide() == 'client') {
$this->assertEquals($document['headers']['status-code'], 401);
}
if($this->getSide() == 'server') {
$this->assertEquals($document['headers']['status-code'], 200);
2021-06-16 02:24:51 +12:00
$this->assertEquals($document['body']['title'], 'Captain America 3');
2021-03-22 20:34:51 +13:00
$this->assertEquals($document['body']['releaseYear'], 1946);
2021-06-16 02:24:51 +12:00
$this->assertCount(0, $document['body']['$read']);
$this->assertCount(0, $document['body']['$write']);
$this->assertEquals([], $document['body']['$read']);
$this->assertEquals([], $document['body']['$write']);
2021-03-22 20:34:51 +13:00
}
return $data;
2020-01-13 21:46:09 +13:00
}
}