DbHandlerTest.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Federation\Tests;
  8. use OCA\Federation\DbHandler;
  9. use OCA\Federation\TrustedServers;
  10. use OCP\IDBConnection;
  11. use OCP\IL10N;
  12. use Test\TestCase;
  13. /**
  14. * @group DB
  15. */
  16. class DbHandlerTest extends TestCase {
  17. /** @var DbHandler */
  18. private $dbHandler;
  19. /** @var IL10N | \PHPUnit\Framework\MockObject\MockObject */
  20. private $il10n;
  21. /** @var IDBConnection */
  22. private $connection;
  23. /** @var string */
  24. private $dbTable = 'trusted_servers';
  25. protected function setUp(): void {
  26. parent::setUp();
  27. $this->connection = \OC::$server->getDatabaseConnection();
  28. $this->il10n = $this->getMockBuilder(IL10N::class)->getMock();
  29. $this->dbHandler = new DbHandler(
  30. $this->connection,
  31. $this->il10n
  32. );
  33. $query = $this->connection->getQueryBuilder()->select('*')->from($this->dbTable);
  34. $qResult = $query->execute();
  35. $result = $qResult->fetchAll();
  36. $qResult->closeCursor();
  37. $this->assertEmpty($result, 'we need to start with a empty trusted_servers table');
  38. }
  39. protected function tearDown(): void {
  40. parent::tearDown();
  41. $query = $this->connection->getQueryBuilder()->delete($this->dbTable);
  42. $query->execute();
  43. }
  44. /**
  45. * @dataProvider dataTestAddServer
  46. *
  47. * @param string $url passed to the method
  48. * @param string $expectedUrl the url we expect to be written to the db
  49. * @param string $expectedHash the hash value we expect to be written to the db
  50. */
  51. public function testAddServer($url, $expectedUrl, $expectedHash) {
  52. $id = $this->dbHandler->addServer($url);
  53. $query = $this->connection->getQueryBuilder()->select('*')->from($this->dbTable);
  54. $qResult = $query->execute();
  55. $result = $qResult->fetchAll();
  56. $qResult->closeCursor();
  57. $this->assertSame(1, count($result));
  58. $this->assertSame($expectedUrl, $result[0]['url']);
  59. $this->assertSame($id, (int)$result[0]['id']);
  60. $this->assertSame($expectedHash, $result[0]['url_hash']);
  61. $this->assertSame(TrustedServers::STATUS_PENDING, (int)$result[0]['status']);
  62. }
  63. public function dataTestAddServer() {
  64. return [
  65. ['http://owncloud.org', 'http://owncloud.org', sha1('owncloud.org')],
  66. ['https://owncloud.org', 'https://owncloud.org', sha1('owncloud.org')],
  67. ['http://owncloud.org/', 'http://owncloud.org', sha1('owncloud.org')],
  68. ];
  69. }
  70. public function testRemove() {
  71. $id1 = $this->dbHandler->addServer('server1');
  72. $id2 = $this->dbHandler->addServer('server2');
  73. $query = $this->connection->getQueryBuilder()->select('*')->from($this->dbTable);
  74. $qResult = $query->execute();
  75. $result = $qResult->fetchAll();
  76. $qResult->closeCursor();
  77. $this->assertSame(2, count($result));
  78. $this->assertSame('server1', $result[0]['url']);
  79. $this->assertSame('server2', $result[1]['url']);
  80. $this->assertSame($id1, (int)$result[0]['id']);
  81. $this->assertSame($id2, (int)$result[1]['id']);
  82. $this->dbHandler->removeServer($id2);
  83. $query = $this->connection->getQueryBuilder()->select('*')->from($this->dbTable);
  84. $qResult = $query->execute();
  85. $result = $qResult->fetchAll();
  86. $qResult->closeCursor();
  87. $this->assertSame(1, count($result));
  88. $this->assertSame('server1', $result[0]['url']);
  89. $this->assertSame($id1, (int)$result[0]['id']);
  90. }
  91. public function testGetServerById() {
  92. $this->dbHandler->addServer('server1');
  93. $id = $this->dbHandler->addServer('server2');
  94. $result = $this->dbHandler->getServerById($id);
  95. $this->assertSame('server2', $result['url']);
  96. }
  97. public function testGetAll() {
  98. $id1 = $this->dbHandler->addServer('server1');
  99. $id2 = $this->dbHandler->addServer('server2');
  100. $result = $this->dbHandler->getAllServer();
  101. $this->assertSame(2, count($result));
  102. $this->assertSame('server1', $result[0]['url']);
  103. $this->assertSame('server2', $result[1]['url']);
  104. $this->assertSame($id1, (int)$result[0]['id']);
  105. $this->assertSame($id2, (int)$result[1]['id']);
  106. }
  107. /**
  108. * @dataProvider dataTestServerExists
  109. *
  110. * @param string $serverInTable
  111. * @param string $checkForServer
  112. * @param bool $expected
  113. */
  114. public function testServerExists($serverInTable, $checkForServer, $expected) {
  115. $this->dbHandler->addServer($serverInTable);
  116. $this->assertSame($expected,
  117. $this->dbHandler->serverExists($checkForServer)
  118. );
  119. }
  120. public function dataTestServerExists() {
  121. return [
  122. ['server1', 'server1', true],
  123. ['server1', 'http://server1', true],
  124. ['server1', 'server2', false]
  125. ];
  126. }
  127. public function XtestAddToken() {
  128. $this->dbHandler->addServer('server1');
  129. $query = $this->connection->getQueryBuilder()->select('*')->from($this->dbTable);
  130. $qResult = $query->execute();
  131. $result = $qResult->fetchAll();
  132. $qResult->closeCursor();
  133. $this->assertSame(1, count($result));
  134. $this->assertSame(null, $result[0]['token']);
  135. $this->dbHandler->addToken('http://server1', 'token');
  136. $query = $this->connection->getQueryBuilder()->select('*')->from($this->dbTable);
  137. $qResult = $query->execute();
  138. $result = $qResult->fetchAll();
  139. $qResult->closeCursor();
  140. $this->assertSame(1, count($result));
  141. $this->assertSame('token', $result[0]['token']);
  142. }
  143. public function testGetToken() {
  144. $this->dbHandler->addServer('server1');
  145. $this->dbHandler->addToken('http://server1', 'token');
  146. $this->assertSame('token',
  147. $this->dbHandler->getToken('https://server1')
  148. );
  149. }
  150. public function XtestAddSharedSecret() {
  151. $this->dbHandler->addServer('server1');
  152. $query = $this->connection->getQueryBuilder()->select('*')->from($this->dbTable);
  153. $qResult = $query->execute();
  154. $result = $qResult->fetchAll();
  155. $qResult->closeCursor();
  156. $this->assertSame(1, count($result));
  157. $this->assertSame(null, $result[0]['shared_secret']);
  158. $this->dbHandler->addSharedSecret('http://server1', 'secret');
  159. $query = $this->connection->getQueryBuilder()->select('*')->from($this->dbTable);
  160. $qResult = $query->execute();
  161. $result = $qResult->fetchAll();
  162. $qResult->closeCursor();
  163. $this->assertSame(1, count($result));
  164. $this->assertSame('secret', $result[0]['shared_secret']);
  165. }
  166. public function testGetSharedSecret() {
  167. $this->dbHandler->addServer('server1');
  168. $this->dbHandler->addSharedSecret('http://server1', 'secret');
  169. $this->assertSame('secret',
  170. $this->dbHandler->getSharedSecret('https://server1')
  171. );
  172. }
  173. public function testSetServerStatus() {
  174. $this->dbHandler->addServer('server1');
  175. $query = $this->connection->getQueryBuilder()->select('*')->from($this->dbTable);
  176. $qResult = $query->execute();
  177. $result = $qResult->fetchAll();
  178. $qResult->closeCursor();
  179. $this->assertSame(1, count($result));
  180. $this->assertSame(TrustedServers::STATUS_PENDING, (int)$result[0]['status']);
  181. $this->dbHandler->setServerStatus('http://server1', TrustedServers::STATUS_OK);
  182. $query = $this->connection->getQueryBuilder()->select('*')->from($this->dbTable);
  183. $qResult = $query->execute();
  184. $result = $qResult->fetchAll();
  185. $qResult->closeCursor();
  186. $this->assertSame(1, count($result));
  187. $this->assertSame(TrustedServers::STATUS_OK, (int)$result[0]['status']);
  188. }
  189. public function testGetServerStatus() {
  190. $this->dbHandler->addServer('server1');
  191. $this->dbHandler->setServerStatus('http://server1', TrustedServers::STATUS_OK);
  192. $this->assertSame(TrustedServers::STATUS_OK,
  193. $this->dbHandler->getServerStatus('https://server1')
  194. );
  195. // test sync token
  196. $this->dbHandler->setServerStatus('http://server1', TrustedServers::STATUS_OK, 'token1234567890');
  197. $servers = $this->dbHandler->getAllServer();
  198. $this->assertSame('token1234567890', $servers[0]['sync_token']);
  199. }
  200. /**
  201. * hash should always be computed with the normalized URL
  202. *
  203. * @dataProvider dataTestHash
  204. *
  205. * @param string $url
  206. * @param string $expected
  207. */
  208. public function testHash($url, $expected) {
  209. $this->assertSame($expected,
  210. $this->invokePrivate($this->dbHandler, 'hash', [$url])
  211. );
  212. }
  213. public function dataTestHash() {
  214. return [
  215. ['server1', sha1('server1')],
  216. ['http://server1', sha1('server1')],
  217. ['https://server1', sha1('server1')],
  218. ['http://server1/', sha1('server1')],
  219. ];
  220. }
  221. /**
  222. * @dataProvider dataTestNormalizeUrl
  223. *
  224. * @param string $url
  225. * @param string $expected
  226. */
  227. public function testNormalizeUrl($url, $expected) {
  228. $this->assertSame($expected,
  229. $this->invokePrivate($this->dbHandler, 'normalizeUrl', [$url])
  230. );
  231. }
  232. public function dataTestNormalizeUrl() {
  233. return [
  234. ['owncloud.org', 'owncloud.org'],
  235. ['http://owncloud.org', 'owncloud.org'],
  236. ['https://owncloud.org', 'owncloud.org'],
  237. ['https://owncloud.org//mycloud', 'owncloud.org/mycloud'],
  238. ['https://owncloud.org/mycloud/', 'owncloud.org/mycloud'],
  239. ];
  240. }
  241. /**
  242. * @dataProvider providesAuth
  243. */
  244. public function testAuth($expectedResult, $user, $password) {
  245. if ($expectedResult) {
  246. $this->dbHandler->addServer('url1');
  247. $this->dbHandler->addSharedSecret('url1', $password);
  248. }
  249. $result = $this->dbHandler->auth($user, $password);
  250. $this->assertEquals($expectedResult, $result);
  251. }
  252. public function providesAuth() {
  253. return [
  254. [false, 'foo', ''],
  255. [true, 'system', '123456789'],
  256. ];
  257. }
  258. }