ProvisioningApiMiddlewareTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. /**
  3. * @copyright 2016, Roeland Jago Douma <roeland@famdouma.nl>
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OCA\Provisioning_API\Tests\Middleware;
  26. use OCA\Provisioning_API\Middleware\Exceptions\NotSubAdminException;
  27. use OCA\Provisioning_API\Middleware\ProvisioningApiMiddleware;
  28. use OCP\AppFramework\Controller;
  29. use OCP\AppFramework\Http;
  30. use OCP\AppFramework\OCS\OCSException;
  31. use OCP\AppFramework\Utility\IControllerMethodReflector;
  32. use Test\TestCase;
  33. class ProvisioningApiMiddlewareTest extends TestCase {
  34. /** @var IControllerMethodReflector|\PHPUnit\Framework\MockObject\MockObject */
  35. private $reflector;
  36. protected function setUp(): void {
  37. parent::setUp();
  38. $this->reflector = $this->createMock(IControllerMethodReflector::class);
  39. }
  40. public function dataAnnotation() {
  41. return [
  42. [false, false, false, false, false],
  43. [false, false, true, false, false],
  44. [false, true, true, false, false],
  45. [ true, false, false, false, true],
  46. [ true, false, true, false, false],
  47. [ true, true, false, false, false],
  48. [ true, true, true, false, false],
  49. [false, false, false, true, false],
  50. [false, false, true, true, false],
  51. [false, true, true, true, false],
  52. [ true, false, false, true, false],
  53. [ true, false, true, true, false],
  54. [ true, true, false, true, false],
  55. [ true, true, true, true, false],
  56. ];
  57. }
  58. /**
  59. * @dataProvider dataAnnotation
  60. *
  61. * @param bool $subadminRequired
  62. * @param bool $isAdmin
  63. * @param bool $isSubAdmin
  64. * @param bool $shouldThrowException
  65. */
  66. public function testBeforeController($subadminRequired, $isAdmin, $isSubAdmin, $hasSettingAuthorizationAnnotation, $shouldThrowException) {
  67. $middleware = new ProvisioningApiMiddleware(
  68. $this->reflector,
  69. $isAdmin,
  70. $isSubAdmin
  71. );
  72. $this->reflector->method('hasAnnotation')
  73. ->willReturnCallback(function ($annotation) use ($subadminRequired, $hasSettingAuthorizationAnnotation) {
  74. if ($annotation === 'NoSubAdminRequired') {
  75. return !$subadminRequired;
  76. }
  77. if ($annotation === 'AuthorizedAdminSetting') {
  78. return $hasSettingAuthorizationAnnotation;
  79. }
  80. return false;
  81. });
  82. try {
  83. $middleware->beforeController(
  84. $this->createMock(Controller::class),
  85. 'myMethod'
  86. );
  87. $this->assertFalse($shouldThrowException);
  88. } catch (NotSubAdminException $e) {
  89. $this->assertTrue($shouldThrowException);
  90. }
  91. }
  92. public function dataAfterException() {
  93. return [
  94. [new NotSubAdminException(), false],
  95. [new \Exception('test', 42), true],
  96. ];
  97. }
  98. /**
  99. * @dataProvider dataAfterException
  100. *
  101. * @param \Exception $e
  102. * @param bool $forwared
  103. */
  104. public function testAfterException(\Exception $exception, $forwared) {
  105. $middleware = new ProvisioningApiMiddleware(
  106. $this->reflector,
  107. false,
  108. false
  109. );
  110. try {
  111. $middleware->afterException(
  112. $this->createMock(Controller::class),
  113. 'myMethod',
  114. $exception
  115. );
  116. $this->fail();
  117. } catch (OCSException $e) {
  118. $this->assertFalse($forwared);
  119. $this->assertSame($exception->getMessage(), $e->getMessage());
  120. $this->assertSame(Http::STATUS_FORBIDDEN, $e->getCode());
  121. } catch (\Exception $e) {
  122. $this->assertTrue($forwared);
  123. $this->assertSame($exception, $e);
  124. }
  125. }
  126. }