You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DbHandlerTest.php 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Björn Schießle <bjoern@schiessle.org>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. * @author Thomas Müller <thomas.mueller@tmit.eu>
  11. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OCA\Federation\Tests;
  28. use OCA\Federation\DbHandler;
  29. use OCA\Federation\TrustedServers;
  30. use OCP\IDBConnection;
  31. use OCP\IL10N;
  32. use Test\TestCase;
  33. /**
  34. * @group DB
  35. */
  36. class DbHandlerTest extends TestCase {
  37. /** @var DbHandler */
  38. private $dbHandler;
  39. /** @var IL10N | \PHPUnit\Framework\MockObject\MockObject */
  40. private $il10n;
  41. /** @var IDBConnection */
  42. private $connection;
  43. /** @var string */
  44. private $dbTable = 'trusted_servers';
  45. protected function setUp(): void {
  46. parent::setUp();
  47. $this->connection = \OC::$server->getDatabaseConnection();
  48. $this->il10n = $this->getMockBuilder(IL10N::class)->getMock();
  49. $this->dbHandler = new DbHandler(
  50. $this->connection,
  51. $this->il10n
  52. );
  53. $query = $this->connection->getQueryBuilder()->select('*')->from($this->dbTable);
  54. $result = $query->execute()->fetchAll();
  55. $this->assertEmpty($result, 'we need to start with a empty trusted_servers table');
  56. }
  57. protected function tearDown(): void {
  58. parent::tearDown();
  59. $query = $this->connection->getQueryBuilder()->delete($this->dbTable);
  60. $query->execute();
  61. }
  62. /**
  63. * @dataProvider dataTestAddServer
  64. *
  65. * @param string $url passed to the method
  66. * @param string $expectedUrl the url we expect to be written to the db
  67. * @param string $expectedHash the hash value we expect to be written to the db
  68. */
  69. public function testAddServer($url, $expectedUrl, $expectedHash) {
  70. $id = $this->dbHandler->addServer($url);
  71. $query = $this->connection->getQueryBuilder()->select('*')->from($this->dbTable);
  72. $result = $query->execute()->fetchAll();
  73. $this->assertSame(1, count($result));
  74. $this->assertSame($expectedUrl, $result[0]['url']);
  75. $this->assertSame($id, (int)$result[0]['id']);
  76. $this->assertSame($expectedHash, $result[0]['url_hash']);
  77. $this->assertSame(TrustedServers::STATUS_PENDING, (int)$result[0]['status']);
  78. }
  79. public function dataTestAddServer() {
  80. return [
  81. ['http://owncloud.org', 'http://owncloud.org', sha1('owncloud.org')],
  82. ['https://owncloud.org', 'https://owncloud.org', sha1('owncloud.org')],
  83. ['http://owncloud.org/', 'http://owncloud.org', sha1('owncloud.org')],
  84. ];
  85. }
  86. public function testRemove() {
  87. $id1 = $this->dbHandler->addServer('server1');
  88. $id2 = $this->dbHandler->addServer('server2');
  89. $query = $this->connection->getQueryBuilder()->select('*')->from($this->dbTable);
  90. $result = $query->execute()->fetchAll();
  91. $this->assertSame(2, count($result));
  92. $this->assertSame('server1', $result[0]['url']);
  93. $this->assertSame('server2', $result[1]['url']);
  94. $this->assertSame($id1, (int)$result[0]['id']);
  95. $this->assertSame($id2, (int)$result[1]['id']);
  96. $this->dbHandler->removeServer($id2);
  97. $query = $this->connection->getQueryBuilder()->select('*')->from($this->dbTable);
  98. $result = $query->execute()->fetchAll();
  99. $this->assertSame(1, count($result));
  100. $this->assertSame('server1', $result[0]['url']);
  101. $this->assertSame($id1, (int)$result[0]['id']);
  102. }
  103. public function testGetServerById() {
  104. $this->dbHandler->addServer('server1');
  105. $id = $this->dbHandler->addServer('server2');
  106. $result = $this->dbHandler->getServerById($id);
  107. $this->assertSame('server2', $result['url']);
  108. }
  109. public function testGetAll() {
  110. $id1 = $this->dbHandler->addServer('server1');
  111. $id2 = $this->dbHandler->addServer('server2');
  112. $result = $this->dbHandler->getAllServer();
  113. $this->assertSame(2, count($result));
  114. $this->assertSame('server1', $result[0]['url']);
  115. $this->assertSame('server2', $result[1]['url']);
  116. $this->assertSame($id1, (int)$result[0]['id']);
  117. $this->assertSame($id2, (int)$result[1]['id']);
  118. }
  119. /**
  120. * @dataProvider dataTestServerExists
  121. *
  122. * @param string $serverInTable
  123. * @param string $checkForServer
  124. * @param bool $expected
  125. */
  126. public function testServerExists($serverInTable, $checkForServer, $expected) {
  127. $this->dbHandler->addServer($serverInTable);
  128. $this->assertSame($expected,
  129. $this->dbHandler->serverExists($checkForServer)
  130. );
  131. }
  132. public function dataTestServerExists() {
  133. return [
  134. ['server1', 'server1', true],
  135. ['server1', 'http://server1', true],
  136. ['server1', 'server2', false]
  137. ];
  138. }
  139. public function XtestAddToken() {
  140. $this->dbHandler->addServer('server1');
  141. $query = $this->connection->getQueryBuilder()->select('*')->from($this->dbTable);
  142. $result = $query->execute()->fetchAll();
  143. $this->assertSame(1, count($result));
  144. $this->assertSame(null, $result[0]['token']);
  145. $this->dbHandler->addToken('http://server1', 'token');
  146. $query = $this->connection->getQueryBuilder()->select('*')->from($this->dbTable);
  147. $result = $query->execute()->fetchAll();
  148. $this->assertSame(1, count($result));
  149. $this->assertSame('token', $result[0]['token']);
  150. }
  151. public function testGetToken() {
  152. $this->dbHandler->addServer('server1');
  153. $this->dbHandler->addToken('http://server1', 'token');
  154. $this->assertSame('token',
  155. $this->dbHandler->getToken('https://server1')
  156. );
  157. }
  158. public function XtestAddSharedSecret() {
  159. $this->dbHandler->addServer('server1');
  160. $query = $this->connection->getQueryBuilder()->select('*')->from($this->dbTable);
  161. $result = $query->execute()->fetchAll();
  162. $this->assertSame(1, count($result));
  163. $this->assertSame(null, $result[0]['shared_secret']);
  164. $this->dbHandler->addSharedSecret('http://server1', 'secret');
  165. $query = $this->connection->getQueryBuilder()->select('*')->from($this->dbTable);
  166. $result = $query->execute()->fetchAll();
  167. $this->assertSame(1, count($result));
  168. $this->assertSame('secret', $result[0]['shared_secret']);
  169. }
  170. public function testGetSharedSecret() {
  171. $this->dbHandler->addServer('server1');
  172. $this->dbHandler->addSharedSecret('http://server1', 'secret');
  173. $this->assertSame('secret',
  174. $this->dbHandler->getSharedSecret('https://server1')
  175. );
  176. }
  177. public function testSetServerStatus() {
  178. $this->dbHandler->addServer('server1');
  179. $query = $this->connection->getQueryBuilder()->select('*')->from($this->dbTable);
  180. $result = $query->execute()->fetchAll();
  181. $this->assertSame(1, count($result));
  182. $this->assertSame(TrustedServers::STATUS_PENDING, (int)$result[0]['status']);
  183. $this->dbHandler->setServerStatus('http://server1', TrustedServers::STATUS_OK);
  184. $query = $this->connection->getQueryBuilder()->select('*')->from($this->dbTable);
  185. $result = $query->execute()->fetchAll();
  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. }