diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/lib/App/AppStore/Bundles/BundleFetcherTest.php | 4 | ||||
-rw-r--r-- | tests/lib/Avatar/UserAvatarTest.php | 58 | ||||
-rw-r--r-- | tests/lib/BackgroundJob/JobTest.php | 21 | ||||
-rw-r--r-- | tests/lib/Contacts/ContactsMenu/ActionFactoryTest.php | 2 | ||||
-rw-r--r-- | tests/lib/Security/HasherTest.php | 65 | ||||
-rw-r--r-- | tests/lib/Settings/ManagerTest.php | 173 |
6 files changed, 170 insertions, 153 deletions
diff --git a/tests/lib/App/AppStore/Bundles/BundleFetcherTest.php b/tests/lib/App/AppStore/Bundles/BundleFetcherTest.php index f453fbcb59a..b4d30b4bf50 100644 --- a/tests/lib/App/AppStore/Bundles/BundleFetcherTest.php +++ b/tests/lib/App/AppStore/Bundles/BundleFetcherTest.php @@ -26,6 +26,7 @@ use OC\App\AppStore\Bundles\CoreBundle; use OC\App\AppStore\Bundles\EducationBundle; use OC\App\AppStore\Bundles\EnterpriseBundle; use OC\App\AppStore\Bundles\GroupwareBundle; +use OC\App\AppStore\Bundles\HubBundle; use OC\App\AppStore\Bundles\SocialSharingBundle; use OCP\IL10N; use Test\TestCase; @@ -49,6 +50,7 @@ class BundleFetcherTest extends TestCase { public function testGetBundles() { $expected = [ new EnterpriseBundle($this->l10n), + new HubBundle($this->l10n), new GroupwareBundle($this->l10n), new SocialSharingBundle($this->l10n), new EducationBundle($this->l10n), @@ -69,7 +71,7 @@ class BundleFetcherTest extends TestCase { $this->assertEquals(new GroupwareBundle($this->l10n), $this->bundleFetcher->getBundleByIdentifier('GroupwareBundle')); } - + public function testGetBundleByIdentifierWithException() { $this->expectException(\BadMethodCallException::class); $this->expectExceptionMessage('Bundle with specified identifier does not exist'); diff --git a/tests/lib/Avatar/UserAvatarTest.php b/tests/lib/Avatar/UserAvatarTest.php index ff6c63df2a8..43e325b0941 100644 --- a/tests/lib/Avatar/UserAvatarTest.php +++ b/tests/lib/Avatar/UserAvatarTest.php @@ -35,22 +35,20 @@ class UserAvatarTest extends \Test\TestCase { parent::setUp(); $this->folder = $this->createMock(SimpleFolder::class); - /** @var \OCP\IL10N | \PHPUnit_Framework_MockObject_MockObject $l */ - $l = $this->createMock(IL10N::class); - $l->method('t')->will($this->returnArgument(0)); - $this->user = $this->createMock(User::class); + // abcdefghi is a convenient name that our algorithm convert to our nextcloud blue 0082c9 + $this->user = $this->getUserWithDisplayName('abcdefghi'); $this->config = $this->createMock(IConfig::class); - $this->avatar = new \OC\Avatar\UserAvatar( - $this->folder, - $l, - $this->user, - $this->createMock(ILogger::class), - $this->config - ); + $this->avatar = $this->getUserAvatar($this->user); + } - // abcdefghi is a convenient name that our algorithm convert to our nextcloud blue 0082c9 - $this->user->method('getDisplayName')->willReturn('abcdefghi'); + public function avatarTextData() { + return [ + ['', '?'], + ['matchish', 'M'], + ['Firstname Lastname', 'FL'], + ['Firstname Lastname Rest', 'FL'], + ]; } public function testGetNoAvatar() { @@ -239,6 +237,18 @@ class UserAvatarTest extends \Test\TestCase { $this->assertEquals($avatar, $svg); } + + /** + * @dataProvider avatarTextData + */ + public function testGetAvatarText($displayName, $expectedAvatarText) { + $user = $this->getUserWithDisplayName($displayName); + $avatar = $this->getUserAvatar($user); + + $avatarText = $this->invokePrivate($avatar, 'getAvatarText'); + $this->assertEquals($expectedAvatarText, $avatarText); + } + public function testHashToInt() { $hashToInt = $this->invokePrivate($this->avatar, 'hashToInt', ['abcdef', 18]); $this->assertTrue(gettype($hashToInt) === 'integer'); @@ -261,4 +271,26 @@ class UserAvatarTest extends \Test\TestCase { $this->assertTrue(gettype($hashToInt) === 'integer'); } + private function getUserWithDisplayName($name) + { + $user = $this->createMock(User::class); + $user->method('getDisplayName')->willReturn($name); + return $user; + } + + private function getUserAvatar($user) + { + /** @var \OCP\IL10N | \PHPUnit_Framework_MockObject_MockObject $l */ + $l = $this->createMock(IL10N::class); + $l->method('t')->will($this->returnArgument(0)); + + return new \OC\Avatar\UserAvatar( + $this->folder, + $l, + $user, + $this->createMock(ILogger::class), + $this->config + ); + } + } diff --git a/tests/lib/BackgroundJob/JobTest.php b/tests/lib/BackgroundJob/JobTest.php index 6e5474e597c..b4048aa1c22 100644 --- a/tests/lib/BackgroundJob/JobTest.php +++ b/tests/lib/BackgroundJob/JobTest.php @@ -39,6 +39,27 @@ class JobTest extends \Test\TestCase { $this->assertCount(1, $jobList->getAll()); } + public function testRemoveAfterError() { + $jobList = new DummyJobList(); + $job = new TestJob($this, function () { + $test = null; + $test->someMethod(); + }); + $jobList->add($job); + + $logger = $this->getMockBuilder(ILogger::class) + ->disableOriginalConstructor() + ->getMock(); + $logger->expects($this->once()) + ->method('logException') + ->with($this->isInstanceOf(\Throwable::class)); + + $this->assertCount(1, $jobList->getAll()); + $job->execute($jobList, $logger); + $this->assertTrue($this->run); + $this->assertCount(1, $jobList->getAll()); + } + public function markRun() { $this->run = true; } diff --git a/tests/lib/Contacts/ContactsMenu/ActionFactoryTest.php b/tests/lib/Contacts/ContactsMenu/ActionFactoryTest.php index 54106bbb642..8e7f64ff9d1 100644 --- a/tests/lib/Contacts/ContactsMenu/ActionFactoryTest.php +++ b/tests/lib/Contacts/ContactsMenu/ActionFactoryTest.php @@ -61,7 +61,7 @@ class ActionFactoryTest extends TestCase { $this->assertInstanceOf(IAction::class, $action); $this->assertEquals($name, $action->getName()); $this->assertEquals(10, $action->getPriority()); - $this->assertEquals('mailto:user%40example.com', $action->getHref()); + $this->assertEquals('mailto:user@example.com', $action->getHref()); } } diff --git a/tests/lib/Security/HasherTest.php b/tests/lib/Security/HasherTest.php index 3222b5d0984..e680efb19b6 100644 --- a/tests/lib/Security/HasherTest.php +++ b/tests/lib/Security/HasherTest.php @@ -126,8 +126,12 @@ class HasherTest extends \Test\TestCase { $this->config ->expects($this->any()) ->method('getSystemValue') - ->with('passwordsalt', null) - ->will($this->returnValue('6Wow67q1wZQZpUUeI6G2LsWUu4XKx')); + ->willReturnCallback(function ($key, $default) { + if($key === 'passwordsalt') { + return '6Wow67q1wZQZpUUeI6G2LsWUu4XKx'; + } + return $default; + }); $result = $this->hasher->verify($password, $hash); $this->assertSame($expected, $result); @@ -162,4 +166,61 @@ class HasherTest extends \Test\TestCase { $this->assertFalse(password_needs_rehash($relativePath['hash'], PASSWORD_ARGON2I, [])); } + + public function testUsePasswordDefaultArgon2iVerify() { + if (!\defined('PASSWORD_ARGON2I')) { + $this->markTestSkipped('Need ARGON2 support to test ARGON2 hashes'); + } + + $this->config->method('getSystemValue') + ->with('hashing_default_password') + ->willReturn(true); + + $message = 'mysecret'; + + $argon2i = 2 . '|' . password_hash($message, PASSWORD_ARGON2I, []); + + $newHash = null; + $this->assertTrue($this->hasher->verify($message, $argon2i, $newHash)); + $this->assertNotNull($newHash); + } + + public function testDoNotUserPasswordDefaultArgon2iVerify() { + if (!\defined('PASSWORD_ARGON2I')) { + $this->markTestSkipped('Need ARGON2 support to test ARGON2 hashes'); + } + + $this->config->method('getSystemValue') + ->with('hashing_default_password') + ->willReturn(false); + + $message = 'mysecret'; + + $argon2i = 2 . '|' . password_hash($message, PASSWORD_ARGON2I, []); + + $newHash = null; + $this->assertTrue($this->hasher->verify($message, $argon2i, $newHash)); + $this->assertNull($newHash); + } + + public function testHashUsePasswordDefault() { + if (!\defined('PASSWORD_ARGON2I')) { + $this->markTestSkipped('Need ARGON2 support to test ARGON2 hashes'); + } + + $this->config->method('getSystemValue') + ->with('hashing_default_password') + ->willReturn(true); + + $message = 'mysecret'; + + $hash = $this->hasher->hash($message); + $relativePath = self::invokePrivate($this->hasher, 'splitHash', [$hash]); + + $this->assertSame(1, $relativePath['version']); + + $info = password_get_info($relativePath['hash']); + $this->assertEquals(PASSWORD_BCRYPT, $info['algo']); + + } } diff --git a/tests/lib/Settings/ManagerTest.php b/tests/lib/Settings/ManagerTest.php index 761f5f9a5c1..bce547c9a29 100644 --- a/tests/lib/Settings/ManagerTest.php +++ b/tests/lib/Settings/ManagerTest.php @@ -24,15 +24,14 @@ namespace OCA\Settings\Tests\AppInfo; use OC\Settings\Manager; -use OC\Settings\Section; use OCA\Settings\Admin\Sharing; -use OCA\Settings\Personal\Security; use OCP\IDBConnection; use OCP\IL10N; use OCP\ILogger; use OCP\IServerContainer; use OCP\IURLGenerator; use OCP\L10N\IFactory; +use OCP\Settings\ISettings; use OCP\Settings\ISubAdminSettings; use Test\TestCase; @@ -69,99 +68,23 @@ class ManagerTest extends TestCase { } public function testGetAdminSections() { - $this->l10nFactory - ->expects($this->once()) - ->method('get') - ->with('lib') - ->willReturn($this->l10n); - $this->l10n - ->expects($this->any()) - ->method('t') - ->will($this->returnArgument(0)); - $this->manager->registerSection('admin', \OCA\WorkflowEngine\Settings\Section::class); - $this->url->expects($this->exactly(6)) - ->method('imagePath') - ->willReturnMap([ - ['settings', 'admin.svg', '0'], - ['core', 'actions/settings-dark.svg', '1'], - ['core', 'actions/share.svg', '2'], - ['core', 'actions/password.svg', '3'], - ['core', 'places/contacts.svg', '5'], - ['settings', 'help.svg', '4'], - ]); - $this->assertEquals([ - 0 => [new Section('overview', 'Overview', 0, '0')], - 1 => [new Section('server', 'Basic settings', 0, '1')], - 5 => [new Section('sharing', 'Sharing', 0, '2')], - 10 => [new Section('security', 'Security', 0, '3')], - 50 => [new Section('groupware', 'Groupware', 0, '5')], 55 => [\OC::$server->query(\OCA\WorkflowEngine\Settings\Section::class)], - 98 => [new Section('additional', 'Additional settings', 0, '1')], ], $this->manager->getAdminSections()); } public function testGetPersonalSections() { - $this->l10nFactory - ->expects($this->once()) - ->method('get') - ->with('lib') - ->willReturn($this->l10n); - $this->l10n - ->expects($this->any()) - ->method('t') - ->will($this->returnArgument(0)); - $this->manager->registerSection('personal', \OCA\WorkflowEngine\Settings\Section::class); - $this->url->expects($this->exactly(3)) - ->method('imagePath') - ->willReturnMap([ - ['core', 'actions/user.svg', '1'], - ['settings', 'password.svg', '2'], - ['core', 'clients/phone.svg', '3'], - ]); - $this->assertEquals([ - 0 => [new Section('personal-info', 'Personal info', 0, '1')], - 5 => [new Section('security', 'Security', 0, '2')], - 15 => [new Section('sync-clients', 'Mobile & desktop', 0, '3')], 55 => [\OC::$server->query(\OCA\WorkflowEngine\Settings\Section::class)], ], $this->manager->getPersonalSections()); } public function testGetAdminSectionsEmptySection() { - $this->l10nFactory - ->expects($this->once()) - ->method('get') - ->with('lib') - ->willReturn($this->l10n); - $this->l10n - ->expects($this->any()) - ->method('t') - ->will($this->returnArgument(0)); - - $this->url->expects($this->exactly(6)) - ->method('imagePath') - ->willReturnMap([ - ['settings', 'admin.svg', '0'], - ['core', 'actions/settings-dark.svg', '1'], - ['core', 'actions/share.svg', '2'], - ['core', 'actions/password.svg', '3'], - ['core', 'places/contacts.svg', '5'], - ['settings', 'help.svg', '4'], - ]); - - $this->assertEquals([ - 0 => [new Section('overview', 'Overview', 0, '0')], - 1 => [new Section('server', 'Basic settings', 0, '1')], - 5 => [new Section('sharing', 'Sharing', 0, '2')], - 10 => [new Section('security', 'Security', 0, '3')], - 50 => [new Section('groupware', 'Groupware', 0, '5')], - 98 => [new Section('additional', 'Additional settings', 0, '1')], - ], $this->manager->getAdminSections()); + $this->assertEquals([], $this->manager->getAdminSections()); } public function testGetPersonalSectionsEmptySection() { @@ -175,31 +98,20 @@ class ManagerTest extends TestCase { ->method('t') ->will($this->returnArgument(0)); - $this->url->expects($this->exactly(3)) - ->method('imagePath') - ->willReturnMap([ - ['core', 'actions/user.svg', '1'], - ['settings', 'password.svg', '2'], - ['core', 'clients/phone.svg', '3'], - ]); - - $this->assertArraySubset([ - 0 => [new Section('personal-info', 'Personal info', 0, '1')], - 5 => [new Section('security', 'Security', 0, '2')], - 15 => [new Section('sync-clients', 'Mobile & desktop', 0, '3')], - ], $this->manager->getPersonalSections()); + $this->assertEquals([], $this->manager->getPersonalSections()); } public function testGetAdminSettings() { - $section = $this->createMock(Sharing::class); - $section->expects($this->once()) - ->method('getPriority') + $section = $this->createMock(ISettings::class); + $section->method('getPriority') ->willReturn(13); - $this->container->expects($this->once()) - ->method('query') - ->with(Sharing::class) + $section->method('getSection') + ->willReturn('sharing'); + $this->container->method('query') + ->with('myAdminClass') ->willReturn($section); + $this->manager->registerSetting('admin', 'myAdminClass'); $settings = $this->manager->getAdminSettings('sharing'); $this->assertEquals([ @@ -208,12 +120,16 @@ class ManagerTest extends TestCase { } public function testGetAdminSettingsAsSubAdmin() { - $section = $this->createMock(Sharing::class); - $this->container->expects($this->once()) - ->method('query') - ->with(Sharing::class) + $section = $this->createMock(ISettings::class); + $section->method('getPriority') + ->willReturn(13); + $section->method('getSection') + ->willReturn('sharing'); + $this->container->method('query') + ->with('myAdminClass') ->willReturn($section); + $this->manager->registerSetting('admin', 'myAdminClass'); $settings = $this->manager->getAdminSettings('sharing', true); $this->assertEquals([], $settings); @@ -221,14 +137,16 @@ class ManagerTest extends TestCase { public function testGetSubAdminSettingsAsSubAdmin() { $section = $this->createMock(ISubAdminSettings::class); - $section->expects($this->once()) - ->method('getPriority') + $section->method('getPriority') ->willReturn(13); + $section->method('getSection') + ->willReturn('sharing'); $this->container->expects($this->once()) ->method('query') - ->with(Sharing::class) + ->with('mySubAdminClass') ->willReturn($section); + $this->manager->registerSetting('admin', 'mySubAdminClass'); $settings = $this->manager->getAdminSettings('sharing', true); $this->assertEquals([ @@ -237,21 +155,27 @@ class ManagerTest extends TestCase { } public function testGetPersonalSettings() { - $section = $this->createMock(Security::class); - $section->expects($this->once()) - ->method('getPriority') + $section = $this->createMock(ISettings::class); + $section->method('getPriority') ->willReturn(16); - $section2 = $this->createMock(Security\Authtokens::class); - $section2->expects($this->once()) - ->method('getPriority') + $section->method('getSection') + ->willReturn('security'); + $section2 = $this->createMock(ISettings::class); + $section2->method('getPriority') ->willReturn(100); + $section2->method('getSection') + ->willReturn('security'); + + $this->manager->registerSetting('personal', 'section1'); + $this->manager->registerSetting('personal', 'section2'); + $this->container->expects($this->at(0)) ->method('query') - ->with(Security::class) + ->with('section1') ->willReturn($section); $this->container->expects($this->at(1)) ->method('query') - ->with(Security\Authtokens::class) + ->with('section2') ->willReturn($section2); $settings = $this->manager->getPersonalSettings('security'); @@ -276,35 +200,12 @@ class ManagerTest extends TestCase { $this->manager->registerSection('personal', \OCA\WorkflowEngine\Settings\Section::class); $this->manager->registerSection('admin', \OCA\WorkflowEngine\Settings\Section::class); - $this->url->expects($this->exactly(9)) - ->method('imagePath') - ->willReturnMap([ - ['core', 'actions/user.svg', '1'], - ['settings', 'password.svg', '2'], - ['core', 'clients/phone.svg', '3'], - ['settings', 'admin.svg', '0'], - ['core', 'actions/settings-dark.svg', '1'], - ['core', 'actions/share.svg', '2'], - ['core', 'actions/password.svg', '3'], - ['core', 'places/contacts.svg', '5'], - ['settings', 'help.svg', '4'], - ]); - $this->assertEquals([ - 0 => [new Section('personal-info', 'Personal info', 0, '1')], - 5 => [new Section('security', 'Security', 0, '2')], - 15 => [new Section('sync-clients', 'Mobile & desktop', 0, '3')], 55 => [\OC::$server->query(\OCA\WorkflowEngine\Settings\Section::class)], ], $this->manager->getPersonalSections()); $this->assertEquals([ - 0 => [new Section('overview', 'Overview', 0, '0')], - 1 => [new Section('server', 'Basic settings', 0, '1')], - 5 => [new Section('sharing', 'Sharing', 0, '2')], - 10 => [new Section('security', 'Security', 0, '3')], - 50 => [new Section('groupware', 'Groupware', 0, '5')], 55 => [\OC::$server->query(\OCA\WorkflowEngine\Settings\Section::class)], - 98 => [new Section('additional', 'Additional settings', 0, '1')], ], $this->manager->getAdminSections()); } } |