1
0

ShareInfoMiddlewareTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\Files_Sharing\Tests\Middleware;
  7. use OCA\Files_Sharing\Controller\ShareInfoController;
  8. use OCA\Files_Sharing\Exceptions\S2SException;
  9. use OCA\Files_Sharing\Middleware\ShareInfoMiddleware;
  10. use OCP\AppFramework\Controller;
  11. use OCP\AppFramework\Http;
  12. use OCP\AppFramework\Http\JSONResponse;
  13. use OCP\Share\IManager as ShareManager;
  14. use Test\TestCase;
  15. class ShareInfoMiddlewareTest extends TestCase {
  16. /** @var ShareManager|\PHPUnit\Framework\MockObject\MockObject */
  17. private $shareManager;
  18. /** @var ShareInfoMiddleware */
  19. private $middleware;
  20. protected function setUp(): void {
  21. parent::setUp();
  22. $this->shareManager = $this->createMock(ShareManager::class);
  23. $this->middleware = new ShareInfoMiddleware($this->shareManager);
  24. }
  25. public function testBeforeControllerNoShareInfo() {
  26. $this->shareManager->expects($this->never())
  27. ->method($this->anything());
  28. $this->middleware->beforeController($this->createMock(ShareInfoMiddlewareTestController::class), 'foo');
  29. }
  30. public function testBeforeControllerShareInfoNoS2s() {
  31. $this->shareManager->expects($this->once())
  32. ->method('outgoingServer2ServerSharesAllowed')
  33. ->willReturn(false);
  34. $this->expectException(S2SException::class);
  35. $this->middleware->beforeController($this->createMock(ShareInfoController::class), 'foo');
  36. }
  37. public function testBeforeControllerShareInfo() {
  38. $this->shareManager->expects($this->once())
  39. ->method('outgoingServer2ServerSharesAllowed')
  40. ->willReturn(true);
  41. $this->middleware->beforeController($this->createMock(ShareInfoController::class), 'foo');
  42. }
  43. public function testAfterExceptionNoShareInfo() {
  44. $exeption = new \Exception();
  45. try {
  46. $this->middleware->afterException($this->createMock(ShareInfoMiddlewareTestController::class), 'foo', $exeption);
  47. $this->fail();
  48. } catch (\Exception $e) {
  49. $this->assertSame($exeption, $e);
  50. }
  51. }
  52. public function testAfterExceptionNoS2S() {
  53. $exeption = new \Exception();
  54. try {
  55. $this->middleware->afterException($this->createMock(ShareInfoController::class), 'foo', $exeption);
  56. $this->fail();
  57. } catch (\Exception $e) {
  58. $this->assertSame($exeption, $e);
  59. }
  60. }
  61. public function testAfterExceptionS2S() {
  62. $expected = new JSONResponse([], Http::STATUS_NOT_FOUND);
  63. $this->assertEquals(
  64. $expected,
  65. $this->middleware->afterException($this->createMock(ShareInfoController::class), 'foo', new S2SException())
  66. );
  67. }
  68. public function testAfterControllerNoShareInfo() {
  69. $response = $this->createMock(Http\Response::class);
  70. $this->assertEquals(
  71. $response,
  72. $this->middleware->afterController($this->createMock(ShareInfoMiddlewareTestController::class), 'foo', $response)
  73. );
  74. }
  75. public function testAfterControllerNoJSON() {
  76. $response = $this->createMock(Http\Response::class);
  77. $this->assertEquals(
  78. $response,
  79. $this->middleware->afterController($this->createMock(ShareInfoController::class), 'foo', $response)
  80. );
  81. }
  82. public function testAfterControllerJSONok() {
  83. $data = ['foo' => 'bar'];
  84. $response = new JSONResponse($data);
  85. $expected = new JSONResponse([
  86. 'data' => $data,
  87. 'status' => 'success',
  88. ]);
  89. $this->assertEquals(
  90. $expected,
  91. $this->middleware->afterController($this->createMock(ShareInfoController::class), 'foo', $response)
  92. );
  93. }
  94. public function testAfterControllerJSONerror() {
  95. $data = ['foo' => 'bar'];
  96. $response = new JSONResponse($data, Http::STATUS_FORBIDDEN);
  97. $expected = new JSONResponse([
  98. 'data' => $data,
  99. 'status' => 'error',
  100. ], Http::STATUS_FORBIDDEN);
  101. $this->assertEquals(
  102. $expected,
  103. $this->middleware->afterController($this->createMock(ShareInfoController::class), 'foo', $response)
  104. );
  105. }
  106. }
  107. class ShareInfoMiddlewareTestController extends Controller {
  108. }