1
0

InstanceTest.php 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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() {
  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. /**
  62. * @expectedException \Exception
  63. * @expectedExceptionMessage refusing to connect to remote instance(example.com) over http that was previously accessible over https
  64. */
  65. public function testPreventDowngradeAttach() {
  66. $instance = new Instance('example.com', $this->cache, $this->getClientService());
  67. $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"}');
  68. $this->assertEquals('https', $instance->getProtocol());
  69. $this->expectGetRequest('https://example.com/status.php', new \Exception());
  70. $this->cache->remove('remote/example.com/status');
  71. $instance2 = new Instance('example.com', $this->cache, $this->getClientService());
  72. $instance2->getProtocol();
  73. }
  74. }