InstanceTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 Robin Appelman <robin@icewind.nl>
  4. *
  5. * @license GNU AGPL version 3 or any later version
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as
  9. * published by the Free Software Foundation, either version 3 of the
  10. * License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. namespace Test\Remote;
  22. use OC\Memcache\ArrayCache;
  23. use OC\Remote\Instance;
  24. use OCP\ICache;
  25. use Test\TestCase;
  26. use Test\Traits\ClientServiceTrait;
  27. class InstanceTest extends TestCase {
  28. use ClientServiceTrait;
  29. /** @var ICache */
  30. private $cache;
  31. protected function setUp(): void {
  32. parent::setUp();
  33. $this->cache = new ArrayCache();
  34. }
  35. public function testBasicStatus() {
  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(true, $instance->isActive());
  39. $this->assertEquals('13.0.0.5', $instance->getVersion());
  40. $this->assertEquals('https', $instance->getProtocol());
  41. $this->assertEquals('https://example.com', $instance->getFullUrl());
  42. }
  43. public function testHttpFallback() {
  44. $instance = new Instance('example.com', $this->cache, $this->getClientService());
  45. $this->expectGetRequest('https://example.com/status.php', new \Exception());
  46. $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"}');
  47. $this->assertEquals('http', $instance->getProtocol());
  48. $this->assertEquals('http://example.com', $instance->getFullUrl());
  49. }
  50. public function testRerequestHttps() {
  51. $instance = new Instance('example.com', $this->cache, $this->getClientService());
  52. $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"}');
  53. $this->assertEquals('https', $instance->getProtocol());
  54. $this->assertEquals(true, $instance->isActive());
  55. $this->cache->remove('remote/example.com/status');
  56. $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"}');
  57. $instance2 = new Instance('example.com', $this->cache, $this->getClientService());
  58. $this->assertEquals('https', $instance2->getProtocol());
  59. $this->assertEquals(false, $instance2->isActive());
  60. }
  61. public function testPreventDowngradeAttach() {
  62. $this->expectException(\Exception::class);
  63. $this->expectExceptionMessage('refusing to connect to remote instance(example.com) over http that was previously accessible over https');
  64. $instance = new Instance('example.com', $this->cache, $this->getClientService());
  65. $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"}');
  66. $this->assertEquals('https', $instance->getProtocol());
  67. $this->expectGetRequest('https://example.com/status.php', new \Exception());
  68. $this->cache->remove('remote/example.com/status');
  69. $instance2 = new Instance('example.com', $this->cache, $this->getClientService());
  70. $instance2->getProtocol();
  71. }
  72. }