UserStoragesControllerTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Robin Appelman <robin@icewind.nl>
  8. * @author Robin McCorkell <robin@mccorkell.me.uk>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. * @author Vincent Petry <vincent@nextcloud.com>
  11. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OCA\Files_External\Tests\Controller;
  28. use OC\User\User;
  29. use OCA\Files_External\Controller\UserStoragesController;
  30. use OCA\Files_External\Lib\StorageConfig;
  31. use OCA\Files_External\Service\BackendService;
  32. use OCP\AppFramework\Http;
  33. use OCP\EventDispatcher\IEventDispatcher;
  34. use OCP\IConfig;
  35. use OCP\IGroupManager;
  36. use OCP\IL10N;
  37. use OCP\IRequest;
  38. use OCP\IUserSession;
  39. use Psr\Log\LoggerInterface;
  40. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  41. class UserStoragesControllerTest extends StoragesControllerTest {
  42. /**
  43. * @var array
  44. */
  45. private $oldAllowedBackends;
  46. protected function setUp(): void {
  47. parent::setUp();
  48. $this->service = $this->getMockBuilder('\OCA\Files_External\Service\UserStoragesService')
  49. ->disableOriginalConstructor()
  50. ->getMock();
  51. $this->service->method('getVisibilityType')
  52. ->willReturn(BackendService::VISIBILITY_PERSONAL);
  53. $this->controller = $this->createController(true);
  54. }
  55. private function createController($allowCreateLocal = true) {
  56. $session = $this->createMock(IUserSession::class);
  57. $session->method('getUser')
  58. ->willReturn(new User('test', null, $this->createMock(IEventDispatcher::class)));
  59. $config = $this->createMock(IConfig::class);
  60. $config->method('getSystemValue')
  61. ->with('files_external_allow_create_new_local', true)
  62. ->willReturn($allowCreateLocal);
  63. return new UserStoragesController(
  64. 'files_external',
  65. $this->createMock(IRequest::class),
  66. $this->createMock(IL10N::class),
  67. $this->service,
  68. $this->createMock(LoggerInterface::class),
  69. $session,
  70. $this->createMock(IGroupManager::class),
  71. $config
  72. );
  73. }
  74. public function testAddLocalStorageWhenDisabled() {
  75. $this->controller = $this->createController(false);
  76. parent::testAddLocalStorageWhenDisabled();
  77. }
  78. public function testAddOrUpdateStorageDisallowedBackend() {
  79. $backend = $this->getBackendMock();
  80. $backend->method('isVisibleFor')
  81. ->with(BackendService::VISIBILITY_PERSONAL)
  82. ->willReturn(false);
  83. $authMech = $this->getAuthMechMock();
  84. $storageConfig = new StorageConfig(1);
  85. $storageConfig->setMountPoint('mount');
  86. $storageConfig->setBackend($backend);
  87. $storageConfig->setAuthMechanism($authMech);
  88. $storageConfig->setBackendOptions([]);
  89. $this->service->expects($this->exactly(2))
  90. ->method('createStorage')
  91. ->willReturn($storageConfig);
  92. $this->service->expects($this->never())
  93. ->method('addStorage');
  94. $this->service->expects($this->never())
  95. ->method('updateStorage');
  96. $response = $this->controller->create(
  97. 'mount',
  98. '\OCA\Files_External\Lib\Storage\SMB',
  99. '\Auth\Mechanism',
  100. [],
  101. [],
  102. [],
  103. [],
  104. null
  105. );
  106. $this->assertEquals(Http::STATUS_UNPROCESSABLE_ENTITY, $response->getStatus());
  107. $response = $this->controller->update(
  108. 1,
  109. 'mount',
  110. '\OCA\Files_External\Lib\Storage\SMB',
  111. '\Auth\Mechanism',
  112. [],
  113. [],
  114. [],
  115. [],
  116. null
  117. );
  118. $this->assertEquals(Http::STATUS_UNPROCESSABLE_ENTITY, $response->getStatus());
  119. }
  120. }