diff --git a/src/Appwrite/Platform/Modules/Presences/HTTP/Delete.php b/src/Appwrite/Platform/Modules/Presences/HTTP/Delete.php index e6ce848a5a..f6af01de58 100644 --- a/src/Appwrite/Platform/Modules/Presences/HTTP/Delete.php +++ b/src/Appwrite/Platform/Modules/Presences/HTTP/Delete.php @@ -30,7 +30,7 @@ class Delete extends Base ->setHttpPath('/v1/presences/:presenceId') ->desc('Delete presence') ->groups(['api', 'presences']) - ->label('scope', 'documents.write') + ->label('scope', 'users.write') ->label('sdk', new Method( namespace: 'presences', group: 'presences', diff --git a/src/Appwrite/Platform/Modules/Presences/HTTP/Get.php b/src/Appwrite/Platform/Modules/Presences/HTTP/Get.php index 25e0f1ec81..240a2f7bf4 100644 --- a/src/Appwrite/Platform/Modules/Presences/HTTP/Get.php +++ b/src/Appwrite/Platform/Modules/Presences/HTTP/Get.php @@ -29,7 +29,7 @@ class Get extends Base ->setHttpPath('/v1/presences/:presenceId') ->desc('Get presence') ->groups(['api', 'presences']) - ->label('scope', 'documents.read') + ->label('scope', 'users.read') ->label('sdk', new Method( namespace: 'presences', group: 'presences', diff --git a/src/Appwrite/Platform/Modules/Presences/HTTP/Update.php b/src/Appwrite/Platform/Modules/Presences/HTTP/Update.php index 5c3cb4413f..71cd654068 100644 --- a/src/Appwrite/Platform/Modules/Presences/HTTP/Update.php +++ b/src/Appwrite/Platform/Modules/Presences/HTTP/Update.php @@ -34,7 +34,7 @@ class Update extends PresenceAction ->setHttpPath('/v1/presences/:presenceId') ->desc('Update presence') ->groups(['api', 'presences']) - ->label('scope', 'documents.write') + ->label('scope', 'users.write') ->label('sdk', new Method( namespace: 'presences', group: 'presences', diff --git a/src/Appwrite/Platform/Modules/Presences/HTTP/Upsert.php b/src/Appwrite/Platform/Modules/Presences/HTTP/Upsert.php index 03b702b2c2..ddfb6285fe 100644 --- a/src/Appwrite/Platform/Modules/Presences/HTTP/Upsert.php +++ b/src/Appwrite/Platform/Modules/Presences/HTTP/Upsert.php @@ -40,7 +40,7 @@ class Upsert extends PresenceAction ->setHttpPath('/v1/presences/:presenceId') ->desc('Upsert presence') ->groups(['api', 'presences']) - ->label('scope', 'documents.write') + ->label('scope', 'users.write') ->label('sdk', new Method( namespace: 'presences', group: 'presences', diff --git a/src/Appwrite/Platform/Modules/Presences/HTTP/XList.php b/src/Appwrite/Platform/Modules/Presences/HTTP/XList.php index 18de610844..aab3958bdb 100644 --- a/src/Appwrite/Platform/Modules/Presences/HTTP/XList.php +++ b/src/Appwrite/Platform/Modules/Presences/HTTP/XList.php @@ -37,7 +37,7 @@ class XList extends Base ->setHttpPath('/v1/presences') ->desc('List presences') ->groups(['api', 'presences']) - ->label('scope', 'documents.read') + ->label('scope', 'users.read') ->label('sdk', new Method( namespace: 'presences', group: 'presences', diff --git a/tests/e2e/Services/Presence/PresenceBase.php b/tests/e2e/Services/Presence/PresenceBase.php index ff935707e8..88360e8acc 100644 --- a/tests/e2e/Services/Presence/PresenceBase.php +++ b/tests/e2e/Services/Presence/PresenceBase.php @@ -9,6 +9,23 @@ use Utopia\Database\Query; trait PresenceBase { private static array $presenceCache = []; + private static array $presenceApiKeyCache = []; + + protected function getPresenceApiKey(): string + { + $projectId = $this->getProject()['$id']; + + if (!empty(self::$presenceApiKeyCache[$projectId])) { + return self::$presenceApiKeyCache[$projectId]; + } + + self::$presenceApiKeyCache[$projectId] = $this->getNewKey([ + 'users.read', + 'users.write', + ]); + + return self::$presenceApiKeyCache[$projectId]; + } protected function setupPresence(array $overrides = []): array { @@ -34,7 +51,7 @@ trait PresenceBase [ 'content-type' => 'application/json', 'x-appwrite-project' => $projectId, - 'x-appwrite-key' => $this->getProject()['apiKey'], + 'x-appwrite-key' => $this->getPresenceApiKey(), ], $payload ); @@ -62,7 +79,7 @@ trait PresenceBase \array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], - ], $this->getHeaders()), + ], $this->getHeaders(false)), [ 'status' => 'online', 'metadata' => ['device' => 'web'], @@ -81,7 +98,7 @@ trait PresenceBase \array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], - ], $this->getHeaders()) + ], $this->getHeaders(false)) ); $this->assertEquals(200, $get['headers']['status-code']); @@ -99,7 +116,7 @@ trait PresenceBase \array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], - ], $this->getHeaders()) + ], $this->getHeaders(false)) ); $this->assertEquals(401, $list['headers']['status-code']); @@ -114,7 +131,7 @@ trait PresenceBase \array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], - ], $this->getHeaders()), + ], $this->getHeaders(false)), [ 'queries' => [ Query::equal('userId', [$presence['userId']])->toString(), @@ -138,7 +155,7 @@ trait PresenceBase \array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], - ], $this->getHeaders()), + ], $this->getHeaders(false)), [ 'status' => 'busy', 'metadata' => ['source' => 'update'], @@ -169,7 +186,7 @@ trait PresenceBase \array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], - ], $this->getHeaders()), + ], $this->getHeaders(false)), $payload ); @@ -187,7 +204,7 @@ trait PresenceBase \array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], - ], $this->getHeaders()) + ], $this->getHeaders(false)) ); $this->assertEquals(401, $delete['headers']['status-code']); @@ -205,7 +222,7 @@ trait PresenceBase \array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], - ], $this->getHeaders()) + ], $this->getHeaders(false)) ); $this->assertEquals(204, $delete['headers']['status-code']); @@ -220,7 +237,7 @@ trait PresenceBase \array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], - ], $this->getHeaders()), + ], $this->getHeaders(false)), [ 'status' => 'ghost', ] @@ -244,7 +261,7 @@ trait PresenceBase \array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], - ], $this->getHeaders()), + ], $this->getHeaders(false)), $payload ); @@ -264,7 +281,7 @@ trait PresenceBase \array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], - ], $this->getHeaders()), + ], $this->getHeaders(false)), [ 'userId' => ID::unique(), 'status' => 'online', @@ -287,7 +304,7 @@ trait PresenceBase \array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], - ], $this->getHeaders()), + ], $this->getHeaders(false)), [ 'status' => 'online', ] diff --git a/tests/e2e/Services/Presence/PresenceExpiryTest.php b/tests/e2e/Services/Presence/PresenceExpiryTest.php index 818d391325..ab6da1711a 100644 --- a/tests/e2e/Services/Presence/PresenceExpiryTest.php +++ b/tests/e2e/Services/Presence/PresenceExpiryTest.php @@ -14,6 +14,24 @@ class PresenceExpiryTest extends Scope use ProjectCustom; use SideServer; + private static array $presenceApiKeyCache = []; + + private function getPresenceApiKey(): string + { + $projectId = $this->getProject()['$id']; + + if (!empty(self::$presenceApiKeyCache[$projectId])) { + return self::$presenceApiKeyCache[$projectId]; + } + + self::$presenceApiKeyCache[$projectId] = $this->getNewKey([ + 'users.read', + 'users.write', + ]); + + return self::$presenceApiKeyCache[$projectId]; + } + public function testExpiredPresenceDeletedByMaintenance(): void { $projectId = $this->getProject()['$id']; @@ -26,7 +44,7 @@ class PresenceExpiryTest extends Scope [ 'content-type' => 'application/json', 'x-appwrite-project' => $projectId, - 'x-appwrite-key' => $this->getProject()['apiKey'], + 'x-appwrite-key' => $this->getPresenceApiKey(), ], [ 'userId' => $userId, @@ -44,7 +62,7 @@ class PresenceExpiryTest extends Scope [ 'content-type' => 'application/json', 'x-appwrite-project' => $projectId, - 'x-appwrite-key' => $this->getProject()['apiKey'], + 'x-appwrite-key' => $this->getPresenceApiKey(), ], [ 'userId' => $userId, @@ -53,7 +71,10 @@ class PresenceExpiryTest extends Scope ); $this->assertEquals(200, $expireServer['headers']['status-code']); - $this->assertEquals($expiredAt, $expireServer['body']['expiry']); + $this->assertEquals( + (new \DateTime($expiredAt))->getTimestamp(), + (new \DateTime($expireServer['body']['expiry']))->getTimestamp() + ); $createClient = $this->client->call( Client::METHOD_PUT, @@ -88,7 +109,10 @@ class PresenceExpiryTest extends Scope ); $this->assertEquals(200, $expireClient['headers']['status-code']); - $this->assertEquals($expiredAt, $expireClient['body']['expiry']); + $this->assertEquals( + (new \DateTime($expiredAt))->getTimestamp(), + (new \DateTime($expireClient['body']['expiry']))->getTimestamp() + ); $stdout = ''; $stderr = ''; @@ -102,7 +126,7 @@ class PresenceExpiryTest extends Scope [ 'content-type' => 'application/json', 'x-appwrite-project' => $projectId, - 'x-appwrite-key' => $this->getProject()['apiKey'], + 'x-appwrite-key' => $this->getPresenceApiKey(), ] ); @@ -112,7 +136,7 @@ class PresenceExpiryTest extends Scope [ 'content-type' => 'application/json', 'x-appwrite-project' => $projectId, - 'x-appwrite-key' => $this->getProject()['apiKey'], + 'x-appwrite-key' => $this->getPresenceApiKey(), ] );