2019-10-15 07:37:59 +13:00
< ? php
global $utopia , $request , $response ;
2022-02-17 04:28:37 +13:00
use Appwrite\Extend\Exception ;
2021-07-26 02:47:18 +12:00
use Utopia\Database\Document ;
2023-01-14 04:28:04 +13:00
use Utopia\Validator\Host ;
2022-05-27 02:02:31 +12:00
use Appwrite\Utopia\Request ;
2021-03-05 18:30:34 +13:00
use Appwrite\Utopia\Response ;
2020-06-29 05:31:21 +12:00
use Utopia\App ;
2023-09-06 01:47:59 +12:00
use Utopia\Database\Database ;
2019-10-19 12:58:17 +13:00
use Utopia\Validator\ArrayList ;
2021-08-02 22:38:42 +12:00
use Utopia\Validator\Integer ;
2021-06-02 00:13:44 +12:00
use Utopia\Validator\Text ;
2021-01-22 21:28:33 +13:00
use Utopia\Storage\Validator\File ;
2022-02-04 23:23:06 +13:00
use Utopia\Validator\WhiteList ;
2022-12-15 04:42:25 +13:00
use Utopia\Database\Helpers\ID ;
2023-09-06 01:47:59 +12:00
use Utopia\Database\Validator\UID ;
2023-03-17 22:19:39 +13:00
use Utopia\Validator\Nullable ;
2023-10-13 09:15:27 +13:00
use Utopia\VCS\Adapter\Git\GitHub ;
use Utopia\Database\Helpers\Permission ;
use Utopia\Database\Helpers\Role ;
2019-10-15 07:37:59 +13:00
2020-06-29 05:31:21 +12:00
App :: get ( '/v1/mock/tests/foo' )
2021-03-05 19:40:29 +13:00
-> desc ( 'Get Foo' )
2020-08-30 18:51:44 +12:00
-> groups ([ 'mock' ])
2019-10-15 07:37:59 +13:00
-> label ( 'scope' , 'public' )
2021-04-16 19:22:17 +12:00
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_SESSION , APP_AUTH_TYPE_KEY , APP_AUTH_TYPE_JWT ])
2019-10-15 07:37:59 +13:00
-> label ( 'sdk.namespace' , 'foo' )
-> label ( 'sdk.method' , 'get' )
2021-03-05 19:40:29 +13:00
-> label ( 'sdk.description' , 'Mock a get request.' )
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_OK )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_MOCK )
2020-07-15 04:29:49 +12:00
-> label ( 'sdk.mock' , true )
2020-09-11 02:40:14 +12:00
-> param ( 'x' , '' , new Text ( 100 ), 'Sample string param' )
2021-08-02 22:38:42 +12:00
-> param ( 'y' , '' , new Integer ( true ), 'Sample numeric param' )
2022-05-01 19:54:58 +12:00
-> param ( 'z' , null , new ArrayList ( new Text ( 256 ), APP_LIMIT_ARRAY_PARAMS_SIZE ), 'Sample array param' )
2020-07-01 09:38:06 +12:00
-> action ( function ( $x , $y , $z ) {
});
2019-10-15 07:37:59 +13:00
2020-06-29 05:31:21 +12:00
App :: post ( '/v1/mock/tests/foo' )
2021-03-05 19:40:29 +13:00
-> desc ( 'Post Foo' )
2020-08-30 18:51:44 +12:00
-> groups ([ 'mock' ])
2019-10-15 07:37:59 +13:00
-> label ( 'scope' , 'public' )
2021-04-16 19:22:17 +12:00
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_SESSION , APP_AUTH_TYPE_KEY , APP_AUTH_TYPE_JWT ])
2019-10-15 07:37:59 +13:00
-> label ( 'sdk.namespace' , 'foo' )
-> label ( 'sdk.method' , 'post' )
2021-03-05 19:40:29 +13:00
-> label ( 'sdk.description' , 'Mock a post request.' )
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_OK )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_MOCK )
2020-07-15 04:29:49 +12:00
-> label ( 'sdk.mock' , true )
2020-09-11 02:40:14 +12:00
-> param ( 'x' , '' , new Text ( 100 ), 'Sample string param' )
2021-08-02 22:38:42 +12:00
-> param ( 'y' , '' , new Integer ( true ), 'Sample numeric param' )
2022-05-01 19:54:58 +12:00
-> param ( 'z' , null , new ArrayList ( new Text ( 256 ), APP_LIMIT_ARRAY_PARAMS_SIZE ), 'Sample array param' )
2020-07-01 09:38:06 +12:00
-> action ( function ( $x , $y , $z ) {
});
2019-10-15 07:37:59 +13:00
2020-06-29 05:31:21 +12:00
App :: patch ( '/v1/mock/tests/foo' )
2021-03-05 19:40:29 +13:00
-> desc ( 'Patch Foo' )
2020-08-30 18:51:44 +12:00
-> groups ([ 'mock' ])
2019-10-15 07:37:59 +13:00
-> label ( 'scope' , 'public' )
2021-04-16 19:22:17 +12:00
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_SESSION , APP_AUTH_TYPE_KEY , APP_AUTH_TYPE_JWT ])
2019-10-15 07:37:59 +13:00
-> label ( 'sdk.namespace' , 'foo' )
-> label ( 'sdk.method' , 'patch' )
2021-03-05 19:40:29 +13:00
-> label ( 'sdk.description' , 'Mock a patch request.' )
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_OK )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_MOCK )
2020-07-15 04:29:49 +12:00
-> label ( 'sdk.mock' , true )
2020-09-11 02:40:14 +12:00
-> param ( 'x' , '' , new Text ( 100 ), 'Sample string param' )
2021-08-02 22:38:42 +12:00
-> param ( 'y' , '' , new Integer ( true ), 'Sample numeric param' )
2022-05-01 19:54:58 +12:00
-> param ( 'z' , null , new ArrayList ( new Text ( 256 ), APP_LIMIT_ARRAY_PARAMS_SIZE ), 'Sample array param' )
2020-07-01 09:38:06 +12:00
-> action ( function ( $x , $y , $z ) {
});
2019-10-15 07:37:59 +13:00
2020-06-29 05:31:21 +12:00
App :: put ( '/v1/mock/tests/foo' )
2021-03-05 19:40:29 +13:00
-> desc ( 'Put Foo' )
2020-08-30 18:51:44 +12:00
-> groups ([ 'mock' ])
2019-10-15 07:37:59 +13:00
-> label ( 'scope' , 'public' )
2021-04-16 19:22:17 +12:00
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_SESSION , APP_AUTH_TYPE_KEY , APP_AUTH_TYPE_JWT ])
2019-10-15 07:37:59 +13:00
-> label ( 'sdk.namespace' , 'foo' )
-> label ( 'sdk.method' , 'put' )
2021-03-05 19:40:29 +13:00
-> label ( 'sdk.description' , 'Mock a put request.' )
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_OK )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_MOCK )
2020-07-15 04:29:49 +12:00
-> label ( 'sdk.mock' , true )
2020-09-11 02:40:14 +12:00
-> param ( 'x' , '' , new Text ( 100 ), 'Sample string param' )
2021-08-02 22:38:42 +12:00
-> param ( 'y' , '' , new Integer ( true ), 'Sample numeric param' )
2022-05-01 19:54:58 +12:00
-> param ( 'z' , null , new ArrayList ( new Text ( 256 ), APP_LIMIT_ARRAY_PARAMS_SIZE ), 'Sample array param' )
2020-07-01 09:38:06 +12:00
-> action ( function ( $x , $y , $z ) {
});
2019-10-15 07:37:59 +13:00
2020-06-29 05:31:21 +12:00
App :: delete ( '/v1/mock/tests/foo' )
2021-03-05 19:40:29 +13:00
-> desc ( 'Delete Foo' )
2020-08-30 18:51:44 +12:00
-> groups ([ 'mock' ])
2019-10-15 07:37:59 +13:00
-> label ( 'scope' , 'public' )
2021-04-16 19:22:17 +12:00
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_SESSION , APP_AUTH_TYPE_KEY , APP_AUTH_TYPE_JWT ])
2019-10-15 07:37:59 +13:00
-> label ( 'sdk.namespace' , 'foo' )
-> label ( 'sdk.method' , 'delete' )
2021-03-05 19:40:29 +13:00
-> label ( 'sdk.description' , 'Mock a delete request.' )
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_OK )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_MOCK )
2020-07-15 04:29:49 +12:00
-> label ( 'sdk.mock' , true )
2020-09-11 02:40:14 +12:00
-> param ( 'x' , '' , new Text ( 100 ), 'Sample string param' )
2021-08-02 22:38:42 +12:00
-> param ( 'y' , '' , new Integer ( true ), 'Sample numeric param' )
2022-05-01 19:54:58 +12:00
-> param ( 'z' , null , new ArrayList ( new Text ( 256 ), APP_LIMIT_ARRAY_PARAMS_SIZE ), 'Sample array param' )
2020-07-01 09:38:06 +12:00
-> action ( function ( $x , $y , $z ) {
});
2019-10-15 07:37:59 +13:00
2020-06-29 05:31:21 +12:00
App :: get ( '/v1/mock/tests/bar' )
2021-03-05 19:40:29 +13:00
-> desc ( 'Get Bar' )
2020-08-30 18:51:44 +12:00
-> groups ([ 'mock' ])
2019-10-15 07:37:59 +13:00
-> label ( 'scope' , 'public' )
2021-04-16 19:22:17 +12:00
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_SESSION , APP_AUTH_TYPE_KEY , APP_AUTH_TYPE_JWT ])
2019-10-15 07:37:59 +13:00
-> label ( 'sdk.namespace' , 'bar' )
-> label ( 'sdk.method' , 'get' )
2021-03-05 19:40:29 +13:00
-> label ( 'sdk.description' , 'Mock a get request.' )
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_OK )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_MOCK )
2020-07-15 04:29:49 +12:00
-> label ( 'sdk.mock' , true )
2021-08-02 17:30:13 +12:00
-> param ( 'required' , '' , new Text ( 100 ), 'Sample string param' )
2021-08-02 22:38:42 +12:00
-> param ( 'default' , '' , new Integer ( true ), 'Sample numeric param' )
2022-05-01 19:54:58 +12:00
-> param ( 'z' , null , new ArrayList ( new Text ( 256 ), APP_LIMIT_ARRAY_PARAMS_SIZE ), 'Sample array param' )
2021-08-02 22:44:22 +12:00
-> action ( function ( $required , $default , $z ) {
2020-07-01 09:38:06 +12:00
});
2019-10-15 07:37:59 +13:00
2020-06-29 05:31:21 +12:00
App :: post ( '/v1/mock/tests/bar' )
2021-03-05 19:40:29 +13:00
-> desc ( 'Post Bar' )
2020-08-30 18:51:44 +12:00
-> groups ([ 'mock' ])
2019-10-15 07:37:59 +13:00
-> label ( 'scope' , 'public' )
2021-04-16 19:22:17 +12:00
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_SESSION , APP_AUTH_TYPE_KEY , APP_AUTH_TYPE_JWT ])
2019-10-15 07:37:59 +13:00
-> label ( 'sdk.namespace' , 'bar' )
-> label ( 'sdk.method' , 'post' )
2021-03-05 19:40:29 +13:00
-> label ( 'sdk.description' , 'Mock a post request.' )
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_OK )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_MOCK )
2023-02-10 14:06:33 +13:00
-> label ( 'sdk.offline.model' , '/mock/tests/bar' )
-> label ( 'sdk.offline.key' , '{required}' )
2020-07-15 04:29:49 +12:00
-> label ( 'sdk.mock' , true )
2021-08-02 17:30:13 +12:00
-> param ( 'required' , '' , new Text ( 100 ), 'Sample string param' )
2021-08-02 22:38:42 +12:00
-> param ( 'default' , '' , new Integer ( true ), 'Sample numeric param' )
2022-05-01 19:54:58 +12:00
-> param ( 'z' , null , new ArrayList ( new Text ( 256 ), APP_LIMIT_ARRAY_PARAMS_SIZE ), 'Sample array param' )
2021-08-02 22:44:22 +12:00
-> action ( function ( $required , $default , $z ) {
2020-07-01 09:38:06 +12:00
});
2019-10-15 07:37:59 +13:00
2020-06-29 05:31:21 +12:00
App :: patch ( '/v1/mock/tests/bar' )
2021-03-05 19:40:29 +13:00
-> desc ( 'Patch Bar' )
2020-08-30 18:51:44 +12:00
-> groups ([ 'mock' ])
2019-10-15 07:37:59 +13:00
-> label ( 'scope' , 'public' )
2021-04-16 19:22:17 +12:00
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_SESSION , APP_AUTH_TYPE_KEY , APP_AUTH_TYPE_JWT ])
2019-10-15 07:37:59 +13:00
-> label ( 'sdk.namespace' , 'bar' )
-> label ( 'sdk.method' , 'patch' )
2021-03-05 19:40:29 +13:00
-> label ( 'sdk.description' , 'Mock a patch request.' )
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_OK )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_MOCK )
2020-07-15 04:29:49 +12:00
-> label ( 'sdk.mock' , true )
2021-08-02 17:30:13 +12:00
-> param ( 'required' , '' , new Text ( 100 ), 'Sample string param' )
2021-08-02 22:38:42 +12:00
-> param ( 'default' , '' , new Integer ( true ), 'Sample numeric param' )
2022-05-01 19:54:58 +12:00
-> param ( 'z' , null , new ArrayList ( new Text ( 256 ), APP_LIMIT_ARRAY_PARAMS_SIZE ), 'Sample array param' )
2021-08-02 22:44:22 +12:00
-> action ( function ( $required , $default , $z ) {
2020-07-01 09:38:06 +12:00
});
2019-10-15 07:37:59 +13:00
2020-06-29 05:31:21 +12:00
App :: put ( '/v1/mock/tests/bar' )
2021-03-05 19:40:29 +13:00
-> desc ( 'Put Bar' )
2020-08-30 18:51:44 +12:00
-> groups ([ 'mock' ])
2019-10-15 07:37:59 +13:00
-> label ( 'scope' , 'public' )
2021-04-16 19:22:17 +12:00
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_SESSION , APP_AUTH_TYPE_KEY , APP_AUTH_TYPE_JWT ])
2019-10-15 07:37:59 +13:00
-> label ( 'sdk.namespace' , 'bar' )
-> label ( 'sdk.method' , 'put' )
2021-03-05 19:40:29 +13:00
-> label ( 'sdk.description' , 'Mock a put request.' )
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_OK )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_MOCK )
2020-07-15 04:29:49 +12:00
-> label ( 'sdk.mock' , true )
2021-08-02 17:30:13 +12:00
-> param ( 'required' , '' , new Text ( 100 ), 'Sample string param' )
2021-08-02 22:38:42 +12:00
-> param ( 'default' , '' , new Integer ( true ), 'Sample numeric param' )
2022-05-01 19:54:58 +12:00
-> param ( 'z' , null , new ArrayList ( new Text ( 256 ), APP_LIMIT_ARRAY_PARAMS_SIZE ), 'Sample array param' )
2021-08-02 22:44:22 +12:00
-> action ( function ( $required , $default , $z ) {
2020-07-01 09:38:06 +12:00
});
2019-10-15 07:37:59 +13:00
2020-06-29 05:31:21 +12:00
App :: delete ( '/v1/mock/tests/bar' )
2021-03-05 19:40:29 +13:00
-> desc ( 'Delete Bar' )
2020-08-30 18:51:44 +12:00
-> groups ([ 'mock' ])
2019-10-15 07:37:59 +13:00
-> label ( 'scope' , 'public' )
2021-04-16 19:22:17 +12:00
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_SESSION , APP_AUTH_TYPE_KEY , APP_AUTH_TYPE_JWT ])
2019-10-15 07:37:59 +13:00
-> label ( 'sdk.namespace' , 'bar' )
-> label ( 'sdk.method' , 'delete' )
2021-03-05 19:40:29 +13:00
-> label ( 'sdk.description' , 'Mock a delete request.' )
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_OK )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_MOCK )
2020-07-15 04:29:49 +12:00
-> label ( 'sdk.mock' , true )
2021-08-02 17:30:13 +12:00
-> param ( 'required' , '' , new Text ( 100 ), 'Sample string param' )
2021-08-02 22:38:42 +12:00
-> param ( 'default' , '' , new Integer ( true ), 'Sample numeric param' )
2022-05-01 19:54:58 +12:00
-> param ( 'z' , null , new ArrayList ( new Text ( 256 ), APP_LIMIT_ARRAY_PARAMS_SIZE ), 'Sample array param' )
2021-08-02 22:44:22 +12:00
-> action ( function ( $required , $default , $z ) {
2020-07-01 09:38:06 +12:00
});
2019-10-15 07:37:59 +13:00
2023-08-31 09:55:27 +12:00
App :: get ( '/v1/mock/tests/general/headers' )
-> desc ( 'Get headers' )
-> groups ([ 'mock' ])
-> label ( 'scope' , 'public' )
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_SESSION , APP_AUTH_TYPE_KEY , APP_AUTH_TYPE_JWT ])
-> label ( 'sdk.namespace' , 'general' )
-> label ( 'sdk.method' , 'headers' )
-> label ( 'sdk.description' , 'Return headers from the request' )
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_OK )
-> label ( 'sdk.response.model' , Response :: MODEL_MOCK )
-> label ( 'sdk.mock' , true )
-> inject ( 'request' )
-> inject ( 'response' )
-> action ( function ( Request $request , Response $response ) {
$res = [
'x-sdk-name' => $request -> getHeader ( 'x-sdk-name' ),
'x-sdk-platform' => $request -> getHeader ( 'x-sdk-platform' ),
'x-sdk-language' => $request -> getHeader ( 'x-sdk-language' ),
'x-sdk-version' => $request -> getHeader ( 'x-sdk-version' ),
];
$res = array_map ( function ( $key , $value ) {
return $key . ': ' . $value ;
}, array_keys ( $res ), $res );
$res = implode ( " ; " , $res );
$response -> dynamic ( new Document ([ 'result' => $res ]), Response :: MODEL_MOCK );
});
2021-06-02 05:24:08 +12:00
App :: get ( '/v1/mock/tests/general/download' )
-> desc ( 'Download File' )
-> groups ([ 'mock' ])
-> label ( 'scope' , 'public' )
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_SESSION , APP_AUTH_TYPE_KEY , APP_AUTH_TYPE_JWT ])
-> label ( 'sdk.namespace' , 'general' )
-> label ( 'sdk.method' , 'download' )
-> label ( 'sdk.methodType' , 'location' )
-> label ( 'sdk.description' , 'Mock a file download request.' )
-> label ( 'sdk.response.type' , '*/*' )
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_OK )
-> label ( 'sdk.mock' , true )
-> inject ( 'response' )
2022-05-27 02:02:31 +12:00
-> action ( function ( Response $response ) {
2022-05-31 23:35:59 +12:00
2021-06-02 05:24:08 +12:00
$response
-> setContentType ( 'text/plain' )
-> addHeader ( 'Content-Disposition' , 'attachment; filename="test.txt"' )
2022-05-24 02:54:50 +12:00
-> addHeader ( 'Expires' , \date ( 'D, d M Y H:i:s' , \time () + ( 60 * 60 * 24 * 45 )) . ' GMT' ) // 45 days cache
2021-06-02 05:24:08 +12:00
-> addHeader ( 'X-Peak' , \memory_get_peak_usage ())
2022-08-03 17:03:34 +12:00
-> send ( " GET:/v1/mock/tests/general/download:passed " )
2021-06-02 05:24:08 +12:00
;
});
2020-06-29 05:31:21 +12:00
App :: post ( '/v1/mock/tests/general/upload' )
2021-03-05 18:30:34 +13:00
-> desc ( 'Upload File' )
2020-08-30 18:51:44 +12:00
-> groups ([ 'mock' ])
2019-10-15 07:37:59 +13:00
-> label ( 'scope' , 'public' )
2021-04-16 19:22:17 +12:00
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_SESSION , APP_AUTH_TYPE_KEY , APP_AUTH_TYPE_JWT ])
2019-12-25 09:59:31 +13:00
-> label ( 'sdk.namespace' , 'general' )
2019-10-20 01:52:17 +13:00
-> label ( 'sdk.method' , 'upload' )
2021-03-05 19:40:29 +13:00
-> label ( 'sdk.description' , 'Mock a file upload request.' )
2020-11-12 11:02:02 +13:00
-> label ( 'sdk.request.type' , 'multipart/form-data' )
2021-03-05 19:40:29 +13:00
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_OK )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_MOCK )
2020-07-15 04:29:49 +12:00
-> label ( 'sdk.mock' , true )
2020-09-11 02:40:14 +12:00
-> param ( 'x' , '' , new Text ( 100 ), 'Sample string param' )
2021-08-02 22:38:42 +12:00
-> param ( 'y' , '' , new Integer ( true ), 'Sample numeric param' )
2022-05-01 19:54:58 +12:00
-> param ( 'z' , null , new ArrayList ( new Text ( 256 ), APP_LIMIT_ARRAY_PARAMS_SIZE ), 'Sample array param' )
2023-04-12 14:53:35 +12:00
-> param ( 'file' , [], new File (), 'Sample file param' , skipValidation : true )
2020-12-27 01:19:53 +13:00
-> inject ( 'request' )
2021-11-02 18:32:02 +13:00
-> inject ( 'response' )
2022-06-24 21:39:44 +12:00
-> action ( function ( string $x , int $y , array $z , mixed $file , Request $request , Response $response ) {
2022-05-31 23:35:59 +12:00
2020-07-01 09:38:06 +12:00
$file = $request -> getFiles ( 'file' );
2022-05-24 02:54:50 +12:00
2021-11-02 18:32:02 +13:00
$contentRange = $request -> getHeader ( 'content-range' );
2022-03-01 17:50:25 +13:00
2022-05-24 02:54:50 +12:00
$chunkSize = 5 * 1024 * 1024 ; // 5MB
2022-03-01 17:50:25 +13:00
2022-05-24 02:54:50 +12:00
if ( ! empty ( $contentRange )) {
2021-11-02 18:32:02 +13:00
$start = $request -> getContentRangeStart ();
$end = $request -> getContentRangeEnd ();
$size = $request -> getContentRangeSize ();
$id = $request -> getHeader ( 'x-appwrite-id' , '' );
2022-03-04 19:12:44 +13:00
$file [ 'size' ] = ( \is_array ( $file [ 'size' ])) ? $file [ 'size' ][ 0 ] : $file [ 'size' ];
2020-07-01 09:38:06 +12:00
2023-04-19 18:15:24 +12:00
if ( is_null ( $start ) || is_null ( $end ) || is_null ( $size ) || $end >= $size ) {
2022-08-14 18:56:12 +12:00
throw new Exception ( Exception :: GENERAL_MOCK , 'Invalid content-range header' );
2020-02-13 21:04:17 +13:00
}
2022-05-24 02:54:50 +12:00
if ( $start > $end || $end > $size ) {
2022-08-14 18:56:12 +12:00
throw new Exception ( Exception :: GENERAL_MOCK , 'Invalid content-range header' );
2021-11-02 18:32:02 +13:00
}
2022-05-24 02:54:50 +12:00
if ( $start === 0 && ! empty ( $id )) {
2022-08-14 18:56:12 +12:00
throw new Exception ( Exception :: GENERAL_MOCK , 'First chunked request cannot have id header' );
2020-02-13 21:04:17 +13:00
}
2019-10-20 01:52:17 +13:00
2022-05-24 02:54:50 +12:00
if ( $start !== 0 && $id !== 'newfileid' ) {
2022-08-14 18:56:12 +12:00
throw new Exception ( Exception :: GENERAL_MOCK , 'All chunked request must have id header (except first)' );
2021-11-02 18:32:02 +13:00
}
2023-04-19 18:15:24 +12:00
if ( $end !== $size - 1 && $end - $start + 1 !== $chunkSize ) {
2022-08-14 18:56:12 +12:00
throw new Exception ( Exception :: GENERAL_MOCK , 'Chunk size must be 5MB (except last chunk)' );
2020-02-13 21:04:17 +13:00
}
2019-10-20 01:52:17 +13:00
2023-04-19 18:15:24 +12:00
if ( $end !== $size - 1 && $file [ 'size' ] !== $chunkSize ) {
2022-08-14 18:56:12 +12:00
throw new Exception ( Exception :: GENERAL_MOCK , 'Wrong chunk size' );
2021-11-02 18:32:02 +13:00
}
2022-05-24 02:54:50 +12:00
if ( $file [ 'size' ] > $chunkSize ) {
2022-08-14 18:56:12 +12:00
throw new Exception ( Exception :: GENERAL_MOCK , 'Chunk size must be 5MB or less' );
2022-03-04 19:12:44 +13:00
}
2023-04-19 18:15:24 +12:00
if ( $end !== $size - 1 ) {
2022-03-01 17:50:25 +13:00
$response -> json ([
2022-08-14 22:33:36 +12:00
'$id' => ID :: custom ( 'newfileid' ),
2023-04-19 18:15:24 +12:00
'chunksTotal' => ( int ) ceil ( $size / ( $end + 1 - $start )),
'chunksUploaded' => ceil ( $start / $chunkSize ) + 1
2022-03-01 17:50:25 +13:00
]);
2021-11-02 21:59:26 +13:00
}
2021-11-02 18:32:02 +13:00
} else {
2022-03-04 19:14:20 +13:00
$file [ 'tmp_name' ] = ( \is_array ( $file [ 'tmp_name' ])) ? $file [ 'tmp_name' ][ 0 ] : $file [ 'tmp_name' ];
$file [ 'name' ] = ( \is_array ( $file [ 'name' ])) ? $file [ 'name' ][ 0 ] : $file [ 'name' ];
$file [ 'size' ] = ( \is_array ( $file [ 'size' ])) ? $file [ 'size' ][ 0 ] : $file [ 'size' ];
2022-05-24 02:54:50 +12:00
2022-03-04 19:18:10 +13:00
if ( $file [ 'name' ] !== 'file.png' ) {
2022-08-14 18:56:12 +12:00
throw new Exception ( Exception :: GENERAL_MOCK , 'Wrong file name' );
2021-11-02 18:32:02 +13:00
}
2022-05-24 02:54:50 +12:00
2022-03-04 19:18:10 +13:00
if ( $file [ 'size' ] !== 38756 ) {
2022-08-14 18:56:12 +12:00
throw new Exception ( Exception :: GENERAL_MOCK , 'Wrong file size' );
2021-11-02 18:32:02 +13:00
}
2022-03-04 19:14:20 +13:00
if ( \md5 ( \file_get_contents ( $file [ 'tmp_name' ])) !== 'd80e7e6999a3eb2ae0d631a96fe135a4' ) {
2022-08-14 18:56:12 +12:00
throw new Exception ( Exception :: GENERAL_MOCK , 'Wrong file uploaded' );
2019-10-15 07:37:59 +13:00
}
}
2020-12-27 01:19:53 +13:00
});
2019-10-15 07:37:59 +13:00
2020-06-29 05:31:21 +12:00
App :: get ( '/v1/mock/tests/general/redirect' )
2021-03-05 18:30:34 +13:00
-> desc ( 'Redirect' )
2020-08-30 18:51:44 +12:00
-> groups ([ 'mock' ])
2019-12-25 09:59:31 +13:00
-> label ( 'scope' , 'public' )
2021-04-16 19:22:17 +12:00
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_SESSION , APP_AUTH_TYPE_KEY , APP_AUTH_TYPE_JWT ])
2019-12-25 09:59:31 +13:00
-> label ( 'sdk.namespace' , 'general' )
-> label ( 'sdk.method' , 'redirect' )
2021-03-05 19:40:29 +13:00
-> label ( 'sdk.description' , 'Mock a redirect request.' )
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_MOVED_PERMANENTLY )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_HTML )
2021-06-02 00:13:44 +12:00
-> label ( 'sdk.response.model' , Response :: MODEL_MOCK )
2020-07-15 04:29:49 +12:00
-> label ( 'sdk.mock' , true )
2020-12-27 01:19:53 +13:00
-> inject ( 'response' )
2022-05-27 02:02:31 +12:00
-> action ( function ( Response $response ) {
2020-07-01 09:38:06 +12:00
2020-12-23 10:01:11 +13:00
$response -> redirect ( '/v1/mock/tests/general/redirect/done' );
2020-12-27 01:19:53 +13:00
});
2019-12-25 09:59:31 +13:00
2020-12-23 10:01:11 +13:00
App :: get ( '/v1/mock/tests/general/redirect/done' )
2021-03-05 19:40:29 +13:00
-> desc ( 'Redirected' )
2020-08-30 18:51:44 +12:00
-> groups ([ 'mock' ])
2019-12-25 09:59:31 +13:00
-> label ( 'scope' , 'public' )
2021-04-16 19:22:17 +12:00
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_SESSION , APP_AUTH_TYPE_KEY , APP_AUTH_TYPE_JWT ])
2019-12-25 09:59:31 +13:00
-> label ( 'sdk.namespace' , 'general' )
2019-12-25 19:31:09 +13:00
-> label ( 'sdk.method' , 'redirected' )
2021-03-05 19:40:29 +13:00
-> label ( 'sdk.description' , 'Mock a redirected request.' )
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_OK )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_MOCK )
2020-07-15 04:29:49 +12:00
-> label ( 'sdk.mock' , true )
2020-07-01 09:38:06 +12:00
-> action ( function () {
});
2019-12-25 09:59:31 +13:00
2020-06-29 05:31:21 +12:00
App :: get ( '/v1/mock/tests/general/set-cookie' )
2021-03-05 18:30:34 +13:00
-> desc ( 'Set Cookie' )
2020-08-30 18:51:44 +12:00
-> groups ([ 'mock' ])
2020-04-05 07:16:00 +12:00
-> label ( 'scope' , 'public' )
2021-04-16 19:22:17 +12:00
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_SESSION , APP_AUTH_TYPE_KEY , APP_AUTH_TYPE_JWT ])
2020-04-05 07:16:00 +12:00
-> label ( 'sdk.namespace' , 'general' )
-> label ( 'sdk.method' , 'setCookie' )
2021-03-05 18:30:34 +13:00
-> label ( 'sdk.description' , 'Mock a set cookie request.' )
2021-03-05 19:40:29 +13:00
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_OK )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_MOCK )
2020-07-15 04:29:49 +12:00
-> label ( 'sdk.mock' , true )
2020-12-27 01:19:53 +13:00
-> inject ( 'response' )
2022-01-12 00:30:29 +13:00
-> inject ( 'request' )
2022-05-27 02:02:31 +12:00
-> action ( function ( Response $response , Request $request ) {
2020-07-01 09:38:06 +12:00
2022-01-12 00:30:29 +13:00
$response -> addCookie ( 'cookieName' , 'cookieValue' , \time () + 31536000 , '/' , $request -> getHostname (), true , true );
2020-12-27 01:19:53 +13:00
});
2020-04-05 07:16:00 +12:00
2020-06-29 05:31:21 +12:00
App :: get ( '/v1/mock/tests/general/get-cookie' )
2021-03-05 18:30:34 +13:00
-> desc ( 'Get Cookie' )
2020-08-30 18:51:44 +12:00
-> groups ([ 'mock' ])
2020-04-05 07:16:00 +12:00
-> label ( 'scope' , 'public' )
2021-04-16 19:22:17 +12:00
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_SESSION , APP_AUTH_TYPE_KEY , APP_AUTH_TYPE_JWT ])
2020-04-05 07:16:00 +12:00
-> label ( 'sdk.namespace' , 'general' )
2020-04-05 07:32:38 +12:00
-> label ( 'sdk.method' , 'getCookie' )
2021-03-05 18:30:34 +13:00
-> label ( 'sdk.description' , 'Mock a cookie response.' )
2021-03-05 19:40:29 +13:00
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_OK )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_MOCK )
2020-07-15 04:29:49 +12:00
-> label ( 'sdk.mock' , true )
2020-12-27 01:19:53 +13:00
-> inject ( 'request' )
2022-05-27 02:02:31 +12:00
-> action ( function ( Request $request ) {
2020-07-01 09:38:06 +12:00
if ( $request -> getCookie ( 'cookieName' , '' ) !== 'cookieValue' ) {
2022-08-14 18:56:12 +12:00
throw new Exception ( Exception :: GENERAL_MOCK , 'Missing cookie value' );
2020-04-05 07:16:00 +12:00
}
2020-12-27 01:19:53 +13:00
});
2020-04-05 07:16:00 +12:00
2020-06-29 05:31:21 +12:00
App :: get ( '/v1/mock/tests/general/empty' )
2021-03-05 18:30:34 +13:00
-> desc ( 'Empty Response' )
2020-08-30 18:51:44 +12:00
-> groups ([ 'mock' ])
2019-12-25 19:31:09 +13:00
-> label ( 'scope' , 'public' )
2021-04-16 19:22:17 +12:00
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_SESSION , APP_AUTH_TYPE_KEY , APP_AUTH_TYPE_JWT ])
2019-12-25 19:31:09 +13:00
-> label ( 'sdk.namespace' , 'general' )
-> label ( 'sdk.method' , 'empty' )
2021-05-07 03:53:07 +12:00
-> label ( 'sdk.description' , 'Mock an empty response.' )
2021-03-05 18:30:34 +13:00
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_NOCONTENT )
-> label ( 'sdk.response.model' , Response :: MODEL_NONE )
2020-07-15 04:29:49 +12:00
-> label ( 'sdk.mock' , true )
2020-12-27 01:19:53 +13:00
-> inject ( 'response' )
2022-05-27 02:02:31 +12:00
-> action ( function ( Response $response ) {
2020-07-01 09:38:06 +12:00
$response -> noContent ();
2020-12-27 01:19:53 +13:00
});
2019-12-25 19:31:09 +13:00
2023-03-17 22:19:39 +13:00
App :: post ( '/v1/mock/tests/general/nullable' )
-> desc ( 'Nullable Test' )
-> groups ([ 'mock' ])
-> label ( 'scope' , 'public' )
2023-03-17 22:31:36 +13:00
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_SESSION , APP_AUTH_TYPE_KEY , APP_AUTH_TYPE_JWT ])
-> label ( 'sdk.namespace' , 'general' )
-> label ( 'sdk.method' , 'nullable' )
-> label ( 'sdk.description' , 'Mock a nullable parameter.' )
-> label ( 'sdk.mock' , true )
2023-03-17 22:19:39 +13:00
-> param ( 'required' , '' , new Text ( 100 ), 'Sample string param' )
-> param ( 'nullable' , '' , new Nullable ( new Text ( 100 )), 'Sample string param' )
-> param ( 'optional' , '' , new Text ( 100 ), 'Sample string param' , true )
-> action ( function ( string $required , string $nullable , ? string $optional ) {
});
2023-09-08 09:21:06 +12:00
App :: post ( '/v1/mock/tests/general/enum' )
2023-09-08 11:25:42 +12:00
-> desc ( 'Enum Test' )
2023-09-08 09:21:06 +12:00
-> groups ([ 'mock' ])
-> label ( 'scope' , 'public' )
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_SESSION , APP_AUTH_TYPE_KEY , APP_AUTH_TYPE_JWT ])
-> label ( 'sdk.namespace' , 'general' )
2023-09-08 11:25:42 +12:00
-> label ( 'sdk.method' , 'enum' )
-> label ( 'sdk.description' , 'Mock an enum parameter.' )
2023-09-08 09:21:06 +12:00
-> label ( 'sdk.mock' , true )
2023-09-08 11:25:42 +12:00
-> param ( 'mockType' , '' , new WhiteList ([ 'first' , 'second' , 'third' ]), 'Sample enum param' )
-> action ( function ( string $mockType ) {
2023-09-08 09:21:06 +12:00
});
2021-03-05 10:32:03 +13:00
App :: get ( '/v1/mock/tests/general/400-error' )
2021-03-05 18:30:34 +13:00
-> desc ( '400 Error' )
2021-03-05 10:32:03 +13:00
-> groups ([ 'mock' ])
-> label ( 'scope' , 'public' )
2021-04-16 19:22:17 +12:00
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_SESSION , APP_AUTH_TYPE_KEY , APP_AUTH_TYPE_JWT ])
2021-03-05 10:32:03 +13:00
-> label ( 'sdk.namespace' , 'general' )
-> label ( 'sdk.method' , 'error400' )
2021-05-07 03:53:48 +12:00
-> label ( 'sdk.description' , 'Mock a 400 failed request.' )
2021-03-05 18:30:34 +13:00
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_BAD_REQUEST )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_ERROR )
2021-03-05 10:32:03 +13:00
-> label ( 'sdk.mock' , true )
-> action ( function () {
2022-08-14 18:56:12 +12:00
throw new Exception ( Exception :: GENERAL_MOCK , 'Mock 400 error' );
2021-03-05 10:32:03 +13:00
});
App :: get ( '/v1/mock/tests/general/500-error' )
2021-03-05 18:30:34 +13:00
-> desc ( '500 Error' )
2021-03-05 10:32:03 +13:00
-> groups ([ 'mock' ])
-> label ( 'scope' , 'public' )
2021-04-16 19:22:17 +12:00
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_SESSION , APP_AUTH_TYPE_KEY , APP_AUTH_TYPE_JWT ])
2021-03-05 10:32:03 +13:00
-> label ( 'sdk.namespace' , 'general' )
-> label ( 'sdk.method' , 'error500' )
2021-05-07 03:53:48 +12:00
-> label ( 'sdk.description' , 'Mock a 500 failed request.' )
2021-03-05 18:30:34 +13:00
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_INTERNAL_SERVER_ERROR )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_ERROR )
2021-03-05 10:32:03 +13:00
-> label ( 'sdk.mock' , true )
-> action ( function () {
2022-08-14 18:56:12 +12:00
throw new Exception ( Exception :: GENERAL_MOCK , 'Mock 500 error' , 500 );
2021-03-05 10:32:03 +13:00
});
2021-05-07 03:45:32 +12:00
App :: get ( '/v1/mock/tests/general/502-error' )
-> desc ( '502 Error' )
-> groups ([ 'mock' ])
-> label ( 'scope' , 'public' )
2021-05-08 01:52:40 +12:00
-> label ( 'sdk.platform' , [ APP_PLATFORM_CLIENT , APP_PLATFORM_SERVER ])
2021-05-07 03:45:32 +12:00
-> label ( 'sdk.namespace' , 'general' )
-> label ( 'sdk.method' , 'error502' )
2021-05-07 03:53:48 +12:00
-> label ( 'sdk.description' , 'Mock a 502 bad gateway.' )
2021-05-07 03:45:32 +12:00
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_BAD_GATEWAY )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_TEXT )
2021-06-02 00:13:44 +12:00
-> label ( 'sdk.response.model' , Response :: MODEL_ANY )
2021-05-07 03:45:32 +12:00
-> label ( 'sdk.mock' , true )
-> inject ( 'response' )
2022-05-27 02:02:31 +12:00
-> action ( function ( Response $response ) {
2021-05-07 03:45:32 +12:00
$response
-> setStatusCode ( 502 )
-> text ( 'This is a text error' )
;
});
2020-06-29 05:31:21 +12:00
App :: get ( '/v1/mock/tests/general/oauth2' )
2021-03-05 19:40:29 +13:00
-> desc ( 'OAuth Login' )
2020-08-30 18:51:44 +12:00
-> groups ([ 'mock' ])
2020-01-14 01:23:45 +13:00
-> label ( 'scope' , 'public' )
2020-01-14 01:40:10 +13:00
-> label ( 'docs' , false )
2020-07-15 04:29:49 +12:00
-> label ( 'sdk.mock' , true )
2020-09-11 02:40:14 +12:00
-> param ( 'client_id' , '' , new Text ( 100 ), 'OAuth2 Client ID.' )
-> param ( 'redirect_uri' , '' , new Host ([ 'localhost' ]), 'OAuth2 Redirect URI.' ) // Important to deny an open redirect attack
-> param ( 'scope' , '' , new Text ( 100 ), 'OAuth2 scope list.' )
-> param ( 'state' , '' , new Text ( 1024 ), 'OAuth2 state.' )
2020-12-27 01:19:53 +13:00
-> inject ( 'response' )
2022-05-27 02:02:31 +12:00
-> action ( function ( string $client_id , string $redirectURI , string $scope , string $state , Response $response ) {
2020-07-01 09:38:06 +12:00
2022-05-24 02:54:50 +12:00
$response -> redirect ( $redirectURI . '?' . \http_build_query ([ 'code' => 'abcdef' , 'state' => $state ]));
2020-12-27 01:19:53 +13:00
});
2020-01-14 01:23:45 +13:00
2020-06-29 05:31:21 +12:00
App :: get ( '/v1/mock/tests/general/oauth2/token' )
2021-03-05 19:40:29 +13:00
-> desc ( 'OAuth2 Token' )
2020-08-30 18:51:44 +12:00
-> groups ([ 'mock' ])
2020-01-14 01:23:45 +13:00
-> label ( 'scope' , 'public' )
2020-01-14 01:40:10 +13:00
-> label ( 'docs' , false )
2020-07-15 04:29:49 +12:00
-> label ( 'sdk.mock' , true )
2020-09-11 02:40:14 +12:00
-> param ( 'client_id' , '' , new Text ( 100 ), 'OAuth2 Client ID.' )
-> param ( 'client_secret' , '' , new Text ( 100 ), 'OAuth2 scope list.' )
2022-02-05 04:37:47 +13:00
-> param ( 'grant_type' , 'authorization_code' , new WhiteList ([ 'refresh_token' , 'authorization_code' ]), 'OAuth2 Grant Type.' , true )
2022-02-04 23:23:06 +13:00
-> param ( 'redirect_uri' , '' , new Host ([ 'localhost' ]), 'OAuth2 Redirect URI.' , true )
-> param ( 'code' , '' , new Text ( 100 ), 'OAuth2 state.' , true )
-> param ( 'refresh_token' , '' , new Text ( 100 ), 'OAuth2 refresh token.' , true )
2020-12-27 01:19:53 +13:00
-> inject ( 'response' )
2022-05-27 02:02:31 +12:00
-> action ( function ( string $client_id , string $client_secret , string $grantType , string $redirectURI , string $code , string $refreshToken , Response $response ) {
2020-01-14 01:23:45 +13:00
2020-10-28 04:33:13 +13:00
if ( $client_id != '1' ) {
2022-08-14 18:56:12 +12:00
throw new Exception ( Exception :: GENERAL_MOCK , 'Invalid client ID' );
2020-07-01 09:38:06 +12:00
}
2020-01-14 01:23:45 +13:00
2020-10-28 04:33:13 +13:00
if ( $client_secret != '123456' ) {
2022-08-14 18:56:12 +12:00
throw new Exception ( Exception :: GENERAL_MOCK , 'Invalid client secret' );
2020-07-01 09:38:06 +12:00
}
2020-01-14 01:23:45 +13:00
2022-02-04 23:23:06 +13:00
$responseJson = [
'access_token' => '123456' ,
'refresh_token' => 'tuvwxyz' ,
'expires_in' => 14400
];
2022-05-24 02:54:50 +12:00
if ( $grantType === 'authorization_code' ) {
2022-02-04 23:23:06 +13:00
if ( $code !== 'abcdef' ) {
2022-08-14 18:56:12 +12:00
throw new Exception ( Exception :: GENERAL_MOCK , 'Invalid token' );
2022-02-04 23:23:06 +13:00
}
$response -> json ( $responseJson );
2022-05-24 02:54:50 +12:00
} elseif ( $grantType === 'refresh_token' ) {
2022-02-04 23:23:06 +13:00
if ( $refreshToken !== 'tuvwxyz' ) {
2022-08-14 18:56:12 +12:00
throw new Exception ( Exception :: GENERAL_MOCK , 'Invalid refresh token' );
2022-02-04 23:23:06 +13:00
}
2020-07-01 09:38:06 +12:00
2022-02-04 23:23:06 +13:00
$response -> json ( $responseJson );
} else {
2022-08-14 18:56:12 +12:00
throw new Exception ( Exception :: GENERAL_MOCK , 'Invalid grant type' );
2022-02-04 23:23:06 +13:00
}
2020-12-27 01:19:53 +13:00
});
2020-01-14 01:23:45 +13:00
2020-06-29 05:31:21 +12:00
App :: get ( '/v1/mock/tests/general/oauth2/user' )
2021-03-05 19:40:29 +13:00
-> desc ( 'OAuth2 User' )
2020-08-30 18:51:44 +12:00
-> groups ([ 'mock' ])
2020-01-14 01:23:45 +13:00
-> label ( 'scope' , 'public' )
2020-01-14 01:40:10 +13:00
-> label ( 'docs' , false )
2020-09-11 02:40:14 +12:00
-> param ( 'token' , '' , new Text ( 100 ), 'OAuth2 Access Token.' )
2020-12-27 01:19:53 +13:00
-> inject ( 'response' )
2022-05-27 02:02:31 +12:00
-> action ( function ( string $token , Response $response ) {
2020-01-14 01:23:45 +13:00
2020-07-01 09:38:06 +12:00
if ( $token != '123456' ) {
2022-08-14 18:56:12 +12:00
throw new Exception ( Exception :: GENERAL_MOCK , 'Invalid token' );
2020-01-14 01:23:45 +13:00
}
2020-07-01 09:38:06 +12:00
$response -> json ([
'id' => 1 ,
'name' => 'User Name' ,
2022-02-04 23:23:06 +13:00
'email' => 'useroauth@localhost.test' ,
2020-07-01 09:38:06 +12:00
]);
2020-12-27 01:19:53 +13:00
});
2020-01-14 01:23:45 +13:00
2020-06-29 05:31:21 +12:00
App :: get ( '/v1/mock/tests/general/oauth2/success' )
2021-03-05 19:40:29 +13:00
-> desc ( 'OAuth2 Success' )
2020-08-30 18:51:44 +12:00
-> groups ([ 'mock' ])
2021-03-05 19:40:29 +13:00
-> label ( 'scope' , 'public' )
2020-01-14 04:15:52 +13:00
-> label ( 'docs' , false )
2020-12-27 01:19:53 +13:00
-> inject ( 'response' )
2022-05-27 02:02:31 +12:00
-> action ( function ( Response $response ) {
2020-07-01 09:38:06 +12:00
$response -> json ([
'result' => 'success' ,
]);
2020-12-27 01:19:53 +13:00
});
2020-01-14 04:15:52 +13:00
2020-06-29 05:31:21 +12:00
App :: get ( '/v1/mock/tests/general/oauth2/failure' )
2021-03-05 19:40:29 +13:00
-> desc ( 'OAuth2 Failure' )
2020-08-30 18:51:44 +12:00
-> groups ([ 'mock' ])
2020-01-14 04:15:52 +13:00
-> label ( 'scope' , 'public' )
-> label ( 'docs' , false )
2020-12-27 01:19:53 +13:00
-> inject ( 'response' )
2022-05-27 02:02:31 +12:00
-> action ( function ( Response $response ) {
2020-07-01 09:38:06 +12:00
$response
-> setStatusCode ( Response :: STATUS_CODE_BAD_REQUEST )
-> json ([
'result' => 'failure' ,
]);
2020-12-27 01:19:53 +13:00
});
2020-07-01 09:38:06 +12:00
2023-09-06 01:47:59 +12:00
App :: patch ( '/v1/mock/functions-v2' )
2023-09-06 20:00:04 +12:00
-> desc ( 'Update Function Version to V2 (outdated code syntax)' )
-> groups ([ 'mock' , 'api' , 'functions' ])
2023-09-06 01:47:59 +12:00
-> label ( 'scope' , 'functions.write' )
-> label ( 'docs' , false )
-> param ( 'functionId' , '' , new UID (), 'Function ID.' )
-> inject ( 'response' )
-> inject ( 'dbForProject' )
-> action ( function ( string $functionId , Response $response , Database $dbForProject ) {
$isDevelopment = App :: getEnv ( '_APP_ENV' , 'development' ) === 'development' ;
if ( ! $isDevelopment ) {
throw new Exception ( Exception :: GENERAL_NOT_IMPLEMENTED );
}
$function = $dbForProject -> getDocument ( 'functions' , $functionId );
if ( $function -> isEmpty ()) {
throw new Exception ( Exception :: FUNCTION_NOT_FOUND );
}
$dbForProject -> updateDocument ( 'functions' , $function -> getId (), $function -> setAttribute ( 'version' , 'v2' ));
$response -> noContent ();
});
2023-10-14 02:20:58 +13:00
App :: get ( '/v1/mock/github/callback' )
2023-10-13 09:15:27 +13:00
-> desc ( 'Create installation document using GitHub installation id' )
-> groups ([ 'mock' , 'api' , 'vcs' ])
-> label ( 'scope' , 'public' )
-> param ( 'installation_id' , '' , new Text ( 256 , 0 ), 'GitHub installation ID' , true )
-> param ( 'projectId' , '' , new Text ( 2048 ), 'Project ID of the project where app is to be installed' , true )
-> inject ( 'gitHub' )
-> inject ( 'project' )
-> inject ( 'response' )
-> inject ( 'dbForConsole' )
-> action ( function ( string $providerInstallationId , string $projectId , GitHub $github , Document $project , Response $response , Database $dbForConsole ) {
if ( empty ( $projectId )) {
$error = 'Installation requests from organisation members for the Appwrite GitHub App are currently unsupported. To proceed with the installation, login to the Appwrite Console and install the GitHub App.' ;
throw new Exception ( Exception :: GENERAL_ARGUMENT_INVALID , $error );
}
$project = $dbForConsole -> getDocument ( 'projects' , $projectId );
if ( $project -> isEmpty ()) {
$error = 'Project with the ID from state could not be found.' ;
throw new Exception ( Exception :: PROJECT_NOT_FOUND , $error );
}
// Create / Update installation
if ( ! empty ( $providerInstallationId )) {
$privateKey = App :: getEnv ( '_APP_VCS_GITHUB_PRIVATE_KEY' );
$githubAppId = App :: getEnv ( '_APP_VCS_GITHUB_APP_ID' );
$github -> initializeVariables ( $providerInstallationId , $privateKey , $githubAppId );
$owner = $github -> getOwnerName ( $providerInstallationId ) ? ? '' ;
$projectInternalId = $project -> getInternalId ();
$teamId = $project -> getAttribute ( 'teamId' , '' );
$installation = new Document ([
'$id' => ID :: unique (),
'$permissions' => [
Permission :: read ( Role :: team ( ID :: custom ( $teamId ))),
Permission :: update ( Role :: team ( ID :: custom ( $teamId ), 'owner' )),
Permission :: update ( Role :: team ( ID :: custom ( $teamId ), 'developer' )),
Permission :: delete ( Role :: team ( ID :: custom ( $teamId ), 'owner' )),
Permission :: delete ( Role :: team ( ID :: custom ( $teamId ), 'developer' )),
],
'providerInstallationId' => $providerInstallationId ,
'projectId' => $projectId ,
'projectInternalId' => $projectInternalId ,
'provider' => 'github' ,
'organization' => $owner ,
2023-10-18 20:54:32 +13:00
'personal' => false
2023-10-13 09:15:27 +13:00
]);
$installation = $dbForConsole -> createDocument ( 'installations' , $installation );
} else {
$error = 'Installation of the Appwrite GitHub App on organization accounts is restricted to organization owners. As a member of the organization, you do not have the necessary permissions to install this GitHub App. Please contact the organization owner to create the installation from the Appwrite console.' ;
throw new Exception ( Exception :: GENERAL_ARGUMENT_INVALID , $error );
}
$response -> json ([
'installationId' => $installation -> getId (),
]);
});
2022-07-22 18:00:42 +12:00
App :: shutdown ()
2022-08-02 13:10:48 +12:00
-> groups ([ 'mock' ])
2022-07-22 18:00:42 +12:00
-> inject ( 'utopia' )
-> inject ( 'response' )
-> inject ( 'request' )
-> action ( function ( App $utopia , Response $response , Request $request ) {
2020-01-14 04:15:52 +13:00
2022-07-22 18:00:42 +12:00
$result = [];
2023-02-20 00:04:12 +13:00
$route = $utopia -> getRoute ();
2022-07-22 18:00:42 +12:00
$path = APP_STORAGE_CACHE . '/tests.json' ;
$tests = ( \file_exists ( $path )) ? \json_decode ( \file_get_contents ( $path ), true ) : [];
2022-05-24 02:54:50 +12:00
2022-07-22 18:00:42 +12:00
if ( ! \is_array ( $tests )) {
2022-08-14 18:56:12 +12:00
throw new Exception ( Exception :: GENERAL_MOCK , 'Failed to read results' , 500 );
2022-07-22 18:00:42 +12:00
}
2019-10-15 07:37:59 +13:00
2022-07-22 18:00:42 +12:00
$result [ $route -> getMethod () . ':' . $route -> getPath ()] = true ;
2019-10-15 07:37:59 +13:00
2022-07-22 18:00:42 +12:00
$tests = \array_merge ( $tests , $result );
2019-10-15 07:37:59 +13:00
2022-07-22 18:00:42 +12:00
if ( ! \file_put_contents ( $path , \json_encode ( $tests ), LOCK_EX )) {
2022-08-14 18:56:12 +12:00
throw new Exception ( Exception :: GENERAL_MOCK , 'Failed to save results' , 500 );
2022-07-22 18:00:42 +12:00
}
2019-10-16 01:48:19 +13:00
2022-07-22 18:00:42 +12:00
$response -> dynamic ( new Document ([ 'result' => $route -> getMethod () . ':' . $route -> getPath () . ':passed' ]), Response :: MODEL_MOCK );
});