]> source.dussan.org Git - nextcloud-server.git/commitdiff
Fix unit tests
authorJoas Schilling <coding@schilljs.com>
Wed, 23 Mar 2022 10:00:34 +0000 (11:00 +0100)
committerJohn Molakvoæ (Rebase PR Action) <skjnldsv@users.noreply.github.com>
Fri, 13 May 2022 18:34:19 +0000 (18:34 +0000)
Signed-off-by: Joas Schilling <coding@schilljs.com>
tests/lib/Authentication/Token/PublicKeyTokenProviderTest.php
tests/lib/Authentication/TwoFactorAuth/ManagerTest.php

index f27100b5d781c7d2e9d2bb45342107a78c4237b8..9503a5e450116c1c94833c655ffe508cccb4117f 100644 (file)
@@ -82,10 +82,7 @@ class PublicKeyTokenProviderTest extends TestCase {
                $uid = 'user';
                $user = 'User';
                $password = 'passme';
-               $name = 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12'
-                       . 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12'
-                       . 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12'
-                       . 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12';
+               $name = 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12';
                $type = IToken::PERMANENT_TOKEN;
 
                $actual = $this->tokenProvider->generateToken($token, $uid, $user, $password, $name, $type, IToken::DO_NOT_REMEMBER);
@@ -98,6 +95,22 @@ class PublicKeyTokenProviderTest extends TestCase {
                $this->assertSame($password, $this->tokenProvider->getPassword($actual, $token));
        }
 
+       public function testGenerateTokenInvalidName() {
+               $this->expectException(\OC\Authentication\Exceptions\InvalidTokenException::class);
+
+               $token = 'token';
+               $uid = 'user';
+               $user = 'User';
+               $password = 'passme';
+               $name = 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12'
+                       . 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12'
+                       . 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12'
+                       . 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12';
+               $type = IToken::PERMANENT_TOKEN;
+
+               $actual = $this->tokenProvider->generateToken($token, $uid, $user, $password, $name, $type, IToken::DO_NOT_REMEMBER);
+       }
+
        public function testUpdateToken() {
                $tk = new PublicKeyToken();
                $tk->setLastActivity($this->time - 200);
@@ -140,10 +153,7 @@ class PublicKeyTokenProviderTest extends TestCase {
                $uid = 'user';
                $user = 'User';
                $password = 'passme';
-               $name = 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12'
-                       . 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12'
-                       . 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12'
-                       . 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12';
+               $name = 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12';
                $type = IToken::PERMANENT_TOKEN;
 
                $actual = $this->tokenProvider->generateToken($token, $uid, $user, $password, $name, $type, IToken::DO_NOT_REMEMBER);
@@ -170,10 +180,7 @@ class PublicKeyTokenProviderTest extends TestCase {
                $uid = 'user';
                $user = 'User';
                $password = 'passme';
-               $name = 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12'
-                       . 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12'
-                       . 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12'
-                       . 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12';
+               $name = 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12';
                $type = IToken::PERMANENT_TOKEN;
 
                $actual = $this->tokenProvider->generateToken($token, $uid, $user, $password, $name, $type, IToken::DO_NOT_REMEMBER);
@@ -186,10 +193,7 @@ class PublicKeyTokenProviderTest extends TestCase {
                $uid = 'user';
                $user = 'User';
                $password = 'passme';
-               $name = 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12'
-                       . 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12'
-                       . 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12'
-                       . 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12';
+               $name = 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12';
                $type = IToken::PERMANENT_TOKEN;
 
                $actual = $this->tokenProvider->generateToken($token, $uid, $user, $password, $name, $type, IToken::DO_NOT_REMEMBER);
@@ -249,12 +253,12 @@ class PublicKeyTokenProviderTest extends TestCase {
                                ['session_lifetime', $defaultSessionLifetime, 150],
                                ['remember_login_cookie_lifetime', $defaultRememberMeLifetime, 300],
                        ]);
-               $this->mapper->expects($this->at(0))
-                       ->method('invalidateOld')
-                       ->with($this->time - 150);
-               $this->mapper->expects($this->at(1))
+               $this->mapper->expects($this->exactly(2))
                        ->method('invalidateOld')
-                       ->with($this->time - 300);
+                       ->withConsecutive(
+                               [$this->time - 150],
+                               [$this->time - 300]
+                       );
 
                $this->tokenProvider->invalidateOldTokens();
        }
@@ -264,21 +268,18 @@ class PublicKeyTokenProviderTest extends TestCase {
                $uid = 'user';
                $user = 'User';
                $password = null;
-               $name = 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12'
-                       . 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12'
-                       . 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12'
-                       . 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12';
+               $name = 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12';
                $type = IToken::PERMANENT_TOKEN;
 
                $oldToken = $this->tokenProvider->generateToken($token, $uid, $user, $password, $name, $type, IToken::DO_NOT_REMEMBER);
 
                $this->mapper
-                       ->expects($this->at(0))
+                       ->expects($this->once())
                        ->method('getToken')
                        ->with(hash('sha512', 'oldId' . '1f4h9s'))
                        ->willReturn($oldToken);
                $this->mapper
-                       ->expects($this->at(1))
+                       ->expects($this->once())
                        ->method('insert')
                        ->with($this->callback(function (PublicKeyToken $token) use ($user, $uid, $name) {
                                return $token->getUID() === $uid &&
@@ -289,7 +290,7 @@ class PublicKeyTokenProviderTest extends TestCase {
                                        $token->getPassword() === null;
                        }));
                $this->mapper
-                       ->expects($this->at(2))
+                       ->expects($this->once())
                        ->method('delete')
                        ->with($this->callback(function ($token) use ($oldToken) {
                                return $token === $oldToken;
@@ -303,21 +304,18 @@ class PublicKeyTokenProviderTest extends TestCase {
                $uid = 'user';
                $user = 'User';
                $password = 'password';
-               $name = 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12'
-                       . 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12'
-                       . 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12'
-                       . 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12';
+               $name = 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12';
                $type = IToken::PERMANENT_TOKEN;
 
                $oldToken = $this->tokenProvider->generateToken($token, $uid, $user, $password, $name, $type, IToken::DO_NOT_REMEMBER);
 
                $this->mapper
-                       ->expects($this->at(0))
+                       ->expects($this->once())
                        ->method('getToken')
                        ->with(hash('sha512', 'oldId' . '1f4h9s'))
                        ->willReturn($oldToken);
                $this->mapper
-                       ->expects($this->at(1))
+                       ->expects($this->once())
                        ->method('insert')
                        ->with($this->callback(function (PublicKeyToken $token) use ($user, $uid, $name) {
                                return $token->getUID() === $uid &&
@@ -329,7 +327,7 @@ class PublicKeyTokenProviderTest extends TestCase {
                                        $this->tokenProvider->getPassword($token, 'newId') === 'password';
                        }));
                $this->mapper
-                       ->expects($this->at(2))
+                       ->expects($this->once())
                        ->method('delete')
                        ->with($this->callback(function ($token) use ($oldToken) {
                                return $token === $oldToken;
@@ -373,10 +371,7 @@ class PublicKeyTokenProviderTest extends TestCase {
                $uid = 'user';
                $user = 'User';
                $password = 'passme';
-               $name = 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12'
-                       . 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12'
-                       . 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12'
-                       . 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12';
+               $name = 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12';
                $type = IToken::PERMANENT_TOKEN;
 
                $actual = $this->tokenProvider->generateToken($token, $uid, $user, $password, $name, $type, IToken::DO_NOT_REMEMBER);
@@ -441,10 +436,7 @@ class PublicKeyTokenProviderTest extends TestCase {
                $uid = 'user';
                $user = 'User';
                $password = 'password';
-               $name = 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12'
-                       . 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12'
-                       . 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12'
-                       . 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12';
+               $name = 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12';
                $type = IToken::PERMANENT_TOKEN;
 
                $actual = $this->tokenProvider->generateToken($token, $uid, $user, $password, $name, $type, IToken::DO_NOT_REMEMBER);
@@ -459,10 +451,7 @@ class PublicKeyTokenProviderTest extends TestCase {
                $uid = 'user';
                $user = 'User';
                $password = null;
-               $name = 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12'
-                       . 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12'
-                       . 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12'
-                       . 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12';
+               $name = 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12';
                $type = IToken::PERMANENT_TOKEN;
 
                $actual = $this->tokenProvider->generateToken($token, $uid, $user, $password, $name, $type, IToken::DO_NOT_REMEMBER);
index a04e0f05f9dc9dd79c614013bd157d57dd5f9890..38ce020e3740d3fe7cc89cec0e0cfb1881384728 100644 (file)
@@ -370,13 +370,13 @@ class ManagerTest extends TestCase {
                        ->method('get')
                        ->with('two_factor_remember_login')
                        ->willReturn(false);
-               $this->session->expects($this->at(1))
+               $this->session->expects($this->exactly(2))
                        ->method('remove')
-                       ->with('two_factor_auth_uid');
-               $this->session->expects($this->at(2))
-                       ->method('remove')
-                       ->with('two_factor_remember_login');
-               $this->session->expects($this->at(3))
+                       ->withConsecutive(
+                               ['two_factor_auth_uid'],
+                               ['two_factor_remember_login']
+                       );
+               $this->session->expects($this->once())
                        ->method('set')
                        ->with(Manager::SESSION_UID_DONE, 'jos');
                $this->session->method('getId')
@@ -488,17 +488,13 @@ class ManagerTest extends TestCase {
 
        public function testNeedsSecondFactor() {
                $user = $this->createMock(IUser::class);
-               $this->session->expects($this->at(0))
-                       ->method('exists')
-                       ->with('app_password')
-                       ->willReturn(false);
-               $this->session->expects($this->at(1))
+               $this->session->expects($this->exactly(3))
                        ->method('exists')
-                       ->with('two_factor_auth_uid')
-                       ->willReturn(false);
-               $this->session->expects($this->at(2))
-                       ->method('exists')
-                       ->with(Manager::SESSION_UID_DONE)
+                       ->withConsecutive(
+                               ['app_password'],
+                               ['two_factor_auth_uid'],
+                               [Manager::SESSION_UID_DONE],
+                       )
                        ->willReturn(false);
 
                $this->session->method('getId')
@@ -568,12 +564,12 @@ class ManagerTest extends TestCase {
                $this->user->method('getUID')
                        ->willReturn('ferdinand');
 
-               $this->session->expects($this->at(0))
-                       ->method('set')
-                       ->with('two_factor_auth_uid', 'ferdinand');
-               $this->session->expects($this->at(1))
+               $this->session->expects($this->exactly(2))
                        ->method('set')
-                       ->with('two_factor_remember_login', true);
+                       ->withConsecutive(
+                               ['two_factor_auth_uid', 'ferdinand'],
+                               ['two_factor_remember_login', true]
+                       );
 
                $this->session->method('getId')
                        ->willReturn('mysessionid');
@@ -598,12 +594,12 @@ class ManagerTest extends TestCase {
                $this->user->method('getUID')
                        ->willReturn('ferdinand');
 
-               $this->session->expects($this->at(0))
-                       ->method('set')
-                       ->with('two_factor_auth_uid', 'ferdinand');
-               $this->session->expects($this->at(1))
+               $this->session->expects($this->exactly(2))
                        ->method('set')
-                       ->with('two_factor_remember_login', false);
+                       ->withConsecutive(
+                               ['two_factor_auth_uid', 'ferdinand'],
+                               ['two_factor_remember_login', false]
+                       );
 
                $this->session->method('getId')
                        ->willReturn('mysessionid');