2021-12-07 01:03:12 +13:00
< ? php
namespace Tests\E2E\Services\Realtime ;
2022-10-26 08:16:05 +13:00
use CURLFile ;
2021-12-07 01:03:12 +13:00
use Tests\E2E\Client ;
use Tests\E2E\Scopes\Scope ;
use Tests\E2E\Scopes\ProjectCustom ;
use Tests\E2E\Scopes\SideConsole ;
2022-10-26 08:16:05 +13:00
use Tests\E2E\Services\Functions\FunctionsBase ;
2023-02-06 09:07:46 +13:00
use Utopia\Database\Helpers\ID ;
use Utopia\Database\Helpers\Permission ;
use Utopia\Database\Helpers\Role ;
2021-12-07 01:03:12 +13:00
class RealtimeConsoleClientTest extends Scope
{
2022-10-26 08:16:05 +13:00
use FunctionsBase ;
2021-12-07 01:03:12 +13:00
use RealtimeBase ;
use ProjectCustom ;
use SideConsole ;
2022-07-06 22:53:30 +12:00
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 ();
}
2021-12-07 01:03:12 +13:00
public function testAttributes ()
{
$user = $this -> getUser ();
$projectId = 'console' ;
$client = $this -> getWebsocket ([ 'console' ], [
'origin' => 'http://localhost' ,
2022-04-19 04:21:45 +12:00
'cookie' => 'a_session_console=' . $this -> getRoot ()[ 'session' ],
2021-12-07 01:03:12 +13:00
], $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' ]);
2022-06-22 22:51:49 +12:00
/**
* Create database
*/
$database = $this -> client -> call ( Client :: METHOD_POST , '/databases' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
2022-08-14 22:33:36 +12:00
'databaseId' => ID :: unique (),
2022-06-22 22:51:49 +12:00
'name' => 'Actors DB' ,
]);
$databaseId = $database [ 'body' ][ '$id' ];
2021-12-07 01:03:12 +13:00
/**
* Test Attributes
*/
2022-06-22 22:51:49 +12:00
$actors = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections' , array_merge ([
2021-12-07 01:03:12 +13:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
2022-08-14 22:33:36 +12:00
'collectionId' => ID :: unique (),
2021-12-07 01:03:12 +13:00
'name' => 'Actors' ,
2022-08-03 16:17:49 +12:00
'permissions' => [
2022-08-14 17:21:11 +12:00
Permission :: read ( Role :: any ()),
Permission :: create ( Role :: any ()),
Permission :: update ( Role :: any ()),
Permission :: delete ( Role :: any ()),
2022-08-03 16:17:49 +12:00
],
2021-12-07 01:03:12 +13:00
]);
2022-04-19 04:21:45 +12:00
$actorsId = $actors [ 'body' ][ '$id' ];
2021-12-07 01:03:12 +13:00
2022-06-22 22:51:49 +12:00
$name = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $actorsId . '/attributes/string' , array_merge ([
2021-12-07 01:03:12 +13:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
2021-12-17 05:10:01 +13:00
'key' => 'name' ,
2021-12-07 01:03:12 +13:00
'size' => 256 ,
'required' => true ,
]);
2022-04-19 04:21:45 +12:00
$attributeKey = $name [ 'body' ][ 'key' ];
2022-07-19 01:22:23 +12:00
$this -> assertEquals ( $name [ 'headers' ][ 'status-code' ], 202 );
2021-12-07 01:03:12 +13:00
$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' ]);
2022-06-22 22:51:49 +12:00
$this -> assertContains ( " databases. { $databaseId } .collections. { $actorsId } .attributes.*.create " , $response [ 'data' ][ 'events' ]);
$this -> assertContains ( " databases. { $databaseId } .collections. { $actorsId } .attributes.* " , $response [ 'data' ][ 'events' ]);
$this -> assertContains ( " databases. { $databaseId } .collections. { $actorsId } " , $response [ 'data' ][ 'events' ]);
$this -> assertContains ( " databases. { $databaseId } .collections.*.attributes.*.create " , $response [ 'data' ][ 'events' ]);
$this -> assertContains ( " databases. { $databaseId } .collections.*.attributes.* " , $response [ 'data' ][ 'events' ]);
$this -> assertContains ( " databases. { $databaseId } .collections.* " , $response [ 'data' ][ 'events' ]);
$this -> assertContains ( " databases. { $databaseId } " , $response [ 'data' ][ 'events' ]);
$this -> assertContains ( " databases.* " , $response [ 'data' ][ 'events' ]);
2021-12-07 01:03:12 +13:00
$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' ]);
2022-06-22 22:51:49 +12:00
$this -> assertContains ( " databases. { $databaseId } .collections. { $actorsId } .attributes.*.update " , $response [ 'data' ][ 'events' ]);
$this -> assertContains ( " databases. { $databaseId } .collections. { $actorsId } .attributes.* " , $response [ 'data' ][ 'events' ]);
$this -> assertContains ( " databases. { $databaseId } .collections. { $actorsId } " , $response [ 'data' ][ 'events' ]);
$this -> assertContains ( " databases. { $databaseId } .collections.*.attributes.*.update " , $response [ 'data' ][ 'events' ]);
$this -> assertContains ( " databases. { $databaseId } .collections.*.attributes.* " , $response [ 'data' ][ 'events' ]);
$this -> assertContains ( " databases. { $databaseId } .collections.* " , $response [ 'data' ][ 'events' ]);
$this -> assertContains ( " databases. { $databaseId } " , $response [ 'data' ][ 'events' ]);
$this -> assertContains ( " databases.* " , $response [ 'data' ][ 'events' ]);
2021-12-07 01:03:12 +13:00
$this -> assertNotEmpty ( $response [ 'data' ][ 'payload' ]);
$this -> assertEquals ( 'available' , $response [ 'data' ][ 'payload' ][ 'status' ]);
$client -> close ();
2022-06-22 22:51:49 +12:00
$data = [ 'actorsId' => $actorsId , 'databaseId' => $databaseId ];
2022-04-19 04:21:45 +12:00
2021-12-07 01:03:12 +13:00
return $data ;
}
/**
* @ depends testAttributes
*/
public function testIndexes ( array $data )
{
$projectId = 'console' ;
2022-04-19 04:21:45 +12:00
$actorsId = $data [ 'actorsId' ];
2022-06-22 22:51:49 +12:00
$databaseId = $data [ 'databaseId' ];
2021-12-07 01:03:12 +13:00
$client = $this -> getWebsocket ([ 'console' ], [
'origin' => 'http://localhost' ,
2022-04-19 04:21:45 +12:00
'cookie' => 'a_session_console=' . $this -> getRoot ()[ 'session' ],
2021-12-07 01:03:12 +13:00
], $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
*/
2022-06-22 22:51:49 +12:00
$index = $this -> client -> call ( Client :: METHOD_POST , '/databases/' . $databaseId . '/collections/' . $actorsId . '/indexes' , array_merge ([
2021-12-07 01:03:12 +13:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
2021-12-17 05:10:01 +13:00
'key' => 'key_name' ,
2021-12-07 01:03:12 +13:00
'type' => 'key' ,
'attributes' => [
'name' ,
],
]);
2022-07-21 00:43:18 +12:00
$this -> assertEquals ( $index [ 'headers' ][ 'status-code' ], 202 );
2022-04-19 04:21:45 +12:00
$indexKey = $index [ 'body' ][ 'key' ];
2021-12-07 01:03:12 +13:00
$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' ]);
2022-06-22 22:51:49 +12:00
$this -> assertContains ( " databases. { $databaseId } .collections. { $actorsId } .indexes.*.create " , $response [ 'data' ][ 'events' ]);
$this -> assertContains ( " databases. { $databaseId } .collections. { $actorsId } .indexes.* " , $response [ 'data' ][ 'events' ]);
$this -> assertContains ( " databases. { $databaseId } .collections. { $actorsId } " , $response [ 'data' ][ 'events' ]);
$this -> assertContains ( " databases. { $databaseId } .collections.*.indexes.*.create " , $response [ 'data' ][ 'events' ]);
$this -> assertContains ( " databases. { $databaseId } .collections.*.indexes.* " , $response [ 'data' ][ 'events' ]);
$this -> assertContains ( " databases. { $databaseId } .collections.* " , $response [ 'data' ][ 'events' ]);
2021-12-07 01:03:12 +13:00
$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' ]);
2022-06-22 22:51:49 +12:00
$this -> assertContains ( " databases. { $databaseId } .collections. { $actorsId } .indexes.*.update " , $response [ 'data' ][ 'events' ]);
$this -> assertContains ( " databases. { $databaseId } .collections. { $actorsId } .indexes.* " , $response [ 'data' ][ 'events' ]);
$this -> assertContains ( " databases. { $databaseId } .collections. { $actorsId } " , $response [ 'data' ][ 'events' ]);
$this -> assertContains ( " databases. { $databaseId } .collections.*.indexes.*.update " , $response [ 'data' ][ 'events' ]);
$this -> assertContains ( " databases. { $databaseId } .collections.*.indexes.* " , $response [ 'data' ][ 'events' ]);
$this -> assertContains ( " databases. { $databaseId } .collections.* " , $response [ 'data' ][ 'events' ]);
2021-12-07 01:03:12 +13:00
$this -> assertNotEmpty ( $response [ 'data' ][ 'payload' ]);
$this -> assertEquals ( 'available' , $response [ 'data' ][ 'payload' ][ 'status' ]);
$client -> close ();
return $data ;
}
/**
* @ depends testIndexes
*/
public function testDeleteIndex ( array $data )
{
2022-04-19 04:21:45 +12:00
$actorsId = $data [ 'actorsId' ];
2021-12-07 01:03:12 +13:00
$projectId = 'console' ;
2022-06-22 22:51:49 +12:00
$databaseId = $data [ 'databaseId' ];
2021-12-07 01:03:12 +13:00
$client = $this -> getWebsocket ([ 'console' ], [
'origin' => 'http://localhost' ,
2022-04-19 04:21:45 +12:00
'cookie' => 'a_session_console=' . $this -> getRoot ()[ 'session' ],
2021-12-07 01:03:12 +13:00
], $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
*/
2023-10-19 04:42:23 +13:00
$indexKey = 'key_name' ;
2023-10-19 04:50:40 +13:00
$attribute = $this -> client -> call ( Client :: METHOD_DELETE , '/databases/' . $databaseId . '/collections/' . $actorsId . '/indexes/' . $indexKey , array_merge ([
2021-12-07 01:03:12 +13:00
'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' ]);
2023-10-19 05:30:47 +13:00
$this -> assertContains ( " databases. { $databaseId } .collections. { $actorsId } .indexes.*.update " , $response [ 'data' ][ 'events' ]);
2022-06-22 22:51:49 +12:00
$this -> assertContains ( " databases. { $databaseId } .collections. { $actorsId } .indexes.* " , $response [ 'data' ][ 'events' ]);
$this -> assertContains ( " databases. { $databaseId } .collections. { $actorsId } " , $response [ 'data' ][ 'events' ]);
2023-10-19 05:30:47 +13:00
$this -> assertContains ( " databases. { $databaseId } .collections.*.indexes.*.update " , $response [ 'data' ][ 'events' ]);
2022-06-22 22:51:49 +12:00
$this -> assertContains ( " databases. { $databaseId } .collections.*.indexes.* " , $response [ 'data' ][ 'events' ]);
$this -> assertContains ( " databases. { $databaseId } .collections.* " , $response [ 'data' ][ 'events' ]);
2021-12-07 01:03:12 +13:00
$this -> assertNotEmpty ( $response [ 'data' ][ 'payload' ]);
2023-10-19 04:50:40 +13:00
/** Delete index generates two events. One from the API and one from the database worker */
2023-10-19 04:42:23 +13:00
$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 -> assertContains ( " databases. { $databaseId } .collections. { $actorsId } .indexes.*.delete " , $response [ 'data' ][ 'events' ]);
$this -> assertContains ( " databases. { $databaseId } .collections. { $actorsId } .indexes.* " , $response [ 'data' ][ 'events' ]);
$this -> assertContains ( " databases. { $databaseId } .collections. { $actorsId } " , $response [ 'data' ][ 'events' ]);
$this -> assertContains ( " databases. { $databaseId } .collections.*.indexes.*.delete " , $response [ 'data' ][ 'events' ]);
$this -> assertContains ( " databases. { $databaseId } .collections.*.indexes.* " , $response [ 'data' ][ 'events' ]);
$this -> assertContains ( " databases. { $databaseId } .collections.* " , $response [ 'data' ][ 'events' ]);
$this -> assertNotEmpty ( $response [ 'data' ][ 'payload' ]);
2021-12-07 01:03:12 +13:00
$client -> close ();
return $data ;
}
/**
* @ depends testDeleteIndex
*/
public function testDeleteAttribute ( array $data )
{
2022-04-19 04:21:45 +12:00
$actorsId = $data [ 'actorsId' ];
2021-12-07 01:03:12 +13:00
$projectId = 'console' ;
2022-06-22 22:51:49 +12:00
$databaseId = $data [ 'databaseId' ];
2021-12-07 01:03:12 +13:00
$client = $this -> getWebsocket ([ 'console' ], [
'origin' => 'http://localhost' ,
2022-04-19 04:21:45 +12:00
'cookie' => 'a_session_console=' . $this -> getRoot ()[ 'session' ],
2021-12-07 01:03:12 +13:00
], $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
*/
2023-10-19 04:42:23 +13:00
$attributeKey = 'name' ;
$attribute = $this -> client -> call ( Client :: METHOD_DELETE , '/databases/' . $databaseId . '/collections/' . $data [ 'actorsId' ] . '/attributes/' . $attributeKey , array_merge ([
2021-12-07 01:03:12 +13:00
'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' ]);
2023-10-19 05:30:47 +13:00
$this -> assertNotEmpty ( $response [ 'data' ]);
$this -> assertArrayHasKey ( 'timestamp' , $response [ 'data' ]);
$this -> assertCount ( 1 , $response [ 'data' ][ 'channels' ]);
$this -> assertContains ( 'console' , $response [ 'data' ][ 'channels' ]);
$this -> assertContains ( " databases. { $databaseId } .collections. { $actorsId } .attributes.*.update " , $response [ 'data' ][ 'events' ]);
$this -> assertContains ( " databases. { $databaseId } .collections. { $actorsId } .attributes.* " , $response [ 'data' ][ 'events' ]);
$this -> assertContains ( " databases. { $databaseId } .collections. { $actorsId } " , $response [ 'data' ][ 'events' ]);
$this -> assertContains ( " databases. { $databaseId } .collections.*.attributes.*.update " , $response [ 'data' ][ 'events' ]);
$this -> assertContains ( " databases. { $databaseId } .collections.*.attributes.* " , $response [ 'data' ][ 'events' ]);
$this -> assertContains ( " databases. { $databaseId } .collections.* " , $response [ 'data' ][ 'events' ]);
$this -> assertNotEmpty ( $response [ 'data' ][ 'payload' ]);
$response = json_decode ( $client -> receive (), true );
$this -> assertArrayHasKey ( 'type' , $response );
$this -> assertArrayHasKey ( 'data' , $response );
$this -> assertEquals ( 'event' , $response [ 'type' ]);
2021-12-07 01:03:12 +13:00
$this -> assertNotEmpty ( $response [ 'data' ]);
$this -> assertArrayHasKey ( 'timestamp' , $response [ 'data' ]);
$this -> assertCount ( 1 , $response [ 'data' ][ 'channels' ]);
$this -> assertContains ( 'console' , $response [ 'data' ][ 'channels' ]);
2022-06-22 22:51:49 +12:00
$this -> assertContains ( " databases. { $databaseId } .collections. { $actorsId } .attributes.*.delete " , $response [ 'data' ][ 'events' ]);
$this -> assertContains ( " databases. { $databaseId } .collections. { $actorsId } .attributes.* " , $response [ 'data' ][ 'events' ]);
$this -> assertContains ( " databases. { $databaseId } .collections. { $actorsId } " , $response [ 'data' ][ 'events' ]);
$this -> assertContains ( " databases. { $databaseId } .collections.*.attributes.*.delete " , $response [ 'data' ][ 'events' ]);
$this -> assertContains ( " databases. { $databaseId } .collections.*.attributes.* " , $response [ 'data' ][ 'events' ]);
$this -> assertContains ( " databases. { $databaseId } .collections.* " , $response [ 'data' ][ 'events' ]);
2021-12-07 01:03:12 +13:00
$this -> assertNotEmpty ( $response [ 'data' ][ 'payload' ]);
$client -> close ();
}
2022-10-26 08:16:05 +13:00
public function testCreateDeployment ()
{
$response1 = $this -> client -> call ( Client :: METHOD_POST , '/functions' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
'functionId' => ID :: unique (),
'name' => 'Test' ,
'runtime' => 'php-8.0' ,
2023-08-12 01:34:57 +12:00
'entrypoint' => 'index.php' ,
2022-10-26 08:16:05 +13:00
'events' => [
'users.*.create' ,
'users.*.delete' ,
],
'schedule' => '0 0 1 1 *' ,
2023-03-02 01:00:36 +13:00
'timeout' => 10
2022-10-26 08:16:05 +13:00
]);
$functionId = $response1 [ 'body' ][ '$id' ] ? ? '' ;
$this -> assertEquals ( 201 , $response1 [ 'headers' ][ 'status-code' ]);
$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 Create Deployment
*/
$folder = 'php' ;
$code = realpath ( __DIR__ . '/../../../resources/functions' ) . " / $folder /code.tar.gz " ;
$this -> packageCode ( $folder );
$deployment = $this -> client -> call ( Client :: METHOD_POST , '/functions/' . $functionId . '/deployments' , array_merge ([
'content-type' => 'multipart/form-data' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
'entrypoint' => 'index.php' ,
'code' => new CURLFile ( $code , 'application/x-gzip' , \basename ( $code )),
2023-03-02 01:00:36 +13:00
'activate' => true
2022-10-26 08:16:05 +13:00
]);
$deploymentId = $deployment [ 'body' ][ '$id' ] ? ? '' ;
$this -> assertEquals ( 202 , $deployment [ 'headers' ][ 'status-code' ]);
$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' ]);
2023-06-16 01:23:15 +12:00
// $this->assertContains("functions.{$functionId}.deployments.{$deploymentId}.create", $response['data']['events']); TODO @christyjacob4 : enable test once we allow functions.* events
2022-10-26 08:16:05 +13:00
$this -> assertNotEmpty ( $response [ 'data' ][ 'payload' ]);
$client -> close ();
}
2022-04-19 04:21:45 +12:00
}