1
0

ProvisioningApiMiddlewareTest.php 3.3 KB

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