UserStoragesControllerTest.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Files_External\Tests\Controller;
  8. use OC\User\User;
  9. use OCA\Files_External\Controller\UserStoragesController;
  10. use OCA\Files_External\Lib\StorageConfig;
  11. use OCA\Files_External\Service\BackendService;
  12. use OCP\AppFramework\Http;
  13. use OCP\EventDispatcher\IEventDispatcher;
  14. use OCP\IConfig;
  15. use OCP\IGroupManager;
  16. use OCP\IL10N;
  17. use OCP\IRequest;
  18. use OCP\IUserSession;
  19. use Psr\Log\LoggerInterface;
  20. class UserStoragesControllerTest extends StoragesControllerTest {
  21. /**
  22. * @var array
  23. */
  24. private $oldAllowedBackends;
  25. protected function setUp(): void {
  26. parent::setUp();
  27. $this->service = $this->getMockBuilder('\OCA\Files_External\Service\UserStoragesService')
  28. ->disableOriginalConstructor()
  29. ->getMock();
  30. $this->service->method('getVisibilityType')
  31. ->willReturn(BackendService::VISIBILITY_PERSONAL);
  32. $this->controller = $this->createController(true);
  33. }
  34. private function createController($allowCreateLocal = true) {
  35. $session = $this->createMock(IUserSession::class);
  36. $session->method('getUser')
  37. ->willReturn(new User('test', null, $this->createMock(IEventDispatcher::class)));
  38. $config = $this->createMock(IConfig::class);
  39. $config->method('getSystemValue')
  40. ->with('files_external_allow_create_new_local', true)
  41. ->willReturn($allowCreateLocal);
  42. return new UserStoragesController(
  43. 'files_external',
  44. $this->createMock(IRequest::class),
  45. $this->createMock(IL10N::class),
  46. $this->service,
  47. $this->createMock(LoggerInterface::class),
  48. $session,
  49. $this->createMock(IGroupManager::class),
  50. $config
  51. );
  52. }
  53. public function testAddLocalStorageWhenDisabled(): void {
  54. $this->controller = $this->createController(false);
  55. parent::testAddLocalStorageWhenDisabled();
  56. }
  57. public function testAddOrUpdateStorageDisallowedBackend(): void {
  58. $backend = $this->getBackendMock();
  59. $backend->method('isVisibleFor')
  60. ->with(BackendService::VISIBILITY_PERSONAL)
  61. ->willReturn(false);
  62. $authMech = $this->getAuthMechMock();
  63. $storageConfig = new StorageConfig(1);
  64. $storageConfig->setMountPoint('mount');
  65. $storageConfig->setBackend($backend);
  66. $storageConfig->setAuthMechanism($authMech);
  67. $storageConfig->setBackendOptions([]);
  68. $this->service->expects($this->exactly(2))
  69. ->method('createStorage')
  70. ->willReturn($storageConfig);
  71. $this->service->expects($this->never())
  72. ->method('addStorage');
  73. $this->service->expects($this->never())
  74. ->method('updateStorage');
  75. $response = $this->controller->create(
  76. 'mount',
  77. '\OCA\Files_External\Lib\Storage\SMB',
  78. '\Auth\Mechanism',
  79. [],
  80. [],
  81. [],
  82. [],
  83. null
  84. );
  85. $this->assertEquals(Http::STATUS_UNPROCESSABLE_ENTITY, $response->getStatus());
  86. $response = $this->controller->update(
  87. 1,
  88. 'mount',
  89. '\OCA\Files_External\Lib\Storage\SMB',
  90. '\Auth\Mechanism',
  91. [],
  92. [],
  93. [],
  94. [],
  95. null
  96. );
  97. $this->assertEquals(Http::STATUS_UNPROCESSABLE_ENTITY, $response->getStatus());
  98. }
  99. }