From 56199eba376e044f65e9e02844a9d98e524340b4 Mon Sep 17 00:00:00 2001 From: Christoph Wurst Date: Mon, 20 Jun 2016 10:41:23 +0200 Subject: [PATCH] fix unit test warning/errors --- lib/private/User/Session.php | 20 +++++++++----------- tests/lib/User/SessionTest.php | 33 +++++++++++++++++---------------- 2 files changed, 26 insertions(+), 27 deletions(-) diff --git a/lib/private/User/Session.php b/lib/private/User/Session.php index 07235c1b42..aedb308539 100644 --- a/lib/private/User/Session.php +++ b/lib/private/User/Session.php @@ -550,14 +550,12 @@ class Session implements IUserSession, Emitter { $pwd = $this->tokenProvider->getPassword($dbToken, $token); } catch (InvalidTokenException $ex) { // An invalid token password was used -> log user out - $this->logout(); return false; } catch (PasswordlessTokenException $ex) { // Token has no password if (!is_null($this->activeUser) && !$this->activeUser->isEnabled()) { $this->tokenProvider->invalidateToken($token); - $this->logout(); return false; } @@ -570,7 +568,6 @@ class Session implements IUserSession, Emitter { || (!is_null($this->activeUser) && !$this->activeUser->isEnabled())) { $this->tokenProvider->invalidateToken($token); // Password has changed or user was disabled -> log user out - $this->logout(); return false; } $dbToken->setLastCheck($now); @@ -613,20 +610,21 @@ class Session implements IUserSession, Emitter { if (strpos($authHeader, 'token ') === false) { // No auth header, let's try session id try { - $sessionId = $this->session->getId(); + $token = $this->session->getId(); } catch (SessionNotAvailableException $ex) { return false; } - - if (!$this->validateToken($sessionId)) { - return false; - } - - return $this->loginWithToken($sessionId); } else { $token = substr($authHeader, 6); - return $this->validateToken($token); } + + if (!$this->loginWithToken($token)) { + return false; + } + if(!$this->validateToken($token)) { + return false; + } + return true; } /** diff --git a/tests/lib/User/SessionTest.php b/tests/lib/User/SessionTest.php index 974b5d3fd8..6b72cf81bc 100644 --- a/tests/lib/User/SessionTest.php +++ b/tests/lib/User/SessionTest.php @@ -151,7 +151,7 @@ class SessionTest extends \Test\TestCase { $this->tokenProvider->expects($this->once()) ->method('getToken') ->with('bar') - ->will($this->throwException('\OC\Authentication\Exceptions\InvalidTokenException')); + ->will($this->throwException(new \OC\Authentication\Exceptions\InvalidTokenException())); $session->expects($this->exactly(2)) ->method('set') ->with($this->callback(function ($key) { @@ -698,9 +698,15 @@ class SessionTest extends \Test\TestCase { ->disableOriginalConstructor() ->getMock(); $session = new Memory(''); - $token = $this->getMock('\OC\Authentication\Token\IToken'); + $token = new \OC\Authentication\Token\DefaultToken(); + $token->setLoginName('fritz'); + $token->setUid('fritz0'); + $token->setLastCheck(100); // Needs check $user = $this->getMock('\OCP\IUser'); - $userSession = new \OC\User\Session($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config); + $userSession = $this->getMockBuilder('\OC\User\Session') + ->setMethods(['logout']) + ->setConstructorArgs([$manager, $session, $this->timeFactory, $this->tokenProvider, $this->config]) + ->getMock(); $request = $this->getMock('\OCP\IRequest'); $request->expects($this->once()) @@ -708,15 +714,12 @@ class SessionTest extends \Test\TestCase { ->with('Authorization') ->will($this->returnValue('token xxxxx')); $this->tokenProvider->expects($this->once()) - ->method('validateToken') + ->method('getToken') ->with('xxxxx') ->will($this->returnValue($token)); - $token->expects($this->once()) - ->method('getUID') - ->will($this->returnValue('user123')); $manager->expects($this->once()) ->method('get') - ->with('user123') + ->with('fritz0') ->will($this->returnValue($user)); $user->expects($this->once()) ->method('isEnabled') @@ -762,16 +765,14 @@ class SessionTest extends \Test\TestCase { $user->expects($this->once()) ->method('isEnabled') ->will($this->returnValue(false)); - $this->tokenProvider->expects($this->once()) - ->method('invalidateToken') - ->with($token); - $session->expects($this->once()) - ->method('logout'); $tokenProvider->expects($this->once()) - ->method('updateToken') - ->with($token); + ->method('invalidateToken') + ->with('APP-PASSWORD'); + $userSession->expects($this->once()) + ->method('logout'); - $this->invokePrivate($userSession, 'validateSession', [$user]); + $userSession->setUser($user); + $this->invokePrivate($userSession, 'validateSession'); } public function testValidateSessionNoPassword() {