diff options
author | Joas Schilling <nickvergessen@owncloud.com> | 2016-05-19 10:43:49 +0200 |
---|---|---|
committer | Joas Schilling <nickvergessen@owncloud.com> | 2016-05-19 11:18:25 +0200 |
commit | 392bc0c6b9967d3e33570ada51a391159d4d69aa (patch) | |
tree | 26bf91653e6aa726a12c0748d2b225ff31b77668 /tests/Core | |
parent | 7ca5b35379144d868a36f10c237c78de56f4ed5a (diff) | |
download | nextcloud-server-392bc0c6b9967d3e33570ada51a391159d4d69aa.tar.gz nextcloud-server-392bc0c6b9967d3e33570ada51a391159d4d69aa.zip |
Move tests/core/ to PSR-4
Diffstat (limited to 'tests/Core')
26 files changed, 4730 insertions, 0 deletions
diff --git a/tests/Core/Command/Config/App/DeleteConfigTest.php b/tests/Core/Command/Config/App/DeleteConfigTest.php new file mode 100644 index 00000000000..7056e1b1ff9 --- /dev/null +++ b/tests/Core/Command/Config/App/DeleteConfigTest.php @@ -0,0 +1,123 @@ +<?php +/** + * @author Joas Schilling <nickvergessen@owncloud.com> + * + * @copyright Copyright (c) 2015, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ + +namespace Tests\Core\Command\Config\App; + + +use OC\Core\Command\Config\App\DeleteConfig; +use Test\TestCase; + +class DeleteConfigTest extends TestCase { + /** @var \PHPUnit_Framework_MockObject_MockObject */ + protected $config; + + /** @var \PHPUnit_Framework_MockObject_MockObject */ + protected $consoleInput; + /** @var \PHPUnit_Framework_MockObject_MockObject */ + protected $consoleOutput; + + /** @var \Symfony\Component\Console\Command\Command */ + protected $command; + + protected function setUp() { + parent::setUp(); + + $config = $this->config = $this->getMockBuilder('OCP\IConfig') + ->disableOriginalConstructor() + ->getMock(); + $this->consoleInput = $this->getMock('Symfony\Component\Console\Input\InputInterface'); + $this->consoleOutput = $this->getMock('Symfony\Component\Console\Output\OutputInterface'); + + /** @var \OCP\IConfig $config */ + $this->command = new DeleteConfig($config); + } + + + public function deleteData() { + return [ + [ + 'name', + true, + true, + 0, + 'info', + ], + [ + 'name', + true, + false, + 0, + 'info', + ], + [ + 'name', + false, + false, + 0, + 'info', + ], + [ + 'name', + false, + true, + 1, + 'error', + ], + ]; + } + + /** + * @dataProvider deleteData + * + * @param string $configName + * @param bool $configExists + * @param bool $checkIfExists + * @param int $expectedReturn + * @param string $expectedMessage + */ + public function testDelete($configName, $configExists, $checkIfExists, $expectedReturn, $expectedMessage) { + $this->config->expects(($checkIfExists) ? $this->once() : $this->never()) + ->method('getAppKeys') + ->with('app-name') + ->willReturn($configExists ? [$configName] : []); + + $this->config->expects(($expectedReturn === 0) ? $this->once() : $this->never()) + ->method('deleteAppValue') + ->with('app-name', $configName); + + $this->consoleInput->expects($this->exactly(2)) + ->method('getArgument') + ->willReturnMap([ + ['app', 'app-name'], + ['name', $configName], + ]); + $this->consoleInput->expects($this->any()) + ->method('hasParameterOption') + ->with('--error-if-not-exists') + ->willReturn($checkIfExists); + + $this->consoleOutput->expects($this->any()) + ->method('writeln') + ->with($this->stringContains($expectedMessage)); + + $this->assertSame($expectedReturn, $this->invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput])); + } +} diff --git a/tests/Core/Command/Config/App/GetConfigTest.php b/tests/Core/Command/Config/App/GetConfigTest.php new file mode 100644 index 00000000000..1ceeb16ccf4 --- /dev/null +++ b/tests/Core/Command/Config/App/GetConfigTest.php @@ -0,0 +1,163 @@ +<?php +/** + * @author Joas Schilling <nickvergessen@owncloud.com> + * + * @copyright Copyright (c) 2015, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ + +namespace Tests\Core\Command\Config\App; + + +use OC\Core\Command\Config\App\GetConfig; +use Test\TestCase; + +class GetConfigTest extends TestCase { + /** @var \PHPUnit_Framework_MockObject_MockObject */ + protected $config; + + /** @var \PHPUnit_Framework_MockObject_MockObject */ + protected $consoleInput; + /** @var \PHPUnit_Framework_MockObject_MockObject */ + protected $consoleOutput; + + /** @var \Symfony\Component\Console\Command\Command */ + protected $command; + + protected function setUp() { + parent::setUp(); + + $config = $this->config = $this->getMockBuilder('OCP\IConfig') + ->disableOriginalConstructor() + ->getMock(); + $this->consoleInput = $this->getMock('Symfony\Component\Console\Input\InputInterface'); + $this->consoleOutput = $this->getMock('Symfony\Component\Console\Output\OutputInterface'); + + /** @var \OCP\IConfig $config */ + $this->command = new GetConfig($config); + } + + + public function getData() { + return [ + // String output as json + ['name', 'newvalue', true, null, false, 'json', 0, json_encode('newvalue')], + // String output as plain text + ['name', 'newvalue', true, null, false, 'plain', 0, 'newvalue'], + // String falling back to default output as json + ['name', null, false, 'newvalue', true, 'json', 0, json_encode('newvalue')], + // String falling back without default: error + ['name', null, false, null, false, 'json', 1, null], + + // Int "0" output as json/plain + ['name', 0, true, null, false, 'json', 0, json_encode(0)], + ['name', 0, true, null, false, 'plain', 0, '0'], + // Int "1" output as json/plain + ['name', 1, true, null, false, 'json', 0, json_encode(1)], + ['name', 1, true, null, false, 'plain', 0, '1'], + + // Bool "true" output as json/plain + ['name', true, true, null, false, 'json', 0, json_encode(true)], + ['name', true, true, null, false, 'plain', 0, 'true'], + // Bool "false" output as json/plain + ['name', false, true, null, false, 'json', 0, json_encode(false)], + ['name', false, true, null, false, 'plain', 0, 'false'], + + // Null output as json/plain + ['name', null, true, null, false, 'json', 0, json_encode(null)], + ['name', null, true, null, false, 'plain', 0, 'null'], + + // Array output as json/plain + ['name', ['a', 'b'], true, null, false, 'json', 0, json_encode(['a', 'b'])], + ['name', ['a', 'b'], true, null, false, 'plain', 0, "a\nb"], + // Key array output as json/plain + ['name', [0 => 'a', 1 => 'b'], true, null, false, 'json', 0, json_encode(['a', 'b'])], + ['name', [0 => 'a', 1 => 'b'], true, null, false, 'plain', 0, "a\nb"], + // Associative array output as json/plain + ['name', ['a' => 1, 'b' => 2], true, null, false, 'json', 0, json_encode(['a' => 1, 'b' => 2])], + ['name', ['a' => 1, 'b' => 2], true, null, false, 'plain', 0, "a: 1\nb: 2"], + + ]; + } + + /** + * @dataProvider getData + * + * @param string $configName + * @param mixed $value + * @param bool $configExists + * @param mixed $defaultValue + * @param bool $hasDefault + * @param string $outputFormat + * @param int $expectedReturn + * @param string $expectedMessage + */ + public function testGet($configName, $value, $configExists, $defaultValue, $hasDefault, $outputFormat, $expectedReturn, $expectedMessage) { + $this->config->expects($this->atLeastOnce()) + ->method('getAppKeys') + ->with('app-name') + ->willReturn($configExists ? [$configName] : []); + + if (!$expectedReturn) { + if ($configExists) { + $this->config->expects($this->once()) + ->method('getAppValue') + ->with('app-name', $configName) + ->willReturn($value); + } + } + + $this->consoleInput->expects($this->exactly(2)) + ->method('getArgument') + ->willReturnMap([ + ['app', 'app-name'], + ['name', $configName], + ]); + $this->consoleInput->expects($this->any()) + ->method('getOption') + ->willReturnMap([ + ['default-value', $defaultValue], + ['output', $outputFormat], + ]); + $this->consoleInput->expects($this->any()) + ->method('hasParameterOption') + ->willReturnMap([ + ['--output', true], + ['--default-value', $hasDefault], + ]); + + if ($expectedMessage !== null) { + global $output; + + $output = ''; + $this->consoleOutput->expects($this->any()) + ->method('writeln') + ->willReturnCallback(function($value) { + global $output; + $output .= $value . "\n"; + return $output; + }); + } + + $this->assertSame($expectedReturn, $this->invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput])); + + if ($expectedMessage !== null) { + global $output; + // Remove the trailing newline + $this->assertSame($expectedMessage, substr($output, 0, -1)); + } + } +} diff --git a/tests/Core/Command/Config/App/SetConfigTest.php b/tests/Core/Command/Config/App/SetConfigTest.php new file mode 100644 index 00000000000..14d7b0cb7b5 --- /dev/null +++ b/tests/Core/Command/Config/App/SetConfigTest.php @@ -0,0 +1,118 @@ +<?php +/** + * @author Joas Schilling <nickvergessen@owncloud.com> + * + * @copyright Copyright (c) 2015, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ + +namespace Tests\Core\Command\Config\App; + + +use OC\Core\Command\Config\App\SetConfig; +use Test\TestCase; + +class SetConfigTest extends TestCase { + /** @var \PHPUnit_Framework_MockObject_MockObject */ + protected $config; + + /** @var \PHPUnit_Framework_MockObject_MockObject */ + protected $consoleInput; + /** @var \PHPUnit_Framework_MockObject_MockObject */ + protected $consoleOutput; + + /** @var \Symfony\Component\Console\Command\Command */ + protected $command; + + protected function setUp() { + parent::setUp(); + + $config = $this->config = $this->getMockBuilder('OCP\IConfig') + ->disableOriginalConstructor() + ->getMock(); + $this->consoleInput = $this->getMock('Symfony\Component\Console\Input\InputInterface'); + $this->consoleOutput = $this->getMock('Symfony\Component\Console\Output\OutputInterface'); + + /** @var \OCP\IConfig $config */ + $this->command = new SetConfig($config); + } + + + public function setData() { + return [ + [ + 'name', + 'newvalue', + true, + true, + true, + 'info', + ], + [ + 'name', + 'newvalue', + false, + true, + false, + 'comment', + ], + ]; + } + + /** + * @dataProvider setData + * + * @param string $configName + * @param mixed $newValue + * @param bool $configExists + * @param bool $updateOnly + * @param bool $updated + * @param string $expectedMessage + */ + public function testSet($configName, $newValue, $configExists, $updateOnly, $updated, $expectedMessage) { + $this->config->expects($this->once()) + ->method('getAppKeys') + ->with('app-name') + ->willReturn($configExists ? [$configName] : []); + + if ($updated) { + $this->config->expects($this->once()) + ->method('setAppValue') + ->with('app-name', $configName, $newValue); + } + + $this->consoleInput->expects($this->exactly(2)) + ->method('getArgument') + ->willReturnMap([ + ['app', 'app-name'], + ['name', $configName], + ]); + $this->consoleInput->expects($this->any()) + ->method('getOption') + ->with('value') + ->willReturn($newValue); + $this->consoleInput->expects($this->any()) + ->method('hasParameterOption') + ->with('--update-only') + ->willReturn($updateOnly); + + $this->consoleOutput->expects($this->any()) + ->method('writeln') + ->with($this->stringContains($expectedMessage)); + + $this->invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]); + } +} diff --git a/tests/Core/Command/Config/ImportTest.php b/tests/Core/Command/Config/ImportTest.php new file mode 100644 index 00000000000..f14880f8bf4 --- /dev/null +++ b/tests/Core/Command/Config/ImportTest.php @@ -0,0 +1,185 @@ +<?php +/** + * @author Joas Schilling <nickvergessen@owncloud.com> + * + * @copyright Copyright (c) 2015, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ + +namespace Tests\Core\Command\Config; + + +use OC\Core\Command\Config\Import; +use Test\TestCase; + +class ImportTest extends TestCase { + /** @var \PHPUnit_Framework_MockObject_MockObject */ + protected $config; + + /** @var \PHPUnit_Framework_MockObject_MockObject */ + protected $consoleInput; + /** @var \PHPUnit_Framework_MockObject_MockObject */ + protected $consoleOutput; + + /** @var \Symfony\Component\Console\Command\Command */ + protected $command; + + protected function setUp() { + parent::setUp(); + + $config = $this->config = $this->getMockBuilder('OCP\IConfig') + ->disableOriginalConstructor() + ->getMock(); + $this->consoleInput = $this->getMock('Symfony\Component\Console\Input\InputInterface'); + $this->consoleOutput = $this->getMock('Symfony\Component\Console\Output\OutputInterface'); + + /** @var \OCP\IConfig $config */ + $this->command = new Import($config); + } + + public function validateAppsArrayData() { + return [ + [0], + [1], + [null], + ['new \Exception()'], + [json_encode([])], + ]; + } + + /** + * @dataProvider validateAppsArrayData + * + * @param mixed $configValue + */ + public function testValidateAppsArray($configValue) { + $this->invokePrivate($this->command, 'validateAppsArray', [['app' => ['name' => $configValue]]]); + $this->assertTrue(true, 'Asserting that no exception is thrown'); + } + + public function validateAppsArrayThrowsData() { + return [ + [false], + [true], + [[]], + [new \Exception()], + ]; + } + + /** + * @dataProvider validateAppsArrayThrowsData + * + * @param mixed $configValue + */ + public function testValidateAppsArrayThrows($configValue) { + try { + $this->invokePrivate($this->command, 'validateAppsArray', [['app' => ['name' => $configValue]]]); + $this->fail('Did not throw expected UnexpectedValueException'); + } catch (\UnexpectedValueException $e) { + $this->assertStringStartsWith('Invalid app config value for "app":"name".', $e->getMessage()); + } + } + + public function checkTypeRecursivelyData() { + return [ + [0], + [1], + [null], + ['new \Exception()'], + [json_encode([])], + [false], + [true], + [[]], + [['string']], + [['test' => 'string']], + [['test' => ['sub' => 'string']]], + ]; + } + + /** + * @dataProvider checkTypeRecursivelyData + * + * @param mixed $configValue + */ + public function testCheckTypeRecursively($configValue) { + $this->invokePrivate($this->command, 'checkTypeRecursively', [$configValue, 'name']); + $this->assertTrue(true, 'Asserting that no exception is thrown'); + } + + public function checkTypeRecursivelyThrowsData() { + return [ + [new \Exception()], + [[new \Exception()]], + [['test' => new \Exception()]], + [['test' => ['sub' => new \Exception()]]], + ]; + } + + /** + * @dataProvider checkTypeRecursivelyThrowsData + * + * @param mixed $configValue + */ + public function testCheckTypeRecursivelyThrows($configValue) { + try { + $this->invokePrivate($this->command, 'checkTypeRecursively', [$configValue, 'name']); + $this->fail('Did not throw expected UnexpectedValueException'); + } catch (\UnexpectedValueException $e) { + $this->assertStringStartsWith('Invalid system config value for "name"', $e->getMessage()); + } + } + + public function validateArrayData() { + return [ + [['system' => []]], + [['apps' => []]], + [['system' => [], 'apps' => []]], + ]; + } + + /** + * @dataProvider validateArrayData + * + * @param array $configArray + */ + public function testValidateArray($configArray) { + $this->invokePrivate($this->command, 'validateArray', [$configArray]); + $this->assertTrue(true, 'Asserting that no exception is thrown'); + } + + public function validateArrayThrowsData() { + return [ + [[], 'At least one key of the following is expected:'], + [[0 => []], 'Found invalid entries in root'], + [['string' => []], 'Found invalid entries in root'], + ]; + } + + /** + * @dataProvider validateArrayThrowsData + * + * @param mixed $configArray + * @param string $expectedException + */ + public function testValidateArrayThrows($configArray, $expectedException) { + try { + $this->invokePrivate($this->command, 'validateArray', [$configArray]); + $this->fail('Did not throw expected UnexpectedValueException'); + } catch (\UnexpectedValueException $e) { + $this->assertStringStartsWith($expectedException, $e->getMessage()); + } + } +} diff --git a/tests/Core/Command/Config/ListConfigsTest.php b/tests/Core/Command/Config/ListConfigsTest.php new file mode 100644 index 00000000000..bde6a1b0db3 --- /dev/null +++ b/tests/Core/Command/Config/ListConfigsTest.php @@ -0,0 +1,324 @@ +<?php +/** + * @author Joas Schilling <nickvergessen@owncloud.com> + * + * @copyright Copyright (c) 2015, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ + +namespace Tests\Core\Command\Config; + + +use OC\Core\Command\Config\ListConfigs; +use OCP\IConfig; +use Test\TestCase; + +class ListConfigsTest extends TestCase { + /** @var \PHPUnit_Framework_MockObject_MockObject */ + protected $appConfig; + /** @var \PHPUnit_Framework_MockObject_MockObject */ + protected $systemConfig; + + /** @var \PHPUnit_Framework_MockObject_MockObject */ + protected $consoleInput; + /** @var \PHPUnit_Framework_MockObject_MockObject */ + protected $consoleOutput; + + /** @var \Symfony\Component\Console\Command\Command */ + protected $command; + + protected function setUp() { + parent::setUp(); + + $systemConfig = $this->systemConfig = $this->getMockBuilder('OC\SystemConfig') + ->disableOriginalConstructor() + ->getMock(); + $appConfig = $this->appConfig = $this->getMockBuilder('OCP\IAppConfig') + ->disableOriginalConstructor() + ->getMock(); + $this->consoleInput = $this->getMock('Symfony\Component\Console\Input\InputInterface'); + $this->consoleOutput = $this->getMock('Symfony\Component\Console\Output\OutputInterface'); + + /** @var \OC\SystemConfig $systemConfig */ + /** @var \OCP\IAppConfig $appConfig */ + $this->command = new ListConfigs($systemConfig, $appConfig); + } + + public function listData() { + return [ + [ + 'all', + // config.php + [ + 'secret', + 'overwrite.cli.url', + ], + [ + ['secret', 'N;', IConfig::SENSITIVE_VALUE], + ['overwrite.cli.url', 'N;', 'http://localhost'], + ], + // app config + [ + ['files', false, [ + 'enabled' => 'yes', + ]], + ['core', false, [ + 'global_cache_gc_lastrun' => '1430388388', + ]], + ], + false, + json_encode([ + 'system' => [ + 'secret' => IConfig::SENSITIVE_VALUE, + 'overwrite.cli.url' => 'http://localhost', + ], + 'apps' => [ + 'core' => [ + 'global_cache_gc_lastrun' => '1430388388', + ], + 'files' => [ + 'enabled' => 'yes', + ], + ], + ]), + ], + [ + 'all', + // config.php + [ + 'secret', + 'overwrite.cli.url', + ], + [ + ['secret', 'N;', 'my secret'], + ['overwrite.cli.url', 'N;', 'http://localhost'], + ], + // app config + [ + ['files', false, [ + 'enabled' => 'yes', + ]], + ['core', false, [ + 'global_cache_gc_lastrun' => '1430388388', + ]], + ], + true, + json_encode([ + 'system' => [ + 'secret' => 'my secret', + 'overwrite.cli.url' => 'http://localhost', + ], + 'apps' => [ + 'core' => [ + 'global_cache_gc_lastrun' => '1430388388', + ], + 'files' => [ + 'enabled' => 'yes', + ], + ], + ]), + ], + [ + 'system', + // config.php + [ + 'secret', + 'objectstore', + 'overwrite.cli.url', + ], + [ + ['secret', 'N;', IConfig::SENSITIVE_VALUE], + ['objectstore', 'N;', [ + 'class' => 'OC\\Files\\ObjectStore\\Swift', + 'arguments' => [ + 'username' => 'facebook100000123456789', + 'password' => IConfig::SENSITIVE_VALUE, + ], + ]], + ['overwrite.cli.url', 'N;', 'http://localhost'], + ], + // app config + [ + ['files', false, [ + 'enabled' => 'yes', + ]], + ['core', false, [ + 'global_cache_gc_lastrun' => '1430388388', + ]], + ], + false, + json_encode([ + 'system' => [ + 'secret' => IConfig::SENSITIVE_VALUE, + 'objectstore' => [ + 'class' => 'OC\\Files\\ObjectStore\\Swift', + 'arguments' => [ + 'username' => 'facebook100000123456789', + 'password' => IConfig::SENSITIVE_VALUE, + ], + ], + 'overwrite.cli.url' => 'http://localhost', + ], + ]), + ], + [ + 'system', + // config.php + [ + 'secret', + 'overwrite.cli.url', + ], + [ + ['secret', 'N;', 'my secret'], + ['overwrite.cli.url', 'N;', 'http://localhost'], + ], + // app config + [ + ['files', false, [ + 'enabled' => 'yes', + ]], + ['core', false, [ + 'global_cache_gc_lastrun' => '1430388388', + ]], + ], + true, + json_encode([ + 'system' => [ + 'secret' => 'my secret', + 'overwrite.cli.url' => 'http://localhost', + ], + ]), + ], + [ + 'files', + // config.php + [ + 'secret', + 'overwrite.cli.url', + ], + [ + ['secret', 'N;', 'my secret'], + ['overwrite.cli.url', 'N;', 'http://localhost'], + ], + // app config + [ + ['files', false, [ + 'enabled' => 'yes', + ]], + ['core', false, [ + 'global_cache_gc_lastrun' => '1430388388', + ]], + ], + false, + json_encode([ + 'apps' => [ + 'files' => [ + 'enabled' => 'yes', + ], + ], + ]), + ], + [ + 'files', + // config.php + [ + 'secret', + 'overwrite.cli.url', + ], + [ + ['secret', 'N;', 'my secret'], + ['overwrite.cli.url', 'N;', 'http://localhost'], + ], + // app config + [ + ['files', false, [ + 'enabled' => 'yes', + ]], + ['core', false, [ + 'global_cache_gc_lastrun' => '1430388388', + ]], + ], + true, + json_encode([ + 'apps' => [ + 'files' => [ + 'enabled' => 'yes', + ], + ], + ]), + ], + ]; + } + + /** + * @dataProvider listData + * + * @param string $app + * @param array $systemConfigs + * @param array $systemConfigMap + * @param array $appConfig + * @param bool $private + * @param string $expected + */ + public function testList($app, $systemConfigs, $systemConfigMap, $appConfig, $private, $expected) { + $this->systemConfig->expects($this->any()) + ->method('getKeys') + ->willReturn($systemConfigs); + if ($private) { + $this->systemConfig->expects($this->any()) + ->method('getValue') + ->willReturnMap($systemConfigMap); + } else { + $this->systemConfig->expects($this->any()) + ->method('getFilteredValue') + ->willReturnMap($systemConfigMap); + } + + $this->appConfig->expects($this->any()) + ->method('getApps') + ->willReturn(['core', 'files']); + $this->appConfig->expects($this->any()) + ->method('getValues') + ->willReturnMap($appConfig); + + $this->consoleInput->expects($this->once()) + ->method('getArgument') + ->with('app') + ->willReturn($app); + + $this->consoleInput->expects($this->any()) + ->method('getOption') + ->willReturnMap([ + ['output', 'json'], + ['private', $private], + ]); + + global $output; + + $output = ''; + $this->consoleOutput->expects($this->any()) + ->method('writeln') + ->willReturnCallback(function($value) { + global $output; + $output .= $value . "\n"; + return $output; + }); + + $this->invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]); + + $this->assertEquals($expected, trim($output, "\n")); + } +} diff --git a/tests/Core/Command/Config/System/DeleteConfigTest.php b/tests/Core/Command/Config/System/DeleteConfigTest.php new file mode 100644 index 00000000000..11bfb6ae7ad --- /dev/null +++ b/tests/Core/Command/Config/System/DeleteConfigTest.php @@ -0,0 +1,218 @@ +<?php +/** + * @author Joas Schilling <nickvergessen@owncloud.com> + * + * @copyright Copyright (c) 2015, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ + +namespace Tests\Core\Command\Config\System; + + +use OC\Core\Command\Config\System\DeleteConfig; +use Test\TestCase; + +class DeleteConfigTest extends TestCase { + /** @var \PHPUnit_Framework_MockObject_MockObject */ + protected $systemConfig; + + /** @var \PHPUnit_Framework_MockObject_MockObject */ + protected $consoleInput; + /** @var \PHPUnit_Framework_MockObject_MockObject */ + protected $consoleOutput; + + /** @var \Symfony\Component\Console\Command\Command */ + protected $command; + + protected function setUp() { + parent::setUp(); + + $systemConfig = $this->systemConfig = $this->getMockBuilder('OC\SystemConfig') + ->disableOriginalConstructor() + ->getMock(); + $this->consoleInput = $this->getMock('Symfony\Component\Console\Input\InputInterface'); + $this->consoleOutput = $this->getMock('Symfony\Component\Console\Output\OutputInterface'); + + /** @var \OC\SystemConfig $systemConfig */ + $this->command = new DeleteConfig($systemConfig); + } + + public function deleteData() { + return [ + [ + 'name1', + true, + true, + 0, + 'info', + ], + [ + 'name2', + true, + false, + 0, + 'info', + ], + [ + 'name3', + false, + false, + 0, + 'info', + ], + [ + 'name4', + false, + true, + 1, + 'error', + ], + ]; + } + + /** + * @dataProvider deleteData + * + * @param string $configName + * @param bool $configExists + * @param bool $checkIfExists + * @param int $expectedReturn + * @param string $expectedMessage + */ + public function testDelete($configName, $configExists, $checkIfExists, $expectedReturn, $expectedMessage) { + $this->systemConfig->expects(($checkIfExists) ? $this->once() : $this->never()) + ->method('getKeys') + ->willReturn($configExists ? [$configName] : []); + + $this->systemConfig->expects(($expectedReturn === 0) ? $this->once() : $this->never()) + ->method('deleteValue') + ->with($configName); + + $this->consoleInput->expects($this->once()) + ->method('getArgument') + ->with('name') + ->willReturn([$configName]); + $this->consoleInput->expects($this->any()) + ->method('hasParameterOption') + ->with('--error-if-not-exists') + ->willReturn($checkIfExists); + + $this->consoleOutput->expects($this->any()) + ->method('writeln') + ->with($this->stringContains($expectedMessage)); + + $this->assertSame($expectedReturn, $this->invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput])); + } + + public function deleteArrayData() { + return [ + [ + ['name', 'sub'], + true, + false, + true, + true, + 0, + 'info', + ], + [ + ['name', 'sub', '2sub'], + true, + false, + ['sub' => ['2sub' => 1], 'sub2' => false], + ['sub' => [], 'sub2' => false], + 0, + 'info', + ], + [ + ['name', 'sub3'], + true, + false, + ['sub' => ['2sub' => 1], 'sub2' => false], + ['sub' => ['2sub' => 1], 'sub2' => false], + 0, + 'info', + ], + [ + ['name', 'sub'], + false, + true, + true, + true, + 1, + 'error', + ], + [ + ['name', 'sub'], + true, + true, + true, + true, + 1, + 'error', + ], + [ + ['name', 'sub3'], + true, + true, + ['sub' => ['2sub' => 1], 'sub2' => false], + ['sub' => ['2sub' => 1], 'sub2' => false], + 1, + 'error', + ], + ]; + } + + /** + * @dataProvider deleteArrayData + * + * @param string[] $configNames + * @param bool $configKeyExists + * @param bool $checkIfKeyExists + * @param mixed $configValue + * @param mixed $updateValue + * @param int $expectedReturn + * @param string $expectedMessage + */ + public function testArrayDelete(array $configNames, $configKeyExists, $checkIfKeyExists, $configValue, $updateValue, $expectedReturn, $expectedMessage) { + $this->systemConfig->expects(($checkIfKeyExists) ? $this->once() : $this->never()) + ->method('getKeys') + ->willReturn($configKeyExists ? [$configNames[0]] : []); + + $this->systemConfig->expects(($configKeyExists) ? $this->once() : $this->never()) + ->method('getValue') + ->willReturn($configValue); + + $this->systemConfig->expects(($expectedReturn === 0) ? $this->once() : $this->never()) + ->method('setValue') + ->with($configNames[0], $updateValue); + + $this->consoleInput->expects($this->once()) + ->method('getArgument') + ->with('name') + ->willReturn($configNames); + $this->consoleInput->expects($this->any()) + ->method('hasParameterOption') + ->with('--error-if-not-exists') + ->willReturn($checkIfKeyExists); + + $this->consoleOutput->expects($this->any()) + ->method('writeln') + ->with($this->stringContains($expectedMessage)); + + $this->assertSame($expectedReturn, $this->invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput])); + } +} diff --git a/tests/Core/Command/Config/System/GetConfigTest.php b/tests/Core/Command/Config/System/GetConfigTest.php new file mode 100644 index 00000000000..ebbea634cde --- /dev/null +++ b/tests/Core/Command/Config/System/GetConfigTest.php @@ -0,0 +1,172 @@ +<?php +/** + * @author Joas Schilling <nickvergessen@owncloud.com> + * + * @copyright Copyright (c) 2015, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ + +namespace Tests\Core\Command\Config\System; + + +use OC\Core\Command\Config\System\GetConfig; +use Test\TestCase; + +class GetConfigTest extends TestCase { + /** @var \PHPUnit_Framework_MockObject_MockObject */ + protected $systemConfig; + + /** @var \PHPUnit_Framework_MockObject_MockObject */ + protected $consoleInput; + /** @var \PHPUnit_Framework_MockObject_MockObject */ + protected $consoleOutput; + + /** @var \Symfony\Component\Console\Command\Command */ + protected $command; + + protected function setUp() { + parent::setUp(); + + $systemConfig = $this->systemConfig = $this->getMockBuilder('OC\SystemConfig') + ->disableOriginalConstructor() + ->getMock(); + $this->consoleInput = $this->getMock('Symfony\Component\Console\Input\InputInterface'); + $this->consoleOutput = $this->getMock('Symfony\Component\Console\Output\OutputInterface'); + + /** @var \OC\SystemConfig $systemConfig */ + $this->command = new GetConfig($systemConfig); + } + + + public function getData() { + return [ + // String output as json + ['name', 'newvalue', true, null, false, 'json', 0, json_encode('newvalue')], + // String output as plain text + ['name', 'newvalue', true, null, false, 'plain', 0, 'newvalue'], + // String falling back to default output as json + ['name', null, false, 'newvalue', true, 'json', 0, json_encode('newvalue')], + // String falling back without default: error + ['name', null, false, null, false, 'json', 1, null], + + // Int "0" output as json/plain + ['name', 0, true, null, false, 'json', 0, json_encode(0)], + ['name', 0, true, null, false, 'plain', 0, '0'], + // Int "1" output as json/plain + ['name', 1, true, null, false, 'json', 0, json_encode(1)], + ['name', 1, true, null, false, 'plain', 0, '1'], + + // Bool "true" output as json/plain + ['name', true, true, null, false, 'json', 0, json_encode(true)], + ['name', true, true, null, false, 'plain', 0, 'true'], + // Bool "false" output as json/plain + ['name', false, true, null, false, 'json', 0, json_encode(false)], + ['name', false, true, null, false, 'plain', 0, 'false'], + + // Null output as json/plain + ['name', null, true, null, false, 'json', 0, json_encode(null)], + ['name', null, true, null, false, 'plain', 0, 'null'], + + // Array output as json/plain + ['name', ['a', 'b'], true, null, false, 'json', 0, json_encode(['a', 'b'])], + ['name', ['a', 'b'], true, null, false, 'plain', 0, "a\nb"], + // Key array output as json/plain + ['name', [0 => 'a', 1 => 'b'], true, null, false, 'json', 0, json_encode(['a', 'b'])], + ['name', [0 => 'a', 1 => 'b'], true, null, false, 'plain', 0, "a\nb"], + // Associative array output as json/plain + ['name', ['a' => 1, 'b' => 2], true, null, false, 'json', 0, json_encode(['a' => 1, 'b' => 2])], + ['name', ['a' => 1, 'b' => 2], true, null, false, 'plain', 0, "a: 1\nb: 2"], + + // Nested depth + [['name', 'a'], ['a' => 1, 'b' => 2], true, null, false, 'json', 0, json_encode(1)], + [['name', 'a'], ['a' => 1, 'b' => 2], true, null, false, 'plain', 0, '1'], + [['name', 'c'], ['a' => 1, 'b' => 2], true, true, true, 'json', 0, json_encode(true)], + [['name', 'c'], ['a' => 1, 'b' => 2], true, true, false, 'json', 1, null], + + ]; + } + + /** + * @dataProvider getData + * + * @param string[] $configNames + * @param mixed $value + * @param bool $configExists + * @param mixed $defaultValue + * @param bool $hasDefault + * @param string $outputFormat + * @param int $expectedReturn + * @param string $expectedMessage + */ + public function testGet($configNames, $value, $configExists, $defaultValue, $hasDefault, $outputFormat, $expectedReturn, $expectedMessage) { + if (is_array($configNames)) { + $configName = $configNames[0]; + } else { + $configName = $configNames; + $configNames = [$configName]; + } + $this->systemConfig->expects($this->atLeastOnce()) + ->method('getKeys') + ->willReturn($configExists ? [$configName] : []); + + if (!$expectedReturn) { + if ($configExists) { + $this->systemConfig->expects($this->once()) + ->method('getValue') + ->with($configName) + ->willReturn($value); + } + } + + $this->consoleInput->expects($this->once()) + ->method('getArgument') + ->with('name') + ->willReturn($configNames); + $this->consoleInput->expects($this->any()) + ->method('getOption') + ->willReturnMap([ + ['default-value', $defaultValue], + ['output', $outputFormat], + ]); + $this->consoleInput->expects($this->any()) + ->method('hasParameterOption') + ->willReturnMap([ + ['--output', true], + ['--default-value', $hasDefault], + ]); + + if ($expectedMessage !== null) { + global $output; + + $output = ''; + $this->consoleOutput->expects($this->any()) + ->method('writeln') + ->willReturnCallback(function($value) { + global $output; + $output .= $value . "\n"; + return $output; + }); + } + + $this->assertSame($expectedReturn, $this->invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput])); + + if ($expectedMessage !== null) { + global $output; + // Remove the trailing newline + $this->assertSame($expectedMessage, substr($output, 0, -1)); + } + } +} diff --git a/tests/Core/Command/Config/System/SetConfigTest.php b/tests/Core/Command/Config/System/SetConfigTest.php new file mode 100644 index 00000000000..c0b664d7522 --- /dev/null +++ b/tests/Core/Command/Config/System/SetConfigTest.php @@ -0,0 +1,175 @@ +<?php +/** + * @author Joas Schilling <nickvergessen@owncloud.com> + * + * @copyright Copyright (c) 2015, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ + +namespace Tests\Core\Command\Config\System; + + +use OC\Core\Command\Config\System\SetConfig; +use Test\TestCase; + +class SetConfigTest extends TestCase { + /** @var \PHPUnit_Framework_MockObject_MockObject */ + protected $systemConfig; + + /** @var \PHPUnit_Framework_MockObject_MockObject */ + protected $consoleInput; + /** @var \PHPUnit_Framework_MockObject_MockObject */ + protected $consoleOutput; + + /** @var \Symfony\Component\Console\Command\Command */ + protected $command; + + protected function setUp() { + parent::setUp(); + + $systemConfig = $this->systemConfig = $this->getMockBuilder('OC\SystemConfig') + ->disableOriginalConstructor() + ->getMock(); + $this->consoleInput = $this->getMock('Symfony\Component\Console\Input\InputInterface'); + $this->consoleOutput = $this->getMock('Symfony\Component\Console\Output\OutputInterface'); + + /** @var \OC\SystemConfig $systemConfig */ + $this->command = new SetConfig($systemConfig); + } + + + public function setData() { + return [ + [['name'], 'newvalue', null, 'newvalue'], + [['a', 'b', 'c'], 'foobar', null, ['b' => ['c' => 'foobar']]], + [['a', 'b', 'c'], 'foobar', ['b' => ['d' => 'barfoo']], ['b' => ['d' => 'barfoo', 'c' => 'foobar']]], + ]; + } + + /** + * @dataProvider setData + * + * @param array $configNames + * @param string $newValue + * @param mixed $existingData + * @param mixed $expectedValue + */ + public function testSet($configNames, $newValue, $existingData, $expectedValue) { + $this->systemConfig->expects($this->once()) + ->method('setValue') + ->with($configNames[0], $expectedValue); + $this->systemConfig->method('getValue') + ->with($configNames[0]) + ->willReturn($existingData); + + $this->consoleInput->expects($this->once()) + ->method('getArgument') + ->with('name') + ->willReturn($configNames); + $this->consoleInput->method('getOption') + ->will($this->returnValueMap([ + ['value', $newValue], + ['type', 'string'], + ])); + + $this->invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]); + } + + public function setUpdateOnlyProvider() { + return [ + [['name'], null], + [['a', 'b', 'c'], null], + [['a', 'b', 'c'], ['b' => 'foobar']], + [['a', 'b', 'c'], ['b' => ['d' => 'foobar']]], + ]; + } + + /** + * @dataProvider setUpdateOnlyProvider + * @expectedException \UnexpectedValueException + */ + public function testSetUpdateOnly($configNames, $existingData) { + $this->systemConfig->expects($this->never()) + ->method('setValue'); + $this->systemConfig->method('getValue') + ->with($configNames[0]) + ->willReturn($existingData); + $this->systemConfig->method('getKeys') + ->willReturn($existingData ? $configNames[0] : []); + + $this->consoleInput->expects($this->once()) + ->method('getArgument') + ->with('name') + ->willReturn($configNames); + $this->consoleInput->method('getOption') + ->will($this->returnValueMap([ + ['value', 'foobar'], + ['type', 'string'], + ['update-only', true], + ])); + + $this->invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]); + } + + public function castValueProvider() { + return [ + [null, 'string', ['value' => '', 'readable-value' => 'empty string']], + + ['abc', 'string', ['value' => 'abc', 'readable-value' => 'string abc']], + + ['123', 'integer', ['value' => 123, 'readable-value' => 'integer 123']], + ['456', 'int', ['value' => 456, 'readable-value' => 'integer 456']], + + ['2.25', 'double', ['value' => 2.25, 'readable-value' => 'double 2.25']], + ['0.5', 'float', ['value' => 0.5, 'readable-value' => 'double 0.5']], + + ['', 'null', ['value' => null, 'readable-value' => 'null']], + + ['true', 'boolean', ['value' => true, 'readable-value' => 'boolean true']], + ['false', 'bool', ['value' => false, 'readable-value' => 'boolean false']], + ]; + } + + /** + * @dataProvider castValueProvider + */ + public function testCastValue($value, $type, $expectedValue) { + $this->assertSame($expectedValue, + $this->invokePrivate($this->command, 'castValue', [$value, $type]) + ); + } + + public function castValueInvalidProvider() { + return [ + ['123', 'foobar'], + + [null, 'integer'], + ['abc', 'integer'], + ['76ggg', 'double'], + ['true', 'float'], + ['foobar', 'boolean'], + ]; + } + + /** + * @dataProvider castValueInvalidProvider + * @expectedException \InvalidArgumentException + */ + public function testCastValueInvalid($value, $type) { + $this->invokePrivate($this->command, 'castValue', [$value, $type]); + } + +} diff --git a/tests/Core/Command/Encryption/ChangeKeyStorageRootTest.php b/tests/Core/Command/Encryption/ChangeKeyStorageRootTest.php new file mode 100644 index 00000000000..2a1f48983f1 --- /dev/null +++ b/tests/Core/Command/Encryption/ChangeKeyStorageRootTest.php @@ -0,0 +1,381 @@ +<?php +/** + * @author Björn Schießle <schiessle@owncloud.com> + * + * @copyright Copyright (c) 2015, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ + + +namespace Tests\Core\Command\Encryption; + + +use OC\Core\Command\Encryption\ChangeKeyStorageRoot; +use OC\Encryption\Util; +use OC\Files\View; +use OCP\IConfig; +use OCP\IUserManager; +use Symfony\Component\Console\Helper\QuestionHelper; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; +use Test\TestCase; + +class ChangeKeyStorageRootTest extends TestCase { + + /** @var ChangeKeyStorageRoot */ + protected $changeKeyStorageRoot; + + /** @var View | \PHPUnit_Framework_MockObject_MockObject */ + protected $view; + + /** @var IUserManager | \PHPUnit_Framework_MockObject_MockObject */ + protected $userManager; + + /** @var IConfig | \PHPUnit_Framework_MockObject_MockObject */ + protected $config; + + /** @var Util | \PHPUnit_Framework_MockObject_MockObject */ + protected $util; + + /** @var QuestionHelper | \PHPUnit_Framework_MockObject_MockObject */ + protected $questionHelper; + + /** @var InputInterface | \PHPUnit_Framework_MockObject_MockObject */ + protected $inputInterface; + + /** @var OutputInterface | \PHPUnit_Framework_MockObject_MockObject */ + protected $outputInterface; + + /** @var \OCP\UserInterface | \PHPUnit_Framework_MockObject_MockObject */ + protected $userInterface; + + public function setUp() { + parent::setUp(); + + $this->view = $this->getMock('\OC\Files\View'); + $this->userManager = $this->getMock('\OCP\IUserManager'); + $this->config = $this->getMock('\OCP\IConfig'); + $this->util = $this->getMockBuilder('OC\Encryption\Util')->disableOriginalConstructor()->getMock(); + $this->questionHelper = $this->getMock('Symfony\Component\Console\Helper\QuestionHelper'); + $this->inputInterface = $this->getMock('Symfony\Component\Console\Input\InputInterface'); + $this->outputInterface = $this->getMock('Symfony\Component\Console\Output\OutputInterface'); + $this->userInterface = $this->getMock('\OCP\UserInterface'); + + $outputFormatterInterface = $this->getMock('Symfony\Component\Console\Formatter\OutputFormatterInterface'); + $this->outputInterface->expects($this->any())->method('getFormatter') + ->willReturn($outputFormatterInterface); + + $this->changeKeyStorageRoot = new ChangeKeyStorageRoot( + $this->view, + $this->userManager, + $this->config, + $this->util, + $this->questionHelper + ); + + } + + /** + * @dataProvider dataTestExecute + */ + public function testExecute($newRoot, $answer, $successMoveKey) { + + $changeKeyStorageRoot = $this->getMockBuilder('OC\Core\Command\Encryption\ChangeKeyStorageRoot') + ->setConstructorArgs( + [ + $this->view, + $this->userManager, + $this->config, + $this->util, + $this->questionHelper + ] + )->setMethods(['moveAllKeys'])->getMock(); + + $this->util->expects($this->once())->method('getKeyStorageRoot') + ->willReturn(''); + $this->inputInterface->expects($this->once())->method('getArgument') + ->with('newRoot')->willReturn($newRoot); + + if ($answer === true || $newRoot !== null) { + $changeKeyStorageRoot->expects($this->once())->method('moveAllKeys') + ->willReturn($successMoveKey); + } else { + $changeKeyStorageRoot->expects($this->never())->method('moveAllKeys'); + } + + if ($successMoveKey === true) { + $this->util->expects($this->once())->method('setKeyStorageRoot'); + } else { + $this->util->expects($this->never())->method('setKeyStorageRoot'); + } + + if ($newRoot === null) { + $this->questionHelper->expects($this->once())->method('ask')->willReturn($answer); + } else { + $this->questionHelper->expects($this->never())->method('ask'); + } + + $this->invokePrivate( + $changeKeyStorageRoot, + 'execute', + [$this->inputInterface, $this->outputInterface] + ); + } + + public function dataTestExecute() { + return [ + [null, true, true], + [null, true, false], + [null, false, null], + ['/newRoot', null, true], + ['/newRoot', null, false] + ]; + } + + public function testMoveAllKeys() { + + /** @var \OC\Core\Command\Encryption\ChangeKeyStorageRoot $changeKeyStorageRoot */ + $changeKeyStorageRoot = $this->getMockBuilder('OC\Core\Command\Encryption\ChangeKeyStorageRoot') + ->setConstructorArgs( + [ + $this->view, + $this->userManager, + $this->config, + $this->util, + $this->questionHelper + ] + )->setMethods(['prepareNewRoot', 'moveSystemKeys', 'moveUserKeys'])->getMock(); + + $changeKeyStorageRoot->expects($this->at(0))->method('prepareNewRoot')->with('newRoot'); + $changeKeyStorageRoot->expects($this->at(1))->method('moveSystemKeys')->with('oldRoot', 'newRoot'); + $changeKeyStorageRoot->expects($this->at(2))->method('moveUserKeys')->with('oldRoot', 'newRoot', $this->outputInterface); + + $this->invokePrivate($changeKeyStorageRoot, 'moveAllKeys', ['oldRoot', 'newRoot', $this->outputInterface]); + + } + + public function testPrepareNewRoot() { + $this->view->expects($this->once())->method('is_dir')->with('newRoot') + ->willReturn(true); + + $this->view->expects($this->once())->method('file_put_contents') + ->with('newRoot/' . \OC\Encryption\Keys\Storage::KEY_STORAGE_MARKER, + 'ownCloud will detect this folder as key storage root only if this file exists'); + + $this->invokePrivate($this->changeKeyStorageRoot, 'prepareNewRoot', ['newRoot']); + } + + /** + * @dataProvider dataTestPrepareNewRootException + * @expectedException \Exception + * + * @param bool $dirExists + * @param bool $couldCreateFile + */ + public function testPrepareNewRootException($dirExists, $couldCreateFile) { + $this->view->expects($this->once())->method('is_dir')->with('newRoot') + ->willReturn($dirExists); + $this->view->expects($this->any())->method('file_put_contents')->willReturn($couldCreateFile); + + $this->invokePrivate($this->changeKeyStorageRoot, 'prepareNewRoot', ['newRoot']); + } + + public function dataTestPrepareNewRootException() { + return [ + [true, false], + [false, true] + ]; + } + + /** + * @dataProvider dataTestMoveSystemKeys + * + * @param bool $dirExists + * @param bool $targetExists + * @param bool $executeRename + */ + public function testMoveSystemKeys($dirExists, $targetExists, $executeRename) { + + $changeKeyStorageRoot = $this->getMockBuilder('OC\Core\Command\Encryption\ChangeKeyStorageRoot') + ->setConstructorArgs( + [ + $this->view, + $this->userManager, + $this->config, + $this->util, + $this->questionHelper + ] + )->setMethods(['targetExists'])->getMock(); + + $this->view->expects($this->once())->method('is_dir') + ->with('oldRoot/files_encryption')->willReturn($dirExists); + $changeKeyStorageRoot->expects($this->any())->method('targetExists') + ->with('newRoot/files_encryption')->willReturn($targetExists); + + if ($executeRename) { + $this->view->expects($this->once())->method('rename') + ->with('oldRoot/files_encryption', 'newRoot/files_encryption'); + } else { + $this->view->expects($this->never())->method('rename'); + } + + $this->invokePrivate($changeKeyStorageRoot, 'moveSystemKeys', ['oldRoot', 'newRoot']); + + } + + public function dataTestMoveSystemKeys() { + return [ + [true, false, true], + [false, true, false], + [true, true, false], + [false, false, false] + ]; + } + + + public function testMoveUserKeys() { + + $changeKeyStorageRoot = $this->getMockBuilder('OC\Core\Command\Encryption\ChangeKeyStorageRoot') + ->setConstructorArgs( + [ + $this->view, + $this->userManager, + $this->config, + $this->util, + $this->questionHelper + ] + )->setMethods(['setupUserFS', 'moveUserEncryptionFolder'])->getMock(); + + $this->userManager->expects($this->once())->method('getBackends') + ->willReturn([$this->userInterface]); + $this->userInterface->expects($this->once())->method('getUsers') + ->willReturn(['user1', 'user2']); + $changeKeyStorageRoot->expects($this->exactly(2))->method('setupUserFS'); + $changeKeyStorageRoot->expects($this->exactly(2))->method('moveUserEncryptionFolder'); + + $this->invokePrivate($changeKeyStorageRoot, 'moveUserKeys', ['oldRoot', 'newRoot', $this->outputInterface]); + } + + /** + * @dataProvider dataTestMoveUserEncryptionFolder + * + * @param bool $userExists + * @param bool $isDir + * @param bool $targetExists + * @param bool $shouldRename + */ + public function testMoveUserEncryptionFolder($userExists, $isDir, $targetExists, $shouldRename) { + + $changeKeyStorageRoot = $this->getMockBuilder('OC\Core\Command\Encryption\ChangeKeyStorageRoot') + ->setConstructorArgs( + [ + $this->view, + $this->userManager, + $this->config, + $this->util, + $this->questionHelper + ] + )->setMethods(['targetExists', 'prepareParentFolder'])->getMock(); + + $this->userManager->expects($this->once())->method('userExists') + ->willReturn($userExists); + $this->view->expects($this->any())->method('is_dir') + ->willReturn($isDir); + $changeKeyStorageRoot->expects($this->any())->method('targetExists') + ->willReturn($targetExists); + + if ($shouldRename) { + $changeKeyStorageRoot->expects($this->once())->method('prepareParentFolder') + ->with('newRoot/user1'); + $this->view->expects($this->once())->method('rename') + ->with('oldRoot/user1/files_encryption', 'newRoot/user1/files_encryption'); + } else { + $changeKeyStorageRoot->expects($this->never())->method('prepareParentFolder'); + $this->view->expects($this->never())->method('rename'); + } + + $this->invokePrivate($changeKeyStorageRoot, 'moveUserEncryptionFolder', ['user1', 'oldRoot', 'newRoot']); + + } + + public function dataTestMoveUserEncryptionFolder() { + return [ + [true, true, false, true], + [true, false, true, false], + [false, true, true, false], + [false, false, true, false], + [false, true, false, false], + [false, true, true, false], + [false, false, false, false] + ]; + } + + + /** + * @dataProvider dataTestPrepareParentFolder + */ + public function testPrepareParentFolder($path, $pathExists) { + $this->view->expects($this->any())->method('file_exists') + ->willReturnCallback( + function($fileExistsPath) use ($path, $pathExists) { + if ($path === $fileExistsPath) { + return $pathExists; + } + return false; + } + ); + + if ($pathExists === false) { + $subDirs = explode('/', ltrim($path, '/')); + $this->view->expects($this->exactly(count($subDirs)))->method('mkdir'); + } else { + $this->view->expects($this->never())->method('mkdir'); + } + + $this->invokePrivate( + $this->changeKeyStorageRoot, + 'prepareParentFolder', + [$path] + ); + } + + public function dataTestPrepareParentFolder() { + return [ + ['/user/folder/sub_folder/keystorage', true], + ['/user/folder/sub_folder/keystorage', false] + ]; + } + + public function testTargetExists() { + $this->view->expects($this->once())->method('file_exists')->with('path') + ->willReturn(false); + + $this->assertFalse( + $this->invokePrivate($this->changeKeyStorageRoot, 'targetExists', ['path']) + ); + } + + /** + * @expectedException \Exception + */ + public function testTargetExistsException() { + $this->view->expects($this->once())->method('file_exists')->with('path') + ->willReturn(true); + + $this->invokePrivate($this->changeKeyStorageRoot, 'targetExists', ['path']); + } + +} diff --git a/tests/Core/Command/Encryption/DecryptAllTest.php b/tests/Core/Command/Encryption/DecryptAllTest.php new file mode 100644 index 00000000000..972ea03150c --- /dev/null +++ b/tests/Core/Command/Encryption/DecryptAllTest.php @@ -0,0 +1,218 @@ +<?php +/** + * @author Björn Schießle <schiessle@owncloud.com> + * + * @copyright Copyright (c) 2015, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ + + +namespace Tests\Core\Command\Encryption; + + +use OC\Core\Command\Encryption\DecryptAll; +use Test\TestCase; + +class DecryptAllTest extends TestCase { + + /** @var \PHPUnit_Framework_MockObject_MockObject | \OCP\IConfig */ + protected $config; + + /** @var \PHPUnit_Framework_MockObject_MockObject | \OCP\Encryption\IManager */ + protected $encryptionManager; + + /** @var \PHPUnit_Framework_MockObject_MockObject | \OCP\App\IAppManager */ + protected $appManager; + + /** @var \PHPUnit_Framework_MockObject_MockObject | \Symfony\Component\Console\Input\InputInterface */ + protected $consoleInput; + + /** @var \PHPUnit_Framework_MockObject_MockObject | \Symfony\Component\Console\Output\OutputInterface */ + protected $consoleOutput; + + /** @var \PHPUnit_Framework_MockObject_MockObject | \Symfony\Component\Console\Helper\QuestionHelper */ + protected $questionHelper; + + /** @var \PHPUnit_Framework_MockObject_MockObject | \OC\Encryption\DecryptAll */ + protected $decryptAll; + + public function setUp() { + parent::setUp(); + + $this->config = $this->getMockBuilder('OCP\IConfig') + ->disableOriginalConstructor() + ->getMock(); + $this->encryptionManager = $this->getMockBuilder('OCP\Encryption\IManager') + ->disableOriginalConstructor() + ->getMock(); + $this->appManager = $this->getMockBuilder('OCP\App\IAppManager') + ->disableOriginalConstructor() + ->getMock(); + $this->questionHelper = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper') + ->disableOriginalConstructor() + ->getMock(); + $this->decryptAll = $this->getMockBuilder('OC\Encryption\DecryptAll') + ->disableOriginalConstructor()->getMock(); + $this->consoleInput = $this->getMock('Symfony\Component\Console\Input\InputInterface'); + $this->consoleOutput = $this->getMock('Symfony\Component\Console\Output\OutputInterface'); + + $this->config->expects($this->any()) + ->method('getSystemValue') + ->with('singleuser', false) + ->willReturn(false); + $this->appManager->expects($this->any()) + ->method('isEnabledForUser') + ->with('files_trashbin')->willReturn(true); + + } + + public function testSingleUserAndTrashbin() { + + // on construct we enable single-user-mode and disable the trash bin + $this->config->expects($this->at(1)) + ->method('setSystemValue') + ->with('singleuser', true); + $this->appManager->expects($this->once()) + ->method('disableApp') + ->with('files_trashbin'); + + // on destruct wi disable single-user-mode again and enable the trash bin + $this->config->expects($this->at(2)) + ->method('setSystemValue') + ->with('singleuser', false); + $this->appManager->expects($this->once()) + ->method('enableApp') + ->with('files_trashbin'); + + $instance = new DecryptAll( + $this->encryptionManager, + $this->appManager, + $this->config, + $this->decryptAll, + $this->questionHelper + ); + $this->invokePrivate($instance, 'forceSingleUserAndTrashbin'); + + $this->assertTrue( + $this->invokePrivate($instance, 'wasTrashbinEnabled') + ); + + $this->assertFalse( + $this->invokePrivate($instance, 'wasSingleUserModeEnabled') + ); + $this->invokePrivate($instance, 'resetSingleUserAndTrashbin'); + } + + /** + * @dataProvider dataTestExecute + */ + public function testExecute($encryptionEnabled, $continue) { + + $instance = new DecryptAll( + $this->encryptionManager, + $this->appManager, + $this->config, + $this->decryptAll, + $this->questionHelper + ); + + $this->encryptionManager->expects($this->once()) + ->method('isEnabled') + ->willReturn($encryptionEnabled); + + $this->consoleInput->expects($this->any()) + ->method('getArgument') + ->with('user') + ->willReturn('user1'); + + if ($encryptionEnabled) { + $this->config->expects($this->at(0)) + ->method('setAppValue') + ->with('core', 'encryption_enabled', 'no'); + $this->questionHelper->expects($this->once()) + ->method('ask') + ->willReturn($continue); + if ($continue) { + $this->decryptAll->expects($this->once()) + ->method('decryptAll') + ->with($this->consoleInput, $this->consoleOutput, 'user1'); + } else { + $this->decryptAll->expects($this->never())->method('decryptAll'); + $this->config->expects($this->at(1)) + ->method('setAppValue') + ->with('core', 'encryption_enabled', 'yes'); + } + } else { + $this->config->expects($this->never())->method('setAppValue'); + $this->decryptAll->expects($this->never())->method('decryptAll'); + $this->questionHelper->expects($this->never())->method('ask'); + } + + $this->invokePrivate($instance, 'execute', [$this->consoleInput, $this->consoleOutput]); + } + + public function dataTestExecute() { + return [ + [true, true], + [true, false], + [false, true], + [false, false] + ]; + } + + /** + * @expectedException \Exception + */ + public function testExecuteFailure() { + $instance = new DecryptAll( + $this->encryptionManager, + $this->appManager, + $this->config, + $this->decryptAll, + $this->questionHelper + ); + + $this->config->expects($this->at(0)) + ->method('setAppValue') + ->with('core', 'encryption_enabled', 'no'); + + // make sure that we enable encryption again after a exception was thrown + $this->config->expects($this->at(3)) + ->method('setAppValue') + ->with('core', 'encryption_enabled', 'yes'); + + $this->encryptionManager->expects($this->once()) + ->method('isEnabled') + ->willReturn(true); + + $this->consoleInput->expects($this->any()) + ->method('getArgument') + ->with('user') + ->willReturn('user1'); + + $this->questionHelper->expects($this->once()) + ->method('ask') + ->willReturn(true); + + $this->decryptAll->expects($this->once()) + ->method('decryptAll') + ->with($this->consoleInput, $this->consoleOutput, 'user1') + ->willReturnCallback(function() { throw new \Exception(); }); + + $this->invokePrivate($instance, 'execute', [$this->consoleInput, $this->consoleOutput]); + } + +} diff --git a/tests/Core/Command/Encryption/DisableTest.php b/tests/Core/Command/Encryption/DisableTest.php new file mode 100644 index 00000000000..dfd06e2e26e --- /dev/null +++ b/tests/Core/Command/Encryption/DisableTest.php @@ -0,0 +1,85 @@ +<?php +/** + * @author Joas Schilling <nickvergessen@owncloud.com> + * + * @copyright Copyright (c) 2015, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ + +namespace Tests\Core\Command\Encryption; + + +use OC\Core\Command\Encryption\Disable; +use Test\TestCase; + +class DisableTest extends TestCase { + /** @var \PHPUnit_Framework_MockObject_MockObject */ + protected $config; + /** @var \PHPUnit_Framework_MockObject_MockObject */ + protected $consoleInput; + /** @var \PHPUnit_Framework_MockObject_MockObject */ + protected $consoleOutput; + + /** @var \Symfony\Component\Console\Command\Command */ + protected $command; + + protected function setUp() { + parent::setUp(); + + $config = $this->config = $this->getMockBuilder('OCP\IConfig') + ->disableOriginalConstructor() + ->getMock(); + $this->consoleInput = $this->getMock('Symfony\Component\Console\Input\InputInterface'); + $this->consoleOutput = $this->getMock('Symfony\Component\Console\Output\OutputInterface'); + + /** @var \OCP\IConfig $config */ + $this->command = new Disable($config); + } + + + public function dataDisable() { + return [ + ['yes', true, 'Encryption disabled'], + ['no', false, 'Encryption is already disabled'], + ]; + } + + /** + * @dataProvider dataDisable + * + * @param string $oldStatus + * @param bool $isUpdating + * @param string $expectedString + */ + public function testDisable($oldStatus, $isUpdating, $expectedString) { + $this->config->expects($this->once()) + ->method('getAppValue') + ->with('core', 'encryption_enabled', $this->anything()) + ->willReturn($oldStatus); + + $this->consoleOutput->expects($this->once()) + ->method('writeln') + ->with($this->stringContains($expectedString)); + + if ($isUpdating) { + $this->config->expects($this->once()) + ->method('setAppValue') + ->with('core', 'encryption_enabled', 'no'); + } + + self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]); + } +} diff --git a/tests/Core/Command/Encryption/EnableTest.php b/tests/Core/Command/Encryption/EnableTest.php new file mode 100644 index 00000000000..e2357464aa1 --- /dev/null +++ b/tests/Core/Command/Encryption/EnableTest.php @@ -0,0 +1,119 @@ +<?php +/** + * @author Joas Schilling <nickvergessen@owncloud.com> + * + * @copyright Copyright (c) 2015, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ + +namespace Tests\Core\Command\Encryption; + + +use OC\Core\Command\Encryption\Enable; +use Test\TestCase; + +class EnableTest extends TestCase { + /** @var \PHPUnit_Framework_MockObject_MockObject */ + protected $config; + /** @var \PHPUnit_Framework_MockObject_MockObject */ + protected $manager; + /** @var \PHPUnit_Framework_MockObject_MockObject */ + protected $consoleInput; + /** @var \PHPUnit_Framework_MockObject_MockObject */ + protected $consoleOutput; + + /** @var \Symfony\Component\Console\Command\Command */ + protected $command; + + protected function setUp() { + parent::setUp(); + + $config = $this->config = $this->getMockBuilder('OCP\IConfig') + ->disableOriginalConstructor() + ->getMock(); + $manager = $this->manager = $this->getMockBuilder('OCP\Encryption\IManager') + ->disableOriginalConstructor() + ->getMock(); + $this->consoleInput = $this->getMock('Symfony\Component\Console\Input\InputInterface'); + $this->consoleOutput = $this->getMock('Symfony\Component\Console\Output\OutputInterface'); + + /** @var \OCP\IConfig $config */ + /** @var \OCP\Encryption\IManager $manager */ + $this->command = new Enable($config, $manager); + } + + + public function dataEnable() { + return [ + ['no', null, [], true, 'Encryption enabled', 'No encryption module is loaded'], + ['yes', null, [], false, 'Encryption is already enabled', 'No encryption module is loaded'], + ['no', null, ['OC_TEST_MODULE' => []], true, 'Encryption enabled', 'No default module is set'], + ['no', 'OC_NO_MODULE', ['OC_TEST_MODULE' => []], true, 'Encryption enabled', 'The current default module does not exist: OC_NO_MODULE'], + ['no', 'OC_TEST_MODULE', ['OC_TEST_MODULE' => []], true, 'Encryption enabled', 'Default module: OC_TEST_MODULE'], + ]; + } + + /** + * @dataProvider dataEnable + * + * @param string $oldStatus + * @param string $defaultModule + * @param array $availableModules + * @param bool $isUpdating + * @param string $expectedString + * @param string $expectedDefaultModuleString + */ + public function testEnable($oldStatus, $defaultModule, $availableModules, $isUpdating, $expectedString, $expectedDefaultModuleString) { + $invokeCount = 0; + $this->config->expects($this->at($invokeCount)) + ->method('getAppValue') + ->with('core', 'encryption_enabled', $this->anything()) + ->willReturn($oldStatus); + $invokeCount++; + + if ($isUpdating) { + $this->config->expects($this->once()) + ->method('setAppValue') + ->with('core', 'encryption_enabled', 'yes'); + $invokeCount++; + } + + $this->manager->expects($this->atLeastOnce()) + ->method('getEncryptionModules') + ->willReturn($availableModules); + + if (!empty($availableModules)) { + $this->config->expects($this->at($invokeCount)) + ->method('getAppValue') + ->with('core', 'default_encryption_module', $this->anything()) + ->willReturn($defaultModule); + } + + $this->consoleOutput->expects($this->at(0)) + ->method('writeln') + ->with($this->stringContains($expectedString)); + + $this->consoleOutput->expects($this->at(1)) + ->method('writeln') + ->with(''); + + $this->consoleOutput->expects($this->at(2)) + ->method('writeln') + ->with($this->stringContains($expectedDefaultModuleString)); + + self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]); + } +} diff --git a/tests/Core/Command/Encryption/EncryptAllTest.php b/tests/Core/Command/Encryption/EncryptAllTest.php new file mode 100644 index 00000000000..128b4caa148 --- /dev/null +++ b/tests/Core/Command/Encryption/EncryptAllTest.php @@ -0,0 +1,133 @@ +<?php +/** + * @author Björn Schießle <schiessle@owncloud.com> + * + * @copyright Copyright (c) 2015, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ + + +namespace Tests\Core\Command\Encryption; + + +use OC\Core\Command\Encryption\EncryptAll; +use Test\TestCase; + +class EncryptAllTest extends TestCase { + + /** @var \PHPUnit_Framework_MockObject_MockObject | \OCP\IConfig */ + protected $config; + + /** @var \PHPUnit_Framework_MockObject_MockObject | \OCP\Encryption\IManager */ + protected $encryptionManager; + + /** @var \PHPUnit_Framework_MockObject_MockObject | \OCP\App\IAppManager */ + protected $appManager; + + /** @var \PHPUnit_Framework_MockObject_MockObject | \Symfony\Component\Console\Input\InputInterface */ + protected $consoleInput; + + /** @var \PHPUnit_Framework_MockObject_MockObject | \Symfony\Component\Console\Output\OutputInterface */ + protected $consoleOutput; + + /** @var \PHPUnit_Framework_MockObject_MockObject | \Symfony\Component\Console\Helper\QuestionHelper */ + protected $questionHelper; + + /** @var \PHPUnit_Framework_MockObject_MockObject | \OCP\Encryption\IEncryptionModule */ + protected $encryptionModule; + + /** @var EncryptAll */ + protected $command; + + protected function setUp() { + parent::setUp(); + + $this->config = $this->getMockBuilder('OCP\IConfig') + ->disableOriginalConstructor() + ->getMock(); + $this->encryptionManager = $this->getMockBuilder('OCP\Encryption\IManager') + ->disableOriginalConstructor() + ->getMock(); + $this->appManager = $this->getMockBuilder('OCP\App\IAppManager') + ->disableOriginalConstructor() + ->getMock(); + $this->encryptionModule = $this->getMockBuilder('\OCP\Encryption\IEncryptionModule') + ->disableOriginalConstructor() + ->getMock(); + $this->questionHelper = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper') + ->disableOriginalConstructor() + ->getMock(); + $this->consoleInput = $this->getMock('Symfony\Component\Console\Input\InputInterface'); + $this->consoleOutput = $this->getMock('Symfony\Component\Console\Output\OutputInterface'); + + } + + public function testEncryptAll() { + // trash bin needs to be disabled in order to avoid adding dummy files to the users + // trash bin which gets deleted during the encryption process + $this->appManager->expects($this->once())->method('disableApp')->with('files_trashbin'); + // enable single user mode to avoid that other user login during encryption + // destructor should disable the single user mode again + $this->config->expects($this->once())->method('getSystemValue')->with('singleuser', false)->willReturn(false); + $this->config->expects($this->at(1))->method('setSystemValue')->with('singleuser', true); + $this->config->expects($this->at(2))->method('setSystemValue')->with('singleuser', false); + + $instance = new EncryptAll($this->encryptionManager, $this->appManager, $this->config, $this->questionHelper); + $this->invokePrivate($instance, 'forceSingleUserAndTrashbin'); + $this->invokePrivate($instance, 'resetSingleUserAndTrashbin'); + } + + /** + * @dataProvider dataTestExecute + */ + public function testExecute($answer, $askResult) { + + $command = new EncryptAll($this->encryptionManager, $this->appManager, $this->config, $this->questionHelper); + + $this->encryptionManager->expects($this->once())->method('isEnabled')->willReturn(true); + $this->questionHelper->expects($this->once())->method('ask')->willReturn($askResult); + + if ($answer === 'Y' || $answer === 'y') { + $this->encryptionManager->expects($this->once()) + ->method('getEncryptionModule')->willReturn($this->encryptionModule); + $this->encryptionModule->expects($this->once()) + ->method('encryptAll')->with($this->consoleInput, $this->consoleOutput); + } else { + $this->encryptionManager->expects($this->never())->method('getEncryptionModule'); + $this->encryptionModule->expects($this->never())->method('encryptAll'); + } + + $this->invokePrivate($command, 'execute', [$this->consoleInput, $this->consoleOutput]); + } + + public function dataTestExecute() { + return [ + ['y', true], ['Y', true], ['n', false], ['N', false], ['', false] + ]; + } + + /** + * @expectedException \Exception + */ + public function testExecuteException() { + $command = new EncryptAll($this->encryptionManager, $this->appManager, $this->config, $this->questionHelper); + $this->encryptionManager->expects($this->once())->method('isEnabled')->willReturn(false); + $this->encryptionManager->expects($this->never())->method('getEncryptionModule'); + $this->encryptionModule->expects($this->never())->method('encryptAll'); + $this->invokePrivate($command, 'execute', [$this->consoleInput, $this->consoleOutput]); + } + +} diff --git a/tests/Core/Command/Encryption/SetDefaultModuleTest.php b/tests/Core/Command/Encryption/SetDefaultModuleTest.php new file mode 100644 index 00000000000..3230a57db07 --- /dev/null +++ b/tests/Core/Command/Encryption/SetDefaultModuleTest.php @@ -0,0 +1,92 @@ +<?php +/** + * @author Joas Schilling <nickvergessen@owncloud.com> + * + * @copyright Copyright (c) 2015, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ + +namespace Tests\Core\Command\Encryption; + + +use OC\Core\Command\Encryption\SetDefaultModule; +use Test\TestCase; + +class SetDefaultModuleTest extends TestCase { + /** @var \PHPUnit_Framework_MockObject_MockObject */ + protected $manager; + /** @var \PHPUnit_Framework_MockObject_MockObject */ + protected $consoleInput; + /** @var \PHPUnit_Framework_MockObject_MockObject */ + protected $consoleOutput; + + /** @var \Symfony\Component\Console\Command\Command */ + protected $command; + + protected function setUp() { + parent::setUp(); + + $manager = $this->manager = $this->getMockBuilder('OCP\Encryption\IManager') + ->disableOriginalConstructor() + ->getMock(); + $this->consoleInput = $this->getMock('Symfony\Component\Console\Input\InputInterface'); + $this->consoleOutput = $this->getMock('Symfony\Component\Console\Output\OutputInterface'); + + /** @var \OCP\Encryption\IManager $manager */ + $this->command = new SetDefaultModule($manager); + } + + + public function dataSetDefaultModule() { + return [ + ['ID0', 'ID0', null, null, 'already'], + ['ID0', 'ID1', 'ID1', true, 'info'], + ['ID0', 'ID1', 'ID1', false, 'error'], + ]; + } + + /** + * @dataProvider dataSetDefaultModule + * + * @param string $oldModule + * @param string $newModule + * @param string $updateModule + * @param bool $updateSuccess + * @param string $expectedString + */ + public function testSetDefaultModule($oldModule, $newModule, $updateModule, $updateSuccess, $expectedString) { + $this->consoleInput->expects($this->once()) + ->method('getArgument') + ->with('module') + ->willReturn($newModule); + + $this->manager->expects($this->once()) + ->method('getDefaultEncryptionModuleId') + ->willReturn($oldModule); + if ($updateModule) { + $this->manager->expects($this->once()) + ->method('setDefaultEncryptionModule') + ->with($updateModule) + ->willReturn($updateSuccess); + } + + $this->consoleOutput->expects($this->once()) + ->method('writeln') + ->with($this->stringContains($expectedString)); + + self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]); + } +} diff --git a/tests/Core/Command/Log/ManageTest.php b/tests/Core/Command/Log/ManageTest.php new file mode 100644 index 00000000000..6fb83347f23 --- /dev/null +++ b/tests/Core/Command/Log/ManageTest.php @@ -0,0 +1,181 @@ +<?php +/** + * @author Robin McCorkell <rmccorkell@owncloud.com> + * + * @copyright Copyright (c) 2015, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ + +namespace Tests\Core\Command\Log; + + +use OC\Core\Command\Log\Manage; +use Test\TestCase; + +class ManageTest extends TestCase { + /** @var \PHPUnit_Framework_MockObject_MockObject */ + protected $config; + /** @var \PHPUnit_Framework_MockObject_MockObject */ + protected $consoleInput; + /** @var \PHPUnit_Framework_MockObject_MockObject */ + protected $consoleOutput; + + /** @var \Symfony\Component\Console\Command\Command */ + protected $command; + + protected function setUp() { + parent::setUp(); + + $config = $this->config = $this->getMockBuilder('OCP\IConfig') + ->disableOriginalConstructor() + ->getMock(); + $this->consoleInput = $this->getMock('Symfony\Component\Console\Input\InputInterface'); + $this->consoleOutput = $this->getMock('Symfony\Component\Console\Output\OutputInterface'); + + $this->command = new Manage($config); + } + + public function testChangeBackend() { + $this->consoleInput->method('getOption') + ->will($this->returnValueMap([ + ['backend', 'syslog'] + ])); + $this->config->expects($this->once()) + ->method('setSystemValue') + ->with('log_type', 'syslog'); + + self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]); + } + + public function testChangeLevel() { + $this->consoleInput->method('getOption') + ->will($this->returnValueMap([ + ['level', 'debug'] + ])); + $this->config->expects($this->once()) + ->method('setSystemValue') + ->with('loglevel', 0); + + self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]); + } + + public function testChangeTimezone() { + $this->consoleInput->method('getOption') + ->will($this->returnValueMap([ + ['timezone', 'UTC'] + ])); + $this->config->expects($this->once()) + ->method('setSystemValue') + ->with('logtimezone', 'UTC'); + + self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]); + } + + /** + * @expectedException \InvalidArgumentException + */ + public function testValidateBackend() { + self::invokePrivate($this->command, 'validateBackend', ['notabackend']); + } + + /** + * @expectedException \Exception + */ + public function testValidateTimezone() { + // this might need to be changed when humanity colonises Mars + self::invokePrivate($this->command, 'validateTimezone', ['Mars/OlympusMons']); + } + + public function convertLevelStringProvider() { + return [ + ['dEbug', 0], + ['inFO', 1], + ['Warning', 2], + ['wArn', 2], + ['error', 3], + ['eRr', 3], + ]; + } + + /** + * @dataProvider convertLevelStringProvider + */ + public function testConvertLevelString($levelString, $expectedInt) { + $this->assertEquals($expectedInt, + self::invokePrivate($this->command, 'convertLevelString', [$levelString]) + ); + } + + /** + * @expectedException \InvalidArgumentException + */ + public function testConvertLevelStringInvalid() { + self::invokePrivate($this->command, 'convertLevelString', ['abc']); + } + + public function convertLevelNumberProvider() { + return [ + [0, 'Debug'], + [1, 'Info'], + [2, 'Warning'], + [3, 'Error'], + ]; + } + + /** + * @dataProvider convertLevelNumberProvider + */ + public function testConvertLevelNumber($levelNum, $expectedString) { + $this->assertEquals($expectedString, + self::invokePrivate($this->command, 'convertLevelNumber', [$levelNum]) + ); + } + + /** + * @expectedException \InvalidArgumentException + */ + public function testConvertLevelNumberInvalid() { + self::invokePrivate($this->command, 'convertLevelNumber', [11]); + } + + public function testGetConfiguration() { + $this->config->expects($this->at(0)) + ->method('getSystemValue') + ->with('log_type', 'owncloud') + ->willReturn('log_type_value'); + $this->config->expects($this->at(1)) + ->method('getSystemValue') + ->with('loglevel', 2) + ->willReturn(0); + $this->config->expects($this->at(2)) + ->method('getSystemValue') + ->with('logtimezone', 'UTC') + ->willReturn('logtimezone_value'); + + $this->consoleOutput->expects($this->at(0)) + ->method('writeln') + ->with('Enabled logging backend: log_type_value'); + $this->consoleOutput->expects($this->at(1)) + ->method('writeln') + ->with('Log level: Debug (0)'); + $this->consoleOutput->expects($this->at(2)) + ->method('writeln') + ->with('Log timezone: logtimezone_value'); + + self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]); + } + +} diff --git a/tests/Core/Command/Log/OwnCloudTest.php b/tests/Core/Command/Log/OwnCloudTest.php new file mode 100644 index 00000000000..3cb05221c37 --- /dev/null +++ b/tests/Core/Command/Log/OwnCloudTest.php @@ -0,0 +1,121 @@ +<?php +/** + * @author Robin McCorkell <rmccorkell@owncloud.com> + * + * @copyright Copyright (c) 2015, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ + +namespace Tests\Core\Command\Log; + + +use OC\Core\Command\Log\OwnCloud; +use Test\TestCase; + +class OwnCloudTest extends TestCase { + /** @var \PHPUnit_Framework_MockObject_MockObject */ + protected $config; + /** @var \PHPUnit_Framework_MockObject_MockObject */ + protected $consoleInput; + /** @var \PHPUnit_Framework_MockObject_MockObject */ + protected $consoleOutput; + + /** @var \Symfony\Component\Console\Command\Command */ + protected $command; + + protected function setUp() { + parent::setUp(); + + $config = $this->config = $this->getMockBuilder('OCP\IConfig') + ->disableOriginalConstructor() + ->getMock(); + $this->consoleInput = $this->getMock('Symfony\Component\Console\Input\InputInterface'); + $this->consoleOutput = $this->getMock('Symfony\Component\Console\Output\OutputInterface'); + + $this->command = new OwnCloud($config); + } + + public function testEnable() { + $this->consoleInput->method('getOption') + ->will($this->returnValueMap([ + ['enable', 'true'] + ])); + $this->config->expects($this->once()) + ->method('setSystemValue') + ->with('log_type', 'owncloud'); + + self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]); + } + + public function testChangeFile() { + $this->consoleInput->method('getOption') + ->will($this->returnValueMap([ + ['file', '/foo/bar/file.log'] + ])); + $this->config->expects($this->once()) + ->method('setSystemValue') + ->with('logfile', '/foo/bar/file.log'); + + self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]); + } + + public function changeRotateSizeProvider() { + return [ + ['42', 42], + ['0', 0], + ['1 kB', 1024], + ['5MB', 5 * 1024 * 1024], + ]; + } + + /** + * @dataProvider changeRotateSizeProvider + */ + public function testChangeRotateSize($optionValue, $configValue) { + $this->consoleInput->method('getOption') + ->will($this->returnValueMap([ + ['rotate-size', $optionValue] + ])); + $this->config->expects($this->once()) + ->method('setSystemValue') + ->with('log_rotate_size', $configValue); + + self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]); + } + + public function testGetConfiguration() { + $this->config->method('getSystemValue') + ->will($this->returnValueMap([ + ['log_type', 'owncloud', 'log_type_value'], + ['datadirectory', \OC::$SERVERROOT.'/data', '/data/directory/'], + ['logfile', '/data/directory/owncloud.log', '/var/log/owncloud.log'], + ['log_rotate_size', 0, 5 * 1024 * 1024], + ])); + + $this->consoleOutput->expects($this->at(0)) + ->method('writeln') + ->with('Log backend ownCloud: disabled'); + $this->consoleOutput->expects($this->at(1)) + ->method('writeln') + ->with('Log file: /var/log/owncloud.log'); + $this->consoleOutput->expects($this->at(2)) + ->method('writeln') + ->with('Rotate at: 5 MB'); + + self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]); + } + +} diff --git a/tests/Core/Command/Maintenance/DataFingerprintTest.php b/tests/Core/Command/Maintenance/DataFingerprintTest.php new file mode 100644 index 00000000000..4d661b5c027 --- /dev/null +++ b/tests/Core/Command/Maintenance/DataFingerprintTest.php @@ -0,0 +1,64 @@ +<?php +/** + * @author Roeland Jago Douma <rullzer@owncloud.com> + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ + +namespace Tests\Core\Command\Maintenance; + +use OC\Core\Command\Maintenance\DataFingerprint; +use OCP\AppFramework\Utility\ITimeFactory; +use OCP\IConfig; +use Test\TestCase; + +class DataFingerprintTest extends TestCase { + /** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */ + protected $config; + /** @var \PHPUnit_Framework_MockObject_MockObject */ + protected $consoleInput; + /** @var \PHPUnit_Framework_MockObject_MockObject */ + protected $consoleOutput; + /** @var ITimeFactory|\PHPUnit_Framework_MockObject_MockObject */ + protected $timeFactory; + + /** @var \Symfony\Component\Console\Command\Command */ + protected $command; + + protected function setUp() { + parent::setUp(); + + $this->config = $this->getMock('OCP\IConfig'); + $this->timeFactory = $this->getMock('OCP\AppFramework\Utility\ITimeFactory'); + $this->consoleInput = $this->getMock('Symfony\Component\Console\Input\InputInterface'); + $this->consoleOutput = $this->getMock('Symfony\Component\Console\Output\OutputInterface'); + + /** @var \OCP\IConfig $config */ + $this->command = new DataFingerprint($this->config, $this->timeFactory); + } + + public function testSetFingerPrint() { + $this->timeFactory->expects($this->once()) + ->method('getTime') + ->willReturn(42); + $this->config->expects($this->once()) + ->method('setSystemValue') + ->with('data-fingerprint', md5(42)); + + self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]); + } +} diff --git a/tests/Core/Command/Maintenance/Mimetype/UpdateDBTest.php b/tests/Core/Command/Maintenance/Mimetype/UpdateDBTest.php new file mode 100644 index 00000000000..217301102c5 --- /dev/null +++ b/tests/Core/Command/Maintenance/Mimetype/UpdateDBTest.php @@ -0,0 +1,184 @@ +<?php +/** + * @author Robin McCorkell <rmccorkell@owncloud.com> + * + * @copyright Copyright (c) 2015, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ + +namespace Tests\Core\Command\Maintenance\Mimetype; + +use OC\Core\Command\Maintenance\Mimetype\UpdateDB; +use Test\TestCase; +use OCP\Files\IMimeTypeDetector; +use OCP\Files\IMimeTypeLoader; + +class UpdateDBTest extends TestCase { + /** @var IMimeTypeDetector */ + protected $detector; + /** @var IMimeTypeLoader */ + protected $loader; + + /** @var \PHPUnit_Framework_MockObject_MockObject */ + protected $consoleInput; + /** @var \PHPUnit_Framework_MockObject_MockObject */ + protected $consoleOutput; + + /** @var \Symfony\Component\Console\Command\Command */ + protected $command; + + protected function setUp() { + parent::setUp(); + + $this->detector = $this->getMockBuilder('OC\Files\Type\Detection') + ->disableOriginalConstructor() + ->getMock(); + $this->loader = $this->getMockBuilder('OC\Files\Type\Loader') + ->disableOriginalConstructor() + ->getMock(); + + $this->consoleInput = $this->getMock('Symfony\Component\Console\Input\InputInterface'); + $this->consoleOutput = $this->getMock('Symfony\Component\Console\Output\OutputInterface'); + + $this->command = new UpdateDB($this->detector, $this->loader); + } + + public function testNoop() { + $this->consoleInput->method('getOption') + ->with('repair-filecache') + ->willReturn(false); + + $this->detector->expects($this->once()) + ->method('getAllMappings') + ->willReturn([ + 'ext' => ['testing/existingmimetype'] + ]); + $this->loader->expects($this->once()) + ->method('exists') + ->with('testing/existingmimetype') + ->willReturn(true); + + $this->loader->expects($this->never()) + ->method('updateFilecache'); + + $this->consoleOutput->expects($this->at(0)) + ->method('writeln') + ->with('Added 0 new mimetypes'); + $this->consoleOutput->expects($this->at(1)) + ->method('writeln') + ->with('Updated 0 filecache rows'); + + self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]); + } + + public function testAddMimetype() { + $this->consoleInput->method('getOption') + ->with('repair-filecache') + ->willReturn(false); + + $this->detector->expects($this->once()) + ->method('getAllMappings') + ->willReturn([ + 'ext' => ['testing/existingmimetype'], + 'new' => ['testing/newmimetype'] + ]); + $this->loader->expects($this->exactly(2)) + ->method('exists') + ->will($this->returnValueMap([ + ['testing/existingmimetype', true], + ['testing/newmimetype', false], + ])); + $this->loader->expects($this->exactly(2)) + ->method('getId') + ->will($this->returnValueMap([ + ['testing/existingmimetype', 1], + ['testing/newmimetype', 2], + ])); + + $this->loader->expects($this->once()) + ->method('updateFilecache') + ->with('new', 2) + ->willReturn(3); + + $this->consoleOutput->expects($this->at(0)) + ->method('writeln') + ->with('Added mimetype "testing/newmimetype" to database'); + $this->consoleOutput->expects($this->at(1)) + ->method('writeln') + ->with('Updated 3 filecache rows for mimetype "testing/newmimetype"'); + + $this->consoleOutput->expects($this->at(2)) + ->method('writeln') + ->with('Added 1 new mimetypes'); + $this->consoleOutput->expects($this->at(3)) + ->method('writeln') + ->with('Updated 3 filecache rows'); + + self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]); + } + + public function testSkipComments() { + $this->detector->expects($this->once()) + ->method('getAllMappings') + ->willReturn([ + '_comment' => 'some comment in the JSON' + ]); + $this->loader->expects($this->never()) + ->method('exists'); + + self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]); + } + + public function testRepairFilecache() { + $this->consoleInput->method('getOption') + ->with('repair-filecache') + ->willReturn(true); + + $this->detector->expects($this->once()) + ->method('getAllMappings') + ->willReturn([ + 'ext' => ['testing/existingmimetype'], + ]); + $this->loader->expects($this->exactly(1)) + ->method('exists') + ->will($this->returnValueMap([ + ['testing/existingmimetype', true], + ])); + $this->loader->expects($this->exactly(1)) + ->method('getId') + ->will($this->returnValueMap([ + ['testing/existingmimetype', 1], + ])); + + $this->loader->expects($this->once()) + ->method('updateFilecache') + ->with('ext', 1) + ->willReturn(3); + + $this->consoleOutput->expects($this->at(0)) + ->method('writeln') + ->with('Updated 3 filecache rows for mimetype "testing/existingmimetype"'); + + $this->consoleOutput->expects($this->at(1)) + ->method('writeln') + ->with('Added 0 new mimetypes'); + $this->consoleOutput->expects($this->at(2)) + ->method('writeln') + ->with('Updated 3 filecache rows'); + + self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]); + } +} diff --git a/tests/Core/Command/Maintenance/SingleUserTest.php b/tests/Core/Command/Maintenance/SingleUserTest.php new file mode 100644 index 00000000000..6629f39564f --- /dev/null +++ b/tests/Core/Command/Maintenance/SingleUserTest.php @@ -0,0 +1,129 @@ +<?php +/** + * @author Morris Jobke <hey@morrisjobke.de> + * + * @copyright Copyright (c) 2015, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ + +namespace Tests\Core\Command\Maintenance; + + +use OC\Core\Command\Maintenance\SingleUser; +use Test\TestCase; + +class SingleUserTest extends TestCase { + /** @var \PHPUnit_Framework_MockObject_MockObject */ + protected $config; + /** @var \PHPUnit_Framework_MockObject_MockObject */ + protected $consoleInput; + /** @var \PHPUnit_Framework_MockObject_MockObject */ + protected $consoleOutput; + + /** @var \Symfony\Component\Console\Command\Command */ + protected $command; + + protected function setUp() { + parent::setUp(); + + $config = $this->config = $this->getMockBuilder('OCP\IConfig') + ->disableOriginalConstructor() + ->getMock(); + $this->consoleInput = $this->getMock('Symfony\Component\Console\Input\InputInterface'); + $this->consoleOutput = $this->getMock('Symfony\Component\Console\Output\OutputInterface'); + + /** @var \OCP\IConfig $config */ + $this->command = new SingleUser($config); + } + + public function testChangeStateToOn() { + + $this->consoleInput->expects($this->once()) + ->method('getOption') + ->with('on') + ->willReturn(true); + + $this->config->expects($this->once()) + ->method('setSystemValue') + ->with('singleuser', true); + + $this->consoleOutput->expects($this->once()) + ->method('writeln') + ->with('Single user mode enabled'); + + self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]); + } + + public function testChangeStateToOff() { + + $this->consoleInput->expects($this->at(0)) + ->method('getOption') + ->with('on') + ->willReturn(false); + + $this->consoleInput->expects($this->at(1)) + ->method('getOption') + ->with('off') + ->willReturn(true); + + $this->config->expects($this->once()) + ->method('setSystemValue') + ->with('singleuser', false); + + $this->consoleOutput->expects($this->once()) + ->method('writeln') + ->with('Single user mode disabled'); + + self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]); + } + + public function stateData() { + return [ + [ true, 'Single user mode is currently enabled' ], + [ false, 'Single user mode is currently disabled' ], + ]; + } + + /** + * @dataProvider stateData + * + * @param $state + * @param $expectedOutput + */ + public function testState($state, $expectedOutput) { + + $this->consoleInput->expects($this->at(0)) + ->method('getOption') + ->with('on') + ->willReturn(false); + + $this->consoleInput->expects($this->at(1)) + ->method('getOption') + ->with('off') + ->willReturn(false); + + $this->config->expects($this->once()) + ->method('getSystemValue') + ->with('singleuser', false) + ->willReturn($state); + + $this->consoleOutput->expects($this->once()) + ->method('writeln') + ->with($expectedOutput); + + self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]); + } +} diff --git a/tests/Core/Command/User/DeleteTest.php b/tests/Core/Command/User/DeleteTest.php new file mode 100644 index 00000000000..bb813626d7a --- /dev/null +++ b/tests/Core/Command/User/DeleteTest.php @@ -0,0 +1,106 @@ +<?php +/** + * @author Joas Schilling <nickvergessen@owncloud.com> + * + * @copyright Copyright (c) 2015, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ + +namespace Tests\Core\Command\User; + + +use OC\Core\Command\User\Delete; +use Test\TestCase; + +class DeleteTest extends TestCase { + /** @var \PHPUnit_Framework_MockObject_MockObject */ + protected $userManager; + /** @var \PHPUnit_Framework_MockObject_MockObject */ + protected $consoleInput; + /** @var \PHPUnit_Framework_MockObject_MockObject */ + protected $consoleOutput; + + /** @var \Symfony\Component\Console\Command\Command */ + protected $command; + + protected function setUp() { + parent::setUp(); + + $userManager = $this->userManager = $this->getMockBuilder('OCP\IUserManager') + ->disableOriginalConstructor() + ->getMock(); + $this->consoleInput = $this->getMock('Symfony\Component\Console\Input\InputInterface'); + $this->consoleOutput = $this->getMock('Symfony\Component\Console\Output\OutputInterface'); + + /** @var \OCP\IUserManager $userManager */ + $this->command = new Delete($userManager); + } + + + public function validUserLastSeen() { + return [ + [true, 'The specified user was deleted'], + [false, 'The specified user could not be deleted'], + ]; + } + + /** + * @dataProvider validUserLastSeen + * + * @param bool $deleteSuccess + * @param string $expectedString + */ + public function testValidUser($deleteSuccess, $expectedString) { + $user = $this->getMock('OCP\IUser'); + $user->expects($this->once()) + ->method('delete') + ->willReturn($deleteSuccess); + + $this->userManager->expects($this->once()) + ->method('get') + ->with('user') + ->willReturn($user); + + $this->consoleInput->expects($this->once()) + ->method('getArgument') + ->with('uid') + ->willReturn('user'); + + $this->consoleOutput->expects($this->once()) + ->method('writeln') + ->with($this->stringContains($expectedString)); + + self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]); + } + + public function testInvalidUser() { + $this->userManager->expects($this->once()) + ->method('get') + ->with('user') + ->willReturn(null); + + $this->consoleInput->expects($this->once()) + ->method('getArgument') + ->with('uid') + ->willReturn('user'); + + $this->consoleOutput->expects($this->once()) + ->method('writeln') + ->with($this->stringContains('User does not exist')); + + self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]); + } +} diff --git a/tests/Core/Command/User/LastSeenTest.php b/tests/Core/Command/User/LastSeenTest.php new file mode 100644 index 00000000000..84805f5c072 --- /dev/null +++ b/tests/Core/Command/User/LastSeenTest.php @@ -0,0 +1,105 @@ +<?php +/** + * @author Joas Schilling <nickvergessen@owncloud.com> + * + * @copyright Copyright (c) 2015, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ + +namespace Tests\Core\Command\User; + + +use OC\Core\Command\User\LastSeen; +use Test\TestCase; + +class LastSeenTest extends TestCase { + /** @var \PHPUnit_Framework_MockObject_MockObject */ + protected $userManager; + /** @var \PHPUnit_Framework_MockObject_MockObject */ + protected $consoleInput; + /** @var \PHPUnit_Framework_MockObject_MockObject */ + protected $consoleOutput; + + /** @var \Symfony\Component\Console\Command\Command */ + protected $command; + + protected function setUp() { + parent::setUp(); + + $userManager = $this->userManager = $this->getMockBuilder('OCP\IUserManager') + ->disableOriginalConstructor() + ->getMock(); + $this->consoleInput = $this->getMock('Symfony\Component\Console\Input\InputInterface'); + $this->consoleOutput = $this->getMock('Symfony\Component\Console\Output\OutputInterface'); + + /** @var \OCP\IUserManager $userManager */ + $this->command = new LastSeen($userManager); + } + + public function validUserLastSeen() { + return [ + [0, 'never logged in'], + [time(), 'last login'], + ]; + } + + /** + * @dataProvider validUserLastSeen + * + * @param int $lastSeen + * @param string $expectedString + */ + public function testValidUser($lastSeen, $expectedString) { + $user = $this->getMock('OCP\IUser'); + $user->expects($this->once()) + ->method('getLastLogin') + ->willReturn($lastSeen); + + $this->userManager->expects($this->once()) + ->method('get') + ->with('user') + ->willReturn($user); + + $this->consoleInput->expects($this->once()) + ->method('getArgument') + ->with('uid') + ->willReturn('user'); + + $this->consoleOutput->expects($this->once()) + ->method('writeln') + ->with($this->stringContains($expectedString)); + + self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]); + } + + public function testInvalidUser() { + $this->userManager->expects($this->once()) + ->method('get') + ->with('user') + ->willReturn(null); + + $this->consoleInput->expects($this->once()) + ->method('getArgument') + ->with('uid') + ->willReturn('user'); + + $this->consoleOutput->expects($this->once()) + ->method('writeln') + ->with($this->stringContains('User does not exist')); + + self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]); + } +} diff --git a/tests/Core/Controller/AvatarControllerTest.php b/tests/Core/Controller/AvatarControllerTest.php new file mode 100644 index 00000000000..937d8aaf17b --- /dev/null +++ b/tests/Core/Controller/AvatarControllerTest.php @@ -0,0 +1,436 @@ +<?php +/** + * @author Roeland Jago Douma <roeland@famdouma.nl> + * + * @copyright Copyright (c) 2015, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ + +namespace Tests\Core\Controller; + +use OC\Core\Application; +use OCP\AppFramework\IAppContainer; +use OCP\AppFramework\Http; +use OCP\Files\File; +use OCP\Files\NotFoundException; +use OCP\IUser; +use OCP\IAvatar; +use Punic\Exception; +use Test\Traits\UserTrait; + +/** + * Overwrite is_uploaded_file in this namespace to allow proper unit testing of + * the postAvatar call. + */ +function is_uploaded_file($filename) { + return file_exists($filename); +} + +/** + * Class AvatarControllerTest + * + * @group DB + * + * @package OC\Core\Controller + */ +class AvatarControllerTest extends \Test\TestCase { + use UserTrait; + + /** @var IAppContainer */ + private $container; + /** @var \OC\Core\Controller\AvatarController */ + private $avatarController; + /** @var IAvatar */ + private $avatarMock; + /** @var IUser */ + private $userMock; + /** @var File */ + private $avatarFile; + + protected function setUp() { + parent::setUp(); + $this->createUser('userid', 'pass'); + $this->loginAsUser('userid'); + + $app = new Application; + $this->container = $app->getContainer(); + $this->container['AppName'] = 'core'; + $this->container['AvatarManager'] = $this->getMock('OCP\IAvatarManager'); + $this->container['Cache'] = $this->getMockBuilder('OC\Cache\File') + ->disableOriginalConstructor()->getMock(); + $this->container['L10N'] = $this->getMock('OCP\IL10N'); + $this->container['L10N']->method('t')->will($this->returnArgument(0)); + $this->container['UserManager'] = $this->getMock('OCP\IUserManager'); + $this->container['UserSession'] = $this->getMock('OCP\IUserSession'); + $this->container['Request'] = $this->getMock('OCP\IRequest'); + $this->container['UserFolder'] = $this->getMock('OCP\Files\Folder'); + $this->container['Logger'] = $this->getMock('OCP\ILogger'); + + $this->avatarMock = $this->getMock('OCP\IAvatar'); + $this->userMock = $this->getMock('OCP\IUser'); + + $this->avatarController = $this->container['AvatarController']; + + // Configure userMock + $this->userMock->method('getDisplayName')->willReturn('displayName'); + $this->userMock->method('getUID')->willReturn('userId'); + $this->container['UserManager']->method('get') + ->willReturnMap([['userId', $this->userMock]]); + $this->container['UserSession']->method('getUser')->willReturn($this->userMock); + + $this->avatarFile = $this->getMock('OCP\Files\File'); + $this->avatarFile->method('getContent')->willReturn('image data'); + $this->avatarFile->method('getMimeType')->willReturn('image type'); + $this->avatarFile->method('getEtag')->willReturn('my etag'); + } + + public function tearDown() { + $this->logout(); + parent::tearDown(); + } + + /** + * Fetch an avatar if a user has no avatar + */ + public function testGetAvatarNoAvatar() { + $this->container['AvatarManager']->method('getAvatar')->willReturn($this->avatarMock); + $this->avatarMock->method('getFile')->will($this->throwException(new NotFoundException())); + $response = $this->avatarController->getAvatar('userId', 32); + + //Comment out until JS is fixed + //$this->assertEquals(Http::STATUS_NOT_FOUND, $response->getStatus()); + $this->assertEquals(Http::STATUS_OK, $response->getStatus()); + $this->assertEquals('displayName', $response->getData()['data']['displayname']); + } + + /** + * Fetch the user's avatar + */ + public function testGetAvatar() { + $this->avatarMock->method('getFile')->willReturn($this->avatarFile); + $this->container['AvatarManager']->method('getAvatar')->with('userId')->willReturn($this->avatarMock); + + $response = $this->avatarController->getAvatar('userId', 32); + + $this->assertEquals(Http::STATUS_OK, $response->getStatus()); + $this->assertArrayHasKey('Content-Type', $response->getHeaders()); + $this->assertEquals('image type', $response->getHeaders()['Content-Type']); + + $this->assertEquals('my etag', $response->getEtag()); + } + + /** + * Fetch the avatar of a non-existing user + */ + public function testGetAvatarNoUser() { + $this->container['AvatarManager'] + ->method('getAvatar') + ->with('userDoesNotExist') + ->will($this->throwException(new \Exception('user does not exist'))); + + $response = $this->avatarController->getAvatar('userDoesNotExist', 32); + + //Comment out until JS is fixed + //$this->assertEquals(Http::STATUS_NOT_FOUND, $response->getStatus()); + $this->assertEquals(Http::STATUS_OK, $response->getStatus()); + $this->assertEquals('', $response->getData()['data']['displayname']); + } + + /** + * Make sure we get the correct size + */ + public function testGetAvatarSize() { + $this->avatarMock->expects($this->once()) + ->method('getFile') + ->with($this->equalTo(32)) + ->willReturn($this->avatarFile); + + $this->container['AvatarManager']->method('getAvatar')->willReturn($this->avatarMock); + + $this->avatarController->getAvatar('userId', 32); + } + + /** + * We cannot get avatars that are 0 or negative + */ + public function testGetAvatarSizeMin() { + $this->avatarMock->expects($this->once()) + ->method('getFile') + ->with($this->equalTo(64)) + ->willReturn($this->avatarFile); + + $this->container['AvatarManager']->method('getAvatar')->willReturn($this->avatarMock); + + $this->avatarController->getAvatar('userId', 0); + } + + /** + * We do not support avatars larger than 2048*2048 + */ + public function testGetAvatarSizeMax() { + $this->avatarMock->expects($this->once()) + ->method('getFile') + ->with($this->equalTo(2048)) + ->willReturn($this->avatarFile); + + $this->container['AvatarManager']->method('getAvatar')->willReturn($this->avatarMock); + + $this->avatarController->getAvatar('userId', 2049); + } + + /** + * Remove an avatar + */ + public function testDeleteAvatar() { + $this->container['AvatarManager']->method('getAvatar')->willReturn($this->avatarMock); + + $response = $this->avatarController->deleteAvatar(); + $this->assertEquals(Http::STATUS_OK, $response->getStatus()); + } + + /** + * Test what happens if the removing of the avatar fails + */ + public function testDeleteAvatarException() { + $this->avatarMock->method('remove')->will($this->throwException(new \Exception("foo"))); + $this->container['AvatarManager']->method('getAvatar')->willReturn($this->avatarMock); + + $this->container['Logger']->expects($this->once()) + ->method('logException') + ->with(new \Exception("foo")); + $expectedResponse = new Http\DataResponse(['data' => ['message' => 'An error occurred. Please contact your admin.']], Http::STATUS_BAD_REQUEST); + $this->assertEquals($expectedResponse, $this->avatarController->deleteAvatar()); + } + + /** + * Trying to get a tmp avatar when it is not available. 404 + */ + public function testTmpAvatarNoTmp() { + $response = $this->avatarController->getTmpAvatar(); + $this->assertEquals(Http::STATUS_NOT_FOUND, $response->getStatus()); + } + + /** + * Fetch tmp avatar + */ + public function testTmpAvatarValid() { + $this->container['Cache']->method('get')->willReturn(file_get_contents(\OC::$SERVERROOT.'/tests/data/testimage.jpg')); + + $response = $this->avatarController->getTmpAvatar(); + $this->assertEquals(Http::STATUS_OK, $response->getStatus()); + } + + + /** + * When trying to post a new avatar a path or image should be posted. + */ + public function testPostAvatarNoPathOrImage() { + $response = $this->avatarController->postAvatar(null); + + $this->assertEquals(Http::STATUS_BAD_REQUEST, $response->getStatus()); + } + + /** + * Test a correct post of an avatar using POST + */ + public function testPostAvatarFile() { + //Create temp file + $fileName = tempnam(null, "avatarTest"); + $copyRes = copy(\OC::$SERVERROOT.'/tests/data/testimage.jpg', $fileName); + $this->assertTrue($copyRes); + + //Create file in cache + $this->container['Cache']->method('get')->willReturn(file_get_contents(\OC::$SERVERROOT.'/tests/data/testimage.jpg')); + + //Create request return + $reqRet = ['error' => [0], 'tmp_name' => [$fileName], 'size' => [filesize(\OC::$SERVERROOT.'/tests/data/testimage.jpg')]]; + $this->container['Request']->method('getUploadedFile')->willReturn($reqRet); + + $response = $this->avatarController->postAvatar(null); + + //On correct upload always respond with the notsquare message + $this->assertEquals('notsquare', $response->getData()['data']); + + //File should be deleted + $this->assertFalse(file_exists($fileName)); + } + + /** + * Test invalid post os an avatar using POST + */ + public function testPostAvatarInvalidFile() { + //Create request return + $reqRet = ['error' => [1], 'tmp_name' => ['foo']]; + $this->container['Request']->method('getUploadedFile')->willReturn($reqRet); + + $response = $this->avatarController->postAvatar(null); + + $this->assertEquals(Http::STATUS_BAD_REQUEST, $response->getStatus()); + } + + /** + * Check what happens when we upload a GIF + */ + public function testPostAvatarFileGif() { + //Create temp file + $fileName = tempnam(null, "avatarTest"); + $copyRes = copy(\OC::$SERVERROOT.'/tests/data/testimage.gif', $fileName); + $this->assertTrue($copyRes); + + //Create file in cache + $this->container['Cache']->method('get')->willReturn(file_get_contents(\OC::$SERVERROOT.'/tests/data/testimage.gif')); + + //Create request return + $reqRet = ['error' => [0], 'tmp_name' => [$fileName], 'size' => filesize(\OC::$SERVERROOT.'/tests/data/testimage.gif')]; + $this->container['Request']->method('getUploadedFile')->willReturn($reqRet); + + $response = $this->avatarController->postAvatar(null); + + $this->assertEquals('Unknown filetype', $response->getData()['data']['message']); + + //File should be deleted + $this->assertFalse(file_exists($fileName)); + } + + /** + * Test posting avatar from existing file + */ + public function testPostAvatarFromFile() { + //Mock node API call + $file = $this->getMockBuilder('OCP\Files\File') + ->disableOriginalConstructor()->getMock(); + $file->method('getContent')->willReturn(file_get_contents(\OC::$SERVERROOT.'/tests/data/testimage.jpg')); + $this->container['UserFolder']->method('get')->willReturn($file); + + //Create request return + $response = $this->avatarController->postAvatar('avatar.jpg'); + + //On correct upload always respond with the notsquare message + $this->assertEquals('notsquare', $response->getData()['data']); + } + + /** + * Test posting avatar from existing folder + */ + public function testPostAvatarFromNoFile() { + $file = $this->getMock('OCP\Files\Node'); + $this->container['UserFolder'] + ->method('get') + ->with('folder') + ->willReturn($file); + + //Create request return + $response = $this->avatarController->postAvatar('folder'); + + //On correct upload always respond with the notsquare message + $this->assertEquals(['data' => ['message' => 'Please select a file.']], $response->getData()); + } + + /** + * Test what happens if the upload of the avatar fails + */ + public function testPostAvatarException() { + $this->container['Cache']->expects($this->once()) + ->method('set') + ->will($this->throwException(new \Exception("foo"))); + $file = $this->getMockBuilder('OCP\Files\File') + ->disableOriginalConstructor()->getMock(); + $file->method('getContent')->willReturn(file_get_contents(\OC::$SERVERROOT.'/tests/data/testimage.jpg')); + $this->container['UserFolder']->method('get')->willReturn($file); + + $this->container['Logger']->expects($this->once()) + ->method('logException') + ->with(new \Exception("foo")); + $expectedResponse = new Http\DataResponse(['data' => ['message' => 'An error occurred. Please contact your admin.']], Http::STATUS_OK); + $this->assertEquals($expectedResponse, $this->avatarController->postAvatar('avatar.jpg')); + } + + + /** + * Test invalid crop argument + */ + public function testPostCroppedAvatarInvalidCrop() { + $response = $this->avatarController->postCroppedAvatar([]); + + $this->assertEquals(Http::STATUS_BAD_REQUEST, $response->getStatus()); + } + + /** + * Test no tmp avatar to crop + */ + public function testPostCroppedAvatarNoTmpAvatar() { + $response = $this->avatarController->postCroppedAvatar(['x' => 0, 'y' => 0, 'w' => 10, 'h' => 10]); + + $this->assertEquals(Http::STATUS_BAD_REQUEST, $response->getStatus()); + } + + /** + * Test with non square crop + */ + public function testPostCroppedAvatarNoSquareCrop() { + $this->container['Cache']->method('get')->willReturn(file_get_contents(\OC::$SERVERROOT.'/tests/data/testimage.jpg')); + + $this->avatarMock->method('set')->will($this->throwException(new \OC\NotSquareException)); + $this->container['AvatarManager']->method('getAvatar')->willReturn($this->avatarMock); + $response = $this->avatarController->postCroppedAvatar(['x' => 0, 'y' => 0, 'w' => 10, 'h' => 11]); + + $this->assertEquals(Http::STATUS_BAD_REQUEST, $response->getStatus()); + } + + /** + * Check for proper reply on proper crop argument + */ + public function testPostCroppedAvatarValidCrop() { + $this->container['Cache']->method('get')->willReturn(file_get_contents(\OC::$SERVERROOT.'/tests/data/testimage.jpg')); + $this->container['AvatarManager']->method('getAvatar')->willReturn($this->avatarMock); + $response = $this->avatarController->postCroppedAvatar(['x' => 0, 'y' => 0, 'w' => 10, 'h' => 10]); + + $this->assertEquals(Http::STATUS_OK, $response->getStatus()); + $this->assertEquals('success', $response->getData()['status']); + } + + /** + * Test what happens if the cropping of the avatar fails + */ + public function testPostCroppedAvatarException() { + $this->container['Cache']->method('get')->willReturn(file_get_contents(\OC::$SERVERROOT.'/tests/data/testimage.jpg')); + + $this->avatarMock->method('set')->will($this->throwException(new \Exception('foo'))); + $this->container['AvatarManager']->method('getAvatar')->willReturn($this->avatarMock); + + $this->container['Logger']->expects($this->once()) + ->method('logException') + ->with(new \Exception('foo')); + $expectedResponse = new Http\DataResponse(['data' => ['message' => 'An error occurred. Please contact your admin.']], Http::STATUS_BAD_REQUEST); + $this->assertEquals($expectedResponse, $this->avatarController->postCroppedAvatar(['x' => 0, 'y' => 0, 'w' => 10, 'h' => 11])); + } + + + /** + * Check for proper reply on proper crop argument + */ + public function testFileTooBig() { + $fileName = \OC::$SERVERROOT.'/tests/data/testimage.jpg'; + //Create request return + $reqRet = ['error' => [0], 'tmp_name' => [$fileName], 'size' => [21*1024*1024]]; + $this->container['Request']->method('getUploadedFile')->willReturn($reqRet); + + $response = $this->avatarController->postAvatar(null); + + $this->assertEquals('File is too big', $response->getData()['data']['message']); + } + +} diff --git a/tests/Core/Controller/LoginControllerTest.php b/tests/Core/Controller/LoginControllerTest.php new file mode 100644 index 00000000000..32902a01530 --- /dev/null +++ b/tests/Core/Controller/LoginControllerTest.php @@ -0,0 +1,340 @@ +<?php +/** + * @author Lukas Reschke <lukas@owncloud.com> + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ + +namespace Tests\Core\Controller; + +use OC\Core\Controller\LoginController; +use OCP\AppFramework\Http\RedirectResponse; +use OCP\AppFramework\Http\TemplateResponse; +use OCP\IConfig; +use OCP\IRequest; +use OCP\ISession; +use OCP\IURLGenerator; +use OCP\IUserManager; +use OCP\IUserSession; +use Test\TestCase; + +class LoginControllerTest extends TestCase { + /** @var LoginController */ + private $loginController; + /** @var IRequest */ + private $request; + /** @var IUserManager */ + private $userManager; + /** @var IConfig */ + private $config; + /** @var ISession */ + private $session; + /** @var IUserSession */ + private $userSession; + /** @var IURLGenerator */ + private $urlGenerator; + + public function setUp() { + parent::setUp(); + $this->request = $this->getMock('\\OCP\\IRequest'); + $this->userManager = $this->getMock('\\OCP\\IUserManager'); + $this->config = $this->getMock('\\OCP\\IConfig'); + $this->session = $this->getMock('\\OCP\\ISession'); + $this->userSession = $this->getMockBuilder('\\OC\\User\\Session') + ->disableOriginalConstructor() + ->getMock(); + $this->urlGenerator = $this->getMock('\\OCP\\IURLGenerator'); + + $this->loginController = new LoginController( + 'core', + $this->request, + $this->userManager, + $this->config, + $this->session, + $this->userSession, + $this->urlGenerator + ); + } + + public function testLogoutWithoutToken() { + $this->request + ->expects($this->once()) + ->method('getCookie') + ->with('oc_token') + ->willReturn(null); + $this->config + ->expects($this->never()) + ->method('deleteUserValue'); + $this->urlGenerator + ->expects($this->once()) + ->method('linkToRouteAbsolute') + ->with('core.login.showLoginForm') + ->willReturn('/login'); + + $expected = new RedirectResponse('/login'); + $this->assertEquals($expected, $this->loginController->logout()); + } + + public function testLogoutWithToken() { + $this->request + ->expects($this->once()) + ->method('getCookie') + ->with('oc_token') + ->willReturn('MyLoginToken'); + $user = $this->getMock('\\OCP\\IUser'); + $user + ->expects($this->once()) + ->method('getUID') + ->willReturn('JohnDoe'); + $this->userSession + ->expects($this->once()) + ->method('getUser') + ->willReturn($user); + $this->config + ->expects($this->once()) + ->method('deleteUserValue') + ->with('JohnDoe', 'login_token', 'MyLoginToken'); + $this->urlGenerator + ->expects($this->once()) + ->method('linkToRouteAbsolute') + ->with('core.login.showLoginForm') + ->willReturn('/login'); + + $expected = new RedirectResponse('/login'); + $this->assertEquals($expected, $this->loginController->logout()); + } + + public function testShowLoginFormForLoggedInUsers() { + $this->userSession + ->expects($this->once()) + ->method('isLoggedIn') + ->willReturn(true); + + $expectedResponse = new RedirectResponse(\OC_Util::getDefaultPageUrl()); + $this->assertEquals($expectedResponse, $this->loginController->showLoginForm('', '', '')); + } + + public function testShowLoginFormWithErrorsInSession() { + $this->userSession + ->expects($this->once()) + ->method('isLoggedIn') + ->willReturn(false); + $this->session + ->expects($this->once()) + ->method('get') + ->with('loginMessages') + ->willReturn( + [ + [ + 'ErrorArray1', + 'ErrorArray2', + ], + [ + 'MessageArray1', + 'MessageArray2', + ], + ] + ); + + $expectedResponse = new TemplateResponse( + 'core', + 'login', + [ + 'ErrorArray1' => true, + 'ErrorArray2' => true, + 'messages' => [ + 'MessageArray1', + 'MessageArray2', + ], + 'loginName' => '', + 'user_autofocus' => true, + 'canResetPassword' => true, + 'alt_login' => [], + 'rememberLoginAllowed' => \OC_Util::rememberLoginAllowed(), + 'rememberLoginState' => 0, + ], + 'guest' + ); + $this->assertEquals($expectedResponse, $this->loginController->showLoginForm('', '', '')); + } + + /** + * @return array + */ + public function passwordResetDataProvider() { + return [ + [ + true, + true, + ], + [ + false, + false, + ], + ]; + } + + /** + * @dataProvider passwordResetDataProvider + */ + public function testShowLoginFormWithPasswordResetOption($canChangePassword, + $expectedResult) { + $this->userSession + ->expects($this->once()) + ->method('isLoggedIn') + ->willReturn(false); + $this->config + ->expects($this->once()) + ->method('getSystemValue') + ->with('lost_password_link') + ->willReturn(false); + $user = $this->getMock('\\OCP\\IUser'); + $user + ->expects($this->once()) + ->method('canChangePassword') + ->willReturn($canChangePassword); + $this->userManager + ->expects($this->once()) + ->method('get') + ->with('LdapUser') + ->willReturn($user); + + $expectedResponse = new TemplateResponse( + 'core', + 'login', + [ + 'messages' => [], + 'loginName' => 'LdapUser', + 'user_autofocus' => false, + 'canResetPassword' => $expectedResult, + 'alt_login' => [], + 'rememberLoginAllowed' => \OC_Util::rememberLoginAllowed(), + 'rememberLoginState' => 0, + ], + 'guest' + ); + $this->assertEquals($expectedResponse, $this->loginController->showLoginForm('LdapUser', '', '')); + } + + public function testShowLoginFormForUserNamedNull() { + $this->userSession + ->expects($this->once()) + ->method('isLoggedIn') + ->willReturn(false); + $this->config + ->expects($this->once()) + ->method('getSystemValue') + ->with('lost_password_link') + ->willReturn(false); + $user = $this->getMock('\\OCP\\IUser'); + $user + ->expects($this->once()) + ->method('canChangePassword') + ->willReturn(false); + $this->userManager + ->expects($this->once()) + ->method('get') + ->with('0') + ->willReturn($user); + + $expectedResponse = new TemplateResponse( + 'core', + 'login', + [ + 'messages' => [], + 'loginName' => '0', + 'user_autofocus' => false, + 'canResetPassword' => false, + 'alt_login' => [], + 'rememberLoginAllowed' => \OC_Util::rememberLoginAllowed(), + 'rememberLoginState' => 0, + ], + 'guest' + ); + $this->assertEquals($expectedResponse, $this->loginController->showLoginForm('0', '', '')); + } + + public function testLoginWithInvalidCredentials() { + $user = $this->getMock('\OCP\IUser'); + $password = 'secret'; + $loginPageUrl = 'some url'; + + $this->userManager->expects($this->once()) + ->method('checkPassword') + ->will($this->returnValue(false)); + $this->urlGenerator->expects($this->once()) + ->method('linkToRoute') + ->with('core.login.showLoginForm') + ->will($this->returnValue($loginPageUrl)); + + $this->userSession->expects($this->never()) + ->method('createSessionToken'); + + $expected = new \OCP\AppFramework\Http\RedirectResponse($loginPageUrl); + $this->assertEquals($expected, $this->loginController->tryLogin($user, $password, '')); + } + + public function testLoginWithValidCredentials() { + $user = $this->getMock('\OCP\IUser'); + $password = 'secret'; + $indexPageUrl = 'some url'; + + $this->userManager->expects($this->once()) + ->method('checkPassword') + ->will($this->returnValue($user)); + $this->userSession->expects($this->once()) + ->method('createSessionToken') + ->with($this->request, $user->getUID(), $password); + $this->urlGenerator->expects($this->once()) + ->method('linkToRoute') + ->with('files.view.index') + ->will($this->returnValue($indexPageUrl)); + + $expected = new \OCP\AppFramework\Http\RedirectResponse($indexPageUrl); + $this->assertEquals($expected, $this->loginController->tryLogin($user, $password, null)); + } + + public function testLoginWithValidCredentialsAndRedirectUrl() { + $user = $this->getMock('\OCP\IUser'); + $user->expects($this->any()) + ->method('getUID') + ->will($this->returnValue('jane')); + $password = 'secret'; + $originalUrl = 'another%20url'; + $redirectUrl = 'http://localhost/another url'; + + $this->userManager->expects($this->once()) + ->method('checkPassword') + ->with('jane', $password) + ->will($this->returnValue($user)); + $this->userSession->expects($this->once()) + ->method('createSessionToken') + ->with($this->request, $user->getUID(), $password); + $this->userSession->expects($this->once()) + ->method('isLoggedIn') + ->with() + ->will($this->returnValue(true)); + $this->urlGenerator->expects($this->once()) + ->method('getAbsoluteURL') + ->with(urldecode($originalUrl)) + ->will($this->returnValue($redirectUrl)); + + $expected = new \OCP\AppFramework\Http\RedirectResponse(urldecode($redirectUrl)); + $this->assertEquals($expected, $this->loginController->tryLogin($user->getUID(), $password, $originalUrl)); + } + +} diff --git a/tests/Core/Controller/LostControllerTest.php b/tests/Core/Controller/LostControllerTest.php new file mode 100644 index 00000000000..ca63c3404eb --- /dev/null +++ b/tests/Core/Controller/LostControllerTest.php @@ -0,0 +1,443 @@ +<?php +/** + * @author Lukas Reschke <lukas@owncloud.com> + * + * @copyright Copyright (c) 2015, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ + +namespace Tests\Core\Controller; + +use OC\Core\Controller\LostController; +use OCP\AppFramework\Http\TemplateResponse; +use OCP\AppFramework\Utility\ITimeFactory; +use OCP\IConfig; +use OCP\IL10N; +use OCP\IRequest; +use OCP\IURLGenerator; +use OCP\IUser; +use OCP\IUserManager; +use OCP\Mail\IMailer; +use OCP\Security\ISecureRandom; +use PHPUnit_Framework_MockObject_MockObject; + +/** + * Class LostControllerTest + * + * @package OC\Core\Controller + */ +class LostControllerTest extends \PHPUnit_Framework_TestCase { + + /** @var LostController */ + private $lostController; + /** @var IUser */ + private $existingUser; + /** @var IURLGenerator | PHPUnit_Framework_MockObject_MockObject */ + private $urlGenerator; + /** @var IL10N */ + private $l10n; + /** @var IUserManager | PHPUnit_Framework_MockObject_MockObject */ + private $userManager; + /** @var \OC_Defaults */ + private $defaults; + /** @var IConfig | PHPUnit_Framework_MockObject_MockObject */ + private $config; + /** @var IMailer | PHPUnit_Framework_MockObject_MockObject */ + private $mailer; + /** @var ISecureRandom | PHPUnit_Framework_MockObject_MockObject */ + private $secureRandom; + /** @var ITimeFactory | PHPUnit_Framework_MockObject_MockObject */ + private $timeFactory; + /** @var IRequest */ + private $request; + + protected function setUp() { + + $this->existingUser = $this->getMockBuilder('OCP\IUser') + ->disableOriginalConstructor()->getMock(); + + $this->existingUser + ->expects($this->any()) + ->method('getEMailAddress') + ->willReturn('test@example.com'); + + $this->config = $this->getMockBuilder('\OCP\IConfig') + ->disableOriginalConstructor()->getMock(); + $this->l10n = $this->getMockBuilder('\OCP\IL10N') + ->disableOriginalConstructor()->getMock(); + $this->l10n + ->expects($this->any()) + ->method('t') + ->will($this->returnCallback(function($text, $parameters = array()) { + return vsprintf($text, $parameters); + })); + $this->defaults = $this->getMockBuilder('\OC_Defaults') + ->disableOriginalConstructor()->getMock(); + $this->userManager = $this->getMockBuilder('\OCP\IUserManager') + ->disableOriginalConstructor()->getMock(); + $this->urlGenerator = $this->getMockBuilder('\OCP\IURLGenerator') + ->disableOriginalConstructor()->getMock(); + $this->mailer = $this->getMockBuilder('\OCP\Mail\IMailer') + ->disableOriginalConstructor()->getMock(); + $this->secureRandom = $this->getMockBuilder('\OCP\Security\ISecureRandom') + ->disableOriginalConstructor()->getMock(); + $this->timeFactory = $this->getMockBuilder('\OCP\AppFramework\Utility\ITimeFactory') + ->disableOriginalConstructor()->getMock(); + $this->request = $this->getMockBuilder('OCP\IRequest') + ->disableOriginalConstructor()->getMock(); + $this->lostController = new LostController( + 'Core', + $this->request, + $this->urlGenerator, + $this->userManager, + $this->defaults, + $this->l10n, + $this->config, + $this->secureRandom, + 'lostpassword-noreply@localhost', + true, + $this->mailer, + $this->timeFactory + ); + } + + public function testResetFormUnsuccessful() { + $userId = 'admin'; + $token = 'MySecretToken'; + + $this->urlGenerator + ->expects($this->once()) + ->method('linkToRouteAbsolute') + ->with('core.lost.setPassword', array('userId' => 'admin', 'token' => 'MySecretToken')) + ->will($this->returnValue('https://ownCloud.com/index.php/lostpassword/')); + + $response = $this->lostController->resetform($token, $userId); + $expectedResponse = new TemplateResponse('core', + 'lostpassword/resetpassword', + array( + 'link' => 'https://ownCloud.com/index.php/lostpassword/', + ), + 'guest'); + $this->assertEquals($expectedResponse, $response); + } + + public function testEmailUnsucessful() { + $existingUser = 'ExistingUser'; + $nonExistingUser = 'NonExistingUser'; + $this->userManager + ->expects($this->any()) + ->method('userExists') + ->will($this->returnValueMap(array( + array(true, $existingUser), + array(false, $nonExistingUser) + ))); + + // With a non existing user + $response = $this->lostController->email($nonExistingUser); + $expectedResponse = [ + 'status' => 'error', + 'msg' => 'Couldn\'t send reset email. Please make sure your username is correct.' + ]; + $this->assertSame($expectedResponse, $response); + + // With no mail address + $this->config + ->expects($this->any()) + ->method('getUserValue') + ->with($existingUser, 'settings', 'email') + ->will($this->returnValue(null)); + $response = $this->lostController->email($existingUser); + $expectedResponse = [ + 'status' => 'error', + 'msg' => 'Couldn\'t send reset email. Please make sure your username is correct.' + ]; + $this->assertSame($expectedResponse, $response); + } + + public function testEmailSuccessful() { + $this->secureRandom + ->expects($this->once()) + ->method('generate') + ->with('21') + ->will($this->returnValue('ThisIsMaybeANotSoSecretToken!')); + $this->userManager + ->expects($this->once()) + ->method('userExists') + ->with('ExistingUser') + ->will($this->returnValue(true)); + $this->userManager + ->expects($this->any()) + ->method('get') + ->with('ExistingUser') + ->willReturn($this->existingUser); + $this->timeFactory + ->expects($this->once()) + ->method('getTime') + ->will($this->returnValue(12348)); + $this->config + ->expects($this->once()) + ->method('setUserValue') + ->with('ExistingUser', 'owncloud', 'lostpassword', '12348:ThisIsMaybeANotSoSecretToken!'); + $this->urlGenerator + ->expects($this->once()) + ->method('linkToRouteAbsolute') + ->with('core.lost.resetform', array('userId' => 'ExistingUser', 'token' => 'ThisIsMaybeANotSoSecretToken!')) + ->will($this->returnValue('https://ownCloud.com/index.php/lostpassword/')); + $message = $this->getMockBuilder('\OC\Mail\Message') + ->disableOriginalConstructor()->getMock(); + $message + ->expects($this->at(0)) + ->method('setTo') + ->with(['test@example.com' => 'ExistingUser']); + $message + ->expects($this->at(1)) + ->method('setSubject') + ->with(' password reset'); + $message + ->expects($this->at(2)) + ->method('setPlainBody') + ->with('Use the following link to reset your password: https://ownCloud.com/index.php/lostpassword/'); + $message + ->expects($this->at(3)) + ->method('setFrom') + ->with(['lostpassword-noreply@localhost' => null]); + $this->mailer + ->expects($this->at(0)) + ->method('createMessage') + ->will($this->returnValue($message)); + $this->mailer + ->expects($this->at(1)) + ->method('send') + ->with($message); + + $response = $this->lostController->email('ExistingUser'); + $expectedResponse = array('status' => 'success'); + $this->assertSame($expectedResponse, $response); + } + + public function testEmailCantSendException() { + $this->secureRandom + ->expects($this->once()) + ->method('generate') + ->with('21') + ->will($this->returnValue('ThisIsMaybeANotSoSecretToken!')); + $this->userManager + ->expects($this->once()) + ->method('userExists') + ->with('ExistingUser') + ->will($this->returnValue(true)); + $this->userManager + ->expects($this->any()) + ->method('get') + ->with('ExistingUser') + ->willReturn($this->existingUser); + $this->config + ->expects($this->once()) + ->method('setUserValue') + ->with('ExistingUser', 'owncloud', 'lostpassword', '12348:ThisIsMaybeANotSoSecretToken!'); + $this->timeFactory + ->expects($this->once()) + ->method('getTime') + ->will($this->returnValue(12348)); + $this->urlGenerator + ->expects($this->once()) + ->method('linkToRouteAbsolute') + ->with('core.lost.resetform', array('userId' => 'ExistingUser', 'token' => 'ThisIsMaybeANotSoSecretToken!')) + ->will($this->returnValue('https://ownCloud.com/index.php/lostpassword/')); + $message = $this->getMockBuilder('\OC\Mail\Message') + ->disableOriginalConstructor()->getMock(); + $message + ->expects($this->at(0)) + ->method('setTo') + ->with(['test@example.com' => 'ExistingUser']); + $message + ->expects($this->at(1)) + ->method('setSubject') + ->with(' password reset'); + $message + ->expects($this->at(2)) + ->method('setPlainBody') + ->with('Use the following link to reset your password: https://ownCloud.com/index.php/lostpassword/'); + $message + ->expects($this->at(3)) + ->method('setFrom') + ->with(['lostpassword-noreply@localhost' => null]); + $this->mailer + ->expects($this->at(0)) + ->method('createMessage') + ->will($this->returnValue($message)); + $this->mailer + ->expects($this->at(1)) + ->method('send') + ->with($message) + ->will($this->throwException(new \Exception())); + + $response = $this->lostController->email('ExistingUser'); + $expectedResponse = ['status' => 'error', 'msg' => 'Couldn\'t send reset email. Please contact your administrator.']; + $this->assertSame($expectedResponse, $response); + } + + public function testSetPasswordUnsuccessful() { + $this->config + ->expects($this->once()) + ->method('getUserValue') + ->with('InvalidTokenUser', 'owncloud', 'lostpassword', null) + ->will($this->returnValue('TheOnlyAndOnlyOneTokenToResetThePassword')); + + // With an invalid token + $userName = 'InvalidTokenUser'; + $response = $this->lostController->setPassword('wrongToken', $userName, 'NewPassword', true); + $expectedResponse = [ + 'status' => 'error', + 'msg' => 'Couldn\'t reset password because the token is invalid' + ]; + $this->assertSame($expectedResponse, $response); + + // With a valid token and no proceed + $response = $this->lostController->setPassword('TheOnlyAndOnlyOneTokenToResetThePassword!', $userName, 'NewPassword', false); + $expectedResponse = ['status' => 'error', 'msg' => '', 'encryption' => true]; + $this->assertSame($expectedResponse, $response); + } + + public function testSetPasswordSuccessful() { + $this->config + ->expects($this->once()) + ->method('getUserValue') + ->with('ValidTokenUser', 'owncloud', 'lostpassword', null) + ->will($this->returnValue('12345:TheOnlyAndOnlyOneTokenToResetThePassword')); + $user = $this->getMockBuilder('\OCP\IUser') + ->disableOriginalConstructor()->getMock(); + $user + ->expects($this->once()) + ->method('getLastLogin') + ->will($this->returnValue(12344)); + $user->expects($this->once()) + ->method('setPassword') + ->with('NewPassword') + ->will($this->returnValue(true)); + $this->userManager + ->expects($this->once()) + ->method('get') + ->with('ValidTokenUser') + ->will($this->returnValue($user)); + $this->config + ->expects($this->once()) + ->method('deleteUserValue') + ->with('ValidTokenUser', 'owncloud', 'lostpassword'); + $this->timeFactory + ->expects($this->once()) + ->method('getTime') + ->will($this->returnValue(12348)); + + $response = $this->lostController->setPassword('TheOnlyAndOnlyOneTokenToResetThePassword', 'ValidTokenUser', 'NewPassword', true); + $expectedResponse = array('status' => 'success'); + $this->assertSame($expectedResponse, $response); + } + + public function testSetPasswordExpiredToken() { + $this->config + ->expects($this->once()) + ->method('getUserValue') + ->with('ValidTokenUser', 'owncloud', 'lostpassword', null) + ->will($this->returnValue('12345:TheOnlyAndOnlyOneTokenToResetThePassword')); + $user = $this->getMockBuilder('\OCP\IUser') + ->disableOriginalConstructor()->getMock(); + $this->userManager + ->expects($this->once()) + ->method('get') + ->with('ValidTokenUser') + ->will($this->returnValue($user)); + $this->timeFactory + ->expects($this->once()) + ->method('getTime') + ->will($this->returnValue(55546)); + + $response = $this->lostController->setPassword('TheOnlyAndOnlyOneTokenToResetThePassword', 'ValidTokenUser', 'NewPassword', true); + $expectedResponse = [ + 'status' => 'error', + 'msg' => 'Couldn\'t reset password because the token is expired', + ]; + $this->assertSame($expectedResponse, $response); + } + + public function testSetPasswordInvalidDataInDb() { + $this->config + ->expects($this->once()) + ->method('getUserValue') + ->with('ValidTokenUser', 'owncloud', 'lostpassword', null) + ->will($this->returnValue('TheOnlyAndOnlyOneTokenToResetThePassword')); + $user = $this->getMockBuilder('\OCP\IUser') + ->disableOriginalConstructor()->getMock(); + $this->userManager + ->expects($this->once()) + ->method('get') + ->with('ValidTokenUser') + ->will($this->returnValue($user)); + + $response = $this->lostController->setPassword('TheOnlyAndOnlyOneTokenToResetThePassword', 'ValidTokenUser', 'NewPassword', true); + $expectedResponse = [ + 'status' => 'error', + 'msg' => 'Couldn\'t reset password because the token is invalid', + ]; + $this->assertSame($expectedResponse, $response); + } + + public function testSetPasswordExpiredTokenDueToLogin() { + $this->config + ->expects($this->once()) + ->method('getUserValue') + ->with('ValidTokenUser', 'owncloud', 'lostpassword', null) + ->will($this->returnValue('12345:TheOnlyAndOnlyOneTokenToResetThePassword')); + $user = $this->getMockBuilder('\OCP\IUser') + ->disableOriginalConstructor()->getMock(); + $user + ->expects($this->once()) + ->method('getLastLogin') + ->will($this->returnValue(12346)); + $this->userManager + ->expects($this->once()) + ->method('get') + ->with('ValidTokenUser') + ->will($this->returnValue($user)); + $this->timeFactory + ->expects($this->once()) + ->method('getTime') + ->will($this->returnValue(12345)); + + $response = $this->lostController->setPassword('TheOnlyAndOnlyOneTokenToResetThePassword', 'ValidTokenUser', 'NewPassword', true); + $expectedResponse = [ + 'status' => 'error', + 'msg' => 'Couldn\'t reset password because the token is expired', + ]; + $this->assertSame($expectedResponse, $response); + } + + public function testIsSetPasswordWithoutTokenFailing() { + $this->config + ->expects($this->once()) + ->method('getUserValue') + ->with('ValidTokenUser', 'owncloud', 'lostpassword', null) + ->will($this->returnValue(null)); + + $response = $this->lostController->setPassword('', 'ValidTokenUser', 'NewPassword', true); + $expectedResponse = [ + 'status' => 'error', + 'msg' => 'Couldn\'t reset password because the token is invalid' + ]; + $this->assertSame($expectedResponse, $response); + } + +} diff --git a/tests/Core/Controller/TokenControllerTest.php b/tests/Core/Controller/TokenControllerTest.php new file mode 100644 index 00000000000..b600bfa5451 --- /dev/null +++ b/tests/Core/Controller/TokenControllerTest.php @@ -0,0 +1,95 @@ +<?php + +/** + * @author Christoph Wurst <christoph@owncloud.com> + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ + +namespace Tests\Core\Controller; + +use OC\AppFramework\Http; +use OC\Core\Controller\TokenController; +use OCP\AppFramework\Http\Response; +use Test\TestCase; + +class TokenControllerTest extends TestCase { + + /** \OC\Core\Controller\TokenController */ + private $tokenController; + private $request; + private $userManager; + private $tokenProvider; + private $secureRandom; + + protected function setUp() { + parent::setUp(); + + $this->request = $this->getMock('\OCP\IRequest'); + $this->userManager = $this->getMockBuilder('\OC\User\Manager') + ->disableOriginalConstructor() + ->getMock(); + $this->tokenProvider = $this->getMockBuilder('\OC\Authentication\Token\DefaultTokenProvider') + ->disableOriginalConstructor() + ->getMock(); + $this->secureRandom = $this->getMock('\OCP\Security\ISecureRandom'); + + $this->tokenController = new TokenController('core', $this->request, $this->userManager, $this->tokenProvider, + $this->secureRandom); + } + + public function testWithoutCredentials() { + $expected = new Response(); + $expected->setStatus(Http::STATUS_UNPROCESSABLE_ENTITY); + + $actual = $this->tokenController->generateToken(null, null); + + $this->assertEquals($expected, $actual); + } + + public function testWithInvalidCredentials() { + $this->userManager->expects($this->once()) + ->method('checkPassword') + ->with('john', 'passme') + ->will($this->returnValue(false)); + $expected = new Response(); + $expected->setStatus(Http::STATUS_UNAUTHORIZED); + + $actual = $this->tokenController->generateToken('john', 'passme'); + + $this->assertEquals($expected, $actual); + } + + public function testWithValidCredentials() { + $this->userManager->expects($this->once()) + ->method('checkPassword') + ->with('john', '123456') + ->will($this->returnValue(true)); + $this->secureRandom->expects($this->once()) + ->method('generate') + ->with(128) + ->will($this->returnValue('verysecurerandomtoken')); + $expected = [ + 'token' => 'verysecurerandomtoken' + ]; + + $actual = $this->tokenController->generateToken('john', '123456'); + + $this->assertEquals($expected, $actual); + } + +} diff --git a/tests/Core/Templates/TemplatesTest.php b/tests/Core/Templates/TemplatesTest.php new file mode 100644 index 00000000000..03565411a13 --- /dev/null +++ b/tests/Core/Templates/TemplatesTest.php @@ -0,0 +1,20 @@ +<?php + +namespace Tests\Core\Templates; + +class TemplatesTest extends \Test\TestCase { + + public function test403() { + $template = \OC::$SERVERROOT . '/core/templates/403.php'; + $expectedHtml = "<ul><li class='error'>\n\t\tAccess forbidden<br><p class='hint'></p></li></ul>"; + $this->assertTemplate($expectedHtml, $template); + } + + public function test404() { + $template = \OC::$SERVERROOT . '/core/templates/404.php'; + $href = \OC::$server->getURLGenerator()->linkTo('', 'index.php'); + $expectedHtml = "<ul><li class='error'>\n\t\t\tFile not found<br><p class='hint'>The specified document has not been found on the server.</p>\n<p class='hint'><a href='$href'>You can click here to return to ownCloud.</a></p>\n\t\t</li></ul>"; + $this->assertTemplate($expectedHtml, $template); + } + +} |