InstanceTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace Test\Remote;
  7. use OC\Memcache\ArrayCache;
  8. use OC\Remote\Instance;
  9. use OCP\ICache;
  10. use Test\TestCase;
  11. use Test\Traits\ClientServiceTrait;
  12. class InstanceTest extends TestCase {
  13. use ClientServiceTrait;
  14. /** @var ICache */
  15. private $cache;
  16. protected function setUp(): void {
  17. parent::setUp();
  18. $this->cache = new ArrayCache();
  19. }
  20. public function testBasicStatus(): void {
  21. $instance = new Instance('example.com', $this->cache, $this->getClientService());
  22. $this->expectGetRequest('https://example.com/status.php', '{"installed":true,"maintenance":false,"needsDbUpgrade":false,"version":"13.0.0.5","versionstring":"13.0.0 alpha","edition":"","productname":"Nextcloud"}');
  23. $this->assertEquals(true, $instance->isActive());
  24. $this->assertEquals('13.0.0.5', $instance->getVersion());
  25. $this->assertEquals('https', $instance->getProtocol());
  26. $this->assertEquals('https://example.com', $instance->getFullUrl());
  27. }
  28. public function testHttpFallback(): void {
  29. $instance = new Instance('example.com', $this->cache, $this->getClientService());
  30. $this->expectGetRequest('https://example.com/status.php', new \Exception());
  31. $this->expectGetRequest('http://example.com/status.php', '{"installed":true,"maintenance":false,"needsDbUpgrade":false,"version":"13.0.0.5","versionstring":"13.0.0 alpha","edition":"","productname":"Nextcloud"}');
  32. $this->assertEquals('http', $instance->getProtocol());
  33. $this->assertEquals('http://example.com', $instance->getFullUrl());
  34. }
  35. public function testRerequestHttps(): void {
  36. $instance = new Instance('example.com', $this->cache, $this->getClientService());
  37. $this->expectGetRequest('https://example.com/status.php', '{"installed":true,"maintenance":false,"needsDbUpgrade":false,"version":"13.0.0.5","versionstring":"13.0.0 alpha","edition":"","productname":"Nextcloud"}');
  38. $this->assertEquals('https', $instance->getProtocol());
  39. $this->assertEquals(true, $instance->isActive());
  40. $this->cache->remove('remote/example.com/status');
  41. $this->expectGetRequest('https://example.com/status.php', '{"installed":true,"maintenance":true,"needsDbUpgrade":false,"version":"13.0.0.5","versionstring":"13.0.0 alpha","edition":"","productname":"Nextcloud"}');
  42. $instance2 = new Instance('example.com', $this->cache, $this->getClientService());
  43. $this->assertEquals('https', $instance2->getProtocol());
  44. $this->assertEquals(false, $instance2->isActive());
  45. }
  46. public function testPreventDowngradeAttach(): void {
  47. $this->expectException(\Exception::class);
  48. $this->expectExceptionMessage('refusing to connect to remote instance(example.com) over http that was previously accessible over https');
  49. $instance = new Instance('example.com', $this->cache, $this->getClientService());
  50. $this->expectGetRequest('https://example.com/status.php', '{"installed":true,"maintenance":false,"needsDbUpgrade":false,"version":"13.0.0.5","versionstring":"13.0.0 alpha","edition":"","productname":"Nextcloud"}');
  51. $this->assertEquals('https', $instance->getProtocol());
  52. $this->expectGetRequest('https://example.com/status.php', new \Exception());
  53. $this->cache->remove('remote/example.com/status');
  54. $instance2 = new Instance('example.com', $this->cache, $this->getClientService());
  55. $instance2->getProtocol();
  56. }
  57. }