StoragesControllerTest.php 11 KB

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