2016-04-11 13:08:00 +03:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @author Lukas Reschke <lukas@owncloud.com>
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OC\Core\Controller;
|
|
|
|
|
|
|
|
use OCP\AppFramework\Http\RedirectResponse;
|
|
|
|
use OCP\AppFramework\Http\TemplateResponse;
|
|
|
|
use OCP\IConfig;
|
|
|
|
use OCP\IRequest;
|
|
|
|
use OCP\ISession;
|
2016-04-18 13:14:07 +03:00
|
|
|
use OCP\IURLGenerator;
|
2016-04-11 13:08:00 +03:00
|
|
|
use OCP\IUserManager;
|
|
|
|
use OCP\IUserSession;
|
|
|
|
use Test\TestCase;
|
|
|
|
|
|
|
|
class LoginControllerTest extends TestCase {
|
|
|
|
/** @var LoginController */
|
|
|
|
private $loginController;
|
|
|
|
/** @var IRequest */
|
|
|
|
private $request;
|
|
|
|
/** @var IUserManager */
|
|
|
|
private $userManager;
|
|
|
|
/** @var IConfig */
|
|
|
|
private $config;
|
|
|
|
/** @var ISession */
|
|
|
|
private $session;
|
|
|
|
/** @var IUserSession */
|
|
|
|
private $userSession;
|
2016-04-18 13:14:07 +03:00
|
|
|
/** @var IURLGenerator */
|
|
|
|
private $urlGenerator;
|
2016-04-11 13:08:00 +03:00
|
|
|
|
|
|
|
public function setUp() {
|
|
|
|
parent::setUp();
|
|
|
|
$this->request = $this->getMock('\\OCP\\IRequest');
|
|
|
|
$this->userManager = $this->getMock('\\OCP\\IUserManager');
|
|
|
|
$this->config = $this->getMock('\\OCP\\IConfig');
|
|
|
|
$this->session = $this->getMock('\\OCP\\ISession');
|
2016-04-27 17:44:51 +03:00
|
|
|
$this->userSession = $this->getMockBuilder('\\OC\\User\\Session')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2016-04-18 13:14:07 +03:00
|
|
|
$this->urlGenerator = $this->getMock('\\OCP\\IURLGenerator');
|
2016-04-11 13:08:00 +03:00
|
|
|
|
|
|
|
$this->loginController = new LoginController(
|
|
|
|
'core',
|
|
|
|
$this->request,
|
|
|
|
$this->userManager,
|
|
|
|
$this->config,
|
|
|
|
$this->session,
|
2016-04-18 13:14:07 +03:00
|
|
|
$this->userSession,
|
|
|
|
$this->urlGenerator
|
2016-04-11 13:08:00 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-04-18 13:14:07 +03:00
|
|
|
public function testLogoutWithoutToken() {
|
|
|
|
$this->request
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getCookie')
|
|
|
|
->with('oc_token')
|
|
|
|
->willReturn(null);
|
|
|
|
$this->config
|
|
|
|
->expects($this->never())
|
|
|
|
->method('deleteUserValue');
|
|
|
|
$this->urlGenerator
|
|
|
|
->expects($this->once())
|
|
|
|
->method('linkToRouteAbsolute')
|
|
|
|
->with('core.login.showLoginForm')
|
|
|
|
->willReturn('/login');
|
|
|
|
|
|
|
|
$expected = new RedirectResponse('/login');
|
|
|
|
$this->assertEquals($expected, $this->loginController->logout());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testLogoutWithToken() {
|
|
|
|
$this->request
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getCookie')
|
|
|
|
->with('oc_token')
|
|
|
|
->willReturn('MyLoginToken');
|
|
|
|
$user = $this->getMock('\\OCP\\IUser');
|
|
|
|
$user
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getUID')
|
|
|
|
->willReturn('JohnDoe');
|
|
|
|
$this->userSession
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getUser')
|
|
|
|
->willReturn($user);
|
|
|
|
$this->config
|
|
|
|
->expects($this->once())
|
|
|
|
->method('deleteUserValue')
|
|
|
|
->with('JohnDoe', 'login_token', 'MyLoginToken');
|
|
|
|
$this->urlGenerator
|
|
|
|
->expects($this->once())
|
|
|
|
->method('linkToRouteAbsolute')
|
|
|
|
->with('core.login.showLoginForm')
|
|
|
|
->willReturn('/login');
|
|
|
|
|
|
|
|
$expected = new RedirectResponse('/login');
|
|
|
|
$this->assertEquals($expected, $this->loginController->logout());
|
|
|
|
}
|
|
|
|
|
2016-04-11 13:08:00 +03:00
|
|
|
public function testShowLoginFormForLoggedInUsers() {
|
|
|
|
$this->userSession
|
|
|
|
->expects($this->once())
|
|
|
|
->method('isLoggedIn')
|
|
|
|
->willReturn(true);
|
|
|
|
|
|
|
|
$expectedResponse = new RedirectResponse(\OC_Util::getDefaultPageUrl());
|
|
|
|
$this->assertEquals($expectedResponse, $this->loginController->showLoginForm('', '', ''));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testShowLoginFormWithErrorsInSession() {
|
|
|
|
$this->userSession
|
|
|
|
->expects($this->once())
|
|
|
|
->method('isLoggedIn')
|
|
|
|
->willReturn(false);
|
|
|
|
$this->session
|
|
|
|
->expects($this->once())
|
|
|
|
->method('get')
|
|
|
|
->with('loginMessages')
|
|
|
|
->willReturn(
|
|
|
|
[
|
|
|
|
[
|
|
|
|
'ErrorArray1',
|
|
|
|
'ErrorArray2',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'MessageArray1',
|
|
|
|
'MessageArray2',
|
|
|
|
],
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
$expectedResponse = new TemplateResponse(
|
|
|
|
'core',
|
|
|
|
'login',
|
|
|
|
[
|
|
|
|
'ErrorArray1' => true,
|
|
|
|
'ErrorArray2' => true,
|
|
|
|
'messages' => [
|
|
|
|
'MessageArray1',
|
|
|
|
'MessageArray2',
|
|
|
|
],
|
2016-04-15 20:02:19 +03:00
|
|
|
'loginName' => '',
|
2016-04-11 13:08:00 +03:00
|
|
|
'user_autofocus' => true,
|
|
|
|
'canResetPassword' => true,
|
|
|
|
'alt_login' => [],
|
|
|
|
'rememberLoginAllowed' => \OC_Util::rememberLoginAllowed(),
|
|
|
|
'rememberLoginState' => 0,
|
|
|
|
],
|
|
|
|
'guest'
|
|
|
|
);
|
|
|
|
$this->assertEquals($expectedResponse, $this->loginController->showLoginForm('', '', ''));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function passwordResetDataProvider() {
|
|
|
|
return [
|
|
|
|
[
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
],
|
|
|
|
[
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider passwordResetDataProvider
|
|
|
|
*/
|
|
|
|
public function testShowLoginFormWithPasswordResetOption($canChangePassword,
|
|
|
|
$expectedResult) {
|
|
|
|
$this->userSession
|
|
|
|
->expects($this->once())
|
|
|
|
->method('isLoggedIn')
|
|
|
|
->willReturn(false);
|
|
|
|
$this->config
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getSystemValue')
|
|
|
|
->with('lost_password_link')
|
|
|
|
->willReturn(false);
|
|
|
|
$user = $this->getMock('\\OCP\\IUser');
|
|
|
|
$user
|
|
|
|
->expects($this->once())
|
|
|
|
->method('canChangePassword')
|
|
|
|
->willReturn($canChangePassword);
|
|
|
|
$this->userManager
|
|
|
|
->expects($this->once())
|
|
|
|
->method('get')
|
|
|
|
->with('LdapUser')
|
|
|
|
->willReturn($user);
|
|
|
|
|
|
|
|
$expectedResponse = new TemplateResponse(
|
|
|
|
'core',
|
|
|
|
'login',
|
|
|
|
[
|
|
|
|
'messages' => [],
|
2016-04-15 20:02:19 +03:00
|
|
|
'loginName' => 'LdapUser',
|
2016-04-11 13:08:00 +03:00
|
|
|
'user_autofocus' => false,
|
|
|
|
'canResetPassword' => $expectedResult,
|
|
|
|
'alt_login' => [],
|
|
|
|
'rememberLoginAllowed' => \OC_Util::rememberLoginAllowed(),
|
|
|
|
'rememberLoginState' => 0,
|
|
|
|
],
|
|
|
|
'guest'
|
|
|
|
);
|
|
|
|
$this->assertEquals($expectedResponse, $this->loginController->showLoginForm('LdapUser', '', ''));
|
|
|
|
}
|
2016-04-15 19:57:11 +03:00
|
|
|
|
|
|
|
public function testShowLoginFormForUserNamedNull() {
|
|
|
|
$this->userSession
|
|
|
|
->expects($this->once())
|
|
|
|
->method('isLoggedIn')
|
|
|
|
->willReturn(false);
|
|
|
|
$this->config
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getSystemValue')
|
|
|
|
->with('lost_password_link')
|
|
|
|
->willReturn(false);
|
|
|
|
$user = $this->getMock('\\OCP\\IUser');
|
|
|
|
$user
|
|
|
|
->expects($this->once())
|
|
|
|
->method('canChangePassword')
|
|
|
|
->willReturn(false);
|
|
|
|
$this->userManager
|
|
|
|
->expects($this->once())
|
|
|
|
->method('get')
|
|
|
|
->with('0')
|
|
|
|
->willReturn($user);
|
|
|
|
|
|
|
|
$expectedResponse = new TemplateResponse(
|
|
|
|
'core',
|
|
|
|
'login',
|
|
|
|
[
|
|
|
|
'messages' => [],
|
2016-04-15 20:02:19 +03:00
|
|
|
'loginName' => '0',
|
2016-04-15 19:57:11 +03:00
|
|
|
'user_autofocus' => false,
|
|
|
|
'canResetPassword' => false,
|
|
|
|
'alt_login' => [],
|
|
|
|
'rememberLoginAllowed' => \OC_Util::rememberLoginAllowed(),
|
|
|
|
'rememberLoginState' => 0,
|
|
|
|
],
|
|
|
|
'guest'
|
|
|
|
);
|
|
|
|
$this->assertEquals($expectedResponse, $this->loginController->showLoginForm('0', '', ''));
|
|
|
|
}
|
2016-04-27 17:44:51 +03:00
|
|
|
|
|
|
|
public function testLoginWithInvalidCredentials() {
|
2016-05-09 16:33:56 +03:00
|
|
|
$user = $this->getMock('\OCP\IUser');
|
2016-04-27 17:44:51 +03:00
|
|
|
$password = 'secret';
|
|
|
|
$loginPageUrl = 'some url';
|
|
|
|
|
|
|
|
$this->userManager->expects($this->once())
|
|
|
|
->method('checkPassword')
|
|
|
|
->will($this->returnValue(false));
|
|
|
|
$this->urlGenerator->expects($this->once())
|
|
|
|
->method('linkToRoute')
|
2016-04-28 12:59:26 +03:00
|
|
|
->with('core.login.showLoginForm')
|
2016-04-27 17:44:51 +03:00
|
|
|
->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() {
|
2016-05-09 16:33:56 +03:00
|
|
|
$user = $this->getMock('\OCP\IUser');
|
2016-04-27 17:44:51 +03:00
|
|
|
$password = 'secret';
|
|
|
|
$indexPageUrl = 'some url';
|
|
|
|
|
|
|
|
$this->userManager->expects($this->once())
|
|
|
|
->method('checkPassword')
|
2016-05-09 16:33:56 +03:00
|
|
|
->will($this->returnValue($user));
|
2016-04-27 17:44:51 +03:00
|
|
|
$this->userSession->expects($this->once())
|
|
|
|
->method('createSessionToken')
|
2016-05-09 16:33:56 +03:00
|
|
|
->with($this->request, $user->getUID(), $password);
|
2016-04-27 17:44:51 +03:00
|
|
|
$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() {
|
2016-05-09 16:33:56 +03:00
|
|
|
$user = $this->getMock('\OCP\IUser');
|
|
|
|
$user->expects($this->any())
|
|
|
|
->method('getUID')
|
|
|
|
->will($this->returnValue('jane'));
|
2016-04-27 17:44:51 +03:00
|
|
|
$password = 'secret';
|
|
|
|
$originalUrl = 'another%20url';
|
|
|
|
$redirectUrl = 'http://localhost/another url';
|
|
|
|
|
|
|
|
$this->userManager->expects($this->once())
|
|
|
|
->method('checkPassword')
|
2016-05-09 16:33:56 +03:00
|
|
|
->with('jane', $password)
|
|
|
|
->will($this->returnValue($user));
|
2016-04-27 17:44:51 +03:00
|
|
|
$this->userSession->expects($this->once())
|
|
|
|
->method('createSessionToken')
|
2016-05-09 16:33:56 +03:00
|
|
|
->with($this->request, $user->getUID(), $password);
|
2016-04-27 17:44:51 +03:00
|
|
|
$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));
|
2016-05-09 16:33:56 +03:00
|
|
|
$this->assertEquals($expected, $this->loginController->tryLogin($user->getUID(), $password, $originalUrl));
|
2016-04-27 17:44:51 +03:00
|
|
|
}
|
|
|
|
|
2016-04-11 13:08:00 +03:00
|
|
|
}
|