dbconfigservicetest.php 11 KB

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