1
0
Fork 0
mirror of synced 2024-05-17 19:22:34 +12:00
appwrite/app/controllers/mock.php

530 lines
20 KiB
PHP
Raw Normal View History

2019-10-15 07:37:59 +13:00
<?php
global $utopia, $request, $response;
2021-03-05 19:40:29 +13:00
use Appwrite\Database\Document;
use Appwrite\Network\Validator\Host;
2021-03-05 18:30:34 +13:00
use Appwrite\Utopia\Response;
2020-06-29 05:31:21 +12:00
use Utopia\App;
2019-10-19 12:58:17 +13:00
use Utopia\Validator\ArrayList;
use Utopia\Validator\Numeric;
use Utopia\Validator\Text;
2021-01-22 21:28:33 +13:00
use Utopia\Storage\Validator\File;
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')
->param('y', '', new Numeric(), 'Sample numeric param')
2020-09-11 02:40:14 +12:00
->param('z', null, new ArrayList(new Text(256)), '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')
->param('y', '', new Numeric(), 'Sample numeric param')
2020-09-11 02:40:14 +12:00
->param('z', null, new ArrayList(new Text(256)), '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')
->param('y', '', new Numeric(), 'Sample numeric param')
2020-09-11 02:40:14 +12:00
->param('z', null, new ArrayList(new Text(256)), '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')
->param('y', '', new Numeric(), 'Sample numeric param')
2020-09-11 02:40:14 +12:00
->param('z', null, new ArrayList(new Text(256)), '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')
->param('y', '', new Numeric(), 'Sample numeric param')
2020-09-11 02:40:14 +12:00
->param('z', null, new ArrayList(new Text(256)), '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)
2020-09-11 02:40:14 +12:00
->param('x', '', new Text(100), 'Sample string param')
->param('y', '', new Numeric(), 'Sample numeric param')
2020-09-11 02:40:14 +12:00
->param('z', null, new ArrayList(new Text(256)), '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/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)
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')
->param('y', '', new Numeric(), 'Sample numeric param')
2020-09-11 02:40:14 +12:00
->param('z', null, new ArrayList(new Text(256)), '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/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)
2020-09-11 02:40:14 +12:00
->param('x', '', new Text(100), 'Sample string param')
->param('y', '', new Numeric(), 'Sample numeric param')
2020-09-11 02:40:14 +12:00
->param('z', null, new ArrayList(new Text(256)), '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/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)
2020-09-11 02:40:14 +12:00
->param('x', '', new Text(100), 'Sample string param')
->param('y', '', new Numeric(), 'Sample numeric param')
2020-09-11 02:40:14 +12:00
->param('z', null, new ArrayList(new Text(256)), '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/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)
2020-09-11 02:40:14 +12:00
->param('x', '', new Text(100), 'Sample string param')
->param('y', '', new Numeric(), 'Sample numeric param')
2020-09-11 02:40:14 +12:00
->param('z', null, new ArrayList(new Text(256)), 'Sample array param')
2020-07-01 09:38:06 +12:00
->action(function ($x, $y, $z) {
});
2019-10-15 07:37:59 +13:00
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')
->action(function ($response) {
/** @var Utopia\Swoole\Request $request */
$response
->setContentType('text/plain')
->addHeader('Content-Disposition', 'attachment; filename="test.txt"')
->addHeader('Expires', \date('D, d M Y H:i:s', \time() + (60 * 60 * 24 * 45)).' GMT') // 45 days cache
->addHeader('X-Peak', \memory_get_peak_usage())
->send("Download test passed.")
;
});
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')
->param('y', '', new Numeric(), 'Sample numeric param')
2020-09-11 02:40:14 +12:00
->param('z', null, new ArrayList(new Text(256)), 'Sample array param')
->param('file', [], new File(), 'Sample file param', false)
2020-12-27 01:19:53 +13:00
->inject('request')
2020-07-01 09:38:06 +12:00
->action(function ($x, $y, $z, $file, $request) {
2020-08-30 18:51:44 +12:00
/** @var Utopia\Swoole\Request $request */
2020-07-01 09:38:06 +12:00
$file = $request->getFiles('file');
$file['tmp_name'] = (\is_array($file['tmp_name'])) ? $file['tmp_name'] : [$file['tmp_name']];
$file['name'] = (\is_array($file['name'])) ? $file['name'] : [$file['name']];
$file['size'] = (\is_array($file['size'])) ? $file['size'] : [$file['size']];
foreach ($file['name'] as $i => $name) {
if ($name !== 'file.png') {
throw new Exception('Wrong file name', 400);
2020-02-13 21:04:17 +13:00
}
2020-07-01 09:38:06 +12:00
}
2020-02-13 21:04:17 +13:00
2020-07-01 09:38:06 +12:00
foreach ($file['size'] as $i => $size) {
if ($size !== 38756) {
throw new Exception('Wrong file size', 400);
2020-02-13 21:04:17 +13:00
}
2020-07-01 09:38:06 +12:00
}
2019-10-20 01:52:17 +13:00
2020-07-01 09:38:06 +12:00
foreach ($file['tmp_name'] as $i => $tmpName) {
if (\md5(\file_get_contents($tmpName)) !== 'd80e7e6999a3eb2ae0d631a96fe135a4') {
throw new Exception('Wrong file uploaded', 400);
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)
->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')
2020-07-01 09:38:06 +12:00
->action(function ($response) {
2020-08-30 18:51:44 +12:00
/** @var Appwrite\Utopia\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')
2020-07-01 09:38:06 +12:00
->action(function ($response) {
2020-08-30 18:51:44 +12:00
/** @var Appwrite\Utopia\Response $response */
2020-07-01 09:38:06 +12:00
$response->addCookie('cookieName', 'cookieValue', \time() + 31536000, '/', 'localhost', 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')
2020-07-01 09:38:06 +12:00
->action(function ($request) {
2020-08-30 18:51:44 +12:00
/** @var Utopia\Swoole\Request $request */
2020-07-01 09:38:06 +12:00
if ($request->getCookie('cookieName', '') !== 'cookieValue') {
throw new Exception('Missing cookie value', 400);
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')
2020-07-01 09:38:06 +12:00
->action(function ($response) {
2020-08-30 18:51:44 +12:00
/** @var Appwrite\Utopia\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
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 () {
throw new Exception('Mock 400 error', 400);
});
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 () {
throw new Exception('Mock 500 error', 500);
});
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)
->label('sdk.response.model', Response::MODEL_ANY)
2021-05-07 03:45:32 +12:00
->label('sdk.mock', true)
->inject('response')
->action(function ($response) {
/** @var Appwrite\Utopia\Response $response */
$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')
->action(function ($client_id, $redirectURI, $scope, $state, $response) {
2020-08-30 18:51:44 +12:00
/** @var Appwrite\Utopia\Response $response */
2020-07-01 09:38:06 +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('redirect_uri', '', new Host(['localhost']), 'OAuth2 Redirect URI.')
->param('client_secret', '', new Text(100), 'OAuth2 scope list.')
->param('code', '', new Text(100), 'OAuth2 state.')
2020-12-27 01:19:53 +13:00
->inject('response')
2021-01-04 12:29:04 +13:00
->action(function ($client_id, $redirectURI, $client_secret, $code, $response) {
2020-08-30 18:51:44 +12:00
/** @var Appwrite\Utopia\Response $response */
2020-01-14 01:23:45 +13:00
2020-10-28 04:33:13 +13:00
if ($client_id != '1') {
2020-07-01 09:38:06 +12:00
throw new Exception('Invalid client ID');
}
2020-01-14 01:23:45 +13:00
2020-10-28 04:33:13 +13:00
if ($client_secret != '123456') {
2020-07-01 09:38:06 +12:00
throw new Exception('Invalid client secret');
}
2020-01-14 01:23:45 +13:00
2020-07-01 09:38:06 +12:00
if ($code != 'abcdef') {
throw new Exception('Invalid token');
2020-01-14 01:23:45 +13:00
}
2020-07-01 09:38:06 +12:00
$response->json(['access_token' => '123456']);
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')
2020-07-01 09:38:06 +12:00
->action(function ($token, $response) {
2020-08-30 18:51:44 +12:00
/** @var Appwrite\Utopia\Response $response */
2020-01-14 01:23:45 +13:00
2020-07-01 09:38:06 +12:00
if ($token != '123456') {
throw new Exception('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',
'email' => 'user@localhost.test',
]);
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')
2020-07-01 09:38:06 +12:00
->action(function ($response) {
2020-08-30 18:51:44 +12:00
/** @var Appwrite\Utopia\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')
2020-07-01 09:38:06 +12:00
->action(function ($response) {
2020-08-30 18:51:44 +12:00
/** @var Appwrite\Utopia\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
App::shutdown(function($utopia, $response, $request) {
/** @var Utopia\App $utopia */
2020-08-30 18:51:44 +12:00
/** @var Utopia\Swoole\Request $request */
/** @var Appwrite\Utopia\Response $response */
2020-01-14 04:15:52 +13:00
2020-06-30 09:43:34 +12:00
$result = [];
2019-10-15 07:37:59 +13:00
$route = $utopia->match($request);
2020-02-20 01:41:23 +13:00
$path = APP_STORAGE_CACHE.'/tests.json';
2020-06-20 23:20:49 +12:00
$tests = (\file_exists($path)) ? \json_decode(\file_get_contents($path), true) : [];
2019-10-15 07:37:59 +13:00
2020-06-25 08:59:04 +12:00
if (!\is_array($tests)) {
2019-10-15 07:37:59 +13:00
throw new Exception('Failed to read results', 500);
}
$result[$route->getMethod() . ':' . $route->getURL()] = true;
2020-06-20 23:20:49 +12:00
$tests = \array_merge($tests, $result);
2019-10-15 07:37:59 +13:00
2020-06-25 08:59:04 +12:00
if (!\file_put_contents($path, \json_encode($tests), LOCK_EX)) {
2019-10-15 07:37:59 +13:00
throw new Exception('Failed to save resutls', 500);
}
2019-10-16 01:48:19 +13:00
2021-03-05 19:40:29 +13:00
$response->dynamic(new Document(['result' => $route->getMethod() . ':' . $route->getURL() . ':passed']), Response::MODEL_MOCK);
2020-07-01 09:38:06 +12:00
}, ['utopia', 'response', 'request'], 'mock');