summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChristoph Wurst <christoph@owncloud.com>2016-04-27 16:44:51 +0200
committerThomas Müller <thomas.mueller@tmit.eu>2016-05-11 13:36:46 +0200
commitaafd660b9777bd4e3434dd1894ce61717fb541fc (patch)
tree439f58ea60570f6196c0fc3f36180368cd2ba33f
parent7aa16e1559e8ef1121ac2529090a6881b4d919d5 (diff)
downloadnextcloud-server-aafd660b9777bd4e3434dd1894ce61717fb541fc.tar.gz
nextcloud-server-aafd660b9777bd4e3434dd1894ce61717fb541fc.zip
fix LoginController unit tests
-rw-r--r--core/Controller/LoginController.php2
-rw-r--r--tests/core/controller/LoginControllerTest.php70
2 files changed, 70 insertions, 2 deletions
diff --git a/core/Controller/LoginController.php b/core/Controller/LoginController.php
index ba9fc55d451..63ea7babaf1 100644
--- a/core/Controller/LoginController.php
+++ b/core/Controller/LoginController.php
@@ -181,7 +181,7 @@ class LoginController extends Controller {
}
$this->userSession->createSessionToken($this->request, $user, $password);
if (!is_null($redirect_url) && $this->userSession->isLoggedIn()) {
- $location = OC::$server->getURLGenerator()->getAbsoluteURL(urldecode($redirect_url));
+ $location = $this->urlGenerator->getAbsoluteURL(urldecode($redirect_url));
// Deny the redirect if the URL contains a @
// This prevents unvalidated redirects like ?redirect_url=:user@domain.com
if (strpos($location, '@') === false) {
diff --git a/tests/core/controller/LoginControllerTest.php b/tests/core/controller/LoginControllerTest.php
index f9a6080892b..93e2f517179 100644
--- a/tests/core/controller/LoginControllerTest.php
+++ b/tests/core/controller/LoginControllerTest.php
@@ -53,7 +53,9 @@ class LoginControllerTest extends TestCase {
$this->userManager = $this->getMock('\\OCP\\IUserManager');
$this->config = $this->getMock('\\OCP\\IConfig');
$this->session = $this->getMock('\\OCP\\ISession');
- $this->userSession = $this->getMock('\\OCP\\IUserSession');
+ $this->userSession = $this->getMockBuilder('\\OC\\User\\Session')
+ ->disableOriginalConstructor()
+ ->getMock();
$this->urlGenerator = $this->getMock('\\OCP\\IURLGenerator');
$this->loginController = new LoginController(
@@ -264,4 +266,70 @@ class LoginControllerTest extends TestCase {
);
$this->assertEquals($expectedResponse, $this->loginController->showLoginForm('0', '', ''));
}
+
+ public function testLoginWithInvalidCredentials() {
+ $user = 'jane';
+ $password = 'secret';
+ $loginPageUrl = 'some url';
+
+ $this->userManager->expects($this->once())
+ ->method('checkPassword')
+ ->will($this->returnValue(false));
+ $this->urlGenerator->expects($this->once())
+ ->method('linkToRoute')
+ ->with('login#showLoginForm')
+ ->will($this->returnValue($loginPageUrl));
+
+ $this->userSession->expects($this->never())
+ ->method('createSessionToken');
+
+ $expected = new \OCP\AppFramework\Http\RedirectResponse($loginPageUrl);
+ $this->assertEquals($expected, $this->loginController->tryLogin($user, $password, ''));
+ }
+
+ public function testLoginWithValidCredentials() {
+ $user = 'jane';
+ $password = 'secret';
+ $indexPageUrl = 'some url';
+
+ $this->userManager->expects($this->once())
+ ->method('checkPassword')
+ ->will($this->returnValue(true));
+ $this->userSession->expects($this->once())
+ ->method('createSessionToken')
+ ->with($this->request, $user, $password);
+ $this->urlGenerator->expects($this->once())
+ ->method('linkTo')
+ ->with('files', 'index')
+ ->will($this->returnValue($indexPageUrl));
+
+ $expected = new \OCP\AppFramework\Http\RedirectResponse($indexPageUrl);
+ $this->assertEquals($expected, $this->loginController->tryLogin($user, $password, null));
+ }
+
+ public function testLoginWithValidCredentialsAndRedirectUrl() {
+ $user = 'jane';
+ $password = 'secret';
+ $originalUrl = 'another%20url';
+ $redirectUrl = 'http://localhost/another url';
+
+ $this->userManager->expects($this->once())
+ ->method('checkPassword')
+ ->will($this->returnValue(true));
+ $this->userSession->expects($this->once())
+ ->method('createSessionToken')
+ ->with($this->request, $user, $password);
+ $this->userSession->expects($this->once())
+ ->method('isLoggedIn')
+ ->with()
+ ->will($this->returnValue(true));
+ $this->urlGenerator->expects($this->once())
+ ->method('getAbsoluteURL')
+ ->with(urldecode($originalUrl))
+ ->will($this->returnValue($redirectUrl));
+
+ $expected = new \OCP\AppFramework\Http\RedirectResponse(urldecode($redirectUrl));
+ $this->assertEquals($expected, $this->loginController->tryLogin($user, $password, $originalUrl));
+ }
+
}