StoragesControllerTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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 OCA\Files_External\Service\UserStoragesService;
  36. use OCP\AppFramework\Http;
  37. use PHPUnit\Framework\MockObject\MockObject;
  38. abstract class StoragesControllerTest extends \Test\TestCase {
  39. /**
  40. * @var GlobalStoragesController
  41. */
  42. protected $controller;
  43. /**
  44. * @var GlobalStoragesService|UserStoragesService|MockObject
  45. */
  46. protected $service;
  47. protected function setUp(): void {
  48. \OCA\Files_External\MountConfig::$skipTest = true;
  49. }
  50. protected function tearDown(): void {
  51. \OCA\Files_External\MountConfig::$skipTest = false;
  52. }
  53. /**
  54. * @return \OCA\Files_External\Lib\Backend\Backend|MockObject
  55. */
  56. protected function getBackendMock($class = '\OCA\Files_External\Lib\Backend\SMB', $storageClass = '\OCA\Files_External\Lib\Storage\SMB') {
  57. $backend = $this->getMockBuilder(Backend::class)
  58. ->disableOriginalConstructor()
  59. ->getMock();
  60. $backend->method('getStorageClass')
  61. ->willReturn($storageClass);
  62. $backend->method('getIdentifier')
  63. ->willReturn('identifier:'.$class);
  64. $backend->method('getParameters')
  65. ->willReturn([]);
  66. return $backend;
  67. }
  68. /**
  69. * @return \OCA\Files_External\Lib\Auth\AuthMechanism|MockObject
  70. */
  71. protected function getAuthMechMock($scheme = 'null', $class = '\OCA\Files_External\Lib\Auth\NullMechanism') {
  72. $authMech = $this->getMockBuilder(AuthMechanism::class)
  73. ->disableOriginalConstructor()
  74. ->getMock();
  75. $authMech->method('getScheme')
  76. ->willReturn($scheme);
  77. $authMech->method('getIdentifier')
  78. ->willReturn('identifier:'.$class);
  79. $authMech->method('getParameters')
  80. ->willReturn([]);
  81. return $authMech;
  82. }
  83. public function testAddStorage() {
  84. $authMech = $this->getAuthMechMock();
  85. $authMech->method('validateStorage')
  86. ->willReturn(true);
  87. $authMech->method('isVisibleFor')
  88. ->willReturn(true);
  89. $backend = $this->getBackendMock();
  90. $backend->method('validateStorage')
  91. ->willReturn(true);
  92. $backend->method('isVisibleFor')
  93. ->willReturn(true);
  94. $storageConfig = new StorageConfig(1);
  95. $storageConfig->setMountPoint('mount');
  96. $storageConfig->setBackend($backend);
  97. $storageConfig->setAuthMechanism($authMech);
  98. $storageConfig->setBackendOptions([]);
  99. $this->service->expects($this->once())
  100. ->method('createStorage')
  101. ->willReturn($storageConfig);
  102. $this->service->expects($this->once())
  103. ->method('addStorage')
  104. ->willReturn($storageConfig);
  105. $response = $this->controller->create(
  106. 'mount',
  107. '\OCA\Files_External\Lib\Storage\SMB',
  108. '\OCA\Files_External\Lib\Auth\NullMechanism',
  109. [],
  110. [],
  111. [],
  112. [],
  113. null
  114. );
  115. $data = $response->getData();
  116. $this->assertEquals(Http::STATUS_CREATED, $response->getStatus());
  117. $this->assertEquals($storageConfig->jsonSerialize(), $data);
  118. }
  119. public function testAddLocalStorageWhenDisabled() {
  120. $authMech = $this->getAuthMechMock();
  121. $backend = $this->getBackendMock();
  122. $storageConfig = new StorageConfig(1);
  123. $storageConfig->setMountPoint('mount');
  124. $storageConfig->setBackend($backend);
  125. $storageConfig->setAuthMechanism($authMech);
  126. $storageConfig->setBackendOptions([]);
  127. $this->service->expects($this->never())
  128. ->method('createStorage');
  129. $this->service->expects($this->never())
  130. ->method('addStorage');
  131. $response = $this->controller->create(
  132. 'mount',
  133. 'local',
  134. '\OCA\Files_External\Lib\Auth\NullMechanism',
  135. [],
  136. [],
  137. [],
  138. [],
  139. null
  140. );
  141. $data = $response->getData();
  142. $this->assertEquals(Http::STATUS_FORBIDDEN, $response->getStatus());
  143. }
  144. public function testUpdateStorage() {
  145. $authMech = $this->getAuthMechMock();
  146. $authMech->method('validateStorage')
  147. ->willReturn(true);
  148. $authMech->method('isVisibleFor')
  149. ->willReturn(true);
  150. $backend = $this->getBackendMock();
  151. $backend->method('validateStorage')
  152. ->willReturn(true);
  153. $backend->method('isVisibleFor')
  154. ->willReturn(true);
  155. $storageConfig = new StorageConfig(1);
  156. $storageConfig->setMountPoint('mount');
  157. $storageConfig->setBackend($backend);
  158. $storageConfig->setAuthMechanism($authMech);
  159. $storageConfig->setBackendOptions([]);
  160. $this->service->expects($this->once())
  161. ->method('createStorage')
  162. ->willReturn($storageConfig);
  163. $this->service->expects($this->once())
  164. ->method('updateStorage')
  165. ->willReturn($storageConfig);
  166. $response = $this->controller->update(
  167. 1,
  168. 'mount',
  169. '\OCA\Files_External\Lib\Storage\SMB',
  170. '\OCA\Files_External\Lib\Auth\NullMechanism',
  171. [],
  172. [],
  173. [],
  174. [],
  175. null
  176. );
  177. $data = $response->getData();
  178. $this->assertEquals(Http::STATUS_OK, $response->getStatus());
  179. $this->assertEquals($storageConfig->jsonSerialize(), $data);
  180. }
  181. public function mountPointNamesProvider() {
  182. return [
  183. [''],
  184. ['/'],
  185. ['//'],
  186. ];
  187. }
  188. /**
  189. * @dataProvider mountPointNamesProvider
  190. */
  191. public function testAddOrUpdateStorageInvalidMountPoint($mountPoint) {
  192. $storageConfig = new StorageConfig(1);
  193. $storageConfig->setMountPoint($mountPoint);
  194. $storageConfig->setBackend($this->getBackendMock());
  195. $storageConfig->setAuthMechanism($this->getAuthMechMock());
  196. $storageConfig->setBackendOptions([]);
  197. $this->service->expects($this->exactly(2))
  198. ->method('createStorage')
  199. ->willReturn($storageConfig);
  200. $this->service->expects($this->never())
  201. ->method('addStorage');
  202. $this->service->expects($this->never())
  203. ->method('updateStorage');
  204. $response = $this->controller->create(
  205. $mountPoint,
  206. '\OCA\Files_External\Lib\Storage\SMB',
  207. '\OCA\Files_External\Lib\Auth\NullMechanism',
  208. [],
  209. [],
  210. [],
  211. [],
  212. null
  213. );
  214. $this->assertEquals(Http::STATUS_UNPROCESSABLE_ENTITY, $response->getStatus());
  215. $response = $this->controller->update(
  216. 1,
  217. $mountPoint,
  218. '\OCA\Files_External\Lib\Storage\SMB',
  219. '\OCA\Files_External\Lib\Auth\NullMechanism',
  220. [],
  221. [],
  222. [],
  223. [],
  224. null
  225. );
  226. $this->assertEquals(Http::STATUS_UNPROCESSABLE_ENTITY, $response->getStatus());
  227. }
  228. public function testAddOrUpdateStorageInvalidBackend() {
  229. $this->service->expects($this->exactly(2))
  230. ->method('createStorage')
  231. ->will($this->throwException(new \InvalidArgumentException()));
  232. $this->service->expects($this->never())
  233. ->method('addStorage');
  234. $this->service->expects($this->never())
  235. ->method('updateStorage');
  236. $response = $this->controller->create(
  237. 'mount',
  238. '\OC\Files\Storage\InvalidStorage',
  239. '\OCA\Files_External\Lib\Auth\NullMechanism',
  240. [],
  241. [],
  242. [],
  243. [],
  244. null
  245. );
  246. $this->assertEquals(Http::STATUS_UNPROCESSABLE_ENTITY, $response->getStatus());
  247. $response = $this->controller->update(
  248. 1,
  249. 'mount',
  250. '\OC\Files\Storage\InvalidStorage',
  251. '\OCA\Files_External\Lib\Auth\NullMechanism',
  252. [],
  253. [],
  254. [],
  255. [],
  256. null
  257. );
  258. $this->assertEquals(Http::STATUS_UNPROCESSABLE_ENTITY, $response->getStatus());
  259. }
  260. public function testUpdateStorageNonExisting() {
  261. $authMech = $this->getAuthMechMock();
  262. $authMech->method('validateStorage')
  263. ->willReturn(true);
  264. $authMech->method('isVisibleFor')
  265. ->willReturn(true);
  266. $backend = $this->getBackendMock();
  267. $backend->method('validateStorage')
  268. ->willReturn(true);
  269. $backend->method('isVisibleFor')
  270. ->willReturn(true);
  271. $storageConfig = new StorageConfig(255);
  272. $storageConfig->setMountPoint('mount');
  273. $storageConfig->setBackend($backend);
  274. $storageConfig->setAuthMechanism($authMech);
  275. $storageConfig->setBackendOptions([]);
  276. $this->service->expects($this->once())
  277. ->method('createStorage')
  278. ->willReturn($storageConfig);
  279. $this->service->expects($this->once())
  280. ->method('updateStorage')
  281. ->will($this->throwException(new NotFoundException()));
  282. $response = $this->controller->update(
  283. 255,
  284. 'mount',
  285. '\OCA\Files_External\Lib\Storage\SMB',
  286. '\OCA\Files_External\Lib\Auth\NullMechanism',
  287. [],
  288. [],
  289. [],
  290. [],
  291. null
  292. );
  293. $this->assertEquals(Http::STATUS_NOT_FOUND, $response->getStatus());
  294. }
  295. public function testDeleteStorage() {
  296. $this->service->expects($this->once())
  297. ->method('removeStorage');
  298. $response = $this->controller->destroy(1);
  299. $this->assertEquals(Http::STATUS_NO_CONTENT, $response->getStatus());
  300. }
  301. public function testDeleteStorageNonExisting() {
  302. $this->service->expects($this->once())
  303. ->method('removeStorage')
  304. ->will($this->throwException(new NotFoundException()));
  305. $response = $this->controller->destroy(255);
  306. $this->assertEquals(Http::STATUS_NOT_FOUND, $response->getStatus());
  307. }
  308. public function testGetStorage() {
  309. $backend = $this->getBackendMock();
  310. $authMech = $this->getAuthMechMock();
  311. $storageConfig = new StorageConfig(1);
  312. $storageConfig->setMountPoint('test');
  313. $storageConfig->setBackend($backend);
  314. $storageConfig->setAuthMechanism($authMech);
  315. $storageConfig->setBackendOptions(['user' => 'test', 'password', 'password123']);
  316. $storageConfig->setMountOptions(['priority' => false]);
  317. $this->service->expects($this->once())
  318. ->method('getStorage')
  319. ->with(1)
  320. ->willReturn($storageConfig);
  321. $response = $this->controller->show(1);
  322. $this->assertEquals(Http::STATUS_OK, $response->getStatus());
  323. $expected = $storageConfig->jsonSerialize();
  324. $expected['can_edit'] = false;
  325. $this->assertEquals($expected, $response->getData());
  326. }
  327. public function validateStorageProvider() {
  328. return [
  329. [true, true, true],
  330. [false, true, false],
  331. [true, false, false],
  332. [false, false, false]
  333. ];
  334. }
  335. /**
  336. * @dataProvider validateStorageProvider
  337. */
  338. public function testValidateStorage($backendValidate, $authMechValidate, $expectSuccess) {
  339. $backend = $this->getBackendMock();
  340. $backend->method('validateStorage')
  341. ->willReturn($backendValidate);
  342. $backend->method('isVisibleFor')
  343. ->willReturn(true);
  344. $authMech = $this->getAuthMechMock();
  345. $authMech->method('validateStorage')
  346. ->willReturn($authMechValidate);
  347. $authMech->method('isVisibleFor')
  348. ->willReturn(true);
  349. $storageConfig = new StorageConfig();
  350. $storageConfig->setMountPoint('mount');
  351. $storageConfig->setBackend($backend);
  352. $storageConfig->setAuthMechanism($authMech);
  353. $storageConfig->setBackendOptions([]);
  354. $this->service->expects($this->once())
  355. ->method('createStorage')
  356. ->willReturn($storageConfig);
  357. if ($expectSuccess) {
  358. $this->service->expects($this->once())
  359. ->method('addStorage')
  360. ->with($storageConfig)
  361. ->willReturn($storageConfig);
  362. } else {
  363. $this->service->expects($this->never())
  364. ->method('addStorage');
  365. }
  366. $response = $this->controller->create(
  367. 'mount',
  368. '\OCA\Files_External\Lib\Storage\SMB',
  369. '\OCA\Files_External\Lib\Auth\NullMechanism',
  370. [],
  371. [],
  372. [],
  373. [],
  374. null
  375. );
  376. if ($expectSuccess) {
  377. $this->assertEquals(Http::STATUS_CREATED, $response->getStatus());
  378. } else {
  379. $this->assertEquals(Http::STATUS_UNPROCESSABLE_ENTITY, $response->getStatus());
  380. }
  381. }
  382. }