2020-11-21 01:35:16 +13:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\E2E\Services\Webhooks;
|
|
|
|
|
2022-08-30 23:10:15 +12:00
|
|
|
use Appwrite\Tests\Retry;
|
2020-11-21 01:35:16 +13:00
|
|
|
use CURLFile;
|
|
|
|
use Tests\E2E\Client;
|
2022-07-13 04:44:58 +12:00
|
|
|
use Utopia\Database\DateTime;
|
2023-02-06 09:07:46 +13:00
|
|
|
use Utopia\Database\Helpers\ID;
|
|
|
|
use Utopia\Database\Helpers\Permission;
|
|
|
|
use Utopia\Database\Helpers\Role;
|
2023-03-02 01:00:36 +13:00
|
|
|
use Utopia\Database\Validator\Datetime as DatetimeValidator;
|
2020-11-21 01:35:16 +13:00
|
|
|
|
|
|
|
trait WebhooksBase
|
|
|
|
{
|
2022-06-08 20:03:07 +12:00
|
|
|
public static function getWebhookSignature(array $webhook, string $signatureKey): string
|
2022-06-08 19:20:54 +12:00
|
|
|
{
|
2022-06-08 19:19:50 +12:00
|
|
|
$payload = json_encode($webhook['data']);
|
|
|
|
$url = $webhook['url'];
|
|
|
|
return base64_encode(hash_hmac('sha1', $url . $payload, $signatureKey, true));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-12-01 10:41:58 +13:00
|
|
|
public function testCreateCollection(): array
|
|
|
|
{
|
|
|
|
/**
|
2022-06-22 22:51:49 +12:00
|
|
|
* Create database
|
|
|
|
*/
|
|
|
|
$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']
|
|
|
|
]), [
|
2022-08-14 22:33:36 +12:00
|
|
|
'databaseId' => ID::unique(),
|
2022-06-22 22:51:49 +12:00
|
|
|
'name' => 'Actors DB',
|
|
|
|
]);
|
|
|
|
|
|
|
|
$databaseId = $database['body']['$id'];
|
|
|
|
|
|
|
|
/**
|
2020-12-01 10:41:58 +13:00
|
|
|
* Test for SUCCESS
|
|
|
|
*/
|
2022-06-22 22:51:49 +12:00
|
|
|
$actors = $this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/collections', array_merge([
|
2020-12-01 10:41:58 +13:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
]), [
|
2022-08-14 22:33:36 +12:00
|
|
|
'collectionId' => ID::unique(),
|
2020-12-01 10:41:58 +13:00
|
|
|
'name' => 'Actors',
|
2022-08-03 16:17:49 +12:00
|
|
|
'permissions' => [
|
2022-08-14 17:21:11 +12:00
|
|
|
Permission::read(Role::any()),
|
|
|
|
Permission::create(Role::any()),
|
|
|
|
Permission::update(Role::any()),
|
|
|
|
Permission::delete(Role::any()),
|
2022-08-03 16:17:49 +12:00
|
|
|
],
|
|
|
|
'documentSecurity' => true,
|
2020-12-01 10:41:58 +13:00
|
|
|
]);
|
2022-04-18 08:34:32 +12:00
|
|
|
|
2022-04-19 04:21:45 +12:00
|
|
|
$actorsId = $actors['body']['$id'];
|
|
|
|
|
2020-12-01 10:41:58 +13:00
|
|
|
$this->assertEquals($actors['headers']['status-code'], 201);
|
|
|
|
$this->assertNotEmpty($actors['body']['$id']);
|
|
|
|
|
|
|
|
$webhook = $this->getLastRequest();
|
2022-06-08 19:19:50 +12:00
|
|
|
$signatureExpected = self::getWebhookSignature($webhook, $this->getProject()['signatureKey']);
|
2020-12-01 10:41:58 +13:00
|
|
|
|
|
|
|
$this->assertEquals($webhook['method'], 'POST');
|
|
|
|
$this->assertEquals($webhook['headers']['Content-Type'], 'application/json');
|
|
|
|
$this->assertEquals($webhook['headers']['User-Agent'], 'Appwrite-Server vdev. Please report abuse at security@appwrite.io');
|
2022-06-22 22:51:49 +12:00
|
|
|
$this->assertStringContainsString('databases.' . $databaseId . '.collections.*', $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString('databases.' . $databaseId . '.collections.*.create', $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString("databases.{$databaseId}.collections.{$actorsId}", $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString("databases.{$databaseId}.collections.{$actorsId}.create", $webhook['headers']['X-Appwrite-Webhook-Events']);
|
2022-06-08 03:11:07 +12:00
|
|
|
$this->assertEquals($webhook['headers']['X-Appwrite-Webhook-Signature'], $signatureExpected);
|
2020-12-04 06:56:07 +13:00
|
|
|
$this->assertEquals($webhook['headers']['X-Appwrite-Webhook-Id'] ?? '', $this->getProject()['webhookId']);
|
|
|
|
$this->assertEquals($webhook['headers']['X-Appwrite-Webhook-Project-Id'] ?? '', $this->getProject()['$id']);
|
|
|
|
$this->assertEquals(empty($webhook['headers']['X-Appwrite-Webhook-User-Id'] ?? ''), true);
|
2020-12-01 10:41:58 +13:00
|
|
|
$this->assertNotEmpty($webhook['data']['$id']);
|
|
|
|
$this->assertEquals($webhook['data']['name'], 'Actors');
|
2022-08-02 21:21:53 +12:00
|
|
|
$this->assertIsArray($webhook['data']['$permissions']);
|
2022-08-14 02:10:28 +12:00
|
|
|
$this->assertCount(4, $webhook['data']['$permissions']);
|
2020-12-01 10:41:58 +13:00
|
|
|
|
2022-06-22 22:51:49 +12:00
|
|
|
return array_merge(['actorsId' => $actorsId, 'databaseId' => $databaseId]);
|
2020-12-01 10:41:58 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testCreateCollection
|
|
|
|
*/
|
2021-07-03 08:22:12 +12:00
|
|
|
public function testCreateAttributes(array $data): array
|
|
|
|
{
|
2022-04-19 04:21:45 +12:00
|
|
|
$actorsId = $data['actorsId'];
|
2022-06-22 22:51:49 +12:00
|
|
|
$databaseId = $data['databaseId'];
|
2022-04-19 04:21:45 +12:00
|
|
|
|
2022-06-22 22:51:49 +12:00
|
|
|
$firstName = $this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/collections/' . $actorsId . '/attributes/string', array_merge([
|
2021-07-03 08:22:12 +12:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
]), [
|
2021-12-17 05:10:01 +13:00
|
|
|
'key' => 'firstName',
|
2021-07-03 08:22:12 +12:00
|
|
|
'size' => 256,
|
|
|
|
'required' => true,
|
|
|
|
]);
|
|
|
|
|
2022-06-22 22:51:49 +12:00
|
|
|
$lastName = $this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/collections/' . $actorsId . '/attributes/string', array_merge([
|
2021-07-03 08:22:12 +12:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
]), [
|
2021-12-17 05:10:01 +13:00
|
|
|
'key' => 'lastName',
|
2021-07-03 08:22:12 +12:00
|
|
|
'size' => 256,
|
|
|
|
'required' => true,
|
|
|
|
]);
|
|
|
|
|
2022-06-22 22:51:49 +12:00
|
|
|
$extra = $this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/collections/' . $actorsId . '/attributes/string', array_merge([
|
2021-10-26 14:14:30 +13:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
]), [
|
2021-12-17 05:10:01 +13:00
|
|
|
'key' => 'extra',
|
2021-10-26 14:14:30 +13:00
|
|
|
'size' => 64,
|
|
|
|
'required' => false,
|
|
|
|
]);
|
|
|
|
|
2022-04-19 04:21:45 +12:00
|
|
|
$attributeId = $extra['body']['key'];
|
|
|
|
|
2022-07-19 01:22:23 +12:00
|
|
|
$this->assertEquals($firstName['headers']['status-code'], 202);
|
2021-08-23 19:27:09 +12:00
|
|
|
$this->assertEquals($firstName['body']['key'], 'firstName');
|
2022-07-19 01:22:23 +12:00
|
|
|
$this->assertEquals($lastName['headers']['status-code'], 202);
|
2021-08-23 19:27:09 +12:00
|
|
|
$this->assertEquals($lastName['body']['key'], 'lastName');
|
2022-07-19 01:22:23 +12:00
|
|
|
$this->assertEquals($extra['headers']['status-code'], 202);
|
2021-10-26 14:14:30 +13:00
|
|
|
$this->assertEquals($extra['body']['key'], 'extra');
|
2021-07-03 08:22:12 +12:00
|
|
|
|
|
|
|
// wait for database worker to kick in
|
2021-07-03 12:20:09 +12:00
|
|
|
sleep(10);
|
2021-07-03 08:22:12 +12:00
|
|
|
|
|
|
|
$webhook = $this->getLastRequest();
|
2022-06-08 19:19:50 +12:00
|
|
|
$signatureExpected = self::getWebhookSignature($webhook, $this->getProject()['signatureKey']);
|
2021-07-03 08:22:12 +12:00
|
|
|
|
|
|
|
$this->assertEquals($webhook['method'], 'POST');
|
|
|
|
$this->assertEquals($webhook['headers']['Content-Type'], 'application/json');
|
|
|
|
$this->assertEquals($webhook['headers']['User-Agent'], 'Appwrite-Server vdev. Please report abuse at security@appwrite.io');
|
2022-06-22 22:51:49 +12:00
|
|
|
$this->assertStringContainsString('databases.' . $databaseId . '.collections.*', $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString('databases.' . $databaseId . '.collections.*.attributes.*', $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString('databases.' . $databaseId . '.collections.*.attributes.*.create', $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString("databases.{$databaseId}.collections.{$actorsId}", $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString("databases.{$databaseId}.collections.{$actorsId}.attributes.*", $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString("databases.{$databaseId}.collections.{$actorsId}.attributes.*.create", $webhook['headers']['X-Appwrite-Webhook-Events']);
|
2022-06-08 03:11:07 +12:00
|
|
|
$this->assertEquals($webhook['headers']['X-Appwrite-Webhook-Signature'], $signatureExpected);
|
2021-07-03 08:22:12 +12:00
|
|
|
$this->assertEquals($webhook['headers']['X-Appwrite-Webhook-Id'] ?? '', $this->getProject()['webhookId']);
|
|
|
|
$this->assertEquals($webhook['headers']['X-Appwrite-Webhook-Project-Id'] ?? '', $this->getProject()['$id']);
|
2021-08-23 19:27:09 +12:00
|
|
|
$this->assertNotEmpty($webhook['data']['key']);
|
2021-10-26 14:14:30 +13:00
|
|
|
$this->assertEquals($webhook['data']['key'], 'extra');
|
2022-04-19 04:21:45 +12:00
|
|
|
|
2022-06-22 22:51:49 +12:00
|
|
|
$removed = $this->client->call(Client::METHOD_DELETE, '/databases/' . $databaseId . '/collections/' . $data['actorsId'] . '/attributes/' . $extra['body']['key'], array_merge([
|
2021-10-26 14:14:30 +13:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
]));
|
|
|
|
|
|
|
|
$this->assertEquals(204, $removed['headers']['status-code']);
|
|
|
|
|
|
|
|
$webhook = $this->getLastRequest();
|
2022-06-08 19:19:50 +12:00
|
|
|
$signatureExpected = self::getWebhookSignature($webhook, $this->getProject()['signatureKey']);
|
2021-10-26 14:14:30 +13:00
|
|
|
|
|
|
|
// $this->assertEquals($webhook['method'], 'DELETE');
|
|
|
|
$this->assertEquals($webhook['headers']['Content-Type'], 'application/json');
|
|
|
|
$this->assertEquals($webhook['headers']['User-Agent'], 'Appwrite-Server vdev. Please report abuse at security@appwrite.io');
|
2022-06-22 22:51:49 +12:00
|
|
|
$this->assertStringContainsString('databases.' . $databaseId . '.collections.*', $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString('databases.' . $databaseId . '.collections.*.attributes.*', $webhook['headers']['X-Appwrite-Webhook-Events']);
|
2023-10-19 06:06:59 +13:00
|
|
|
$this->assertStringContainsString('databases.' . $databaseId . '.collections.*.attributes.*.update', $webhook['headers']['X-Appwrite-Webhook-Events']);
|
2022-06-22 22:51:49 +12:00
|
|
|
$this->assertStringContainsString("databases.{$databaseId}.collections.{$actorsId}", $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString("databases.{$databaseId}.collections.{$actorsId}.attributes.*", $webhook['headers']['X-Appwrite-Webhook-Events']);
|
2023-10-19 06:06:59 +13:00
|
|
|
$this->assertStringContainsString("databases.{$databaseId}.collections.{$actorsId}.attributes.*.update", $webhook['headers']['X-Appwrite-Webhook-Events']);
|
2022-06-08 03:11:07 +12:00
|
|
|
$this->assertEquals($webhook['headers']['X-Appwrite-Webhook-Signature'], $signatureExpected);
|
2021-10-26 14:14:30 +13:00
|
|
|
$this->assertEquals($webhook['headers']['X-Appwrite-Webhook-Id'] ?? '', $this->getProject()['webhookId']);
|
|
|
|
$this->assertEquals($webhook['headers']['X-Appwrite-Webhook-Project-Id'] ?? '', $this->getProject()['$id']);
|
|
|
|
$this->assertNotEmpty($webhook['data']['key']);
|
|
|
|
$this->assertEquals($webhook['data']['key'], 'extra');
|
2021-07-03 08:22:12 +12:00
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testCreateAttributes
|
|
|
|
*/
|
2020-12-01 10:41:58 +13:00
|
|
|
public function testCreateDocument(array $data): array
|
|
|
|
{
|
2022-04-19 04:21:45 +12:00
|
|
|
$actorsId = $data['actorsId'];
|
2022-06-22 22:51:49 +12:00
|
|
|
$databaseId = $data['databaseId'];
|
2022-04-19 04:21:45 +12:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test for SUCCESS
|
|
|
|
*/
|
2022-06-22 22:51:49 +12:00
|
|
|
$document = $this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/collections/' . $actorsId . '/documents', array_merge([
|
2020-12-01 10:41:58 +13:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
], $this->getHeaders()), [
|
2022-08-14 22:33:36 +12:00
|
|
|
'documentId' => ID::unique(),
|
2020-12-01 10:41:58 +13:00
|
|
|
'data' => [
|
|
|
|
'firstName' => 'Chris',
|
|
|
|
'lastName' => 'Evans',
|
|
|
|
],
|
2022-08-03 16:17:49 +12:00
|
|
|
'permissions' => [
|
2022-08-14 17:21:11 +12:00
|
|
|
Permission::read(Role::any()),
|
|
|
|
Permission::update(Role::any()),
|
|
|
|
Permission::delete(Role::any()),
|
2022-08-03 16:17:49 +12:00
|
|
|
],
|
2020-12-01 10:41:58 +13:00
|
|
|
]);
|
|
|
|
|
2022-04-19 04:21:45 +12:00
|
|
|
$documentId = $document['body']['$id'];
|
|
|
|
|
2020-12-01 10:41:58 +13:00
|
|
|
$this->assertEquals($document['headers']['status-code'], 201);
|
|
|
|
$this->assertNotEmpty($document['body']['$id']);
|
|
|
|
|
|
|
|
$webhook = $this->getLastRequest();
|
2022-06-08 19:19:50 +12:00
|
|
|
$signatureExpected = self::getWebhookSignature($webhook, $this->getProject()['signatureKey']);
|
2020-12-01 10:41:58 +13:00
|
|
|
|
|
|
|
$this->assertEquals($webhook['method'], 'POST');
|
|
|
|
$this->assertEquals($webhook['headers']['Content-Type'], 'application/json');
|
|
|
|
$this->assertEquals($webhook['headers']['User-Agent'], 'Appwrite-Server vdev. Please report abuse at security@appwrite.io');
|
2022-06-22 22:51:49 +12:00
|
|
|
$this->assertStringContainsString('databases.' . $databaseId . '.collections.*', $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString('databases.' . $databaseId . '.collections.*.documents.*', $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString('databases.' . $databaseId . '.collections.*.documents.*.create', $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString("databases.{$databaseId}.collections.*.documents.{$documentId}", $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString("databases.{$databaseId}.collections.*.documents.{$documentId}.create", $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString("databases.{$databaseId}.collections.{$actorsId}", $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString("databases.{$databaseId}.collections.{$actorsId}.documents.*", $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString("databases.{$databaseId}.collections.{$actorsId}.documents.*.create", $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString("databases.{$databaseId}.collections.{$actorsId}.documents.{$documentId}", $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString("databases.{$databaseId}.collections.{$actorsId}.documents.{$documentId}.create", $webhook['headers']['X-Appwrite-Webhook-Events']);
|
2022-06-08 03:11:07 +12:00
|
|
|
$this->assertEquals($webhook['headers']['X-Appwrite-Webhook-Signature'], $signatureExpected);
|
2020-12-04 06:56:07 +13:00
|
|
|
$this->assertEquals($webhook['headers']['X-Appwrite-Webhook-Id'] ?? '', $this->getProject()['webhookId']);
|
|
|
|
$this->assertEquals($webhook['headers']['X-Appwrite-Webhook-Project-Id'] ?? '', $this->getProject()['$id']);
|
|
|
|
$this->assertEquals(empty($webhook['headers']['X-Appwrite-Webhook-User-Id'] ?? ''), ('server' === $this->getSide()));
|
2020-12-01 10:41:58 +13:00
|
|
|
$this->assertNotEmpty($webhook['data']['$id']);
|
|
|
|
$this->assertEquals($webhook['data']['firstName'], 'Chris');
|
|
|
|
$this->assertEquals($webhook['data']['lastName'], 'Evans');
|
2022-08-02 21:21:53 +12:00
|
|
|
$this->assertIsArray($webhook['data']['$permissions']);
|
2022-08-14 02:10:28 +12:00
|
|
|
$this->assertCount(3, $webhook['data']['$permissions']);
|
2020-12-01 10:41:58 +13:00
|
|
|
|
|
|
|
$data['documentId'] = $document['body']['$id'];
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testCreateDocument
|
|
|
|
*/
|
|
|
|
public function testUpdateDocument(array $data): array
|
|
|
|
{
|
2022-04-19 04:21:45 +12:00
|
|
|
$actorsId = $data['actorsId'];
|
2022-06-22 22:51:49 +12:00
|
|
|
$databaseId = $data['databaseId'];
|
2022-04-19 04:21:45 +12:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test for SUCCESS
|
|
|
|
*/
|
2022-06-22 22:51:49 +12:00
|
|
|
$document = $this->client->call(Client::METHOD_PATCH, '/databases/' . $databaseId . '/collections/' . $actorsId . '/documents/' . $data['documentId'], array_merge([
|
2020-12-01 10:41:58 +13:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
], $this->getHeaders()), [
|
|
|
|
'data' => [
|
|
|
|
'firstName' => 'Chris1',
|
|
|
|
'lastName' => 'Evans2',
|
|
|
|
],
|
2022-08-03 16:17:49 +12:00
|
|
|
'permissions' => [
|
2022-08-14 17:21:11 +12:00
|
|
|
Permission::read(Role::any()),
|
|
|
|
Permission::update(Role::any()),
|
|
|
|
Permission::delete(Role::any()),
|
2022-08-03 16:17:49 +12:00
|
|
|
],
|
2020-12-01 10:41:58 +13:00
|
|
|
]);
|
|
|
|
|
2022-04-19 04:21:45 +12:00
|
|
|
$documentId = $document['body']['$id'];
|
|
|
|
|
2020-12-01 10:41:58 +13:00
|
|
|
$this->assertEquals($document['headers']['status-code'], 200);
|
|
|
|
$this->assertNotEmpty($document['body']['$id']);
|
|
|
|
|
|
|
|
$webhook = $this->getLastRequest();
|
2022-06-08 19:19:50 +12:00
|
|
|
$signatureExpected = self::getWebhookSignature($webhook, $this->getProject()['signatureKey']);
|
2020-12-01 10:41:58 +13:00
|
|
|
|
|
|
|
$this->assertEquals($webhook['method'], 'POST');
|
|
|
|
$this->assertEquals($webhook['headers']['Content-Type'], 'application/json');
|
|
|
|
$this->assertEquals($webhook['headers']['User-Agent'], 'Appwrite-Server vdev. Please report abuse at security@appwrite.io');
|
2022-06-22 22:51:49 +12:00
|
|
|
$this->assertStringContainsString('databases.' . $databaseId . '.collections.*', $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString('databases.' . $databaseId . '.collections.*.documents.*', $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString('databases.' . $databaseId . '.collections.*.documents.*.update', $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString("databases.{$databaseId}.collections.*.documents.{$documentId}", $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString("databases.{$databaseId}.collections.*.documents.{$documentId}.update", $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString("databases.{$databaseId}.collections.{$actorsId}", $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString("databases.{$databaseId}.collections.{$actorsId}.documents.*", $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString("databases.{$databaseId}.collections.{$actorsId}.documents.*.update", $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString("databases.{$databaseId}.collections.{$actorsId}.documents.{$documentId}", $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString("databases.{$databaseId}.collections.{$actorsId}.documents.{$documentId}.update", $webhook['headers']['X-Appwrite-Webhook-Events']);
|
2022-06-08 03:11:07 +12:00
|
|
|
$this->assertEquals($webhook['headers']['X-Appwrite-Webhook-Signature'], $signatureExpected);
|
2020-12-04 06:56:07 +13:00
|
|
|
$this->assertEquals($webhook['headers']['X-Appwrite-Webhook-Id'] ?? '', $this->getProject()['webhookId']);
|
|
|
|
$this->assertEquals($webhook['headers']['X-Appwrite-Webhook-Project-Id'] ?? '', $this->getProject()['$id']);
|
|
|
|
$this->assertEquals(empty($webhook['headers']['X-Appwrite-Webhook-User-Id'] ?? ''), ('server' === $this->getSide()));
|
2020-12-01 10:41:58 +13:00
|
|
|
$this->assertNotEmpty($webhook['data']['$id']);
|
|
|
|
$this->assertEquals($webhook['data']['firstName'], 'Chris1');
|
|
|
|
$this->assertEquals($webhook['data']['lastName'], 'Evans2');
|
2022-08-02 21:21:53 +12:00
|
|
|
$this->assertIsArray($webhook['data']['$permissions']);
|
2022-08-14 02:10:28 +12:00
|
|
|
$this->assertCount(3, $webhook['data']['$permissions']);
|
2020-12-01 10:41:58 +13:00
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testCreateCollection
|
|
|
|
*/
|
2022-08-30 23:10:15 +12:00
|
|
|
#[Retry(count: 1)]
|
2020-12-01 10:41:58 +13:00
|
|
|
public function testDeleteDocument(array $data): array
|
|
|
|
{
|
2022-04-19 04:21:45 +12:00
|
|
|
$actorsId = $data['actorsId'];
|
2022-06-22 22:51:49 +12:00
|
|
|
$databaseId = $data['databaseId'];
|
2022-04-19 04:21:45 +12:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test for SUCCESS
|
|
|
|
*/
|
2022-06-22 22:51:49 +12:00
|
|
|
$document = $this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/collections/' . $actorsId . '/documents', array_merge([
|
2020-12-01 10:41:58 +13:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
], $this->getHeaders()), [
|
2022-08-14 22:33:36 +12:00
|
|
|
'documentId' => ID::unique(),
|
2020-12-01 10:41:58 +13:00
|
|
|
'data' => [
|
|
|
|
'firstName' => 'Bradly',
|
|
|
|
'lastName' => 'Cooper',
|
2022-04-19 04:21:45 +12:00
|
|
|
|
2020-12-01 10:41:58 +13:00
|
|
|
],
|
2022-08-03 16:17:49 +12:00
|
|
|
'permissions' => [
|
2022-08-14 17:21:11 +12:00
|
|
|
Permission::read(Role::any()),
|
|
|
|
Permission::update(Role::any()),
|
|
|
|
Permission::delete(Role::any()),
|
2022-08-03 16:17:49 +12:00
|
|
|
],
|
2020-12-01 10:41:58 +13:00
|
|
|
]);
|
|
|
|
|
2022-04-19 04:21:45 +12:00
|
|
|
$documentId = $document['body']['$id'];
|
|
|
|
|
2020-12-01 10:41:58 +13:00
|
|
|
$this->assertEquals($document['headers']['status-code'], 201);
|
|
|
|
$this->assertNotEmpty($document['body']['$id']);
|
|
|
|
|
2022-06-22 22:51:49 +12:00
|
|
|
$document = $this->client->call(Client::METHOD_DELETE, '/databases/' . $databaseId . '/collections/' . $actorsId . '/documents/' . $document['body']['$id'], array_merge([
|
2020-12-01 10:41:58 +13:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
], $this->getHeaders()));
|
|
|
|
|
|
|
|
$this->assertEquals($document['headers']['status-code'], 204);
|
|
|
|
|
|
|
|
$webhook = $this->getLastRequest();
|
2022-06-08 19:19:50 +12:00
|
|
|
$signatureExpected = self::getWebhookSignature($webhook, $this->getProject()['signatureKey']);
|
2020-12-01 10:41:58 +13:00
|
|
|
|
|
|
|
$this->assertEquals($webhook['method'], 'POST');
|
|
|
|
$this->assertEquals($webhook['headers']['Content-Type'], 'application/json');
|
|
|
|
$this->assertEquals($webhook['headers']['User-Agent'], 'Appwrite-Server vdev. Please report abuse at security@appwrite.io');
|
2022-06-22 22:51:49 +12:00
|
|
|
$this->assertStringContainsString('databases.' . $databaseId . '.collections.*', $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString('databases.' . $databaseId . '.collections.*.documents.*', $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString('databases.' . $databaseId . '.collections.*.documents.*.delete', $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString("databases.{$databaseId}.collections.*.documents.{$documentId}", $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString("databases.{$databaseId}.collections.*.documents.{$documentId}.delete", $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString("databases.{$databaseId}.collections.{$actorsId}", $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString("databases.{$databaseId}.collections.{$actorsId}.documents.*", $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString("databases.{$databaseId}.collections.{$actorsId}.documents.*.delete", $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString("databases.{$databaseId}.collections.{$actorsId}.documents.{$documentId}", $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString("databases.{$databaseId}.collections.{$actorsId}.documents.{$documentId}.delete", $webhook['headers']['X-Appwrite-Webhook-Events']);
|
2022-06-08 03:11:07 +12:00
|
|
|
$this->assertEquals($webhook['headers']['X-Appwrite-Webhook-Signature'], $signatureExpected);
|
2020-12-04 06:56:07 +13:00
|
|
|
$this->assertEquals($webhook['headers']['X-Appwrite-Webhook-Id'] ?? '', $this->getProject()['webhookId']);
|
|
|
|
$this->assertEquals($webhook['headers']['X-Appwrite-Webhook-Project-Id'] ?? '', $this->getProject()['$id']);
|
|
|
|
$this->assertEquals(empty($webhook['headers']['X-Appwrite-Webhook-User-Id'] ?? ''), ('server' === $this->getSide()));
|
2020-12-01 10:41:58 +13:00
|
|
|
$this->assertNotEmpty($webhook['data']['$id']);
|
|
|
|
$this->assertEquals($webhook['data']['firstName'], 'Bradly');
|
|
|
|
$this->assertEquals($webhook['data']['lastName'], 'Cooper');
|
2022-08-02 21:21:53 +12:00
|
|
|
$this->assertIsArray($webhook['data']['$permissions']);
|
2022-08-14 02:10:28 +12:00
|
|
|
$this->assertCount(3, $webhook['data']['$permissions']);
|
2020-12-01 10:41:58 +13:00
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
2021-07-28 19:00:12 +12:00
|
|
|
|
|
|
|
public function testCreateStorageBucket(): array
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Test for SUCCESS
|
|
|
|
*/
|
|
|
|
$bucket = $this->client->call(Client::METHOD_POST, '/storage/buckets', array_merge([
|
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
]), [
|
2022-08-14 22:33:36 +12:00
|
|
|
'bucketId' => ID::unique(),
|
2021-07-28 19:00:12 +12:00
|
|
|
'name' => 'Test Bucket',
|
2022-08-03 16:17:49 +12:00
|
|
|
'permissions' => [
|
2022-08-14 17:21:11 +12:00
|
|
|
Permission::read(Role::any()),
|
|
|
|
Permission::create(Role::any()),
|
|
|
|
Permission::update(Role::any()),
|
|
|
|
Permission::delete(Role::any()),
|
2022-08-03 16:17:49 +12:00
|
|
|
],
|
2021-07-28 19:00:12 +12:00
|
|
|
]);
|
2022-04-19 04:21:45 +12:00
|
|
|
|
|
|
|
$bucketId = $bucket['body']['$id'];
|
|
|
|
|
2021-07-28 19:00:12 +12:00
|
|
|
$this->assertEquals($bucket['headers']['status-code'], 201);
|
|
|
|
$this->assertNotEmpty($bucket['body']['$id']);
|
|
|
|
|
|
|
|
$webhook = $this->getLastRequest();
|
2022-06-08 19:19:50 +12:00
|
|
|
$signatureExpected = self::getWebhookSignature($webhook, $this->getProject()['signatureKey']);
|
2021-07-28 19:00:12 +12:00
|
|
|
|
|
|
|
$this->assertEquals($webhook['method'], 'POST');
|
|
|
|
$this->assertEquals($webhook['headers']['Content-Type'], 'application/json');
|
|
|
|
$this->assertEquals($webhook['headers']['User-Agent'], 'Appwrite-Server vdev. Please report abuse at security@appwrite.io');
|
2022-05-11 01:25:49 +12:00
|
|
|
$this->assertStringContainsString('buckets.*', $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString('buckets.*.create', $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString("buckets.{$bucketId}", $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString("buckets.{$bucketId}.create", $webhook['headers']['X-Appwrite-Webhook-Events']);
|
2022-06-08 03:11:07 +12:00
|
|
|
$this->assertEquals($webhook['headers']['X-Appwrite-Webhook-Signature'], $signatureExpected);
|
2021-07-28 19:00:12 +12:00
|
|
|
$this->assertEquals($webhook['headers']['X-Appwrite-Webhook-Id'] ?? '', $this->getProject()['webhookId']);
|
|
|
|
$this->assertEquals($webhook['headers']['X-Appwrite-Webhook-Project-Id'] ?? '', $this->getProject()['$id']);
|
|
|
|
$this->assertEquals(empty($webhook['headers']['X-Appwrite-Webhook-User-Id'] ?? ''), true);
|
|
|
|
$this->assertNotEmpty($webhook['data']['$id']);
|
|
|
|
$this->assertEquals('Test Bucket', $webhook['data']['name']);
|
|
|
|
$this->assertEquals(true, $webhook['data']['enabled']);
|
2022-08-02 21:21:53 +12:00
|
|
|
$this->assertIsArray($webhook['data']['$permissions']);
|
2022-04-19 04:21:45 +12:00
|
|
|
|
|
|
|
return array_merge(['bucketId' => $bucketId]);
|
2021-07-28 19:00:12 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testCreateStorageBucket
|
|
|
|
*/
|
|
|
|
public function testUpdateStorageBucket(array $data): array
|
|
|
|
{
|
2022-04-19 04:21:45 +12:00
|
|
|
$bucketId = $data['bucketId'];
|
|
|
|
|
2021-07-28 19:00:12 +12:00
|
|
|
/**
|
|
|
|
* Test for SUCCESS
|
|
|
|
*/
|
|
|
|
$bucket = $this->client->call(Client::METHOD_PUT, '/storage/buckets/' . $data['bucketId'], array_merge([
|
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
]), [
|
|
|
|
'name' => 'Test Bucket Updated',
|
2022-08-03 16:17:49 +12:00
|
|
|
'fileSecurity' => true,
|
2021-07-28 19:00:12 +12:00
|
|
|
'enabled' => false,
|
|
|
|
]);
|
2022-04-19 04:21:45 +12:00
|
|
|
|
2021-07-28 19:00:12 +12:00
|
|
|
$this->assertEquals($bucket['headers']['status-code'], 200);
|
|
|
|
$this->assertNotEmpty($bucket['body']['$id']);
|
|
|
|
|
|
|
|
$webhook = $this->getLastRequest();
|
2022-06-08 19:19:50 +12:00
|
|
|
$signatureExpected = self::getWebhookSignature($webhook, $this->getProject()['signatureKey']);
|
2021-07-28 19:00:12 +12:00
|
|
|
|
|
|
|
$this->assertEquals($webhook['method'], 'POST');
|
|
|
|
$this->assertEquals($webhook['headers']['Content-Type'], 'application/json');
|
|
|
|
$this->assertEquals($webhook['headers']['User-Agent'], 'Appwrite-Server vdev. Please report abuse at security@appwrite.io');
|
2022-05-11 01:25:49 +12:00
|
|
|
$this->assertStringContainsString('buckets.*', $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString('buckets.*.update', $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString("buckets.{$bucketId}", $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString("buckets.{$bucketId}.update", $webhook['headers']['X-Appwrite-Webhook-Events']);
|
2022-06-08 03:11:07 +12:00
|
|
|
$this->assertEquals($webhook['headers']['X-Appwrite-Webhook-Signature'], $signatureExpected);
|
2021-07-28 19:00:12 +12:00
|
|
|
$this->assertEquals($webhook['headers']['X-Appwrite-Webhook-Id'] ?? '', $this->getProject()['webhookId']);
|
|
|
|
$this->assertEquals($webhook['headers']['X-Appwrite-Webhook-Project-Id'] ?? '', $this->getProject()['$id']);
|
|
|
|
$this->assertEquals(empty($webhook['headers']['X-Appwrite-Webhook-User-Id'] ?? ''), true);
|
|
|
|
$this->assertNotEmpty($webhook['data']['$id']);
|
|
|
|
$this->assertEquals('Test Bucket Updated', $webhook['data']['name']);
|
|
|
|
$this->assertEquals(false, $webhook['data']['enabled']);
|
2022-08-02 21:21:53 +12:00
|
|
|
$this->assertIsArray($webhook['data']['$permissions']);
|
2022-04-19 04:21:45 +12:00
|
|
|
|
2021-07-28 19:00:12 +12:00
|
|
|
return array_merge(['bucketId' => $bucket['body']['$id']]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testCreateStorageBucket
|
|
|
|
*/
|
2021-07-28 21:14:38 +12:00
|
|
|
public function testCreateBucketFile(array $data): array
|
2020-11-21 01:35:16 +13:00
|
|
|
{
|
2022-04-19 04:21:45 +12:00
|
|
|
$bucketId = $data['bucketId'];
|
|
|
|
|
2021-11-19 18:21:33 +13:00
|
|
|
//enable bucket
|
|
|
|
$bucket = $this->client->call(Client::METHOD_PUT, '/storage/buckets/' . $data['bucketId'], array_merge([
|
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
]), [
|
2021-11-19 19:45:46 +13:00
|
|
|
'name' => 'Test Bucket Updated',
|
2022-08-03 16:17:49 +12:00
|
|
|
'fileSecurity' => true,
|
2021-11-19 18:21:33 +13:00
|
|
|
'enabled' => true,
|
|
|
|
]);
|
2022-04-19 04:21:45 +12:00
|
|
|
|
2021-11-19 18:21:33 +13:00
|
|
|
$this->assertEquals($bucket['headers']['status-code'], 200);
|
2020-11-21 01:35:16 +13:00
|
|
|
/**
|
|
|
|
* Test for SUCCESS
|
|
|
|
*/
|
2022-04-19 04:21:45 +12:00
|
|
|
$file = $this->client->call(Client::METHOD_POST, '/storage/buckets/' . $data['bucketId'] . '/files', array_merge([
|
2020-11-21 01:35:16 +13:00
|
|
|
'content-type' => 'multipart/form-data',
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
], $this->getHeaders()), [
|
2022-08-14 22:33:36 +12:00
|
|
|
'fileId' => ID::unique(),
|
2020-11-21 01:35:16 +13:00
|
|
|
'file' => new CURLFile(realpath(__DIR__ . '/../../../resources/logo.png'), 'image/png', 'logo.png'),
|
2022-08-03 16:17:49 +12:00
|
|
|
'permissions' => [
|
2022-08-14 17:21:11 +12:00
|
|
|
Permission::read(Role::any()),
|
|
|
|
Permission::update(Role::any()),
|
|
|
|
Permission::delete(Role::any()),
|
2022-08-03 16:17:49 +12:00
|
|
|
],
|
2022-08-14 22:33:36 +12:00
|
|
|
'folderId' => ID::custom('xyz'),
|
2020-11-21 01:35:16 +13:00
|
|
|
]);
|
|
|
|
|
2022-04-19 04:21:45 +12:00
|
|
|
$fileId = $file['body']['$id'];
|
|
|
|
|
2020-11-21 01:35:16 +13:00
|
|
|
$this->assertEquals($file['headers']['status-code'], 201);
|
|
|
|
$this->assertNotEmpty($file['body']['$id']);
|
2020-11-21 10:03:48 +13:00
|
|
|
|
2020-11-21 01:35:16 +13:00
|
|
|
$webhook = $this->getLastRequest();
|
2022-06-08 19:19:50 +12:00
|
|
|
$signatureExpected = self::getWebhookSignature($webhook, $this->getProject()['signatureKey']);
|
2020-11-21 01:35:16 +13:00
|
|
|
|
2020-11-21 10:03:48 +13:00
|
|
|
$this->assertEquals($webhook['method'], 'POST');
|
|
|
|
$this->assertEquals($webhook['headers']['Content-Type'], 'application/json');
|
|
|
|
$this->assertEquals($webhook['headers']['User-Agent'], 'Appwrite-Server vdev. Please report abuse at security@appwrite.io');
|
2022-05-11 01:25:49 +12:00
|
|
|
$this->assertStringContainsString('buckets.*', $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString('buckets.*.files.*', $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString('buckets.*.files.*.create', $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString("buckets.*.files.{$fileId}", $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString("buckets.*.files.{$fileId}.create", $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString("buckets.{$bucketId}", $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString("buckets.{$bucketId}.files.*", $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString("buckets.{$bucketId}.files.*.create", $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString("buckets.{$bucketId}.files.{$fileId}", $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString("buckets.{$bucketId}.files.{$fileId}.create", $webhook['headers']['X-Appwrite-Webhook-Events']);
|
2022-06-08 03:11:07 +12:00
|
|
|
$this->assertEquals($webhook['headers']['X-Appwrite-Webhook-Signature'], $signatureExpected);
|
2020-12-04 06:56:07 +13:00
|
|
|
$this->assertEquals($webhook['headers']['X-Appwrite-Webhook-Id'] ?? '', $this->getProject()['webhookId']);
|
|
|
|
$this->assertEquals($webhook['headers']['X-Appwrite-Webhook-Project-Id'] ?? '', $this->getProject()['$id']);
|
|
|
|
$this->assertEquals(empty($webhook['headers']['X-Appwrite-Webhook-User-Id'] ?? ''), ('server' === $this->getSide()));
|
2020-11-21 10:03:48 +13:00
|
|
|
$this->assertNotEmpty($webhook['data']['$id']);
|
2022-08-02 21:21:53 +12:00
|
|
|
$this->assertIsArray($webhook['data']['$permissions']);
|
2020-11-21 10:03:48 +13:00
|
|
|
$this->assertEquals($webhook['data']['name'], 'logo.png');
|
2023-02-06 09:39:41 +13:00
|
|
|
$this->assertEquals(true, (new DatetimeValidator())->isValid($webhook['data']['$createdAt']));
|
2020-11-21 10:03:48 +13:00
|
|
|
$this->assertNotEmpty($webhook['data']['signature']);
|
|
|
|
$this->assertEquals($webhook['data']['mimeType'], 'image/png');
|
|
|
|
$this->assertEquals($webhook['data']['sizeOriginal'], 47218);
|
2020-11-21 01:35:16 +13:00
|
|
|
|
2022-04-19 04:21:45 +12:00
|
|
|
$data['fileId'] = $fileId;
|
|
|
|
|
2021-07-28 19:00:12 +12:00
|
|
|
return $data;
|
2020-11-21 01:35:16 +13:00
|
|
|
}
|
2022-04-19 04:21:45 +12:00
|
|
|
|
2020-11-21 10:03:48 +13:00
|
|
|
/**
|
2021-07-28 21:14:38 +12:00
|
|
|
* @depends testCreateBucketFile
|
2020-11-21 10:03:48 +13:00
|
|
|
*/
|
2021-07-28 21:14:38 +12:00
|
|
|
public function testUpdateBucketFile(array $data): array
|
2020-11-21 10:03:48 +13:00
|
|
|
{
|
2022-04-19 04:21:45 +12:00
|
|
|
$bucketId = $data['bucketId'];
|
|
|
|
$fileId = $data['fileId'];
|
|
|
|
|
2020-11-21 10:03:48 +13:00
|
|
|
/**
|
|
|
|
* Test for SUCCESS
|
|
|
|
*/
|
2022-04-19 04:21:45 +12:00
|
|
|
$file = $this->client->call(Client::METHOD_PUT, '/storage/buckets/' . $bucketId . '/files/' . $fileId, array_merge([
|
2020-11-21 10:03:48 +13:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
], $this->getHeaders()), [
|
2022-08-03 16:17:49 +12:00
|
|
|
'permissions' => [
|
2022-08-14 17:21:11 +12:00
|
|
|
Permission::read(Role::any()),
|
|
|
|
Permission::update(Role::any()),
|
|
|
|
Permission::delete(Role::any()),
|
2022-08-03 16:17:49 +12:00
|
|
|
],
|
2020-11-21 10:03:48 +13:00
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertEquals($file['headers']['status-code'], 200);
|
|
|
|
$this->assertNotEmpty($file['body']['$id']);
|
|
|
|
|
|
|
|
$webhook = $this->getLastRequest();
|
2022-06-08 19:19:50 +12:00
|
|
|
$signatureExpected = self::getWebhookSignature($webhook, $this->getProject()['signatureKey']);
|
2020-11-21 10:03:48 +13:00
|
|
|
|
|
|
|
$this->assertEquals($webhook['method'], 'POST');
|
|
|
|
$this->assertEquals($webhook['headers']['Content-Type'], 'application/json');
|
|
|
|
$this->assertEquals($webhook['headers']['User-Agent'], 'Appwrite-Server vdev. Please report abuse at security@appwrite.io');
|
2022-05-11 01:25:49 +12:00
|
|
|
$this->assertStringContainsString('buckets.*', $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString('buckets.*.files.*', $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString('buckets.*.files.*.update', $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString("buckets.*.files.{$fileId}", $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString("buckets.*.files.{$fileId}.update", $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString("buckets.{$bucketId}", $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString("buckets.{$bucketId}.files.*", $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString("buckets.{$bucketId}.files.*.update", $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString("buckets.{$bucketId}.files.{$fileId}", $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString("buckets.{$bucketId}.files.{$fileId}.update", $webhook['headers']['X-Appwrite-Webhook-Events']);
|
2022-06-08 03:11:07 +12:00
|
|
|
$this->assertEquals($webhook['headers']['X-Appwrite-Webhook-Signature'], $signatureExpected);
|
2020-12-04 06:56:07 +13:00
|
|
|
$this->assertEquals($webhook['headers']['X-Appwrite-Webhook-Id'] ?? '', $this->getProject()['webhookId']);
|
|
|
|
$this->assertEquals($webhook['headers']['X-Appwrite-Webhook-Project-Id'] ?? '', $this->getProject()['$id']);
|
|
|
|
$this->assertEquals(empty($webhook['headers']['X-Appwrite-Webhook-User-Id'] ?? ''), ('server' === $this->getSide()));
|
2020-11-21 10:03:48 +13:00
|
|
|
$this->assertNotEmpty($webhook['data']['$id']);
|
2022-08-02 21:21:53 +12:00
|
|
|
$this->assertIsArray($webhook['data']['$permissions']);
|
2020-11-21 10:03:48 +13:00
|
|
|
$this->assertEquals($webhook['data']['name'], 'logo.png');
|
2023-02-06 09:39:41 +13:00
|
|
|
$this->assertEquals(true, (new DatetimeValidator())->isValid($webhook['data']['$createdAt']));
|
2020-11-21 10:03:48 +13:00
|
|
|
$this->assertNotEmpty($webhook['data']['signature']);
|
|
|
|
$this->assertEquals($webhook['data']['mimeType'], 'image/png');
|
|
|
|
$this->assertEquals($webhook['data']['sizeOriginal'], 47218);
|
2022-04-19 04:21:45 +12:00
|
|
|
|
2020-11-21 10:03:48 +13:00
|
|
|
return $data;
|
|
|
|
}
|
2022-04-19 04:21:45 +12:00
|
|
|
|
2020-11-21 10:03:48 +13:00
|
|
|
/**
|
2021-07-28 21:14:38 +12:00
|
|
|
* @depends testUpdateBucketFile
|
2020-11-21 10:03:48 +13:00
|
|
|
*/
|
2021-07-28 21:14:38 +12:00
|
|
|
public function testDeleteBucketFile(array $data): array
|
2020-11-21 10:03:48 +13:00
|
|
|
{
|
2022-04-19 04:21:45 +12:00
|
|
|
$bucketId = $data['bucketId'];
|
|
|
|
$fileId = $data['fileId'];
|
|
|
|
|
2020-11-21 10:03:48 +13:00
|
|
|
/**
|
|
|
|
* Test for SUCCESS
|
|
|
|
*/
|
2021-07-28 19:00:12 +12:00
|
|
|
$file = $this->client->call(Client::METHOD_DELETE, '/storage/buckets/' . $data['bucketId'] . '/files/' . $data['fileId'], array_merge([
|
2020-11-21 10:03:48 +13:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
], $this->getHeaders()));
|
|
|
|
|
|
|
|
$this->assertEquals(204, $file['headers']['status-code']);
|
|
|
|
$this->assertEmpty($file['body']);
|
|
|
|
|
|
|
|
$webhook = $this->getLastRequest();
|
2022-06-08 19:19:50 +12:00
|
|
|
$signatureExpected = self::getWebhookSignature($webhook, $this->getProject()['signatureKey']);
|
2020-11-21 10:03:48 +13:00
|
|
|
|
|
|
|
$this->assertEquals($webhook['method'], 'POST');
|
|
|
|
$this->assertEquals($webhook['headers']['Content-Type'], 'application/json');
|
|
|
|
$this->assertEquals($webhook['headers']['User-Agent'], 'Appwrite-Server vdev. Please report abuse at security@appwrite.io');
|
2022-05-11 01:25:49 +12:00
|
|
|
$this->assertStringContainsString('buckets.*', $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString('buckets.*.files.*', $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString('buckets.*.files.*.delete', $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString("buckets.*.files.{$fileId}", $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString("buckets.*.files.{$fileId}.delete", $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString("buckets.{$bucketId}", $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString("buckets.{$bucketId}.files.*", $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString("buckets.{$bucketId}.files.*.delete", $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString("buckets.{$bucketId}.files.{$fileId}", $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString("buckets.{$bucketId}.files.{$fileId}.delete", $webhook['headers']['X-Appwrite-Webhook-Events']);
|
2022-06-08 03:11:07 +12:00
|
|
|
$this->assertEquals($webhook['headers']['X-Appwrite-Webhook-Signature'], $signatureExpected);
|
2020-12-04 06:56:07 +13:00
|
|
|
$this->assertEquals($webhook['headers']['X-Appwrite-Webhook-Id'] ?? '', $this->getProject()['webhookId']);
|
|
|
|
$this->assertEquals($webhook['headers']['X-Appwrite-Webhook-Project-Id'] ?? '', $this->getProject()['$id']);
|
|
|
|
$this->assertEquals(empty($webhook['headers']['X-Appwrite-Webhook-User-Id'] ?? ''), ('server' === $this->getSide()));
|
2020-11-21 10:03:48 +13:00
|
|
|
$this->assertNotEmpty($webhook['data']['$id']);
|
2022-08-02 21:21:53 +12:00
|
|
|
$this->assertIsArray($webhook['data']['$permissions']);
|
2020-11-21 10:03:48 +13:00
|
|
|
$this->assertEquals($webhook['data']['name'], 'logo.png');
|
2023-02-06 09:39:41 +13:00
|
|
|
$this->assertEquals(true, (new DatetimeValidator())->isValid($webhook['data']['$createdAt']));
|
2020-11-21 10:03:48 +13:00
|
|
|
$this->assertNotEmpty($webhook['data']['signature']);
|
|
|
|
$this->assertEquals($webhook['data']['mimeType'], 'image/png');
|
|
|
|
$this->assertEquals($webhook['data']['sizeOriginal'], 47218);
|
2022-04-19 04:21:45 +12:00
|
|
|
|
2020-11-21 10:03:48 +13:00
|
|
|
return $data;
|
|
|
|
}
|
2020-12-03 11:15:20 +13:00
|
|
|
|
2022-04-19 04:21:45 +12:00
|
|
|
/**
|
2021-07-28 21:14:38 +12:00
|
|
|
* @depends testDeleteBucketFile
|
2021-07-28 19:00:12 +12:00
|
|
|
*/
|
|
|
|
public function testDeleteStorageBucket(array $data)
|
|
|
|
{
|
2022-04-19 04:21:45 +12:00
|
|
|
$bucketId = $data['bucketId'];
|
2021-07-28 19:00:12 +12:00
|
|
|
/**
|
|
|
|
* Test for SUCCESS
|
|
|
|
*/
|
2022-04-19 04:21:45 +12:00
|
|
|
$bucket = $this->client->call(Client::METHOD_DELETE, '/storage/buckets/' . $bucketId, array_merge([
|
2021-07-28 19:00:12 +12:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
]));
|
2022-04-19 04:21:45 +12:00
|
|
|
|
2021-07-28 19:00:12 +12:00
|
|
|
$this->assertEquals($bucket['headers']['status-code'], 204);
|
|
|
|
$this->assertEmpty($bucket['body']);
|
|
|
|
|
|
|
|
$webhook = $this->getLastRequest();
|
2022-06-08 19:19:50 +12:00
|
|
|
$signatureExpected = self::getWebhookSignature($webhook, $this->getProject()['signatureKey']);
|
2021-07-28 19:00:12 +12:00
|
|
|
|
|
|
|
$this->assertEquals($webhook['method'], 'POST');
|
|
|
|
$this->assertEquals($webhook['headers']['Content-Type'], 'application/json');
|
|
|
|
$this->assertEquals($webhook['headers']['User-Agent'], 'Appwrite-Server vdev. Please report abuse at security@appwrite.io');
|
2022-05-11 01:25:49 +12:00
|
|
|
$this->assertStringContainsString('buckets.*', $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString('buckets.*.delete', $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString("buckets.{$bucketId}", $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString("buckets.{$bucketId}.delete", $webhook['headers']['X-Appwrite-Webhook-Events']);
|
2022-06-08 03:11:07 +12:00
|
|
|
$this->assertEquals($webhook['headers']['X-Appwrite-Webhook-Signature'], $signatureExpected);
|
2021-07-28 19:00:12 +12:00
|
|
|
$this->assertEquals($webhook['headers']['X-Appwrite-Webhook-Id'] ?? '', $this->getProject()['webhookId']);
|
|
|
|
$this->assertEquals($webhook['headers']['X-Appwrite-Webhook-Project-Id'] ?? '', $this->getProject()['$id']);
|
|
|
|
$this->assertEquals(empty($webhook['headers']['X-Appwrite-Webhook-User-Id'] ?? ''), true);
|
|
|
|
$this->assertNotEmpty($webhook['data']['$id']);
|
|
|
|
$this->assertEquals('Test Bucket Updated', $webhook['data']['name']);
|
2021-11-19 20:17:12 +13:00
|
|
|
$this->assertEquals(true, $webhook['data']['enabled']);
|
2022-08-02 21:21:53 +12:00
|
|
|
$this->assertIsArray($webhook['data']['$permissions']);
|
2021-07-28 19:00:12 +12:00
|
|
|
}
|
|
|
|
|
2020-12-03 11:15:20 +13:00
|
|
|
public function testCreateTeam(): array
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Test for SUCCESS
|
|
|
|
*/
|
|
|
|
$team = $this->client->call(Client::METHOD_POST, '/teams', array_merge([
|
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
], $this->getHeaders()), [
|
2022-08-14 22:33:36 +12:00
|
|
|
'teamId' => ID::unique(),
|
2020-12-03 11:15:20 +13:00
|
|
|
'name' => 'Arsenal'
|
|
|
|
]);
|
|
|
|
|
2022-04-19 04:21:45 +12:00
|
|
|
$teamId = $team['body']['$id'];
|
|
|
|
|
2020-12-03 11:15:20 +13:00
|
|
|
$this->assertEquals(201, $team['headers']['status-code']);
|
|
|
|
$this->assertNotEmpty($team['body']['$id']);
|
|
|
|
|
|
|
|
$webhook = $this->getLastRequest();
|
2022-06-08 19:19:50 +12:00
|
|
|
$signatureExpected = self::getWebhookSignature($webhook, $this->getProject()['signatureKey']);
|
2020-12-03 11:15:20 +13:00
|
|
|
|
|
|
|
$this->assertEquals($webhook['method'], 'POST');
|
|
|
|
$this->assertEquals($webhook['headers']['Content-Type'], 'application/json');
|
|
|
|
$this->assertEquals($webhook['headers']['User-Agent'], 'Appwrite-Server vdev. Please report abuse at security@appwrite.io');
|
2022-05-11 01:25:49 +12:00
|
|
|
$this->assertStringContainsString('teams.*', $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString('teams.*.create', $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString("teams.{$teamId}", $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString("teams.{$teamId}.create", $webhook['headers']['X-Appwrite-Webhook-Events']);
|
2022-06-08 03:11:07 +12:00
|
|
|
$this->assertEquals($webhook['headers']['X-Appwrite-Webhook-Signature'], $signatureExpected);
|
2020-12-04 06:56:07 +13:00
|
|
|
$this->assertEquals($webhook['headers']['X-Appwrite-Webhook-Id'] ?? '', $this->getProject()['webhookId']);
|
|
|
|
$this->assertEquals($webhook['headers']['X-Appwrite-Webhook-Project-Id'] ?? '', $this->getProject()['$id']);
|
|
|
|
$this->assertEquals(empty($webhook['headers']['X-Appwrite-Webhook-User-Id'] ?? ''), ('server' === $this->getSide()));
|
2020-12-03 11:15:20 +13:00
|
|
|
$this->assertNotEmpty($webhook['data']['$id']);
|
|
|
|
$this->assertEquals('Arsenal', $webhook['data']['name']);
|
2022-02-27 22:57:09 +13:00
|
|
|
$this->assertGreaterThan(-1, $webhook['data']['total']);
|
|
|
|
$this->assertIsInt($webhook['data']['total']);
|
2023-02-06 09:39:41 +13:00
|
|
|
$this->assertEquals(true, (new DatetimeValidator())->isValid($webhook['data']['$createdAt']));
|
2020-12-03 11:15:20 +13:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test for FAILURE
|
|
|
|
*/
|
2022-04-19 04:21:45 +12:00
|
|
|
return ['teamId' => $teamId];
|
2020-12-03 11:15:20 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testCreateTeam
|
|
|
|
*/
|
|
|
|
public function testUpdateTeam($data): array
|
|
|
|
{
|
2022-04-19 04:21:45 +12:00
|
|
|
$teamId = $data['teamId'];
|
2020-12-03 11:15:20 +13:00
|
|
|
/**
|
|
|
|
* Test for SUCCESS
|
|
|
|
*/
|
2022-04-19 04:21:45 +12:00
|
|
|
$team = $this->client->call(Client::METHOD_PUT, '/teams/' . $teamId, array_merge([
|
2020-12-03 11:15:20 +13:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
], $this->getHeaders()), [
|
|
|
|
'name' => 'Demo New'
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertEquals(200, $team['headers']['status-code']);
|
|
|
|
$this->assertNotEmpty($team['body']['$id']);
|
|
|
|
|
|
|
|
$webhook = $this->getLastRequest();
|
2022-06-08 19:19:50 +12:00
|
|
|
$signatureExpected = self::getWebhookSignature($webhook, $this->getProject()['signatureKey']);
|
2020-12-03 11:15:20 +13:00
|
|
|
|
|
|
|
$this->assertEquals($webhook['method'], 'POST');
|
|
|
|
$this->assertEquals($webhook['headers']['Content-Type'], 'application/json');
|
|
|
|
$this->assertEquals($webhook['headers']['User-Agent'], 'Appwrite-Server vdev. Please report abuse at security@appwrite.io');
|
2022-05-11 01:25:49 +12:00
|
|
|
$this->assertStringContainsString('teams.*', $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString('teams.*.update', $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString("teams.{$teamId}", $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString("teams.{$teamId}.update", $webhook['headers']['X-Appwrite-Webhook-Events']);
|
2022-06-08 03:11:07 +12:00
|
|
|
$this->assertEquals($webhook['headers']['X-Appwrite-Webhook-Signature'], $signatureExpected);
|
2020-12-04 06:56:07 +13:00
|
|
|
$this->assertEquals($webhook['headers']['X-Appwrite-Webhook-Id'] ?? '', $this->getProject()['webhookId']);
|
|
|
|
$this->assertEquals($webhook['headers']['X-Appwrite-Webhook-Project-Id'] ?? '', $this->getProject()['$id']);
|
|
|
|
$this->assertEquals(empty($webhook['headers']['X-Appwrite-Webhook-User-Id'] ?? ''), ('server' === $this->getSide()));
|
2020-12-03 11:15:20 +13:00
|
|
|
$this->assertNotEmpty($webhook['data']['$id']);
|
|
|
|
$this->assertEquals('Demo New', $webhook['data']['name']);
|
2022-02-27 22:57:09 +13:00
|
|
|
$this->assertGreaterThan(-1, $webhook['data']['total']);
|
|
|
|
$this->assertIsInt($webhook['data']['total']);
|
2023-02-06 09:39:41 +13:00
|
|
|
$this->assertEquals(true, (new DatetimeValidator())->isValid($webhook['data']['$createdAt']));
|
2020-12-03 11:15:20 +13:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test for FAILURE
|
|
|
|
*/
|
|
|
|
return ['teamId' => $team['body']['$id']];
|
|
|
|
}
|
|
|
|
|
2023-03-07 03:24:02 +13:00
|
|
|
/**
|
|
|
|
* @depends testCreateTeam
|
|
|
|
*/
|
|
|
|
public function testUpdateTeamPrefs(array $data): array
|
|
|
|
{
|
|
|
|
$id = $data['teamId'] ?? '';
|
|
|
|
|
2023-03-24 01:06:48 +13:00
|
|
|
$team = $this->client->call(Client::METHOD_PUT, '/teams/' . $id . '/prefs', array_merge([
|
2023-03-07 03:24:02 +13:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
], $this->getHeaders()), [
|
|
|
|
'prefs' => [
|
|
|
|
'prefKey1' => 'prefValue1',
|
|
|
|
'prefKey2' => 'prefValue2',
|
|
|
|
]
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertEquals($team['headers']['status-code'], 200);
|
|
|
|
$this->assertIsArray($team['body']);
|
|
|
|
|
|
|
|
$webhook = $this->getLastRequest();
|
|
|
|
$signatureKey = $this->getProject()['signatureKey'];
|
|
|
|
$payload = json_encode($webhook['data']);
|
|
|
|
$url = $webhook['url'];
|
|
|
|
$signatureExpected = base64_encode(hash_hmac('sha1', $url . $payload, $signatureKey, true));
|
|
|
|
|
|
|
|
$this->assertEquals($webhook['method'], 'POST');
|
|
|
|
$this->assertEquals($webhook['headers']['Content-Type'], 'application/json');
|
|
|
|
$this->assertEquals($webhook['headers']['User-Agent'], 'Appwrite-Server vdev. Please report abuse at security@appwrite.io');
|
|
|
|
$this->assertStringContainsString('teams.*', $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString('teams.*.update', $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString('teams.*.update.prefs', $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString("teams.{$id}", $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString("teams.{$id}.update", $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString("teams.{$id}.update.prefs", $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertEquals($webhook['headers']['X-Appwrite-Webhook-Signature'], $signatureExpected);
|
|
|
|
$this->assertEquals($webhook['headers']['X-Appwrite-Webhook-Id'] ?? '', $this->getProject()['webhookId']);
|
|
|
|
$this->assertEquals($webhook['headers']['X-Appwrite-Webhook-Project-Id'] ?? '', $this->getProject()['$id']);
|
|
|
|
$this->assertEquals(empty($webhook['headers']['X-Appwrite-Webhook-User-Id'] ?? ''), ('server' === $this->getSide()));
|
|
|
|
$this->assertEquals($webhook['data'], [
|
|
|
|
'prefKey1' => 'prefValue1',
|
|
|
|
'prefKey2' => 'prefValue2',
|
|
|
|
]);
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
2020-12-03 11:15:20 +13:00
|
|
|
public function testDeleteTeam(): array
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Test for SUCCESS
|
|
|
|
*/
|
|
|
|
$team = $this->client->call(Client::METHOD_POST, '/teams', array_merge([
|
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
], $this->getHeaders()), [
|
2022-08-14 22:33:36 +12:00
|
|
|
'teamId' => ID::unique(),
|
2020-12-03 11:15:20 +13:00
|
|
|
'name' => 'Chelsea'
|
|
|
|
]);
|
|
|
|
|
2022-04-19 04:21:45 +12:00
|
|
|
$teamId = $team['body']['$id'];
|
|
|
|
|
2020-12-03 11:15:20 +13:00
|
|
|
$this->assertEquals(201, $team['headers']['status-code']);
|
|
|
|
$this->assertNotEmpty($team['body']['$id']);
|
|
|
|
|
2022-04-19 04:21:45 +12:00
|
|
|
$team = $this->client->call(Client::METHOD_DELETE, '/teams/' . $team['body']['$id'], array_merge([
|
2020-12-03 11:15:20 +13:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
], $this->getHeaders()));
|
|
|
|
|
|
|
|
$webhook = $this->getLastRequest();
|
2022-06-08 19:19:50 +12:00
|
|
|
$signatureExpected = self::getWebhookSignature($webhook, $this->getProject()['signatureKey']);
|
2020-12-03 11:15:20 +13:00
|
|
|
|
|
|
|
$this->assertEquals($webhook['method'], 'POST');
|
|
|
|
$this->assertEquals($webhook['headers']['Content-Type'], 'application/json');
|
|
|
|
$this->assertEquals($webhook['headers']['User-Agent'], 'Appwrite-Server vdev. Please report abuse at security@appwrite.io');
|
2022-05-11 01:25:49 +12:00
|
|
|
$this->assertStringContainsString('teams.*', $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString('teams.*.delete', $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString("teams.{$teamId}", $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString("teams.{$teamId}.delete", $webhook['headers']['X-Appwrite-Webhook-Events']);
|
2022-06-08 03:11:07 +12:00
|
|
|
$this->assertEquals($webhook['headers']['X-Appwrite-Webhook-Signature'], $signatureExpected);
|
2020-12-04 06:56:07 +13:00
|
|
|
$this->assertEquals($webhook['headers']['X-Appwrite-Webhook-Id'] ?? '', $this->getProject()['webhookId']);
|
|
|
|
$this->assertEquals($webhook['headers']['X-Appwrite-Webhook-Project-Id'] ?? '', $this->getProject()['$id']);
|
|
|
|
$this->assertEquals(empty($webhook['headers']['X-Appwrite-Webhook-User-Id'] ?? ''), ('server' === $this->getSide()));
|
2020-12-03 11:15:20 +13:00
|
|
|
$this->assertNotEmpty($webhook['data']['$id']);
|
|
|
|
$this->assertEquals('Chelsea', $webhook['data']['name']);
|
2022-02-27 22:57:09 +13:00
|
|
|
$this->assertGreaterThan(-1, $webhook['data']['total']);
|
|
|
|
$this->assertIsInt($webhook['data']['total']);
|
2023-02-06 09:39:41 +13:00
|
|
|
$this->assertEquals(true, (new DatetimeValidator())->isValid($webhook['data']['$createdAt']));
|
2020-12-03 11:15:20 +13:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test for FAILURE
|
|
|
|
*/
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testCreateTeam
|
|
|
|
*/
|
|
|
|
public function testCreateTeamMembership($data): array
|
|
|
|
{
|
2022-04-19 04:21:45 +12:00
|
|
|
$teamId = $data['teamId'] ?? '';
|
|
|
|
$email = uniqid() . 'friend@localhost.test';
|
2020-12-03 11:15:20 +13:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test for SUCCESS
|
|
|
|
*/
|
2022-04-19 04:21:45 +12:00
|
|
|
$team = $this->client->call(Client::METHOD_POST, '/teams/' . $teamId . '/memberships', array_merge([
|
2020-12-03 11:15:20 +13:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
], $this->getHeaders()), [
|
|
|
|
'email' => $email,
|
|
|
|
'name' => 'Friend User',
|
|
|
|
'roles' => ['admin', 'editor'],
|
|
|
|
'url' => 'http://localhost:5000/join-us#title'
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertEquals(201, $team['headers']['status-code']);
|
|
|
|
$this->assertNotEmpty($team['body']['$id']);
|
|
|
|
|
|
|
|
$lastEmail = $this->getLastEmail();
|
|
|
|
|
|
|
|
$secret = substr($lastEmail['text'], strpos($lastEmail['text'], '&secret=', 0) + 8, 256);
|
2022-04-19 04:21:45 +12:00
|
|
|
$membershipId = $team['body']['$id'];
|
2020-12-03 11:15:20 +13:00
|
|
|
|
|
|
|
$webhook = $this->getLastRequest();
|
2022-06-08 19:19:50 +12:00
|
|
|
$signatureExpected = self::getWebhookSignature($webhook, $this->getProject()['signatureKey']);
|
2020-12-03 11:15:20 +13:00
|
|
|
|
|
|
|
$this->assertEquals($webhook['method'], 'POST');
|
|
|
|
$this->assertEquals($webhook['headers']['Content-Type'], 'application/json');
|
|
|
|
$this->assertEquals($webhook['headers']['User-Agent'], 'Appwrite-Server vdev. Please report abuse at security@appwrite.io');
|
2022-05-11 01:25:49 +12:00
|
|
|
$this->assertStringContainsString('teams.*', $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString('teams.*.memberships.*', $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString('teams.*.memberships.*.create', $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString("teams.*.memberships.{$membershipId}", $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString("teams.*.memberships.{$membershipId}.create", $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString("teams.{$teamId}", $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString("teams.{$teamId}.memberships.*", $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString("teams.{$teamId}.memberships.*.create", $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString("teams.{$teamId}.memberships.{$membershipId}", $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString("teams.{$teamId}.memberships.{$membershipId}.create", $webhook['headers']['X-Appwrite-Webhook-Events']);
|
2022-06-08 03:11:07 +12:00
|
|
|
$this->assertEquals($webhook['headers']['X-Appwrite-Webhook-Signature'], $signatureExpected);
|
2020-12-04 06:56:07 +13:00
|
|
|
$this->assertEquals($webhook['headers']['X-Appwrite-Webhook-Id'] ?? '', $this->getProject()['webhookId']);
|
|
|
|
$this->assertEquals($webhook['headers']['X-Appwrite-Webhook-Project-Id'] ?? '', $this->getProject()['$id']);
|
|
|
|
$this->assertEquals(empty($webhook['headers']['X-Appwrite-Webhook-User-Id'] ?? ''), ('server' === $this->getSide()));
|
2020-12-03 11:15:20 +13:00
|
|
|
$this->assertNotEmpty($webhook['data']['$id']);
|
|
|
|
$this->assertNotEmpty($webhook['data']['userId']);
|
|
|
|
$this->assertNotEmpty($webhook['data']['teamId']);
|
|
|
|
$this->assertCount(2, $webhook['data']['roles']);
|
2023-02-06 09:39:41 +13:00
|
|
|
$this->assertEquals(true, (new DatetimeValidator())->isValid($webhook['data']['invited']));
|
2020-12-03 11:15:20 +13:00
|
|
|
$this->assertEquals(('server' === $this->getSide()), $webhook['data']['confirm']);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test for FAILURE
|
|
|
|
*/
|
|
|
|
return [
|
2022-04-19 04:21:45 +12:00
|
|
|
'teamId' => $teamId,
|
2020-12-03 11:15:20 +13:00
|
|
|
'secret' => $secret,
|
2022-04-19 04:21:45 +12:00
|
|
|
'membershipId' => $membershipId,
|
2020-12-03 11:15:20 +13:00
|
|
|
'userId' => $webhook['data']['userId'],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-04-19 04:21:45 +12:00
|
|
|
* @depends testCreateTeamMembership
|
2020-12-03 11:15:20 +13:00
|
|
|
*/
|
2022-04-19 04:21:45 +12:00
|
|
|
public function testDeleteTeamMembership($data): void
|
2020-12-03 11:15:20 +13:00
|
|
|
{
|
2022-04-19 04:21:45 +12:00
|
|
|
$teamId = $data['teamId'] ?? '';
|
|
|
|
$email = uniqid() . 'friend@localhost.test';
|
2020-12-03 11:15:20 +13:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test for SUCCESS
|
|
|
|
*/
|
2022-04-19 04:21:45 +12:00
|
|
|
$team = $this->client->call(Client::METHOD_POST, '/teams/' . $teamId . '/memberships', array_merge([
|
2020-12-03 11:15:20 +13:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
], $this->getHeaders()), [
|
|
|
|
'email' => $email,
|
|
|
|
'name' => 'Friend User',
|
|
|
|
'roles' => ['admin', 'editor'],
|
|
|
|
'url' => 'http://localhost:5000/join-us#title'
|
|
|
|
]);
|
|
|
|
|
2022-04-19 04:21:45 +12:00
|
|
|
$membershipId = $team['body']['$id'] ?? '';
|
|
|
|
|
2020-12-03 11:15:20 +13:00
|
|
|
$this->assertEquals(201, $team['headers']['status-code']);
|
|
|
|
$this->assertNotEmpty($team['body']['$id']);
|
2022-04-19 04:21:45 +12:00
|
|
|
|
|
|
|
$team = $this->client->call(Client::METHOD_DELETE, '/teams/' . $teamId . '/memberships/' . $team['body']['$id'], array_merge([
|
2020-12-03 11:15:20 +13:00
|
|
|
'origin' => 'http://localhost',
|
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
], $this->getHeaders()));
|
|
|
|
|
|
|
|
$this->assertEquals(204, $team['headers']['status-code']);
|
|
|
|
|
|
|
|
$webhook = $this->getLastRequest();
|
2022-06-08 19:19:50 +12:00
|
|
|
$signatureExpected = self::getWebhookSignature($webhook, $this->getProject()['signatureKey']);
|
2020-12-03 11:15:20 +13:00
|
|
|
|
|
|
|
$this->assertEquals($webhook['method'], 'POST');
|
|
|
|
$this->assertEquals($webhook['headers']['Content-Type'], 'application/json');
|
|
|
|
$this->assertEquals($webhook['headers']['User-Agent'], 'Appwrite-Server vdev. Please report abuse at security@appwrite.io');
|
2022-05-11 01:25:49 +12:00
|
|
|
$this->assertStringContainsString('teams.*', $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString('teams.*.memberships.*', $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString('teams.*.memberships.*.delete', $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString("teams.*.memberships.{$membershipId}", $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString("teams.*.memberships.{$membershipId}.delete", $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString("teams.{$teamId}", $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString("teams.{$teamId}.memberships.*", $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString("teams.{$teamId}.memberships.*.delete", $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString("teams.{$teamId}.memberships.{$membershipId}", $webhook['headers']['X-Appwrite-Webhook-Events']);
|
|
|
|
$this->assertStringContainsString("teams.{$teamId}.memberships.{$membershipId}.delete", $webhook['headers']['X-Appwrite-Webhook-Events']);
|
2022-06-08 03:11:07 +12:00
|
|
|
$this->assertEquals($webhook['headers']['X-Appwrite-Webhook-Signature'], $signatureExpected);
|
2020-12-04 06:56:07 +13:00
|
|
|
$this->assertEquals($webhook['headers']['X-Appwrite-Webhook-Id'] ?? '', $this->getProject()['webhookId']);
|
|
|
|
$this->assertEquals($webhook['headers']['X-Appwrite-Webhook-Project-Id'] ?? '', $this->getProject()['$id']);
|
|
|
|
$this->assertEquals(empty($webhook['headers']['X-Appwrite-Webhook-User-Id'] ?? ''), ('server' === $this->getSide()));
|
2020-12-03 11:15:20 +13:00
|
|
|
$this->assertNotEmpty($webhook['data']['$id']);
|
|
|
|
$this->assertNotEmpty($webhook['data']['userId']);
|
|
|
|
$this->assertNotEmpty($webhook['data']['teamId']);
|
|
|
|
$this->assertCount(2, $webhook['data']['roles']);
|
2023-02-06 09:39:41 +13:00
|
|
|
$this->assertEquals(true, (new DatetimeValidator())->isValid($webhook['data']['invited']));
|
2020-12-03 11:15:20 +13:00
|
|
|
$this->assertEquals(('server' === $this->getSide()), $webhook['data']['confirm']);
|
|
|
|
}
|
2024-01-18 22:49:57 +13:00
|
|
|
|
|
|
|
public function testCreateWebhookWithPrivateDomain(): void
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Test for FAILURE
|
|
|
|
*/
|
|
|
|
$projectId = $this->getProject()['$id'];
|
|
|
|
$webhook = $this->client->call(Client::METHOD_POST, '/projects/' . $projectId . '/webhooks', [
|
|
|
|
'origin' => 'http://localhost',
|
|
|
|
'content-type' => 'application/json',
|
|
|
|
'cookie' => 'a_session_console=' . $this->getRoot()['session'],
|
|
|
|
'x-appwrite-project' => 'console',
|
|
|
|
], [
|
|
|
|
'name' => 'Webhook Test',
|
|
|
|
'enabled' => true,
|
|
|
|
'events' => [
|
|
|
|
'databases.*',
|
|
|
|
'functions.*',
|
|
|
|
'buckets.*',
|
|
|
|
'teams.*',
|
|
|
|
'users.*'
|
|
|
|
],
|
|
|
|
'url' => 'http://localhost/webhook', // private domains not allowed
|
|
|
|
'security' => false,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertEquals(400, $webhook['headers']['status-code']);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testUpdateWebhookWithPrivateDomain(): void
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Test for FAILURE
|
|
|
|
*/
|
|
|
|
$projectId = $this->getProject()['$id'];
|
|
|
|
$webhookId = $this->getProject()['webhookId'];
|
|
|
|
$webhook = $this->client->call(Client::METHOD_PUT, '/projects/' . $projectId . '/webhooks/' . $webhookId, [
|
|
|
|
'origin' => 'http://localhost',
|
|
|
|
'content-type' => 'application/json',
|
|
|
|
'cookie' => 'a_session_console=' . $this->getRoot()['session'],
|
|
|
|
'x-appwrite-project' => 'console',
|
|
|
|
], [
|
|
|
|
'name' => 'Webhook Test',
|
|
|
|
'enabled' => true,
|
|
|
|
'events' => [
|
|
|
|
'databases.*',
|
|
|
|
'functions.*',
|
|
|
|
'buckets.*',
|
|
|
|
'teams.*',
|
|
|
|
'users.*'
|
|
|
|
],
|
|
|
|
'url' => 'http://localhost/webhook', // private domains not allowed
|
|
|
|
'security' => false,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertEquals(400, $webhook['headers']['status-code']);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testCreateCollection
|
|
|
|
*/
|
|
|
|
public function testWebhookAutoDisable(array $data): void
|
|
|
|
{
|
|
|
|
$projectId = $this->getProject()['$id'];
|
|
|
|
$webhookId = $this->getProject()['webhookId'];
|
|
|
|
$databaseId = $data['databaseId'];
|
|
|
|
|
|
|
|
$webhook = $this->client->call(Client::METHOD_PUT, '/projects/' . $projectId . '/webhooks/' . $webhookId, [
|
|
|
|
'origin' => 'http://localhost',
|
|
|
|
'content-type' => 'application/json',
|
|
|
|
'cookie' => 'a_session_console=' . $this->getRoot()['session'],
|
|
|
|
'x-appwrite-project' => 'console',
|
|
|
|
], [
|
|
|
|
'name' => 'Webhook Test',
|
|
|
|
'enabled' => true,
|
|
|
|
'events' => [
|
|
|
|
'databases.*',
|
|
|
|
'functions.*',
|
|
|
|
'buckets.*',
|
|
|
|
'teams.*',
|
|
|
|
'users.*'
|
|
|
|
],
|
|
|
|
'url' => 'http://appwrite-non-existing-domain.com', // set non-existent URL
|
|
|
|
'security' => false,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertEquals(200, $webhook['headers']['status-code']);
|
|
|
|
$this->assertNotEmpty($webhook['body']);
|
2024-01-19 01:42:58 +13:00
|
|
|
|
2024-01-18 22:49:57 +13:00
|
|
|
// trigger webhook for failure event 10 times
|
|
|
|
for ($i = 0; $i < 10; $i++) {
|
|
|
|
$newCollection = $this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/collections', array_merge([
|
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
]), [
|
|
|
|
'collectionId' => ID::unique(),
|
|
|
|
'name' => 'newCollection' . $i,
|
|
|
|
'permissions' => [
|
|
|
|
Permission::read(Role::any()),
|
|
|
|
Permission::create(Role::any()),
|
|
|
|
Permission::update(Role::any()),
|
|
|
|
Permission::delete(Role::any()),
|
|
|
|
],
|
|
|
|
'documentSecurity' => true,
|
|
|
|
]);
|
2024-01-19 01:42:58 +13:00
|
|
|
|
2024-01-18 22:49:57 +13:00
|
|
|
$this->assertEquals($newCollection['headers']['status-code'], 201);
|
|
|
|
$this->assertNotEmpty($newCollection['body']['$id']);
|
|
|
|
}
|
|
|
|
|
|
|
|
sleep(10);
|
|
|
|
|
|
|
|
$webhook = $this->client->call(Client::METHOD_GET, '/projects/' . $projectId . '/webhooks/' . $webhookId, array_merge([
|
|
|
|
'origin' => 'http://localhost',
|
|
|
|
'content-type' => 'application/json',
|
|
|
|
'cookie' => 'a_session_console=' . $this->getRoot()['session'],
|
|
|
|
'x-appwrite-project' => 'console',
|
|
|
|
]));
|
|
|
|
|
|
|
|
// assert that the webhook is now disabled after 10 consecutive failures
|
|
|
|
$this->assertEquals($webhook['body']['enabled'], false);
|
|
|
|
$this->assertEquals($webhook['body']['attempts'], 10);
|
|
|
|
}
|
2022-04-19 04:21:45 +12:00
|
|
|
}
|