You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

OccControllerTest.php 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. /**
  3. * @author Victor Dubiniuk <dubiniuk@owncloud.com>
  4. *
  5. * @copyright Copyright (c) 2015, ownCloud, Inc.
  6. * @license AGPL-3.0
  7. *
  8. * This code is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License, version 3,
  10. * as published by the Free Software Foundation.
  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, version 3,
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>
  19. *
  20. */
  21. namespace Tests\Core\Controller;
  22. use OC\Console\Application;
  23. use OC\Core\Controller\OccController;
  24. use OCP\IConfig;
  25. use Symfony\Component\Console\Output\Output;
  26. use Test\TestCase;
  27. /**
  28. * Class OccControllerTest
  29. *
  30. * @package OC\Core\Controller
  31. */
  32. class OccControllerTest extends TestCase {
  33. const TEMP_SECRET = 'test';
  34. /** @var \OC\AppFramework\Http\Request | \PHPUnit_Framework_MockObject_MockObject */
  35. private $request;
  36. /** @var \OC\Core\Controller\OccController | \PHPUnit_Framework_MockObject_MockObject */
  37. private $controller;
  38. /** @var IConfig | \PHPUnit_Framework_MockObject_MockObject */
  39. private $config;
  40. /** @var Application | \PHPUnit_Framework_MockObject_MockObject */
  41. private $console;
  42. public function testFromInvalidLocation(){
  43. $this->getControllerMock('example.org');
  44. $response = $this->controller->execute('status', '');
  45. $responseData = $response->getData();
  46. $this->assertArrayHasKey('exitCode', $responseData);
  47. $this->assertEquals(126, $responseData['exitCode']);
  48. $this->assertArrayHasKey('details', $responseData);
  49. $this->assertEquals('Web executor is not allowed to run from a different host', $responseData['details']);
  50. }
  51. public function testNotWhiteListedCommand(){
  52. $this->getControllerMock('localhost');
  53. $response = $this->controller->execute('missing_command', '');
  54. $responseData = $response->getData();
  55. $this->assertArrayHasKey('exitCode', $responseData);
  56. $this->assertEquals(126, $responseData['exitCode']);
  57. $this->assertArrayHasKey('details', $responseData);
  58. $this->assertEquals('Command "missing_command" is not allowed to run via web request', $responseData['details']);
  59. }
  60. public function testWrongToken(){
  61. $this->getControllerMock('localhost');
  62. $response = $this->controller->execute('status', self::TEMP_SECRET . '-');
  63. $responseData = $response->getData();
  64. $this->assertArrayHasKey('exitCode', $responseData);
  65. $this->assertEquals(126, $responseData['exitCode']);
  66. $this->assertArrayHasKey('details', $responseData);
  67. $this->assertEquals('updater.secret does not match the provided token', $responseData['details']);
  68. }
  69. public function testSuccess(){
  70. $this->getControllerMock('localhost');
  71. $this->console->expects($this->once())->method('run')
  72. ->willReturnCallback(
  73. function ($input, $output) {
  74. /** @var Output $output */
  75. $output->writeln('{"installed":true,"version":"9.1.0.8","versionstring":"9.1.0 beta 2","edition":""}');
  76. return 0;
  77. }
  78. );
  79. $response = $this->controller->execute('status', self::TEMP_SECRET, ['--output'=>'json']);
  80. $responseData = $response->getData();
  81. $this->assertArrayHasKey('exitCode', $responseData);
  82. $this->assertEquals(0, $responseData['exitCode']);
  83. $this->assertArrayHasKey('response', $responseData);
  84. $decoded = json_decode($responseData['response'], true);
  85. $this->assertArrayHasKey('installed', $decoded);
  86. $this->assertEquals(true, $decoded['installed']);
  87. }
  88. private function getControllerMock($host){
  89. $this->request = $this->getMockBuilder('OC\AppFramework\Http\Request')
  90. ->setConstructorArgs([
  91. ['server' => []],
  92. \OC::$server->getSecureRandom(),
  93. \OC::$server->getConfig()
  94. ])
  95. ->setMethods(['getRemoteAddress'])
  96. ->getMock();
  97. $this->request->expects($this->any())->method('getRemoteAddress')
  98. ->will($this->returnValue($host));
  99. $this->config = $this->getMockBuilder('\OCP\IConfig')
  100. ->disableOriginalConstructor()
  101. ->getMock();
  102. $this->config->expects($this->any())->method('getSystemValue')
  103. ->with('updater.secret')
  104. ->willReturn(password_hash(self::TEMP_SECRET, PASSWORD_DEFAULT));
  105. $this->console = $this->getMockBuilder('\OC\Console\Application')
  106. ->disableOriginalConstructor()
  107. ->getMock();
  108. $this->controller = new OccController(
  109. 'core',
  110. $this->request,
  111. $this->config,
  112. $this->console
  113. );
  114. }
  115. }