diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/karma.config.js | 12 | ||||
-rw-r--r-- | tests/lib/app.php | 7 | ||||
-rw-r--r-- | tests/lib/app/manager.php | 195 | ||||
-rw-r--r-- | tests/lib/appframework/http/JSONResponseTest.php | 2 | ||||
-rw-r--r-- | tests/lib/appframework/middleware/security/SecurityMiddlewareTest.php | 4 | ||||
-rw-r--r-- | tests/lib/files/storage/storage.php | 6 | ||||
-rw-r--r-- | tests/lib/files/view.php | 31 | ||||
-rw-r--r-- | tests/lib/security/hasher.php | 115 | ||||
-rw-r--r-- | tests/lib/template/resourcelocator.php | 16 | ||||
-rw-r--r-- | tests/lib/user/manager.php | 13 | ||||
-rw-r--r-- | tests/settings/controller/mailsettingscontrollertest.php | 6 | ||||
-rw-r--r-- | tests/settings/controller/securitysettingscontrollertest.php | 138 |
12 files changed, 517 insertions, 28 deletions
diff --git a/tests/karma.config.js b/tests/karma.config.js index f67fa9977c2..414f1552584 100644 --- a/tests/karma.config.js +++ b/tests/karma.config.js @@ -120,6 +120,12 @@ module.exports = function(config) { files.push(corePath + 'tests/specHelper.js'); var srcFile, i; + // add vendor library files + for ( i = 0; i < coreModule.vendor.length; i++ ) { + srcFile = vendorPath + coreModule.vendor[i]; + files.push(srcFile); + } + // add core library files for ( i = 0; i < coreModule.libraries.length; i++ ) { srcFile = corePath + coreModule.libraries[i]; @@ -135,12 +141,6 @@ module.exports = function(config) { } } - // add vendor library files - for ( i = 0; i < coreModule.vendor.length; i++ ) { - srcFile = vendorPath + coreModule.vendor[i]; - files.push(srcFile); - } - // TODO: settings pages // need to test the core app as well ? diff --git a/tests/lib/app.php b/tests/lib/app.php index e8289f58a26..6ae548759ed 100644 --- a/tests/lib/app.php +++ b/tests/lib/app.php @@ -360,10 +360,7 @@ class Test_App extends PHPUnit_Framework_TestCase { $user1->delete(); $user2->delete(); $user3->delete(); - // clear user cache... - $userManager->delete(self::TEST_USER1); - $userManager->delete(self::TEST_USER2); - $userManager->delete(self::TEST_USER3); + $group1->delete(); $group2->delete(); } @@ -399,8 +396,6 @@ class Test_App extends PHPUnit_Framework_TestCase { \OC_User::setUserId(null); $user1->delete(); - // clear user cache... - $userManager->delete(self::TEST_USER1); } /** diff --git a/tests/lib/app/manager.php b/tests/lib/app/manager.php new file mode 100644 index 00000000000..4c0555b501f --- /dev/null +++ b/tests/lib/app/manager.php @@ -0,0 +1,195 @@ +<?php + +/** + * Copyright (c) 2014 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\App; + +use OC\Group\Group; +use OC\User\User; + +class Manager extends \PHPUnit_Framework_TestCase { + /** + * @return \OCP\IAppConfig | \PHPUnit_Framework_MockObject_MockObject + */ + protected function getAppConfig() { + $appConfig = array(); + $config = $this->getMockBuilder('\OCP\IAppConfig') + ->disableOriginalConstructor() + ->getMock(); + + $config->expects($this->any()) + ->method('getValue') + ->will($this->returnCallback(function ($app, $key, $default) use (&$appConfig) { + return (isset($appConfig[$app]) and isset($appConfig[$app][$key])) ? $appConfig[$app][$key] : $default; + })); + $config->expects($this->any()) + ->method('setValue') + ->will($this->returnCallback(function ($app, $key, $value) use (&$appConfig) { + if (!isset($appConfig[$app])) { + $appConfig[$app] = array(); + } + $appConfig[$app][$key] = $value; + })); + $config->expects($this->any()) + ->method('getValues') + ->will($this->returnCallback(function ($app, $key) use (&$appConfig) { + if ($app) { + return $appConfig[$app]; + } else { + $values = array(); + foreach ($appConfig as $app => $appData) { + if (isset($appData[$key])) { + $values[$app] = $appData[$key]; + } + } + return $values; + } + })); + + return $config; + } + + public function testEnableApp() { + $userSession = $this->getMock('\OCP\IUserSession'); + $groupManager = $this->getMock('\OCP\IGroupManager'); + $appConfig = $this->getAppConfig(); + $manager = new \OC\App\AppManager($userSession, $appConfig, $groupManager); + $manager->enableApp('test'); + $this->assertEquals('yes', $appConfig->getValue('test', 'enabled', 'no')); + } + + public function testDisableApp() { + $userSession = $this->getMock('\OCP\IUserSession'); + $groupManager = $this->getMock('\OCP\IGroupManager'); + $appConfig = $this->getAppConfig(); + $manager = new \OC\App\AppManager($userSession, $appConfig, $groupManager); + $manager->disableApp('test'); + $this->assertEquals('no', $appConfig->getValue('test', 'enabled', 'no')); + } + + public function testEnableAppForGroups() { + $groups = array( + new Group('group1', array(), null), + new Group('group2', array(), null) + ); + $groupManager = $this->getMock('\OCP\IGroupManager'); + $userSession = $this->getMock('\OCP\IUserSession'); + $appConfig = $this->getAppConfig(); + $manager = new \OC\App\AppManager($userSession, $appConfig, $groupManager); + $manager->enableAppForGroups('test', $groups); + $this->assertEquals('["group1","group2"]', $appConfig->getValue('test', 'enabled', 'no')); + } + + public function testIsInstalledEnabled() { + $userSession = $this->getMock('\OCP\IUserSession'); + $groupManager = $this->getMock('\OCP\IGroupManager'); + $appConfig = $this->getAppConfig(); + $manager = new \OC\App\AppManager($userSession, $appConfig, $groupManager); + $appConfig->setValue('test', 'enabled', 'yes'); + $this->assertTrue($manager->isInstalled('test')); + } + + public function testIsInstalledDisabled() { + $userSession = $this->getMock('\OCP\IUserSession'); + $groupManager = $this->getMock('\OCP\IGroupManager'); + $appConfig = $this->getAppConfig(); + $manager = new \OC\App\AppManager($userSession, $appConfig, $groupManager); + $appConfig->setValue('test', 'enabled', 'no'); + $this->assertFalse($manager->isInstalled('test')); + } + + public function testIsInstalledEnabledForGroups() { + $userSession = $this->getMock('\OCP\IUserSession'); + $groupManager = $this->getMock('\OCP\IGroupManager'); + $appConfig = $this->getAppConfig(); + $manager = new \OC\App\AppManager($userSession, $appConfig, $groupManager); + $appConfig->setValue('test', 'enabled', '["foo"]'); + $this->assertTrue($manager->isInstalled('test')); + } + + public function testIsEnabledForUserEnabled() { + $userSession = $this->getMock('\OCP\IUserSession'); + $groupManager = $this->getMock('\OCP\IGroupManager'); + $appConfig = $this->getAppConfig(); + $manager = new \OC\App\AppManager($userSession, $appConfig, $groupManager); + $appConfig->setValue('test', 'enabled', 'yes'); + $user = new User('user1', null); + $this->assertTrue($manager->isEnabledForUser('test', $user)); + } + + public function testIsEnabledForUserDisabled() { + $userSession = $this->getMock('\OCP\IUserSession'); + $groupManager = $this->getMock('\OCP\IGroupManager'); + $appConfig = $this->getAppConfig(); + $manager = new \OC\App\AppManager($userSession, $appConfig, $groupManager); + $appConfig->setValue('test', 'enabled', 'no'); + $user = new User('user1', null); + $this->assertFalse($manager->isEnabledForUser('test', $user)); + } + + public function testIsEnabledForUserEnabledForGroup() { + $userSession = $this->getMock('\OCP\IUserSession'); + $groupManager = $this->getMock('\OCP\IGroupManager'); + $user = new User('user1', null); + + $groupManager->expects($this->once()) + ->method('getUserGroupIds') + ->with($user) + ->will($this->returnValue(array('foo', 'bar'))); + + $appConfig = $this->getAppConfig(); + $manager = new \OC\App\AppManager($userSession, $appConfig, $groupManager); + $appConfig->setValue('test', 'enabled', '["foo"]'); + $this->assertTrue($manager->isEnabledForUser('test', $user)); + } + + public function testIsEnabledForUserDisabledForGroup() { + $userSession = $this->getMock('\OCP\IUserSession'); + $groupManager = $this->getMock('\OCP\IGroupManager'); + $user = new User('user1', null); + + $groupManager->expects($this->once()) + ->method('getUserGroupIds') + ->with($user) + ->will($this->returnValue(array('bar'))); + + $appConfig = $this->getAppConfig(); + $manager = new \OC\App\AppManager($userSession, $appConfig, $groupManager); + $appConfig->setValue('test', 'enabled', '["foo"]'); + $this->assertFalse($manager->isEnabledForUser('test', $user)); + } + + public function testIsEnabledForUserLoggedOut() { + $userSession = $this->getMock('\OCP\IUserSession'); + $groupManager = $this->getMock('\OCP\IGroupManager'); + + $appConfig = $this->getAppConfig(); + $manager = new \OC\App\AppManager($userSession, $appConfig, $groupManager); + $appConfig->setValue('test', 'enabled', '["foo"]'); + $this->assertFalse($manager->IsEnabledForUser('test')); + } + + public function testIsEnabledForUserLoggedIn() { + $userSession = $this->getMock('\OCP\IUserSession'); + $groupManager = $this->getMock('\OCP\IGroupManager'); + $user = new User('user1', null); + + $userSession->expects($this->once()) + ->method('getUser') + ->will($this->returnValue($user)); + $groupManager->expects($this->once()) + ->method('getUserGroupIds') + ->with($user) + ->will($this->returnValue(array('foo', 'bar'))); + + $appConfig = $this->getAppConfig(); + $manager = new \OC\App\AppManager($userSession, $appConfig, $groupManager); + $appConfig->setValue('test', 'enabled', '["foo"]'); + $this->assertTrue($manager->isEnabledForUser('test')); + } +} diff --git a/tests/lib/appframework/http/JSONResponseTest.php b/tests/lib/appframework/http/JSONResponseTest.php index f7c89a9d2e1..06cd3410a69 100644 --- a/tests/lib/appframework/http/JSONResponseTest.php +++ b/tests/lib/appframework/http/JSONResponseTest.php @@ -44,7 +44,7 @@ class JSONResponseTest extends \PHPUnit_Framework_TestCase { public function testHeader() { $headers = $this->json->getHeaders(); - $this->assertEquals('application/json; charset=utf-8', $headers['Content-type']); + $this->assertEquals('application/json; charset=utf-8', $headers['Content-Type']); } diff --git a/tests/lib/appframework/middleware/security/SecurityMiddlewareTest.php b/tests/lib/appframework/middleware/security/SecurityMiddlewareTest.php index 74fc7907fb5..cc7704f4d1a 100644 --- a/tests/lib/appframework/middleware/security/SecurityMiddlewareTest.php +++ b/tests/lib/appframework/middleware/security/SecurityMiddlewareTest.php @@ -77,7 +77,7 @@ class SecurityMiddlewareTest extends \PHPUnit_Framework_TestCase { $this->navigationManager, $this->urlGenerator, $this->logger, - 'test', + 'files', $isLoggedIn, $isAdminUser ); @@ -91,7 +91,7 @@ class SecurityMiddlewareTest extends \PHPUnit_Framework_TestCase { public function testSetNavigationEntry(){ $this->navigationManager->expects($this->once()) ->method('setActiveEntry') - ->with($this->equalTo('test')); + ->with($this->equalTo('files')); $this->reader->reflect(__CLASS__, __FUNCTION__); $this->middleware->beforeController(__CLASS__, __FUNCTION__); diff --git a/tests/lib/files/storage/storage.php b/tests/lib/files/storage/storage.php index cf42523a5e2..960eb137ea0 100644 --- a/tests/lib/files/storage/storage.php +++ b/tests/lib/files/storage/storage.php @@ -340,10 +340,10 @@ abstract class Storage extends \PHPUnit_Framework_TestCase { } public function testTouchCreateFile() { - $this->assertFalse($this->instance->file_exists('foo')); + $this->assertFalse($this->instance->file_exists('touch')); // returns true on success - $this->assertTrue($this->instance->touch('foo')); - $this->assertTrue($this->instance->file_exists('foo')); + $this->assertTrue($this->instance->touch('touch')); + $this->assertTrue($this->instance->file_exists('touch')); } public function testRecursiveRmdir() { diff --git a/tests/lib/files/view.php b/tests/lib/files/view.php index f42308318d5..44fea65e64e 100644 --- a/tests/lib/files/view.php +++ b/tests/lib/files/view.php @@ -8,6 +8,7 @@ namespace Test\Files; use OC\Files\Cache\Watcher; +use OC\Files\Storage\Temporary; class TemporaryNoTouch extends \OC\Files\Storage\Temporary { public function touch($path, $mtime = null) { @@ -664,6 +665,36 @@ class View extends \PHPUnit_Framework_TestCase { $this->assertSame($info['etag'], $info2['etag']); } + public function testWatcherEtagCrossStorage() { + $storage1 = new Temporary(array()); + $storage2 = new Temporary(array()); + $scanner1 = $storage1->getScanner(); + $scanner2 = $storage2->getScanner(); + $storage1->mkdir('sub'); + \OC\Files\Filesystem::mount($storage1, array(), '/test/'); + \OC\Files\Filesystem::mount($storage2, array(), '/test/sub/storage'); + + $past = time() - 100; + $storage2->file_put_contents('test.txt', 'foobar'); + $scanner1->scan(''); + $scanner2->scan(''); + $view = new \OC\Files\View(''); + + $storage2->getWatcher('')->setPolicy(Watcher::CHECK_ALWAYS); + + $oldFileInfo = $view->getFileInfo('/test/sub/storage/test.txt'); + $oldFolderInfo = $view->getFileInfo('/test'); + + $storage2->getCache()->update($oldFileInfo->getId(), array( + 'storage_mtime' => $past + )); + + $view->getFileInfo('/test/sub/storage/test.txt'); + $newFolderInfo = $view->getFileInfo('/test'); + + $this->assertNotEquals($newFolderInfo->getEtag(), $oldFolderInfo->getEtag()); + } + /** * @dataProvider absolutePathProvider */ diff --git a/tests/lib/security/hasher.php b/tests/lib/security/hasher.php new file mode 100644 index 00000000000..330789c67a0 --- /dev/null +++ b/tests/lib/security/hasher.php @@ -0,0 +1,115 @@ +<?php +/** + * Copyright (c) 2014 Lukas Reschke <lukas@owncloud.com> + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +use OC\Security\Hasher; + +/** + * Class HasherTest + */ +class HasherTest extends \PHPUnit_Framework_TestCase { + + /** + * @return array + */ + public function versionHashProvider() + { + return array( + array('asf32äà$$a.|3', null), + array('asf32äà$$a.|3|5', null), + array('1|2|3|4', array('version' => 1, 'hash' => '2|3|4')), + array('1|我看|这本书。 我看這本書', array('version' => 1, 'hash' => '我看|这本书。 我看這本書')) + ); + } + + /** + * @return array + */ + public function allHashProviders() + { + return array( + // Bogus values + array(null, 'asf32äà$$a.|3', false), + array(null, false, false), + + // Valid SHA1 strings + array('password', '5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8', true), + array('owncloud.com', '27a4643e43046c3569e33b68c1a4b15d31306d29', true), + + // Invalid SHA1 strings + array('InvalidString', '5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8', false), + array('AnotherInvalidOne', '27a4643e43046c3569e33b68c1a4b15d31306d29', false), + + // Valid legacy password string with password salt "6Wow67q1wZQZpUUeI6G2LsWUu4XKx" + array('password', '$2a$08$emCpDEl.V.QwPWt5gPrqrOhdpH6ailBmkj2Hd2vD5U8qIy20HBe7.', true), + array('password', '$2a$08$yjaLO4ev70SaOsWZ9gRS3eRSEpHVsmSWTdTms1949mylxJ279hzo2', true), + array('password', '$2a$08$.jNRG/oB4r7gHJhAyb.mDupNUAqTnBIW/tWBqFobaYflKXiFeG0A6', true), + array('owncloud.com', '$2a$08$YbEsyASX/hXVNMv8hXQo7ezreN17T8Jl6PjecGZvpX.Ayz2aUyaZ2', true), + array('owncloud.com', '$2a$11$cHdDA2IkUP28oNGBwlL7jO/U3dpr8/0LIjTZmE8dMPA7OCUQsSTqS', true), + array('owncloud.com', '$2a$08$GH.UoIfJ1e.qeZ85KPqzQe6NR8XWRgJXWIUeE1o/j1xndvyTA1x96', true), + + // Invalid legacy passwords + array('password', '$2a$08$oKAQY5IhnZocP.61MwP7xu7TNeOb7Ostvk3j6UpacvaNMs.xRj7O2', false), + + // Valid passwords "6Wow67q1wZQZpUUeI6G2LsWUu4XKx" + array('password', '1|$2a$05$ezAE0dkwk57jlfo6z5Pql.gcIK3ReXT15W7ITNxVS0ksfhO/4E4Kq', true), + array('password', '1|$2a$05$4OQmloFW4yTVez2MEWGIleDO9Z5G9tWBXxn1vddogmKBQq/Mq93pe', true), + array('password', '1|$2a$11$yj0hlp6qR32G9exGEXktB.yW2rgt2maRBbPgi3EyxcDwKrD14x/WO', true), + array('owncloud.com', '1|$2a$10$Yiss2WVOqGakxuuqySv5UeOKpF8d8KmNjuAPcBMiRJGizJXjA2bKm', true), + array('owncloud.com', '1|$2a$10$v9mh8/.mF/Ut9jZ7pRnpkuac3bdFCnc4W/gSumheQUi02Sr.xMjPi', true), + array('owncloud.com', '1|$2a$05$ST5E.rplNRfDCzRpzq69leRzsTGtY7k88h9Vy2eWj0Ug/iA9w5kGK', true), + + // Invalid passwords + array('password', '0|$2a$08$oKAQY5IhnZocP.61MwP7xu7TNeOb7Ostvk3j6UpacvaNMs.xRj7O2', false), + array('password', '1|$2a$08$oKAQY5IhnZocP.61MwP7xu7TNeOb7Ostvk3j6UpacvaNMs.xRj7O2', false), + array('password', '2|$2a$08$oKAQY5IhnZocP.61MwP7xu7TNeOb7Ostvk3j6UpacvaNMs.xRj7O2', false), + ); + } + + + + /** @var Hasher */ + protected $hasher; + /** @var \OCP\IConfig */ + protected $config; + + protected function setUp() { + $this->config = $this->getMockBuilder('\OCP\IConfig') + ->disableOriginalConstructor()->getMock(); + + $this->hasher = new Hasher($this->config); + } + + function testHash() { + $hash = $this->hasher->hash('String To Hash'); + $this->assertNotNull($hash); + } + + /** + * @dataProvider versionHashProvider + */ + function testSplitHash($hash, $expected) { + $relativePath = \Test_Helper::invokePrivate($this->hasher, 'splitHash', array($hash)); + $this->assertSame($expected, $relativePath); + } + + + /** + * @dataProvider allHashProviders + */ + function testVerify($password, $hash, $expected) { + $this->config + ->expects($this->any()) + ->method('getSystemValue') + ->with('passwordsalt', null) + ->will($this->returnValue('6Wow67q1wZQZpUUeI6G2LsWUu4XKx')); + + $result = $this->hasher->verify($password, $hash); + $this->assertSame($expected, $result); + } + +} diff --git a/tests/lib/template/resourcelocator.php b/tests/lib/template/resourcelocator.php index 619560643fe..cd354df0036 100644 --- a/tests/lib/template/resourcelocator.php +++ b/tests/lib/template/resourcelocator.php @@ -10,19 +10,17 @@ class Test_ResourceLocator extends PHPUnit_Framework_TestCase { /** * @param string $theme - * @param string $form_factor */ - public function getResourceLocator( $theme, $form_factor, $core_map, $party_map, $appsroots ) { + public function getResourceLocator( $theme, $core_map, $party_map, $appsroots ) { return $this->getMockForAbstractClass('OC\Template\ResourceLocator', - array( $theme, $form_factor, $core_map, $party_map, $appsroots ), + array( $theme, $core_map, $party_map, $appsroots ), '', true, true, true, array()); } public function testConstructor() { - $locator = $this->getResourceLocator('theme', 'form_factor', + $locator = $this->getResourceLocator('theme', array('core'=>'map'), array('3rd'=>'party'), array('foo'=>'bar')); $this->assertAttributeEquals('theme', 'theme', $locator); - $this->assertAttributeEquals('form_factor', 'form_factor', $locator); $this->assertAttributeEquals('core', 'serverroot', $locator); $this->assertAttributeEquals(array('core'=>'map','3rd'=>'party'), 'mapping', $locator); $this->assertAttributeEquals('3rd', 'thirdpartyroot', $locator); @@ -31,7 +29,7 @@ class Test_ResourceLocator extends PHPUnit_Framework_TestCase { } public function testFind() { - $locator = $this->getResourceLocator('theme', 'form_factor', + $locator = $this->getResourceLocator('theme', array('core'=>'map'), array('3rd'=>'party'), array('foo'=>'bar')); $locator->expects($this->once()) ->method('doFind') @@ -41,7 +39,7 @@ class Test_ResourceLocator extends PHPUnit_Framework_TestCase { ->with('foo'); $locator->find(array('foo')); - $locator = $this->getResourceLocator('theme', 'form_factor', + $locator = $this->getResourceLocator('theme', array('core'=>'map'), array('3rd'=>'party'), array('foo'=>'bar')); $locator->expects($this->once()) ->method('doFind') @@ -50,12 +48,12 @@ class Test_ResourceLocator extends PHPUnit_Framework_TestCase { try { $locator->find(array('foo')); } catch (\Exception $e) { - $this->assertEquals('test formfactor:form_factor serverroot:core', $e->getMessage()); + $this->assertEquals('test serverroot:core', $e->getMessage()); } } public function testAppendIfExist() { - $locator = $this->getResourceLocator('theme', 'form_factor', + $locator = $this->getResourceLocator('theme', array(__DIR__=>'map'), array('3rd'=>'party'), array('foo'=>'bar')); $method = new ReflectionMethod($locator, 'appendIfExist'); $method->setAccessible(true); diff --git a/tests/lib/user/manager.php b/tests/lib/user/manager.php index fd0931af7e4..15b28e61bd5 100644 --- a/tests/lib/user/manager.php +++ b/tests/lib/user/manager.php @@ -416,6 +416,17 @@ class Manager extends \PHPUnit_Framework_TestCase { $users = array_shift($result); //users from backends shall be summed up - $this->assertEquals(7+16, $users); + $this->assertEquals(7 + 16, $users); + } + + public function testDeleteUser() { + $manager = new \OC\User\Manager(); + $backend = new \OC_User_Dummy(); + + $backend->createUser('foo', 'bar'); + $manager->registerBackend($backend); + $this->assertTrue($manager->userExists('foo')); + $manager->get('foo')->delete(); + $this->assertFalse($manager->userExists('foo')); } } diff --git a/tests/settings/controller/mailsettingscontrollertest.php b/tests/settings/controller/mailsettingscontrollertest.php index ff3d1d93a1b..6d3485d28e4 100644 --- a/tests/settings/controller/mailsettingscontrollertest.php +++ b/tests/settings/controller/mailsettingscontrollertest.php @@ -147,6 +147,12 @@ class MailSettingscontrollerTest extends \PHPUnit_Framework_TestCase { } public function testSendTestMail() { + /** + * FIXME: Disabled due to missing DI on mail class. + * TODO: Re-enable when https://github.com/owncloud/core/pull/12085 is merged. + */ + $this->markTestSkipped('Disable test until OC_Mail is rewritten.'); + $user = $this->getMockBuilder('\OC\User\User') ->disableOriginalConstructor() ->getMock(); diff --git a/tests/settings/controller/securitysettingscontrollertest.php b/tests/settings/controller/securitysettingscontrollertest.php new file mode 100644 index 00000000000..d89e4932368 --- /dev/null +++ b/tests/settings/controller/securitysettingscontrollertest.php @@ -0,0 +1,138 @@ +<?php +/** + * @author Lukas Reschke + * @copyright 2014 Lukas Reschke lukas@owncloud.com + * + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ +namespace OC\Settings\Controller; + +use \OC\Settings\Application; + +/** + * @package OC\Settings\Controller + */ +class SecuritySettingsControllerTest extends \PHPUnit_Framework_TestCase { + + /** @var \OCP\AppFramework\IAppContainer */ + private $container; + + /** @var SecuritySettingsController */ + private $securitySettingsController; + + protected function setUp() { + $app = new Application(); + $this->container = $app->getContainer(); + $this->container['Config'] = $this->getMockBuilder('\OCP\IConfig') + ->disableOriginalConstructor()->getMock(); + $this->container['AppName'] = 'settings'; + $this->securitySettingsController = $this->container['SecuritySettingsController']; + } + + + public function testEnforceSSLEmpty() { + $this->container['Config'] + ->expects($this->once()) + ->method('setSystemValue') + ->with('forcessl', false); + + $response = $this->securitySettingsController->enforceSSL(); + $expectedResponse = array('status' => 'success'); + + $this->assertSame($expectedResponse, $response); + } + + public function testEnforceSSL() { + $this->container['Config'] + ->expects($this->once()) + ->method('setSystemValue') + ->with('forcessl', true); + + $response = $this->securitySettingsController->enforceSSL(true); + $expectedResponse = array('status' => 'success'); + + $this->assertSame($expectedResponse, $response); + } + + public function testEnforceSSLInvalid() { + $this->container['Config'] + ->expects($this->exactly(0)) + ->method('setSystemValue'); + + $response = $this->securitySettingsController->enforceSSL('blah'); + $expectedResponse = array('status' => 'error'); + + $this->assertSame($expectedResponse, $response); + } + + public function testEnforceSSLForSubdomainsEmpty() { + $this->container['Config'] + ->expects($this->once()) + ->method('setSystemValue') + ->with('forceSSLforSubdomains', false); + + $response = $this->securitySettingsController->enforceSSLForSubdomains(); + $expectedResponse = array('status' => 'success'); + + $this->assertSame($expectedResponse, $response); + } + + public function testEnforceSSLForSubdomains() { + $this->container['Config'] + ->expects($this->once()) + ->method('setSystemValue') + ->with('forceSSLforSubdomains', true); + + $response = $this->securitySettingsController->enforceSSLForSubdomains(true); + $expectedResponse = array('status' => 'success'); + + $this->assertSame($expectedResponse, $response); + } + + public function testEnforceSSLForSubdomainsInvalid() { + $this->container['Config'] + ->expects($this->exactly(0)) + ->method('setSystemValue'); + + $response = $this->securitySettingsController->enforceSSLForSubdomains('blah'); + $expectedResponse = array('status' => 'error'); + + $this->assertSame($expectedResponse, $response); + } + + public function testTrustedDomainsWithExistingValues() { + $this->container['Config'] + ->expects($this->once()) + ->method('setSystemValue') + ->with('trusted_domains', array('owncloud.org', 'owncloud.com', 'newdomain.com')); + $this->container['Config'] + ->expects($this->once()) + ->method('getSystemValue') + ->with('trusted_domains') + ->will($this->returnValue(array('owncloud.org', 'owncloud.com'))); + + $response = $this->securitySettingsController->trustedDomains('newdomain.com'); + $expectedResponse = array('status' => 'success'); + + $this->assertSame($expectedResponse, $response); + } + + public function testTrustedDomainsEmpty() { + $this->container['Config'] + ->expects($this->once()) + ->method('setSystemValue') + ->with('trusted_domains', array('newdomain.com')); + $this->container['Config'] + ->expects($this->once()) + ->method('getSystemValue') + ->with('trusted_domains') + ->will($this->returnValue('')); + + $response = $this->securitySettingsController->trustedDomains('newdomain.com'); + $expectedResponse = array('status' => 'success'); + + $this->assertSame($expectedResponse, $response); + } +} |