diff --git a/app/config/collections/platform.php b/app/config/collections/platform.php index 7496b7a9a7..f93d82456e 100644 --- a/app/config/collections/platform.php +++ b/app/config/collections/platform.php @@ -342,6 +342,17 @@ $platformCollections = [ 'array' => true, 'filters' => [], ], + [ + '$id' => ID::custom('onboarding'), + 'type' => Database::VAR_STRING, + 'format' => '', + 'size' => 65536, + 'signed' => true, + 'required' => false, + 'default' => [], + 'array' => false, + 'filters' => ['json'], + ], [ '$id' => 'status', 'type' => Database::VAR_STRING, diff --git a/app/config/onboarding.php b/app/config/onboarding.php new file mode 100644 index 0000000000..014301a72e --- /dev/null +++ b/app/config/onboarding.php @@ -0,0 +1,76 @@ + true, + 'project.createAndroidPlatform' => true, + 'project.createApplePlatform' => true, + 'project.createWindowsPlatform' => true, + 'project.createLinuxPlatform' => true, + // Connect — API key + 'project.createKey' => true, + + // Auth + 'users.create' => true, + 'account.create' => true, + 'account.createAnonymousSession' => true, + 'teams.create' => true, + + // Databases — tablesDB + 'tablesDB.create' => true, + 'tablesDB.createTable' => true, + 'tablesDB.createRow' => true, + // Databases — documentsDB + 'documentsDB.create' => true, + 'documentsDB.createCollection' => true, + 'documentsDB.createDocument' => true, + // Databases — legacy + 'databases.create' => true, + 'databases.createCollection' => true, + 'databases.createDocument' => true, + + // Storage + 'storage.createBucket' => true, + 'storage.createFile' => true, + + // Functions + 'functions.create' => true, + // Deployment-creation SDK methods; OR them in the UI for a single + // "created a deployment" milestone. + 'functions.createDeployment' => true, + 'functions.createTemplateDeployment' => true, + 'functions.createVcsDeployment' => true, + 'functions.updateFunctionDeployment' => true, + + // Messaging + 'messaging.createTopic' => true, + 'messaging.createMailgunProvider' => true, + 'messaging.createSendgridProvider' => true, + 'messaging.createSesProvider' => true, + 'messaging.createResendProvider' => true, + 'messaging.createSmtpProvider' => true, + 'messaging.createSMTPProvider' => true, + 'messaging.createMsg91Provider' => true, + 'messaging.createTelesignProvider' => true, + 'messaging.createTextmagicProvider' => true, + 'messaging.createTwilioProvider' => true, + 'messaging.createVonageProvider' => true, + 'messaging.createFcmProvider' => true, + 'messaging.createFCMProvider' => true, + 'messaging.createApnsProvider' => true, + 'messaging.createAPNSProvider' => true, + + // Sites + 'sites.create' => true, + // Deployment-creation SDK methods; OR them in the UI for a single + // "created a deployment" milestone. + 'sites.createDeployment' => true, + 'sites.createTemplateDeployment' => true, + 'sites.createVcsDeployment' => true, + 'sites.updateSiteDeployment' => true, +]; diff --git a/app/config/roles.php b/app/config/roles.php index d5ae7c1331..04718f65d2 100644 --- a/app/config/roles.php +++ b/app/config/roles.php @@ -107,6 +107,8 @@ $admins = [ 'tokens.write', 'schedules.read', 'schedules.write', + 'stages.read', + 'stages.write', 'insights.read', 'insights.write', 'reports.read', diff --git a/app/config/scopes/project.php b/app/config/scopes/project.php index cd50abd57b..bfafebb5fb 100644 --- a/app/config/scopes/project.php +++ b/app/config/scopes/project.php @@ -75,6 +75,16 @@ return [ "Access to create, update, and delete project\'s templates", "category" => "Project", ], + "stages.read" => [ + "description" => + "Access to read project\'s stages", + "category" => "Project", + ], + "stages.write" => [ + "description" => + "Access to update project\'s stages", + "category" => "Project", + ], "oauth2.read" => [ "description" => "Access to read project\'s OAuth2 configuration", diff --git a/app/controllers/shared/api.php b/app/controllers/shared/api.php index 0eec29b7fa..791ab0fe0e 100644 --- a/app/controllers/shared/api.php +++ b/app/controllers/shared/api.php @@ -24,6 +24,7 @@ use Appwrite\Usage\Context; use Appwrite\Utopia\Database\Documents\User; use Appwrite\Utopia\Request; use Appwrite\Utopia\Response; +use Throwable; use Utopia\Abuse\Abuse; use Utopia\Bus\Bus; use Utopia\Cache\Adapter\Filesystem; @@ -1085,6 +1086,95 @@ Http::shutdown() } }); +Http::shutdown() + ->groups(['api']) + ->inject('route') + ->inject('response') + ->inject('project') + ->inject('user') + ->inject('dbForPlatform') + ->inject('authorization') + ->inject('apiKey') + ->inject('mode') + ->action(function (Route $route, Response $response, Document $project, User $user, Database $dbForPlatform, Authorization $authorization, ?Key $apiKey, string $mode) { + /** + * Persist completed onboarding stage after usage shutdown so a schema/write failure here + * cannot suppress RequestCompleted or usage metrics on the same request. + */ + $statusCode = $response->getStatusCode(); + if ($statusCode < 200 || $statusCode >= 300 || $project->getId() === 'console') { + return; + } + + $sdkLabel = $route->getLabel('sdk', false); + if ($sdkLabel === false || $sdkLabel === null) { + return; + } + + /** @var array $onboarding */ + $onboarding = Config::getParam('onboarding', []); + if ($onboarding === []) { + return; + } + + $method = null; + if ($sdkLabel instanceof Method) { + $key = $sdkLabel->getNamespace() . '.' . $sdkLabel->getMethodName(); + if (isset($onboarding[$key])) { + $method = $key; + } + } elseif (\is_array($sdkLabel)) { + foreach ($sdkLabel as $sdkMethod) { + if (! $sdkMethod instanceof Method) { + continue; + } + $key = $sdkMethod->getNamespace() . '.' . $sdkMethod->getMethodName(); + if (isset($onboarding[$key])) { + $method = $key; + break; + } + } + } + + if ($method === null) { + return; + } + + $byMethod = $project->getAttribute('onboarding', []); + $status = \is_array($byMethod) ? ($byMethod[$method]['status'] ?? null) : null; + if ($status === ONBOARDING_STATUS_COMPLETED || $status === ONBOARDING_STATUS_SKIPPED) { + return; + } + + if (! \is_array($byMethod)) { + $byMethod = []; + } + + $actorType = ($apiKey !== null && $apiKey->getRole() === User::ROLE_KEYS) + ? match ($apiKey->getType()) { + API_KEY_ACCOUNT => ACTOR_TYPE_KEY_ACCOUNT, + API_KEY_ORGANIZATION => ACTOR_TYPE_KEY_ORGANIZATION, + API_KEY_STANDARD, API_KEY_EPHEMERAL => ACTOR_TYPE_KEY_PROJECT, + default => ACTOR_TYPE_KEY_PROJECT, + } + : (! $user->isEmpty() + ? ($mode === APP_MODE_ADMIN ? ACTOR_TYPE_ADMIN : ACTOR_TYPE_USER) + : ACTOR_TYPE_GUEST); + $byMethod[$method] = [ + 'status' => ONBOARDING_STATUS_COMPLETED, + 'at' => DateTime::now(), + 'actorType' => $actorType, + ]; + + try { + $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ + 'onboarding' => $byMethod, + ]))); + } catch (Throwable) { + // Missing `onboarding` attribute on upgraded installs must not break the request lifecycle. + } + }); + Http::init() ->groups(['usage']) ->action(function () { diff --git a/app/init/configs.php b/app/init/configs.php index 360a7abc34..f45ee1a7fe 100644 --- a/app/init/configs.php +++ b/app/init/configs.php @@ -26,6 +26,7 @@ Config::load('projectScopes', __DIR__ . '/../config/scopes/project.php', $config Config::load('organizationScopes', __DIR__ . '/../config/scopes/organization.php', $configAdapter); Config::load('accountScopes', __DIR__ . '/../config/scopes/account.php', $configAdapter); Config::load('services', __DIR__ . '/../config/services.php', $configAdapter); // List of services +Config::load('onboarding', __DIR__ . '/../config/onboarding.php', $configAdapter); // Project onboarding stages → routes Config::load('variables', __DIR__ . '/../config/variables.php', $configAdapter); // List of env variables Config::load('regions', __DIR__ . '/../config/regions.php', $configAdapter); // List of available regions Config::load('avatar-browsers', __DIR__ . '/../config/avatars/browsers.php', $configAdapter); diff --git a/app/init/constants.php b/app/init/constants.php index 8859937685..9efb6e3222 100644 --- a/app/init/constants.php +++ b/app/init/constants.php @@ -172,6 +172,12 @@ const ACTOR_TYPE_KEY_PROJECT = 'keyProject'; const ACTOR_TYPE_KEY_ACCOUNT = 'keyAccount'; const ACTOR_TYPE_KEY_ORGANIZATION = 'keyOrganization'; +/** + * Project onboarding stage status (stored per SDK method key under project.onboarding JSON). + */ +const ONBOARDING_STATUS_COMPLETED = 'completed'; +const ONBOARDING_STATUS_SKIPPED = 'skipped'; + /** * MFA */ diff --git a/app/init/models.php b/app/init/models.php index d0f665bb4e..efd2c13215 100644 --- a/app/init/models.php +++ b/app/init/models.php @@ -195,6 +195,7 @@ use Appwrite\Utopia\Response\Model\Schedule; use Appwrite\Utopia\Response\Model\Session; use Appwrite\Utopia\Response\Model\Site; use Appwrite\Utopia\Response\Model\Specification; +use Appwrite\Utopia\Response\Model\Stage; use Appwrite\Utopia\Response\Model\Subscriber; use Appwrite\Utopia\Response\Model\Table; use Appwrite\Utopia\Response\Model\Target; @@ -285,6 +286,7 @@ Response::setModel(new BaseList('Email Templates List', Response::MODEL_EMAIL_TE Response::setModel(new BaseList('Status List', Response::MODEL_HEALTH_STATUS_LIST, 'statuses', Response::MODEL_HEALTH_STATUS)); Response::setModel(new BaseList('Rule List', Response::MODEL_PROXY_RULE_LIST, 'rules', Response::MODEL_PROXY_RULE)); Response::setModel(new BaseList('Schedules List', Response::MODEL_SCHEDULE_LIST, 'schedules', Response::MODEL_SCHEDULE)); +Response::setModel(new BaseList('Stages List', Response::MODEL_STAGE_LIST, 'stages', Response::MODEL_STAGE, false, false)); Response::setModel(new BaseList('Locale codes list', Response::MODEL_LOCALE_CODE_LIST, 'localeCodes', Response::MODEL_LOCALE_CODE)); Response::setModel(new BaseList('Provider list', Response::MODEL_PROVIDER_LIST, 'providers', Response::MODEL_PROVIDER)); Response::setModel(new BaseList('Message list', Response::MODEL_MESSAGE_LIST, 'messages', Response::MODEL_MESSAGE)); @@ -507,6 +509,7 @@ Response::setModel(new Headers()); Response::setModel(new Specification()); Response::setModel(new Rule()); Response::setModel(new Schedule()); +Response::setModel(new Stage()); Response::setModel(new TemplateEmail()); Response::setModel(new ConsoleVariables()); Response::setModel(new ConsoleOAuth2ProviderParameter()); diff --git a/docs/references/projects/list-stages.md b/docs/references/projects/list-stages.md new file mode 100644 index 0000000000..3a2f6cc401 --- /dev/null +++ b/docs/references/projects/list-stages.md @@ -0,0 +1 @@ +Get the onboarding stages for the current project, including each stage’s SDK method key and status (for example pending, completed, or skipped). diff --git a/docs/references/projects/update-stage.md b/docs/references/projects/update-stage.md new file mode 100644 index 0000000000..9e6f43919e --- /dev/null +++ b/docs/references/projects/update-stage.md @@ -0,0 +1 @@ +Update an onboarding stage for the current project. Use this endpoint to skip a stage or leave it unchanged without performing the related API action. diff --git a/src/Appwrite/Platform/Modules/Projects/Http/Stages/Update.php b/src/Appwrite/Platform/Modules/Projects/Http/Stages/Update.php new file mode 100644 index 0000000000..c345712bc8 --- /dev/null +++ b/src/Appwrite/Platform/Modules/Projects/Http/Stages/Update.php @@ -0,0 +1,144 @@ +setHttpMethod(Action::HTTP_REQUEST_METHOD_PATCH) + ->setHttpPath('/v1/projects/:projectId/stages/:stageId') + ->desc('Update stage') + ->groups(['api', 'projects']) + ->label('scope', 'stages.write') + ->label('audits.event', 'stages.update') + ->label('audits.resource', 'project/{request.projectId}') + ->label('sdk', new Method( + namespace: 'projects', + group: 'stages', + name: 'updateStage', + description: '/docs/references/projects/update-stage.md', + auth: [AuthType::ADMIN], + responses: [ + new SDKResponse( + code: Response::STATUS_CODE_OK, + model: Response::MODEL_STAGE, + ) + ], + )) + ->param('projectId', '', new UID(), 'Project unique ID.') + ->param('stageId', '', new Text(128), 'SDK method key (namespace.method).') + ->param('skip', true, new Boolean(), 'Mark the stage as skipped.', true) + ->inject('response') + ->inject('dbForPlatform') + ->inject('apiKey') + ->inject('user') + ->inject('mode') + ->callback($this->action(...)); + } + + public function action(string $projectId, string $stageId, bool $skip, Response $response, Database $dbForPlatform, ?Key $apiKey, User $user, string $mode): void + { + $project = $dbForPlatform->getDocument('projects', $projectId); + + if ($project->isEmpty()) { + throw new Exception(Exception::PROJECT_NOT_FOUND); + } + + $this->assertOnboardingMethod($stageId); + + $byMethod = $project->getAttribute('onboarding', []); + if (! \is_array($byMethod)) { + $byMethod = []; + } + + $row = \is_array($byMethod[$stageId] ?? null) ? $byMethod[$stageId] : null; + + if ($skip) { + $prev = \is_array($row) ? ($row['status'] ?? '') : ''; + if ($prev !== ONBOARDING_STATUS_COMPLETED) { + $byMethod[$stageId] = [ + 'status' => ONBOARDING_STATUS_SKIPPED, + 'at' => DateTime::now(), + 'actorType' => $this->resolveActorType($apiKey, $user, $mode), + ]; + $project = $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ + 'onboarding' => $byMethod, + ])); + $byMethod = $project->getAttribute('onboarding', []); + $row = \is_array($byMethod[$stageId] ?? null) ? $byMethod[$stageId] : null; + } + } + + $response->dynamic(new Document($this->formatStageRow($stageId, $row)), Response::MODEL_STAGE); + } + + private function assertOnboardingMethod(string $method): void + { + if (! \array_key_exists($method, Config::getParam('onboarding', []))) { + throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'Unknown SDK method: ' . $method); + } + } + + /** + * @param array|null $row + * @return array + */ + private function formatStageRow(string $method, ?array $row): array + { + $status = \is_array($row) ? ($row['status'] ?? null) : null; + $at = \is_array($row) ? ($row['at'] ?? '') : ''; + $actorType = \is_array($row) ? ($row['actorType'] ?? '') : ''; + + return [ + 'id' => $method, + 'sdk' => $method, + 'status' => $status ?? 'pending', + 'at' => $at, + 'actorType' => $actorType, + ]; + } + + private function resolveActorType(?Key $apiKey, User $user, string $mode): string + { + if ($apiKey !== null && $apiKey->getRole() === User::ROLE_KEYS) { + return match ($apiKey->getType()) { + API_KEY_ACCOUNT => ACTOR_TYPE_KEY_ACCOUNT, + API_KEY_ORGANIZATION => ACTOR_TYPE_KEY_ORGANIZATION, + API_KEY_STANDARD, API_KEY_EPHEMERAL => ACTOR_TYPE_KEY_PROJECT, + default => ACTOR_TYPE_KEY_PROJECT, + }; + } + + if (! $user->isEmpty()) { + return $mode === APP_MODE_ADMIN ? ACTOR_TYPE_ADMIN : ACTOR_TYPE_USER; + } + + return ACTOR_TYPE_GUEST; + } +} diff --git a/src/Appwrite/Platform/Modules/Projects/Http/Stages/XList.php b/src/Appwrite/Platform/Modules/Projects/Http/Stages/XList.php new file mode 100644 index 0000000000..7fa6537152 --- /dev/null +++ b/src/Appwrite/Platform/Modules/Projects/Http/Stages/XList.php @@ -0,0 +1,96 @@ +setHttpMethod(Action::HTTP_REQUEST_METHOD_GET) + ->setHttpPath('/v1/projects/:projectId/stages') + ->desc('List stages') + ->groups(['api', 'projects']) + ->label('scope', 'stages.read') + ->label('sdk', new Method( + namespace: 'projects', + group: 'stages', + name: 'listStages', + description: '/docs/references/projects/list-stages.md', + auth: [AuthType::ADMIN], + responses: [ + new SDKResponse( + code: Response::STATUS_CODE_OK, + model: Response::MODEL_STAGE_LIST, + ) + ], + )) + ->param('projectId', '', new UID(), 'Project unique ID.') + ->inject('response') + ->inject('dbForPlatform') + ->callback($this->action(...)); + } + + public function action(string $projectId, Response $response, Database $dbForPlatform): void + { + $project = $dbForPlatform->getDocument('projects', $projectId); + + if ($project->isEmpty()) { + throw new Exception(Exception::PROJECT_NOT_FOUND); + } + + $response->dynamic(new Document([ + 'stages' => $this->buildStagesList($project), + ]), Response::MODEL_STAGE_LIST); + } + + /** + * @return array> + */ + private function buildStagesList(Document $project): array + { + $methods = \array_keys(Config::getParam('onboarding', [])); + $byMethod = $project->getAttribute('onboarding', []); + if (! \is_array($byMethod)) { + $byMethod = []; + } + + $out = []; + foreach ($methods as $method) { + $row = $byMethod[$method] ?? null; + $status = \is_array($row) ? ($row['status'] ?? null) : null; + + $at = \is_array($row) ? ($row['at'] ?? '') : ''; + $actorType = \is_array($row) ? ($row['actorType'] ?? '') : ''; + + $out[] = [ + 'id' => $method, + 'sdk' => $method, + 'status' => $status ?? 'pending', + 'at' => $at, + 'actorType' => $actorType, + ]; + } + + return $out; + } +} diff --git a/src/Appwrite/Platform/Modules/Projects/Services/Http.php b/src/Appwrite/Platform/Modules/Projects/Services/Http.php index 8275e664d5..56e8c26d73 100644 --- a/src/Appwrite/Platform/Modules/Projects/Services/Http.php +++ b/src/Appwrite/Platform/Modules/Projects/Services/Http.php @@ -14,6 +14,8 @@ use Appwrite\Platform\Modules\Projects\Http\Projects\XList as ListProjects; use Appwrite\Platform\Modules\Projects\Http\Schedules\Create as CreateSchedule; use Appwrite\Platform\Modules\Projects\Http\Schedules\Get as GetSchedule; use Appwrite\Platform\Modules\Projects\Http\Schedules\XList as ListSchedules; +use Appwrite\Platform\Modules\Projects\Http\Stages\Update as UpdateStages; +use Appwrite\Platform\Modules\Projects\Http\Stages\XList as ListStages; use Utopia\Platform\Service; class Http extends Service @@ -35,5 +37,8 @@ class Http extends Service $this->addAction(CreateSchedule::getName(), new CreateSchedule()); $this->addAction(GetSchedule::getName(), new GetSchedule()); $this->addAction(ListSchedules::getName(), new ListSchedules()); + + $this->addAction(ListStages::getName(), new ListStages()); + $this->addAction(UpdateStages::getName(), new UpdateStages()); } } diff --git a/src/Appwrite/Utopia/Response.php b/src/Appwrite/Utopia/Response.php index 45d0b5dc25..e8701b315e 100644 --- a/src/Appwrite/Utopia/Response.php +++ b/src/Appwrite/Utopia/Response.php @@ -252,6 +252,8 @@ class Response extends SwooleResponse // Project public const MODEL_PROJECT = 'project'; public const MODEL_PROJECT_LIST = 'projectList'; + public const MODEL_STAGE = 'stage'; + public const MODEL_STAGE_LIST = 'stageList'; public const MODEL_PROJECT_AUTH_METHOD = 'projectAuthMethod'; public const MODEL_PROJECT_SERVICE = 'projectService'; public const MODEL_PROJECT_PROTOCOL = 'projectProtocol'; diff --git a/src/Appwrite/Utopia/Response/Model/Project.php b/src/Appwrite/Utopia/Response/Model/Project.php index 3d5fd76de3..c4c2fe0607 100644 --- a/src/Appwrite/Utopia/Response/Model/Project.php +++ b/src/Appwrite/Utopia/Response/Model/Project.php @@ -153,6 +153,12 @@ class Project extends Model 'default' => 'active', 'example' => 'active', ]) + ->addRule('onboarding', [ + 'type' => self::TYPE_JSON, + 'description' => 'Stage progress (completed or skipped) with timestamps and actor types, keyed by stage id.', + 'default' => [], + 'example' => [], + ]) // Resource: Auth methods ->addRule('authMethods', [ diff --git a/src/Appwrite/Utopia/Response/Model/Stage.php b/src/Appwrite/Utopia/Response/Model/Stage.php new file mode 100644 index 0000000000..2b39499f8f --- /dev/null +++ b/src/Appwrite/Utopia/Response/Model/Stage.php @@ -0,0 +1,61 @@ +addRule('id', [ + 'type' => self::TYPE_STRING, + 'description' => 'Stage ID.', + 'default' => '', + 'example' => 'tablesDB.create', + ]) + ->addRule('sdk', [ + 'type' => self::TYPE_STRING, + 'description' => 'SDK method key (namespace.name) for this stage.', + 'default' => '', + 'example' => 'tablesDB.create', + ]) + ->addRule('status', [ + 'type' => self::TYPE_STRING, + 'description' => 'Stage status.', + 'default' => 'pending', + 'example' => 'completed', + ]) + ->addRule('at', [ + 'type' => self::TYPE_DATETIME, + 'description' => 'When the stage was completed or skipped, in ISO 8601 format.', + 'default' => '', + 'example' => self::TYPE_DATETIME_EXAMPLE, + ]) + ->addRule('actorType', [ + 'type' => self::TYPE_STRING, + 'description' => 'Actor type when the stage was recorded.', + 'default' => '', + 'example' => 'user', + ]) + ; + } + + public function getName(): string + { + return 'Stage'; + } + + public function getType(): string + { + return Response::MODEL_STAGE; + } + + public function filter(Document $document): Document + { + return $document; + } +} diff --git a/tests/e2e/Services/Project/StagesBase.php b/tests/e2e/Services/Project/StagesBase.php new file mode 100644 index 0000000000..471fdd9acb --- /dev/null +++ b/tests/e2e/Services/Project/StagesBase.php @@ -0,0 +1,140 @@ +getProject()['$id']; + + $response = $this->listStages($projectId); + + $this->assertSame(200, $response['headers']['status-code']); + $this->assertIsArray($response['body']['stages']); + $this->assertCount(\count(Config::getParam('onboarding', [])), $response['body']['stages']); + + $ids = array_column($response['body']['stages'], 'id'); + $this->assertContains('tablesDB.create', $ids); + $this->assertContains('storage.createBucket', $ids); + $this->assertContains('functions.create', $ids); + + foreach ($response['body']['stages'] as $stage) { + $this->assertSame($stage['id'], $stage['sdk']); + $this->assertArrayHasKey('status', $stage); + $this->assertArrayHasKey('at', $stage); + $this->assertArrayHasKey('actorType', $stage); + } + } + + public function testListProjectStagesWithoutAuthentication(): void + { + $response = $this->listStages($this->getProject()['$id'], false); + + $this->assertSame(401, $response['headers']['status-code']); + } + + // ========================================================================= + // Update stage (skip) tests + // ========================================================================= + + public function testUpdateProjectStageSkip(): void + { + $projectId = $this->getProject()['$id']; + $method = 'tablesDB.create'; + + $response = $this->updateStage($projectId, $method, true); + + $this->assertSame(200, $response['headers']['status-code']); + $this->assertSame($method, $response['body']['id']); + $this->assertSame($method, $response['body']['sdk']); + $this->assertSame(ONBOARDING_STATUS_SKIPPED, $response['body']['status']); + $this->assertNotEmpty($response['body']['at']); + $this->assertSame(ACTOR_TYPE_USER, $response['body']['actorType']); + + $dateValidator = new DatetimeValidator(); + $this->assertSame(true, $dateValidator->isValid($response['body']['at'])); + + $list = $this->listStages($projectId); + $this->assertSame(200, $list['headers']['status-code']); + $databaseStage = null; + foreach ($list['body']['stages'] as $stage) { + if ($stage['id'] === $method) { + $databaseStage = $stage; + break; + } + } + $this->assertNotNull($databaseStage); + $this->assertSame(ONBOARDING_STATUS_SKIPPED, $databaseStage['status']); + $this->assertSame(ACTOR_TYPE_USER, $databaseStage['actorType']); + + $response = $this->updateStage($projectId, 'unknown.method', true); + $this->assertSame(400, $response['headers']['status-code']); + $this->assertSame(Exception::GENERAL_ARGUMENT_INVALID, $response['body']['type']); + } + + public function testUpdateProjectStageSkipFalseLeavesPending(): void + { + $projectId = $this->getProject()['$id']; + $method = 'storage.createBucket'; + + $response = $this->updateStage($projectId, $method, false); + + $this->assertSame(200, $response['headers']['status-code']); + $this->assertSame($method, $response['body']['id']); + $this->assertSame('pending', $response['body']['status']); + } + + public function testUpdateProjectStageWithoutAuthentication(): void + { + $response = $this->updateStage($this->getProject()['$id'], 'tablesDB.create', true, false); + + $this->assertSame(401, $response['headers']['status-code']); + } + + // ========================================================================= + // Helpers + // ========================================================================= + + protected function listStages(string $projectId, bool $authenticated = true): mixed + { + // Stages live under /projects (console scope); use the root console session. + $headers = [ + 'content-type' => 'application/json', + 'x-appwrite-project' => 'console', + ]; + + if ($authenticated) { + $headers['origin'] = 'http://localhost'; + $headers['cookie'] = 'a_session_console=' . $this->getRoot()['session']; + } + + return $this->client->call(Client::METHOD_GET, '/projects/' . $projectId . '/stages', $headers, []); + } + + protected function updateStage(string $projectId, string $stageId, bool $skip, bool $authenticated = true): mixed + { + $headers = [ + 'content-type' => 'application/json', + 'x-appwrite-project' => 'console', + ]; + + if ($authenticated) { + $headers['origin'] = 'http://localhost'; + $headers['cookie'] = 'a_session_console=' . $this->getRoot()['session']; + } + + return $this->client->call(Client::METHOD_PATCH, '/projects/' . $projectId . '/stages/' . rawurlencode($stageId), $headers, [ + 'skip' => $skip, + ]); + } +} diff --git a/tests/e2e/Services/Project/StagesConsoleClientTest.php b/tests/e2e/Services/Project/StagesConsoleClientTest.php new file mode 100644 index 0000000000..51ba5fa3e4 --- /dev/null +++ b/tests/e2e/Services/Project/StagesConsoleClientTest.php @@ -0,0 +1,16 @@ +