From e944da2f3e08e2f6b864a0538a9cf2a980d8c02b Mon Sep 17 00:00:00 2001 From: Torsten Dittmann Date: Mon, 6 Dec 2021 13:03:12 +0100 Subject: [PATCH 1/3] feat(realtime): add console channel --- app/controllers/shared/api.php | 8 +- app/init.php | 40 - app/workers/database.php | 72 +- public/dist/scripts/app-all.js | 3 +- public/dist/scripts/app.js | 3 +- public/scripts/init.js | 23 +- src/Appwrite/Messaging/Adapter/Realtime.php | 18 +- tests/e2e/Services/Realtime/RealtimeBase.php | 1041 +---------------- .../Realtime/RealtimeConsoleClientTest.php | 276 +++++ .../Realtime/RealtimeCustomClientTest.php | 1037 ++++++++++++++++ 10 files changed, 1434 insertions(+), 1087 deletions(-) create mode 100644 tests/e2e/Services/Realtime/RealtimeConsoleClientTest.php diff --git a/app/controllers/shared/api.php b/app/controllers/shared/api.php index 210cb46912..3bd1aba579 100644 --- a/app/controllers/shared/api.php +++ b/app/controllers/shared/api.php @@ -166,7 +166,7 @@ App::init(function ($utopia, $request, $project) { throw new Exception('JWT authentication is disabled for this project', 501); } break; - + default: throw new Exception('Unsupported authentication route'); break; @@ -187,7 +187,7 @@ App::shutdown(function ($utopia, $request, $response, $project, $events, $audits /** @var bool $mode */ if (!empty($events->getParam('event'))) { - if(empty($events->getParam('eventData'))) { + if (empty($events->getParam('eventData'))) { $events->setParam('eventData', $response->getPayload()); } @@ -206,10 +206,10 @@ App::shutdown(function ($utopia, $request, $response, $project, $events, $audits if ($project->getId() !== 'console') { $payload = new Document($response->getPayload()); - $target = Realtime::fromPayload($events->getParam('event'), $payload); + $target = Realtime::fromPayload($events->getParam('event'), $payload, $project); Realtime::send( - $project->getId(), + $target['projectId'] ?? $project->getId(), $response->getPayload(), $events->getParam('event'), $target['channels'], diff --git a/app/init.php b/app/init.php index 6992728987..0c7c9e55fc 100644 --- a/app/init.php +++ b/app/init.php @@ -21,9 +21,6 @@ use Appwrite\Extend\PDO; use Ahc\Jwt\JWT; use Ahc\Jwt\JWTException; use Appwrite\Auth\Auth; -use Appwrite\Database\Database as DatabaseOld; -use Appwrite\Database\Adapter\MySQL as MySQLAdapter; -use Appwrite\Database\Adapter\Redis as RedisAdapter; use Appwrite\Event\Event; use Appwrite\Network\Validator\Email; use Appwrite\Network\Validator\IP; @@ -156,43 +153,6 @@ if(!empty($user) || !empty($pass)) { Resque::setBackend(App::getEnv('_APP_REDIS_HOST', '').':'.App::getEnv('_APP_REDIS_PORT', '')); } -/** - * Old DB Filters - */ -DatabaseOld::addFilter('json', - function($value) { - if(!is_array($value)) { - return $value; - } - return json_encode($value); - }, - function($value) { - return json_decode($value, true); - } -); - -DatabaseOld::addFilter('encrypt', - function($value) { - $key = App::getEnv('_APP_OPENSSL_KEY_V1'); - $iv = OpenSSL::randomPseudoBytes(OpenSSL::cipherIVLength(OpenSSL::CIPHER_AES_128_GCM)); - $tag = null; - - return json_encode([ - 'data' => OpenSSL::encrypt($value, OpenSSL::CIPHER_AES_128_GCM, $key, 0, $iv, $tag), - 'method' => OpenSSL::CIPHER_AES_128_GCM, - 'iv' => bin2hex($iv), - 'tag' => bin2hex($tag), - 'version' => '1', - ]); - }, - function($value) { - $value = json_decode($value, true); - $key = App::getEnv('_APP_OPENSSL_KEY_V'.$value['version']); - - return OpenSSL::decrypt($value['data'], $value['method'], $key, 0, hex2bin($value['iv']), hex2bin($value['tag'])); - } -); - /** * New DB Filters */ diff --git a/app/workers/database.php b/app/workers/database.php index 3b9f36afbd..f58502704a 100644 --- a/app/workers/database.php +++ b/app/workers/database.php @@ -1,5 +1,6 @@ isEmpty()) { throw new Exception('Missing document'); } - + switch (strval($type)) { case DATABASE_TYPE_CREATE_ATTRIBUTE: $this->createAttribute($collection, $document, $projectId); @@ -67,9 +68,11 @@ class DatabaseV1 extends Worker */ protected function createAttribute(Document $collection, Document $attribute, string $projectId): void { + $dbForConsole = $this->getConsoleDB(); $dbForInternal = $this->getInternalDB($projectId); $dbForExternal = $this->getExternalDB($projectId); + $event = 'database.attributes.update'; $collectionId = $collection->getId(); $key = $attribute->getAttribute('key', ''); $type = $attribute->getAttribute('type', ''); @@ -81,6 +84,7 @@ class DatabaseV1 extends Worker $format = $attribute->getAttribute('format', ''); $formatOptions = $attribute->getAttribute('formatOptions', []); $filters = $attribute->getAttribute('filters', []); + $project = $dbForConsole->getDocument('projects', $projectId); try { if(!$dbForExternal->createAttribute($collectionId, $key, $type, $size, $required, $default, $signed, $array, $format, $formatOptions, $filters)) { @@ -90,6 +94,20 @@ class DatabaseV1 extends Worker } catch (\Throwable $th) { Console::error($th->getMessage()); $dbForInternal->updateDocument('attributes', $attribute->getId(), $attribute->setAttribute('status', 'failed')); + } finally { + $target = Realtime::fromPayload($event, $attribute, $project); + + Realtime::send( + projectId: 'console', + payload: $attribute->getArrayCopy(), + event: $event, + channels: $target['channels'], + roles: $target['roles'], + options: [ + 'projectId' => $projectId, + 'collectionId' => $collection->getId() + ] + ); } $dbForInternal->deleteCachedDocument('collections', $collectionId); @@ -102,11 +120,15 @@ class DatabaseV1 extends Worker */ protected function deleteAttribute(Document $collection, Document $attribute, string $projectId): void { + $dbForConsole = $this->getConsoleDB(); $dbForInternal = $this->getInternalDB($projectId); $dbForExternal = $this->getExternalDB($projectId); + + $event = 'database.attributes.delete'; $collectionId = $collection->getId(); $key = $attribute->getAttribute('key', ''); $status = $attribute->getAttribute('status', ''); + $project = $dbForConsole->getDocument('projects', $projectId); // possible states at this point: // - available: should not land in queue; controller flips these to 'deleting' @@ -122,6 +144,20 @@ class DatabaseV1 extends Worker } catch (\Throwable $th) { Console::error($th->getMessage()); $dbForInternal->updateDocument('attributes', $attribute->getId(), $attribute->setAttribute('status', 'stuck')); + } finally { + $target = Realtime::fromPayload($event, $attribute, $project); + + Realtime::send( + projectId: 'console', + payload: $attribute->getArrayCopy(), + event: $event, + channels: $target['channels'], + roles: $target['roles'], + options: [ + 'projectId' => $projectId, + 'collectionId' => $collection->getId() + ] + ); } // The underlying database removes/rebuilds indexes when attribute is removed @@ -185,15 +221,18 @@ class DatabaseV1 extends Worker */ protected function createIndex(Document $collection, Document $index, string $projectId): void { + $dbForConsole = $this->getConsoleDB(); $dbForInternal = $this->getInternalDB($projectId); $dbForExternal = $this->getExternalDB($projectId); + $event = 'database.indexes.update'; $collectionId = $collection->getId(); $key = $index->getAttribute('key', ''); $type = $index->getAttribute('type', ''); $attributes = $index->getAttribute('attributes', []); $lengths = $index->getAttribute('lengths', []); $orders = $index->getAttribute('orders', []); + $project = $dbForConsole->getDocument('projects', $projectId); try { if(!$dbForExternal->createIndex($collectionId, $key, $type, $attributes, $lengths, $orders)) { @@ -203,6 +242,20 @@ class DatabaseV1 extends Worker } catch (\Throwable $th) { Console::error($th->getMessage()); $dbForInternal->updateDocument('indexes', $index->getId(), $index->setAttribute('status', 'failed')); + } finally { + $target = Realtime::fromPayload($event, $index, $project); + + Realtime::send( + projectId: 'console', + payload: $index->getArrayCopy(), + event: $event, + channels: $target['channels'], + roles: $target['roles'], + options: [ + 'projectId' => $projectId, + 'collectionId' => $collection->getId() + ] + ); } $dbForInternal->deleteCachedDocument('collections', $collectionId); @@ -215,12 +268,15 @@ class DatabaseV1 extends Worker */ protected function deleteIndex(Document $collection, Document $index, string $projectId): void { + $dbForConsole = $this->getConsoleDB(); $dbForInternal = $this->getInternalDB($projectId); $dbForExternal = $this->getExternalDB($projectId); $collectionId = $collection->getId(); $key = $index->getAttribute('key'); $status = $index->getAttribute('status', ''); + $event = 'database.indexes.delete'; + $project = $dbForConsole->getDocument('projects', $projectId); try { if($status !== 'failed' && !$dbForExternal->deleteIndex($collectionId, $key)) { @@ -230,6 +286,20 @@ class DatabaseV1 extends Worker } catch (\Throwable $th) { Console::error($th->getMessage()); $dbForInternal->updateDocument('indexes', $index->getId(), $index->setAttribute('status', 'stuck')); + } finally { + $target = Realtime::fromPayload($event, $index, $project); + + Realtime::send( + projectId: 'console', + payload: $index->getArrayCopy(), + event: $event, + channels: $target['channels'], + roles: $target['roles'], + options: [ + 'projectId' => $projectId, + 'collectionId' => $collection->getId() + ] + ); } $dbForInternal->deleteCachedDocument('collections', $collectionId); diff --git a/public/dist/scripts/app-all.js b/public/dist/scripts/app-all.js index cbddd2d614..b9632bf90b 100644 --- a/public/dist/scripts/app-all.js +++ b/public/dist/scripts/app-all.js @@ -3427,7 +3427,8 @@ function handler2(){} handler2.inline=(el,{expression},{cleanup:cleanup2})=>{let root=closestRoot(el);if(!root._x_refs) root._x_refs={};root._x_refs[expression]=el;cleanup2(()=>delete root._x_refs[expression]);};directive("ref",handler2);directive("if",(el,{expression},{effect:effect3,cleanup:cleanup2})=>{let evaluate2=evaluateLater(el,expression);let show=()=>{if(el._x_currentIfEl) return el._x_currentIfEl;let clone2=el.content.cloneNode(true).firstElementChild;addScopeToNode(clone2,{},el);mutateDom(()=>{el.after(clone2);initTree(clone2);});el._x_currentIfEl=clone2;el._x_undoIf=()=>{clone2.remove();delete el._x_currentIfEl;};return clone2;};let hide=()=>{if(!el._x_undoIf) -return;el._x_undoIf();delete el._x_undoIf;};effect3(()=>evaluate2((value)=>{value?show():hide();}));cleanup2(()=>el._x_undoIf&&el._x_undoIf());});mapAttributes(startingWith("@",into(prefix("on:"))));directive("on",skipDuringClone((el,{value,modifiers,expression},{cleanup:cleanup2})=>{let evaluate2=expression?evaluateLater(el,expression):()=>{};let removeListener=on(el,value,modifiers,(e)=>{evaluate2(()=>{},{scope:{$event:e},params:[e]});});cleanup2(()=>removeListener());}));alpine_default.setEvaluator(normalEvaluator);alpine_default.setReactivityEngine({reactive:reactive2,effect:effect2,release:stop,raw:toRaw});var src_default=alpine_default;window.Alpine=src_default;queueMicrotask(()=>{src_default.start();});})();window.ls.error=function(){return function(error){window.console.error(error);if(window.location.pathname!=='/console'){window.location='/console';}};};window.addEventListener("error",function(event){console.error("ERROR-EVENT:",event.error.message,event.error.stack);});document.addEventListener("account.deleteSession",function(){window.location="/auth/signin";});document.addEventListener("account.create",function(){let container=window.ls.container;let form=container.get('serviceForm');let sdk=container.get('console');let promise=sdk.account.createSession(form.email,form.password);container.set("serviceForm",{},true,true);promise.then(function(){var subscribe=document.getElementById('newsletter').checked;if(subscribe){let alerts=container.get('alerts');let loaderId=alerts.add({text:'Loading...',class:""},0);fetch('https://appwrite.io/v1/newsletter/subscribe',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({name:form.name,email:form.email,}),}).finally(function(){alerts.remove(loaderId);window.location='/console';});}else{window.location='/console';}},function(error){window.location='/auth/signup?failure=1';});});window.addEventListener("load",async()=>{const bars=12;const realtime=window.ls.container.get('realtime');const sleep=ms=>new Promise(resolve=>setTimeout(resolve,ms));let current={};window.ls.container.get('console').subscribe('project',event=>{for(let project in event.payload){current[project]=event.payload[project]??0;}});while(true){let newHistory={};let createdHistory=false;for(const project in current){let history=realtime?.history??{};if(!(project in history)){history[project]=new Array(bars).fill({percentage:0,value:0});} +return;el._x_undoIf();delete el._x_undoIf;};effect3(()=>evaluate2((value)=>{value?show():hide();}));cleanup2(()=>el._x_undoIf&&el._x_undoIf());});mapAttributes(startingWith("@",into(prefix("on:"))));directive("on",skipDuringClone((el,{value,modifiers,expression},{cleanup:cleanup2})=>{let evaluate2=expression?evaluateLater(el,expression):()=>{};let removeListener=on(el,value,modifiers,(e)=>{evaluate2(()=>{},{scope:{$event:e},params:[e]});});cleanup2(()=>removeListener());}));alpine_default.setEvaluator(normalEvaluator);alpine_default.setReactivityEngine({reactive:reactive2,effect:effect2,release:stop,raw:toRaw});var src_default=alpine_default;window.Alpine=src_default;queueMicrotask(()=>{src_default.start();});})();window.ls.error=function(){return function(error){window.console.error(error);if(window.location.pathname!=='/console'){window.location='/console';}};};window.addEventListener("error",function(event){console.error("ERROR-EVENT:",event.error.message,event.error.stack);});document.addEventListener("account.deleteSession",function(){window.location="/auth/signin";});document.addEventListener("account.create",function(){let container=window.ls.container;let form=container.get('serviceForm');let sdk=container.get('console');let promise=sdk.account.createSession(form.email,form.password);container.set("serviceForm",{},true,true);promise.then(function(){var subscribe=document.getElementById('newsletter').checked;if(subscribe){let alerts=container.get('alerts');let loaderId=alerts.add({text:'Loading...',class:""},0);fetch('https://appwrite.io/v1/newsletter/subscribe',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({name:form.name,email:form.email,}),}).finally(function(){alerts.remove(loaderId);window.location='/console';});}else{window.location='/console';}},function(error){window.location='/auth/signup?failure=1';});});window.addEventListener("load",async()=>{const bars=12;const realtime=window.ls.container.get('realtime');const sleep=ms=>new Promise(resolve=>setTimeout(resolve,ms));let current={};window.ls.container.get('console').subscribe(['project','console'],response=>{switch(response.event){case'stats.connections':for(let project in response.payload){current[project]=response.payload[project]??0;} +break;case'database.attributes.create':case'database.attributes.update':case'database.attributes.delete':document.dispatchEvent(new CustomEvent('database.createAttribute'));break;case'database.indexes.create':case'database.indexes.update':case'database.indexes.delete':document.dispatchEvent(new CustomEvent('database.createIndex'));break;}});while(true){let newHistory={};let createdHistory=false;for(const project in current){let history=realtime?.history??{};if(!(project in history)){history[project]=new Array(bars).fill({percentage:0,value:0});} history=history[project];history.push({percentage:0,value:current[project]});if(history.length>=bars){history.shift();} const highest=history.reduce((prev,curr)=>{return(curr.value>prev)?curr.value:prev;},0);history=history.map(({percentage,value})=>{createdHistory=true;percentage=value===0?0:((Math.round((value/highest)*10)/10)*100);if(percentage>100)percentage=100;else if(percentage==0&&value!=0)percentage=5;return{percentage:percentage,value:value};}) newHistory[project]=history;} diff --git a/public/dist/scripts/app.js b/public/dist/scripts/app.js index c7eb1c9eee..f2f5a08f79 100644 --- a/public/dist/scripts/app.js +++ b/public/dist/scripts/app.js @@ -493,7 +493,8 @@ function handler2(){} handler2.inline=(el,{expression},{cleanup:cleanup2})=>{let root=closestRoot(el);if(!root._x_refs) root._x_refs={};root._x_refs[expression]=el;cleanup2(()=>delete root._x_refs[expression]);};directive("ref",handler2);directive("if",(el,{expression},{effect:effect3,cleanup:cleanup2})=>{let evaluate2=evaluateLater(el,expression);let show=()=>{if(el._x_currentIfEl) return el._x_currentIfEl;let clone2=el.content.cloneNode(true).firstElementChild;addScopeToNode(clone2,{},el);mutateDom(()=>{el.after(clone2);initTree(clone2);});el._x_currentIfEl=clone2;el._x_undoIf=()=>{clone2.remove();delete el._x_currentIfEl;};return clone2;};let hide=()=>{if(!el._x_undoIf) -return;el._x_undoIf();delete el._x_undoIf;};effect3(()=>evaluate2((value)=>{value?show():hide();}));cleanup2(()=>el._x_undoIf&&el._x_undoIf());});mapAttributes(startingWith("@",into(prefix("on:"))));directive("on",skipDuringClone((el,{value,modifiers,expression},{cleanup:cleanup2})=>{let evaluate2=expression?evaluateLater(el,expression):()=>{};let removeListener=on(el,value,modifiers,(e)=>{evaluate2(()=>{},{scope:{$event:e},params:[e]});});cleanup2(()=>removeListener());}));alpine_default.setEvaluator(normalEvaluator);alpine_default.setReactivityEngine({reactive:reactive2,effect:effect2,release:stop,raw:toRaw});var src_default=alpine_default;window.Alpine=src_default;queueMicrotask(()=>{src_default.start();});})();window.ls.error=function(){return function(error){window.console.error(error);if(window.location.pathname!=='/console'){window.location='/console';}};};window.addEventListener("error",function(event){console.error("ERROR-EVENT:",event.error.message,event.error.stack);});document.addEventListener("account.deleteSession",function(){window.location="/auth/signin";});document.addEventListener("account.create",function(){let container=window.ls.container;let form=container.get('serviceForm');let sdk=container.get('console');let promise=sdk.account.createSession(form.email,form.password);container.set("serviceForm",{},true,true);promise.then(function(){var subscribe=document.getElementById('newsletter').checked;if(subscribe){let alerts=container.get('alerts');let loaderId=alerts.add({text:'Loading...',class:""},0);fetch('https://appwrite.io/v1/newsletter/subscribe',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({name:form.name,email:form.email,}),}).finally(function(){alerts.remove(loaderId);window.location='/console';});}else{window.location='/console';}},function(error){window.location='/auth/signup?failure=1';});});window.addEventListener("load",async()=>{const bars=12;const realtime=window.ls.container.get('realtime');const sleep=ms=>new Promise(resolve=>setTimeout(resolve,ms));let current={};window.ls.container.get('console').subscribe('project',event=>{for(let project in event.payload){current[project]=event.payload[project]??0;}});while(true){let newHistory={};let createdHistory=false;for(const project in current){let history=realtime?.history??{};if(!(project in history)){history[project]=new Array(bars).fill({percentage:0,value:0});} +return;el._x_undoIf();delete el._x_undoIf;};effect3(()=>evaluate2((value)=>{value?show():hide();}));cleanup2(()=>el._x_undoIf&&el._x_undoIf());});mapAttributes(startingWith("@",into(prefix("on:"))));directive("on",skipDuringClone((el,{value,modifiers,expression},{cleanup:cleanup2})=>{let evaluate2=expression?evaluateLater(el,expression):()=>{};let removeListener=on(el,value,modifiers,(e)=>{evaluate2(()=>{},{scope:{$event:e},params:[e]});});cleanup2(()=>removeListener());}));alpine_default.setEvaluator(normalEvaluator);alpine_default.setReactivityEngine({reactive:reactive2,effect:effect2,release:stop,raw:toRaw});var src_default=alpine_default;window.Alpine=src_default;queueMicrotask(()=>{src_default.start();});})();window.ls.error=function(){return function(error){window.console.error(error);if(window.location.pathname!=='/console'){window.location='/console';}};};window.addEventListener("error",function(event){console.error("ERROR-EVENT:",event.error.message,event.error.stack);});document.addEventListener("account.deleteSession",function(){window.location="/auth/signin";});document.addEventListener("account.create",function(){let container=window.ls.container;let form=container.get('serviceForm');let sdk=container.get('console');let promise=sdk.account.createSession(form.email,form.password);container.set("serviceForm",{},true,true);promise.then(function(){var subscribe=document.getElementById('newsletter').checked;if(subscribe){let alerts=container.get('alerts');let loaderId=alerts.add({text:'Loading...',class:""},0);fetch('https://appwrite.io/v1/newsletter/subscribe',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({name:form.name,email:form.email,}),}).finally(function(){alerts.remove(loaderId);window.location='/console';});}else{window.location='/console';}},function(error){window.location='/auth/signup?failure=1';});});window.addEventListener("load",async()=>{const bars=12;const realtime=window.ls.container.get('realtime');const sleep=ms=>new Promise(resolve=>setTimeout(resolve,ms));let current={};window.ls.container.get('console').subscribe(['project','console'],response=>{switch(response.event){case'stats.connections':for(let project in response.payload){current[project]=response.payload[project]??0;} +break;case'database.attributes.create':case'database.attributes.update':case'database.attributes.delete':document.dispatchEvent(new CustomEvent('database.createAttribute'));break;case'database.indexes.create':case'database.indexes.update':case'database.indexes.delete':document.dispatchEvent(new CustomEvent('database.createIndex'));break;}});while(true){let newHistory={};let createdHistory=false;for(const project in current){let history=realtime?.history??{};if(!(project in history)){history[project]=new Array(bars).fill({percentage:0,value:0});} history=history[project];history.push({percentage:0,value:current[project]});if(history.length>=bars){history.shift();} const highest=history.reduce((prev,curr)=>{return(curr.value>prev)?curr.value:prev;},0);history=history.map(({percentage,value})=>{createdHistory=true;percentage=value===0?0:((Math.round((value/highest)*10)/10)*100);if(percentage>100)percentage=100;else if(percentage==0&&value!=0)percentage=5;return{percentage:percentage,value:value};}) newHistory[project]=history;} diff --git a/public/scripts/init.js b/public/scripts/init.js index 711c678ac5..fb0e4b5383 100644 --- a/public/scripts/init.js +++ b/public/scripts/init.js @@ -57,10 +57,27 @@ window.addEventListener("load", async () => { const realtime = window.ls.container.get('realtime'); const sleep = ms => new Promise(resolve => setTimeout(resolve, ms)); let current = {}; - window.ls.container.get('console').subscribe('project', event => { - for (let project in event.payload) { - current[project] = event.payload[project] ?? 0; + window.ls.container.get('console').subscribe(['project', 'console'], response => { + switch (response.event) { + case 'stats.connections': + for (let project in response.payload) { + current[project] = response.payload[project] ?? 0; + } + break; + case 'database.attributes.create': + case 'database.attributes.update': + case 'database.attributes.delete': + document.dispatchEvent(new CustomEvent('database.createAttribute')); + + break; + case 'database.indexes.create': + case 'database.indexes.update': + case 'database.indexes.delete': + document.dispatchEvent(new CustomEvent('database.createIndex')); + + break; } + }); while (true) { diff --git a/src/Appwrite/Messaging/Adapter/Realtime.php b/src/Appwrite/Messaging/Adapter/Realtime.php index 4269fa04dc..71738e42ba 100644 --- a/src/Appwrite/Messaging/Adapter/Realtime.php +++ b/src/Appwrite/Messaging/Adapter/Realtime.php @@ -233,17 +233,19 @@ class Realtime extends Adapter } /** - * Create channels array based on the event name and payload. - * + * Creae channels array based on the event name and payload. + * * @param string $event * @param Document $payload + * @param Document|null $project * @return array */ - public static function fromPayload(string $event, Document $payload): array + public static function fromPayload(string $event, Document $payload, Document $project = null): array { $channels = []; $roles = []; $permissionsChanged = false; + $projectId = null; switch (true) { case strpos($event, 'account.recovery.') === 0: @@ -279,6 +281,13 @@ class Realtime extends Adapter $channels[] = 'collections.' . $payload->getId(); $roles = $payload->getRead(); + break; + case strpos($event, 'database.attributes.') === 0: + case strpos($event, 'database.indexes.') === 0: + $channels[] = 'console'; + $projectId = 'console'; + $roles = ['team:' . $project->getAttribute('teamId')]; + break; case strpos($event, 'database.documents.') === 0: $channels[] = 'documents'; @@ -306,7 +315,8 @@ class Realtime extends Adapter return [ 'channels' => $channels, 'roles' => $roles, - 'permissionsChanged' => $permissionsChanged + 'permissionsChanged' => $permissionsChanged, + 'projectId' => $projectId ]; } } diff --git a/tests/e2e/Services/Realtime/RealtimeBase.php b/tests/e2e/Services/Realtime/RealtimeBase.php index 68a121df93..1f734333d8 100644 --- a/tests/e2e/Services/Realtime/RealtimeBase.php +++ b/tests/e2e/Services/Realtime/RealtimeBase.php @@ -2,24 +2,26 @@ namespace Tests\E2E\Services\Realtime; -use CURLFile; -use Tests\E2E\Client; -use WebSocket\Client as WebSocketClient; use WebSocket\ConnectionException; +use WebSocket\Client as WebSocketClient; trait RealtimeBase { - - private function getWebsocket($channels = [], $headers = []) + private function getWebsocket($channels = [], $headers = [], $projectId = null) { + if (is_null($projectId)) { + $projectId = $this->getProject()['$id']; + } + $headers = array_merge([ 'Origin' => 'appwrite.test' ], $headers); $query = [ - 'project' => $this->getProject()['$id'], + 'project' => $projectId, 'channels' => $channels ]; + return new WebSocketClient('ws://appwrite-traefik/v1/realtime?' . http_build_query($query), [ 'headers' => $headers, 'timeout' => 30, @@ -38,17 +40,6 @@ trait RealtimeBase /** * Test for FAILURE */ - $client = $this->getWebsocket(['documents'], ['origin' => 'http://appwrite.unknown']); - $payload = json_decode($client->receive(), true); - - $this->assertArrayHasKey('type', $payload); - $this->assertArrayHasKey('data', $payload); - $this->assertEquals('error', $payload['type']); - $this->assertEquals(1008, $payload['data']['code']); - $this->assertEquals('Invalid Origin. Register your new client (appwrite.unknown) as a new Web platform on your project console dashboard', $payload['data']['message']); - $this->expectException(ConnectionException::class); // Check if server disconnnected client - $client->close(); - $client = $this->getWebsocket(); $payload = json_decode($client->receive(), true); @@ -90,1020 +81,4 @@ trait RealtimeBase $this->expectException(ConnectionException::class); // Check if server disconnnected client $client->close(); } - - public function testChannelParsing() - { - $user = $this->getUser(); - $userId = $user['$id'] ?? ''; - $session = $user['session'] ?? ''; - $headers = [ - 'origin' => 'http://localhost', - 'cookie' => 'a_session_'.$this->getProject()['$id'].'=' . $session - ]; - - $client = $this->getWebsocket(['documents'], $headers); - $response = json_decode($client->receive(), true); - - $this->assertArrayHasKey('type', $response); - $this->assertArrayHasKey('data', $response); - $this->assertEquals('connected', $response['type']); - $this->assertNotEmpty($response['data']); - $this->assertNotEmpty($response['data']['user']); - $this->assertCount(1, $response['data']['channels']); - $this->assertContains('documents', $response['data']['channels']); - $this->assertEquals($userId, $response['data']['user']['$id']); - - $client->close(); - - $client = $this->getWebsocket(['account'], $headers); - $response = json_decode($client->receive(), true); - - $this->assertArrayHasKey('type', $response); - $this->assertArrayHasKey('data', $response); - $this->assertEquals('connected', $response['type']); - $this->assertNotEmpty($response['data']); - $this->assertNotEmpty($response['data']['user']); - $this->assertCount(2, $response['data']['channels']); - $this->assertContains('account', $response['data']['channels']); - $this->assertContains('account.' . $userId, $response['data']['channels']); - $this->assertEquals($userId, $response['data']['user']['$id']); - - $client->close(); - - $client = $this->getWebsocket(['account', 'documents', 'account.123'], $headers); - $response = json_decode($client->receive(), true); - - $this->assertArrayHasKey('type', $response); - $this->assertArrayHasKey('data', $response); - $this->assertEquals('connected', $response['type']); - $this->assertNotEmpty($response['data']); - $this->assertNotEmpty($response['data']['user']); - $this->assertCount(3, $response['data']['channels']); - $this->assertContains('documents', $response['data']['channels']); - $this->assertContains('account', $response['data']['channels']); - $this->assertContains('account.' . $userId, $response['data']['channels']); - $this->assertEquals($userId, $response['data']['user']['$id']); - - $client->close(); - - $client = $this->getWebsocket([ - 'account', - 'files', - 'files.1', - 'collections', - 'collections.1', - 'collections.1.documents', - 'collections.2', - 'collections.2.documents', - 'documents', - 'documents.1', - 'documents.2', - ], $headers); - - $response = json_decode($client->receive(), true); - - $this->assertArrayHasKey('type', $response); - $this->assertArrayHasKey('data', $response); - $this->assertEquals('connected', $response['type']); - $this->assertNotEmpty($response['data']); - $this->assertNotEmpty($response['data']['user']); - $this->assertCount(12, $response['data']['channels']); - $this->assertContains('account', $response['data']['channels']); - $this->assertContains('account.' . $userId, $response['data']['channels']); - $this->assertContains('files', $response['data']['channels']); - $this->assertContains('files.1', $response['data']['channels']); - $this->assertContains('collections', $response['data']['channels']); - $this->assertContains('collections.1', $response['data']['channels']); - $this->assertContains('collections.1.documents', $response['data']['channels']); - $this->assertContains('collections.2', $response['data']['channels']); - $this->assertContains('collections.2.documents', $response['data']['channels']); - $this->assertContains('documents', $response['data']['channels']); - $this->assertContains('documents.1', $response['data']['channels']); - $this->assertContains('documents.2', $response['data']['channels']); - $this->assertEquals($userId, $response['data']['user']['$id']); - - $client->close(); - } - - public function testManualAuthentication() - { - $user = $this->getUser(); - $userId = $user['$id'] ?? ''; - $session = $user['session'] ?? ''; - - /** - * Test for SUCCESS - */ - $client = $this->getWebsocket(['account'], [ - 'origin' => 'http://localhost' - ]); - $response = json_decode($client->receive(), true); - - $this->assertArrayHasKey('type', $response); - $this->assertArrayHasKey('data', $response); - $this->assertEquals('connected', $response['type']); - $this->assertNotEmpty($response['data']); - $this->assertCount(1, $response['data']['channels']); - $this->assertContains('account', $response['data']['channels']); - - $client->send(\json_encode([ - 'type' => 'authentication', - 'data' => [ - 'session' => $session - ] - ])); - - $response = json_decode($client->receive(), true); - - $this->assertArrayHasKey('type', $response); - $this->assertArrayHasKey('data', $response); - $this->assertEquals('response', $response['type']); - $this->assertNotEmpty($response['data']); - $this->assertEquals('authentication', $response['data']['to']); - $this->assertTrue($response['data']['success']); - $this->assertNotEmpty($response['data']['user']); - $this->assertEquals($userId, $response['data']['user']['$id']); - - /** - * Test for FAILURE - */ - $client->send(\json_encode([ - 'type' => 'authentication', - 'data' => [ - 'session' => 'invalid_session' - ] - ])); - - $response = json_decode($client->receive(), true); - - $this->assertArrayHasKey('type', $response); - $this->assertArrayHasKey('data', $response); - $this->assertEquals('error', $response['type']); - $this->assertNotEmpty($response['data']); - $this->assertEquals(1003, $response['data']['code']); - $this->assertEquals('Session is not valid.', $response['data']['message']); - - $client->send(\json_encode([ - 'type' => 'authentication', - 'data' => [] - ])); - - $response = json_decode($client->receive(), true); - - $this->assertArrayHasKey('type', $response); - $this->assertArrayHasKey('data', $response); - $this->assertEquals('error', $response['type']); - $this->assertNotEmpty($response['data']); - $this->assertEquals(1003, $response['data']['code']); - $this->assertEquals('Payload is not valid.', $response['data']['message']); - - $client->send(\json_encode([ - 'type' => 'unknown', - 'data' => [ - 'session' => 'invalid_session' - ] - ])); - - $response = json_decode($client->receive(), true); - - $this->assertArrayHasKey('type', $response); - $this->assertArrayHasKey('data', $response); - $this->assertEquals('error', $response['type']); - $this->assertNotEmpty($response['data']); - $this->assertEquals(1003, $response['data']['code']); - $this->assertEquals('Message type is not valid.', $response['data']['message']); - - $client->send(\json_encode([ - 'test' => '123', - ])); - - $response = json_decode($client->receive(), true); - - $this->assertArrayHasKey('type', $response); - $this->assertArrayHasKey('data', $response); - $this->assertEquals('error', $response['type']); - $this->assertNotEmpty($response['data']); - $this->assertEquals(1003, $response['data']['code']); - $this->assertEquals('Message format is not valid.', $response['data']['message']); - - - $client->close(); - } - - public function testChannelAccount() - { - $user = $this->getUser(); - $userId = $user['$id'] ?? ''; - $session = $user['session'] ?? ''; - $projectId = $this->getProject()['$id']; - - $client = $this->getWebsocket(['account'], [ - 'origin' => 'http://localhost', - 'cookie' => 'a_session_'.$projectId.'=' . $session - ]); - $response = json_decode($client->receive(), true); - - $this->assertArrayHasKey('type', $response); - $this->assertArrayHasKey('data', $response); - $this->assertEquals('connected', $response['type']); - $this->assertNotEmpty($response['data']); - $this->assertCount(2, $response['data']['channels']); - $this->assertContains('account', $response['data']['channels']); - $this->assertContains('account.' . $userId, $response['data']['channels']); - $this->assertNotEmpty($response['data']['user']); - $this->assertEquals($userId, $response['data']['user']['$id']); - - /** - * Test Account Name Event - */ - $name = "Torsten Dittmann"; - - $this->client->call(Client::METHOD_PATCH, '/account/name', array_merge([ - 'origin' => 'http://localhost', - 'content-type' => 'application/json', - 'x-appwrite-project' => $projectId, - 'cookie' => 'a_session_' . $projectId . '=' . $session, - ]), [ - 'name' => $name - ]); - - $response = json_decode($client->receive(), true); - - $this->assertArrayHasKey('type', $response); - $this->assertArrayHasKey('data', $response); - $this->assertEquals('event', $response['type']); - $this->assertNotEmpty($response['data']); - $this->assertCount(2, $response['data']['channels']); - $this->assertArrayHasKey('timestamp', $response['data']); - $this->assertContains('account', $response['data']['channels']); - $this->assertContains('account.' . $userId, $response['data']['channels']); - $this->assertEquals('account.update.name', $response['data']['event']); - $this->assertNotEmpty($response['data']['payload']); - - $this->assertEquals($name, $response['data']['payload']['name']); - - - /** - * Test Account Password Event - */ - $this->client->call(Client::METHOD_PATCH, '/account/password', array_merge([ - 'origin' => 'http://localhost', - 'content-type' => 'application/json', - 'x-appwrite-project' => $projectId, - 'cookie' => 'a_session_'.$projectId.'=' . $session, - ]), [ - 'password' => 'new-password', - 'oldPassword' => 'password', - ]); - - $response = json_decode($client->receive(), true); - - $this->assertArrayHasKey('type', $response); - $this->assertArrayHasKey('data', $response); - $this->assertEquals('event', $response['type']); - $this->assertNotEmpty($response['data']); - $this->assertCount(2, $response['data']['channels']); - $this->assertArrayHasKey('timestamp', $response['data']); - $this->assertContains('account', $response['data']['channels']); - $this->assertContains('account.' . $userId, $response['data']['channels']); - $this->assertEquals('account.update.password', $response['data']['event']); - $this->assertNotEmpty($response['data']['payload']); - - $this->assertEquals($name, $response['data']['payload']['name']); - - /** - * Test Account Email Update - */ - $this->client->call(Client::METHOD_PATCH, '/account/email', array_merge([ - 'origin' => 'http://localhost', - 'content-type' => 'application/json', - 'x-appwrite-project' => $projectId, - 'cookie' => 'a_session_'.$projectId.'=' . $session, - ]), [ - 'email' => 'torsten@appwrite.io', - 'password' => 'new-password', - ]); - - $response = json_decode($client->receive(), true); - - $this->assertArrayHasKey('type', $response); - $this->assertArrayHasKey('data', $response); - $this->assertEquals('event', $response['type']); - $this->assertNotEmpty($response['data']); - $this->assertCount(2, $response['data']['channels']); - $this->assertArrayHasKey('timestamp', $response['data']); - $this->assertContains('account', $response['data']['channels']); - $this->assertContains('account.' . $userId, $response['data']['channels']); - $this->assertEquals('account.update.email', $response['data']['event']); - $this->assertNotEmpty($response['data']['payload']); - - $this->assertEquals('torsten@appwrite.io', $response['data']['payload']['email']); - - /** - * Test Account Verification Create - */ - $this->client->call(Client::METHOD_POST, '/account/verification', array_merge([ - 'origin' => 'http://localhost', - 'content-type' => 'application/json', - 'x-appwrite-project' => $projectId, - 'cookie' => 'a_session_'.$projectId.'=' . $session, - ]), [ - 'url' => 'http://localhost/verification', - ]); - - $response = json_decode($client->receive(), true); - - $this->assertArrayHasKey('type', $response); - $this->assertArrayHasKey('data', $response); - $this->assertEquals('event', $response['type']); - $this->assertNotEmpty($response['data']); - $this->assertCount(2, $response['data']['channels']); - $this->assertArrayHasKey('timestamp', $response['data']); - $this->assertContains('account', $response['data']['channels']); - $this->assertContains('account.' . $userId, $response['data']['channels']); - $this->assertEquals('account.verification.create', $response['data']['event']); - - $lastEmail = $this->getLastEmail(); - $verification = substr($lastEmail['text'], strpos($lastEmail['text'], '&secret=', 0) + 8, 256); - - /** - * Test Account Verification Complete - */ - $response = $this->client->call(Client::METHOD_PUT, '/account/verification', array_merge([ - 'origin' => 'http://localhost', - 'content-type' => 'application/json', - 'x-appwrite-project' => $projectId, - 'cookie' => 'a_session_'.$projectId.'=' . $session, - ]), [ - 'userId' => $userId, - 'secret' => $verification, - ]); - - $response = json_decode($client->receive(), true); - - $this->assertArrayHasKey('type', $response); - $this->assertArrayHasKey('data', $response); - $this->assertEquals('event', $response['type']); - $this->assertNotEmpty($response['data']); - $this->assertCount(2, $response['data']['channels']); - $this->assertArrayHasKey('timestamp', $response['data']); - $this->assertContains('account', $response['data']['channels']); - $this->assertContains('account.' . $userId, $response['data']['channels']); - $this->assertEquals('account.verification.update', $response['data']['event']); - - /** - * Test Acoount Prefs Update - */ - $this->client->call(Client::METHOD_PATCH, '/account/prefs', array_merge([ - 'origin' => 'http://localhost', - 'content-type' => 'application/json', - 'x-appwrite-project' => $projectId, - 'cookie' => 'a_session_'.$projectId.'=' . $session, - ]), [ - 'prefs' => [ - 'prefKey1' => 'prefValue1', - 'prefKey2' => 'prefValue2', - ] - ]); - - $response = json_decode($client->receive(), true); - - $this->assertArrayHasKey('type', $response); - $this->assertArrayHasKey('data', $response); - $this->assertEquals('event', $response['type']); - $this->assertNotEmpty($response['data']); - $this->assertCount(2, $response['data']['channels']); - $this->assertArrayHasKey('timestamp', $response['data']); - $this->assertContains('account', $response['data']['channels']); - $this->assertContains('account.' . $userId, $response['data']['channels']); - $this->assertEquals('account.update.prefs', $response['data']['event']); - $this->assertNotEmpty($response['data']['payload']); - - /** - * Test Account Session Create - */ - $response = $this->client->call(Client::METHOD_POST, '/account/sessions', array_merge([ - 'origin' => 'http://localhost', - 'content-type' => 'application/json', - 'x-appwrite-project' => $projectId, - ]), [ - 'email' => 'torsten@appwrite.io', - 'password' => 'new-password', - ]); - - $sessionNew = $this->client->parseCookie((string)$response['headers']['set-cookie'])['a_session_'.$projectId]; - $sessionNewId = $response['body']['$id']; - - $response = json_decode($client->receive(), true); - - $this->assertArrayHasKey('type', $response); - $this->assertArrayHasKey('data', $response); - $this->assertEquals('event', $response['type']); - $this->assertNotEmpty($response['data']); - $this->assertCount(2, $response['data']['channels']); - $this->assertArrayHasKey('timestamp', $response['data']); - $this->assertContains('account', $response['data']['channels']); - $this->assertContains('account.' . $userId, $response['data']['channels']); - $this->assertEquals('account.sessions.create', $response['data']['event']); - $this->assertNotEmpty($response['data']['payload']); - - /** - * Test Account Session Delete - */ - $this->client->call(Client::METHOD_DELETE, '/account/sessions/'.$sessionNewId, array_merge([ - 'origin' => 'http://localhost', - 'content-type' => 'application/json', - 'x-appwrite-project' => $projectId, - 'cookie' => 'a_session_'.$projectId.'=' . $sessionNew, - ])); - - $response = json_decode($client->receive(), true); - - $this->assertArrayHasKey('type', $response); - $this->assertArrayHasKey('data', $response); - $this->assertEquals('event', $response['type']); - $this->assertNotEmpty($response['data']); - $this->assertCount(2, $response['data']['channels']); - $this->assertArrayHasKey('timestamp', $response['data']); - $this->assertContains('account', $response['data']['channels']); - $this->assertContains('account.' . $userId, $response['data']['channels']); - $this->assertEquals('account.sessions.delete', $response['data']['event']); - $this->assertNotEmpty($response['data']['payload']); - - /** - * Test Account Create Recovery - */ - $this->client->call(Client::METHOD_POST, '/account/recovery', array_merge([ - 'origin' => 'http://localhost', - 'content-type' => 'application/json', - 'x-appwrite-project' => $projectId, - ]), [ - 'email' => 'torsten@appwrite.io', - 'url' => 'http://localhost/recovery', - ]); - - $response = json_decode($client->receive(), true); - - $lastEmail = $this->getLastEmail(); - $recovery = substr($lastEmail['text'], strpos($lastEmail['text'], '&secret=', 0) + 8, 256); - - $this->assertArrayHasKey('type', $response); - $this->assertArrayHasKey('data', $response); - $this->assertEquals('event', $response['type']); - $this->assertNotEmpty($response['data']); - $this->assertCount(2, $response['data']['channels']); - $this->assertArrayHasKey('timestamp', $response['data']); - $this->assertContains('account', $response['data']['channels']); - $this->assertContains('account.' . $userId, $response['data']['channels']); - $this->assertEquals('account.recovery.create', $response['data']['event']); - $this->assertNotEmpty($response['data']['payload']); - - $response = $this->client->call(Client::METHOD_PUT, '/account/recovery', array_merge([ - 'origin' => 'http://localhost', - 'content-type' => 'application/json', - 'x-appwrite-project' => $projectId, - ]), [ - 'userId' => $userId, - 'secret' => $recovery, - 'password' => 'test-recovery', - 'passwordAgain' => 'test-recovery', - ]); - - $response = json_decode($client->receive(), true); - - $this->assertArrayHasKey('type', $response); - $this->assertArrayHasKey('data', $response); - $this->assertEquals('event', $response['type']); - $this->assertNotEmpty($response['data']); - $this->assertCount(2, $response['data']['channels']); - $this->assertArrayHasKey('timestamp', $response['data']); - $this->assertContains('account', $response['data']['channels']); - $this->assertContains('account.' . $userId, $response['data']['channels']); - $this->assertEquals('account.recovery.update', $response['data']['event']); - $this->assertNotEmpty($response['data']['payload']); - - $client->close(); - } - - public function testChannelDatabase() - { - $user = $this->getUser(); - $session = $user['session'] ?? ''; - $projectId = $this->getProject()['$id']; - - $client = $this->getWebsocket(['documents', 'collections'], [ - 'origin' => 'http://localhost', - 'cookie' => 'a_session_'.$projectId.'=' . $session - ]); - - $response = json_decode($client->receive(), true); - - $this->assertArrayHasKey('type', $response); - $this->assertArrayHasKey('data', $response); - $this->assertEquals('connected', $response['type']); - $this->assertNotEmpty($response['data']); - $this->assertCount(2, $response['data']['channels']); - $this->assertContains('documents', $response['data']['channels']); - $this->assertContains('collections', $response['data']['channels']); - $this->assertNotEmpty($response['data']['user']); - $this->assertEquals($user['$id'], $response['data']['user']['$id']); - - /** - * Test Collection Create - */ - $actors = $this->client->call(Client::METHOD_POST, '/database/collections', array_merge([ - 'content-type' => 'application/json', - 'x-appwrite-project' => $this->getProject()['$id'], - 'x-appwrite-key' => $this->getProject()['apiKey'] - ]), [ - 'collectionId' => 'unique()', - 'name' => 'Actors', - 'read' => ['role:all'], - 'write' => ['role:all'], - 'permission' => 'collection' - ]); - - $response = json_decode($client->receive(), true); - - $this->assertArrayHasKey('type', $response); - $this->assertArrayHasKey('data', $response); - $this->assertEquals('event', $response['type']); - $this->assertNotEmpty($response['data']); - $this->assertArrayHasKey('timestamp', $response['data']); - $this->assertCount(2, $response['data']['channels']); - $this->assertContains('collections', $response['data']['channels']); - $this->assertContains('collections.' . $actors['body']['$id'], $response['data']['channels']); - $this->assertEquals('database.collections.create', $response['data']['event']); - $this->assertNotEmpty($response['data']['payload']); - - $data = ['actorsId' => $actors['body']['$id']]; - - $name = $this->client->call(Client::METHOD_POST, '/database/collections/' . $data['actorsId'] . '/attributes/string', array_merge([ - 'content-type' => 'application/json', - 'x-appwrite-project' => $this->getProject()['$id'], - 'x-appwrite-key' => $this->getProject()['apiKey'] - ]), [ - 'attributeId' => 'name', - 'size' => 256, - 'required' => true, - ]); - - $this->assertEquals($name['headers']['status-code'], 201); - $this->assertEquals($name['body']['key'], 'name'); - $this->assertEquals($name['body']['type'], 'string'); - $this->assertEquals($name['body']['size'], 256); - $this->assertEquals($name['body']['required'], true); - - sleep(2); - - /** - * Test Document Create - */ - $document = $this->client->call(Client::METHOD_POST, '/database/collections/' . $data['actorsId'] . '/documents', array_merge([ - 'content-type' => 'application/json', - 'x-appwrite-project' => $this->getProject()['$id'], - ], $this->getHeaders()), [ - 'documentId' => 'unique()', - 'data' => [ - 'name' => 'Chris Evans' - ], - 'read' => ['role:all'], - 'write' => ['role:all'], - ]); - - $response = json_decode($client->receive(), true); - - $this->assertArrayHasKey('type', $response); - $this->assertArrayHasKey('data', $response); - $this->assertEquals('event', $response['type']); - $this->assertNotEmpty($response['data']); - $this->assertArrayHasKey('timestamp', $response['data']); - $this->assertCount(3, $response['data']['channels']); - $this->assertContains('documents', $response['data']['channels']); - $this->assertContains('documents.' . $document['body']['$id'], $response['data']['channels']); - $this->assertContains('collections.' . $actors['body']['$id'] . '.documents', $response['data']['channels']); - $this->assertEquals('database.documents.create', $response['data']['event']); - $this->assertNotEmpty($response['data']['payload']); - $this->assertEquals($response['data']['payload']['name'], 'Chris Evans'); - - $data['documentId'] = $document['body']['$id']; - - /** - * Test Document Update - */ - $document = $this->client->call(Client::METHOD_PATCH, '/database/collections/' . $data['actorsId'] . '/documents/' . $data['documentId'], array_merge([ - 'content-type' => 'application/json', - 'x-appwrite-project' => $this->getProject()['$id'], - ], $this->getHeaders()), [ - 'documentId' => 'unique()', - 'data' => [ - 'name' => 'Chris Evans 2' - ], - 'read' => ['role:all'], - 'write' => ['role:all'], - ]); - - $response = json_decode($client->receive(), true); - - $this->assertArrayHasKey('type', $response); - $this->assertArrayHasKey('data', $response); - $this->assertEquals('event', $response['type']); - $this->assertNotEmpty($response['data']); - $this->assertArrayHasKey('timestamp', $response['data']); - $this->assertCount(3, $response['data']['channels']); - $this->assertContains('documents', $response['data']['channels']); - $this->assertContains('documents.' . $data['documentId'], $response['data']['channels']); - $this->assertContains('collections.' . $data['actorsId'] . '.documents', $response['data']['channels']); - $this->assertEquals('database.documents.update', $response['data']['event']); - $this->assertNotEmpty($response['data']['payload']); - - $this->assertEquals($response['data']['payload']['name'], 'Chris Evans 2'); - - - /** - * Test Document Delete - */ - $document = $this->client->call(Client::METHOD_POST, '/database/collections/' . $data['actorsId'] . '/documents', array_merge([ - 'content-type' => 'application/json', - 'x-appwrite-project' => $this->getProject()['$id'], - ], $this->getHeaders()), [ - 'documentId' => 'unique()', - 'data' => [ - 'name' => 'Bradley Cooper' - ], - 'read' => ['role:all'], - 'write' => ['role:all'], - ]); - - $client->receive(); - - $this->client->call(Client::METHOD_DELETE, '/database/collections/' . $data['actorsId'] . '/documents/' . $document['body']['$id'], array_merge([ - 'content-type' => 'application/json', - 'x-appwrite-project' => $this->getProject()['$id'], - ], $this->getHeaders())); - - $response = json_decode($client->receive(), true); - - $this->assertArrayHasKey('type', $response); - $this->assertArrayHasKey('data', $response); - $this->assertEquals('event', $response['type']); - $this->assertNotEmpty($response['data']); - $this->assertArrayHasKey('timestamp', $response['data']); - $this->assertCount(3, $response['data']['channels']); - $this->assertContains('documents', $response['data']['channels']); - $this->assertContains('documents.' . $document['body']['$id'], $response['data']['channels']); - $this->assertContains('collections.' . $data['actorsId'] . '.documents', $response['data']['channels']); - $this->assertEquals('database.documents.delete', $response['data']['event']); - $this->assertNotEmpty($response['data']['payload']); - $this->assertEquals($response['data']['payload']['name'], 'Bradley Cooper'); - - $client->close(); - } - - public function testChannelFiles() - { - $user = $this->getUser(); - $session = $user['session'] ?? ''; - $projectId = $this->getProject()['$id']; - - $client = $this->getWebsocket(['files'], [ - 'origin' => 'http://localhost', - 'cookie' => 'a_session_'.$projectId.'=' . $session - ]); - $response = json_decode($client->receive(), true); - - $this->assertArrayHasKey('type', $response); - $this->assertArrayHasKey('data', $response); - $this->assertEquals('connected', $response['type']); - $this->assertNotEmpty($response['data']); - $this->assertCount(1, $response['data']['channels']); - $this->assertContains('files', $response['data']['channels']); - $this->assertNotEmpty($response['data']['user']); - $this->assertEquals($user['$id'], $response['data']['user']['$id']); - - /** - * Test File Create - */ - $file = $this->client->call(Client::METHOD_POST, '/storage/files', array_merge([ - 'content-type' => 'multipart/form-data', - 'x-appwrite-project' => $this->getProject()['$id'], - ], $this->getHeaders()), [ - 'fileId' => 'unique()', - 'file' => new CURLFile(realpath(__DIR__ . '/../../../resources/logo.png'), 'image/png', 'logo.png'), - 'read' => ['role:all'], - 'write' => ['role:all'], - 'folderId' => 'xyz', - ]); - - $response = json_decode($client->receive(), true); - - $this->assertArrayHasKey('type', $response); - $this->assertArrayHasKey('data', $response); - $this->assertEquals('event', $response['type']); - $this->assertNotEmpty($response['data']); - $this->assertArrayHasKey('timestamp', $response['data']); - $this->assertCount(2, $response['data']['channels']); - $this->assertContains('files', $response['data']['channels']); - $this->assertContains('files.' . $file['body']['$id'], $response['data']['channels']); - $this->assertEquals('storage.files.create', $response['data']['event']); - $this->assertNotEmpty($response['data']['payload']); - - $data = ['fileId' => $file['body']['$id']]; - - /** - * Test File Update - */ - $this->client->call(Client::METHOD_PUT, '/storage/files/' . $data['fileId'], array_merge([ - 'content-type' => 'application/json', - 'x-appwrite-project' => $this->getProject()['$id'], - ], $this->getHeaders()), [ - 'read' => ['role:all'], - 'write' => ['role:all'], - ]); - - $response = json_decode($client->receive(), true); - - $this->assertArrayHasKey('type', $response); - $this->assertArrayHasKey('data', $response); - $this->assertEquals('event', $response['type']); - $this->assertNotEmpty($response['data']); - $this->assertArrayHasKey('timestamp', $response['data']); - $this->assertCount(2, $response['data']['channels']); - $this->assertContains('files', $response['data']['channels']); - $this->assertContains('files.' . $file['body']['$id'], $response['data']['channels']); - $this->assertEquals('storage.files.update', $response['data']['event']); - $this->assertNotEmpty($response['data']['payload']); - - /** - * Test File Delete - */ - $this->client->call(Client::METHOD_DELETE, '/storage/files/' . $data['fileId'], array_merge([ - 'content-type' => 'application/json', - 'x-appwrite-project' => $this->getProject()['$id'], - ], $this->getHeaders())); - - $response = json_decode($client->receive(), true); - - $this->assertArrayHasKey('type', $response); - $this->assertArrayHasKey('data', $response); - $this->assertEquals('event', $response['type']); - $this->assertNotEmpty($response['data']); - $this->assertArrayHasKey('timestamp', $response['data']); - $this->assertCount(2, $response['data']['channels']); - $this->assertContains('files', $response['data']['channels']); - $this->assertContains('files.' . $file['body']['$id'], $response['data']['channels']); - $this->assertEquals('storage.files.delete', $response['data']['event']); - $this->assertNotEmpty($response['data']['payload']); - - $client->close(); - } - - public function testChannelExecutions() - { - $user = $this->getUser(); - $session = $user['session'] ?? ''; - $projectId = $this->getProject()['$id']; - - $client = $this->getWebsocket(['executions'], [ - 'origin' => 'http://localhost', - 'cookie' => 'a_session_'.$projectId.'=' . $session - ]); - - $response = json_decode($client->receive(), true); - - $this->assertArrayHasKey('type', $response); - $this->assertArrayHasKey('data', $response); - $this->assertEquals('connected', $response['type']); - $this->assertNotEmpty($response['data']); - $this->assertCount(1, $response['data']['channels']); - $this->assertContains('executions', $response['data']['channels']); - $this->assertNotEmpty($response['data']['user']); - $this->assertEquals($user['$id'], $response['data']['user']['$id']); - - /** - * Test Functions Create - */ - $function = $this->client->call(Client::METHOD_POST, '/functions', [ - 'content-type' => 'application/json', - 'x-appwrite-project' => $this->getProject()['$id'], - 'x-appwrite-key' => $this->getProject()['apiKey'] - ], [ - 'functionId' => 'unique()', - 'name' => 'Test', - 'execute' => ['role:member'], - 'runtime' => 'php-8.0', - 'timeout' => 10, - ]); - - $functionId = $function['body']['$id'] ?? ''; - - $this->assertEquals($function['headers']['status-code'], 201); - $this->assertNotEmpty($function['body']['$id']); - - $tag = $this->client->call(Client::METHOD_POST, '/functions/'.$functionId.'/tags', array_merge([ - 'content-type' => 'multipart/form-data', - 'x-appwrite-project' => $this->getProject()['$id'], - 'x-appwrite-key' => $this->getProject()['apiKey'] - ]), [ - 'command' => 'php index.php', - 'code' => new CURLFile(realpath(__DIR__ . '/../../../resources/functions/timeout.tar.gz'), 'application/x-gzip', 'php-fx.tar.gz'), - ]); - - $tagId = $tag['body']['$id'] ?? ''; - - $this->assertEquals($tag['headers']['status-code'], 201); - $this->assertNotEmpty($tag['body']['$id']); - - $response = $this->client->call(Client::METHOD_PATCH, '/functions/'.$functionId.'/tag', array_merge([ - 'content-type' => 'application/json', - 'x-appwrite-project' => $this->getProject()['$id'], - 'x-appwrite-key' => $this->getProject()['apiKey'] - ]), [ - 'tag' => $tagId, - ]); - - $this->assertEquals($response['headers']['status-code'], 200); - $this->assertNotEmpty($response['body']['$id']); - - $execution = $this->client->call(Client::METHOD_POST, '/functions/'.$functionId.'/executions', array_merge([ - 'content-type' => 'application/json', - 'x-appwrite-project' => $this->getProject()['$id'] - ], $this->getHeaders()), []); - - $this->assertEquals($execution['headers']['status-code'], 201); - $this->assertNotEmpty($execution['body']['$id']); - - $response = json_decode($client->receive(), true); - $responseUpdate = json_decode($client->receive(), true); - - $this->assertArrayHasKey('type', $response); - $this->assertArrayHasKey('data', $response); - $this->assertEquals('event', $response['type']); - $this->assertNotEmpty($response['data']); - $this->assertArrayHasKey('timestamp', $response['data']); - $this->assertCount(3, $response['data']['channels']); - $this->assertContains('executions', $response['data']['channels']); - $this->assertContains('executions.' . $execution['body']['$id'], $response['data']['channels']); - $this->assertContains('functions.' . $execution['body']['functionId'], $response['data']['channels']); - $this->assertEquals('functions.executions.create', $response['data']['event']); - $this->assertNotEmpty($response['data']['payload']); - - $this->assertArrayHasKey('type', $responseUpdate); - $this->assertArrayHasKey('data', $responseUpdate); - $this->assertEquals('event', $responseUpdate['type']); - $this->assertNotEmpty($responseUpdate['data']); - $this->assertArrayHasKey('timestamp', $responseUpdate['data']); - $this->assertCount(3, $responseUpdate['data']['channels']); - $this->assertContains('executions', $responseUpdate['data']['channels']); - $this->assertContains('executions.' . $execution['body']['$id'], $responseUpdate['data']['channels']); - $this->assertContains('functions.' . $execution['body']['functionId'], $responseUpdate['data']['channels']); - $this->assertEquals('functions.executions.update', $responseUpdate['data']['event']); - $this->assertNotEmpty($responseUpdate['data']['payload']); - - $client->close(); - } - - public function testChannelTeams(): array - { - $user = $this->getUser(); - $session = $user['session'] ?? ''; - $projectId = $this->getProject()['$id']; - - $client = $this->getWebsocket(['teams'], [ - 'origin' => 'http://localhost', - 'cookie' => 'a_session_'.$projectId.'=' . $session - ]); - - $response = json_decode($client->receive(), true); - - $this->assertArrayHasKey('type', $response); - $this->assertArrayHasKey('data', $response); - $this->assertEquals('connected', $response['type']); - $this->assertNotEmpty($response['data']); - $this->assertCount(1, $response['data']['channels']); - $this->assertContains('teams', $response['data']['channels']); - $this->assertNotEmpty($response['data']['user']); - $this->assertEquals($user['$id'], $response['data']['user']['$id']); - - /** - * Test Team Create - */ - $team = $this->client->call(Client::METHOD_POST, '/teams', array_merge([ - 'content-type' => 'application/json', - 'x-appwrite-project' => $projectId, - ], $this->getHeaders()), [ - 'teamId' => 'unique()', - 'name' => 'Arsenal' - ]); - - $teamId = $team['body']['$id'] ?? ''; - - $this->assertEquals(201, $team['headers']['status-code']); - $this->assertNotEmpty($team['body']['$id']); - - $response = json_decode($client->receive(), true); - - $this->assertArrayHasKey('type', $response); - $this->assertArrayHasKey('data', $response); - $this->assertEquals('event', $response['type']); - $this->assertNotEmpty($response['data']); - $this->assertArrayHasKey('timestamp', $response['data']); - $this->assertCount(2, $response['data']['channels']); - $this->assertContains('teams', $response['data']['channels']); - $this->assertContains('teams.' . $teamId, $response['data']['channels']); - $this->assertEquals('teams.create', $response['data']['event']); - $this->assertNotEmpty($response['data']['payload']); - - /** - * Test Team Update - */ - $team = $this->client->call(Client::METHOD_PUT, '/teams/'.$teamId, array_merge([ - 'content-type' => 'application/json', - 'x-appwrite-project' => $projectId, - ], $this->getHeaders()), [ - 'name' => 'Manchester' - ]); - - $this->assertEquals($team['headers']['status-code'], 200); - $this->assertNotEmpty($team['body']['$id']); - - $response = json_decode($client->receive(), true); - - $this->assertArrayHasKey('type', $response); - $this->assertArrayHasKey('data', $response); - $this->assertEquals('event', $response['type']); - $this->assertNotEmpty($response['data']); - $this->assertArrayHasKey('timestamp', $response['data']); - $this->assertCount(2, $response['data']['channels']); - $this->assertContains('teams', $response['data']['channels']); - $this->assertContains('teams.' . $teamId, $response['data']['channels']); - $this->assertEquals('teams.update', $response['data']['event']); - $this->assertNotEmpty($response['data']['payload']); - - $client->close(); - - return ['teamId' => $teamId]; - } - - /** - * @depends testChannelTeams - */ - public function testChannelMemberships(array $data) - { - $teamId = $data['teamId'] ?? ''; - - $user = $this->getUser(); - $session = $user['session'] ?? ''; - $projectId = $this->getProject()['$id']; - - $client = $this->getWebsocket(['memberships'], [ - 'origin' => 'http://localhost', - 'cookie' => 'a_session_'.$projectId.'='.$session - ]); - - $response = json_decode($client->receive(), true); - - $this->assertArrayHasKey('type', $response); - $this->assertArrayHasKey('data', $response); - $this->assertEquals('connected', $response['type']); - $this->assertNotEmpty($response['data']); - $this->assertCount(1, $response['data']['channels']); - $this->assertContains('memberships', $response['data']['channels']); - $this->assertNotEmpty($response['data']['user']); - $this->assertEquals($user['$id'], $response['data']['user']['$id']); - - $response = $this->client->call(Client::METHOD_GET, '/teams/'.$teamId.'/memberships', array_merge([ - 'content-type' => 'application/json', - 'x-appwrite-project' => $this->getProject()['$id'], - ], $this->getHeaders())); - - $membershipId = $response['body']['memberships'][0]['$id']; - - /** - * Test Update Membership - */ - $roles = ['admin', 'editor', 'uncle']; - $this->client->call(Client::METHOD_PATCH, '/teams/'.$teamId.'/memberships/'.$membershipId, array_merge([ - 'origin' => 'http://localhost', - 'content-type' => 'application/json', - 'x-appwrite-project' => $this->getProject()['$id'], - ], $this->getHeaders()), [ - 'roles' => $roles - ]); - - $response = json_decode($client->receive(), true); - - $this->assertArrayHasKey('type', $response); - $this->assertArrayHasKey('data', $response); - $this->assertEquals('event', $response['type']); - $this->assertNotEmpty($response['data']); - $this->assertArrayHasKey('timestamp', $response['data']); - $this->assertCount(2, $response['data']['channels']); - $this->assertContains('memberships', $response['data']['channels']); - $this->assertContains('memberships.' . $membershipId, $response['data']['channels']); - $this->assertEquals('teams.memberships.update', $response['data']['event']); - $this->assertNotEmpty($response['data']['payload']); - - $client->close(); - } } diff --git a/tests/e2e/Services/Realtime/RealtimeConsoleClientTest.php b/tests/e2e/Services/Realtime/RealtimeConsoleClientTest.php new file mode 100644 index 0000000000..00c39c5d05 --- /dev/null +++ b/tests/e2e/Services/Realtime/RealtimeConsoleClientTest.php @@ -0,0 +1,276 @@ +getUser(); + $session = $user['session'] ?? ''; + $projectId = 'console'; + + $client = $this->getWebsocket(['console'], [ + 'origin' => 'http://localhost', + 'cookie' => 'a_session_console='. $this->getRoot()['session'], + ], $projectId); + + $response = json_decode($client->receive(), true); + + $this->assertArrayHasKey('type', $response); + $this->assertArrayHasKey('data', $response); + $this->assertEquals('connected', $response['type']); + $this->assertNotEmpty($response['data']); + $this->assertCount(1, $response['data']['channels']); + $this->assertContains('console', $response['data']['channels']); + $this->assertNotEmpty($response['data']['user']); + + /** + * Test Attributes + */ + $actors = $this->client->call(Client::METHOD_POST, '/database/collections', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), [ + 'collectionId' => 'unique()', + 'name' => 'Actors', + 'read' => ['role:all'], + 'write' => ['role:all'], + 'permission' => 'collection' + ]); + + $data = ['actorsId' => $actors['body']['$id']]; + + $name = $this->client->call(Client::METHOD_POST, '/database/collections/' . $data['actorsId'] . '/attributes/string', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), [ + 'attributeId' => 'name', + 'size' => 256, + 'required' => true, + ]); + + $this->assertEquals($name['headers']['status-code'], 201); + $this->assertEquals($name['body']['key'], 'name'); + $this->assertEquals($name['body']['type'], 'string'); + $this->assertEquals($name['body']['size'], 256); + $this->assertEquals($name['body']['required'], true); + + $response = json_decode($client->receive(), true); + + $this->assertArrayHasKey('type', $response); + $this->assertArrayHasKey('data', $response); + $this->assertEquals('event', $response['type']); + $this->assertNotEmpty($response['data']); + $this->assertArrayHasKey('timestamp', $response['data']); + $this->assertCount(1, $response['data']['channels']); + $this->assertContains('console', $response['data']['channels']); + $this->assertEquals('database.attributes.create', $response['data']['event']); + $this->assertNotEmpty($response['data']['payload']); + $this->assertEquals('processing', $response['data']['payload']['status']); + + $response = json_decode($client->receive(), true); + + $this->assertArrayHasKey('type', $response); + $this->assertArrayHasKey('data', $response); + $this->assertEquals('event', $response['type']); + $this->assertNotEmpty($response['data']); + $this->assertArrayHasKey('timestamp', $response['data']); + $this->assertCount(1, $response['data']['channels']); + $this->assertContains('console', $response['data']['channels']); + $this->assertEquals('database.attributes.update', $response['data']['event']); + $this->assertNotEmpty($response['data']['payload']); + $this->assertEquals('available', $response['data']['payload']['status']); + + $client->close(); + + return $data; + } + + /** + * @depends testAttributes + */ + public function testIndexes(array $data) + { + $user = $this->getUser(); + $session = $user['session'] ?? ''; + $projectId = 'console'; + + $client = $this->getWebsocket(['console'], [ + 'origin' => 'http://localhost', + 'cookie' => 'a_session_console='. $this->getRoot()['session'], + ], $projectId); + + $response = json_decode($client->receive(), true); + + $this->assertArrayHasKey('type', $response); + $this->assertArrayHasKey('data', $response); + $this->assertEquals('connected', $response['type']); + $this->assertNotEmpty($response['data']); + $this->assertCount(1, $response['data']['channels']); + $this->assertContains('console', $response['data']['channels']); + $this->assertNotEmpty($response['data']['user']); + + /** + * Test Indexes + */ + $index = $this->client->call(Client::METHOD_POST, '/database/collections/' . $data['actorsId'] . '/indexes', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), [ + 'indexId' => 'key_name', + 'type' => 'key', + 'attributes' => [ + 'name', + ], + ]); + + $this->assertEquals($index['headers']['status-code'], 201); + + $response = json_decode($client->receive(), true); + + $this->assertArrayHasKey('type', $response); + $this->assertArrayHasKey('data', $response); + $this->assertEquals('event', $response['type']); + $this->assertNotEmpty($response['data']); + $this->assertArrayHasKey('timestamp', $response['data']); + $this->assertCount(1, $response['data']['channels']); + $this->assertContains('console', $response['data']['channels']); + $this->assertEquals('database.indexes.create', $response['data']['event']); + $this->assertNotEmpty($response['data']['payload']); + $this->assertEquals('processing', $response['data']['payload']['status']); + + $response = json_decode($client->receive(), true); + + $this->assertArrayHasKey('type', $response); + $this->assertArrayHasKey('data', $response); + $this->assertEquals('event', $response['type']); + $this->assertNotEmpty($response['data']); + $this->assertArrayHasKey('timestamp', $response['data']); + $this->assertCount(1, $response['data']['channels']); + $this->assertContains('console', $response['data']['channels']); + $this->assertEquals('database.indexes.update', $response['data']['event']); + $this->assertNotEmpty($response['data']['payload']); + $this->assertEquals('available', $response['data']['payload']['status']); + + $client->close(); + + return $data; + } + + /** + * @depends testIndexes + */ + public function testDeleteIndex(array $data) + { + $user = $this->getUser(); + $session = $user['session'] ?? ''; + $projectId = 'console'; + + $client = $this->getWebsocket(['console'], [ + 'origin' => 'http://localhost', + 'cookie' => 'a_session_console='. $this->getRoot()['session'], + ], $projectId); + + $response = json_decode($client->receive(), true); + + $this->assertArrayHasKey('type', $response); + $this->assertArrayHasKey('data', $response); + $this->assertEquals('connected', $response['type']); + $this->assertNotEmpty($response['data']); + $this->assertCount(1, $response['data']['channels']); + $this->assertContains('console', $response['data']['channels']); + $this->assertNotEmpty($response['data']['user']); + + /** + * Test Delete Index + */ + $attribute = $this->client->call(Client::METHOD_DELETE, '/database/collections/' . $data['actorsId'] . '/indexes/key_name', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders())); + + $this->assertEquals($attribute['headers']['status-code'], 204); + + $response = json_decode($client->receive(), true); + + $this->assertArrayHasKey('type', $response); + $this->assertArrayHasKey('data', $response); + $this->assertEquals('event', $response['type']); + $this->assertNotEmpty($response['data']); + $this->assertArrayHasKey('timestamp', $response['data']); + $this->assertCount(1, $response['data']['channels']); + $this->assertContains('console', $response['data']['channels']); + $this->assertEquals('database.indexes.delete', $response['data']['event']); + $this->assertNotEmpty($response['data']['payload']); + + $client->close(); + + return $data; + } + + /** + * @depends testDeleteIndex + */ + public function testDeleteAttribute(array $data) + { + $user = $this->getUser(); + $session = $user['session'] ?? ''; + $projectId = 'console'; + + $client = $this->getWebsocket(['console'], [ + 'origin' => 'http://localhost', + 'cookie' => 'a_session_console='. $this->getRoot()['session'], + ], $projectId); + + $response = json_decode($client->receive(), true); + + $this->assertArrayHasKey('type', $response); + $this->assertArrayHasKey('data', $response); + $this->assertEquals('connected', $response['type']); + $this->assertNotEmpty($response['data']); + $this->assertCount(1, $response['data']['channels']); + $this->assertContains('console', $response['data']['channels']); + $this->assertNotEmpty($response['data']['user']); + + /** + * Test Delete Attribute + */ + $attribute = $this->client->call(Client::METHOD_DELETE, '/database/collections/' . $data['actorsId'] . '/attributes/name', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders())); + + $this->assertEquals($attribute['headers']['status-code'], 204); + + $response = json_decode($client->receive(), true); + + $this->assertArrayHasKey('type', $response); + $this->assertArrayHasKey('data', $response); + $this->assertEquals('event', $response['type']); + $this->assertNotEmpty($response['data']); + $this->assertArrayHasKey('timestamp', $response['data']); + $this->assertCount(1, $response['data']['channels']); + $this->assertContains('console', $response['data']['channels']); + $this->assertEquals('database.attributes.delete', $response['data']['event']); + $this->assertNotEmpty($response['data']['payload']); + + $client->close(); + } +} \ No newline at end of file diff --git a/tests/e2e/Services/Realtime/RealtimeCustomClientTest.php b/tests/e2e/Services/Realtime/RealtimeCustomClientTest.php index 8822c493b2..ed10aefa59 100644 --- a/tests/e2e/Services/Realtime/RealtimeCustomClientTest.php +++ b/tests/e2e/Services/Realtime/RealtimeCustomClientTest.php @@ -2,9 +2,12 @@ namespace Tests\E2E\Services\Realtime; +use CURLFile; +use Tests\E2E\Client; use Tests\E2E\Scopes\Scope; use Tests\E2E\Scopes\ProjectCustom; use Tests\E2E\Scopes\SideClient; +use WebSocket\ConnectionException; class RealtimeCustomClientTest extends Scope @@ -12,4 +15,1038 @@ class RealtimeCustomClientTest extends Scope use RealtimeBase; use ProjectCustom; use SideClient; + + public function testChannelParsing() + { + $user = $this->getUser(); + $userId = $user['$id'] ?? ''; + $session = $user['session'] ?? ''; + + $headers = [ + 'origin' => 'http://localhost', + 'cookie' => 'a_session_'.$this->getProject()['$id'].'=' . $session + ]; + + $client = $this->getWebsocket(['documents'], $headers); + $response = json_decode($client->receive(), true); + + $this->assertArrayHasKey('type', $response); + $this->assertArrayHasKey('data', $response); + $this->assertEquals('connected', $response['type']); + $this->assertNotEmpty($response['data']); + $this->assertNotEmpty($response['data']['user']); + $this->assertCount(1, $response['data']['channels']); + $this->assertContains('documents', $response['data']['channels']); + $this->assertEquals($userId, $response['data']['user']['$id']); + + $client->close(); + + $client = $this->getWebsocket(['account'], $headers); + $response = json_decode($client->receive(), true); + + $this->assertArrayHasKey('type', $response); + $this->assertArrayHasKey('data', $response); + $this->assertEquals('connected', $response['type']); + $this->assertNotEmpty($response['data']); + $this->assertNotEmpty($response['data']['user']); + $this->assertCount(2, $response['data']['channels']); + $this->assertContains('account', $response['data']['channels']); + $this->assertContains('account.' . $userId, $response['data']['channels']); + $this->assertEquals($userId, $response['data']['user']['$id']); + + $client->close(); + + $client = $this->getWebsocket(['account', 'documents', 'account.123'], $headers); + $response = json_decode($client->receive(), true); + + $this->assertArrayHasKey('type', $response); + $this->assertArrayHasKey('data', $response); + $this->assertEquals('connected', $response['type']); + $this->assertNotEmpty($response['data']); + $this->assertNotEmpty($response['data']['user']); + $this->assertCount(3, $response['data']['channels']); + $this->assertContains('documents', $response['data']['channels']); + $this->assertContains('account', $response['data']['channels']); + $this->assertContains('account.' . $userId, $response['data']['channels']); + $this->assertEquals($userId, $response['data']['user']['$id']); + + $client->close(); + + $client = $this->getWebsocket([ + 'account', + 'files', + 'files.1', + 'collections', + 'collections.1', + 'collections.1.documents', + 'collections.2', + 'collections.2.documents', + 'documents', + 'documents.1', + 'documents.2', + ], $headers); + + $response = json_decode($client->receive(), true); + + $this->assertArrayHasKey('type', $response); + $this->assertArrayHasKey('data', $response); + $this->assertEquals('connected', $response['type']); + $this->assertNotEmpty($response['data']); + $this->assertNotEmpty($response['data']['user']); + $this->assertCount(12, $response['data']['channels']); + $this->assertContains('account', $response['data']['channels']); + $this->assertContains('account.' . $userId, $response['data']['channels']); + $this->assertContains('files', $response['data']['channels']); + $this->assertContains('files.1', $response['data']['channels']); + $this->assertContains('collections', $response['data']['channels']); + $this->assertContains('collections.1', $response['data']['channels']); + $this->assertContains('collections.1.documents', $response['data']['channels']); + $this->assertContains('collections.2', $response['data']['channels']); + $this->assertContains('collections.2.documents', $response['data']['channels']); + $this->assertContains('documents', $response['data']['channels']); + $this->assertContains('documents.1', $response['data']['channels']); + $this->assertContains('documents.2', $response['data']['channels']); + $this->assertEquals($userId, $response['data']['user']['$id']); + + $client->close(); + } + + public function testManualAuthentication() + { + $user = $this->getUser(); + $userId = $user['$id'] ?? ''; + $session = $user['session'] ?? ''; + + /** + * Test for SUCCESS + */ + $client = $this->getWebsocket(['account'], [ + 'origin' => 'http://localhost' + ]); + $response = json_decode($client->receive(), true); + + $this->assertArrayHasKey('type', $response); + $this->assertArrayHasKey('data', $response); + $this->assertEquals('connected', $response['type']); + $this->assertNotEmpty($response['data']); + $this->assertCount(1, $response['data']['channels']); + $this->assertContains('account', $response['data']['channels']); + + $client->send(\json_encode([ + 'type' => 'authentication', + 'data' => [ + 'session' => $session + ] + ])); + + $response = json_decode($client->receive(), true); + + $this->assertArrayHasKey('type', $response); + $this->assertArrayHasKey('data', $response); + $this->assertEquals('response', $response['type']); + $this->assertNotEmpty($response['data']); + $this->assertEquals('authentication', $response['data']['to']); + $this->assertTrue($response['data']['success']); + $this->assertNotEmpty($response['data']['user']); + $this->assertEquals($userId, $response['data']['user']['$id']); + + /** + * Test for FAILURE + */ + $client->send(\json_encode([ + 'type' => 'authentication', + 'data' => [ + 'session' => 'invalid_session' + ] + ])); + + $response = json_decode($client->receive(), true); + + $this->assertArrayHasKey('type', $response); + $this->assertArrayHasKey('data', $response); + $this->assertEquals('error', $response['type']); + $this->assertNotEmpty($response['data']); + $this->assertEquals(1003, $response['data']['code']); + $this->assertEquals('Session is not valid.', $response['data']['message']); + + $client->send(\json_encode([ + 'type' => 'authentication', + 'data' => [] + ])); + + $response = json_decode($client->receive(), true); + + $this->assertArrayHasKey('type', $response); + $this->assertArrayHasKey('data', $response); + $this->assertEquals('error', $response['type']); + $this->assertNotEmpty($response['data']); + $this->assertEquals(1003, $response['data']['code']); + $this->assertEquals('Payload is not valid.', $response['data']['message']); + + $client->send(\json_encode([ + 'type' => 'unknown', + 'data' => [ + 'session' => 'invalid_session' + ] + ])); + + $response = json_decode($client->receive(), true); + + $this->assertArrayHasKey('type', $response); + $this->assertArrayHasKey('data', $response); + $this->assertEquals('error', $response['type']); + $this->assertNotEmpty($response['data']); + $this->assertEquals(1003, $response['data']['code']); + $this->assertEquals('Message type is not valid.', $response['data']['message']); + + $client->send(\json_encode([ + 'test' => '123', + ])); + + $response = json_decode($client->receive(), true); + + $this->assertArrayHasKey('type', $response); + $this->assertArrayHasKey('data', $response); + $this->assertEquals('error', $response['type']); + $this->assertNotEmpty($response['data']); + $this->assertEquals(1003, $response['data']['code']); + $this->assertEquals('Message format is not valid.', $response['data']['message']); + + + $client->close(); + } + + public function testConnectionPlatform() + { + /** + * Test for FAILURE + */ + $client = $this->getWebsocket(['documents'], ['origin' => 'http://appwrite.unknown']); + $payload = json_decode($client->receive(), true); + + $this->assertArrayHasKey('type', $payload); + $this->assertArrayHasKey('data', $payload); + $this->assertEquals('error', $payload['type']); + $this->assertEquals(1008, $payload['data']['code']); + $this->assertEquals('Invalid Origin. Register your new client (appwrite.unknown) as a new Web platform on your project console dashboard', $payload['data']['message']); + $this->expectException(ConnectionException::class); // Check if server disconnnected client + $client->close(); + } + + public function testChannelAccount() + { + $user = $this->getUser(); + $userId = $user['$id'] ?? ''; + $session = $user['session'] ?? ''; + $projectId = $this->getProject()['$id']; + + $client = $this->getWebsocket(['account'], [ + 'origin' => 'http://localhost', + 'cookie' => 'a_session_'.$projectId.'=' . $session + ]); + $response = json_decode($client->receive(), true); + + $this->assertArrayHasKey('type', $response); + $this->assertArrayHasKey('data', $response); + $this->assertEquals('connected', $response['type']); + $this->assertNotEmpty($response['data']); + $this->assertCount(2, $response['data']['channels']); + $this->assertContains('account', $response['data']['channels']); + $this->assertContains('account.' . $userId, $response['data']['channels']); + $this->assertNotEmpty($response['data']['user']); + $this->assertEquals($userId, $response['data']['user']['$id']); + + /** + * Test Account Name Event + */ + $name = "Torsten Dittmann"; + + $this->client->call(Client::METHOD_PATCH, '/account/name', array_merge([ + 'origin' => 'http://localhost', + 'content-type' => 'application/json', + 'x-appwrite-project' => $projectId, + 'cookie' => 'a_session_' . $projectId . '=' . $session, + ]), [ + 'name' => $name + ]); + + $response = json_decode($client->receive(), true); + + $this->assertArrayHasKey('type', $response); + $this->assertArrayHasKey('data', $response); + $this->assertEquals('event', $response['type']); + $this->assertNotEmpty($response['data']); + $this->assertCount(2, $response['data']['channels']); + $this->assertArrayHasKey('timestamp', $response['data']); + $this->assertContains('account', $response['data']['channels']); + $this->assertContains('account.' . $userId, $response['data']['channels']); + $this->assertEquals('account.update.name', $response['data']['event']); + $this->assertNotEmpty($response['data']['payload']); + + $this->assertEquals($name, $response['data']['payload']['name']); + + + /** + * Test Account Password Event + */ + $this->client->call(Client::METHOD_PATCH, '/account/password', array_merge([ + 'origin' => 'http://localhost', + 'content-type' => 'application/json', + 'x-appwrite-project' => $projectId, + 'cookie' => 'a_session_'.$projectId.'=' . $session, + ]), [ + 'password' => 'new-password', + 'oldPassword' => 'password', + ]); + + $response = json_decode($client->receive(), true); + + $this->assertArrayHasKey('type', $response); + $this->assertArrayHasKey('data', $response); + $this->assertEquals('event', $response['type']); + $this->assertNotEmpty($response['data']); + $this->assertCount(2, $response['data']['channels']); + $this->assertArrayHasKey('timestamp', $response['data']); + $this->assertContains('account', $response['data']['channels']); + $this->assertContains('account.' . $userId, $response['data']['channels']); + $this->assertEquals('account.update.password', $response['data']['event']); + $this->assertNotEmpty($response['data']['payload']); + + $this->assertEquals($name, $response['data']['payload']['name']); + + /** + * Test Account Email Update + */ + $this->client->call(Client::METHOD_PATCH, '/account/email', array_merge([ + 'origin' => 'http://localhost', + 'content-type' => 'application/json', + 'x-appwrite-project' => $projectId, + 'cookie' => 'a_session_'.$projectId.'=' . $session, + ]), [ + 'email' => 'torsten@appwrite.io', + 'password' => 'new-password', + ]); + + $response = json_decode($client->receive(), true); + + $this->assertArrayHasKey('type', $response); + $this->assertArrayHasKey('data', $response); + $this->assertEquals('event', $response['type']); + $this->assertNotEmpty($response['data']); + $this->assertCount(2, $response['data']['channels']); + $this->assertArrayHasKey('timestamp', $response['data']); + $this->assertContains('account', $response['data']['channels']); + $this->assertContains('account.' . $userId, $response['data']['channels']); + $this->assertEquals('account.update.email', $response['data']['event']); + $this->assertNotEmpty($response['data']['payload']); + + $this->assertEquals('torsten@appwrite.io', $response['data']['payload']['email']); + + /** + * Test Account Verification Create + */ + $this->client->call(Client::METHOD_POST, '/account/verification', array_merge([ + 'origin' => 'http://localhost', + 'content-type' => 'application/json', + 'x-appwrite-project' => $projectId, + 'cookie' => 'a_session_'.$projectId.'=' . $session, + ]), [ + 'url' => 'http://localhost/verification', + ]); + + $response = json_decode($client->receive(), true); + + $this->assertArrayHasKey('type', $response); + $this->assertArrayHasKey('data', $response); + $this->assertEquals('event', $response['type']); + $this->assertNotEmpty($response['data']); + $this->assertCount(2, $response['data']['channels']); + $this->assertArrayHasKey('timestamp', $response['data']); + $this->assertContains('account', $response['data']['channels']); + $this->assertContains('account.' . $userId, $response['data']['channels']); + $this->assertEquals('account.verification.create', $response['data']['event']); + + $lastEmail = $this->getLastEmail(); + $verification = substr($lastEmail['text'], strpos($lastEmail['text'], '&secret=', 0) + 8, 256); + + /** + * Test Account Verification Complete + */ + $response = $this->client->call(Client::METHOD_PUT, '/account/verification', array_merge([ + 'origin' => 'http://localhost', + 'content-type' => 'application/json', + 'x-appwrite-project' => $projectId, + 'cookie' => 'a_session_'.$projectId.'=' . $session, + ]), [ + 'userId' => $userId, + 'secret' => $verification, + ]); + + $response = json_decode($client->receive(), true); + + $this->assertArrayHasKey('type', $response); + $this->assertArrayHasKey('data', $response); + $this->assertEquals('event', $response['type']); + $this->assertNotEmpty($response['data']); + $this->assertCount(2, $response['data']['channels']); + $this->assertArrayHasKey('timestamp', $response['data']); + $this->assertContains('account', $response['data']['channels']); + $this->assertContains('account.' . $userId, $response['data']['channels']); + $this->assertEquals('account.verification.update', $response['data']['event']); + + /** + * Test Acoount Prefs Update + */ + $this->client->call(Client::METHOD_PATCH, '/account/prefs', array_merge([ + 'origin' => 'http://localhost', + 'content-type' => 'application/json', + 'x-appwrite-project' => $projectId, + 'cookie' => 'a_session_'.$projectId.'=' . $session, + ]), [ + 'prefs' => [ + 'prefKey1' => 'prefValue1', + 'prefKey2' => 'prefValue2', + ] + ]); + + $response = json_decode($client->receive(), true); + + $this->assertArrayHasKey('type', $response); + $this->assertArrayHasKey('data', $response); + $this->assertEquals('event', $response['type']); + $this->assertNotEmpty($response['data']); + $this->assertCount(2, $response['data']['channels']); + $this->assertArrayHasKey('timestamp', $response['data']); + $this->assertContains('account', $response['data']['channels']); + $this->assertContains('account.' . $userId, $response['data']['channels']); + $this->assertEquals('account.update.prefs', $response['data']['event']); + $this->assertNotEmpty($response['data']['payload']); + + /** + * Test Account Session Create + */ + $response = $this->client->call(Client::METHOD_POST, '/account/sessions', array_merge([ + 'origin' => 'http://localhost', + 'content-type' => 'application/json', + 'x-appwrite-project' => $projectId, + ]), [ + 'email' => 'torsten@appwrite.io', + 'password' => 'new-password', + ]); + + $sessionNew = $this->client->parseCookie((string)$response['headers']['set-cookie'])['a_session_'.$projectId]; + $sessionNewId = $response['body']['$id']; + + $response = json_decode($client->receive(), true); + + $this->assertArrayHasKey('type', $response); + $this->assertArrayHasKey('data', $response); + $this->assertEquals('event', $response['type']); + $this->assertNotEmpty($response['data']); + $this->assertCount(2, $response['data']['channels']); + $this->assertArrayHasKey('timestamp', $response['data']); + $this->assertContains('account', $response['data']['channels']); + $this->assertContains('account.' . $userId, $response['data']['channels']); + $this->assertEquals('account.sessions.create', $response['data']['event']); + $this->assertNotEmpty($response['data']['payload']); + + /** + * Test Account Session Delete + */ + $this->client->call(Client::METHOD_DELETE, '/account/sessions/'.$sessionNewId, array_merge([ + 'origin' => 'http://localhost', + 'content-type' => 'application/json', + 'x-appwrite-project' => $projectId, + 'cookie' => 'a_session_'.$projectId.'=' . $sessionNew, + ])); + + $response = json_decode($client->receive(), true); + + $this->assertArrayHasKey('type', $response); + $this->assertArrayHasKey('data', $response); + $this->assertEquals('event', $response['type']); + $this->assertNotEmpty($response['data']); + $this->assertCount(2, $response['data']['channels']); + $this->assertArrayHasKey('timestamp', $response['data']); + $this->assertContains('account', $response['data']['channels']); + $this->assertContains('account.' . $userId, $response['data']['channels']); + $this->assertEquals('account.sessions.delete', $response['data']['event']); + $this->assertNotEmpty($response['data']['payload']); + + /** + * Test Account Create Recovery + */ + $this->client->call(Client::METHOD_POST, '/account/recovery', array_merge([ + 'origin' => 'http://localhost', + 'content-type' => 'application/json', + 'x-appwrite-project' => $projectId, + ]), [ + 'email' => 'torsten@appwrite.io', + 'url' => 'http://localhost/recovery', + ]); + + $response = json_decode($client->receive(), true); + + $lastEmail = $this->getLastEmail(); + $recovery = substr($lastEmail['text'], strpos($lastEmail['text'], '&secret=', 0) + 8, 256); + + $this->assertArrayHasKey('type', $response); + $this->assertArrayHasKey('data', $response); + $this->assertEquals('event', $response['type']); + $this->assertNotEmpty($response['data']); + $this->assertCount(2, $response['data']['channels']); + $this->assertArrayHasKey('timestamp', $response['data']); + $this->assertContains('account', $response['data']['channels']); + $this->assertContains('account.' . $userId, $response['data']['channels']); + $this->assertEquals('account.recovery.create', $response['data']['event']); + $this->assertNotEmpty($response['data']['payload']); + + $response = $this->client->call(Client::METHOD_PUT, '/account/recovery', array_merge([ + 'origin' => 'http://localhost', + 'content-type' => 'application/json', + 'x-appwrite-project' => $projectId, + ]), [ + 'userId' => $userId, + 'secret' => $recovery, + 'password' => 'test-recovery', + 'passwordAgain' => 'test-recovery', + ]); + + $response = json_decode($client->receive(), true); + + $this->assertArrayHasKey('type', $response); + $this->assertArrayHasKey('data', $response); + $this->assertEquals('event', $response['type']); + $this->assertNotEmpty($response['data']); + $this->assertCount(2, $response['data']['channels']); + $this->assertArrayHasKey('timestamp', $response['data']); + $this->assertContains('account', $response['data']['channels']); + $this->assertContains('account.' . $userId, $response['data']['channels']); + $this->assertEquals('account.recovery.update', $response['data']['event']); + $this->assertNotEmpty($response['data']['payload']); + + $client->close(); + } + + public function testChannelDatabase() + { + $user = $this->getUser(); + $session = $user['session'] ?? ''; + $projectId = $this->getProject()['$id']; + + $client = $this->getWebsocket(['documents', 'collections'], [ + 'origin' => 'http://localhost', + 'cookie' => 'a_session_'.$projectId.'=' . $session + ]); + + $response = json_decode($client->receive(), true); + + $this->assertArrayHasKey('type', $response); + $this->assertArrayHasKey('data', $response); + $this->assertEquals('connected', $response['type']); + $this->assertNotEmpty($response['data']); + $this->assertCount(2, $response['data']['channels']); + $this->assertContains('documents', $response['data']['channels']); + $this->assertContains('collections', $response['data']['channels']); + $this->assertNotEmpty($response['data']['user']); + $this->assertEquals($user['$id'], $response['data']['user']['$id']); + + /** + * Test Collection Create + */ + $actors = $this->client->call(Client::METHOD_POST, '/database/collections', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'] + ]), [ + 'collectionId' => 'unique()', + 'name' => 'Actors', + 'read' => ['role:all'], + 'write' => ['role:all'], + 'permission' => 'collection' + ]); + + $response = json_decode($client->receive(), true); + + $this->assertArrayHasKey('type', $response); + $this->assertArrayHasKey('data', $response); + $this->assertEquals('event', $response['type']); + $this->assertNotEmpty($response['data']); + $this->assertArrayHasKey('timestamp', $response['data']); + $this->assertCount(2, $response['data']['channels']); + $this->assertContains('collections', $response['data']['channels']); + $this->assertContains('collections.' . $actors['body']['$id'], $response['data']['channels']); + $this->assertEquals('database.collections.create', $response['data']['event']); + $this->assertNotEmpty($response['data']['payload']); + + $data = ['actorsId' => $actors['body']['$id']]; + + $name = $this->client->call(Client::METHOD_POST, '/database/collections/' . $data['actorsId'] . '/attributes/string', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'] + ]), [ + 'attributeId' => 'name', + 'size' => 256, + 'required' => true, + ]); + + $this->assertEquals($name['headers']['status-code'], 201); + $this->assertEquals($name['body']['key'], 'name'); + $this->assertEquals($name['body']['type'], 'string'); + $this->assertEquals($name['body']['size'], 256); + $this->assertEquals($name['body']['required'], true); + + sleep(2); + + /** + * Test Document Create + */ + $document = $this->client->call(Client::METHOD_POST, '/database/collections/' . $data['actorsId'] . '/documents', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), [ + 'documentId' => 'unique()', + 'data' => [ + 'name' => 'Chris Evans' + ], + 'read' => ['role:all'], + 'write' => ['role:all'], + ]); + + $response = json_decode($client->receive(), true); + + $this->assertArrayHasKey('type', $response); + $this->assertArrayHasKey('data', $response); + $this->assertEquals('event', $response['type']); + $this->assertNotEmpty($response['data']); + $this->assertArrayHasKey('timestamp', $response['data']); + $this->assertCount(3, $response['data']['channels']); + $this->assertContains('documents', $response['data']['channels']); + $this->assertContains('documents.' . $document['body']['$id'], $response['data']['channels']); + $this->assertContains('collections.' . $actors['body']['$id'] . '.documents', $response['data']['channels']); + $this->assertEquals('database.documents.create', $response['data']['event']); + $this->assertNotEmpty($response['data']['payload']); + $this->assertEquals($response['data']['payload']['name'], 'Chris Evans'); + + $data['documentId'] = $document['body']['$id']; + + /** + * Test Document Update + */ + $document = $this->client->call(Client::METHOD_PATCH, '/database/collections/' . $data['actorsId'] . '/documents/' . $data['documentId'], array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), [ + 'documentId' => 'unique()', + 'data' => [ + 'name' => 'Chris Evans 2' + ], + 'read' => ['role:all'], + 'write' => ['role:all'], + ]); + + $response = json_decode($client->receive(), true); + + $this->assertArrayHasKey('type', $response); + $this->assertArrayHasKey('data', $response); + $this->assertEquals('event', $response['type']); + $this->assertNotEmpty($response['data']); + $this->assertArrayHasKey('timestamp', $response['data']); + $this->assertCount(3, $response['data']['channels']); + $this->assertContains('documents', $response['data']['channels']); + $this->assertContains('documents.' . $data['documentId'], $response['data']['channels']); + $this->assertContains('collections.' . $data['actorsId'] . '.documents', $response['data']['channels']); + $this->assertEquals('database.documents.update', $response['data']['event']); + $this->assertNotEmpty($response['data']['payload']); + + $this->assertEquals($response['data']['payload']['name'], 'Chris Evans 2'); + + + /** + * Test Document Delete + */ + $document = $this->client->call(Client::METHOD_POST, '/database/collections/' . $data['actorsId'] . '/documents', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), [ + 'documentId' => 'unique()', + 'data' => [ + 'name' => 'Bradley Cooper' + ], + 'read' => ['role:all'], + 'write' => ['role:all'], + ]); + + $client->receive(); + + $this->client->call(Client::METHOD_DELETE, '/database/collections/' . $data['actorsId'] . '/documents/' . $document['body']['$id'], array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders())); + + $response = json_decode($client->receive(), true); + + $this->assertArrayHasKey('type', $response); + $this->assertArrayHasKey('data', $response); + $this->assertEquals('event', $response['type']); + $this->assertNotEmpty($response['data']); + $this->assertArrayHasKey('timestamp', $response['data']); + $this->assertCount(3, $response['data']['channels']); + $this->assertContains('documents', $response['data']['channels']); + $this->assertContains('documents.' . $document['body']['$id'], $response['data']['channels']); + $this->assertContains('collections.' . $data['actorsId'] . '.documents', $response['data']['channels']); + $this->assertEquals('database.documents.delete', $response['data']['event']); + $this->assertNotEmpty($response['data']['payload']); + $this->assertEquals($response['data']['payload']['name'], 'Bradley Cooper'); + + $client->close(); + } + + public function testChannelFiles() + { + $user = $this->getUser(); + $session = $user['session'] ?? ''; + $projectId = $this->getProject()['$id']; + + $client = $this->getWebsocket(['files'], [ + 'origin' => 'http://localhost', + 'cookie' => 'a_session_'.$projectId.'=' . $session + ]); + $response = json_decode($client->receive(), true); + + $this->assertArrayHasKey('type', $response); + $this->assertArrayHasKey('data', $response); + $this->assertEquals('connected', $response['type']); + $this->assertNotEmpty($response['data']); + $this->assertCount(1, $response['data']['channels']); + $this->assertContains('files', $response['data']['channels']); + $this->assertNotEmpty($response['data']['user']); + $this->assertEquals($user['$id'], $response['data']['user']['$id']); + + /** + * Test File Create + */ + $file = $this->client->call(Client::METHOD_POST, '/storage/files', array_merge([ + 'content-type' => 'multipart/form-data', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), [ + 'fileId' => 'unique()', + 'file' => new CURLFile(realpath(__DIR__ . '/../../../resources/logo.png'), 'image/png', 'logo.png'), + 'read' => ['role:all'], + 'write' => ['role:all'], + 'folderId' => 'xyz', + ]); + + $response = json_decode($client->receive(), true); + + $this->assertArrayHasKey('type', $response); + $this->assertArrayHasKey('data', $response); + $this->assertEquals('event', $response['type']); + $this->assertNotEmpty($response['data']); + $this->assertArrayHasKey('timestamp', $response['data']); + $this->assertCount(2, $response['data']['channels']); + $this->assertContains('files', $response['data']['channels']); + $this->assertContains('files.' . $file['body']['$id'], $response['data']['channels']); + $this->assertEquals('storage.files.create', $response['data']['event']); + $this->assertNotEmpty($response['data']['payload']); + + $data = ['fileId' => $file['body']['$id']]; + + /** + * Test File Update + */ + $this->client->call(Client::METHOD_PUT, '/storage/files/' . $data['fileId'], array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), [ + 'read' => ['role:all'], + 'write' => ['role:all'], + ]); + + $response = json_decode($client->receive(), true); + + $this->assertArrayHasKey('type', $response); + $this->assertArrayHasKey('data', $response); + $this->assertEquals('event', $response['type']); + $this->assertNotEmpty($response['data']); + $this->assertArrayHasKey('timestamp', $response['data']); + $this->assertCount(2, $response['data']['channels']); + $this->assertContains('files', $response['data']['channels']); + $this->assertContains('files.' . $file['body']['$id'], $response['data']['channels']); + $this->assertEquals('storage.files.update', $response['data']['event']); + $this->assertNotEmpty($response['data']['payload']); + + /** + * Test File Delete + */ + $this->client->call(Client::METHOD_DELETE, '/storage/files/' . $data['fileId'], array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders())); + + $response = json_decode($client->receive(), true); + + $this->assertArrayHasKey('type', $response); + $this->assertArrayHasKey('data', $response); + $this->assertEquals('event', $response['type']); + $this->assertNotEmpty($response['data']); + $this->assertArrayHasKey('timestamp', $response['data']); + $this->assertCount(2, $response['data']['channels']); + $this->assertContains('files', $response['data']['channels']); + $this->assertContains('files.' . $file['body']['$id'], $response['data']['channels']); + $this->assertEquals('storage.files.delete', $response['data']['event']); + $this->assertNotEmpty($response['data']['payload']); + + $client->close(); + } + + public function testChannelExecutions() + { + $user = $this->getUser(); + $session = $user['session'] ?? ''; + $projectId = $this->getProject()['$id']; + + $client = $this->getWebsocket(['executions'], [ + 'origin' => 'http://localhost', + 'cookie' => 'a_session_'.$projectId.'=' . $session + ]); + + $response = json_decode($client->receive(), true); + + $this->assertArrayHasKey('type', $response); + $this->assertArrayHasKey('data', $response); + $this->assertEquals('connected', $response['type']); + $this->assertNotEmpty($response['data']); + $this->assertCount(1, $response['data']['channels']); + $this->assertContains('executions', $response['data']['channels']); + $this->assertNotEmpty($response['data']['user']); + $this->assertEquals($user['$id'], $response['data']['user']['$id']); + + /** + * Test Functions Create + */ + $function = $this->client->call(Client::METHOD_POST, '/functions', [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'] + ], [ + 'functionId' => 'unique()', + 'name' => 'Test', + 'execute' => ['role:member'], + 'runtime' => 'php-8.0', + 'timeout' => 10, + ]); + + $functionId = $function['body']['$id'] ?? ''; + + $this->assertEquals($function['headers']['status-code'], 201); + $this->assertNotEmpty($function['body']['$id']); + + $tag = $this->client->call(Client::METHOD_POST, '/functions/'.$functionId.'/tags', array_merge([ + 'content-type' => 'multipart/form-data', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'] + ]), [ + 'command' => 'php index.php', + 'code' => new CURLFile(realpath(__DIR__ . '/../../../resources/functions/timeout.tar.gz'), 'application/x-gzip', 'php-fx.tar.gz'), + ]); + + $tagId = $tag['body']['$id'] ?? ''; + + $this->assertEquals($tag['headers']['status-code'], 201); + $this->assertNotEmpty($tag['body']['$id']); + + $response = $this->client->call(Client::METHOD_PATCH, '/functions/'.$functionId.'/tag', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'] + ]), [ + 'tag' => $tagId, + ]); + + $this->assertEquals($response['headers']['status-code'], 200); + $this->assertNotEmpty($response['body']['$id']); + + $execution = $this->client->call(Client::METHOD_POST, '/functions/'.$functionId.'/executions', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'] + ], $this->getHeaders()), []); + + $this->assertEquals($execution['headers']['status-code'], 201); + $this->assertNotEmpty($execution['body']['$id']); + + $response = json_decode($client->receive(), true); + $responseUpdate = json_decode($client->receive(), true); + + $this->assertArrayHasKey('type', $response); + $this->assertArrayHasKey('data', $response); + $this->assertEquals('event', $response['type']); + $this->assertNotEmpty($response['data']); + $this->assertArrayHasKey('timestamp', $response['data']); + $this->assertCount(3, $response['data']['channels']); + $this->assertContains('executions', $response['data']['channels']); + $this->assertContains('executions.' . $execution['body']['$id'], $response['data']['channels']); + $this->assertContains('functions.' . $execution['body']['functionId'], $response['data']['channels']); + $this->assertEquals('functions.executions.create', $response['data']['event']); + $this->assertNotEmpty($response['data']['payload']); + + $this->assertArrayHasKey('type', $responseUpdate); + $this->assertArrayHasKey('data', $responseUpdate); + $this->assertEquals('event', $responseUpdate['type']); + $this->assertNotEmpty($responseUpdate['data']); + $this->assertArrayHasKey('timestamp', $responseUpdate['data']); + $this->assertCount(3, $responseUpdate['data']['channels']); + $this->assertContains('executions', $responseUpdate['data']['channels']); + $this->assertContains('executions.' . $execution['body']['$id'], $responseUpdate['data']['channels']); + $this->assertContains('functions.' . $execution['body']['functionId'], $responseUpdate['data']['channels']); + $this->assertEquals('functions.executions.update', $responseUpdate['data']['event']); + $this->assertNotEmpty($responseUpdate['data']['payload']); + + $client->close(); + } + + public function testChannelTeams(): array + { + $user = $this->getUser(); + $session = $user['session'] ?? ''; + $projectId = $this->getProject()['$id']; + + $client = $this->getWebsocket(['teams'], [ + 'origin' => 'http://localhost', + 'cookie' => 'a_session_'.$projectId.'=' . $session + ]); + + $response = json_decode($client->receive(), true); + + $this->assertArrayHasKey('type', $response); + $this->assertArrayHasKey('data', $response); + $this->assertEquals('connected', $response['type']); + $this->assertNotEmpty($response['data']); + $this->assertCount(1, $response['data']['channels']); + $this->assertContains('teams', $response['data']['channels']); + $this->assertNotEmpty($response['data']['user']); + $this->assertEquals($user['$id'], $response['data']['user']['$id']); + + /** + * Test Team Create + */ + $team = $this->client->call(Client::METHOD_POST, '/teams', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $projectId, + ], $this->getHeaders()), [ + 'teamId' => 'unique()', + 'name' => 'Arsenal' + ]); + + $teamId = $team['body']['$id'] ?? ''; + + $this->assertEquals(201, $team['headers']['status-code']); + $this->assertNotEmpty($team['body']['$id']); + + $response = json_decode($client->receive(), true); + + $this->assertArrayHasKey('type', $response); + $this->assertArrayHasKey('data', $response); + $this->assertEquals('event', $response['type']); + $this->assertNotEmpty($response['data']); + $this->assertArrayHasKey('timestamp', $response['data']); + $this->assertCount(2, $response['data']['channels']); + $this->assertContains('teams', $response['data']['channels']); + $this->assertContains('teams.' . $teamId, $response['data']['channels']); + $this->assertEquals('teams.create', $response['data']['event']); + $this->assertNotEmpty($response['data']['payload']); + + /** + * Test Team Update + */ + $team = $this->client->call(Client::METHOD_PUT, '/teams/'.$teamId, array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $projectId, + ], $this->getHeaders()), [ + 'name' => 'Manchester' + ]); + + $this->assertEquals($team['headers']['status-code'], 200); + $this->assertNotEmpty($team['body']['$id']); + + $response = json_decode($client->receive(), true); + + $this->assertArrayHasKey('type', $response); + $this->assertArrayHasKey('data', $response); + $this->assertEquals('event', $response['type']); + $this->assertNotEmpty($response['data']); + $this->assertArrayHasKey('timestamp', $response['data']); + $this->assertCount(2, $response['data']['channels']); + $this->assertContains('teams', $response['data']['channels']); + $this->assertContains('teams.' . $teamId, $response['data']['channels']); + $this->assertEquals('teams.update', $response['data']['event']); + $this->assertNotEmpty($response['data']['payload']); + + $client->close(); + + return ['teamId' => $teamId]; + } + + /** + * @depends testChannelTeams + */ + public function testChannelMemberships(array $data) + { + $teamId = $data['teamId'] ?? ''; + + $user = $this->getUser(); + $session = $user['session'] ?? ''; + $projectId = $this->getProject()['$id']; + + $client = $this->getWebsocket(['memberships'], [ + 'origin' => 'http://localhost', + 'cookie' => 'a_session_'.$projectId.'='.$session + ]); + + $response = json_decode($client->receive(), true); + + $this->assertArrayHasKey('type', $response); + $this->assertArrayHasKey('data', $response); + $this->assertEquals('connected', $response['type']); + $this->assertNotEmpty($response['data']); + $this->assertCount(1, $response['data']['channels']); + $this->assertContains('memberships', $response['data']['channels']); + $this->assertNotEmpty($response['data']['user']); + $this->assertEquals($user['$id'], $response['data']['user']['$id']); + + $response = $this->client->call(Client::METHOD_GET, '/teams/'.$teamId.'/memberships', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders())); + + $membershipId = $response['body']['memberships'][0]['$id']; + + /** + * Test Update Membership + */ + $roles = ['admin', 'editor', 'uncle']; + $this->client->call(Client::METHOD_PATCH, '/teams/'.$teamId.'/memberships/'.$membershipId, array_merge([ + 'origin' => 'http://localhost', + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), [ + 'roles' => $roles + ]); + + $response = json_decode($client->receive(), true); + + $this->assertArrayHasKey('type', $response); + $this->assertArrayHasKey('data', $response); + $this->assertEquals('event', $response['type']); + $this->assertNotEmpty($response['data']); + $this->assertArrayHasKey('timestamp', $response['data']); + $this->assertCount(2, $response['data']['channels']); + $this->assertContains('memberships', $response['data']['channels']); + $this->assertContains('memberships.' . $membershipId, $response['data']['channels']); + $this->assertEquals('teams.memberships.update', $response['data']['event']); + $this->assertNotEmpty($response['data']['payload']); + + $client->close(); + } } \ No newline at end of file From fbb5696f89d4e31a0fbc6a21209692f7dbbaf03e Mon Sep 17 00:00:00 2001 From: Torsten Dittmann Date: Mon, 13 Dec 2021 10:29:50 +0100 Subject: [PATCH 2/3] Update src/Appwrite/Messaging/Adapter/Realtime.php Co-authored-by: kodumbeats --- src/Appwrite/Messaging/Adapter/Realtime.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Appwrite/Messaging/Adapter/Realtime.php b/src/Appwrite/Messaging/Adapter/Realtime.php index 71738e42ba..8fb3ad4aa5 100644 --- a/src/Appwrite/Messaging/Adapter/Realtime.php +++ b/src/Appwrite/Messaging/Adapter/Realtime.php @@ -233,7 +233,7 @@ class Realtime extends Adapter } /** - * Creae channels array based on the event name and payload. + * Create channels array based on the event name and payload. * * @param string $event * @param Document $payload From 7e9e40d03e650852f527c045fcc1be3e7fe59113 Mon Sep 17 00:00:00 2001 From: Torsten Dittmann Date: Mon, 13 Dec 2021 16:42:40 +0100 Subject: [PATCH 3/3] chore(deps): update utopia dependencies --- composer.json | 2 +- composer.lock | 90 +++++++++---------- src/Appwrite/Auth/Validator/Password.php | 4 +- .../Database/Validator/Authorization.php | 4 +- .../Database/Validator/Collection.php | 2 +- src/Appwrite/Database/Validator/CustomId.php | 2 +- .../Database/Validator/DocumentId.php | 4 +- src/Appwrite/Database/Validator/Key.php | 4 +- .../Database/Validator/Permissions.php | 4 +- src/Appwrite/Database/Validator/Structure.php | 4 +- src/Appwrite/Database/Validator/UID.php | 4 +- src/Appwrite/Network/Validator/CNAME.php | 4 +- src/Appwrite/Network/Validator/Domain.php | 4 +- src/Appwrite/Network/Validator/Email.php | 4 +- src/Appwrite/Network/Validator/Host.php | 4 +- src/Appwrite/Network/Validator/IP.php | 4 +- src/Appwrite/Network/Validator/Origin.php | 4 +- src/Appwrite/Network/Validator/URL.php | 4 +- src/Appwrite/Template/Template.php | 2 +- 19 files changed, 77 insertions(+), 77 deletions(-) diff --git a/composer.json b/composer.json index f534615f20..d2953bf5b4 100644 --- a/composer.json +++ b/composer.json @@ -51,7 +51,7 @@ "utopia-php/registry": "0.5.*", "utopia-php/preloader": "0.2.*", "utopia-php/domains": "1.1.*", - "utopia-php/swoole": "0.2.*", + "utopia-php/swoole": "0.3.*", "utopia-php/storage": "0.5.*", "utopia-php/websocket": "0.0.*", "utopia-php/image": "0.5.*", diff --git a/composer.lock b/composer.lock index b541ac91de..33a29d90b7 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "7e24a95bc534ed39b042f19b27268de9", + "content-hash": "9dcf48d4173daea87c60b464b104cd22", "packages": [ { "name": "adhocore/jwt", @@ -2138,16 +2138,16 @@ }, { "name": "utopia-php/database", - "version": "0.12.0", + "version": "0.12.1", "source": { "type": "git", "url": "https://github.com/utopia-php/database.git", - "reference": "102ee1d21fd55fc92dc7a07b60672a98ae49be26" + "reference": "af512b7a00cc7c6e30fa03efbc5fd7e77a93e2df" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/database/zipball/102ee1d21fd55fc92dc7a07b60672a98ae49be26", - "reference": "102ee1d21fd55fc92dc7a07b60672a98ae49be26", + "url": "https://api.github.com/repos/utopia-php/database/zipball/af512b7a00cc7c6e30fa03efbc5fd7e77a93e2df", + "reference": "af512b7a00cc7c6e30fa03efbc5fd7e77a93e2df", "shasum": "" }, "require": { @@ -2155,7 +2155,7 @@ "ext-pdo": "*", "ext-redis": "*", "mongodb/mongodb": "1.8.0", - "php": ">=7.1", + "php": ">=8.0", "utopia-php/cache": "0.4.*", "utopia-php/framework": "0.*.*" }, @@ -2195,9 +2195,9 @@ ], "support": { "issues": "https://github.com/utopia-php/database/issues", - "source": "https://github.com/utopia-php/database/tree/0.12.0" + "source": "https://github.com/utopia-php/database/tree/0.12.1" }, - "time": "2021-11-24T14:53:22+00:00" + "time": "2021-12-13T14:57:32+00:00" }, { "name": "utopia-php/domains", @@ -2255,24 +2255,24 @@ }, { "name": "utopia-php/framework", - "version": "0.19.1", + "version": "0.19.2", "source": { "type": "git", "url": "https://github.com/utopia-php/framework.git", - "reference": "cc7629b5f7a8f45912ec2e069b7f14e361e41c34" + "reference": "49e4374b97c0f4d14bc84b424bdc9c3b7747e15f" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/framework/zipball/cc7629b5f7a8f45912ec2e069b7f14e361e41c34", - "reference": "cc7629b5f7a8f45912ec2e069b7f14e361e41c34", + "url": "https://api.github.com/repos/utopia-php/framework/zipball/49e4374b97c0f4d14bc84b424bdc9c3b7747e15f", + "reference": "49e4374b97c0f4d14bc84b424bdc9c3b7747e15f", "shasum": "" }, "require": { - "php": ">=7.3.0" + "php": ">=8.0.0" }, "require-dev": { - "phpunit/phpunit": "^9.4", - "vimeo/psalm": "4.0.1" + "phpunit/phpunit": "^9.5.10", + "vimeo/psalm": "4.13.1" }, "type": "library", "autoload": { @@ -2298,9 +2298,9 @@ ], "support": { "issues": "https://github.com/utopia-php/framework/issues", - "source": "https://github.com/utopia-php/framework/tree/0.19.1" + "source": "https://github.com/utopia-php/framework/tree/0.19.2" }, - "time": "2021-11-25T16:11:40+00:00" + "time": "2021-12-07T09:29:35+00:00" }, { "name": "utopia-php/image", @@ -2567,16 +2567,16 @@ }, { "name": "utopia-php/storage", - "version": "0.5.0", + "version": "0.5.1", "source": { "type": "git", "url": "https://github.com/utopia-php/storage.git", - "reference": "92ae20c7a2ac329f573a58a82dc245134cc63408" + "reference": "e672aa3fc2a8ba689aff65f68ff29f1d608223b8" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/storage/zipball/92ae20c7a2ac329f573a58a82dc245134cc63408", - "reference": "92ae20c7a2ac329f573a58a82dc245134cc63408", + "url": "https://api.github.com/repos/utopia-php/storage/zipball/e672aa3fc2a8ba689aff65f68ff29f1d608223b8", + "reference": "e672aa3fc2a8ba689aff65f68ff29f1d608223b8", "shasum": "" }, "require": { @@ -2613,33 +2613,33 @@ ], "support": { "issues": "https://github.com/utopia-php/storage/issues", - "source": "https://github.com/utopia-php/storage/tree/0.5.0" + "source": "https://github.com/utopia-php/storage/tree/0.5.1" }, - "time": "2021-04-15T16:43:12+00:00" + "time": "2021-12-13T15:17:14+00:00" }, { "name": "utopia-php/swoole", - "version": "0.2.4", + "version": "0.3.2", "source": { "type": "git", "url": "https://github.com/utopia-php/swoole.git", - "reference": "37d8c64b536d6bc7da4f0f5a934a0ec44885abf4" + "reference": "2b714eddf77cd5eda1889219c9656d7c0a63ce73" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/swoole/zipball/37d8c64b536d6bc7da4f0f5a934a0ec44885abf4", - "reference": "37d8c64b536d6bc7da4f0f5a934a0ec44885abf4", + "url": "https://api.github.com/repos/utopia-php/swoole/zipball/2b714eddf77cd5eda1889219c9656d7c0a63ce73", + "reference": "2b714eddf77cd5eda1889219c9656d7c0a63ce73", "shasum": "" }, "require": { "ext-swoole": "*", - "php": ">=7.4", + "php": ">=8.0", "utopia-php/framework": "0.*.*" }, "require-dev": { "phpunit/phpunit": "^9.3", - "swoole/ide-helper": "4.5.5", - "vimeo/psalm": "4.0.1" + "swoole/ide-helper": "4.8.3", + "vimeo/psalm": "4.15.0" }, "type": "library", "autoload": { @@ -2669,9 +2669,9 @@ ], "support": { "issues": "https://github.com/utopia-php/swoole/issues", - "source": "https://github.com/utopia-php/swoole/tree/0.2.4" + "source": "https://github.com/utopia-php/swoole/tree/0.3.2" }, - "time": "2021-06-22T10:49:24+00:00" + "time": "2021-12-13T15:37:41+00:00" }, { "name": "utopia-php/system", @@ -5665,23 +5665,23 @@ }, { "name": "symfony/console", - "version": "v5.4.0", + "version": "v5.4.1", "source": { "type": "git", "url": "https://github.com/symfony/console.git", - "reference": "ec3661faca1d110d6c307e124b44f99ac54179e3" + "reference": "9130e1a0fc93cb0faadca4ee917171bd2ca9e5f4" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/console/zipball/ec3661faca1d110d6c307e124b44f99ac54179e3", - "reference": "ec3661faca1d110d6c307e124b44f99ac54179e3", + "url": "https://api.github.com/repos/symfony/console/zipball/9130e1a0fc93cb0faadca4ee917171bd2ca9e5f4", + "reference": "9130e1a0fc93cb0faadca4ee917171bd2ca9e5f4", "shasum": "" }, "require": { "php": ">=7.2.5", "symfony/deprecation-contracts": "^2.1|^3", "symfony/polyfill-mbstring": "~1.0", - "symfony/polyfill-php73": "^1.8", + "symfony/polyfill-php73": "^1.9", "symfony/polyfill-php80": "^1.16", "symfony/service-contracts": "^1.1|^2|^3", "symfony/string": "^5.1|^6.0" @@ -5744,7 +5744,7 @@ "terminal" ], "support": { - "source": "https://github.com/symfony/console/tree/v5.4.0" + "source": "https://github.com/symfony/console/tree/v5.4.1" }, "funding": [ { @@ -5760,7 +5760,7 @@ "type": "tidelift" } ], - "time": "2021-11-29T15:30:56+00:00" + "time": "2021-12-09T11:22:43+00:00" }, { "name": "symfony/polyfill-intl-grapheme", @@ -6170,16 +6170,16 @@ }, { "name": "symfony/string", - "version": "v6.0.0", + "version": "v6.0.1", "source": { "type": "git", "url": "https://github.com/symfony/string.git", - "reference": "ba727797426af0f587f4800566300bdc0cda0777" + "reference": "0cfed595758ec6e0a25591bdc8ca733c1896af32" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/string/zipball/ba727797426af0f587f4800566300bdc0cda0777", - "reference": "ba727797426af0f587f4800566300bdc0cda0777", + "url": "https://api.github.com/repos/symfony/string/zipball/0cfed595758ec6e0a25591bdc8ca733c1896af32", + "reference": "0cfed595758ec6e0a25591bdc8ca733c1896af32", "shasum": "" }, "require": { @@ -6235,7 +6235,7 @@ "utf8" ], "support": { - "source": "https://github.com/symfony/string/tree/v6.0.0" + "source": "https://github.com/symfony/string/tree/v6.0.1" }, "funding": [ { @@ -6251,7 +6251,7 @@ "type": "tidelift" } ], - "time": "2021-10-29T07:35:21+00:00" + "time": "2021-12-08T15:13:44+00:00" }, { "name": "textalk/websocket", diff --git a/src/Appwrite/Auth/Validator/Password.php b/src/Appwrite/Auth/Validator/Password.php index d7168774bd..3e9bb6fbfc 100644 --- a/src/Appwrite/Auth/Validator/Password.php +++ b/src/Appwrite/Auth/Validator/Password.php @@ -18,7 +18,7 @@ class Password extends Validator * * @return string */ - public function getDescription() + public function getDescription(): string { return 'Password must be at least 8 characters'; } @@ -30,7 +30,7 @@ class Password extends Validator * * @return bool */ - public function isValid($value) + public function isValid($value): bool { if (!\is_string($value)) { return false; diff --git a/src/Appwrite/Database/Validator/Authorization.php b/src/Appwrite/Database/Validator/Authorization.php index 4a7f82b392..914c3e40db 100644 --- a/src/Appwrite/Database/Validator/Authorization.php +++ b/src/Appwrite/Database/Validator/Authorization.php @@ -46,7 +46,7 @@ class Authorization extends Validator * * @return string */ - public function getDescription() + public function getDescription(): string { return $this->message; } @@ -60,7 +60,7 @@ class Authorization extends Validator * * @return bool */ - public function isValid($permissions) + public function isValid($permissions): bool { if (!self::$status) { return true; diff --git a/src/Appwrite/Database/Validator/Collection.php b/src/Appwrite/Database/Validator/Collection.php index 45faa2801d..139ff20cd0 100644 --- a/src/Appwrite/Database/Validator/Collection.php +++ b/src/Appwrite/Database/Validator/Collection.php @@ -39,7 +39,7 @@ class Collection extends Structure * * @return bool */ - public function isValid($document) + public function isValid($document): bool { $document = new Document( \array_merge($this->merge, ($document instanceof Document) ? $document->getArrayCopy() : $document) diff --git a/src/Appwrite/Database/Validator/CustomId.php b/src/Appwrite/Database/Validator/CustomId.php index 59c14de8be..39a8500f81 100644 --- a/src/Appwrite/Database/Validator/CustomId.php +++ b/src/Appwrite/Database/Validator/CustomId.php @@ -13,7 +13,7 @@ class CustomId extends Key { * * @return bool */ - public function isValid($value) + public function isValid($value): bool { return $value == 'unique()' || parent::isValid($value); diff --git a/src/Appwrite/Database/Validator/DocumentId.php b/src/Appwrite/Database/Validator/DocumentId.php index fcc2a57aef..988497cfe6 100644 --- a/src/Appwrite/Database/Validator/DocumentId.php +++ b/src/Appwrite/Database/Validator/DocumentId.php @@ -42,7 +42,7 @@ class DocumentId extends Validator * * @return string */ - public function getDescription() + public function getDescription(): string { return $this->message; } @@ -56,7 +56,7 @@ class DocumentId extends Validator * * @return bool */ - public function isValid($id) + public function isValid($id): bool { $document = $this->database->getDocument($id); diff --git a/src/Appwrite/Database/Validator/Key.php b/src/Appwrite/Database/Validator/Key.php index 1aab06f76d..328f06fec6 100644 --- a/src/Appwrite/Database/Validator/Key.php +++ b/src/Appwrite/Database/Validator/Key.php @@ -18,7 +18,7 @@ class Key extends Validator * * @return string */ - public function getDescription() + public function getDescription(): string { return $this->message; } @@ -32,7 +32,7 @@ class Key extends Validator * * @return bool */ - public function isValid($value) + public function isValid($value): bool { if (!\is_string($value)) { return false; diff --git a/src/Appwrite/Database/Validator/Permissions.php b/src/Appwrite/Database/Validator/Permissions.php index 2c90eef60d..1419c3e7c7 100644 --- a/src/Appwrite/Database/Validator/Permissions.php +++ b/src/Appwrite/Database/Validator/Permissions.php @@ -34,7 +34,7 @@ class Permissions extends Validator * * @return string */ - public function getDescription() + public function getDescription(): string { return $this->message; } @@ -48,7 +48,7 @@ class Permissions extends Validator * * @return bool */ - public function isValid($value) + public function isValid($value): bool { if (!\is_array($value) && !empty($value)) { $this->message = 'Invalid permissions data structure'; diff --git a/src/Appwrite/Database/Validator/Structure.php b/src/Appwrite/Database/Validator/Structure.php index 7f33074766..6cd143a8f7 100644 --- a/src/Appwrite/Database/Validator/Structure.php +++ b/src/Appwrite/Database/Validator/Structure.php @@ -109,7 +109,7 @@ class Structure extends Validator * * @return string */ - public function getDescription() + public function getDescription(): string { return 'Invalid document structure: '.$this->message; } @@ -123,7 +123,7 @@ class Structure extends Validator * * @return bool */ - public function isValid($document) + public function isValid($document): bool { $document = (\is_array($document)) ? new Document($document) : $document; diff --git a/src/Appwrite/Database/Validator/UID.php b/src/Appwrite/Database/Validator/UID.php index 8e60ec0632..ebab6b4628 100644 --- a/src/Appwrite/Database/Validator/UID.php +++ b/src/Appwrite/Database/Validator/UID.php @@ -13,7 +13,7 @@ class UID extends Validator * * @return string */ - public function getDescription() + public function getDescription(): string { return 'Invalid UID format'; } @@ -27,7 +27,7 @@ class UID extends Validator * * @return bool */ - public function isValid($value) + public function isValid($value): bool { if ($value === 0) { // TODO Deprecate confition when we get the chance. return true; diff --git a/src/Appwrite/Network/Validator/CNAME.php b/src/Appwrite/Network/Validator/CNAME.php index 12b88908f7..93a302b962 100644 --- a/src/Appwrite/Network/Validator/CNAME.php +++ b/src/Appwrite/Network/Validator/CNAME.php @@ -22,7 +22,7 @@ class CNAME extends Validator /** * @return string */ - public function getDescription() + public function getDescription(): string { return 'Invalid CNAME record'; } @@ -34,7 +34,7 @@ class CNAME extends Validator * * @return bool */ - public function isValid($domain) + public function isValid($domain): bool { if (!is_string($domain)) { return false; diff --git a/src/Appwrite/Network/Validator/Domain.php b/src/Appwrite/Network/Validator/Domain.php index 8a70ec8b03..9db6641316 100644 --- a/src/Appwrite/Network/Validator/Domain.php +++ b/src/Appwrite/Network/Validator/Domain.php @@ -20,7 +20,7 @@ class Domain extends Validator * * @return string */ - public function getDescription() + public function getDescription(): string { return 'Value must be a valid domain'; } @@ -35,7 +35,7 @@ class Domain extends Validator * @param mixed $value * @return bool */ - public function isValid($value) + public function isValid($value): bool { if (empty($value)) { return false; diff --git a/src/Appwrite/Network/Validator/Email.php b/src/Appwrite/Network/Validator/Email.php index ab3a324ea1..efc1a5d5b3 100644 --- a/src/Appwrite/Network/Validator/Email.php +++ b/src/Appwrite/Network/Validator/Email.php @@ -20,7 +20,7 @@ class Email extends Validator * * @return string */ - public function getDescription() + public function getDescription(): string { return 'Value must be a valid email address'; } @@ -33,7 +33,7 @@ class Email extends Validator * @param mixed $value * @return bool */ - public function isValid($value) + public function isValid($value): bool { if (!\filter_var($value, FILTER_VALIDATE_EMAIL)) { return false; diff --git a/src/Appwrite/Network/Validator/Host.php b/src/Appwrite/Network/Validator/Host.php index b0f8b23073..703907c3d3 100644 --- a/src/Appwrite/Network/Validator/Host.php +++ b/src/Appwrite/Network/Validator/Host.php @@ -30,7 +30,7 @@ class Host extends Validator * * @return string */ - public function getDescription() + public function getDescription(): string { return 'URL host must be one of: ' . \implode(', ', $this->whitelist); } @@ -43,7 +43,7 @@ class Host extends Validator * @param mixed $value * @return bool */ - public function isValid($value) + public function isValid($value): bool { $urlValidator = new URL(); diff --git a/src/Appwrite/Network/Validator/IP.php b/src/Appwrite/Network/Validator/IP.php index 5d7833da6f..c9b50fe3ca 100644 --- a/src/Appwrite/Network/Validator/IP.php +++ b/src/Appwrite/Network/Validator/IP.php @@ -46,7 +46,7 @@ class IP extends Validator * * @return string */ - public function getDescription() + public function getDescription(): string { return 'Value must be a valid IP address'; } @@ -59,7 +59,7 @@ class IP extends Validator * @param mixed $value * @return bool */ - public function isValid($value) + public function isValid($value): bool { switch ($this->type) { case self::ALL: diff --git a/src/Appwrite/Network/Validator/Origin.php b/src/Appwrite/Network/Validator/Origin.php index 9677bc14eb..385f9e2dc0 100644 --- a/src/Appwrite/Network/Validator/Origin.php +++ b/src/Appwrite/Network/Validator/Origin.php @@ -84,7 +84,7 @@ class Origin extends Validator } } - public function getDescription() + public function getDescription(): string { if (!\array_key_exists($this->client, $this->platforms)) { return 'Unsupported platform'; @@ -102,7 +102,7 @@ class Origin extends Validator * * @return bool */ - public function isValid($origin) + public function isValid($origin): bool { if (!is_string($origin)) { return false; diff --git a/src/Appwrite/Network/Validator/URL.php b/src/Appwrite/Network/Validator/URL.php index d26159381c..61d1941e02 100644 --- a/src/Appwrite/Network/Validator/URL.php +++ b/src/Appwrite/Network/Validator/URL.php @@ -20,7 +20,7 @@ class URL extends Validator * * @return string */ - public function getDescription() + public function getDescription(): string { return 'Value must be a valid URL'; } @@ -33,7 +33,7 @@ class URL extends Validator * @param mixed $value * @return bool */ - public function isValid($value) + public function isValid($value): bool { if (\filter_var($value, FILTER_VALIDATE_URL) === false) { return false; diff --git a/src/Appwrite/Template/Template.php b/src/Appwrite/Template/Template.php index 7c175de770..f2343d4412 100644 --- a/src/Appwrite/Template/Template.php +++ b/src/Appwrite/Template/Template.php @@ -63,7 +63,7 @@ class Template extends View * * @throws Exception */ - public function render($minify = true) + public function render($minify = true): string { if ($this->rendered) { // Don't render any template return '';