UserStoragesControllerTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. class UserStoragesControllerTest extends StoragesControllerTest {
  41. /**
  42. * @var array
  43. */
  44. private $oldAllowedBackends;
  45. protected function setUp(): void {
  46. parent::setUp();
  47. $this->service = $this->getMockBuilder('\OCA\Files_External\Service\UserStoragesService')
  48. ->disableOriginalConstructor()
  49. ->getMock();
  50. $this->service->method('getVisibilityType')
  51. ->willReturn(BackendService::VISIBILITY_PERSONAL);
  52. $this->controller = $this->createController(true);
  53. }
  54. private function createController($allowCreateLocal = true) {
  55. $session = $this->createMock(IUserSession::class);
  56. $session->method('getUser')
  57. ->willReturn(new User('test', null, $this->createMock(IEventDispatcher::class)));
  58. $config = $this->createMock(IConfig::class);
  59. $config->method('getSystemValue')
  60. ->with('files_external_allow_create_new_local', true)
  61. ->willReturn($allowCreateLocal);
  62. return new UserStoragesController(
  63. 'files_external',
  64. $this->createMock(IRequest::class),
  65. $this->createMock(IL10N::class),
  66. $this->service,
  67. $this->createMock(LoggerInterface::class),
  68. $session,
  69. $this->createMock(IGroupManager::class),
  70. $config
  71. );
  72. }
  73. public function testAddLocalStorageWhenDisabled() {
  74. $this->controller = $this->createController(false);
  75. parent::testAddLocalStorageWhenDisabled();
  76. }
  77. public function testAddOrUpdateStorageDisallowedBackend() {
  78. $backend = $this->getBackendMock();
  79. $backend->method('isVisibleFor')
  80. ->with(BackendService::VISIBILITY_PERSONAL)
  81. ->willReturn(false);
  82. $authMech = $this->getAuthMechMock();
  83. $storageConfig = new StorageConfig(1);
  84. $storageConfig->setMountPoint('mount');
  85. $storageConfig->setBackend($backend);
  86. $storageConfig->setAuthMechanism($authMech);
  87. $storageConfig->setBackendOptions([]);
  88. $this->service->expects($this->exactly(2))
  89. ->method('createStorage')
  90. ->willReturn($storageConfig);
  91. $this->service->expects($this->never())
  92. ->method('addStorage');
  93. $this->service->expects($this->never())
  94. ->method('updateStorage');
  95. $response = $this->controller->create(
  96. 'mount',
  97. '\OCA\Files_External\Lib\Storage\SMB',
  98. '\Auth\Mechanism',
  99. [],
  100. [],
  101. [],
  102. [],
  103. null
  104. );
  105. $this->assertEquals(Http::STATUS_UNPROCESSABLE_ENTITY, $response->getStatus());
  106. $response = $this->controller->update(
  107. 1,
  108. 'mount',
  109. '\OCA\Files_External\Lib\Storage\SMB',
  110. '\Auth\Mechanism',
  111. [],
  112. [],
  113. [],
  114. [],
  115. null
  116. );
  117. $this->assertEquals(Http::STATUS_UNPROCESSABLE_ENTITY, $response->getStatus());
  118. }
  119. }