1
0

ShareInfoMiddlewareTest.php 3.9 KB

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