diff options
Diffstat (limited to 'tests')
25 files changed, 1103 insertions, 68 deletions
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/lostpassword/controller/lostcontrollertest.php b/tests/core/lostpassword/controller/lostcontrollertest.php index f82fc1ba3ff..0f8cb4fc5c8 100644 --- a/tests/core/lostpassword/controller/lostcontrollertest.php +++ b/tests/core/lostpassword/controller/lostcontrollertest.php @@ -1,9 +1,22 @@ <?php /** - * Copyright (c) 2014-2015 Lukas Reschke <lukas@owncloud.com> - * This file is licensed under the Affero General Public License version 3 or - * later. - * See the COPYING-README file. + * @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 OC\Core\LostPassword\Controller; @@ -47,6 +60,8 @@ class LostControllerTest extends \PHPUnit_Framework_TestCase { ->disableOriginalConstructor()->getMock(); $this->container['SecureRandom'] = $this->getMockBuilder('\OCP\Security\ISecureRandom') ->disableOriginalConstructor()->getMock(); + $this->container['TimeFactory'] = $this->getMockBuilder('\OCP\AppFramework\Utility\ITimeFactory') + ->disableOriginalConstructor()->getMock(); $this->container['IsEncryptionEnabled'] = true; $this->lostController = $this->container['LostController']; } @@ -116,6 +131,10 @@ class LostControllerTest extends \PHPUnit_Framework_TestCase { ->method('userExists') ->with('ExistingUser') ->will($this->returnValue(true)); + $this->container['TimeFactory'] + ->expects($this->once()) + ->method('getTime') + ->will($this->returnValue(12348)); $this->container['Config'] ->expects($this->once()) ->method('getUserValue') @@ -128,7 +147,7 @@ class LostControllerTest extends \PHPUnit_Framework_TestCase { $this->container['Config'] ->expects($this->once()) ->method('setUserValue') - ->with('ExistingUser', 'owncloud', 'lostpassword', 'ThisIsMaybeANotSoSecretToken!'); + ->with('ExistingUser', 'owncloud', 'lostpassword', '12348:ThisIsMaybeANotSoSecretToken!'); $this->container['URLGenerator'] ->expects($this->once()) ->method('linkToRouteAbsolute') @@ -190,7 +209,11 @@ class LostControllerTest extends \PHPUnit_Framework_TestCase { $this->container['Config'] ->expects($this->once()) ->method('setUserValue') - ->with('ExistingUser', 'owncloud', 'lostpassword', 'ThisIsMaybeANotSoSecretToken!'); + ->with('ExistingUser', 'owncloud', 'lostpassword', '12348:ThisIsMaybeANotSoSecretToken!'); + $this->container['TimeFactory'] + ->expects($this->once()) + ->method('getTime') + ->will($this->returnValue(12348)); $this->container['URLGenerator'] ->expects($this->once()) ->method('linkToRouteAbsolute') @@ -256,9 +279,13 @@ class LostControllerTest extends \PHPUnit_Framework_TestCase { ->expects($this->once()) ->method('getUserValue') ->with('ValidTokenUser', 'owncloud', 'lostpassword', null) - ->will($this->returnValue('TheOnlyAndOnlyOneTokenToResetThePassword')); + ->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') @@ -272,12 +299,94 @@ class LostControllerTest extends \PHPUnit_Framework_TestCase { ->expects($this->once()) ->method('deleteUserValue') ->with('ValidTokenUser', 'owncloud', 'lostpassword'); + $this->container['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->container['Config'] + ->expects($this->once()) + ->method('getUserValue') + ->with('ValidTokenUser', 'owncloud', 'lostpassword', null) + ->will($this->returnValue('12345:TheOnlyAndOnlyOneTokenToResetThePassword')); + $user = $this->getMockBuilder('\OCP\IUser') + ->disableOriginalConstructor()->getMock(); + $this->container['UserManager'] + ->expects($this->once()) + ->method('get') + ->with('ValidTokenUser') + ->will($this->returnValue($user)); + $this->container['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->container['Config'] + ->expects($this->once()) + ->method('getUserValue') + ->with('ValidTokenUser', 'owncloud', 'lostpassword', null) + ->will($this->returnValue('TheOnlyAndOnlyOneTokenToResetThePassword')); + $user = $this->getMockBuilder('\OCP\IUser') + ->disableOriginalConstructor()->getMock(); + $this->container['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->container['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->container['UserManager'] + ->expects($this->once()) + ->method('get') + ->with('ValidTokenUser') + ->will($this->returnValue($user)); + $this->container['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->container['Config'] ->expects($this->once()) diff --git a/tests/lib/activitymanager.php b/tests/lib/activitymanager.php index 28caf575948..26759d46270 100644 --- a/tests/lib/activitymanager.php +++ b/tests/lib/activitymanager.php @@ -41,6 +41,9 @@ class Test_ActivityManager extends \Test\TestCase { $this->config ); + $this->assertSame([], $this->invokePrivate($this->activityManager, 'getConsumers')); + $this->assertSame([], $this->invokePrivate($this->activityManager, 'getExtensions')); + $this->activityManager->registerConsumer(function() { return new NoOpConsumer(); }); @@ -50,6 +53,11 @@ class Test_ActivityManager extends \Test\TestCase { $this->activityManager->registerExtension(function() { return new SimpleExtension(); }); + + $this->assertNotEmpty($this->invokePrivate($this->activityManager, 'getConsumers')); + $this->assertNotEmpty($this->invokePrivate($this->activityManager, 'getConsumers')); + $this->assertNotEmpty($this->invokePrivate($this->activityManager, 'getExtensions')); + $this->assertNotEmpty($this->invokePrivate($this->activityManager, 'getExtensions')); } public function testGetConsumers() { @@ -249,6 +257,192 @@ class Test_ActivityManager extends \Test\TestCase { ->method('getUser') ->willReturn($mockUser); } + + /** + * @expectedException BadMethodCallException + * @expectedExceptionMessage App not set + * @expectedExceptionCode 10 + */ + public function testPublishExceptionNoApp() { + $event = new \OC\Activity\Event(); + $this->activityManager->publish($event); + } + + /** + * @expectedException BadMethodCallException + * @expectedExceptionMessage Type not set + * @expectedExceptionCode 11 + */ + public function testPublishExceptionNoType() { + $event = new \OC\Activity\Event(); + $event->setApp('test'); + $this->activityManager->publish($event); + } + + /** + * @expectedException BadMethodCallException + * @expectedExceptionMessage Affected user not set + * @expectedExceptionCode 12 + */ + public function testPublishExceptionNoAffectedUser() { + $event = new \OC\Activity\Event(); + $event->setApp('test') + ->setType('test_type'); + $this->activityManager->publish($event); + } + + /** + * @expectedException BadMethodCallException + * @expectedExceptionMessage Subject not set + * @expectedExceptionCode 13 + */ + public function testPublishExceptionNoSubject() { + $event = new \OC\Activity\Event(); + $event->setApp('test') + ->setType('test_type') + ->setAffectedUser('test_affected'); + $this->activityManager->publish($event); + } + + public function dataPublish() { + return [ + [null], + ['test_author'], + ]; + } + + /** + * @dataProvider dataPublish + * @param string $author + */ + public function testPublish($author) { + if ($author !== null) { + $authorObject = $this->getMockBuilder('OCP\IUser') + ->disableOriginalConstructor() + ->getMock(); + $authorObject->expects($this->once()) + ->method('getUID') + ->willReturn($author); + $this->session->expects($this->atLeastOnce()) + ->method('getUser') + ->willReturn($authorObject); + } + + $event = new \OC\Activity\Event(); + $event->setApp('test') + ->setType('test_type') + ->setSubject('test_subject', []) + ->setAffectedUser('test_affected'); + + $consumer = $this->getMockBuilder('OCP\Activity\IConsumer') + ->disableOriginalConstructor() + ->getMock(); + $consumer->expects($this->once()) + ->method('receive') + ->with($event) + ->willReturnCallback(function(\OCP\Activity\IEvent $event) use ($author) { + $this->assertLessThanOrEqual(time() + 2, $event->getTimestamp(), 'Timestamp not set correctly'); + $this->assertGreaterThanOrEqual(time() - 2, $event->getTimestamp(), 'Timestamp not set correctly'); + $this->assertSame($author, $event->getAuthor(), 'Author name not set correctly'); + }); + $this->activityManager->registerConsumer(function () use ($consumer) { + return $consumer; + }); + + $this->activityManager->publish($event); + } + + public function testPublishAllManually() { + $event = new \OC\Activity\Event(); + $event->setApp('test_app') + ->setType('test_type') + ->setAffectedUser('test_affected') + ->setAuthor('test_author') + ->setTimestamp(1337) + ->setSubject('test_subject', ['test_subject_param']) + ->setMessage('test_message', ['test_message_param']) + ->setObject('test_object_type', 42, 'test_object_name') + ->setLink('test_link') + ; + + $consumer = $this->getMockBuilder('OCP\Activity\IConsumer') + ->disableOriginalConstructor() + ->getMock(); + $consumer->expects($this->once()) + ->method('receive') + ->willReturnCallback(function(\OCP\Activity\IEvent $event) { + $this->assertSame('test_app', $event->getApp(), 'App not set correctly'); + $this->assertSame('test_type', $event->getType(), 'Type not set correctly'); + $this->assertSame('test_affected', $event->getAffectedUser(), 'Affected user not set correctly'); + $this->assertSame('test_author', $event->getAuthor(), 'Author not set correctly'); + $this->assertSame(1337, $event->getTimestamp(), 'Timestamp not set correctly'); + $this->assertSame('test_subject', $event->getSubject(), 'Subject not set correctly'); + $this->assertSame(['test_subject_param'], $event->getSubjectParameters(), 'Subject parameter not set correctly'); + $this->assertSame('test_message', $event->getMessage(), 'Message not set correctly'); + $this->assertSame(['test_message_param'], $event->getMessageParameters(), 'Message parameter not set correctly'); + $this->assertSame('test_object_type', $event->getObjectType(), 'Object type not set correctly'); + $this->assertSame(42, $event->getObjectId(), 'Object ID not set correctly'); + $this->assertSame('test_object_name', $event->getObjectName(), 'Object name not set correctly'); + $this->assertSame('test_link', $event->getLink(), 'Link not set correctly'); + }); + $this->activityManager->registerConsumer(function () use ($consumer) { + return $consumer; + }); + + $this->activityManager->publish($event); + } + + public function testDeprecatedPublishActivity() { + $event = new \OC\Activity\Event(); + $event->setApp('test_app') + ->setType('test_type') + ->setAffectedUser('test_affected') + ->setAuthor('test_author') + ->setTimestamp(1337) + ->setSubject('test_subject', ['test_subject_param']) + ->setMessage('test_message', ['test_message_param']) + ->setObject('test_object_type', 42, 'test_object_name') + ->setLink('test_link') + ; + + $consumer = $this->getMockBuilder('OCP\Activity\IConsumer') + ->disableOriginalConstructor() + ->getMock(); + $consumer->expects($this->once()) + ->method('receive') + ->willReturnCallback(function(\OCP\Activity\IEvent $event) { + $this->assertSame('test_app', $event->getApp(), 'App not set correctly'); + $this->assertSame('test_type', $event->getType(), 'Type not set correctly'); + $this->assertSame('test_affected', $event->getAffectedUser(), 'Affected user not set correctly'); + $this->assertSame('test_subject', $event->getSubject(), 'Subject not set correctly'); + $this->assertSame(['test_subject_param'], $event->getSubjectParameters(), 'Subject parameter not set correctly'); + $this->assertSame('test_message', $event->getMessage(), 'Message not set correctly'); + $this->assertSame(['test_message_param'], $event->getMessageParameters(), 'Message parameter not set correctly'); + $this->assertSame('test_object_name', $event->getObjectName(), 'Object name not set correctly'); + $this->assertSame('test_link', $event->getLink(), 'Link not set correctly'); + + // The following values can not be used via publishActivity() + $this->assertLessThanOrEqual(time() + 2, $event->getTimestamp(), 'Timestamp not set correctly'); + $this->assertGreaterThanOrEqual(time() - 2, $event->getTimestamp(), 'Timestamp not set correctly'); + $this->assertSame(null, $event->getAuthor(), 'Author not set correctly'); + $this->assertSame('', $event->getObjectType(), 'Object type should not be set'); + $this->assertSame(0, $event->getObjectId(), 'Object ID should not be set'); + }); + $this->activityManager->registerConsumer(function () use ($consumer) { + return $consumer; + }); + + $this->activityManager->publishActivity( + $event->getApp(), + $event->getSubject(), $event->getSubjectParameters(), + $event->getMessage(), $event->getMessageParameters(), + $event->getObjectName(), + $event->getLink(), + $event->getAffectedUser(), + $event->getType(), + \OCP\Activity\IExtension::PRIORITY_MEDIUM + ); + } } class SimpleExtension implements \OCP\Activity\IExtension { @@ -368,6 +562,7 @@ class NoOpExtension implements \OCP\Activity\IExtension { class NoOpConsumer implements \OCP\Activity\IConsumer { - public function receive($app, $subject, $subjectParams, $message, $messageParams, $file, $link, $affectedUser, $type, $priority) { + public function receive(\OCP\Activity\IEvent $event) { + } } diff --git a/tests/lib/app/manager.php b/tests/lib/app/manager.php index 6cf7eb3bb6c..7333d7601b1 100644 --- a/tests/lib/app/manager.php +++ b/tests/lib/app/manager.php @@ -204,4 +204,74 @@ class Manager extends \PHPUnit_Framework_TestCase { $this->appConfig->setValue('test4', 'enabled', '["asd"]'); $this->assertEquals(['test1', 'test3'], $this->manager->getEnabledAppsForUser($user)); } + + public function testGetAppsNeedingUpgrade() { + $this->manager = $this->getMockBuilder('\OC\App\AppManager') + ->setConstructorArgs([$this->userSession, $this->appConfig, $this->groupManager, $this->cacheFactory]) + ->setMethods(['getAppInfo']) + ->getMock(); + + $appInfos = [ + 'test1' => ['id' => 'test1', 'version' => '1.0.1', 'requiremax' => '9.0.0'], + 'test2' => ['id' => 'test2', 'version' => '1.0.0', 'requiremin' => '8.2.0'], + 'test3' => ['id' => 'test3', 'version' => '1.2.4', 'requiremin' => '9.0.0'], + 'test4' => ['id' => 'test4', 'version' => '3.0.0', 'requiremin' => '8.1.0'], + 'testnoversion' => ['id' => 'testnoversion', 'requiremin' => '8.2.0'], + ]; + + $this->manager->expects($this->any()) + ->method('getAppInfo') + ->will($this->returnCallback( + function($appId) use ($appInfos) { + return $appInfos[$appId]; + } + )); + + $this->appConfig->setValue('test1', 'enabled', 'yes'); + $this->appConfig->setValue('test1', 'installed_version', '1.0.0'); + $this->appConfig->setValue('test2', 'enabled', 'yes'); + $this->appConfig->setValue('test2', 'installed_version', '1.0.0'); + $this->appConfig->setValue('test3', 'enabled', 'yes'); + $this->appConfig->setValue('test3', 'installed_version', '1.0.0'); + $this->appConfig->setValue('test4', 'enabled', 'yes'); + $this->appConfig->setValue('test4', 'installed_version', '2.4.0'); + + $apps = $this->manager->getAppsNeedingUpgrade('8.2.0'); + + $this->assertCount(2, $apps); + $this->assertEquals('test1', $apps[0]['id']); + $this->assertEquals('test4', $apps[1]['id']); + } + + public function testGetIncompatibleApps() { + $this->manager = $this->getMockBuilder('\OC\App\AppManager') + ->setConstructorArgs([$this->userSession, $this->appConfig, $this->groupManager, $this->cacheFactory]) + ->setMethods(['getAppInfo']) + ->getMock(); + + $appInfos = [ + 'test1' => ['id' => 'test1', 'version' => '1.0.1', 'requiremax' => '8.0.0'], + 'test2' => ['id' => 'test2', 'version' => '1.0.0', 'requiremin' => '8.2.0'], + 'test3' => ['id' => 'test3', 'version' => '1.2.4', 'requiremin' => '9.0.0'], + 'testnoversion' => ['id' => 'testnoversion', 'requiremin' => '8.2.0'], + ]; + + $this->manager->expects($this->any()) + ->method('getAppInfo') + ->will($this->returnCallback( + function($appId) use ($appInfos) { + return $appInfos[$appId]; + } + )); + + $this->appConfig->setValue('test1', 'enabled', 'yes'); + $this->appConfig->setValue('test2', 'enabled', 'yes'); + $this->appConfig->setValue('test3', 'enabled', 'yes'); + + $apps = $this->manager->getIncompatibleApps('8.2.0'); + + $this->assertCount(2, $apps); + $this->assertEquals('test1', $apps[0]['id']); + $this->assertEquals('test3', $apps[1]['id']); + } } diff --git a/tests/lib/appframework/controller/ApiControllerTest.php b/tests/lib/appframework/controller/ApiControllerTest.php index 137e5950f67..573fe7f3bad 100644 --- a/tests/lib/appframework/controller/ApiControllerTest.php +++ b/tests/lib/appframework/controller/ApiControllerTest.php @@ -38,6 +38,7 @@ class ApiControllerTest extends \Test\TestCase { $request = new Request( ['server' => ['HTTP_ORIGIN' => 'test']], $this->getMock('\OCP\Security\ISecureRandom'), + $this->getMock('\OCP\Security\ICrypto'), $this->getMock('\OCP\IConfig') ); $this->controller = new ChildApiController('app', $request, 'verbs', diff --git a/tests/lib/appframework/controller/ControllerTest.php b/tests/lib/appframework/controller/ControllerTest.php index 0d7716da411..243014a91a7 100644 --- a/tests/lib/appframework/controller/ControllerTest.php +++ b/tests/lib/appframework/controller/ControllerTest.php @@ -76,6 +76,7 @@ class ControllerTest extends \Test\TestCase { 'method' => 'hi', ], $this->getMock('\OCP\Security\ISecureRandom'), + $this->getMock('\OCP\Security\ICrypto'), $this->getMock('\OCP\IConfig') ); diff --git a/tests/lib/appframework/controller/OCSControllerTest.php b/tests/lib/appframework/controller/OCSControllerTest.php index 92b092cf0e9..292a56e3caa 100644 --- a/tests/lib/appframework/controller/OCSControllerTest.php +++ b/tests/lib/appframework/controller/OCSControllerTest.php @@ -43,6 +43,7 @@ class OCSControllerTest extends \Test\TestCase { ], ], $this->getMock('\OCP\Security\ISecureRandom'), + $this->getMock('\OCP\Security\ICrypto'), $this->getMock('\OCP\IConfig') ); $controller = new ChildOCSController('app', $request, 'verbs', @@ -64,6 +65,7 @@ class OCSControllerTest extends \Test\TestCase { $controller = new ChildOCSController('app', new Request( [], $this->getMock('\OCP\Security\ISecureRandom'), + $this->getMock('\OCP\Security\ICrypto'), $this->getMock('\OCP\IConfig') )); $expected = "<?xml version=\"1.0\"?>\n" . @@ -96,6 +98,7 @@ class OCSControllerTest extends \Test\TestCase { $controller = new ChildOCSController('app', new Request( [], $this->getMock('\OCP\Security\ISecureRandom'), + $this->getMock('\OCP\Security\ICrypto'), $this->getMock('\OCP\IConfig') )); $expected = "<?xml version=\"1.0\"?>\n" . @@ -128,6 +131,7 @@ class OCSControllerTest extends \Test\TestCase { $controller = new ChildOCSController('app', new Request( [], $this->getMock('\OCP\Security\ISecureRandom'), + $this->getMock('\OCP\Security\ICrypto'), $this->getMock('\OCP\IConfig') )); $expected = '{"ocs":{"meta":{"status":"failure","statuscode":400,"message":"OK",' . diff --git a/tests/lib/appframework/dependencyinjection/DIContainerTest.php b/tests/lib/appframework/dependencyinjection/DIContainerTest.php index 0cbdddbb205..598e70beffc 100644 --- a/tests/lib/appframework/dependencyinjection/DIContainerTest.php +++ b/tests/lib/appframework/dependencyinjection/DIContainerTest.php @@ -74,6 +74,7 @@ class DIContainerTest extends \Test\TestCase { $this->container['Request'] = new Request( ['method' => 'GET'], $this->getMock('\OCP\Security\ISecureRandom'), + $this->getMock('\OCP\Security\ICrypto'), $this->getMock('\OCP\IConfig') ); $security = $this->container['SecurityMiddleware']; diff --git a/tests/lib/appframework/http/DispatcherTest.php b/tests/lib/appframework/http/DispatcherTest.php index 02c86df8e72..c25fd7b6f85 100644 --- a/tests/lib/appframework/http/DispatcherTest.php +++ b/tests/lib/appframework/http/DispatcherTest.php @@ -295,6 +295,7 @@ class DispatcherTest extends \Test\TestCase { 'method' => 'POST' ], $this->getMock('\OCP\Security\ISecureRandom'), + $this->getMock('\OCP\Security\ICrypto'), $this->getMock('\OCP\IConfig') ); $this->dispatcher = new Dispatcher( @@ -322,6 +323,7 @@ class DispatcherTest extends \Test\TestCase { 'method' => 'POST', ], $this->getMock('\OCP\Security\ISecureRandom'), + $this->getMock('\OCP\Security\ICrypto'), $this->getMock('\OCP\IConfig') ); $this->dispatcher = new Dispatcher( @@ -352,6 +354,7 @@ class DispatcherTest extends \Test\TestCase { 'method' => 'GET' ], $this->getMock('\OCP\Security\ISecureRandom'), + $this->getMock('\OCP\Security\ICrypto'), $this->getMock('\OCP\IConfig') ); $this->dispatcher = new Dispatcher( @@ -381,6 +384,7 @@ class DispatcherTest extends \Test\TestCase { 'method' => 'GET' ], $this->getMock('\OCP\Security\ISecureRandom'), + $this->getMock('\OCP\Security\ICrypto'), $this->getMock('\OCP\IConfig') ); $this->dispatcher = new Dispatcher( @@ -411,6 +415,7 @@ class DispatcherTest extends \Test\TestCase { 'method' => 'PUT' ], $this->getMock('\OCP\Security\ISecureRandom'), + $this->getMock('\OCP\Security\ICrypto'), $this->getMock('\OCP\IConfig') ); $this->dispatcher = new Dispatcher( @@ -443,6 +448,7 @@ class DispatcherTest extends \Test\TestCase { 'method' => 'POST' ], $this->getMock('\OCP\Security\ISecureRandom'), + $this->getMock('\OCP\Security\ICrypto'), $this->getMock('\OCP\IConfig') ); $this->dispatcher = new Dispatcher( diff --git a/tests/lib/appframework/http/RequestTest.php b/tests/lib/appframework/http/RequestTest.php index 10a9e486c97..deb28909869 100644 --- a/tests/lib/appframework/http/RequestTest.php +++ b/tests/lib/appframework/http/RequestTest.php @@ -10,6 +10,7 @@ namespace OC\AppFramework\Http; +use OC\Security\Crypto; use OCP\Security\ISecureRandom; use OCP\IConfig; @@ -53,6 +54,7 @@ class RequestTest extends \Test\TestCase { $request = new Request( $vars, $this->secureRandom, + $this->getMock('\OCP\Security\ICrypto'), $this->config, $this->stream ); @@ -85,6 +87,7 @@ class RequestTest extends \Test\TestCase { $request = new Request( $vars, $this->secureRandom, + $this->getMock('\OCP\Security\ICrypto'), $this->config, $this->stream ); @@ -96,8 +99,8 @@ class RequestTest extends \Test\TestCase { /** - * @expectedException \RuntimeException - */ + * @expectedException \RuntimeException + */ public function testImmutableArrayAccess() { $vars = array( 'get' => array('name' => 'John Q. Public', 'nickname' => 'Joey'), @@ -107,6 +110,7 @@ class RequestTest extends \Test\TestCase { $request = new Request( $vars, $this->secureRandom, + $this->getMock('\OCP\Security\ICrypto'), $this->config, $this->stream ); @@ -115,8 +119,8 @@ class RequestTest extends \Test\TestCase { } /** - * @expectedException \RuntimeException - */ + * @expectedException \RuntimeException + */ public function testImmutableMagicAccess() { $vars = array( 'get' => array('name' => 'John Q. Public', 'nickname' => 'Joey'), @@ -126,6 +130,7 @@ class RequestTest extends \Test\TestCase { $request = new Request( $vars, $this->secureRandom, + $this->getMock('\OCP\Security\ICrypto'), $this->config, $this->stream ); @@ -134,8 +139,8 @@ class RequestTest extends \Test\TestCase { } /** - * @expectedException \LogicException - */ + * @expectedException \LogicException + */ public function testGetTheMethodRight() { $vars = array( 'get' => array('name' => 'John Q. Public', 'nickname' => 'Joey'), @@ -145,6 +150,7 @@ class RequestTest extends \Test\TestCase { $request = new Request( $vars, $this->secureRandom, + $this->getMock('\OCP\Security\ICrypto'), $this->config, $this->stream ); @@ -161,6 +167,7 @@ class RequestTest extends \Test\TestCase { $request = new Request( $vars, $this->secureRandom, + $this->getMock('\OCP\Security\ICrypto'), $this->config, $this->stream ); @@ -182,6 +189,7 @@ class RequestTest extends \Test\TestCase { $request = new Request( $vars, $this->secureRandom, + $this->getMock('\OCP\Security\ICrypto'), $this->config, $this->stream ); @@ -206,6 +214,7 @@ class RequestTest extends \Test\TestCase { $request = new Request( $vars, $this->secureRandom, + $this->getMock('\OCP\Security\ICrypto'), $this->config, $this->stream ); @@ -230,6 +239,7 @@ class RequestTest extends \Test\TestCase { $request = new Request( $vars, $this->secureRandom, + $this->getMock('\OCP\Security\ICrypto'), $this->config, $this->stream ); @@ -250,6 +260,7 @@ class RequestTest extends \Test\TestCase { $request = new Request( $vars, $this->secureRandom, + $this->getMock('\OCP\Security\ICrypto'), $this->config, $this->stream ); @@ -274,6 +285,7 @@ class RequestTest extends \Test\TestCase { $request = new Request( $vars, $this->secureRandom, + $this->getMock('\OCP\Security\ICrypto'), $this->config, $this->stream ); @@ -303,6 +315,7 @@ class RequestTest extends \Test\TestCase { $request = new Request( $vars, $this->secureRandom, + $this->getMock('\OCP\Security\ICrypto'), $this->config, $this->stream ); @@ -324,6 +337,7 @@ class RequestTest extends \Test\TestCase { $request = new Request( $vars, $this->secureRandom, + $this->getMock('\OCP\Security\ICrypto'), $this->config, $this->stream ); @@ -347,6 +361,7 @@ class RequestTest extends \Test\TestCase { $request = new Request( [], $this->secureRandom, + $this->getMock('\OCP\Security\ICrypto'), $this->config, $this->stream ); @@ -358,6 +373,7 @@ class RequestTest extends \Test\TestCase { $request = new Request( [], \OC::$server->getSecureRandom(), + $this->getMock('\OCP\Security\ICrypto'), $this->config, $this->stream ); @@ -382,6 +398,7 @@ class RequestTest extends \Test\TestCase { ], ], $this->secureRandom, + $this->getMock('\OCP\Security\ICrypto'), $this->config, $this->stream ); @@ -410,6 +427,7 @@ class RequestTest extends \Test\TestCase { ], ], $this->secureRandom, + $this->getMock('\OCP\Security\ICrypto'), $this->config, $this->stream ); @@ -438,6 +456,7 @@ class RequestTest extends \Test\TestCase { ], ], $this->secureRandom, + $this->getMock('\OCP\Security\ICrypto'), $this->config, $this->stream ); @@ -470,6 +489,7 @@ class RequestTest extends \Test\TestCase { ], ], $this->secureRandom, + $this->getMock('\OCP\Security\ICrypto'), $this->config, $this->stream ); @@ -497,6 +517,7 @@ class RequestTest extends \Test\TestCase { $request = new Request( [], $this->secureRandom, + $this->getMock('\OCP\Security\ICrypto'), $this->config, $this->stream ); @@ -506,10 +527,10 @@ class RequestTest extends \Test\TestCase { public function testGetServerProtocolWithProtoValid() { $this->config - ->expects($this->exactly(2)) - ->method('getSystemValue') - ->with('overwriteprotocol') - ->will($this->returnValue('')); + ->expects($this->exactly(2)) + ->method('getSystemValue') + ->with('overwriteprotocol') + ->will($this->returnValue('')); $requestHttps = new Request( [ @@ -518,6 +539,7 @@ class RequestTest extends \Test\TestCase { ], ], $this->secureRandom, + $this->getMock('\OCP\Security\ICrypto'), $this->config, $this->stream ); @@ -528,6 +550,7 @@ class RequestTest extends \Test\TestCase { ], ], $this->secureRandom, + $this->getMock('\OCP\Security\ICrypto'), $this->config, $this->stream ); @@ -551,6 +574,7 @@ class RequestTest extends \Test\TestCase { ], ], $this->secureRandom, + $this->getMock('\OCP\Security\ICrypto'), $this->config, $this->stream ); @@ -571,6 +595,7 @@ class RequestTest extends \Test\TestCase { ], ], $this->secureRandom, + $this->getMock('\OCP\Security\ICrypto'), $this->config, $this->stream ); @@ -587,6 +612,7 @@ class RequestTest extends \Test\TestCase { $request = new Request( [], $this->secureRandom, + $this->getMock('\OCP\Security\ICrypto'), $this->config, $this->stream ); @@ -607,6 +633,7 @@ class RequestTest extends \Test\TestCase { ], ], $this->secureRandom, + $this->getMock('\OCP\Security\ICrypto'), $this->config, $this->stream ); @@ -628,6 +655,7 @@ class RequestTest extends \Test\TestCase { ] ], $this->secureRandom, + $this->getMock('\OCP\Security\ICrypto'), $this->config, $this->stream ); @@ -716,6 +744,7 @@ class RequestTest extends \Test\TestCase { ] ], $this->secureRandom, + $this->getMock('\OCP\Security\ICrypto'), $this->config, $this->stream ); @@ -732,6 +761,7 @@ class RequestTest extends \Test\TestCase { ] ], $this->secureRandom, + $this->getMock('\OCP\Security\ICrypto'), $this->config, $this->stream ); @@ -749,6 +779,7 @@ class RequestTest extends \Test\TestCase { ] ], $this->secureRandom, + $this->getMock('\OCP\Security\ICrypto'), $this->config, $this->stream ); @@ -766,6 +797,7 @@ class RequestTest extends \Test\TestCase { ] ], $this->secureRandom, + $this->getMock('\OCP\Security\ICrypto'), $this->config, $this->stream ); @@ -793,6 +825,7 @@ class RequestTest extends \Test\TestCase { $request = new Request( [], $this->secureRandom, + $this->getMock('\OCP\Security\ICrypto'), $this->config, $this->stream ); @@ -814,6 +847,7 @@ class RequestTest extends \Test\TestCase { ], ], $this->secureRandom, + $this->getMock('\OCP\Security\ICrypto'), $this->config, $this->stream ); @@ -840,6 +874,7 @@ class RequestTest extends \Test\TestCase { ], ], $this->secureRandom, + $this->getMock('\OCP\Security\ICrypto'), $this->config, $this->stream ); @@ -866,6 +901,7 @@ class RequestTest extends \Test\TestCase { ], ], $this->secureRandom, + $this->getMock('\OCP\Security\ICrypto'), $this->config, $this->stream ); @@ -882,6 +918,7 @@ class RequestTest extends \Test\TestCase { $request = new Request( [], $this->secureRandom, + $this->getMock('\OCP\Security\ICrypto'), $this->config, $this->stream ); @@ -909,6 +946,7 @@ class RequestTest extends \Test\TestCase { $request = new Request( [], $this->secureRandom, + $this->getMock('\OCP\Security\ICrypto'), $this->config, $this->stream ); @@ -924,6 +962,7 @@ class RequestTest extends \Test\TestCase { ] ], $this->secureRandom, + $this->getMock('\OCP\Security\ICrypto'), $this->config, $this->stream ); @@ -944,6 +983,7 @@ class RequestTest extends \Test\TestCase { ] ], $this->secureRandom, + $this->getMock('\OCP\Security\ICrypto'), $this->config, $this->stream ); @@ -964,6 +1004,7 @@ class RequestTest extends \Test\TestCase { ] ], $this->secureRandom, + $this->getMock('\OCP\Security\ICrypto'), $this->config, $this->stream ); @@ -986,6 +1027,7 @@ class RequestTest extends \Test\TestCase { ] ], $this->secureRandom, + $this->getMock('\OCP\Security\ICrypto'), $this->config, $this->stream ); @@ -1008,6 +1050,7 @@ class RequestTest extends \Test\TestCase { ] ], $this->secureRandom, + $this->getMock('\OCP\Security\ICrypto'), $this->config, $this->stream ); @@ -1030,6 +1073,7 @@ class RequestTest extends \Test\TestCase { ] ], $this->secureRandom, + $this->getMock('\OCP\Security\ICrypto'), $this->config, $this->stream ); @@ -1052,6 +1096,7 @@ class RequestTest extends \Test\TestCase { ] ], $this->secureRandom, + $this->getMock('\OCP\Security\ICrypto'), $this->config, $this->stream ); @@ -1105,6 +1150,7 @@ class RequestTest extends \Test\TestCase { ] ], $this->secureRandom, + $this->getMock('\OCP\Security\ICrypto'), $this->config, $this->stream ); @@ -1144,6 +1190,7 @@ class RequestTest extends \Test\TestCase { ] ], $this->secureRandom, + $this->getMock('\OCP\Security\ICrypto'), $this->config, $this->stream ]) @@ -1157,17 +1204,25 @@ class RequestTest extends \Test\TestCase { } public function testPassesCSRFCheckWithGet() { + $crypto = $this->getMock('\OCP\Security\ICrypto'); + $crypto + ->expects($this->once()) + ->method('decrypt') + ->with('1c637c4147e40a8a8f09428ec2059cebea3480c27b402b4e793c69710a731513|wlXxNUaFqHuQnZr5|e6ab49c9e0e20c8d3607e02f1d8e6ec17ad6020ae10b7d64ab4b0a6318c0875940943a6aa303dc090fea0b4cd5b9fb8bcbecac4308a2bd15d9f369cdc22121a4', 'secret') + ->will($this->returnValue('MyStoredRequestToken')); + /** @var Request $request */ $request = $this->getMockBuilder('\OC\AppFramework\Http\Request') ->setMethods(['getScriptName']) ->setConstructorArgs([ [ 'get' => [ - 'requesttoken' => 'MyStoredRequestToken', + 'requesttoken' => '1c637c4147e40a8a8f09428ec2059cebea3480c27b402b4e793c69710a731513|wlXxNUaFqHuQnZr5|e6ab49c9e0e20c8d3607e02f1d8e6ec17ad6020ae10b7d64ab4b0a6318c0875940943a6aa303dc090fea0b4cd5b9fb8bcbecac4308a2bd15d9f369cdc22121a4:secret', ], 'requesttoken' => 'MyStoredRequestToken', ], $this->secureRandom, + $crypto, $this->config, $this->stream ]) @@ -1177,17 +1232,25 @@ class RequestTest extends \Test\TestCase { } public function testPassesCSRFCheckWithPost() { + $crypto = $this->getMock('\OCP\Security\ICrypto'); + $crypto + ->expects($this->once()) + ->method('decrypt') + ->with('1c637c4147e40a8a8f09428ec2059cebea3480c27b402b4e793c69710a731513|wlXxNUaFqHuQnZr5|e6ab49c9e0e20c8d3607e02f1d8e6ec17ad6020ae10b7d64ab4b0a6318c0875940943a6aa303dc090fea0b4cd5b9fb8bcbecac4308a2bd15d9f369cdc22121a4', 'secret') + ->will($this->returnValue('MyStoredRequestToken')); + /** @var Request $request */ $request = $this->getMockBuilder('\OC\AppFramework\Http\Request') ->setMethods(['getScriptName']) ->setConstructorArgs([ [ 'post' => [ - 'requesttoken' => 'MyStoredRequestToken', + 'requesttoken' => '1c637c4147e40a8a8f09428ec2059cebea3480c27b402b4e793c69710a731513|wlXxNUaFqHuQnZr5|e6ab49c9e0e20c8d3607e02f1d8e6ec17ad6020ae10b7d64ab4b0a6318c0875940943a6aa303dc090fea0b4cd5b9fb8bcbecac4308a2bd15d9f369cdc22121a4:secret', ], 'requesttoken' => 'MyStoredRequestToken', ], $this->secureRandom, + $crypto, $this->config, $this->stream ]) @@ -1197,17 +1260,24 @@ class RequestTest extends \Test\TestCase { } public function testPassesCSRFCheckWithHeader() { + $crypto = $this->getMock('\OCP\Security\ICrypto'); + $crypto + ->expects($this->once()) + ->method('decrypt') + ->with('1c637c4147e40a8a8f09428ec2059cebea3480c27b402b4e793c69710a731513|wlXxNUaFqHuQnZr5|e6ab49c9e0e20c8d3607e02f1d8e6ec17ad6020ae10b7d64ab4b0a6318c0875940943a6aa303dc090fea0b4cd5b9fb8bcbecac4308a2bd15d9f369cdc22121a4', 'secret') + ->will($this->returnValue('MyStoredRequestToken')); /** @var Request $request */ $request = $this->getMockBuilder('\OC\AppFramework\Http\Request') ->setMethods(['getScriptName']) ->setConstructorArgs([ [ 'server' => [ - 'HTTP_REQUESTTOKEN' => 'MyStoredRequestToken', + 'HTTP_REQUESTTOKEN' => '1c637c4147e40a8a8f09428ec2059cebea3480c27b402b4e793c69710a731513|wlXxNUaFqHuQnZr5|e6ab49c9e0e20c8d3607e02f1d8e6ec17ad6020ae10b7d64ab4b0a6318c0875940943a6aa303dc090fea0b4cd5b9fb8bcbecac4308a2bd15d9f369cdc22121a4:secret', ], 'requesttoken' => 'MyStoredRequestToken', ], $this->secureRandom, + $crypto, $this->config, $this->stream ]) @@ -1216,18 +1286,34 @@ class RequestTest extends \Test\TestCase { $this->assertTrue($request->passesCSRFCheck()); } - public function testPassesCSRFCheckWithInvalidToken() { + public function invalidTokenDataProvider() { + return [ + ['InvalidSentToken'], + ['InvalidSentToken:InvalidSecret'], + [null], + [''], + ]; + } + + /** + * @dataProvider invalidTokenDataProvider + * @param string $invalidToken + */ + public function testPassesCSRFCheckWithInvalidToken($invalidToken) { + $crypto = new Crypto($this->config, $this->secureRandom); + /** @var Request $request */ $request = $this->getMockBuilder('\OC\AppFramework\Http\Request') ->setMethods(['getScriptName']) ->setConstructorArgs([ [ 'server' => [ - 'HTTP_REQUESTTOKEN' => 'MyInvalidSentToken', + 'HTTP_REQUESTTOKEN' => $invalidToken, ], 'requesttoken' => 'MyStoredRequestToken', ], $this->secureRandom, + $crypto, $this->config, $this->stream ]) @@ -1243,6 +1329,7 @@ class RequestTest extends \Test\TestCase { ->setConstructorArgs([ [], $this->secureRandom, + $this->getMock('\OCP\Security\ICrypto'), $this->config, $this->stream ]) diff --git a/tests/lib/appframework/middleware/MiddlewareDispatcherTest.php b/tests/lib/appframework/middleware/MiddlewareDispatcherTest.php index a8731525798..eab45b03c98 100644 --- a/tests/lib/appframework/middleware/MiddlewareDispatcherTest.php +++ b/tests/lib/appframework/middleware/MiddlewareDispatcherTest.php @@ -133,6 +133,7 @@ class MiddlewareDispatcherTest extends \Test\TestCase { new Request( ['method' => 'GET'], $this->getMock('\OCP\Security\ISecureRandom'), + $this->getMock('\OCP\Security\ICrypto'), $this->getMock('\OCP\IConfig') ) ] diff --git a/tests/lib/appframework/middleware/MiddlewareTest.php b/tests/lib/appframework/middleware/MiddlewareTest.php index 33f04e1383d..8e077b37e2f 100644 --- a/tests/lib/appframework/middleware/MiddlewareTest.php +++ b/tests/lib/appframework/middleware/MiddlewareTest.php @@ -61,6 +61,7 @@ class MiddlewareTest extends \Test\TestCase { new Request( [], $this->getMock('\OCP\Security\ISecureRandom'), + $this->getMock('\OCP\Security\ICrypto'), $this->getMock('\OCP\IConfig') ) ] diff --git a/tests/lib/appframework/middleware/security/CORSMiddlewareTest.php b/tests/lib/appframework/middleware/security/CORSMiddlewareTest.php index ca526fb859c..f5e6106dc3a 100644 --- a/tests/lib/appframework/middleware/security/CORSMiddlewareTest.php +++ b/tests/lib/appframework/middleware/security/CORSMiddlewareTest.php @@ -42,6 +42,7 @@ class CORSMiddlewareTest extends \Test\TestCase { ] ], $this->getMock('\OCP\Security\ISecureRandom'), + $this->getMock('\OCP\Security\ICrypto'), $this->getMock('\OCP\IConfig') ); $this->reflector->reflect($this, __FUNCTION__); @@ -61,6 +62,7 @@ class CORSMiddlewareTest extends \Test\TestCase { ] ], $this->getMock('\OCP\Security\ISecureRandom'), + $this->getMock('\OCP\Security\ICrypto'), $this->getMock('\OCP\IConfig') ); $middleware = new CORSMiddleware($request, $this->reflector, $this->session); @@ -78,6 +80,7 @@ class CORSMiddlewareTest extends \Test\TestCase { $request = new Request( [], $this->getMock('\OCP\Security\ISecureRandom'), + $this->getMock('\OCP\Security\ICrypto'), $this->getMock('\OCP\IConfig') ); $this->reflector->reflect($this, __FUNCTION__); @@ -101,6 +104,7 @@ class CORSMiddlewareTest extends \Test\TestCase { ] ], $this->getMock('\OCP\Security\ISecureRandom'), + $this->getMock('\OCP\Security\ICrypto'), $this->getMock('\OCP\IConfig') ); $this->reflector->reflect($this, __FUNCTION__); @@ -119,6 +123,7 @@ class CORSMiddlewareTest extends \Test\TestCase { $request = new Request( [], $this->getMock('\OCP\Security\ISecureRandom'), + $this->getMock('\OCP\Security\ICrypto'), $this->getMock('\OCP\IConfig') ); $this->reflector->reflect($this, __FUNCTION__); @@ -144,6 +149,7 @@ class CORSMiddlewareTest extends \Test\TestCase { 'PHP_AUTH_PW' => 'pass' ]], $this->getMock('\OCP\Security\ISecureRandom'), + $this->getMock('\OCP\Security\ICrypto'), $this->getMock('\OCP\IConfig') ); $this->session->expects($this->once()) @@ -169,6 +175,7 @@ class CORSMiddlewareTest extends \Test\TestCase { 'PHP_AUTH_PW' => 'pass' ]], $this->getMock('\OCP\Security\ISecureRandom'), + $this->getMock('\OCP\Security\ICrypto'), $this->getMock('\OCP\IConfig') ); $this->session->expects($this->once()) @@ -190,6 +197,7 @@ class CORSMiddlewareTest extends \Test\TestCase { 'PHP_AUTH_PW' => 'pass' ]], $this->getMock('\OCP\Security\ISecureRandom'), + $this->getMock('\OCP\Security\ICrypto'), $this->getMock('\OCP\IConfig') ); $middleware = new CORSMiddleware($request, $this->reflector, $this->session); @@ -206,6 +214,7 @@ class CORSMiddlewareTest extends \Test\TestCase { 'PHP_AUTH_PW' => 'pass' ]], $this->getMock('\OCP\Security\ISecureRandom'), + $this->getMock('\OCP\Security\ICrypto'), $this->getMock('\OCP\IConfig') ); $middleware = new CORSMiddleware($request, $this->reflector, $this->session); @@ -226,6 +235,7 @@ class CORSMiddlewareTest extends \Test\TestCase { 'PHP_AUTH_PW' => 'pass' ]], $this->getMock('\OCP\Security\ISecureRandom'), + $this->getMock('\OCP\Security\ICrypto'), $this->getMock('\OCP\IConfig') ); $middleware = new CORSMiddleware($request, $this->reflector, $this->session); diff --git a/tests/lib/appframework/middleware/security/SecurityMiddlewareTest.php b/tests/lib/appframework/middleware/security/SecurityMiddlewareTest.php index 347a0423ea6..3b4d7987e94 100644 --- a/tests/lib/appframework/middleware/security/SecurityMiddlewareTest.php +++ b/tests/lib/appframework/middleware/security/SecurityMiddlewareTest.php @@ -322,6 +322,7 @@ class SecurityMiddlewareTest extends \Test\TestCase { ] ], $this->getMock('\OCP\Security\ISecureRandom'), + $this->getMock('\OCP\Security\ICrypto'), $this->getMock('\OCP\IConfig') ); $this->middleware = $this->getMiddleware(true, true); diff --git a/tests/lib/appframework/middleware/sessionmiddlewaretest.php b/tests/lib/appframework/middleware/sessionmiddlewaretest.php index 11c1600f515..06390e96d4c 100644 --- a/tests/lib/appframework/middleware/sessionmiddlewaretest.php +++ b/tests/lib/appframework/middleware/sessionmiddlewaretest.php @@ -36,6 +36,7 @@ class SessionMiddlewareTest extends \Test\TestCase { $this->request = new Request( [], $this->getMockBuilder('\OCP\Security\ISecureRandom')->getMock(), + $this->getMock('\OCP\Security\ICrypto'), $this->getMock('\OCP\IConfig') ); $this->reflector = new ControllerMethodReflector(); diff --git a/tests/lib/backgroundjob/joblist.php b/tests/lib/backgroundjob/joblist.php index 13bee12479e..d5136676a47 100644 --- a/tests/lib/backgroundjob/joblist.php +++ b/tests/lib/backgroundjob/joblist.php @@ -202,4 +202,30 @@ class JobList extends \Test\TestCase { $this->instance->remove($job); } + + public function testGetNextNonExisting() { + $job = new TestJob(); + $this->instance->add($job, 1); + $this->instance->add('\OC\Non\Existing\Class'); + $this->instance->add($job, 2); + + $jobs = $this->instance->getAll(); + + $savedJob1 = $jobs[count($jobs) - 2]; + $savedJob2 = $jobs[count($jobs) - 1]; + + $this->config->expects($this->any()) + ->method('getAppValue') + ->with('backgroundjob', 'lastjob', 0) + ->will($this->returnValue($savedJob1->getId())); + + $this->instance->getNext(); + + $nextJob = $this->instance->getNext(); + + $this->assertEquals($savedJob2, $nextJob); + + $this->instance->remove($job, 1); + $this->instance->remove($job, 2); + } } diff --git a/tests/lib/connector/sabre/requesttest/requesttest.php b/tests/lib/connector/sabre/requesttest/requesttest.php index c7739aefcd7..7f33dcf817b 100644 --- a/tests/lib/connector/sabre/requesttest/requesttest.php +++ b/tests/lib/connector/sabre/requesttest/requesttest.php @@ -11,22 +11,18 @@ namespace Test\Connector\Sabre\RequestTest; use OC\Connector\Sabre\Server; use OC\Connector\Sabre\ServerFactory; use OC\Files\Mount\MountPoint; +use OC\Files\Storage\StorageFactory; use OC\Files\Storage\Temporary; use OC\Files\View; use OCP\IUser; use Sabre\HTTP\Request; use Test\TestCase; +use Test\Traits\MountProviderTrait; +use Test\Traits\UserTrait; abstract class RequestTest extends TestCase { - /** - * @var \OC_User_Dummy - */ - protected $userBackend; - - /** - * @var \OCP\Files\Config\IMountProvider[] - */ - protected $mountProviders; + use UserTrait; + use MountProviderTrait; /** * @var \OC\Connector\Sabre\ServerFactory @@ -40,33 +36,8 @@ abstract class RequestTest extends TestCase { return $stream; } - /** - * @param $userId - * @param $storages - * @return \OCP\Files\Config\IMountProvider - */ - protected function getMountProvider($userId, $storages) { - $mounts = []; - foreach ($storages as $mountPoint => $storage) { - $mounts[] = new MountPoint($storage, $mountPoint); - } - $provider = $this->getMock('\OCP\Files\Config\IMountProvider'); - $provider->expects($this->any()) - ->method('getMountsForUser') - ->will($this->returnCallback(function (IUser $user) use ($userId, $mounts) { - if ($user->getUID() === $userId) { - return $mounts; - } else { - return []; - } - })); - return $provider; - } - protected function setUp() { parent::setUp(); - $this->userBackend = new \OC_User_Dummy(); - \OC::$server->getUserManager()->registerBackend($this->userBackend); $this->serverFactory = new ServerFactory( \OC::$server->getConfig(), @@ -78,16 +49,9 @@ abstract class RequestTest extends TestCase { ); } - protected function tearDown() { - parent::tearDown(); - \OC::$server->getUserManager()->removeBackend($this->userBackend); - } - protected function setupUser($name, $password) { - $this->userBackend->createUser($name, $password); - \OC::$server->getMountProviderCollection()->registerProvider($this->getMountProvider($name, [ - '/' . $name => new Temporary() - ])); + $this->createUser($name, $password); + $this->registerMount($name, '\OC\Files\Storage\Temporary', '/' . $name); $this->loginAsUser($name); return new View('/' . $name . '/files'); } diff --git a/tests/lib/server.php b/tests/lib/server.php index 9c5c83ceb5c..bc44c50a22a 100644 --- a/tests/lib/server.php +++ b/tests/lib/server.php @@ -56,6 +56,7 @@ class Server extends \Test\TestCase { ['ContactsManager', '\OCP\Contacts\IManager'], ['Crypto', '\OC\Security\Crypto'], ['Crypto', '\OCP\Security\ICrypto'], + ['CryptoWrapper', '\OC\Session\CryptoWrapper'], ['DatabaseConnection', '\OC\DB\Connection'], ['DatabaseConnection', '\OCP\IDBConnection'], diff --git a/tests/lib/session/cryptosessiondatatest.php b/tests/lib/session/cryptosessiondatatest.php new file mode 100644 index 00000000000..ee6bcbf11c1 --- /dev/null +++ b/tests/lib/session/cryptosessiondatatest.php @@ -0,0 +1,53 @@ +<?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 Test\Session; + +use OC\Session\CryptoSessionData; + +class CryptoSessionDataTest extends Session { + /** @var \PHPUnit_Framework_MockObject_MockObject|\OCP\Security\ICrypto */ + protected $crypto; + + /** @var \OCP\ISession */ + protected $wrappedSession; + + protected function setUp() { + parent::setUp(); + + $this->wrappedSession = new \OC\Session\Memory($this->getUniqueID()); + $this->crypto = $this->getMockBuilder('OCP\Security\ICrypto') + ->disableOriginalConstructor() + ->getMock(); + $this->crypto->expects($this->any()) + ->method('encrypt') + ->willReturnCallback(function ($input) { + return '#' . $input . '#'; + }); + $this->crypto->expects($this->any()) + ->method('decrypt') + ->willReturnCallback(function ($input) { + return substr($input, 1, -1); + }); + + $this->instance = new CryptoSessionData($this->wrappedSession, $this->crypto, 'PASS'); + } +} diff --git a/tests/lib/session/cryptowrappingtest.php b/tests/lib/session/cryptowrappingtest.php new file mode 100644 index 00000000000..12b3c905b7f --- /dev/null +++ b/tests/lib/session/cryptowrappingtest.php @@ -0,0 +1,82 @@ +<?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 Test\Session; + +use OC\Session\CryptoSessionData; +use Test\TestCase; + +class CryptoWrappingTest extends TestCase { + /** @var \PHPUnit_Framework_MockObject_MockObject|\OCP\Security\ICrypto */ + protected $crypto; + + /** @var \PHPUnit_Framework_MockObject_MockObject|\OCP\ISession */ + protected $wrappedSession; + + /** @var \OC\Session\CryptoSessionData */ + protected $instance; + + protected function setUp() { + parent::setUp(); + + $this->wrappedSession = $this->getMockBuilder('OCP\ISession') + ->disableOriginalConstructor() + ->getMock(); + $this->crypto = $this->getMockBuilder('OCP\Security\ICrypto') + ->disableOriginalConstructor() + ->getMock(); + $this->crypto->expects($this->any()) + ->method('encrypt') + ->willReturnCallback(function ($input) { + return $input; + }); + $this->crypto->expects($this->any()) + ->method('decrypt') + ->willReturnCallback(function ($input) { + return substr($input, 1, -1); + }); + + $this->instance = new CryptoSessionData($this->wrappedSession, $this->crypto, 'PASS'); + } + + public function testWrappingSet() { + $unencryptedValue = 'foobar'; + + $this->wrappedSession->expects($this->once()) + ->method('set') + ->with('key', $this->crypto->encrypt(json_encode($unencryptedValue))); + $this->instance->set('key', $unencryptedValue); + } + + public function testUnwrappingGet() { + $unencryptedValue = 'foobar'; + $encryptedValue = $this->crypto->encrypt($unencryptedValue); + + $this->wrappedSession->expects($this->once()) + ->method('get') + ->with('key') + ->willReturnCallback(function () use ($encryptedValue) { + return $encryptedValue; + }); + + $this->assertSame($unencryptedValue, $this->wrappedSession->get('key')); + } +} diff --git a/tests/lib/testcase.php b/tests/lib/testcase.php index fd0b8d5f2de..854d5f4f65b 100644 --- a/tests/lib/testcase.php +++ b/tests/lib/testcase.php @@ -32,21 +32,52 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase { */ private $commandBus; + protected function getTestTraits() { + $traits = []; + $class = $this; + do { + $traits = array_merge(class_uses($class), $traits); + } while ($class = get_parent_class($class)); + foreach ($traits as $trait => $same) { + $traits = array_merge(class_uses($trait), $traits); + } + $traits = array_unique($traits); + return array_filter($traits, function ($trait) { + return substr($trait, 0, 5) === 'Test\\'; + }); + } + protected function setUp() { // overwrite the command bus with one we can run ourselves $this->commandBus = new QueueBus(); \OC::$server->registerService('AsyncCommandBus', function () { return $this->commandBus; }); + + $traits = $this->getTestTraits(); + foreach ($traits as $trait) { + $methodName = 'setUp' . basename(str_replace('\\', '/', $trait)); + if (method_exists($this, $methodName)) { + call_user_func([$this, $methodName]); + } + } } protected function tearDown() { $hookExceptions = \OC_Hook::$thrownExceptions; \OC_Hook::$thrownExceptions = []; \OC::$server->getLockingProvider()->releaseAll(); - if(!empty($hookExceptions)) { + if (!empty($hookExceptions)) { throw $hookExceptions[0]; } + + $traits = $this->getTestTraits(); + foreach ($traits as $trait) { + $methodName = 'tearDown' . basename(str_replace('\\', '/', $trait)); + if (method_exists($this, $methodName)) { + call_user_func([$this, $methodName]); + } + } } /** diff --git a/tests/lib/traits/mountprovidertrait.php b/tests/lib/traits/mountprovidertrait.php new file mode 100644 index 00000000000..66eca1597c8 --- /dev/null +++ b/tests/lib/traits/mountprovidertrait.php @@ -0,0 +1,56 @@ +<?php +/** + * Copyright (c) 2015 Robin Appelman <icewind@owncloud.com> + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace Test\Traits; + +use OC\Files\Mount\MountPoint; +use OC\Files\Storage\StorageFactory; +use OCP\IUser; + +/** + * Allow setting mounts for users + */ +trait MountProviderTrait { + /** + * @var \OCP\Files\Config\IMountProvider + */ + protected $mountProvider; + + /** + * @var \OC\Files\Storage\StorageFactory + */ + protected $storageFactory; + + protected $mounts = []; + + protected function registerMount($userId, $storage, $mountPoint, $arguments = null) { + if (!isset($this->mounts[$userId])) { + $this->mounts[$userId] = []; + } + $this->mounts[$userId][] = new MountPoint($storage, $mountPoint, $arguments, $this->storageFactory); + } + + protected function registerStorageWrapper($name, $wrapper) { + $this->storageFactory->addStorageWrapper($name, $wrapper); + } + + protected function setUpMountProviderTrait() { + $this->storageFactory = new StorageFactory(); + $this->mountProvider = $this->getMock('\OCP\Files\Config\IMountProvider'); + $this->mountProvider->expects($this->any()) + ->method('getMountsForUser') + ->will($this->returnCallback(function (IUser $user) { + if (isset($this->mounts[$user->getUID()])) { + return $this->mounts[$user->getUID()]; + } else { + return []; + } + })); + \OC::$server->getMountProviderCollection()->registerProvider($this->mountProvider); + } +} diff --git a/tests/lib/traits/usertrait.php b/tests/lib/traits/usertrait.php new file mode 100644 index 00000000000..401d8b8a832 --- /dev/null +++ b/tests/lib/traits/usertrait.php @@ -0,0 +1,32 @@ +<?php +/** + * Copyright (c) 2015 Robin Appelman <icewind@owncloud.com> + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace Test\Traits; + +/** + * Allow creating users in a temporary backend + */ +trait UserTrait { + /** + * @var \OC_User_Dummy|\OCP\UserInterface + */ + protected $userBackend; + + protected function createUser($name, $password) { + $this->userBackend->createUser($name, $password); + } + + protected function setUpUserTrait() { + $this->userBackend = new \OC_User_Dummy(); + \OC::$server->getUserManager()->registerBackend($this->userBackend); + } + + protected function tearDownUserTrait() { + \OC::$server->getUserManager()->removeBackend($this->userBackend); + } +} diff --git a/tests/lib/util.php b/tests/lib/util.php index e52a9fcc618..b9b8062653e 100644 --- a/tests/lib/util.php +++ b/tests/lib/util.php @@ -91,7 +91,7 @@ class Test_Util extends \Test\TestCase { function testCallRegister() { $result = strlen(OC_Util::callRegister()); - $this->assertEquals(30, $result); + $this->assertEquals(221, $result); } function testSanitizeHTML() { |