StoragesControllerTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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 Morris Jobke <hey@morrisjobke.de>
  8. * @author Robin Appelman <robin@icewind.nl>
  9. * @author Robin McCorkell <robin@mccorkell.me.uk>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. * @author Vincent Petry <vincent@nextcloud.com>
  12. *
  13. * @license AGPL-3.0
  14. *
  15. * This code is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License, version 3,
  17. * as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License, version 3,
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>
  26. *
  27. */
  28. namespace OCA\Files_External\Tests\Controller;
  29. use OCA\Files_External\Controller\GlobalStoragesController;
  30. use OCA\Files_External\Lib\Auth\AuthMechanism;
  31. use OCA\Files_External\Lib\Backend\Backend;
  32. use OCA\Files_External\Lib\StorageConfig;
  33. use OCA\Files_External\NotFoundException;
  34. use OCA\Files_External\Service\GlobalStoragesService;
  35. use OCP\AppFramework\Http;
  36. abstract class StoragesControllerTest extends \Test\TestCase {
  37. /**
  38. * @var GlobalStoragesController
  39. */
  40. protected $controller;
  41. /**
  42. * @var GlobalStoragesService
  43. */
  44. protected $service;
  45. protected function setUp(): void {
  46. \OCA\Files_External\MountConfig::$skipTest = true;
  47. }
  48. protected function tearDown(): void {
  49. \OCA\Files_External\MountConfig::$skipTest = false;
  50. }
  51. /**
  52. * @return \OCA\Files_External\Lib\Backend\Backend
  53. */
  54. protected function getBackendMock($class = '\OCA\Files_External\Lib\Backend\SMB', $storageClass = '\OCA\Files_External\Lib\Storage\SMB') {
  55. $backend = $this->getMockBuilder(Backend::class)
  56. ->disableOriginalConstructor()
  57. ->getMock();
  58. $backend->method('getStorageClass')
  59. ->willReturn($storageClass);
  60. $backend->method('getIdentifier')
  61. ->willReturn('identifier:'.$class);
  62. $backend->method('getParameters')
  63. ->willReturn([]);
  64. return $backend;
  65. }
  66. /**
  67. * @return \OCA\Files_External\Lib\Auth\AuthMechanism
  68. */
  69. protected function getAuthMechMock($scheme = 'null', $class = '\OCA\Files_External\Lib\Auth\NullMechanism') {
  70. $authMech = $this->getMockBuilder(AuthMechanism::class)
  71. ->disableOriginalConstructor()
  72. ->getMock();
  73. $authMech->method('getScheme')
  74. ->willReturn($scheme);
  75. $authMech->method('getIdentifier')
  76. ->willReturn('identifier:'.$class);
  77. $authMech->method('getParameters')
  78. ->willReturn([]);
  79. return $authMech;
  80. }
  81. public function testAddStorage() {
  82. $authMech = $this->getAuthMechMock();
  83. $authMech->method('validateStorage')
  84. ->willReturn(true);
  85. $authMech->method('isVisibleFor')
  86. ->willReturn(true);
  87. $backend = $this->getBackendMock();
  88. $backend->method('validateStorage')
  89. ->willReturn(true);
  90. $backend->method('isVisibleFor')
  91. ->willReturn(true);
  92. $storageConfig = new StorageConfig(1);
  93. $storageConfig->setMountPoint('mount');
  94. $storageConfig->setBackend($backend);
  95. $storageConfig->setAuthMechanism($authMech);
  96. $storageConfig->setBackendOptions([]);
  97. $this->service->expects($this->once())
  98. ->method('createStorage')
  99. ->willReturn($storageConfig);
  100. $this->service->expects($this->once())
  101. ->method('addStorage')
  102. ->willReturn($storageConfig);
  103. $response = $this->controller->create(
  104. 'mount',
  105. '\OCA\Files_External\Lib\Storage\SMB',
  106. '\OCA\Files_External\Lib\Auth\NullMechanism',
  107. [],
  108. [],
  109. [],
  110. [],
  111. null
  112. );
  113. $data = $response->getData();
  114. $this->assertEquals(Http::STATUS_CREATED, $response->getStatus());
  115. $this->assertEquals($storageConfig, $data);
  116. }
  117. public function testAddLocalStorageWhenDisabled() {
  118. $authMech = $this->getAuthMechMock();
  119. $backend = $this->getBackendMock();
  120. $storageConfig = new StorageConfig(1);
  121. $storageConfig->setMountPoint('mount');
  122. $storageConfig->setBackend($backend);
  123. $storageConfig->setAuthMechanism($authMech);
  124. $storageConfig->setBackendOptions([]);
  125. $this->service->expects($this->never())
  126. ->method('createStorage');
  127. $this->service->expects($this->never())
  128. ->method('addStorage');
  129. $response = $this->controller->create(
  130. 'mount',
  131. 'local',
  132. '\OCA\Files_External\Lib\Auth\NullMechanism',
  133. [],
  134. [],
  135. [],
  136. [],
  137. null
  138. );
  139. $data = $response->getData();
  140. $this->assertEquals(Http::STATUS_FORBIDDEN, $response->getStatus());
  141. }
  142. public function testUpdateStorage() {
  143. $authMech = $this->getAuthMechMock();
  144. $authMech->method('validateStorage')
  145. ->willReturn(true);
  146. $authMech->method('isVisibleFor')
  147. ->willReturn(true);
  148. $backend = $this->getBackendMock();
  149. $backend->method('validateStorage')
  150. ->willReturn(true);
  151. $backend->method('isVisibleFor')
  152. ->willReturn(true);
  153. $storageConfig = new StorageConfig(1);
  154. $storageConfig->setMountPoint('mount');
  155. $storageConfig->setBackend($backend);
  156. $storageConfig->setAuthMechanism($authMech);
  157. $storageConfig->setBackendOptions([]);
  158. $this->service->expects($this->once())
  159. ->method('createStorage')
  160. ->willReturn($storageConfig);
  161. $this->service->expects($this->once())
  162. ->method('updateStorage')
  163. ->willReturn($storageConfig);
  164. $response = $this->controller->update(
  165. 1,
  166. 'mount',
  167. '\OCA\Files_External\Lib\Storage\SMB',
  168. '\OCA\Files_External\Lib\Auth\NullMechanism',
  169. [],
  170. [],
  171. [],
  172. [],
  173. null
  174. );
  175. $data = $response->getData();
  176. $this->assertEquals(Http::STATUS_OK, $response->getStatus());
  177. $this->assertEquals($storageConfig, $data);
  178. }
  179. public function mountPointNamesProvider() {
  180. return [
  181. [''],
  182. ['/'],
  183. ['//'],
  184. ];
  185. }
  186. /**
  187. * @dataProvider mountPointNamesProvider
  188. */
  189. public function testAddOrUpdateStorageInvalidMountPoint($mountPoint) {
  190. $storageConfig = new StorageConfig(1);
  191. $storageConfig->setMountPoint($mountPoint);
  192. $storageConfig->setBackend($this->getBackendMock());
  193. $storageConfig->setAuthMechanism($this->getAuthMechMock());
  194. $storageConfig->setBackendOptions([]);
  195. $this->service->expects($this->exactly(2))
  196. ->method('createStorage')
  197. ->willReturn($storageConfig);
  198. $this->service->expects($this->never())
  199. ->method('addStorage');
  200. $this->service->expects($this->never())
  201. ->method('updateStorage');
  202. $response = $this->controller->create(
  203. $mountPoint,
  204. '\OCA\Files_External\Lib\Storage\SMB',
  205. '\OCA\Files_External\Lib\Auth\NullMechanism',
  206. [],
  207. [],
  208. [],
  209. [],
  210. null
  211. );
  212. $this->assertEquals(Http::STATUS_UNPROCESSABLE_ENTITY, $response->getStatus());
  213. $response = $this->controller->update(
  214. 1,
  215. $mountPoint,
  216. '\OCA\Files_External\Lib\Storage\SMB',
  217. '\OCA\Files_External\Lib\Auth\NullMechanism',
  218. [],
  219. [],
  220. [],
  221. [],
  222. null
  223. );
  224. $this->assertEquals(Http::STATUS_UNPROCESSABLE_ENTITY, $response->getStatus());
  225. }
  226. public function testAddOrUpdateStorageInvalidBackend() {
  227. $this->service->expects($this->exactly(2))
  228. ->method('createStorage')
  229. ->will($this->throwException(new \InvalidArgumentException()));
  230. $this->service->expects($this->never())
  231. ->method('addStorage');
  232. $this->service->expects($this->never())
  233. ->method('updateStorage');
  234. $response = $this->controller->create(
  235. 'mount',
  236. '\OC\Files\Storage\InvalidStorage',
  237. '\OCA\Files_External\Lib\Auth\NullMechanism',
  238. [],
  239. [],
  240. [],
  241. [],
  242. null
  243. );
  244. $this->assertEquals(Http::STATUS_UNPROCESSABLE_ENTITY, $response->getStatus());
  245. $response = $this->controller->update(
  246. 1,
  247. 'mount',
  248. '\OC\Files\Storage\InvalidStorage',
  249. '\OCA\Files_External\Lib\Auth\NullMechanism',
  250. [],
  251. [],
  252. [],
  253. [],
  254. null
  255. );
  256. $this->assertEquals(Http::STATUS_UNPROCESSABLE_ENTITY, $response->getStatus());
  257. }
  258. public function testUpdateStorageNonExisting() {
  259. $authMech = $this->getAuthMechMock();
  260. $authMech->method('validateStorage')
  261. ->willReturn(true);
  262. $authMech->method('isVisibleFor')
  263. ->willReturn(true);
  264. $backend = $this->getBackendMock();
  265. $backend->method('validateStorage')
  266. ->willReturn(true);
  267. $backend->method('isVisibleFor')
  268. ->willReturn(true);
  269. $storageConfig = new StorageConfig(255);
  270. $storageConfig->setMountPoint('mount');
  271. $storageConfig->setBackend($backend);
  272. $storageConfig->setAuthMechanism($authMech);
  273. $storageConfig->setBackendOptions([]);
  274. $this->service->expects($this->once())
  275. ->method('createStorage')
  276. ->willReturn($storageConfig);
  277. $this->service->expects($this->once())
  278. ->method('updateStorage')
  279. ->will($this->throwException(new NotFoundException()));
  280. $response = $this->controller->update(
  281. 255,
  282. 'mount',
  283. '\OCA\Files_External\Lib\Storage\SMB',
  284. '\OCA\Files_External\Lib\Auth\NullMechanism',
  285. [],
  286. [],
  287. [],
  288. [],
  289. null
  290. );
  291. $this->assertEquals(Http::STATUS_NOT_FOUND, $response->getStatus());
  292. }
  293. public function testDeleteStorage() {
  294. $this->service->expects($this->once())
  295. ->method('removeStorage');
  296. $response = $this->controller->destroy(1);
  297. $this->assertEquals(Http::STATUS_NO_CONTENT, $response->getStatus());
  298. }
  299. public function testDeleteStorageNonExisting() {
  300. $this->service->expects($this->once())
  301. ->method('removeStorage')
  302. ->will($this->throwException(new NotFoundException()));
  303. $response = $this->controller->destroy(255);
  304. $this->assertEquals(Http::STATUS_NOT_FOUND, $response->getStatus());
  305. }
  306. public function testGetStorage() {
  307. $backend = $this->getBackendMock();
  308. $authMech = $this->getAuthMechMock();
  309. $storageConfig = new StorageConfig(1);
  310. $storageConfig->setMountPoint('test');
  311. $storageConfig->setBackend($backend);
  312. $storageConfig->setAuthMechanism($authMech);
  313. $storageConfig->setBackendOptions(['user' => 'test', 'password', 'password123']);
  314. $storageConfig->setMountOptions(['priority' => false]);
  315. $this->service->expects($this->once())
  316. ->method('getStorage')
  317. ->with(1)
  318. ->willReturn($storageConfig);
  319. $response = $this->controller->show(1);
  320. $this->assertEquals(Http::STATUS_OK, $response->getStatus());
  321. $expected = $storageConfig->jsonSerialize();
  322. $expected['can_edit'] = false;
  323. $this->assertEquals($expected, $response->getData());
  324. }
  325. public function validateStorageProvider() {
  326. return [
  327. [true, true, true],
  328. [false, true, false],
  329. [true, false, false],
  330. [false, false, false]
  331. ];
  332. }
  333. /**
  334. * @dataProvider validateStorageProvider
  335. */
  336. public function testValidateStorage($backendValidate, $authMechValidate, $expectSuccess) {
  337. $backend = $this->getBackendMock();
  338. $backend->method('validateStorage')
  339. ->willReturn($backendValidate);
  340. $backend->method('isVisibleFor')
  341. ->willReturn(true);
  342. $authMech = $this->getAuthMechMock();
  343. $authMech->method('validateStorage')
  344. ->willReturn($authMechValidate);
  345. $authMech->method('isVisibleFor')
  346. ->willReturn(true);
  347. $storageConfig = new StorageConfig();
  348. $storageConfig->setMountPoint('mount');
  349. $storageConfig->setBackend($backend);
  350. $storageConfig->setAuthMechanism($authMech);
  351. $storageConfig->setBackendOptions([]);
  352. $this->service->expects($this->once())
  353. ->method('createStorage')
  354. ->willReturn($storageConfig);
  355. if ($expectSuccess) {
  356. $this->service->expects($this->once())
  357. ->method('addStorage')
  358. ->with($storageConfig)
  359. ->willReturn($storageConfig);
  360. } else {
  361. $this->service->expects($this->never())
  362. ->method('addStorage');
  363. }
  364. $response = $this->controller->create(
  365. 'mount',
  366. '\OCA\Files_External\Lib\Storage\SMB',
  367. '\OCA\Files_External\Lib\Auth\NullMechanism',
  368. [],
  369. [],
  370. [],
  371. [],
  372. null
  373. );
  374. if ($expectSuccess) {
  375. $this->assertEquals(Http::STATUS_CREATED, $response->getStatus());
  376. } else {
  377. $this->assertEquals(Http::STATUS_UNPROCESSABLE_ENTITY, $response->getStatus());
  378. }
  379. }
  380. }