2016-11-08 11:15:02 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @copyright 2016 Christoph Wurst <christoph@winzerhof-wurst.at>
|
|
|
|
*
|
|
|
|
* @author 2016 Christoph Wurst <christoph@winzerhof-wurst.at>
|
|
|
|
*
|
|
|
|
* @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 Test\Authentication\LoginCredentials;
|
|
|
|
|
|
|
|
use OC\Authentication\Exceptions\InvalidTokenException;
|
|
|
|
use OC\Authentication\Exceptions\PasswordlessTokenException;
|
|
|
|
use OC\Authentication\LoginCredentials\Credentials;
|
|
|
|
use OC\Authentication\LoginCredentials\Store;
|
|
|
|
use OC\Authentication\Token\IProvider;
|
|
|
|
use OC\Authentication\Token\IToken;
|
|
|
|
use OCP\Authentication\Exceptions\CredentialsUnavailableException;
|
|
|
|
use OCP\ILogger;
|
|
|
|
use OCP\ISession;
|
|
|
|
use OCP\Session\Exceptions\SessionNotAvailableException;
|
2017-01-02 12:04:55 +03:00
|
|
|
use PHPUnit_Framework_MockObject_MockObject;
|
2016-11-08 11:15:02 +03:00
|
|
|
use Test\TestCase;
|
|
|
|
|
|
|
|
class StoreTest extends TestCase {
|
|
|
|
|
|
|
|
/** @var ISession|PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
private $session;
|
|
|
|
|
|
|
|
/** @var IProvider|PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
private $tokenProvider;
|
|
|
|
|
|
|
|
/** @var ILogger|PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
private $logger;
|
|
|
|
|
|
|
|
/** @var Store */
|
|
|
|
private $store;
|
|
|
|
|
2019-11-21 18:40:38 +03:00
|
|
|
protected function setUp(): void {
|
2016-11-08 11:15:02 +03:00
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->session = $this->createMock(ISession::class);
|
|
|
|
$this->tokenProvider = $this->createMock(IProvider::class);
|
|
|
|
$this->logger = $this->createMock(ILogger::class);
|
|
|
|
|
2017-01-02 13:57:05 +03:00
|
|
|
$this->store = new Store($this->session, $this->logger, $this->tokenProvider);
|
2016-11-08 11:15:02 +03:00
|
|
|
}
|
|
|
|
|
2017-01-02 12:04:55 +03:00
|
|
|
public function testAuthenticate() {
|
|
|
|
$params = [
|
|
|
|
'run' => true,
|
|
|
|
'uid' => 'user123',
|
|
|
|
'password' => 123456,
|
|
|
|
];
|
|
|
|
|
|
|
|
$this->session->expects($this->once())
|
|
|
|
->method('set')
|
|
|
|
->with($this->equalTo('login_credentials'), $this->equalTo(json_encode($params)));
|
|
|
|
|
|
|
|
$this->store->authenticate($params);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSetSession() {
|
|
|
|
$session = $this->createMock(ISession::class);
|
|
|
|
|
|
|
|
$this->store->setSession($session);
|
2018-01-25 13:23:12 +03:00
|
|
|
$this->addToAssertionCount(1);
|
2017-01-02 12:04:55 +03:00
|
|
|
}
|
|
|
|
|
2017-01-02 13:57:05 +03:00
|
|
|
public function testGetLoginCredentialsNoTokenProvider() {
|
|
|
|
$this->store = new Store($this->session, $this->logger, null);
|
|
|
|
|
|
|
|
$this->expectException(CredentialsUnavailableException::class);
|
|
|
|
|
|
|
|
$this->store->getLoginCredentials();
|
|
|
|
}
|
|
|
|
|
2016-11-08 11:15:02 +03:00
|
|
|
public function testGetLoginCredentials() {
|
|
|
|
$uid = 'uid';
|
|
|
|
$user = 'user123';
|
|
|
|
$password = 'passme';
|
|
|
|
$token = $this->createMock(IToken::class);
|
|
|
|
$this->session->expects($this->once())
|
|
|
|
->method('getId')
|
2017-01-02 12:04:55 +03:00
|
|
|
->willReturn('sess2233');
|
2016-11-08 11:15:02 +03:00
|
|
|
$this->tokenProvider->expects($this->once())
|
|
|
|
->method('getToken')
|
|
|
|
->with('sess2233')
|
2017-01-02 12:04:55 +03:00
|
|
|
->willReturn($token);
|
2016-11-08 11:15:02 +03:00
|
|
|
$token->expects($this->once())
|
|
|
|
->method('getUID')
|
2017-01-02 12:04:55 +03:00
|
|
|
->willReturn($uid);
|
2016-11-08 11:15:02 +03:00
|
|
|
$token->expects($this->once())
|
|
|
|
->method('getLoginName')
|
2017-01-02 12:04:55 +03:00
|
|
|
->willReturn($user);
|
2016-11-08 11:15:02 +03:00
|
|
|
$this->tokenProvider->expects($this->once())
|
|
|
|
->method('getPassword')
|
|
|
|
->with($token, 'sess2233')
|
2017-01-02 12:04:55 +03:00
|
|
|
->willReturn($password);
|
2016-11-08 11:15:02 +03:00
|
|
|
$expected = new Credentials($uid, $user, $password);
|
|
|
|
|
|
|
|
$creds = $this->store->getLoginCredentials();
|
|
|
|
|
|
|
|
$this->assertEquals($expected, $creds);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetLoginCredentialsSessionNotAvailable() {
|
|
|
|
$this->session->expects($this->once())
|
|
|
|
->method('getId')
|
|
|
|
->will($this->throwException(new SessionNotAvailableException()));
|
|
|
|
$this->expectException(CredentialsUnavailableException::class);
|
|
|
|
|
|
|
|
$this->store->getLoginCredentials();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetLoginCredentialsInvalidToken() {
|
|
|
|
$this->session->expects($this->once())
|
|
|
|
->method('getId')
|
2017-01-02 12:04:55 +03:00
|
|
|
->willReturn('sess2233');
|
2016-11-08 11:15:02 +03:00
|
|
|
$this->tokenProvider->expects($this->once())
|
|
|
|
->method('getToken')
|
|
|
|
->with('sess2233')
|
|
|
|
->will($this->throwException(new InvalidTokenException()));
|
|
|
|
$this->expectException(CredentialsUnavailableException::class);
|
|
|
|
|
|
|
|
$this->store->getLoginCredentials();
|
|
|
|
}
|
2017-01-02 12:04:55 +03:00
|
|
|
|
|
|
|
public function testGetLoginCredentialsInvalidTokenLoginCredentials() {
|
2020-06-07 16:41:09 +03:00
|
|
|
$uid = 'id987';
|
|
|
|
$user = 'user987';
|
2017-01-02 12:04:55 +03:00
|
|
|
$password = '7389374';
|
|
|
|
|
|
|
|
$this->session->expects($this->once())
|
|
|
|
->method('getId')
|
|
|
|
->willReturn('sess2233');
|
|
|
|
$this->tokenProvider->expects($this->once())
|
|
|
|
->method('getToken')
|
|
|
|
->with('sess2233')
|
|
|
|
->will($this->throwException(new InvalidTokenException()));
|
|
|
|
$this->session->expects($this->once())
|
|
|
|
->method('exists')
|
|
|
|
->with($this->equalTo('login_credentials'))
|
|
|
|
->willReturn(true);
|
2020-06-07 16:41:09 +03:00
|
|
|
$this->session->expects($this->exactly(2))
|
2017-01-02 12:04:55 +03:00
|
|
|
->method('get')
|
2020-06-07 16:41:09 +03:00
|
|
|
->withConsecutive(['login_credentials'], ['loginname'])
|
|
|
|
->willReturnOnConsecutiveCalls('{"run":true,"uid":"id987","password":"7389374"}', $user);
|
|
|
|
$expected = new Credentials($uid, $user, $password);
|
2017-01-02 12:04:55 +03:00
|
|
|
|
|
|
|
$actual = $this->store->getLoginCredentials();
|
|
|
|
|
|
|
|
$this->assertEquals($expected, $actual);
|
|
|
|
}
|
|
|
|
|
2016-11-08 11:15:02 +03:00
|
|
|
public function testGetLoginCredentialsPasswordlessToken() {
|
|
|
|
$this->session->expects($this->once())
|
|
|
|
->method('getId')
|
2017-01-02 12:04:55 +03:00
|
|
|
->willReturn('sess2233');
|
2016-11-08 11:15:02 +03:00
|
|
|
$this->tokenProvider->expects($this->once())
|
|
|
|
->method('getToken')
|
|
|
|
->with('sess2233')
|
|
|
|
->will($this->throwException(new PasswordlessTokenException()));
|
|
|
|
$this->expectException(CredentialsUnavailableException::class);
|
|
|
|
|
|
|
|
$this->store->getLoginCredentials();
|
|
|
|
}
|
|
|
|
}
|