From f9d0cb7daffaa5044c1b131341811eb31def6fcd Mon Sep 17 00:00:00 2001 From: ArnabChatterjee20k Date: Mon, 13 Apr 2026 21:08:23 +0530 Subject: [PATCH] Refactor presence tests to handle unauthorized access for client side operations and simplify presence setup logic. --- tests/e2e/Services/Presence/PresenceBase.php | 106 ++++++++++++++++--- 1 file changed, 90 insertions(+), 16 deletions(-) diff --git a/tests/e2e/Services/Presence/PresenceBase.php b/tests/e2e/Services/Presence/PresenceBase.php index 692b4a21f5..dabd45a7ac 100644 --- a/tests/e2e/Services/Presence/PresenceBase.php +++ b/tests/e2e/Services/Presence/PresenceBase.php @@ -1,4 +1,5 @@ getProject()['$id']; - $cacheKey = $projectId . ':' . $this->getSide(); + $cacheKey = $projectId; if (empty($overrides) && !empty(self::$presenceCache[$cacheKey])) { return self::$presenceCache[$cacheKey]; } $payload = \array_merge([ + 'userId' => $this->getUser()['$id'], 'status' => 'online', 'metadata' => [ 'device' => 'web', - 'side' => $this->getSide(), + 'setup' => true, ], ], $overrides); - if ($this->getSide() === 'server' && !isset($payload['userId'])) { - $payload['userId'] = $this->getUser()['$id']; - } - $response = $this->client->call( Client::METHOD_PUT, '/presences/' . ID::unique(), - \array_merge([ + [ 'content-type' => 'application/json', 'x-appwrite-project' => $projectId, - ], $this->getHeaders()), + 'x-appwrite-key' => $this->getProject()['apiKey'], + ], $payload ); - $this->assertEquals(200, $response['headers']['status-code']); $this->assertNotEmpty($response['body']['$id']); $this->assertArrayHasKey('userId', $response['body']); $this->assertArrayHasKey('status', $response['body']); $this->assertArrayHasKey('metadata', $response['body']); - if ($this->getSide() === 'client') { - $this->assertEquals($this->getUser()['$id'], $response['body']['userId']); - } else { - $this->assertEquals($payload['userId'], $response['body']['userId']); - } + $this->assertEquals($payload['userId'], $response['body']['userId']); if (empty($overrides)) { self::$presenceCache[$cacheKey] = $response['body']; @@ -61,6 +55,24 @@ trait PresenceBase public function testUpsertAndGetPresence(): void { + if ($this->getSide() === 'client') { + $upsert = $this->client->call( + Client::METHOD_PUT, + '/presences/' . ID::unique(), + \array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), + [ + 'status' => 'online', + 'metadata' => ['device' => 'web'], + ] + ); + + $this->assertEquals(401, $upsert['headers']['status-code']); + return; + } + $presence = $this->setupPresence(); $get = $this->client->call( @@ -80,6 +92,20 @@ trait PresenceBase public function testListPresences(): void { + if ($this->getSide() === 'client') { + $list = $this->client->call( + Client::METHOD_GET, + '/presences', + \array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()) + ); + + $this->assertEquals(401, $list['headers']['status-code']); + return; + } + $presence = $this->setupPresence(); $list = $this->client->call( @@ -105,6 +131,24 @@ trait PresenceBase public function testUpdatePresenceSparseFields(): void { + if ($this->getSide() === 'client') { + $update = $this->client->call( + Client::METHOD_PATCH, + '/presences/' . ID::unique(), + \array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), + [ + 'status' => 'busy', + 'metadata' => ['source' => 'update'], + ] + ); + + $this->assertEquals(401, $update['headers']['status-code']); + return; + } + $presence = $this->setupPresence([ 'status' => 'away', 'metadata' => ['source' => 'setup'], @@ -136,6 +180,20 @@ trait PresenceBase public function testDeletePresence(): void { + if ($this->getSide() === 'client') { + $delete = $this->client->call( + Client::METHOD_DELETE, + '/presences/' . ID::unique(), + \array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()) + ); + + $this->assertEquals(401, $delete['headers']['status-code']); + return; + } + $presence = $this->setupPresence([ 'status' => 'temp-delete', 'metadata' => ['cleanup' => true], @@ -155,6 +213,23 @@ trait PresenceBase public function testUpdateNotFound(): void { + if ($this->getSide() === 'client') { + $response = $this->client->call( + Client::METHOD_PATCH, + '/presences/' . ID::unique(), + \array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), + [ + 'status' => 'ghost', + ] + ); + + $this->assertEquals(401, $response['headers']['status-code']); + return; + } + $payload = [ 'status' => 'ghost', ]; @@ -197,7 +272,6 @@ trait PresenceBase ); $this->assertEquals(401, $response['headers']['status-code']); - $this->assertEquals('general_unauthorized_scope', $response['body']['type']); } public function testServerRequiresUserId(): void @@ -222,4 +296,4 @@ trait PresenceBase $this->assertEquals(400, $response['headers']['status-code']); $this->assertEquals('general_argument_invalid', $response['body']['type']); } -} \ No newline at end of file +}