2018-10-03 12:48:02 +03:00
|
|
|
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
/**
|
|
|
|
* @copyright Copyright (c) 2018, Roeland Jago Douma <roeland@famdouma.nl>
|
|
|
|
*
|
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Tests\Core\Controller;
|
|
|
|
|
|
|
|
use OC\Authentication\Token\IProvider;
|
|
|
|
use OC\Authentication\Token\IToken;
|
|
|
|
use OC\Core\Controller\AppPasswordController;
|
|
|
|
use OCP\AppFramework\OCS\OCSForbiddenException;
|
|
|
|
use OCP\Authentication\Exceptions\CredentialsUnavailableException;
|
|
|
|
use OCP\Authentication\Exceptions\PasswordUnavailableException;
|
|
|
|
use OCP\Authentication\LoginCredentials\ICredentials;
|
|
|
|
use OCP\Authentication\LoginCredentials\IStore;
|
|
|
|
use OCP\IRequest;
|
|
|
|
use OCP\ISession;
|
|
|
|
use OCP\Security\ISecureRandom;
|
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2019-02-18 19:38:38 +03:00
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
2018-10-03 12:48:02 +03:00
|
|
|
use Test\TestCase;
|
|
|
|
|
|
|
|
class AppPasswordControllerTest extends TestCase {
|
|
|
|
|
|
|
|
/** @var ISession|MockObject */
|
|
|
|
private $session;
|
|
|
|
|
|
|
|
/** @var ISecureRandom|MockObject */
|
|
|
|
private $random;
|
|
|
|
|
|
|
|
/** @var IProvider|MockObject */
|
|
|
|
private $tokenProvider;
|
|
|
|
|
|
|
|
/** @var IStore|MockObject */
|
|
|
|
private $credentialStore;
|
|
|
|
|
|
|
|
/** @var IRequest|MockObject */
|
|
|
|
private $request;
|
|
|
|
|
2019-02-18 19:38:38 +03:00
|
|
|
/** @var EventDispatcherInterface|\PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
private $eventDispatcher;
|
2019-02-18 00:45:05 +03:00
|
|
|
|
2018-10-03 12:48:02 +03:00
|
|
|
/** @var AppPasswordController */
|
|
|
|
private $controller;
|
|
|
|
|
|
|
|
public function setUp() {
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->session = $this->createMock(ISession::class);
|
|
|
|
$this->random = $this->createMock(ISecureRandom::class);
|
|
|
|
$this->tokenProvider = $this->createMock(IProvider::class);
|
|
|
|
$this->credentialStore = $this->createMock(IStore::class);
|
|
|
|
$this->request = $this->createMock(IRequest::class);
|
2019-02-18 19:38:38 +03:00
|
|
|
$this->eventDispatcher = $this->createMock(EventDispatcherInterface::class);
|
2018-10-03 12:48:02 +03:00
|
|
|
|
|
|
|
$this->controller = new AppPasswordController(
|
|
|
|
'core',
|
|
|
|
$this->request,
|
|
|
|
$this->session,
|
|
|
|
$this->random,
|
|
|
|
$this->tokenProvider,
|
2019-02-18 00:45:05 +03:00
|
|
|
$this->credentialStore,
|
2019-02-18 19:38:38 +03:00
|
|
|
$this->eventDispatcher
|
2018-10-03 12:48:02 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetAppPasswordWithAppPassword() {
|
|
|
|
$this->session->method('exists')
|
|
|
|
->with('app_password')
|
|
|
|
->willReturn(true);
|
|
|
|
|
|
|
|
$this->expectException(OCSForbiddenException::class);
|
|
|
|
|
|
|
|
$this->controller->getAppPassword();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetAppPasswordNoLoginCreds() {
|
|
|
|
$this->session->method('exists')
|
|
|
|
->with('app_password')
|
|
|
|
->willReturn(false);
|
|
|
|
$this->credentialStore->method('getLoginCredentials')
|
|
|
|
->willThrowException(new CredentialsUnavailableException());
|
|
|
|
|
|
|
|
$this->expectException(OCSForbiddenException::class);
|
|
|
|
|
|
|
|
$this->controller->getAppPassword();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetAppPassword() {
|
|
|
|
$credentials = $this->createMock(ICredentials::class);
|
|
|
|
|
|
|
|
$this->session->method('exists')
|
|
|
|
->with('app_password')
|
|
|
|
->willReturn(false);
|
|
|
|
$this->credentialStore->method('getLoginCredentials')
|
|
|
|
->willReturn($credentials);
|
|
|
|
$credentials->method('getUid')
|
|
|
|
->willReturn('myUID');
|
|
|
|
$credentials->method('getPassword')
|
|
|
|
->willReturn('myPassword');
|
|
|
|
$credentials->method('getLoginName')
|
|
|
|
->willReturn('myLoginName');
|
|
|
|
$this->request->method('getHeader')
|
|
|
|
->with('USER_AGENT')
|
|
|
|
->willReturn('myUA');
|
|
|
|
$this->random->method('generate')
|
|
|
|
->with(
|
|
|
|
72,
|
|
|
|
ISecureRandom::CHAR_UPPER.ISecureRandom::CHAR_LOWER.ISecureRandom::CHAR_DIGITS
|
|
|
|
)->willReturn('myToken');
|
|
|
|
|
|
|
|
$this->tokenProvider->expects($this->once())
|
|
|
|
->method('generateToken')
|
|
|
|
->with(
|
|
|
|
'myToken',
|
|
|
|
'myUID',
|
|
|
|
'myLoginName',
|
|
|
|
'myPassword',
|
|
|
|
'myUA',
|
|
|
|
IToken::PERMANENT_TOKEN,
|
|
|
|
IToken::DO_NOT_REMEMBER
|
|
|
|
);
|
|
|
|
|
2019-02-18 19:38:38 +03:00
|
|
|
$this->eventDispatcher->expects($this->once())
|
|
|
|
->method('dispatch');
|
2019-02-18 00:45:05 +03:00
|
|
|
|
2018-10-03 12:48:02 +03:00
|
|
|
$this->controller->getAppPassword();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetAppPasswordNoPassword() {
|
|
|
|
$credentials = $this->createMock(ICredentials::class);
|
|
|
|
|
|
|
|
$this->session->method('exists')
|
|
|
|
->with('app_password')
|
|
|
|
->willReturn(false);
|
|
|
|
$this->credentialStore->method('getLoginCredentials')
|
|
|
|
->willReturn($credentials);
|
|
|
|
$credentials->method('getUid')
|
|
|
|
->willReturn('myUID');
|
|
|
|
$credentials->method('getPassword')
|
|
|
|
->willThrowException(new PasswordUnavailableException());
|
|
|
|
$credentials->method('getLoginName')
|
|
|
|
->willReturn('myLoginName');
|
|
|
|
$this->request->method('getHeader')
|
|
|
|
->with('USER_AGENT')
|
|
|
|
->willReturn('myUA');
|
|
|
|
$this->random->method('generate')
|
|
|
|
->with(
|
|
|
|
72,
|
|
|
|
ISecureRandom::CHAR_UPPER.ISecureRandom::CHAR_LOWER.ISecureRandom::CHAR_DIGITS
|
|
|
|
)->willReturn('myToken');
|
|
|
|
|
|
|
|
$this->tokenProvider->expects($this->once())
|
|
|
|
->method('generateToken')
|
|
|
|
->with(
|
|
|
|
'myToken',
|
|
|
|
'myUID',
|
|
|
|
'myLoginName',
|
|
|
|
null,
|
|
|
|
'myUA',
|
|
|
|
IToken::PERMANENT_TOKEN,
|
|
|
|
IToken::DO_NOT_REMEMBER
|
|
|
|
);
|
|
|
|
|
2019-02-18 19:38:38 +03:00
|
|
|
$this->eventDispatcher->expects($this->once())
|
|
|
|
->method('dispatch');
|
2019-02-18 00:45:05 +03:00
|
|
|
|
2018-10-03 12:48:02 +03:00
|
|
|
$this->controller->getAppPassword();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|