StoragesControllerTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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 <pvince81@owncloud.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. \OC_Mount_Config::$skipTest = true;
  47. }
  48. protected function tearDown(): void {
  49. \OC_Mount_Config::$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 testUpdateStorage() {
  118. $authMech = $this->getAuthMechMock();
  119. $authMech->method('validateStorage')
  120. ->willReturn(true);
  121. $authMech->method('isVisibleFor')
  122. ->willReturn(true);
  123. $backend = $this->getBackendMock();
  124. $backend->method('validateStorage')
  125. ->willReturn(true);
  126. $backend->method('isVisibleFor')
  127. ->willReturn(true);
  128. $storageConfig = new StorageConfig(1);
  129. $storageConfig->setMountPoint('mount');
  130. $storageConfig->setBackend($backend);
  131. $storageConfig->setAuthMechanism($authMech);
  132. $storageConfig->setBackendOptions([]);
  133. $this->service->expects($this->once())
  134. ->method('createStorage')
  135. ->willReturn($storageConfig);
  136. $this->service->expects($this->once())
  137. ->method('updateStorage')
  138. ->willReturn($storageConfig);
  139. $response = $this->controller->update(
  140. 1,
  141. 'mount',
  142. '\OCA\Files_External\Lib\Storage\SMB',
  143. '\OCA\Files_External\Lib\Auth\NullMechanism',
  144. [],
  145. [],
  146. [],
  147. [],
  148. null
  149. );
  150. $data = $response->getData();
  151. $this->assertEquals(Http::STATUS_OK, $response->getStatus());
  152. $this->assertEquals($storageConfig, $data);
  153. }
  154. public function mountPointNamesProvider() {
  155. return [
  156. [''],
  157. ['/'],
  158. ['//'],
  159. ];
  160. }
  161. /**
  162. * @dataProvider mountPointNamesProvider
  163. */
  164. public function testAddOrUpdateStorageInvalidMountPoint($mountPoint) {
  165. $storageConfig = new StorageConfig(1);
  166. $storageConfig->setMountPoint($mountPoint);
  167. $storageConfig->setBackend($this->getBackendMock());
  168. $storageConfig->setAuthMechanism($this->getAuthMechMock());
  169. $storageConfig->setBackendOptions([]);
  170. $this->service->expects($this->exactly(2))
  171. ->method('createStorage')
  172. ->willReturn($storageConfig);
  173. $this->service->expects($this->never())
  174. ->method('addStorage');
  175. $this->service->expects($this->never())
  176. ->method('updateStorage');
  177. $response = $this->controller->create(
  178. $mountPoint,
  179. '\OCA\Files_External\Lib\Storage\SMB',
  180. '\OCA\Files_External\Lib\Auth\NullMechanism',
  181. [],
  182. [],
  183. [],
  184. [],
  185. null
  186. );
  187. $this->assertEquals(Http::STATUS_UNPROCESSABLE_ENTITY, $response->getStatus());
  188. $response = $this->controller->update(
  189. 1,
  190. $mountPoint,
  191. '\OCA\Files_External\Lib\Storage\SMB',
  192. '\OCA\Files_External\Lib\Auth\NullMechanism',
  193. [],
  194. [],
  195. [],
  196. [],
  197. null
  198. );
  199. $this->assertEquals(Http::STATUS_UNPROCESSABLE_ENTITY, $response->getStatus());
  200. }
  201. public function testAddOrUpdateStorageInvalidBackend() {
  202. $this->service->expects($this->exactly(2))
  203. ->method('createStorage')
  204. ->will($this->throwException(new \InvalidArgumentException()));
  205. $this->service->expects($this->never())
  206. ->method('addStorage');
  207. $this->service->expects($this->never())
  208. ->method('updateStorage');
  209. $response = $this->controller->create(
  210. 'mount',
  211. '\OC\Files\Storage\InvalidStorage',
  212. '\OCA\Files_External\Lib\Auth\NullMechanism',
  213. [],
  214. [],
  215. [],
  216. [],
  217. null
  218. );
  219. $this->assertEquals(Http::STATUS_UNPROCESSABLE_ENTITY, $response->getStatus());
  220. $response = $this->controller->update(
  221. 1,
  222. 'mount',
  223. '\OC\Files\Storage\InvalidStorage',
  224. '\OCA\Files_External\Lib\Auth\NullMechanism',
  225. [],
  226. [],
  227. [],
  228. [],
  229. null
  230. );
  231. $this->assertEquals(Http::STATUS_UNPROCESSABLE_ENTITY, $response->getStatus());
  232. }
  233. public function testUpdateStorageNonExisting() {
  234. $authMech = $this->getAuthMechMock();
  235. $authMech->method('validateStorage')
  236. ->willReturn(true);
  237. $authMech->method('isVisibleFor')
  238. ->willReturn(true);
  239. $backend = $this->getBackendMock();
  240. $backend->method('validateStorage')
  241. ->willReturn(true);
  242. $backend->method('isVisibleFor')
  243. ->willReturn(true);
  244. $storageConfig = new StorageConfig(255);
  245. $storageConfig->setMountPoint('mount');
  246. $storageConfig->setBackend($backend);
  247. $storageConfig->setAuthMechanism($authMech);
  248. $storageConfig->setBackendOptions([]);
  249. $this->service->expects($this->once())
  250. ->method('createStorage')
  251. ->willReturn($storageConfig);
  252. $this->service->expects($this->once())
  253. ->method('updateStorage')
  254. ->will($this->throwException(new NotFoundException()));
  255. $response = $this->controller->update(
  256. 255,
  257. 'mount',
  258. '\OCA\Files_External\Lib\Storage\SMB',
  259. '\OCA\Files_External\Lib\Auth\NullMechanism',
  260. [],
  261. [],
  262. [],
  263. [],
  264. null
  265. );
  266. $this->assertEquals(Http::STATUS_NOT_FOUND, $response->getStatus());
  267. }
  268. public function testDeleteStorage() {
  269. $this->service->expects($this->once())
  270. ->method('removeStorage');
  271. $response = $this->controller->destroy(1);
  272. $this->assertEquals(Http::STATUS_NO_CONTENT, $response->getStatus());
  273. }
  274. public function testDeleteStorageNonExisting() {
  275. $this->service->expects($this->once())
  276. ->method('removeStorage')
  277. ->will($this->throwException(new NotFoundException()));
  278. $response = $this->controller->destroy(255);
  279. $this->assertEquals(Http::STATUS_NOT_FOUND, $response->getStatus());
  280. }
  281. public function testGetStorage() {
  282. $backend = $this->getBackendMock();
  283. $authMech = $this->getAuthMechMock();
  284. $storageConfig = new StorageConfig(1);
  285. $storageConfig->setMountPoint('test');
  286. $storageConfig->setBackend($backend);
  287. $storageConfig->setAuthMechanism($authMech);
  288. $storageConfig->setBackendOptions(['user' => 'test', 'password', 'password123']);
  289. $storageConfig->setMountOptions(['priority' => false]);
  290. $this->service->expects($this->once())
  291. ->method('getStorage')
  292. ->with(1)
  293. ->willReturn($storageConfig);
  294. $response = $this->controller->show(1);
  295. $this->assertEquals(Http::STATUS_OK, $response->getStatus());
  296. $this->assertEquals($storageConfig, $response->getData());
  297. }
  298. public function validateStorageProvider() {
  299. return [
  300. [true, true, true],
  301. [false, true, false],
  302. [true, false, false],
  303. [false, false, false]
  304. ];
  305. }
  306. /**
  307. * @dataProvider validateStorageProvider
  308. */
  309. public function testValidateStorage($backendValidate, $authMechValidate, $expectSuccess) {
  310. $backend = $this->getBackendMock();
  311. $backend->method('validateStorage')
  312. ->willReturn($backendValidate);
  313. $backend->method('isVisibleFor')
  314. ->willReturn(true);
  315. $authMech = $this->getAuthMechMock();
  316. $authMech->method('validateStorage')
  317. ->willReturn($authMechValidate);
  318. $authMech->method('isVisibleFor')
  319. ->willReturn(true);
  320. $storageConfig = new StorageConfig();
  321. $storageConfig->setMountPoint('mount');
  322. $storageConfig->setBackend($backend);
  323. $storageConfig->setAuthMechanism($authMech);
  324. $storageConfig->setBackendOptions([]);
  325. $this->service->expects($this->once())
  326. ->method('createStorage')
  327. ->willReturn($storageConfig);
  328. if ($expectSuccess) {
  329. $this->service->expects($this->once())
  330. ->method('addStorage')
  331. ->with($storageConfig)
  332. ->willReturn($storageConfig);
  333. } else {
  334. $this->service->expects($this->never())
  335. ->method('addStorage');
  336. }
  337. $response = $this->controller->create(
  338. 'mount',
  339. '\OCA\Files_External\Lib\Storage\SMB',
  340. '\OCA\Files_External\Lib\Auth\NullMechanism',
  341. [],
  342. [],
  343. [],
  344. [],
  345. null
  346. );
  347. if ($expectSuccess) {
  348. $this->assertEquals(Http::STATUS_CREATED, $response->getStatus());
  349. } else {
  350. $this->assertEquals(Http::STATUS_UNPROCESSABLE_ENTITY, $response->getStatus());
  351. }
  352. }
  353. }