DBConfigServiceTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Robin Appelman <robin@icewind.nl>
  6. * @author Robin McCorkell <robin@mccorkell.me.uk>
  7. *
  8. * @license AGPL-3.0
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. namespace OCA\Files_External\Tests\Service;
  24. use OCA\Files_External\Service\DBConfigService;
  25. use OCP\IDBConnection;
  26. use Test\TestCase;
  27. /**
  28. * @group DB
  29. */
  30. class DBConfigServiceTest extends TestCase {
  31. /**
  32. * @var DBConfigService
  33. */
  34. private $dbConfig;
  35. /**
  36. * @var IDBConnection
  37. */
  38. private $connection;
  39. private $mounts = [];
  40. public function setUp() {
  41. parent::setUp();
  42. $this->connection = \OC::$server->getDatabaseConnection();
  43. $this->dbConfig = new DBConfigService($this->connection, \OC::$server->getCrypto());
  44. }
  45. public function tearDown() {
  46. foreach ($this->mounts as $mount) {
  47. $this->dbConfig->removeMount($mount);
  48. }
  49. $this->mounts = [];
  50. }
  51. private function addMount($mountPoint, $storageBackend, $authBackend, $priority, $type) {
  52. $id = $this->dbConfig->addMount($mountPoint, $storageBackend, $authBackend, $priority, $type);
  53. $this->mounts[] = $id;
  54. return $id;
  55. }
  56. public function testAddSimpleMount() {
  57. $id = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN);
  58. $mount = $this->dbConfig->getMountById($id);
  59. $this->assertEquals('/test', $mount['mount_point']);
  60. $this->assertEquals('foo', $mount['storage_backend']);
  61. $this->assertEquals('bar', $mount['auth_backend']);
  62. $this->assertEquals(100, $mount['priority']);
  63. $this->assertEquals(DBConfigService::MOUNT_TYPE_ADMIN, $mount['type']);
  64. $this->assertEquals([], $mount['applicable']);
  65. $this->assertEquals([], $mount['config']);
  66. $this->assertEquals([], $mount['options']);
  67. }
  68. public function testAddApplicable() {
  69. $id = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN);
  70. $this->dbConfig->addApplicable($id, DBConfigService::APPLICABLE_TYPE_USER, 'test');
  71. $mount = $this->dbConfig->getMountById($id);
  72. $this->assertEquals([
  73. ['type' => DBConfigService::APPLICABLE_TYPE_USER, 'value' => 'test', 'mount_id' => $id]
  74. ], $mount['applicable']);
  75. $this->dbConfig->addApplicable($id, DBConfigService::APPLICABLE_TYPE_GROUP, 'bar');
  76. $this->dbConfig->addApplicable($id, DBConfigService::APPLICABLE_TYPE_GLOBAL, null);
  77. $mount = $this->dbConfig->getMountById($id);
  78. $this->assertEquals([
  79. ['type' => DBConfigService::APPLICABLE_TYPE_USER, 'value' => 'test', 'mount_id' => $id],
  80. ['type' => DBConfigService::APPLICABLE_TYPE_GROUP, 'value' => 'bar', 'mount_id' => $id],
  81. ['type' => DBConfigService::APPLICABLE_TYPE_GLOBAL, 'value' => null, 'mount_id' => $id]
  82. ], $mount['applicable']);
  83. }
  84. public function testAddApplicableDouble() {
  85. $id = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN);
  86. $this->dbConfig->addApplicable($id, DBConfigService::APPLICABLE_TYPE_USER, 'test');
  87. $this->dbConfig->addApplicable($id, DBConfigService::APPLICABLE_TYPE_USER, 'test');
  88. $mount = $this->dbConfig->getMountById($id);
  89. $this->assertEquals([
  90. ['type' => DBConfigService::APPLICABLE_TYPE_USER, 'value' => 'test', 'mount_id' => $id]
  91. ], $mount['applicable']);
  92. }
  93. public function testDeleteMount() {
  94. $id = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN);
  95. $this->dbConfig->removeMount($id);
  96. $mount = $this->dbConfig->getMountById($id);
  97. $this->assertEquals(null, $mount);
  98. }
  99. public function testRemoveApplicable() {
  100. $id = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN);
  101. $this->dbConfig->addApplicable($id, DBConfigService::APPLICABLE_TYPE_USER, 'test');
  102. $this->dbConfig->removeApplicable($id, DBConfigService::APPLICABLE_TYPE_USER, 'test');
  103. $mount = $this->dbConfig->getMountById($id);
  104. $this->assertEquals([], $mount['applicable']);
  105. }
  106. public function testRemoveApplicableGlobal() {
  107. $id = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN);
  108. $this->dbConfig->addApplicable($id, DBConfigService::APPLICABLE_TYPE_GLOBAL, null);
  109. $this->dbConfig->removeApplicable($id, DBConfigService::APPLICABLE_TYPE_GLOBAL, null);
  110. $this->dbConfig->addApplicable($id, DBConfigService::APPLICABLE_TYPE_USER, 'test');
  111. $mount = $this->dbConfig->getMountById($id);
  112. $this->assertEquals([
  113. ['type' => DBConfigService::APPLICABLE_TYPE_USER, 'value' => 'test', 'mount_id' => $id]
  114. ], $mount['applicable']);
  115. }
  116. public function testSetConfig() {
  117. $id = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN);
  118. $this->dbConfig->setConfig($id, 'foo', 'bar');
  119. $mount = $this->dbConfig->getMountById($id);
  120. $this->assertEquals(['foo' => 'bar'], $mount['config']);
  121. $this->dbConfig->setConfig($id, 'foo2', 'bar2');
  122. $mount = $this->dbConfig->getMountById($id);
  123. $this->assertEquals(['foo' => 'bar', 'foo2' => 'bar2'], $mount['config']);
  124. }
  125. public function testSetConfigOverwrite() {
  126. $id = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN);
  127. $this->dbConfig->setConfig($id, 'foo', 'bar');
  128. $this->dbConfig->setConfig($id, 'asd', '1');
  129. $this->dbConfig->setConfig($id, 'foo', 'qwerty');
  130. $mount = $this->dbConfig->getMountById($id);
  131. $this->assertEquals(['foo' => 'qwerty', 'asd' => '1'], $mount['config']);
  132. }
  133. public function testSetOption() {
  134. $id = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN);
  135. $this->dbConfig->setOption($id, 'foo', 'bar');
  136. $mount = $this->dbConfig->getMountById($id);
  137. $this->assertEquals(['foo' => 'bar'], $mount['options']);
  138. $this->dbConfig->setOption($id, 'foo2', 'bar2');
  139. $mount = $this->dbConfig->getMountById($id);
  140. $this->assertEquals(['foo' => 'bar', 'foo2' => 'bar2'], $mount['options']);
  141. }
  142. public function testSetOptionOverwrite() {
  143. $id = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN);
  144. $this->dbConfig->setOption($id, 'foo', 'bar');
  145. $this->dbConfig->setOption($id, 'asd', '1');
  146. $this->dbConfig->setOption($id, 'foo', 'qwerty');
  147. $mount = $this->dbConfig->getMountById($id);
  148. $this->assertEquals(['foo' => 'qwerty', 'asd' => '1'], $mount['options']);
  149. }
  150. public function testGetMountsFor() {
  151. $mounts = $this->dbConfig->getMountsFor(DBConfigService::APPLICABLE_TYPE_USER, 'test');
  152. $this->assertEquals([], $mounts);
  153. $id = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN);
  154. $this->dbConfig->addApplicable($id, DBConfigService::APPLICABLE_TYPE_USER, 'test');
  155. $mounts = $this->dbConfig->getMountsFor(DBConfigService::APPLICABLE_TYPE_USER, 'test');
  156. $this->assertCount(1, $mounts);
  157. $this->assertEquals($id, $mounts[0]['mount_id']);
  158. $this->assertEquals([['type' => DBConfigService::APPLICABLE_TYPE_USER, 'value' => 'test', 'mount_id' => $id]], $mounts[0]['applicable']);
  159. }
  160. public function testGetAdminMounts() {
  161. $id1 = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN);
  162. $this->addMount('/test2', 'foo2', 'bar2', 100, DBConfigService::MOUNT_TYPE_PERSONAl);
  163. $mounts = $this->dbConfig->getAdminMounts();
  164. $this->assertCount(1, $mounts);
  165. $this->assertEquals($id1, $mounts[0]['mount_id']);
  166. }
  167. public function testGetAdminMountsFor() {
  168. $id1 = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN);
  169. $this->addMount('/test2', 'foo2', 'bar2', 100, DBConfigService::MOUNT_TYPE_ADMIN);
  170. $id3 = $this->addMount('/test3', 'foo3', 'bar3', 100, DBConfigService::MOUNT_TYPE_PERSONAl);
  171. $this->dbConfig->addApplicable($id1, DBConfigService::APPLICABLE_TYPE_USER, 'test');
  172. $this->dbConfig->addApplicable($id3, DBConfigService::APPLICABLE_TYPE_USER, 'test');
  173. $mounts = $this->dbConfig->getAdminMountsFor(DBConfigService::APPLICABLE_TYPE_USER, 'test');
  174. $this->assertCount(1, $mounts);
  175. $this->assertEquals($id1, $mounts[0]['mount_id']);
  176. $this->assertEquals([['type' => DBConfigService::APPLICABLE_TYPE_USER, 'value' => 'test', 'mount_id' => $id1]], $mounts[0]['applicable']);
  177. }
  178. public function testGetUserMountsFor() {
  179. $id1 = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN);
  180. $this->addMount('/test2', 'foo2', 'bar2', 100, DBConfigService::MOUNT_TYPE_PERSONAl);
  181. $id3 = $this->addMount('/test3', 'foo3', 'bar3', 100, DBConfigService::MOUNT_TYPE_PERSONAl);
  182. $this->dbConfig->addApplicable($id1, DBConfigService::APPLICABLE_TYPE_USER, 'test');
  183. $this->dbConfig->addApplicable($id3, DBConfigService::APPLICABLE_TYPE_USER, 'test');
  184. $mounts = $this->dbConfig->getUserMountsFor(DBConfigService::APPLICABLE_TYPE_USER, 'test');
  185. $this->assertCount(1, $mounts);
  186. $this->assertEquals($id3, $mounts[0]['mount_id']);
  187. $this->assertEquals([['type' => DBConfigService::APPLICABLE_TYPE_USER, 'value' => 'test', 'mount_id' => $id3]], $mounts[0]['applicable']);
  188. }
  189. public function testGetAdminMountsForGlobal() {
  190. $id1 = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN);
  191. $this->dbConfig->addApplicable($id1, DBConfigService::APPLICABLE_TYPE_GLOBAL, null);
  192. $mounts = $this->dbConfig->getAdminMountsFor(DBConfigService::APPLICABLE_TYPE_GLOBAL, null);
  193. $this->assertCount(1, $mounts);
  194. $this->assertEquals($id1, $mounts[0]['mount_id']);
  195. $this->assertEquals([['type' => DBConfigService::APPLICABLE_TYPE_GLOBAL, 'value' => null, 'mount_id' => $id1]], $mounts[0]['applicable']);
  196. }
  197. public function testSetMountPoint() {
  198. $id1 = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN);
  199. $id2 = $this->addMount('/foo', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN);
  200. $this->dbConfig->setMountPoint($id1, '/asd');
  201. $mount = $this->dbConfig->getMountById($id1);
  202. $this->assertEquals('/asd', $mount['mount_point']);
  203. // remains unchanged
  204. $mount = $this->dbConfig->getMountById($id2);
  205. $this->assertEquals('/foo', $mount['mount_point']);
  206. }
  207. public function testSetAuthBackend() {
  208. $id1 = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN);
  209. $id2 = $this->addMount('/foo', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN);
  210. $this->dbConfig->setAuthBackend($id1, 'none');
  211. $mount = $this->dbConfig->getMountById($id1);
  212. $this->assertEquals('none', $mount['auth_backend']);
  213. // remains unchanged
  214. $mount = $this->dbConfig->getMountById($id2);
  215. $this->assertEquals('bar', $mount['auth_backend']);
  216. }
  217. public function testGetMountsForDuplicateByGroup() {
  218. $id1 = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN);
  219. $this->dbConfig->addApplicable($id1, DBConfigService::APPLICABLE_TYPE_GROUP, 'group1');
  220. $this->dbConfig->addApplicable($id1, DBConfigService::APPLICABLE_TYPE_GROUP, 'group2');
  221. $mounts = $this->dbConfig->getAdminMountsForMultiple(DBConfigService::APPLICABLE_TYPE_GROUP, ['group1', 'group2']);
  222. $this->assertCount(1, $mounts);
  223. $this->assertEquals($id1, $mounts[0]['mount_id']);
  224. }
  225. public function testGetAllMounts() {
  226. $id1 = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN);
  227. $id2 = $this->addMount('/test2', 'foo2', 'bar2', 100, DBConfigService::MOUNT_TYPE_PERSONAl);
  228. $mounts = $this->dbConfig->getAllMounts();
  229. $this->assertCount(2, $mounts);
  230. $this->assertEquals($id1, $mounts[0]['mount_id']);
  231. $this->assertEquals($id2, $mounts[1]['mount_id']);
  232. }
  233. }