|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors |
| 7 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 8 | + */ |
| 9 | + |
| 10 | +namespace OCA\AppAPI\Tests\php\Middleware; |
| 11 | + |
| 12 | +use OCA\AppAPI\Attribute\MaintenanceModeAvailable; |
| 13 | +use OCA\AppAPI\Controller\AppConfigController; |
| 14 | +use OCA\AppAPI\Controller\ExAppsPageController; |
| 15 | +use OCA\AppAPI\Controller\HarpController; |
| 16 | +use OCA\AppAPI\Controller\OCSApiController; |
| 17 | +use OCA\AppAPI\Controller\OCSExAppController; |
| 18 | +use OCA\AppAPI\Controller\PreferencesController; |
| 19 | +use OCA\AppAPI\Controller\TalkBotController; |
| 20 | +use OCA\AppAPI\Exceptions\MaintenanceModeException; |
| 21 | +use OCA\AppAPI\Middleware\MaintenanceModeMiddleware; |
| 22 | +use OCP\AppFramework\Controller; |
| 23 | +use OCP\AppFramework\Http; |
| 24 | +use OCP\AppFramework\Http\JSONResponse; |
| 25 | +use OCP\IConfig; |
| 26 | +use OCP\IRequest; |
| 27 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 28 | +use PHPUnit\Framework\TestCase; |
| 29 | +use ReflectionMethod; |
| 30 | + |
| 31 | +class MaintenanceModeTestController extends Controller { |
| 32 | + #[MaintenanceModeAvailable] |
| 33 | + public function allowedRoute(): void { |
| 34 | + } |
| 35 | + |
| 36 | + public function blockedRoute(): void { |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +class MaintenanceModeMiddlewareTest extends TestCase { |
| 41 | + private IConfig $config; |
| 42 | + private MaintenanceModeTestController $controller; |
| 43 | + |
| 44 | + protected function setUp(): void { |
| 45 | + parent::setUp(); |
| 46 | + $this->config = $this->createMock(IConfig::class); |
| 47 | + $this->controller = new MaintenanceModeTestController('app_api', $this->createMock(IRequest::class)); |
| 48 | + } |
| 49 | + |
| 50 | + private function middleware(bool $maintenance): MaintenanceModeMiddleware { |
| 51 | + $this->config->method('getSystemValueBool')->with('maintenance', false)->willReturn($maintenance); |
| 52 | + return new MaintenanceModeMiddleware($this->config); |
| 53 | + } |
| 54 | + |
| 55 | + public function testPassesEveryRouteWhenNotInMaintenance(): void { |
| 56 | + $this->middleware(false)->beforeController($this->controller, 'blockedRoute'); |
| 57 | + $this->addToAssertionCount(1); |
| 58 | + } |
| 59 | + |
| 60 | + public function testPassesAllowedRouteDuringMaintenance(): void { |
| 61 | + $this->middleware(true)->beforeController($this->controller, 'allowedRoute'); |
| 62 | + $this->addToAssertionCount(1); |
| 63 | + } |
| 64 | + |
| 65 | + public function testBlocksUnmarkedRouteDuringMaintenance(): void { |
| 66 | + $this->expectException(MaintenanceModeException::class); |
| 67 | + $this->middleware(true)->beforeController($this->controller, 'blockedRoute'); |
| 68 | + } |
| 69 | + |
| 70 | + public function testExceptionIsAnsweredWithMaintenanceResponse(): void { |
| 71 | + $exception = new MaintenanceModeException(); |
| 72 | + $response = $this->middleware(false)->afterException($this->controller, 'blockedRoute', $exception); |
| 73 | + |
| 74 | + self::assertInstanceOf(JSONResponse::class, $response); |
| 75 | + self::assertSame(Http::STATUS_SERVICE_UNAVAILABLE, $response->getStatus()); |
| 76 | + self::assertSame('1', $response->getHeaders()['X-Nextcloud-Maintenance-Mode']); |
| 77 | + self::assertSame('120', $response->getHeaders()['Retry-After']); |
| 78 | + self::assertSame(['message' => $exception->getMessage()], $response->getData()); |
| 79 | + } |
| 80 | + |
| 81 | + public function testBlockedRouteInMaintenanceYields503EndToEnd(): void { |
| 82 | + $middleware = $this->middleware(true); |
| 83 | + |
| 84 | + try { |
| 85 | + $middleware->beforeController($this->controller, 'blockedRoute'); |
| 86 | + self::fail('Expected MaintenanceModeException to be thrown'); |
| 87 | + } catch (MaintenanceModeException $exception) { |
| 88 | + $response = $middleware->afterException($this->controller, 'blockedRoute', $exception); |
| 89 | + } |
| 90 | + |
| 91 | + self::assertSame(Http::STATUS_SERVICE_UNAVAILABLE, $response->getStatus()); |
| 92 | + self::assertSame('1', $response->getHeaders()['X-Nextcloud-Maintenance-Mode']); |
| 93 | + self::assertSame('120', $response->getHeaders()['Retry-After']); |
| 94 | + } |
| 95 | + |
| 96 | + public function testUnrelatedExceptionIsRethrown(): void { |
| 97 | + $exception = new \RuntimeException('boom'); |
| 98 | + $this->expectExceptionObject($exception); |
| 99 | + $this->middleware(false)->afterException($this->controller, 'blockedRoute', $exception); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * @return list<array{class-string, string}> |
| 104 | + */ |
| 105 | + public static function allowlistedRoutes(): array { |
| 106 | + return [ |
| 107 | + [HarpController::class, 'getExAppMetadata'], |
| 108 | + [OCSApiController::class, 'setAppInitProgress'], |
| 109 | + [OCSApiController::class, 'setAppInitProgressDeprecated'], |
| 110 | + [OCSApiController::class, 'getEnabledState'], |
| 111 | + [OCSApiController::class, 'getNextcloudAbsoluteUrl'], |
| 112 | + [OCSApiController::class, 'log'], |
| 113 | + ]; |
| 114 | + } |
| 115 | + |
| 116 | + #[DataProvider('allowlistedRoutes')] |
| 117 | + public function testAllowlistedRouteCarriesAttribute(string $class, string $method): void { |
| 118 | + $attributes = (new ReflectionMethod($class, $method))->getAttributes(MaintenanceModeAvailable::class); |
| 119 | + self::assertNotEmpty($attributes, $class . '::' . $method . ' must stay reachable during maintenance'); |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * @return list<array{class-string, string}> |
| 124 | + */ |
| 125 | + public static function blockedRoutes(): array { |
| 126 | + return [ |
| 127 | + [HarpController::class, 'getUserInfo'], |
| 128 | + [OCSApiController::class, 'getNCUsersList'], |
| 129 | + [OCSExAppController::class, 'getExAppsList'], |
| 130 | + [AppConfigController::class, 'setAppConfigValue'], |
| 131 | + [AppConfigController::class, 'getAppConfigValues'], |
| 132 | + [PreferencesController::class, 'setUserConfigValue'], |
| 133 | + [PreferencesController::class, 'getUserConfigValues'], |
| 134 | + [ExAppsPageController::class, 'uninstallApp'], |
| 135 | + [TalkBotController::class, 'registerExAppTalkBot'], |
| 136 | + ]; |
| 137 | + } |
| 138 | + |
| 139 | + #[DataProvider('blockedRoutes')] |
| 140 | + public function testNonAllowlistedRouteHasNoAttribute(string $class, string $method): void { |
| 141 | + $attributes = (new ReflectionMethod($class, $method))->getAttributes(MaintenanceModeAvailable::class); |
| 142 | + self::assertEmpty($attributes, $class . '::' . $method . ' must return 503 during maintenance'); |
| 143 | + } |
| 144 | +} |
0 commit comments