SubadminMiddlewareTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. /**
  17. * Verifies whether an user has at least subadmin rights.
  18. * To bypass use the `@NoSubadminRequired` annotation
  19. *
  20. * @package Tests\Settings\Middleware
  21. */
  22. class SubadminMiddlewareTest extends \Test\TestCase {
  23. /** @var SubadminMiddleware */
  24. private $subadminMiddlewareAsSubAdmin;
  25. /** @var SubadminMiddleware */
  26. private $subadminMiddleware;
  27. /** @var ControllerMethodReflector */
  28. private $reflector;
  29. /** @var Controller */
  30. private $controller;
  31. protected function setUp() {
  32. parent::setUp();
  33. $this->reflector = $this->getMockBuilder(ControllerMethodReflector::class)
  34. ->disableOriginalConstructor()->getMock();
  35. $this->controller = $this->getMockBuilder(Controller::class)
  36. ->disableOriginalConstructor()->getMock();
  37. $this->subadminMiddlewareAsSubAdmin = new SubadminMiddleware($this->reflector, true);
  38. $this->subadminMiddleware = new SubadminMiddleware($this->reflector, false);
  39. }
  40. /**
  41. * @expectedException \OC\AppFramework\Middleware\Security\Exceptions\NotAdminException
  42. */
  43. public function testBeforeControllerAsUserWithExemption() {
  44. $this->reflector
  45. ->expects($this->once())
  46. ->method('hasAnnotation')
  47. ->with('NoSubadminRequired')
  48. ->will($this->returnValue(false));
  49. $this->subadminMiddleware->beforeController($this->controller, 'foo');
  50. }
  51. public function testBeforeControllerAsUserWithoutExemption() {
  52. $this->reflector
  53. ->expects($this->once())
  54. ->method('hasAnnotation')
  55. ->with('NoSubadminRequired')
  56. ->will($this->returnValue(true));
  57. $this->subadminMiddleware->beforeController($this->controller, 'foo');
  58. }
  59. public function testBeforeControllerAsSubAdminWithoutExemption() {
  60. $this->reflector
  61. ->expects($this->once())
  62. ->method('hasAnnotation')
  63. ->with('NoSubadminRequired')
  64. ->will($this->returnValue(false));
  65. $this->subadminMiddlewareAsSubAdmin->beforeController($this->controller, 'foo');
  66. }
  67. public function testBeforeControllerAsSubAdminWithExemption() {
  68. $this->reflector
  69. ->expects($this->once())
  70. ->method('hasAnnotation')
  71. ->with('NoSubadminRequired')
  72. ->will($this->returnValue(true));
  73. $this->subadminMiddlewareAsSubAdmin->beforeController($this->controller, 'foo');
  74. }
  75. public function testAfterNotAdminException() {
  76. $expectedResponse = new TemplateResponse('core', '403', array(), 'guest');
  77. $expectedResponse->setStatus(403);
  78. $this->assertEquals($expectedResponse, $this->subadminMiddleware->afterException($this->controller, 'foo', new NotAdminException()));
  79. }
  80. /**
  81. * @expectedException \Exception
  82. */
  83. public function testAfterRegularException() {
  84. $expectedResponse = new TemplateResponse('core', '403', array(), 'guest');
  85. $expectedResponse->setStatus(403);
  86. $this->subadminMiddleware->afterException($this->controller, 'foo', new \Exception());
  87. }
  88. }