DbHandlerTest.php 9.1 KB

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