SubadminMiddlewareTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /**
  3. * @author Lukas Reschke
  4. * @copyright 2014 Lukas Reschke lukas@owncloud.com
  5. *
  6. * This file is licensed under the Affero General Public License version 3 or
  7. * later.
  8. * See the COPYING-README file.
  9. */
  10. namespace Tests\Settings\Middleware;
  11. use OC\AppFramework\Middleware\Security\Exceptions\NotAdminException;
  12. use OC\AppFramework\Utility\ControllerMethodReflector;
  13. use OC\Settings\Middleware\SubadminMiddleware;
  14. use OCP\AppFramework\Controller;
  15. use OCP\AppFramework\Http\TemplateResponse;
  16. use OCP\IL10N;
  17. /**
  18. * Verifies whether an user has at least subadmin rights.
  19. * To bypass use the `@NoSubadminRequired` annotation
  20. *
  21. * @package Tests\Settings\Middleware
  22. */
  23. class SubadminMiddlewareTest extends \Test\TestCase {
  24. /** @var SubadminMiddleware */
  25. private $subadminMiddlewareAsSubAdmin;
  26. /** @var SubadminMiddleware */
  27. private $subadminMiddleware;
  28. /** @var ControllerMethodReflector */
  29. private $reflector;
  30. /** @var Controller */
  31. private $controller;
  32. /** @var IL10N */
  33. private $l10n;
  34. protected function setUp() {
  35. parent::setUp();
  36. $this->reflector = $this->getMockBuilder(ControllerMethodReflector::class)
  37. ->disableOriginalConstructor()->getMock();
  38. $this->controller = $this->getMockBuilder(Controller::class)
  39. ->disableOriginalConstructor()->getMock();
  40. $this->l10n = $this->createMock(IL10N::class);
  41. $this->subadminMiddlewareAsSubAdmin = new SubadminMiddleware($this->reflector, true, $this->l10n);
  42. $this->subadminMiddleware = new SubadminMiddleware($this->reflector, false, $this->l10n);
  43. }
  44. /**
  45. * @expectedException \OC\AppFramework\Middleware\Security\Exceptions\NotAdminException
  46. */
  47. public function testBeforeControllerAsUserWithExemption() {
  48. $this->reflector
  49. ->expects($this->once())
  50. ->method('hasAnnotation')
  51. ->with('NoSubadminRequired')
  52. ->will($this->returnValue(false));
  53. $this->subadminMiddleware->beforeController($this->controller, 'foo');
  54. }
  55. public function testBeforeControllerAsUserWithoutExemption() {
  56. $this->reflector
  57. ->expects($this->once())
  58. ->method('hasAnnotation')
  59. ->with('NoSubadminRequired')
  60. ->will($this->returnValue(true));
  61. $this->subadminMiddleware->beforeController($this->controller, 'foo');
  62. }
  63. public function testBeforeControllerAsSubAdminWithoutExemption() {
  64. $this->reflector
  65. ->expects($this->once())
  66. ->method('hasAnnotation')
  67. ->with('NoSubadminRequired')
  68. ->will($this->returnValue(false));
  69. $this->subadminMiddlewareAsSubAdmin->beforeController($this->controller, 'foo');
  70. }
  71. public function testBeforeControllerAsSubAdminWithExemption() {
  72. $this->reflector
  73. ->expects($this->once())
  74. ->method('hasAnnotation')
  75. ->with('NoSubadminRequired')
  76. ->will($this->returnValue(true));
  77. $this->subadminMiddlewareAsSubAdmin->beforeController($this->controller, 'foo');
  78. }
  79. public function testAfterNotAdminException() {
  80. $expectedResponse = new TemplateResponse('core', '403', array(), 'guest');
  81. $expectedResponse->setStatus(403);
  82. $this->assertEquals($expectedResponse, $this->subadminMiddleware->afterException($this->controller, 'foo', new NotAdminException('')));
  83. }
  84. /**
  85. * @expectedException \Exception
  86. */
  87. public function testAfterRegularException() {
  88. $expectedResponse = new TemplateResponse('core', '403', array(), 'guest');
  89. $expectedResponse->setStatus(403);
  90. $this->subadminMiddleware->afterException($this->controller, 'foo', new \Exception());
  91. }
  92. }