Skip to content

Commit 9133464

Browse files
committed
fix(tests): Adapt Middleware tests to API change
Removed a few tests rendered obsolete by the refactoring. Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>
1 parent 447ee17 commit 9133464

12 files changed

Lines changed: 73 additions & 75 deletions

apps/provisioning_api/tests/Middleware/ProvisioningApiMiddlewareTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,14 @@ public function testBeforeController(bool $subadminRequired, bool $isAdmin, bool
5353
);
5454

5555
$this->reflector->method('hasAnnotation')
56-
->willReturnCallback(function ($annotation) use ($subadminRequired, $hasSettingAuthorizationAnnotation) {
56+
->willReturnCallback(function ($annotation) use ($subadminRequired) {
5757
if ($annotation === 'NoSubAdminRequired') {
5858
return !$subadminRequired;
5959
}
60+
return false;
61+
});
62+
$this->reflector->method('hasAnnotationOrAttribute')
63+
->willReturnCallback(function ($annotation, $attribute) use ($hasSettingAuthorizationAnnotation) {
6064
if ($annotation === 'AuthorizedAdminSetting') {
6165
return $hasSettingAuthorizationAnnotation;
6266
}

apps/settings/tests/Middleware/SubadminMiddlewareTest.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use OC\AppFramework\Utility\ControllerMethodReflector;
1515
use OCA\Settings\Middleware\SubadminMiddleware;
1616
use OCP\AppFramework\Controller;
17+
use OCP\AppFramework\Http\Attribute\AuthorizedAdminSetting;
1718
use OCP\AppFramework\Http\TemplateResponse;
1819
use OCP\Group\ISubAdmin;
1920
use OCP\IL10N;
@@ -62,11 +63,16 @@ public function testBeforeControllerAsUserWithoutAnnotation(): void {
6263
$this->expectException(NotAdminException::class);
6364

6465
$this->reflector
65-
->expects($this->exactly(2))
66+
->expects($this->exactly(1))
6667
->method('hasAnnotation')
6768
->willReturnMap([
6869
['NoSubAdminRequired', false],
69-
['AuthorizedAdminSetting', false],
70+
]);
71+
$this->reflector
72+
->expects($this->exactly(1))
73+
->method('hasAnnotationOrAttribute')
74+
->willReturnMap([
75+
['AuthorizedAdminSetting', AuthorizedAdminSetting::class, false],
7076
]);
7177

7278
$this->subAdminManager
@@ -94,11 +100,16 @@ public function testBeforeControllerWithAnnotation(): void {
94100

95101
public function testBeforeControllerAsSubAdminWithoutAnnotation(): void {
96102
$this->reflector
97-
->expects($this->exactly(2))
103+
->expects($this->exactly(1))
98104
->method('hasAnnotation')
99105
->willReturnMap([
100106
['NoSubAdminRequired', false],
101-
['AuthorizedAdminSetting', false],
107+
]);
108+
$this->reflector
109+
->expects($this->exactly(1))
110+
->method('hasAnnotationOrAttribute')
111+
->willReturnMap([
112+
['AuthorizedAdminSetting', AuthorizedAdminSetting::class, false],
102113
]);
103114

104115
$this->subAdminManager

lib/private/AppFramework/Utility/ControllerMethodReflector.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ public function __construct(
3333
* @param string $method the method which we want to inspect
3434
*/
3535
public function reflect($object, string $method) {
36+
$this->annotations = [];
37+
$this->types = [];
38+
$this->parameters = [];
39+
$this->ranges = [];
3640
$this->reflectionMethod = new \ReflectionMethod($object, $method);
3741
$this->startLine = $this->reflectionMethod->getStartLine();
3842
$this->file = $this->reflectionMethod->getFileName();

tests/lib/AppFramework/Http/DispatcherTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ protected function setUp(): void {
123123

124124
$this->request = $this->createMock(Request::class);
125125

126-
$this->reflector = new ControllerMethodReflector();
126+
$this->reflector = new ControllerMethodReflector(\OCP\Server::get(LoggerInterface::class));
127127

128128
$this->dispatcher = new Dispatcher(
129129
$this->http,

tests/lib/AppFramework/Middleware/Security/BruteForceMiddlewareTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class BruteForceMiddlewareTest extends TestCase {
3030
protected function setUp(): void {
3131
parent::setUp();
3232

33-
$this->reflector = new ControllerMethodReflector();
33+
$this->reflector = new ControllerMethodReflector(\OCP\Server::get(LoggerInterface::class));
3434
$this->throttler = $this->createMock(IThrottler::class);
3535
$this->request = $this->createMock(IRequest::class);
3636
$this->logger = $this->createMock(LoggerInterface::class);

tests/lib/AppFramework/Middleware/Security/CORSMiddlewareTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class CORSMiddlewareTest extends \Test\TestCase {
3333

3434
protected function setUp(): void {
3535
parent::setUp();
36-
$this->reflector = new ControllerMethodReflector();
36+
$this->reflector = new ControllerMethodReflector(\OCP\Server::get(LoggerInterface::class));
3737
$this->session = $this->createMock(Session::class);
3838
$this->throttler = $this->createMock(IThrottler::class);
3939
$this->logger = $this->createMock(LoggerInterface::class);
@@ -79,6 +79,7 @@ public function testNoAnnotationNoCORSHEADER(): void {
7979
$this->createMock(IRequestId::class),
8080
$this->createMock(IConfig::class)
8181
);
82+
$this->reflector->reflect($this->controller, __FUNCTION__);
8283
$middleware = new CORSMiddleware($request, new MiddlewareUtils($this->reflector, $this->logger), $this->session, $this->throttler);
8384

8485
$response = $middleware->afterController($this->controller, __FUNCTION__, new Response());
@@ -300,6 +301,7 @@ public function testAfterExceptionWithSecurityExceptionNoStatus(): void {
300301
$this->createMock(IRequestId::class),
301302
$this->createMock(IConfig::class)
302303
);
304+
$this->reflector->reflect($this->controller, __FUNCTION__);
303305
$middleware = new CORSMiddleware($request, new MiddlewareUtils($this->reflector, $this->logger), $this->session, $this->throttler);
304306
$response = $middleware->afterException($this->controller, __FUNCTION__, new SecurityException('A security exception'));
305307

@@ -316,6 +318,7 @@ public function testAfterExceptionWithSecurityExceptionWithStatus(): void {
316318
$this->createMock(IRequestId::class),
317319
$this->createMock(IConfig::class)
318320
);
321+
$this->reflector->reflect($this->controller, __FUNCTION__);
319322
$middleware = new CORSMiddleware($request, new MiddlewareUtils($this->reflector, $this->logger), $this->session, $this->throttler);
320323
$response = $middleware->afterException($this->controller, __FUNCTION__, new SecurityException('A security exception', 501));
321324

@@ -335,6 +338,7 @@ public function testAfterExceptionWithRegularException(): void {
335338
$this->createMock(IRequestId::class),
336339
$this->createMock(IConfig::class)
337340
);
341+
$this->reflector->reflect($this->controller, __FUNCTION__);
338342
$middleware = new CORSMiddleware($request, new MiddlewareUtils($this->reflector, $this->logger), $this->session, $this->throttler);
339343
$middleware->afterException($this->controller, __FUNCTION__, new \Exception('A regular exception'));
340344
}

tests/lib/AppFramework/Middleware/Security/PasswordConfirmationMiddlewareTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class PasswordConfirmationMiddlewareTest extends TestCase {
4545
private Manager $userManager;
4646

4747
protected function setUp(): void {
48-
$this->reflector = new ControllerMethodReflector();
48+
$this->reflector = new ControllerMethodReflector(\OCP\Server::get(LoggerInterface::class));
4949
$this->session = $this->createMock(ISession::class);
5050
$this->userSession = $this->createMock(IUserSession::class);
5151
$this->user = $this->createMock(IUser::class);

tests/lib/AppFramework/Middleware/Security/RateLimitingMiddlewareTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ protected function setUp(): void {
4545

4646
$this->request = $this->createMock(IRequest::class);
4747
$this->userSession = $this->createMock(IUserSession::class);
48-
$this->reflector = new ControllerMethodReflector();
48+
$this->reflector = new ControllerMethodReflector(\OCP\Server::get(LoggerInterface::class));
4949
$this->limiter = $this->createMock(Limiter::class);
5050
$this->session = $this->createMock(ISession::class);
5151
$this->appConfig = $this->createMock(IAppConfig::class);

tests/lib/AppFramework/Middleware/Security/SameSiteCookieMiddlewareTest.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,9 @@ public function testBeforeControllerIndexHasAnnotation(): void {
6161
$this->request->method('getScriptName')
6262
->willReturn('/index.php');
6363

64-
$this->reflector->method('hasAnnotation')
65-
->with('NoSameSiteCookieRequired')
64+
$this->reflector->expects(self::once())
65+
->method('hasAnnotationOrAttribute')
66+
->with('NoSameSiteCookieRequired', NoSameSiteCookieRequired::class)
6667
->willReturn(true);
6768

6869
$this->middleware->beforeController(new HasAnnotationController('foo', $this->request), 'foo');
@@ -73,8 +74,9 @@ public function testBeforeControllerIndexNoAnnotationPassingCheck(): void {
7374
$this->request->method('getScriptName')
7475
->willReturn('/index.php');
7576

76-
$this->reflector->method('hasAnnotation')
77-
->with('NoSameSiteCookieRequired')
77+
$this->reflector->expects(self::once())
78+
->method('hasAnnotationOrAttribute')
79+
->with('NoSameSiteCookieRequired', NoSameSiteCookieRequired::class)
7880
->willReturn(false);
7981

8082
$this->request->method('passesLaxCookieCheck')
@@ -90,8 +92,9 @@ public function testBeforeControllerIndexNoAnnotationFailingCheck(): void {
9092
$this->request->method('getScriptName')
9193
->willReturn('/index.php');
9294

93-
$this->reflector->method('hasAnnotation')
94-
->with('NoSameSiteCookieRequired')
95+
$this->reflector->expects(self::once())
96+
->method('hasAnnotationOrAttribute')
97+
->with('NoSameSiteCookieRequired', NoSameSiteCookieRequired::class)
9598
->willReturn(false);
9699

97100
$this->request->method('passesLaxCookieCheck')

tests/lib/AppFramework/Middleware/Security/SecurityMiddlewareTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ protected function setUp(): void {
7272
'test',
7373
$this->request
7474
);
75-
$this->reader = new ControllerMethodReflector();
75+
$this->reader = new ControllerMethodReflector(\OCP\Server::get(LoggerInterface::class));
7676
$this->logger = $this->createMock(LoggerInterface::class);
7777
$this->navigationManager = $this->createMock(INavigationManager::class);
7878
$this->urlGenerator = $this->createMock(IURLGenerator::class);
@@ -433,6 +433,7 @@ public function testCsrfOcsController(string $controllerClass, bool $hasOcsApiHe
433433
->willReturn(true);
434434

435435
$controller = new $controllerClass('test', $this->request);
436+
$this->reader->reflect($controller, 'foo');
436437

437438
try {
438439
$this->middleware->beforeController($controller, 'foo');

0 commit comments

Comments
 (0)