1
0

SubadminMiddlewareTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. /**
  3. * @copyright 2014 Lukas Reschke lukas@owncloud.com
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Lukas Reschke <lukas@statuscode.ch>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. *
  11. * @license GNU AGPL version 3 or any later version
  12. *
  13. * This program is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License as
  15. * published by the Free Software Foundation, either version 3 of the
  16. * License, or (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25. *
  26. */
  27. namespace OCA\Settings\Tests\Middleware;
  28. use OC\AppFramework\Middleware\Security\Exceptions\NotAdminException;
  29. use OC\AppFramework\Utility\ControllerMethodReflector;
  30. use OCA\Settings\Middleware\SubadminMiddleware;
  31. use OCP\AppFramework\Controller;
  32. use OCP\AppFramework\Http\TemplateResponse;
  33. use OCP\IL10N;
  34. /**
  35. * Verifies whether an user has at least subadmin rights.
  36. * To bypass use the `@NoSubAdminRequired` annotation
  37. *
  38. * @package Tests\Settings\Middleware
  39. */
  40. class SubadminMiddlewareTest extends \Test\TestCase {
  41. /** @var SubadminMiddleware */
  42. private $subadminMiddlewareAsSubAdmin;
  43. /** @var SubadminMiddleware */
  44. private $subadminMiddleware;
  45. /** @var ControllerMethodReflector */
  46. private $reflector;
  47. /** @var Controller */
  48. private $controller;
  49. /** @var IL10N */
  50. private $l10n;
  51. protected function setUp(): void {
  52. parent::setUp();
  53. $this->reflector = $this->getMockBuilder(ControllerMethodReflector::class)
  54. ->disableOriginalConstructor()->getMock();
  55. $this->controller = $this->getMockBuilder(Controller::class)
  56. ->disableOriginalConstructor()->getMock();
  57. $this->l10n = $this->createMock(IL10N::class);
  58. $this->subadminMiddlewareAsSubAdmin = new SubadminMiddleware($this->reflector, true, $this->l10n);
  59. $this->subadminMiddleware = new SubadminMiddleware($this->reflector, false, $this->l10n);
  60. }
  61. public function testBeforeControllerAsUserWithExemption() {
  62. $this->expectException(\OC\AppFramework\Middleware\Security\Exceptions\NotAdminException::class);
  63. $this->reflector
  64. ->expects($this->exactly(2))
  65. ->method('hasAnnotation')
  66. ->withConsecutive(
  67. ['NoSubAdminRequired'],
  68. ['AuthorizedAdminSetting'],
  69. )->willReturn(false);
  70. $this->subadminMiddleware->beforeController($this->controller, 'foo');
  71. }
  72. public function testBeforeControllerAsUserWithoutExemption() {
  73. $this->reflector
  74. ->expects($this->once())
  75. ->method('hasAnnotation')
  76. ->with('NoSubAdminRequired')
  77. ->willReturn(true);
  78. $this->subadminMiddleware->beforeController($this->controller, 'foo');
  79. }
  80. public function testBeforeControllerAsSubAdminWithoutExemption() {
  81. $this->reflector
  82. ->expects($this->exactly(2))
  83. ->method('hasAnnotation')
  84. ->withConsecutive(
  85. ['NoSubAdminRequired'],
  86. ['AuthorizedAdminSetting'],
  87. )->willReturn(false);
  88. $this->subadminMiddlewareAsSubAdmin->beforeController($this->controller, 'foo');
  89. }
  90. public function testBeforeControllerAsSubAdminWithExemption() {
  91. $this->reflector
  92. ->expects($this->once())
  93. ->method('hasAnnotation')
  94. ->with('NoSubAdminRequired')
  95. ->willReturn(true);
  96. $this->subadminMiddlewareAsSubAdmin->beforeController($this->controller, 'foo');
  97. }
  98. public function testAfterNotAdminException() {
  99. $expectedResponse = new TemplateResponse('core', '403', [], 'guest');
  100. $expectedResponse->setStatus(403);
  101. $this->assertEquals($expectedResponse, $this->subadminMiddleware->afterException($this->controller, 'foo', new NotAdminException('')));
  102. }
  103. public function testAfterRegularException() {
  104. $this->expectException(\Exception::class);
  105. $expectedResponse = new TemplateResponse('core', '403', [], 'guest');
  106. $expectedResponse->setStatus(403);
  107. $this->subadminMiddleware->afterException($this->controller, 'foo', new \Exception());
  108. }
  109. }