diff --git a/app/Http/Controllers/Backend/PlatformNginxWikiInfoController.php b/app/Http/Controllers/Backend/PlatformNginxWikiInfoController.php new file mode 100644 index 000000000..3147cd1ed --- /dev/null +++ b/app/Http/Controllers/Backend/PlatformNginxWikiInfoController.php @@ -0,0 +1,75 @@ +query('domain'); + // TODO: inject the MediaWikiHostResolver so that we can pass in a mock when testing + $mediawikiHostResolver = new MediaWikiHostResolver(); + try { + $hosts = $mediawikiHostResolver->getHostsForDomain($domain); + } catch (UnknownWikiDomainException $e) { + return response('Domain not found', 404); + } catch (UnknownDBVersionException $e) { + return response('Unknown database version', 500); + } + + if ($this->wikiInaccessible($domain)) { + return response('Wiki suspended', 403); + } + + return response('OK', 200, [ + 'x-mw-alpha-host' => $hosts['alpha'], + 'x-mw-api-host' => $hosts['api'], + 'x-mw-backend-host' => $hosts['backend'], + 'x-mw-web-host' => $hosts['web'], + ]); + } + + // - wiki has no suspensions -> return accessible + // - wiki's latest suspension is not active -> return accessible + // - wiki's latest suspension is active and wiki has pending review submission with created_at date + // before the active_from date of the latest suspension -> return accessible + // - wiki's latest suspension is active and wiki has pending review submission with created_at date + // after the active_from date of the latest suspension and the review has been picked up -> return accessible + // - all other cases -> return not accessible + private function wikiInaccessible(string $domain): bool { + // TODO: Possible performance improvement is to look at the number of DB queries + // eager loading using the with() method doesn't reduce the number of queries + // TODO: We could also move this up in the controller so it doesn't happen in the MediaWikiHostResolver as well + $wiki = Wiki::whereDomain($domain)->first(); + + // - wiki has no suspensions -> return accessible + // - wiki's latest suspension is not active -> return accessible + if (!$wiki->latestSuspension?->is_active) { + return false; + } + + // - wiki's latest suspension is active and wiki has pending review submission with created_at date + // before the active_from date of the latest suspension -> return accessible + // $wiki->latestSuspension is not null as already checked the $wiki->latestSuspension?->is_active state + // non-pending reviews like accepted, rejected or cancelled are ignored here + if ($wiki->pendingReviewSubmission?->created_at->isBefore($wiki->latestSuspension->active_from)) { + return false; + } + + // - wiki's latest suspension is active and wiki has pending review submission with created_at date after + // the active_from date of the latest suspension and the review has been picked up -> return accessible + if ($wiki->pendingReviewSubmission?->latestAction->type === ReviewSubmissionActionType::REVIEW_STARTED) { + return false; + } + + // - all other cases -> return not accessible + return true; + } +} diff --git a/routes/backend.php b/routes/backend.php index 2d416a956..209c14fc8 100644 --- a/routes/backend.php +++ b/routes/backend.php @@ -1,5 +1,6 @@ group(['prefix' => 'wiki'], function () use ($router): void { // GET $router->get('getWikiForDomain', ['uses' => 'WikiController@getWikiForDomain']); + $router->get('platformNginxInfo', PlatformNginxWikiInfoController::class . '@getInfoForDomain'); // PATCH $router->patch('updateEntityImport', ['uses' => '\App\Http\Controllers\WikiEntityImportController@update']); }); diff --git a/tests/Feature/Http/Controllers/Backend/PlatformNginxWikiInfoControllerTest.php b/tests/Feature/Http/Controllers/Backend/PlatformNginxWikiInfoControllerTest.php new file mode 100644 index 000000000..7d52e352a --- /dev/null +++ b/tests/Feature/Http/Controllers/Backend/PlatformNginxWikiInfoControllerTest.php @@ -0,0 +1,337 @@ + '139', + 'mw1.43-wbs2' => '143', + ]); + } + + public function testSuccess() { + $expectedHosts = [ + 'alpha' => 'mediawiki-143-app-alpha.default.svc.cluster.local', + 'api' => 'mediawiki-143-app-api.default.svc.cluster.local', + 'backend' => 'mediawiki-143-app-backend.default.svc.cluster.local', + 'web' => 'mediawiki-143-app-web.default.svc.cluster.local', + ]; + + $this->createWiki('test139.wikibase.cloud', 'mw1.39-wbs1'); + $this->createWiki('test143.wikibase.cloud', 'mw1.43-wbs2'); + + $this->get("$this->route?domain=test143.wikibase.cloud") + ->assertStatus(200) + ->assertContent('OK') + ->assertHeader('x-mw-alpha-host', $expectedHosts['alpha']) + ->assertHeader('x-mw-api-host', $expectedHosts['api']) + ->assertHeader('x-mw-backend-host', $expectedHosts['backend']) + ->assertHeader('x-mw-web-host', $expectedHosts['web']); + } + + public function testDomainNotfound() { + $this->get("$this->route?domain=notfound.wikibase.cloud") + ->assertStatus(404); + } + + public function testUnknownDbVersion() { + $this->createWiki('test.wikibase.cloud', 'unknownVersion'); + + $this->get("$this->route?domain=test.wikibase.cloud") + ->assertStatus(500); + } + + private function createWiki(string $domain, string $version) { + Wiki::factory(['domain' => $domain]) + ->has(WikiDb::factory(['version' => $version])) + ->create(); + } + + // TODO: Do we keep each individual test case or write some for loops? + // TODO: Improve readability of tests/providers (e.g. Create helper methods? Name parameters in provider? + // Shorted data set names? Calculate days relative to now()? Create builder class for constructing test cases?) + /** + * @covers PlatformNginxWikiInfoController::getInfoForDomain + */ + public static function providePlatformNginxWikiInfoControllerCases(): Generator { + yield 'no active review committee decision, expiry date in the future, submission submitted => 200 OK w/ Host Headers' => [ + '2026-11-01', + ['active_from' => '2027-02-15'], + ['created_at' => '2026-11-20'], + [ + 'type' => ReviewSubmissionActionType::SUBMITTED, + 'actor_user_role' => UserRole::WIKI_MANAGER, + 'created_at' => '2026-11-20', + ], + true, + ]; + + yield 'no active review committee decision, expiry date in the future, submission review started => 200 OK w/ Host Headers' => [ + '2026-11-01', + ['active_from' => '2027-02-15'], + ['created_at' => '2026-11-20'], + [ + // TODO: we should really create the previous Actions first + 'type' => ReviewSubmissionActionType::REVIEW_STARTED, + 'actor_user_role' => UserRole::WIKI_MANAGER, + 'created_at' => '2026-11-20', + ], + true, + ]; + + yield 'no active review committee decision, expiry date in the future, no submission => 200 OK w/ Host Headers' => [ + '2026-11-01', + ['active_from' => '2027-02-15'], + null, + null, + true, + ]; + + yield 'no active review committee decision, expiry date in the future, submission approved => 200 OK w/ Host Headers' => [ + '2026-11-01', + ['active_from' => '2027-02-15'], + // TODO: don't make submission create_at and action created_at the same datetime + ['created_at' => '2026-11-20'], + [ + // TODO: we should really create the previous Actions first + 'type' => ReviewSubmissionActionType::APPROVED, + 'actor_user_role' => UserRole::WIKI_MANAGER, + 'created_at' => '2026-11-20', + ], + true, + ]; + + yield 'no active review committee decision, expiry date in the future, submission rejected => 200 OK w/ Host Headers' => [ + '2026-11-01', + ['active_from' => '2027-02-15'], + // TODO: don't make submission create_at and action created_at the same datetime + ['created_at' => '2026-11-20'], + [ + // TODO: we should really create the previous Actions first + 'type' => ReviewSubmissionActionType::REJECTED, + 'actor_user_role' => UserRole::WIKI_MANAGER, + 'created_at' => '2026-11-20', + ], + true, + ]; + + yield 'no active review committee decision, expiry date in the future, submission cancelled => 200 OK w/ Host Headers' => [ + '2026-11-01', + ['active_from' => '2027-02-15'], + // TODO: don't make submission create_at and action created_at the same datetime + ['created_at' => '2026-11-20'], + [ + // TODO: we should really create the previous Actions first + 'type' => ReviewSubmissionActionType::CANCELLED, + 'actor_user_role' => UserRole::WIKI_MANAGER, + 'created_at' => '2026-11-20', + ], + true, + ]; + + yield 'no active review committee decision, expiry date in the past, submission submitted before expiry => 200 OK' => [ + '2026-11-01', + ['active_from' => '2026-11-10'], + ['created_at' => '2026-11-05'], + [ + 'type' => ReviewSubmissionActionType::SUBMITTED, + 'actor_user_role' => UserRole::WIKI_MANAGER, + 'created_at' => '2026-11-05', + ], + true, + ]; + + yield 'no active review committee decision, expiry date in the past, submission submitted after expiry => 403 Wiki suspended' => [ + '2026-11-01', + ['active_from' => '2026-11-10'], + ['created_at' => '2026-11-20'], + [ + 'type' => ReviewSubmissionActionType::SUBMITTED, + 'actor_user_role' => UserRole::WIKI_MANAGER, + 'created_at' => '2026-11-20', + ], + false, + ]; + + yield 'no active review committee decision, expiry date in the past, submission submitted before expiry and review started => 200 OK w/ Host Headers' => [ + '2026-11-01', + ['active_from' => '2026-11-10'], + // TODO: Make review not start on the same datetime it was created + ['created_at' => '2026-11-05'], + [ + // TODO: we should really create the previous Actions first + 'type' => ReviewSubmissionActionType::REVIEW_STARTED, + 'actor_user_role' => UserRole::WIKI_MANAGER, + 'created_at' => '2026-11-05', + ], + true, + ]; + + yield 'no active review committee decision, expiry date in the past, submission submitted after expiry and review started => 200 OK w/ Host Headers' => [ + '2026-11-01', + ['active_from' => '2026-11-10'], + // TODO: Make review not start on the same datetime it was created + ['created_at' => '2026-11-20'], + [ + // TODO: we should really create the previous Actions first + 'type' => ReviewSubmissionActionType::REVIEW_STARTED, + 'actor_user_role' => UserRole::WIKI_MANAGER, + 'created_at' => '2026-11-20', + ], + true, + ]; + + yield 'no active review committee decision, expiry date in the past, no submission => 403 Wiki suspended' => [ + '2026-11-01', + ['active_from' => '2026-11-10'], + null, + null, + false, + ]; + + yield 'no active review committee decision, expiry date in the past, submission submitted before expiry and cancelled => 403 Wiki suspended' => [ + '2026-11-01', + ['active_from' => '2026-11-10'], + // TODO: Make review not start on the same datetime it was created + ['created_at' => '2026-11-05'], + [ + // TODO: we should really create the previous Actions first + 'type' => ReviewSubmissionActionType::CANCELLED, + 'actor_user_role' => UserRole::WIKI_MANAGER, + 'created_at' => '2026-11-05', + ], + false, + ]; + + yield 'no active review committee decision, expiry date in the past, submission submitted after expiry and cancelled => 403 Wiki suspended' => [ + '2026-11-01', + ['active_from' => '2026-11-10'], + // TODO: Make review not start on the same datetime it was created + ['created_at' => '2026-11-20'], + [ + // TODO: we should really create the previous Actions first + 'type' => ReviewSubmissionActionType::CANCELLED, + 'actor_user_role' => UserRole::WIKI_MANAGER, + 'created_at' => '2026-11-20', + ], + false, + ]; + + yield 'no active review committee decision, expiry date in the past, submission submitted before expiry and approved => 403 Wiki suspended' => [ + '2026-11-01', + ['active_from' => '2026-11-10'], + // TODO: Make review not start on the same datetime it was created + ['created_at' => '2026-11-05'], + [ + // TODO: we should really create the previous Actions first + 'type' => ReviewSubmissionActionType::APPROVED, + 'actor_user_role' => UserRole::WIKI_MANAGER, + 'created_at' => '2026-11-05', + ], + false, + ]; + + yield 'no active review committee decision, expiry date in the past, submission submitted after expiry and approved => 403 Wiki suspended' => [ + '2026-11-01', + ['active_from' => '2026-11-10'], + // TODO: Make review not start on the same datetime it was created + ['created_at' => '2026-11-20'], + [ + // TODO: we should really create the previous Actions first + 'type' => ReviewSubmissionActionType::APPROVED, + 'actor_user_role' => UserRole::WIKI_MANAGER, + 'created_at' => '2026-11-20', + ], + false, + ]; + + yield 'no active review committee decision, expiry date in the past, submission submitted before expiry and rejected => 403 Wiki suspended' => [ + '2026-11-01', + ['active_from' => '2026-11-10'], + // TODO: Make review not start on the same datetime it was created + ['created_at' => '2026-11-05'], + [ + // TODO: we should really create the previous Actions first + 'type' => ReviewSubmissionActionType::REJECTED, + 'actor_user_role' => UserRole::WIKI_MANAGER, + 'created_at' => '2026-11-05', + ], + false, + ]; + + yield 'no active review committee decision, expiry date in the past, submission submitted after expiry and rejected => 403 Wiki suspended' => [ + '2026-11-01', + ['active_from' => '2026-11-10'], + // TODO: Make review not start on the same datetime it was created + ['created_at' => '2026-11-20'], + [ + // TODO: we should really create the previous Actions first + 'type' => ReviewSubmissionActionType::REJECTED, + 'actor_user_role' => UserRole::WIKI_MANAGER, + 'created_at' => '2026-11-20', + ], + false, + ]; + } + + #[DataProvider('providePlatformNginxWikiInfoControllerCases')] + public function testPlatformNginxWikiInfoController( + string $wikiCreatedAt, + ?array $wikiSuspensionState, + ?array $reviewSubmissionState, + ?array $reviewSubmissionActionState, + bool $wikiExpectedToBeAccessible, + ): void { + $this->travelTo('2026-11-24'); + + $wiki = Wiki::factory(['created_at' => $wikiCreatedAt]) + ->has(WikiDb::factory(['version' => 'mw1.43-wbs2'])) + ->create(); + + if ($wikiSuspensionState !== null) { + WikiSuspension::factory($wikiSuspensionState)->for($wiki)->create(); + } + + if ($reviewSubmissionState !== null) { + ReviewSubmission::factory($reviewSubmissionState) + ->for($wiki) + ->when( + $reviewSubmissionActionState !== null, + fn ($factory) => $factory->hasActions($reviewSubmissionActionState) + ) + ->create(); + } + + $response = $this->get("{$this->route}?domain=$wiki->domain") + ->assertStatus($wikiExpectedToBeAccessible ? 200 : 403) + ->assertContent($wikiExpectedToBeAccessible ? 'OK' : 'Wiki suspended'); + + if ($wikiExpectedToBeAccessible) { + $response->assertHeader('x-mw-alpha-host'); + $response->assertHeader('x-mw-api-host'); + $response->assertHeader('x-mw-backend-host'); + $response->assertHeader('x-mw-web-host'); + } + } +}