From 192ba00e3fe0020e1f0a2580f2f481c8b8999b86 Mon Sep 17 00:00:00 2001 From: kodumbeats Date: Thu, 4 Mar 2021 12:18:50 -0500 Subject: [PATCH 01/10] Add event for users.update.prefs --- app/config/events.php | 5 +++++ app/controllers/api/users.php | 1 + 2 files changed, 6 insertions(+) diff --git a/app/config/events.php b/app/config/events.php index 2bb3862fdf..4893f6f188 100644 --- a/app/config/events.php +++ b/app/config/events.php @@ -117,6 +117,11 @@ return [ 'model' => Response::MODEL_USER, 'note' => 'version >= 0.7', ], + 'users.update.prefs' => [ + 'description' => 'This event triggers when a user preference is updated from the users API.', + 'model' => Response::MODEL_USER, + 'note' => 'version >= 0.7', + ], 'users.update.status' => [ 'description' => 'This event triggers when a user status is updated from the users API.', 'model' => Response::MODEL_USER, diff --git a/app/controllers/api/users.php b/app/controllers/api/users.php index efb0041cee..0af3ec03a1 100644 --- a/app/controllers/api/users.php +++ b/app/controllers/api/users.php @@ -373,6 +373,7 @@ App::patch('/v1/users/:userId/status') App::patch('/v1/users/:userId/prefs') ->desc('Update User Preferences') ->groups(['api', 'users']) + ->label('event', 'users.update.prefs') ->label('scope', 'users.write') ->label('sdk.platform', [APP_PLATFORM_SERVER]) ->label('sdk.namespace', 'users') From 440a67e6d2c7eaa75066bd22e1ade47cf3364024 Mon Sep 17 00:00:00 2001 From: kodumbeats Date: Thu, 4 Mar 2021 13:47:02 -0500 Subject: [PATCH 02/10] Add e2e test for users.update.prefs --- .../Webhooks/WebhooksCustomServerTest.php | 43 ++++++++++++++++++- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/tests/e2e/Services/Webhooks/WebhooksCustomServerTest.php b/tests/e2e/Services/Webhooks/WebhooksCustomServerTest.php index 360dee2cb3..5869bddbbe 100644 --- a/tests/e2e/Services/Webhooks/WebhooksCustomServerTest.php +++ b/tests/e2e/Services/Webhooks/WebhooksCustomServerTest.php @@ -186,7 +186,46 @@ class WebhooksCustomServerTest extends Scope */ return ['userId' => $user['body']['$id'], 'name' => $user['body']['name'], 'email' => $user['body']['email']]; } - + + /** + * @depends testCreateUser + */ + public function testUpdateUserPrefs(array $data):array + { + /** + * Test for SUCCESS + */ + $user = $this->client->call(Client::METHOD_PATCH, '/users/' . $data['userId'] . '/prefs', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), [ + 'prefs' => ['test' => true], + ]); + + $this->assertEquals($user['headers']['status-code'], 200); + $this->assertNotEmpty($user['body']['prefs']); + + $webhook = $this->getLastRequest(); + + $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->assertEquals($webhook['headers']['X-Appwrite-Webhook-Event'], 'users.update.prefs'); + $this->assertEquals($webhook['headers']['X-Appwrite-Webhook-Signature'], 'not-yet-implemented'); + $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->assertNotEmpty($webhook['data']['$id']); + $this->assertEquals($webhook['data']['name'], $data['name']); + $this->assertIsInt($webhook['data']['registration']); + $this->assertEquals($webhook['data']['status'], 0); + $this->assertEquals($webhook['data']['email'], $data['email']); + $this->assertEquals($webhook['data']['emailVerification'], false); + $this->assertEquals($webhook['data']['prefs'], ["test" => true]); + + return $data; + } + /** * @depends testCreateUser */ @@ -261,4 +300,4 @@ class WebhooksCustomServerTest extends Scope return $data; } -} \ No newline at end of file +} From 0546a0e428483c85574d84125adcd5d58a292f1c Mon Sep 17 00:00:00 2001 From: kodumbeats Date: Fri, 5 Mar 2021 13:36:30 -0500 Subject: [PATCH 03/10] Return correct response model --- app/config/events.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/config/events.php b/app/config/events.php index 4893f6f188..601b502163 100644 --- a/app/config/events.php +++ b/app/config/events.php @@ -119,7 +119,7 @@ return [ ], 'users.update.prefs' => [ 'description' => 'This event triggers when a user preference is updated from the users API.', - 'model' => Response::MODEL_USER, + 'model' => Response::MODEL_ANY, 'note' => 'version >= 0.7', ], 'users.update.status' => [ From 56f5c73126d2d9c02278040819be8b30764d602f Mon Sep 17 00:00:00 2001 From: kodumbeats Date: Sun, 7 Mar 2021 08:09:45 -0500 Subject: [PATCH 04/10] Update tests --- .../Webhooks/WebhooksCustomServerTest.php | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/tests/e2e/Services/Webhooks/WebhooksCustomServerTest.php b/tests/e2e/Services/Webhooks/WebhooksCustomServerTest.php index 5869bddbbe..9b32b75609 100644 --- a/tests/e2e/Services/Webhooks/WebhooksCustomServerTest.php +++ b/tests/e2e/Services/Webhooks/WebhooksCustomServerTest.php @@ -199,11 +199,10 @@ class WebhooksCustomServerTest extends Scope 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], ], $this->getHeaders()), [ - 'prefs' => ['test' => true], + 'prefs' => ['a' => 'b'] ]); $this->assertEquals($user['headers']['status-code'], 200); - $this->assertNotEmpty($user['body']['prefs']); $webhook = $this->getLastRequest(); @@ -215,13 +214,7 @@ class WebhooksCustomServerTest extends Scope $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->assertNotEmpty($webhook['data']['$id']); - $this->assertEquals($webhook['data']['name'], $data['name']); - $this->assertIsInt($webhook['data']['registration']); - $this->assertEquals($webhook['data']['status'], 0); - $this->assertEquals($webhook['data']['email'], $data['email']); - $this->assertEquals($webhook['data']['emailVerification'], false); - $this->assertEquals($webhook['data']['prefs'], ["test" => true]); + $this->assertEquals($webhook['data']['a'], 'b'); return $data; } @@ -260,7 +253,7 @@ class WebhooksCustomServerTest extends Scope $this->assertEquals($webhook['data']['status'], 2); $this->assertEquals($webhook['data']['email'], $data['email']); $this->assertEquals($webhook['data']['emailVerification'], false); - $this->assertEquals($webhook['data']['prefs'], []); + $this->assertEquals($webhook['data']['prefs']['a'], 'b'); return $data; } @@ -296,7 +289,7 @@ class WebhooksCustomServerTest extends Scope $this->assertEquals($webhook['data']['status'], 2); $this->assertEquals($webhook['data']['email'], $data['email']); $this->assertEquals($webhook['data']['emailVerification'], false); - $this->assertEquals($webhook['data']['prefs'], []); + $this->assertEquals($webhook['data']['prefs']['a'], 'b'); return $data; } From 68b1511672ee22c0a79c97996e483ec540d87a52 Mon Sep 17 00:00:00 2001 From: kodumbeats Date: Mon, 8 Mar 2021 10:16:28 -0500 Subject: [PATCH 05/10] Chain tests together --- tests/e2e/Services/Webhooks/WebhooksCustomServerTest.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/e2e/Services/Webhooks/WebhooksCustomServerTest.php b/tests/e2e/Services/Webhooks/WebhooksCustomServerTest.php index 9b32b75609..dbd1709802 100644 --- a/tests/e2e/Services/Webhooks/WebhooksCustomServerTest.php +++ b/tests/e2e/Services/Webhooks/WebhooksCustomServerTest.php @@ -203,7 +203,6 @@ class WebhooksCustomServerTest extends Scope ]); $this->assertEquals($user['headers']['status-code'], 200); - $webhook = $this->getLastRequest(); $this->assertEquals($webhook['method'], 'POST'); @@ -220,7 +219,7 @@ class WebhooksCustomServerTest extends Scope } /** - * @depends testCreateUser + * @depends testUpdateUserPrefs */ public function testUpdateUserStatus(array $data):array { From 0d75dd362343e2635e07800b1e9eacb0c142003d Mon Sep 17 00:00:00 2001 From: kodumbeats Date: Mon, 8 Mar 2021 15:48:45 -0500 Subject: [PATCH 06/10] Prove user prefs update before webhook --- tests/e2e/Services/Webhooks/WebhooksCustomServerTest.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/e2e/Services/Webhooks/WebhooksCustomServerTest.php b/tests/e2e/Services/Webhooks/WebhooksCustomServerTest.php index dbd1709802..d2d5549134 100644 --- a/tests/e2e/Services/Webhooks/WebhooksCustomServerTest.php +++ b/tests/e2e/Services/Webhooks/WebhooksCustomServerTest.php @@ -203,6 +203,8 @@ class WebhooksCustomServerTest extends Scope ]); $this->assertEquals($user['headers']['status-code'], 200); + $this->assertEquals($user['body']['a'], 'b'); + $webhook = $this->getLastRequest(); $this->assertEquals($webhook['method'], 'POST'); From c6270dcca50147e8604794df78ec8b0feffd1fb9 Mon Sep 17 00:00:00 2001 From: kodumbeats Date: Fri, 12 Mar 2021 08:06:53 -0500 Subject: [PATCH 07/10] Add webhook event to scope --- tests/e2e/Scopes/ProjectCustom.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/e2e/Scopes/ProjectCustom.php b/tests/e2e/Scopes/ProjectCustom.php index e1059f6afa..0596aee09f 100644 --- a/tests/e2e/Scopes/ProjectCustom.php +++ b/tests/e2e/Scopes/ProjectCustom.php @@ -117,6 +117,7 @@ trait ProjectCustom 'storage.files.update', 'storage.files.delete', 'users.create', + 'users.update.prefs', 'users.update.status', 'users.delete', 'users.sessions.delete', From cd247debd482ccf8e4c0d4da1595b89c7cb5b6f2 Mon Sep 17 00:00:00 2001 From: kodumbeats Date: Fri, 12 Mar 2021 09:28:34 -0500 Subject: [PATCH 08/10] Changelog for event users.update.prefs --- CHANGES.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGES.md b/CHANGES.md index 8670aa1d7e..bd32e25561 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -27,6 +27,7 @@ ## Bug Fixes - Updated missing storage env vars +- Added missing event for users.update.prefs #952 ## Security From 5361ddac1b23d693181056dc642d257bdf3afb00 Mon Sep 17 00:00:00 2001 From: kodumbeats Date: Fri, 12 Mar 2021 09:34:11 -0500 Subject: [PATCH 09/10] Revert "Changelog for event users.update.prefs" This reverts commit cd247debd482ccf8e4c0d4da1595b89c7cb5b6f2. --- CHANGES.md | 1 - 1 file changed, 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index bd32e25561..8670aa1d7e 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -27,7 +27,6 @@ ## Bug Fixes - Updated missing storage env vars -- Added missing event for users.update.prefs #952 ## Security From 6d11df4e84de1dd161251dc5c98138b1fb0643dc Mon Sep 17 00:00:00 2001 From: kodumbeats Date: Fri, 12 Mar 2021 09:35:28 -0500 Subject: [PATCH 10/10] Add users.update.prefs to changelog --- CHANGES.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGES.md b/CHANGES.md index d0cd072bc4..bf14eedb70 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -29,6 +29,7 @@ - Updated missing storage env vars - Fixed a bug, that Response format header was not added in the access-control-allow-header list. - Fixed a bug where countryName is unknown on sessions (#933) +- Added missing event users.update.prefs (#952) ## Security