1
0

ProvisioningApiMiddlewareTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\Provisioning_API\Tests\Middleware;
  7. use OCA\Provisioning_API\Middleware\Exceptions\NotSubAdminException;
  8. use OCA\Provisioning_API\Middleware\ProvisioningApiMiddleware;
  9. use OCP\AppFramework\Controller;
  10. use OCP\AppFramework\Http;
  11. use OCP\AppFramework\OCS\OCSException;
  12. use OCP\AppFramework\Utility\IControllerMethodReflector;
  13. use Test\TestCase;
  14. class ProvisioningApiMiddlewareTest extends TestCase {
  15. /** @var IControllerMethodReflector|\PHPUnit\Framework\MockObject\MockObject */
  16. private $reflector;
  17. protected function setUp(): void {
  18. parent::setUp();
  19. $this->reflector = $this->createMock(IControllerMethodReflector::class);
  20. }
  21. public function dataAnnotation() {
  22. return [
  23. [false, false, false, false, false],
  24. [false, false, true, false, false],
  25. [false, true, true, false, false],
  26. [ true, false, false, false, true],
  27. [ true, false, true, false, false],
  28. [ true, true, false, false, false],
  29. [ true, true, true, false, false],
  30. [false, false, false, true, false],
  31. [false, false, true, true, false],
  32. [false, true, true, true, false],
  33. [ true, false, false, true, false],
  34. [ true, false, true, true, false],
  35. [ true, true, false, true, false],
  36. [ true, true, true, true, false],
  37. ];
  38. }
  39. /**
  40. * @dataProvider dataAnnotation
  41. *
  42. * @param bool $subadminRequired
  43. * @param bool $isAdmin
  44. * @param bool $isSubAdmin
  45. * @param bool $shouldThrowException
  46. */
  47. public function testBeforeController($subadminRequired, $isAdmin, $isSubAdmin, $hasSettingAuthorizationAnnotation, $shouldThrowException) {
  48. $middleware = new ProvisioningApiMiddleware(
  49. $this->reflector,
  50. $isAdmin,
  51. $isSubAdmin
  52. );
  53. $this->reflector->method('hasAnnotation')
  54. ->willReturnCallback(function ($annotation) use ($subadminRequired, $hasSettingAuthorizationAnnotation) {
  55. if ($annotation === 'NoSubAdminRequired') {
  56. return !$subadminRequired;
  57. }
  58. if ($annotation === 'AuthorizedAdminSetting') {
  59. return $hasSettingAuthorizationAnnotation;
  60. }
  61. return false;
  62. });
  63. try {
  64. $middleware->beforeController(
  65. $this->createMock(Controller::class),
  66. 'myMethod'
  67. );
  68. $this->assertFalse($shouldThrowException);
  69. } catch (NotSubAdminException $e) {
  70. $this->assertTrue($shouldThrowException);
  71. }
  72. }
  73. public function dataAfterException() {
  74. return [
  75. [new NotSubAdminException(), false],
  76. [new \Exception('test', 42), true],
  77. ];
  78. }
  79. /**
  80. * @dataProvider dataAfterException
  81. *
  82. * @param \Exception $e
  83. * @param bool $forwared
  84. */
  85. public function testAfterException(\Exception $exception, $forwared) {
  86. $middleware = new ProvisioningApiMiddleware(
  87. $this->reflector,
  88. false,
  89. false
  90. );
  91. try {
  92. $middleware->afterException(
  93. $this->createMock(Controller::class),
  94. 'myMethod',
  95. $exception
  96. );
  97. $this->fail();
  98. } catch (OCSException $e) {
  99. $this->assertFalse($forwared);
  100. $this->assertSame($exception->getMessage(), $e->getMessage());
  101. $this->assertSame(Http::STATUS_FORBIDDEN, $e->getCode());
  102. } catch (\Exception $e) {
  103. $this->assertTrue($forwared);
  104. $this->assertSame($exception, $e);
  105. }
  106. }
  107. }