DBConfigServiceTest.php 12 KB

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