1
0

SessionMiddlewareTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2016-2023 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  6. * SPDX-License-Identifier: AGPL-3.0-or-later
  7. */
  8. namespace Test\AppFramework\Middleware;
  9. use OC\AppFramework\Middleware\SessionMiddleware;
  10. use OC\AppFramework\Utility\ControllerMethodReflector;
  11. use OCP\AppFramework\Controller;
  12. use OCP\AppFramework\Http\Attribute\UseSession;
  13. use OCP\AppFramework\Http\Response;
  14. use OCP\IRequest;
  15. use OCP\ISession;
  16. use PHPUnit\Framework\MockObject\MockObject;
  17. use Test\TestCase;
  18. class SessionMiddlewareTest extends TestCase {
  19. private ControllerMethodReflector|MockObject $reflector;
  20. private ISession|MockObject $session;
  21. private Controller $controller;
  22. private SessionMiddleware $middleware;
  23. protected function setUp(): void {
  24. parent::setUp();
  25. $this->reflector = $this->createMock(ControllerMethodReflector::class);
  26. $this->session = $this->createMock(ISession::class);
  27. $this->controller = new class('app', $this->createMock(IRequest::class)) extends Controller {
  28. /**
  29. * @UseSession
  30. */
  31. public function withAnnotation() {
  32. }
  33. #[UseSession]
  34. public function withAttribute() {
  35. }
  36. public function without() {
  37. }
  38. };
  39. $this->middleware = new SessionMiddleware(
  40. $this->reflector,
  41. $this->session,
  42. );
  43. }
  44. public function testSessionNotClosedOnBeforeController(): void {
  45. $this->configureSessionMock(0, 1);
  46. $this->reflector->expects(self::once())
  47. ->method('hasAnnotation')
  48. ->with('UseSession')
  49. ->willReturn(true);
  50. $this->middleware->beforeController($this->controller, 'withAnnotation');
  51. }
  52. public function testSessionNotClosedOnBeforeControllerWithAttribute(): void {
  53. $this->configureSessionMock(0, 1);
  54. $this->reflector->expects(self::once())
  55. ->method('hasAnnotation')
  56. ->with('UseSession')
  57. ->willReturn(false);
  58. $this->middleware->beforeController($this->controller, 'withAttribute');
  59. }
  60. public function testSessionClosedOnAfterController(): void {
  61. $this->configureSessionMock(1);
  62. $this->reflector->expects(self::once())
  63. ->method('hasAnnotation')
  64. ->with('UseSession')
  65. ->willReturn(true);
  66. $this->middleware->afterController($this->controller, 'withAnnotation', new Response());
  67. }
  68. public function testSessionClosedOnAfterControllerWithAttribute(): void {
  69. $this->configureSessionMock(1);
  70. $this->reflector->expects(self::once())
  71. ->method('hasAnnotation')
  72. ->with('UseSession')
  73. ->willReturn(true);
  74. $this->middleware->afterController($this->controller, 'withAttribute', new Response());
  75. }
  76. public function testSessionReopenedAndClosedOnBeforeController(): void {
  77. $this->configureSessionMock(1, 1);
  78. $this->reflector->expects(self::exactly(2))
  79. ->method('hasAnnotation')
  80. ->with('UseSession')
  81. ->willReturn(true);
  82. $this->middleware->beforeController($this->controller, 'withAnnotation');
  83. $this->middleware->afterController($this->controller, 'withAnnotation', new Response());
  84. }
  85. public function testSessionReopenedAndClosedOnBeforeControllerWithAttribute(): void {
  86. $this->configureSessionMock(1, 1);
  87. $this->reflector->expects(self::exactly(2))
  88. ->method('hasAnnotation')
  89. ->with('UseSession')
  90. ->willReturn(false);
  91. $this->middleware->beforeController($this->controller, 'withAttribute');
  92. $this->middleware->afterController($this->controller, 'withAttribute', new Response());
  93. }
  94. public function testSessionClosedOnBeforeController(): void {
  95. $this->configureSessionMock(0);
  96. $this->reflector->expects(self::once())
  97. ->method('hasAnnotation')
  98. ->with('UseSession')
  99. ->willReturn(false);
  100. $this->middleware->beforeController($this->controller, 'without');
  101. }
  102. public function testSessionNotClosedOnAfterController(): void {
  103. $this->configureSessionMock(0);
  104. $this->reflector->expects(self::once())
  105. ->method('hasAnnotation')
  106. ->with('UseSession')
  107. ->willReturn(false);
  108. $this->middleware->afterController($this->controller, 'without', new Response());
  109. }
  110. private function configureSessionMock(int $expectedCloseCount, int $expectedReopenCount = 0): void {
  111. $this->session->expects($this->exactly($expectedCloseCount))
  112. ->method('close');
  113. $this->session->expects($this->exactly($expectedReopenCount))
  114. ->method('reopen');
  115. }
  116. }