SubadminMiddlewareTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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->once())
  65. ->method('hasAnnotation')
  66. ->with('NoSubadminRequired')
  67. ->will($this->returnValue(false));
  68. $this->subadminMiddleware->beforeController($this->controller, 'foo');
  69. }
  70. public function testBeforeControllerAsUserWithoutExemption() {
  71. $this->reflector
  72. ->expects($this->once())
  73. ->method('hasAnnotation')
  74. ->with('NoSubadminRequired')
  75. ->will($this->returnValue(true));
  76. $this->subadminMiddleware->beforeController($this->controller, 'foo');
  77. }
  78. public function testBeforeControllerAsSubAdminWithoutExemption() {
  79. $this->reflector
  80. ->expects($this->once())
  81. ->method('hasAnnotation')
  82. ->with('NoSubadminRequired')
  83. ->will($this->returnValue(false));
  84. $this->subadminMiddlewareAsSubAdmin->beforeController($this->controller, 'foo');
  85. }
  86. public function testBeforeControllerAsSubAdminWithExemption() {
  87. $this->reflector
  88. ->expects($this->once())
  89. ->method('hasAnnotation')
  90. ->with('NoSubadminRequired')
  91. ->will($this->returnValue(true));
  92. $this->subadminMiddlewareAsSubAdmin->beforeController($this->controller, 'foo');
  93. }
  94. public function testAfterNotAdminException() {
  95. $expectedResponse = new TemplateResponse('core', '403', array(), 'guest');
  96. $expectedResponse->setStatus(403);
  97. $this->assertEquals($expectedResponse, $this->subadminMiddleware->afterException($this->controller, 'foo', new NotAdminException('')));
  98. }
  99. public function testAfterRegularException() {
  100. $this->expectException(\Exception::class);
  101. $expectedResponse = new TemplateResponse('core', '403', array(), 'guest');
  102. $expectedResponse->setStatus(403);
  103. $this->subadminMiddleware->afterException($this->controller, 'foo', new \Exception());
  104. }
  105. }