TrustedServersTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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 Morris Jobke <hey@morrisjobke.de>
  8. * @author Thomas Müller <thomas.mueller@tmit.eu>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OCA\Federation\Tests;
  26. use OCA\Federation\DbHandler;
  27. use OCA\Federation\TrustedServers;
  28. use OCP\BackgroundJob\IJobList;
  29. use OCP\Http\Client\IClient;
  30. use OCP\Http\Client\IClientService;
  31. use OCP\Http\Client\IResponse;
  32. use OCP\IConfig;
  33. use OCP\ILogger;
  34. use OCP\Security\ISecureRandom;
  35. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  36. use Test\TestCase;
  37. class TrustedServersTest extends TestCase {
  38. /** @var \PHPUnit_Framework_MockObject_MockObject | TrustedServers */
  39. private $trustedServers;
  40. /** @var \PHPUnit_Framework_MockObject_MockObject | DbHandler */
  41. private $dbHandler;
  42. /** @var \PHPUnit_Framework_MockObject_MockObject | IClientService */
  43. private $httpClientService;
  44. /** @var \PHPUnit_Framework_MockObject_MockObject | IClient */
  45. private $httpClient;
  46. /** @var \PHPUnit_Framework_MockObject_MockObject | IResponse */
  47. private $response;
  48. /** @var \PHPUnit_Framework_MockObject_MockObject | ILogger */
  49. private $logger;
  50. /** @var \PHPUnit_Framework_MockObject_MockObject | IJobList */
  51. private $jobList;
  52. /** @var \PHPUnit_Framework_MockObject_MockObject | ISecureRandom */
  53. private $secureRandom;
  54. /** @var \PHPUnit_Framework_MockObject_MockObject | IConfig */
  55. private $config;
  56. /** @var \PHPUnit_Framework_MockObject_MockObject | EventDispatcherInterface */
  57. private $dispatcher;
  58. public function setUp() {
  59. parent::setUp();
  60. $this->dbHandler = $this->getMockBuilder(DbHandler::class)
  61. ->disableOriginalConstructor()->getMock();
  62. $this->dispatcher = $this->getMockBuilder(EventDispatcherInterface::class)
  63. ->disableOriginalConstructor()->getMock();
  64. $this->httpClientService = $this->getMockBuilder(IClientService::class)->getMock();
  65. $this->httpClient = $this->getMockBuilder(IClient::class)->getMock();
  66. $this->response = $this->getMockBuilder(IResponse::class)->getMock();
  67. $this->logger = $this->getMockBuilder(ILogger::class)->getMock();
  68. $this->jobList = $this->getMockBuilder(IJobList::class)->getMock();
  69. $this->secureRandom = $this->getMockBuilder(ISecureRandom::class)->getMock();
  70. $this->config = $this->getMockBuilder(IConfig::class)->getMock();
  71. $this->trustedServers = new TrustedServers(
  72. $this->dbHandler,
  73. $this->httpClientService,
  74. $this->logger,
  75. $this->jobList,
  76. $this->secureRandom,
  77. $this->config,
  78. $this->dispatcher
  79. );
  80. }
  81. /**
  82. * @dataProvider dataTrueFalse
  83. *
  84. * @param bool $success
  85. */
  86. public function testAddServer($success) {
  87. /** @var \PHPUnit_Framework_MockObject_MockObject|TrustedServers $trustedServers */
  88. $trustedServers = $this->getMockBuilder('OCA\Federation\TrustedServers')
  89. ->setConstructorArgs(
  90. [
  91. $this->dbHandler,
  92. $this->httpClientService,
  93. $this->logger,
  94. $this->jobList,
  95. $this->secureRandom,
  96. $this->config,
  97. $this->dispatcher
  98. ]
  99. )
  100. ->setMethods(['normalizeUrl', 'updateProtocol'])
  101. ->getMock();
  102. $trustedServers->expects($this->once())->method('updateProtocol')
  103. ->with('url')->willReturn('https://url');
  104. $this->dbHandler->expects($this->once())->method('addServer')->with('https://url')
  105. ->willReturn($success);
  106. if ($success) {
  107. $this->secureRandom->expects($this->once())->method('generate')
  108. ->willReturn('token');
  109. $this->dbHandler->expects($this->once())->method('addToken')->with('https://url', 'token');
  110. $this->jobList->expects($this->once())->method('add')
  111. ->with('OCA\Federation\BackgroundJob\RequestSharedSecret',
  112. ['url' => 'https://url', 'token' => 'token']);
  113. } else {
  114. $this->jobList->expects($this->never())->method('add');
  115. }
  116. $this->assertSame($success,
  117. $trustedServers->addServer('url')
  118. );
  119. }
  120. public function dataTrueFalse() {
  121. return [
  122. [true],
  123. [false]
  124. ];
  125. }
  126. /**
  127. * @dataProvider dataTrueFalse
  128. *
  129. * @param bool $status
  130. */
  131. public function testSetAutoAddServers($status) {
  132. if ($status) {
  133. $this->config->expects($this->once())->method('setAppValue')
  134. ->with('federation', 'autoAddServers', '1');
  135. } else {
  136. $this->config->expects($this->once())->method('setAppValue')
  137. ->with('federation', 'autoAddServers', '0');
  138. }
  139. $this->trustedServers->setAutoAddServers($status);
  140. }
  141. /**
  142. * @dataProvider dataTestGetAutoAddServers
  143. *
  144. * @param string $status
  145. * @param bool $expected
  146. */
  147. public function testGetAutoAddServers($status, $expected) {
  148. $this->config->expects($this->once())->method('getAppValue')
  149. ->with('federation', 'autoAddServers', '0')->willReturn($status);
  150. $this->assertSame($expected,
  151. $this->trustedServers->getAutoAddServers()
  152. );
  153. }
  154. public function dataTestGetAutoAddServers() {
  155. return [
  156. ['1', true],
  157. ['0', false]
  158. ];
  159. }
  160. public function testAddSharedSecret() {
  161. $this->dbHandler->expects($this->once())->method('addSharedSecret')
  162. ->with('url', 'secret');
  163. $this->trustedServers->addSharedSecret('url', 'secret');
  164. }
  165. public function testGetSharedSecret() {
  166. $this->dbHandler->expects($this->once())->method('getSharedSecret')
  167. ->with('url')->willReturn(true);
  168. $this->assertTrue(
  169. $this->trustedServers->getSharedSecret('url')
  170. );
  171. }
  172. public function testRemoveServer() {
  173. $id = 42;
  174. $server = ['url_hash' => 'url_hash'];
  175. $this->dbHandler->expects($this->once())->method('removeServer')->with($id);
  176. $this->dbHandler->expects($this->once())->method('getServerById')->with($id)
  177. ->willReturn($server);
  178. $this->dispatcher->expects($this->once())->method('dispatch')
  179. ->willReturnCallback(
  180. function($eventId, $event) {
  181. $this->assertSame($eventId, 'OCP\Federation\TrustedServerEvent::remove');
  182. $this->assertInstanceOf('Symfony\Component\EventDispatcher\GenericEvent', $event);
  183. /** @var \Symfony\Component\EventDispatcher\GenericEvent $event */
  184. $this->assertSame('url_hash', $event->getSubject());
  185. }
  186. );
  187. $this->trustedServers->removeServer($id);
  188. }
  189. public function testGetServers() {
  190. $this->dbHandler->expects($this->once())->method('getAllServer')->willReturn(['servers']);
  191. $this->assertEquals(
  192. ['servers'],
  193. $this->trustedServers->getServers()
  194. );
  195. }
  196. public function testIsTrustedServer() {
  197. $this->dbHandler->expects($this->once())->method('serverExists')->with('url')
  198. ->willReturn(true);
  199. $this->assertTrue(
  200. $this->trustedServers->isTrustedServer('url')
  201. );
  202. }
  203. public function testSetServerStatus() {
  204. $this->dbHandler->expects($this->once())->method('setServerStatus')
  205. ->with('url', 'status');
  206. $this->trustedServers->setServerStatus('url', 'status');
  207. }
  208. public function testGetServerStatus() {
  209. $this->dbHandler->expects($this->once())->method('getServerStatus')
  210. ->with('url')->willReturn(true);
  211. $this->assertTrue(
  212. $this->trustedServers->getServerStatus('url')
  213. );
  214. }
  215. /**
  216. * @dataProvider dataTestIsOwnCloudServer
  217. *
  218. * @param int $statusCode
  219. * @param bool $isValidOwnCloudVersion
  220. * @param bool $expected
  221. */
  222. public function testIsOwnCloudServer($statusCode, $isValidOwnCloudVersion, $expected) {
  223. $server = 'server1';
  224. /** @var \PHPUnit_Framework_MockObject_MockObject | TrustedServers $trustedServers */
  225. $trustedServers = $this->getMockBuilder('OCA\Federation\TrustedServers')
  226. ->setConstructorArgs(
  227. [
  228. $this->dbHandler,
  229. $this->httpClientService,
  230. $this->logger,
  231. $this->jobList,
  232. $this->secureRandom,
  233. $this->config,
  234. $this->dispatcher
  235. ]
  236. )
  237. ->setMethods(['checkOwnCloudVersion'])
  238. ->getMock();
  239. $this->httpClientService->expects($this->once())->method('newClient')
  240. ->willReturn($this->httpClient);
  241. $this->httpClient->expects($this->once())->method('get')->with($server . '/status.php')
  242. ->willReturn($this->response);
  243. $this->response->expects($this->once())->method('getStatusCode')
  244. ->willReturn($statusCode);
  245. if ($statusCode === 200) {
  246. $trustedServers->expects($this->once())->method('checkOwnCloudVersion')
  247. ->willReturn($isValidOwnCloudVersion);
  248. } else {
  249. $trustedServers->expects($this->never())->method('checkOwnCloudVersion');
  250. }
  251. $this->assertSame($expected,
  252. $trustedServers->isOwnCloudServer($server)
  253. );
  254. }
  255. public function dataTestIsOwnCloudServer() {
  256. return [
  257. [200, true, true],
  258. [200, false, false],
  259. [404, true, false],
  260. ];
  261. }
  262. /**
  263. * @expectedExceptionMessage simulated exception
  264. */
  265. public function testIsOwnCloudServerFail() {
  266. $server = 'server1';
  267. $this->httpClientService->expects($this->once())->method('newClient')
  268. ->willReturn($this->httpClient);
  269. $this->httpClient->expects($this->once())->method('get')->with($server . '/status.php')
  270. ->willReturnCallback(function () {
  271. throw new \Exception('simulated exception');
  272. });
  273. $this->assertFalse($this->trustedServers->isOwnCloudServer($server));
  274. }
  275. /**
  276. * @dataProvider dataTestCheckOwnCloudVersion
  277. */
  278. public function testCheckOwnCloudVersion($status) {
  279. $this->assertTrue($this->invokePrivate($this->trustedServers, 'checkOwnCloudVersion', [$status]));
  280. }
  281. public function dataTestCheckOwnCloudVersion() {
  282. return [
  283. ['{"version":"9.0.0"}'],
  284. ['{"version":"9.1.0"}']
  285. ];
  286. }
  287. /**
  288. * @dataProvider dataTestCheckOwnCloudVersionTooLow
  289. * @expectedException \OC\HintException
  290. * @expectedExceptionMessage Remote server version is too low. 9.0 is required.
  291. */
  292. public function testCheckOwnCloudVersionTooLow($status) {
  293. $this->invokePrivate($this->trustedServers, 'checkOwnCloudVersion', [$status]);
  294. }
  295. public function dataTestCheckOwnCloudVersionTooLow() {
  296. return [
  297. ['{"version":"8.2.3"}'],
  298. ];
  299. }
  300. /**
  301. * @dataProvider dataTestUpdateProtocol
  302. * @param string $url
  303. * @param string $expected
  304. */
  305. public function testUpdateProtocol($url, $expected) {
  306. $this->assertSame($expected,
  307. $this->invokePrivate($this->trustedServers, 'updateProtocol', [$url])
  308. );
  309. }
  310. public function dataTestUpdateProtocol() {
  311. return [
  312. ['http://owncloud.org', 'http://owncloud.org'],
  313. ['https://owncloud.org', 'https://owncloud.org'],
  314. ['owncloud.org', 'https://owncloud.org'],
  315. ['httpserver', 'https://httpserver'],
  316. ];
  317. }
  318. }