1
0
Fork 0
mirror of synced 2024-08-20 20:51:40 +12:00
appwrite/app/controllers/mock.php

242 lines
8.7 KiB
PHP
Raw Normal View History

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;
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;
use Utopia\Validator\Text;
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-11-14 21:59:46 +13:00
use Utopia\VCS\Adapter\Git\GitHub;
use Utopia\Database\Helpers\Permission;
use Utopia\Database\Helpers\Role;
2021-05-07 03:45:32 +12:00
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)
->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
$responseJson = [
'access_token' => '123456',
'refresh_token' => 'tuvwxyz',
'expires_in' => 14400
];
2022-05-24 02:54:50 +12:00
if ($grantType === 'authorization_code') {
if ($code !== 'abcdef') {
2022-08-14 18:56:12 +12:00
throw new Exception(Exception::GENERAL_MOCK, 'Invalid token');
}
$response->json($responseJson);
2022-05-24 02:54:50 +12:00
} elseif ($grantType === 'refresh_token') {
if ($refreshToken !== 'tuvwxyz') {
2022-08-14 18:56:12 +12:00
throw new Exception(Exception::GENERAL_MOCK, 'Invalid refresh token');
}
2020-07-01 09:38:06 +12:00
$response->json($responseJson);
} else {
2022-08-14 18:56:12 +12:00
throw new Exception(Exception::GENERAL_MOCK, 'Invalid grant type');
}
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',
'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-11-14 21:59:46 +13:00
App::get('/v1/mock/github/callback')
->desc('Create installation document using GitHub installation id')
->groups(['mock', 'api', 'vcs'])
->label('scope', 'public')
->label('docs', false)
->param('providerInstallationId', '', new UID(), 'GitHub installation ID')
->param('projectId', '', new UID(), 'Project ID of the project where app is to be installed')
->inject('gitHub')
->inject('project')
->inject('response')
->inject('dbForConsole')
->action(function (string $providerInstallationId, string $projectId, GitHub $github, Document $project, Response $response, Database $dbForConsole) {
$isDevelopment = App::getEnv('_APP_ENV', 'development') === 'development';
if (!$isDevelopment) {
throw new Exception(Exception::GENERAL_NOT_IMPLEMENTED);
}
$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);
}
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,
'personal' => false
]);
$installation = $dbForConsole->createDocument('installations', $installation);
}
$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);
});