StoragesControllerTest.php 11 KB

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