1
0
Fork 0
mirror of synced 2024-06-01 18:39:57 +12:00
appwrite/app/controllers/mock.php

376 lines
14 KiB
PHP
Raw Normal View History

2019-10-15 07:37:59 +13:00
<?php
global $utopia, $request, $response;
2020-06-29 05:31:21 +12:00
use Utopia\App;
use Utopia\Response;
2019-10-15 07:37:59 +13:00
use Utopia\Validator\Numeric;
use Utopia\Validator\Text;
2019-10-19 12:58:17 +13:00
use Utopia\Validator\ArrayList;
2020-01-14 01:23:45 +13:00
use Utopia\Validator\Host;
2020-06-12 07:36:10 +12:00
use Appwrite\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')
2019-10-15 07:37:59 +13:00
->desc('Mock a get request for SDK tests')
2020-08-30 18:51:44 +12:00
->groups(['mock'])
2019-10-15 07:37:59 +13:00
->label('scope', 'public')
->label('sdk.namespace', 'foo')
->label('sdk.method', 'get')
->label('sdk.description', 'Mock a get request for SDK tests')
2020-07-15 04:29:49 +12:00
->label('sdk.mock', true)
2019-10-15 07:37:59 +13:00
->param('x', '', function () { return new Text(100); }, 'Sample string param')
->param('y', '', function () { return new Numeric(); }, 'Sample numeric param')
2019-10-19 12:58:17 +13:00
->param('z', null, function () { return 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')
2019-10-15 07:37:59 +13:00
->desc('Mock a post request for SDK tests')
2020-08-30 18:51:44 +12:00
->groups(['mock'])
2019-10-15 07:37:59 +13:00
->label('scope', 'public')
->label('sdk.namespace', 'foo')
->label('sdk.method', 'post')
->label('sdk.description', 'Mock a post request for SDK tests')
2020-07-15 04:29:49 +12:00
->label('sdk.mock', true)
2019-10-15 07:37:59 +13:00
->param('x', '', function () { return new Text(100); }, 'Sample string param')
->param('y', '', function () { return new Numeric(); }, 'Sample numeric param')
2019-10-19 12:58:17 +13:00
->param('z', null, function () { return 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')
2019-10-15 07:37:59 +13:00
->desc('Mock a patch request for SDK tests')
2020-08-30 18:51:44 +12:00
->groups(['mock'])
2019-10-15 07:37:59 +13:00
->label('scope', 'public')
->label('sdk.namespace', 'foo')
->label('sdk.method', 'patch')
->label('sdk.description', 'Mock a get request for SDK tests')
2020-07-15 04:29:49 +12:00
->label('sdk.mock', true)
2019-10-15 07:37:59 +13:00
->param('x', '', function () { return new Text(100); }, 'Sample string param')
->param('y', '', function () { return new Numeric(); }, 'Sample numeric param')
2019-10-19 12:58:17 +13:00
->param('z', null, function () { return 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')
2019-10-15 07:37:59 +13:00
->desc('Mock a put request for SDK tests')
2020-08-30 18:51:44 +12:00
->groups(['mock'])
2019-10-15 07:37:59 +13:00
->label('scope', 'public')
->label('sdk.namespace', 'foo')
->label('sdk.method', 'put')
->label('sdk.description', 'Mock a put request for SDK tests')
2020-07-15 04:29:49 +12:00
->label('sdk.mock', true)
2019-10-15 07:37:59 +13:00
->param('x', '', function () { return new Text(100); }, 'Sample string param')
->param('y', '', function () { return new Numeric(); }, 'Sample numeric param')
2019-10-19 12:58:17 +13:00
->param('z', null, function () { return 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')
2019-10-15 07:37:59 +13:00
->desc('Mock a delete request for SDK tests')
2020-08-30 18:51:44 +12:00
->groups(['mock'])
2019-10-15 07:37:59 +13:00
->label('scope', 'public')
->label('sdk.namespace', 'foo')
->label('sdk.method', 'delete')
->label('sdk.description', 'Mock a delete request for SDK tests')
2020-07-15 04:29:49 +12:00
->label('sdk.mock', true)
2019-10-15 07:37:59 +13:00
->param('x', '', function () { return new Text(100); }, 'Sample string param')
->param('y', '', function () { return new Numeric(); }, 'Sample numeric param')
2019-10-19 12:58:17 +13:00
->param('z', null, function () { return 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')
2019-10-15 07:37:59 +13:00
->desc('Mock a get request for SDK tests')
2020-08-30 18:51:44 +12:00
->groups(['mock'])
2019-10-15 07:37:59 +13:00
->label('scope', 'public')
->label('sdk.namespace', 'bar')
->label('sdk.method', 'get')
->label('sdk.description', 'Mock a get request for SDK tests')
2020-07-15 04:29:49 +12:00
->label('sdk.mock', true)
2019-10-15 07:37:59 +13:00
->param('x', '', function () { return new Text(100); }, 'Sample string param')
->param('y', '', function () { return new Numeric(); }, 'Sample numeric param')
2019-10-19 12:58:17 +13:00
->param('z', null, function () { return 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')
2019-10-15 07:37:59 +13:00
->desc('Mock a post request for SDK tests')
2020-08-30 18:51:44 +12:00
->groups(['mock'])
2019-10-15 07:37:59 +13:00
->label('scope', 'public')
->label('sdk.namespace', 'bar')
->label('sdk.method', 'post')
->label('sdk.description', 'Mock a post request for SDK tests')
2020-07-15 04:29:49 +12:00
->label('sdk.mock', true)
2019-10-15 07:37:59 +13:00
->param('x', '', function () { return new Text(100); }, 'Sample string param')
->param('y', '', function () { return new Numeric(); }, 'Sample numeric param')
2019-10-19 12:58:17 +13:00
->param('z', null, function () { return 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')
2019-10-15 07:37:59 +13:00
->desc('Mock a patch request for SDK tests')
2020-08-30 18:51:44 +12:00
->groups(['mock'])
2019-10-15 07:37:59 +13:00
->label('scope', 'public')
->label('sdk.namespace', 'bar')
->label('sdk.method', 'patch')
->label('sdk.description', 'Mock a get request for SDK tests')
2020-07-15 04:29:49 +12:00
->label('sdk.mock', true)
2019-10-15 07:37:59 +13:00
->param('x', '', function () { return new Text(100); }, 'Sample string param')
->param('y', '', function () { return new Numeric(); }, 'Sample numeric param')
2019-10-19 12:58:17 +13:00
->param('z', null, function () { return 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')
2019-10-15 07:37:59 +13:00
->desc('Mock a put request for SDK tests')
2020-08-30 18:51:44 +12:00
->groups(['mock'])
2019-10-15 07:37:59 +13:00
->label('scope', 'public')
->label('sdk.namespace', 'bar')
->label('sdk.method', 'put')
->label('sdk.description', 'Mock a put request for SDK tests')
2020-07-15 04:29:49 +12:00
->label('sdk.mock', true)
2019-10-15 07:37:59 +13:00
->param('x', '', function () { return new Text(100); }, 'Sample string param')
->param('y', '', function () { return new Numeric(); }, 'Sample numeric param')
2019-10-19 12:58:17 +13:00
->param('z', null, function () { return 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')
2019-10-15 07:37:59 +13:00
->desc('Mock a delete request for SDK tests')
2020-08-30 18:51:44 +12:00
->groups(['mock'])
2019-10-15 07:37:59 +13:00
->label('scope', 'public')
->label('sdk.namespace', 'bar')
->label('sdk.method', 'delete')
->label('sdk.description', 'Mock a delete request for SDK tests')
2020-07-15 04:29:49 +12:00
->label('sdk.mock', true)
2019-10-15 07:37:59 +13:00
->param('x', '', function () { return new Text(100); }, 'Sample string param')
->param('y', '', function () { return new Numeric(); }, 'Sample numeric param')
2019-10-19 12:58:17 +13:00
->param('z', null, function () { return 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/general/upload')
2019-10-20 01:52:17 +13:00
->desc('Mock a post request for SDK tests')
2020-08-30 18:51:44 +12:00
->groups(['mock'])
2019-10-15 07:37:59 +13:00
->label('scope', 'public')
2019-12-25 09:59:31 +13:00
->label('sdk.namespace', 'general')
2019-10-20 01:52:17 +13:00
->label('sdk.method', 'upload')
->label('sdk.description', 'Mock a delete request for SDK tests')
->label('sdk.consumes', 'multipart/form-data')
2020-07-15 04:29:49 +12:00
->label('sdk.mock', true)
2019-10-20 01:52:17 +13:00
->param('x', '', function () { return new Text(100); }, 'Sample string param')
->param('y', '', function () { return new Numeric(); }, 'Sample numeric param')
->param('z', null, function () { return new ArrayList(new Text(256)); }, 'Sample array param')
->param('file', [], function () { return new File(); }, 'Sample file param', false)
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-07-01 09:38:06 +12:00
}, ['request']);
2019-10-15 07:37:59 +13:00
2020-06-29 05:31:21 +12:00
App::get('/v1/mock/tests/general/redirect')
2019-12-25 09:59:31 +13:00
->desc('Mock a post request for SDK tests')
2020-08-30 18:51:44 +12:00
->groups(['mock'])
2019-12-25 09:59:31 +13:00
->label('scope', 'public')
->label('sdk.namespace', 'general')
->label('sdk.method', 'redirect')
->label('sdk.description', 'Mock a redirect request for SDK tests')
2020-07-15 04:29:49 +12:00
->label('sdk.mock', true)
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->redirect('/v1/mock/tests/general/redirected');
}, ['response']);
2019-12-25 09:59:31 +13:00
2020-06-29 05:31:21 +12:00
App::get('/v1/mock/tests/general/redirected')
2019-12-25 09:59:31 +13:00
->desc('Mock a post request for SDK tests')
2020-08-30 18:51:44 +12:00
->groups(['mock'])
2019-12-25 09:59:31 +13:00
->label('scope', 'public')
->label('sdk.namespace', 'general')
2019-12-25 19:31:09 +13:00
->label('sdk.method', 'redirected')
2019-12-25 09:59:31 +13:00
->label('sdk.description', 'Mock a redirected request for SDK tests')
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')
2020-04-05 07:16:00 +12:00
->desc('Mock a cookie request for SDK tests')
2020-08-30 18:51:44 +12:00
->groups(['mock'])
2020-04-05 07:16:00 +12:00
->label('scope', 'public')
->label('sdk.namespace', 'general')
->label('sdk.method', 'setCookie')
->label('sdk.description', 'Mock a set cookie request for SDK tests')
2020-07-15 04:29:49 +12:00
->label('sdk.mock', true)
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);
}, ['response']);
2020-04-05 07:16:00 +12:00
2020-06-29 05:31:21 +12:00
App::get('/v1/mock/tests/general/get-cookie')
2020-04-05 07:16:00 +12:00
->desc('Mock a cookie request for SDK tests')
2020-08-30 18:51:44 +12:00
->groups(['mock'])
2020-04-05 07:16:00 +12:00
->label('scope', 'public')
->label('sdk.namespace', 'general')
2020-04-05 07:32:38 +12:00
->label('sdk.method', 'getCookie')
->label('sdk.description', 'Mock a get cookie request for SDK tests')
2020-07-15 04:29:49 +12:00
->label('sdk.mock', true)
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-07-01 09:38:06 +12:00
}, ['request']);
2020-04-05 07:16:00 +12:00
2020-06-29 05:31:21 +12:00
App::get('/v1/mock/tests/general/empty')
2019-12-25 19:31:09 +13:00
->desc('Mock a post request for SDK tests')
2020-08-30 18:51:44 +12:00
->groups(['mock'])
2019-12-25 19:31:09 +13:00
->label('scope', 'public')
->label('sdk.namespace', 'general')
->label('sdk.method', 'empty')
->label('sdk.description', 'Mock a redirected request for SDK tests')
2020-07-15 04:29:49 +12:00
->label('sdk.mock', true)
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();
exit();
}, ['response']);
2019-12-25 19:31:09 +13:00
2020-06-29 05:31:21 +12:00
App::get('/v1/mock/tests/general/oauth2')
2020-02-17 00:41:03 +13:00
->desc('Mock an OAuth2 login route')
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-02-17 00:41:03 +13:00
->param('client_id', '', function () { return new Text(100); }, 'OAuth2 Client ID.')
2020-03-10 08:15:42 +13:00
->param('redirect_uri', '', function () { return new Host(['localhost']); }, 'OAuth2 Redirect URI.') // Important to deny an open redirect attack
2020-02-17 00:41:03 +13:00
->param('scope', '', function () { return new Text(100); }, 'OAuth2 scope list.')
->param('state', '', function () { return new Text(1024); }, 'OAuth2 state.')
2020-07-01 09:38:06 +12:00
->action(function ($clientId, $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]));
}, ['response']);
2020-01-14 01:23:45 +13:00
2020-06-29 05:31:21 +12:00
App::get('/v1/mock/tests/general/oauth2/token')
2020-02-17 00:41:03 +13:00
->desc('Mock an OAuth2 login route')
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-02-17 00:41:03 +13:00
->param('client_id', '', function () { return new Text(100); }, 'OAuth2 Client ID.')
2020-03-10 08:15:42 +13:00
->param('redirect_uri', '', function () { return new Host(['localhost']); }, 'OAuth2 Redirect URI.')
2020-02-17 00:41:03 +13:00
->param('client_secret', '', function () { return new Text(100); }, 'OAuth2 scope list.')
->param('code', '', function () { return new Text(100); }, 'OAuth2 state.')
2020-07-01 09:38:06 +12:00
->action(function ($clientId, $redirectURI, $clientSecret, $code, $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 ($clientId != '1') {
throw new Exception('Invalid client ID');
}
2020-01-14 01:23:45 +13:00
2020-07-01 09:38:06 +12:00
if ($clientSecret != '123456') {
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']);
}, ['response']);
2020-01-14 01:23:45 +13:00
2020-06-29 05:31:21 +12:00
App::get('/v1/mock/tests/general/oauth2/user')
2020-02-17 00:41:03 +13:00
->desc('Mock an OAuth2 user route')
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-02-17 00:41:03 +13:00
->param('token', '', function () { return new Text(100); }, 'OAuth2 Access Token.')
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',
]);
}, ['response']);
2020-01-14 01:23:45 +13:00
2020-06-29 05:31:21 +12:00
App::get('/v1/mock/tests/general/oauth2/success')
2020-01-14 04:15:52 +13:00
->label('scope', 'public')
2020-08-30 18:51:44 +12:00
->groups(['mock'])
2020-01-14 04:15:52 +13:00
->label('docs', false)
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',
]);
}, ['response']);
2020-01-14 04:15:52 +13:00
2020-06-29 05:31:21 +12:00
App::get('/v1/mock/tests/general/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-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',
]);
}, ['response']);
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
$response->json(['result' => $route->getMethod() . ':' . $route->getURL() . ':passed']);
2020-07-01 09:38:06 +12:00
}, ['utopia', 'response', 'request'], 'mock');