2020-01-13 10:28:26 +13:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\E2E\Services\Storage;
|
|
|
|
|
2021-09-07 01:33:06 +12:00
|
|
|
use CURLFile;
|
2021-12-11 05:48:54 +13:00
|
|
|
use Exception;
|
|
|
|
use SebastianBergmann\RecursionContext\InvalidArgumentException;
|
|
|
|
use PHPUnit\Framework\ExpectationFailedException;
|
2021-09-07 01:33:06 +12:00
|
|
|
use Tests\E2E\Client;
|
2020-01-13 10:28:26 +13:00
|
|
|
use Tests\E2E\Scopes\Scope;
|
|
|
|
use Tests\E2E\Scopes\ProjectCustom;
|
|
|
|
use Tests\E2E\Scopes\SideClient;
|
|
|
|
|
|
|
|
class StorageCustomClientTest extends Scope
|
|
|
|
{
|
|
|
|
use StorageBase;
|
|
|
|
use ProjectCustom;
|
|
|
|
use SideClient;
|
2021-09-07 01:33:06 +12:00
|
|
|
|
2022-03-15 22:51:51 +13:00
|
|
|
public function testBucketPermissions(): void
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Test for SUCCESS
|
|
|
|
*/
|
|
|
|
$bucket = $this->client->call(Client::METHOD_POST, '/storage/buckets', [
|
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey'],
|
|
|
|
], [
|
|
|
|
'bucketId' => 'unique()',
|
|
|
|
'name' => 'Test Bucket',
|
2022-08-03 16:17:49 +12:00
|
|
|
'permissions' => [
|
|
|
|
'read(any)',
|
2022-08-09 18:58:08 +12:00
|
|
|
'create(users)',
|
|
|
|
'update(users)',
|
|
|
|
'delete(users)',
|
2022-08-03 16:17:49 +12:00
|
|
|
],
|
2022-03-15 22:51:51 +13:00
|
|
|
]);
|
|
|
|
|
|
|
|
$bucketId = $bucket['body']['$id'];
|
|
|
|
$this->assertEquals(201, $bucket['headers']['status-code']);
|
|
|
|
$this->assertNotEmpty($bucketId);
|
|
|
|
|
2022-05-24 02:54:50 +12:00
|
|
|
$file = $this->client->call(Client::METHOD_POST, '/storage/buckets/' . $bucketId . '/files', array_merge([
|
2022-03-15 22:51:51 +13:00
|
|
|
'content-type' => 'multipart/form-data',
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
], $this->getHeaders()), [
|
|
|
|
'fileId' => 'unique()',
|
|
|
|
'file' => new CURLFile(realpath(__DIR__ . '/../../../resources/logo.png'), 'image/png', 'permissions.png'),
|
|
|
|
]);
|
|
|
|
|
|
|
|
$fileId = $file['body']['$id'];
|
|
|
|
$this->assertEquals($file['headers']['status-code'], 201);
|
|
|
|
$this->assertNotEmpty($fileId);
|
2022-08-13 02:41:57 +12:00
|
|
|
$this->assertIsInt($file['body']['$createdAt']);
|
2022-03-15 22:51:51 +13:00
|
|
|
$this->assertEquals('permissions.png', $file['body']['name']);
|
|
|
|
$this->assertEquals('image/png', $file['body']['mimeType']);
|
|
|
|
$this->assertEquals(47218, $file['body']['sizeOriginal']);
|
|
|
|
|
|
|
|
$file = $this->client->call(Client::METHOD_GET, '/storage/buckets/' . $bucketId . '/files/' . $fileId, array_merge([
|
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
], $this->getHeaders()));
|
|
|
|
|
|
|
|
$this->assertEquals(200, $file['headers']['status-code']);
|
|
|
|
|
|
|
|
$file = $this->client->call(Client::METHOD_GET, '/storage/buckets/' . $bucketId . '/files/' . $fileId . '/preview', array_merge([
|
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
], $this->getHeaders()));
|
|
|
|
|
|
|
|
$this->assertEquals(200, $file['headers']['status-code']);
|
|
|
|
|
|
|
|
$file = $this->client->call(Client::METHOD_GET, '/storage/buckets/' . $bucketId . '/files/' . $fileId . '/download', array_merge([
|
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
], $this->getHeaders()));
|
|
|
|
|
|
|
|
$this->assertEquals(200, $file['headers']['status-code']);
|
|
|
|
|
|
|
|
$file = $this->client->call(Client::METHOD_GET, '/storage/buckets/' . $bucketId . '/files/' . $fileId . '/view', array_merge([
|
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
], $this->getHeaders()));
|
|
|
|
|
|
|
|
$this->assertEquals(200, $file['headers']['status-code']);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test for FAILURE
|
|
|
|
*/
|
2022-05-24 02:54:50 +12:00
|
|
|
$file = $this->client->call(Client::METHOD_POST, '/storage/buckets/' . $bucketId . '/files', [
|
2022-03-15 22:51:51 +13:00
|
|
|
'content-type' => 'multipart/form-data',
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
], [
|
|
|
|
'fileId' => 'unique()',
|
|
|
|
'file' => new CURLFile(realpath(__DIR__ . '/../../../resources/logo.png'), 'image/png', 'permissions.png'),
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertEquals($file['headers']['status-code'], 401);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test for SUCCESS
|
|
|
|
*/
|
|
|
|
$file = $this->client->call(Client::METHOD_DELETE, '/storage/buckets/' . $bucketId . '/files/' . $fileId, array_merge([
|
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
], $this->getHeaders()));
|
|
|
|
|
|
|
|
$this->assertEquals(204, $file['headers']['status-code']);
|
|
|
|
$this->assertEmpty($file['body']);
|
|
|
|
}
|
|
|
|
|
2021-12-11 05:48:54 +13:00
|
|
|
public function testCreateFileDefaultPermissions(): array
|
2021-09-07 01:33:06 +12:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Test for SUCCESS
|
|
|
|
*/
|
2022-02-16 21:30:09 +13:00
|
|
|
$bucket = $this->client->call(Client::METHOD_POST, '/storage/buckets', [
|
2021-10-17 20:12:59 +13:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey'],
|
2022-02-16 21:30:09 +13:00
|
|
|
], [
|
2021-10-17 20:12:59 +13:00
|
|
|
'bucketId' => 'unique()',
|
|
|
|
'name' => 'Test Bucket',
|
2022-08-02 21:19:15 +12:00
|
|
|
'fileSecurity' => true,
|
|
|
|
'permissions' => [
|
|
|
|
'read(any)',
|
2022-08-05 17:23:18 +12:00
|
|
|
'create(any)',
|
|
|
|
'update(any)',
|
|
|
|
'delete(any)',
|
2022-08-02 21:19:15 +12:00
|
|
|
],
|
2021-10-17 20:12:59 +13:00
|
|
|
]);
|
|
|
|
$this->assertEquals(201, $bucket['headers']['status-code']);
|
|
|
|
$this->assertNotEmpty($bucket['body']['$id']);
|
|
|
|
|
2022-05-24 02:54:50 +12:00
|
|
|
$file = $this->client->call(Client::METHOD_POST, '/storage/buckets/' . $bucket['body']['$id'] . '/files', array_merge([
|
2021-09-07 01:33:06 +12:00
|
|
|
'content-type' => 'multipart/form-data',
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
], $this->getHeaders()), [
|
2021-10-08 21:39:37 +13:00
|
|
|
'fileId' => 'unique()',
|
|
|
|
'file' => new CURLFile(realpath(__DIR__ . '/../../../resources/logo.png'), 'image/png', 'permissions.png'),
|
2021-09-07 01:33:06 +12:00
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertEquals($file['headers']['status-code'], 201);
|
|
|
|
$this->assertNotEmpty($file['body']['$id']);
|
2022-08-09 19:11:30 +12:00
|
|
|
$this->assertContains('read(user:' . $this->getUser()['$id'] . ')', $file['body']['$permissions']);
|
|
|
|
$this->assertContains('update(user:' . $this->getUser()['$id'] . ')', $file['body']['$permissions']);
|
|
|
|
$this->assertContains('delete(user:' . $this->getUser()['$id'] . ')', $file['body']['$permissions']);
|
2022-08-13 02:41:57 +12:00
|
|
|
$this->assertIsInt($file['body']['$createdAt']);
|
2021-10-08 21:39:37 +13:00
|
|
|
$this->assertEquals('permissions.png', $file['body']['name']);
|
2021-09-07 01:33:06 +12:00
|
|
|
$this->assertEquals('image/png', $file['body']['mimeType']);
|
|
|
|
$this->assertEquals(47218, $file['body']['sizeOriginal']);
|
2021-12-11 05:48:54 +13:00
|
|
|
|
2021-12-14 22:42:39 +13:00
|
|
|
return ['fileId' => $file['body']['$id'], 'bucketId' => $bucket['body']['$id']];
|
2021-12-11 05:48:54 +13:00
|
|
|
}
|
|
|
|
|
2021-12-14 22:42:39 +13:00
|
|
|
/**
|
|
|
|
* @depends testCreateFileDefaultPermissions
|
|
|
|
*/
|
|
|
|
public function testCreateFileAbusePermissions(array $data): void
|
2021-12-11 05:48:54 +13:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Test for FAILURE
|
|
|
|
*/
|
2021-12-14 22:42:39 +13:00
|
|
|
$file = $this->client->call(Client::METHOD_POST, '/storage/buckets/' . $data['bucketId'] . '/files', array_merge([
|
2021-12-11 05:48:54 +13:00
|
|
|
'content-type' => 'multipart/form-data',
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
], $this->getHeaders()), [
|
|
|
|
'fileId' => 'unique()',
|
|
|
|
'file' => new CURLFile(realpath(__DIR__ . '/../../../resources/logo.png'), 'image/png', 'permissions.png'),
|
|
|
|
'folderId' => 'xyz',
|
2022-08-03 16:17:49 +12:00
|
|
|
'permissions' => [
|
|
|
|
'read(user:notme)',
|
|
|
|
],
|
2021-12-11 05:48:54 +13:00
|
|
|
]);
|
|
|
|
|
2021-12-14 22:42:39 +13:00
|
|
|
$this->assertEquals(400, $file['headers']['status-code']);
|
2022-08-09 19:11:30 +12:00
|
|
|
$this->assertStringStartsWith('Permissions must be one of:', $file['body']['message']);
|
2022-08-03 16:17:49 +12:00
|
|
|
$this->assertStringContainsString('any', $file['body']['message']);
|
|
|
|
$this->assertStringContainsString('users', $file['body']['message']);
|
2022-05-24 02:54:50 +12:00
|
|
|
$this->assertStringContainsString('user:' . $this->getUser()['$id'], $file['body']['message']);
|
2021-12-11 05:48:54 +13:00
|
|
|
|
2021-12-14 22:42:39 +13:00
|
|
|
$file = $this->client->call(Client::METHOD_POST, '/storage/buckets/' . $data['bucketId'] . '/files', array_merge([
|
2021-12-11 05:48:54 +13:00
|
|
|
'content-type' => 'multipart/form-data',
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
], $this->getHeaders()), [
|
|
|
|
'fileId' => 'unique()',
|
|
|
|
'file' => new CURLFile(realpath(__DIR__ . '/../../../resources/logo.png'), 'image/png', 'permissions.png'),
|
|
|
|
'folderId' => 'xyz',
|
2022-08-08 23:00:15 +12:00
|
|
|
'permissions' => [
|
|
|
|
'create(user:notme)',
|
|
|
|
'update(user:notme)',
|
|
|
|
'delete(user:notme)',
|
|
|
|
]
|
2021-12-11 05:48:54 +13:00
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertEquals($file['headers']['status-code'], 400);
|
2022-08-09 19:11:30 +12:00
|
|
|
$this->assertStringStartsWith('Permissions must be one of:', $file['body']['message']);
|
2022-08-03 16:17:49 +12:00
|
|
|
$this->assertStringContainsString('any', $file['body']['message']);
|
|
|
|
$this->assertStringContainsString('users', $file['body']['message']);
|
2022-05-24 02:54:50 +12:00
|
|
|
$this->assertStringContainsString('user:' . $this->getUser()['$id'], $file['body']['message']);
|
2021-12-11 05:48:54 +13:00
|
|
|
|
2021-12-14 22:42:39 +13:00
|
|
|
$file = $this->client->call(Client::METHOD_POST, '/storage/buckets/' . $data['bucketId'] . '/files', array_merge([
|
2021-12-11 05:48:54 +13:00
|
|
|
'content-type' => 'multipart/form-data',
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
], $this->getHeaders()), [
|
|
|
|
'fileId' => 'unique()',
|
|
|
|
'file' => new CURLFile(realpath(__DIR__ . '/../../../resources/logo.png'), 'image/png', 'permissions.png'),
|
|
|
|
'folderId' => 'xyz',
|
2022-08-03 16:17:49 +12:00
|
|
|
'permissions' => [
|
|
|
|
'read(user:notme)',
|
2022-08-05 17:23:18 +12:00
|
|
|
'create(user:notme)',
|
|
|
|
'update(user:notme)',
|
|
|
|
'delete(user:notme)',
|
2022-08-03 16:17:49 +12:00
|
|
|
],
|
2021-12-11 05:48:54 +13:00
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertEquals($file['headers']['status-code'], 400);
|
2022-08-09 19:11:30 +12:00
|
|
|
$this->assertStringStartsWith('Permissions must be one of:', $file['body']['message']);
|
2022-08-03 16:17:49 +12:00
|
|
|
$this->assertStringContainsString('any', $file['body']['message']);
|
|
|
|
$this->assertStringContainsString('users', $file['body']['message']);
|
2022-05-24 02:54:50 +12:00
|
|
|
$this->assertStringContainsString('user:' . $this->getUser()['$id'], $file['body']['message']);
|
2021-12-11 05:48:54 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testCreateFileDefaultPermissions
|
|
|
|
*/
|
|
|
|
public function testUpdateFileAbusePermissions(array $data): void
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Test for FAILURE
|
|
|
|
*/
|
2021-12-14 22:42:39 +13:00
|
|
|
$file = $this->client->call(Client::METHOD_PUT, '/storage/buckets/' . $data['bucketId'] . '/files/' . $data['fileId'], array_merge([
|
2021-12-11 05:48:54 +13:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
], $this->getHeaders()), [
|
2022-08-03 16:17:49 +12:00
|
|
|
'permissions' => [
|
|
|
|
'read(user:notme)',
|
|
|
|
],
|
2021-12-11 05:48:54 +13:00
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertEquals($file['headers']['status-code'], 400);
|
2022-08-09 19:11:30 +12:00
|
|
|
$this->assertStringStartsWith('Permissions must be one of:', $file['body']['message']);
|
2022-08-03 16:17:49 +12:00
|
|
|
$this->assertStringContainsString('any', $file['body']['message']);
|
|
|
|
$this->assertStringContainsString('users', $file['body']['message']);
|
2022-05-24 02:54:50 +12:00
|
|
|
$this->assertStringContainsString('user:' . $this->getUser()['$id'], $file['body']['message']);
|
2021-12-11 05:48:54 +13:00
|
|
|
|
2021-12-14 22:42:39 +13:00
|
|
|
$file = $this->client->call(Client::METHOD_PUT, '/storage/buckets/' . $data['bucketId'] . '/files/' . $data['fileId'], array_merge([
|
2021-12-11 05:48:54 +13:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
], $this->getHeaders()), [
|
2022-08-08 23:00:15 +12:00
|
|
|
'permissions' => [
|
|
|
|
'create(user:notme)',
|
|
|
|
'update(user:notme)',
|
|
|
|
'delete(user:notme)',
|
|
|
|
]
|
2021-12-11 05:48:54 +13:00
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertEquals($file['headers']['status-code'], 400);
|
2022-08-09 19:11:30 +12:00
|
|
|
$this->assertStringStartsWith('Permissions must be one of:', $file['body']['message']);
|
2022-08-03 16:17:49 +12:00
|
|
|
$this->assertStringContainsString('any', $file['body']['message']);
|
|
|
|
$this->assertStringContainsString('users', $file['body']['message']);
|
2022-05-24 02:54:50 +12:00
|
|
|
$this->assertStringContainsString('user:' . $this->getUser()['$id'], $file['body']['message']);
|
2021-12-11 05:48:54 +13:00
|
|
|
|
2021-12-14 22:42:39 +13:00
|
|
|
$file = $this->client->call(Client::METHOD_PUT, '/storage/buckets/' . $data['bucketId'] . '/files/' . $data['fileId'], array_merge([
|
2021-12-11 05:48:54 +13:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
], $this->getHeaders()), [
|
2022-08-03 16:17:49 +12:00
|
|
|
'permissions' => [
|
|
|
|
'read(user:notme)',
|
2022-08-05 17:23:18 +12:00
|
|
|
'create(user:notme)',
|
|
|
|
'update(user:notme)',
|
|
|
|
'delete(user:notme)',
|
2022-08-03 16:17:49 +12:00
|
|
|
],
|
2021-12-11 05:48:54 +13:00
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertEquals($file['headers']['status-code'], 400);
|
2022-08-09 19:11:30 +12:00
|
|
|
$this->assertStringStartsWith('Permissions must be one of:', $file['body']['message']);
|
2022-08-03 16:17:49 +12:00
|
|
|
$this->assertStringContainsString('any', $file['body']['message']);
|
|
|
|
$this->assertStringContainsString('users', $file['body']['message']);
|
2022-05-24 02:54:50 +12:00
|
|
|
$this->assertStringContainsString('user:' . $this->getUser()['$id'], $file['body']['message']);
|
2021-09-07 01:33:06 +12:00
|
|
|
}
|
2022-05-24 02:54:50 +12:00
|
|
|
}
|