From 7859d0f29b44a26dd2bb94938856793f8f5ee6c8 Mon Sep 17 00:00:00 2001 From: Ollie Date: Tue, 24 Nov 2026 11:32:09 +0000 Subject: [PATCH 1/2] Add endpoint to give platform-nginx required info Bug: T444282 --- .../PlatformNginxWikiInfoController.php | 75 ++++ routes/backend.php | 2 + .../PlatformNginxWikiInfoControllerTest.php | 382 ++++++++++++++++++ 3 files changed, 459 insertions(+) create mode 100644 app/Http/Controllers/Backend/PlatformNginxWikiInfoController.php create mode 100644 tests/Feature/Http/Controllers/Backend/PlatformNginxWikiInfoControllerTest.php diff --git a/app/Http/Controllers/Backend/PlatformNginxWikiInfoController.php b/app/Http/Controllers/Backend/PlatformNginxWikiInfoController.php new file mode 100644 index 000000000..9bead282a --- /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 { + // FUTURE: 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 + // FUTURE: 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 + if ($wiki->pendingReviewSubmission?->created_at->isBefore($wiki->latestSuspension->active_from)) { + // TODO: if the latest action is canceled should this still return false? + 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..9b7be1b31 --- /dev/null +++ b/tests/Feature/Http/Controllers/Backend/PlatformNginxWikiInfoControllerTest.php @@ -0,0 +1,382 @@ + '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(); + } + + // FUTURE: Do we write each individual test case or write some for loops? + // FUTURE: 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(); + } + + // FIXME: The PlatformNginxWikiInfoController creates 5-6 database queries + // Uncomment to see the number of database queries are made in the preceding code + // $this->expectsDatabaseQueryCount(0); + + $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'); + } + } + + // FUTURE: Possible helper method? + // private function createReviewSubmission( + // Wiki $wiki, + // string $createdAt, + // ?ReviewSubmissionActionType $latestActionType, + // string $latestActionCreatedAt, + // ): ReviewSubmission { + // $submission = ReviewSubmission::factory(['created_at' => $createdAt]) + // ->for($wiki) + // ->create(); + + // if ($latestActionType === null) { + // return $submission; + // } + + // ReviewSubmissionAction::factory()->for($submission)->create([ + // 'type' => ReviewSubmissionActionType::SUBMITTED, + // 'actor_user_role' => UserRole::WIKI_MANAGER, + // 'created_at' => $createdAt, + // ]); + + // if ($latestActionType !== ReviewSubmissionActionType::SUBMITTED) { + // ReviewSubmissionAction::factory()->for($submission)->create([ + // 'type' => ReviewSubmissionActionType::REVIEW_STARTED, + // 'actor_user_role' => UserRole::REVIEW_COMMITTEE_ADMIN, + // 'created_at' => $latestActionType === ReviewSubmissionActionType::REVIEW_STARTED + // ? $latestActionCreatedAt : $createdAt, + // ]); + + // if ($latestActionType !== ReviewSubmissionActionType::REVIEW_STARTED) { + // ReviewSubmissionAction::factory()->for($submission)->create([ + // 'type' => $latestActionType, + // 'actor_user_role' => UserRole::REVIEW_COMMITTEE_ADMIN, + // 'created_at' => $latestActionCreatedAt, + // ]); + // } + // } + + // return $submission->refresh(); + // } +} From aa763579abd09422561dd624ba233da10830dffd Mon Sep 17 00:00:00 2001 From: Thomas Arrow Date: Wed, 25 Nov 2026 11:41:17 +0000 Subject: [PATCH 2/2] remove unneeded comments and convert FUTURE to TODO for consistency --- .../PlatformNginxWikiInfoController.php | 6 +-- .../PlatformNginxWikiInfoControllerTest.php | 49 +------------------ 2 files changed, 5 insertions(+), 50 deletions(-) diff --git a/app/Http/Controllers/Backend/PlatformNginxWikiInfoController.php b/app/Http/Controllers/Backend/PlatformNginxWikiInfoController.php index 9bead282a..3147cd1ed 100644 --- a/app/Http/Controllers/Backend/PlatformNginxWikiInfoController.php +++ b/app/Http/Controllers/Backend/PlatformNginxWikiInfoController.php @@ -44,9 +44,9 @@ public function getInfoForDomain(Request $request): Response { // 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 { - // FUTURE: Possible performance improvement is to look at the number of DB queries + // 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 - // FUTURE: We could also move this up in the controller so it doesn't happen in the MediaWikiHostResolver as well + // 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 @@ -58,8 +58,8 @@ private function wikiInaccessible(string $domain): bool { // - 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)) { - // TODO: if the latest action is canceled should this still return false? return false; } diff --git a/tests/Feature/Http/Controllers/Backend/PlatformNginxWikiInfoControllerTest.php b/tests/Feature/Http/Controllers/Backend/PlatformNginxWikiInfoControllerTest.php index 9b7be1b31..7d52e352a 100644 --- a/tests/Feature/Http/Controllers/Backend/PlatformNginxWikiInfoControllerTest.php +++ b/tests/Feature/Http/Controllers/Backend/PlatformNginxWikiInfoControllerTest.php @@ -68,8 +68,8 @@ private function createWiki(string $domain, string $version) { ->create(); } - // FUTURE: Do we write each individual test case or write some for loops? - // FUTURE: Improve readability of tests/providers (e.g. Create helper methods? Name parameters in provider? + // 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 @@ -323,10 +323,6 @@ public function testPlatformNginxWikiInfoController( ->create(); } - // FIXME: The PlatformNginxWikiInfoController creates 5-6 database queries - // Uncomment to see the number of database queries are made in the preceding code - // $this->expectsDatabaseQueryCount(0); - $response = $this->get("{$this->route}?domain=$wiki->domain") ->assertStatus($wikiExpectedToBeAccessible ? 200 : 403) ->assertContent($wikiExpectedToBeAccessible ? 'OK' : 'Wiki suspended'); @@ -338,45 +334,4 @@ public function testPlatformNginxWikiInfoController( $response->assertHeader('x-mw-web-host'); } } - - // FUTURE: Possible helper method? - // private function createReviewSubmission( - // Wiki $wiki, - // string $createdAt, - // ?ReviewSubmissionActionType $latestActionType, - // string $latestActionCreatedAt, - // ): ReviewSubmission { - // $submission = ReviewSubmission::factory(['created_at' => $createdAt]) - // ->for($wiki) - // ->create(); - - // if ($latestActionType === null) { - // return $submission; - // } - - // ReviewSubmissionAction::factory()->for($submission)->create([ - // 'type' => ReviewSubmissionActionType::SUBMITTED, - // 'actor_user_role' => UserRole::WIKI_MANAGER, - // 'created_at' => $createdAt, - // ]); - - // if ($latestActionType !== ReviewSubmissionActionType::SUBMITTED) { - // ReviewSubmissionAction::factory()->for($submission)->create([ - // 'type' => ReviewSubmissionActionType::REVIEW_STARTED, - // 'actor_user_role' => UserRole::REVIEW_COMMITTEE_ADMIN, - // 'created_at' => $latestActionType === ReviewSubmissionActionType::REVIEW_STARTED - // ? $latestActionCreatedAt : $createdAt, - // ]); - - // if ($latestActionType !== ReviewSubmissionActionType::REVIEW_STARTED) { - // ReviewSubmissionAction::factory()->for($submission)->create([ - // 'type' => $latestActionType, - // 'actor_user_role' => UserRole::REVIEW_COMMITTEE_ADMIN, - // 'created_at' => $latestActionCreatedAt, - // ]); - // } - // } - - // return $submission->refresh(); - // } }