ShareInfoMiddlewareTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Roeland Jago Douma <roeland@famdouma.nl>
  4. *
  5. * @author Morris Jobke <hey@morrisjobke.de>
  6. * @author Roeland Jago Douma <roeland@famdouma.nl>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\Files_Sharing\Tests\Middleware;
  25. use OCA\Files_Sharing\Controller\ShareInfoController;
  26. use OCA\Files_Sharing\Exceptions\S2SException;
  27. use OCA\Files_Sharing\Middleware\ShareInfoMiddleware;
  28. use OCP\AppFramework\Controller;
  29. use OCP\AppFramework\Http;
  30. use OCP\AppFramework\Http\JSONResponse;
  31. use OCP\Share\IManager as ShareManager;
  32. use Test\TestCase;
  33. class ShareInfoMiddlewareTest extends TestCase {
  34. /** @var ShareManager|\PHPUnit\Framework\MockObject\MockObject */
  35. private $shareManager;
  36. /** @var ShareInfoMiddleware */
  37. private $middleware;
  38. protected function setUp(): void {
  39. parent::setUp();
  40. $this->shareManager = $this->createMock(ShareManager::class);
  41. $this->middleware = new ShareInfoMiddleware($this->shareManager);
  42. }
  43. public function testBeforeControllerNoShareInfo() {
  44. $this->shareManager->expects($this->never())
  45. ->method($this->anything());
  46. $this->middleware->beforeController($this->createMock(ShareInfoMiddlewareTestController::class), 'foo');
  47. }
  48. public function testBeforeControllerShareInfoNoS2s() {
  49. $this->shareManager->expects($this->once())
  50. ->method('outgoingServer2ServerSharesAllowed')
  51. ->willReturn(false);
  52. $this->expectException(S2SException::class);
  53. $this->middleware->beforeController($this->createMock(ShareInfoController::class), 'foo');
  54. }
  55. public function testBeforeControllerShareInfo() {
  56. $this->shareManager->expects($this->once())
  57. ->method('outgoingServer2ServerSharesAllowed')
  58. ->willReturn(true);
  59. $this->middleware->beforeController($this->createMock(ShareInfoController::class), 'foo');
  60. }
  61. public function testAfterExceptionNoShareInfo() {
  62. $exeption = new \Exception();
  63. try {
  64. $this->middleware->afterException($this->createMock(ShareInfoMiddlewareTestController::class), 'foo', $exeption);
  65. $this->fail();
  66. } catch (\Exception $e) {
  67. $this->assertSame($exeption, $e);
  68. }
  69. }
  70. public function testAfterExceptionNoS2S() {
  71. $exeption = new \Exception();
  72. try {
  73. $this->middleware->afterException($this->createMock(ShareInfoController::class), 'foo', $exeption);
  74. $this->fail();
  75. } catch (\Exception $e) {
  76. $this->assertSame($exeption, $e);
  77. }
  78. }
  79. public function testAfterExceptionS2S() {
  80. $expected = new JSONResponse([], Http::STATUS_NOT_FOUND);
  81. $this->assertEquals(
  82. $expected,
  83. $this->middleware->afterException($this->createMock(ShareInfoController::class), 'foo', new S2SException())
  84. );
  85. }
  86. public function testAfterControllerNoShareInfo() {
  87. $response = $this->createMock(Http\Response::class);
  88. $this->assertEquals(
  89. $response,
  90. $this->middleware->afterController($this->createMock(ShareInfoMiddlewareTestController::class), 'foo', $response)
  91. );
  92. }
  93. public function testAfterControllerNoJSON() {
  94. $response = $this->createMock(Http\Response::class);
  95. $this->assertEquals(
  96. $response,
  97. $this->middleware->afterController($this->createMock(ShareInfoController::class), 'foo', $response)
  98. );
  99. }
  100. public function testAfterControllerJSONok() {
  101. $data = ['foo' => 'bar'];
  102. $response = new JSONResponse($data);
  103. $expected = new JSONResponse([
  104. 'data' => $data,
  105. 'status' => 'success',
  106. ]);
  107. $this->assertEquals(
  108. $expected,
  109. $this->middleware->afterController($this->createMock(ShareInfoController::class), 'foo', $response)
  110. );
  111. }
  112. public function testAfterControllerJSONerror() {
  113. $data = ['foo' => 'bar'];
  114. $response = new JSONResponse($data, Http::STATUS_FORBIDDEN);
  115. $expected = new JSONResponse([
  116. 'data' => $data,
  117. 'status' => 'error',
  118. ], Http::STATUS_FORBIDDEN);
  119. $this->assertEquals(
  120. $expected,
  121. $this->middleware->afterController($this->createMock(ShareInfoController::class), 'foo', $response)
  122. );
  123. }
  124. }
  125. class ShareInfoMiddlewareTestController extends Controller {
  126. }