DBConfigServiceTest.php 12 KB

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