diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/lib/user/session.php | 182 | ||||
-rw-r--r-- | tests/lib/util.php | 2 |
2 files changed, 180 insertions, 4 deletions
diff --git a/tests/lib/user/session.php b/tests/lib/user/session.php index 46b268b3624..2845a9c964a 100644 --- a/tests/lib/user/session.php +++ b/tests/lib/user/session.php @@ -67,7 +67,17 @@ class Session extends \PHPUnit_Framework_TestCase { }, 'foo')); - $manager = $this->getMock('\OC\User\Manager'); + $managerMethods = get_class_methods('\OC\User\Manager'); + //keep following methods intact in order to ensure hooks are + //working + $doNotMock = array('__construct', 'emit', 'listen'); + foreach($doNotMock as $methodName) { + $i = array_search($methodName, $managerMethods, true); + if($i !== false) { + unset($managerMethods[$i]); + } + } + $manager = $this->getMock('\OC\User\Manager', $managerMethods, array()); $backend = $this->getMock('OC_User_Dummy'); @@ -78,6 +88,8 @@ class Session extends \PHPUnit_Framework_TestCase { $user->expects($this->any()) ->method('getUID') ->will($this->returnValue('foo')); + $user->expects($this->once()) + ->method('updateLastLoginTimestamp'); $manager->expects($this->once()) ->method('checkPassword') @@ -94,7 +106,17 @@ class Session extends \PHPUnit_Framework_TestCase { $session->expects($this->never()) ->method('set'); - $manager = $this->getMock('\OC\User\Manager'); + $managerMethods = get_class_methods('\OC\User\Manager'); + //keep following methods intact in order to ensure hooks are + //working + $doNotMock = array('__construct', 'emit', 'listen'); + foreach($doNotMock as $methodName) { + $i = array_search($methodName, $managerMethods, true); + if($i !== false) { + unset($managerMethods[$i]); + } + } + $manager = $this->getMock('\OC\User\Manager', $managerMethods, array()); $backend = $this->getMock('OC_User_Dummy'); @@ -102,6 +124,8 @@ class Session extends \PHPUnit_Framework_TestCase { $user->expects($this->once()) ->method('isEnabled') ->will($this->returnValue(false)); + $user->expects($this->never()) + ->method('updateLastLoginTimestamp'); $manager->expects($this->once()) ->method('checkPassword') @@ -117,13 +141,25 @@ class Session extends \PHPUnit_Framework_TestCase { $session->expects($this->never()) ->method('set'); - $manager = $this->getMock('\OC\User\Manager'); + $managerMethods = get_class_methods('\OC\User\Manager'); + //keep following methods intact in order to ensure hooks are + //working + $doNotMock = array('__construct', 'emit', 'listen'); + foreach($doNotMock as $methodName) { + $i = array_search($methodName, $managerMethods, true); + if($i !== false) { + unset($managerMethods[$i]); + } + } + $manager = $this->getMock('\OC\User\Manager', $managerMethods, array()); $backend = $this->getMock('OC_User_Dummy'); $user = $this->getMock('\OC\User\User', array(), array('foo', $backend)); $user->expects($this->never()) ->method('isEnabled'); + $user->expects($this->never()) + ->method('updateLastLoginTimestamp'); $manager->expects($this->once()) ->method('checkPassword') @@ -151,4 +187,144 @@ class Session extends \PHPUnit_Framework_TestCase { $userSession = new \OC\User\Session($manager, $session); $userSession->login('foo', 'bar'); } + + public function testRememberLoginValidToken() { + $session = $this->getMock('\OC\Session\Memory', array(), array('')); + $session->expects($this->exactly(1)) + ->method('set') + ->with($this->callback(function($key) { + switch($key) { + case 'user_id': + return true; + default: + return false; + } + }, + 'foo')); + + $managerMethods = get_class_methods('\OC\User\Manager'); + //keep following methods intact in order to ensure hooks are + //working + $doNotMock = array('__construct', 'emit', 'listen'); + foreach($doNotMock as $methodName) { + $i = array_search($methodName, $managerMethods, true); + if($i !== false) { + unset($managerMethods[$i]); + } + } + $manager = $this->getMock('\OC\User\Manager', $managerMethods, array()); + + $backend = $this->getMock('OC_User_Dummy'); + + $user = $this->getMock('\OC\User\User', array(), array('foo', $backend)); + + $user->expects($this->any()) + ->method('getUID') + ->will($this->returnValue('foo')); + $user->expects($this->once()) + ->method('updateLastLoginTimestamp'); + + $manager->expects($this->once()) + ->method('get') + ->with('foo') + ->will($this->returnValue($user)); + + //prepare login token + $token = 'goodToken'; + \OC_Preferences::setValue('foo', 'login_token', $token, time()); + + $userSession = $this->getMock( + '\OC\User\Session', + //override, otherwise tests will fail because of setcookie() + array('setMagicInCookie'), + //there are passed as parameters to the constructor + array($manager, $session)); + + $granted = $userSession->loginWithCookie('foo', $token); + + $this->assertSame($granted, true); + } + + public function testRememberLoginInvalidToken() { + $session = $this->getMock('\OC\Session\Memory', array(), array('')); + $session->expects($this->never()) + ->method('set'); + + $managerMethods = get_class_methods('\OC\User\Manager'); + //keep following methods intact in order to ensure hooks are + //working + $doNotMock = array('__construct', 'emit', 'listen'); + foreach($doNotMock as $methodName) { + $i = array_search($methodName, $managerMethods, true); + if($i !== false) { + unset($managerMethods[$i]); + } + } + $manager = $this->getMock('\OC\User\Manager', $managerMethods, array()); + + $backend = $this->getMock('OC_User_Dummy'); + + $user = $this->getMock('\OC\User\User', array(), array('foo', $backend)); + + $user->expects($this->any()) + ->method('getUID') + ->will($this->returnValue('foo')); + $user->expects($this->never()) + ->method('updateLastLoginTimestamp'); + + $manager->expects($this->once()) + ->method('get') + ->with('foo') + ->will($this->returnValue($user)); + + //prepare login token + $token = 'goodToken'; + \OC_Preferences::setValue('foo', 'login_token', $token, time()); + + $userSession = new \OC\User\Session($manager, $session); + $granted = $userSession->loginWithCookie('foo', 'badToken'); + + $this->assertSame($granted, false); + } + + public function testRememberLoginInvalidUser() { + $session = $this->getMock('\OC\Session\Memory', array(), array('')); + $session->expects($this->never()) + ->method('set'); + + $managerMethods = get_class_methods('\OC\User\Manager'); + //keep following methods intact in order to ensure hooks are + //working + $doNotMock = array('__construct', 'emit', 'listen'); + foreach($doNotMock as $methodName) { + $i = array_search($methodName, $managerMethods, true); + if($i !== false) { + unset($managerMethods[$i]); + } + } + $manager = $this->getMock('\OC\User\Manager', $managerMethods, array()); + + $backend = $this->getMock('OC_User_Dummy'); + + $user = $this->getMock('\OC\User\User', array(), array('foo', $backend)); + + $user->expects($this->never()) + ->method('getUID'); + $user->expects($this->never()) + ->method('updateLastLoginTimestamp'); + + $manager->expects($this->once()) + ->method('get') + ->with('foo') + ->will($this->returnValue(null)); + + //prepare login token + $token = 'goodToken'; + \OC_Preferences::setValue('foo', 'login_token', $token, time()); + + $userSession = new \OC\User\Session($manager, $session); + $granted = $userSession->loginWithCookie('foo', $token); + + $this->assertSame($granted, false); + } } diff --git a/tests/lib/util.php b/tests/lib/util.php index 4dc7813d918..0bafb96cabd 100644 --- a/tests/lib/util.php +++ b/tests/lib/util.php @@ -95,7 +95,7 @@ class Test_Util extends PHPUnit_Framework_TestCase { function testGetDefaultEmailAddress() { $email = \OCP\Util::getDefaultEmailAddress("no-reply"); - $this->assertEquals('no-reply@localhost.localdomain', $email); + $this->assertEquals('no-reply@localhost', $email); } function testGetDefaultEmailAddressFromConfig() { |