sessionmiddlewaretest.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. /**
  3. * ownCloud - App Framework
  4. *
  5. * This file is licensed under the Affero General Public License version 3 or
  6. * later. See the COPYING file.
  7. *
  8. * @author Thomas Müller <deepdiver@owncloud.com>
  9. * @copyright Thomas Müller 2014
  10. */
  11. namespace OC\AppFramework\Middleware\Security;
  12. use OC\AppFramework\Http\Request;
  13. use OC\AppFramework\Middleware\SessionMiddleware;
  14. use OC\AppFramework\Utility\ControllerMethodReflector;
  15. use OCP\AppFramework\Http\Response;
  16. class SessionMiddlewareTest extends \Test\TestCase {
  17. /**
  18. * @var ControllerMethodReflector
  19. */
  20. private $reflector;
  21. /**
  22. * @var Request
  23. */
  24. private $request;
  25. protected function setUp() {
  26. parent::setUp();
  27. $this->request = new Request(
  28. [],
  29. $this->getMockBuilder('\OCP\Security\ISecureRandom')->getMock(),
  30. $this->getMock('\OCP\IConfig')
  31. );
  32. $this->reflector = new ControllerMethodReflector();
  33. }
  34. /**
  35. * @UseSession
  36. */
  37. public function testSessionNotClosedOnBeforeController() {
  38. $session = $this->getSessionMock(0);
  39. $this->reflector->reflect($this, __FUNCTION__);
  40. $middleware = new SessionMiddleware($this->request, $this->reflector, $session);
  41. $middleware->beforeController($this, __FUNCTION__);
  42. }
  43. /**
  44. * @UseSession
  45. */
  46. public function testSessionClosedOnAfterController() {
  47. $session = $this->getSessionMock(1);
  48. $this->reflector->reflect($this, __FUNCTION__);
  49. $middleware = new SessionMiddleware($this->request, $this->reflector, $session);
  50. $middleware->afterController($this, __FUNCTION__, new Response());
  51. }
  52. public function testSessionClosedOnBeforeController() {
  53. $session = $this->getSessionMock(1);
  54. $this->reflector->reflect($this, __FUNCTION__);
  55. $middleware = new SessionMiddleware($this->request, $this->reflector, $session);
  56. $middleware->beforeController($this, __FUNCTION__);
  57. }
  58. public function testSessionNotClosedOnAfterController() {
  59. $session = $this->getSessionMock(0);
  60. $this->reflector->reflect($this, __FUNCTION__);
  61. $middleware = new SessionMiddleware($this->request, $this->reflector, $session);
  62. $middleware->afterController($this, __FUNCTION__, new Response());
  63. }
  64. /**
  65. * @return mixed
  66. */
  67. private function getSessionMock($expectedCloseCount) {
  68. $session = $this->getMockBuilder('\OC\Session\Memory')
  69. ->disableOriginalConstructor()
  70. ->getMock();
  71. $session->expects($this->exactly($expectedCloseCount))
  72. ->method('close');
  73. return $session;
  74. }
  75. }