Browse Source

Add missing tests and fix PHPDoc

Signed-off-by: Lukas Reschke <lukas@statuscode.ch>
tags/v11.0RC2
Lukas Reschke 7 years ago
parent
commit
9d6e01ef40
No account linked to committer's email address

+ 8
- 1
lib/private/Authentication/Token/DefaultTokenProvider.php View File

<?php <?php
/** /**
* @copyright Copyright (c) 2016, ownCloud, Inc. * @copyright Copyright (c) 2016, ownCloud, Inc.
* @copyright Copyright (c) 2016, Christoph Wurst <christoph@winzerhof-wurst.at>
* *
* @author Christoph Wurst <christoph@owncloud.com> * @author Christoph Wurst <christoph@owncloud.com>
* *
* @param ILogger $logger * @param ILogger $logger
* @param ITimeFactory $time * @param ITimeFactory $time
*/ */
public function __construct(DefaultTokenMapper $mapper, ICrypto $crypto, IConfig $config, ILogger $logger, ITimeFactory $time) {
public function __construct(DefaultTokenMapper $mapper,
ICrypto $crypto,
IConfig $config,
ILogger $logger,
ITimeFactory $time) {
$this->mapper = $mapper; $this->mapper = $mapper;
$this->crypto = $crypto; $this->crypto = $crypto;
$this->config = $config; $this->config = $config;
* Save the updated token * Save the updated token
* *
* @param IToken $token * @param IToken $token
* @throws InvalidTokenException
*/ */
public function updateToken(IToken $token) { public function updateToken(IToken $token) {
if (!($token instanceof DefaultToken)) { if (!($token instanceof DefaultToken)) {
/** /**
* @param string $oldSessionId * @param string $oldSessionId
* @param string $sessionId * @param string $sessionId
* @throws InvalidTokenException
*/ */
public function renewSessionToken($oldSessionId, $sessionId) { public function renewSessionToken($oldSessionId, $sessionId) {
$token = $this->getToken($oldSessionId); $token = $this->getToken($oldSessionId);

+ 1
- 0
lib/private/Authentication/Token/IProvider.php View File

/** /**
* @param string $oldSessionId * @param string $oldSessionId
* @param string $sessionId * @param string $sessionId
* @throws InvalidTokenException
*/ */
public function renewSessionToken($oldSessionId, $sessionId); public function renewSessionToken($oldSessionId, $sessionId);



+ 116
- 2
tests/lib/Authentication/Token/DefaultTokenProviderTest.php View File

<?php <?php

/** /**
* @author Christoph Wurst <christoph@owncloud.com> * @author Christoph Wurst <christoph@owncloud.com>
* *
* @copyright Copyright (c) 2016, Lukas Reschke <lukas@statuscode.ch>
* @copyright Copyright (c) 2016, ownCloud, Inc. * @copyright Copyright (c) 2016, ownCloud, Inc.
* @license AGPL-3.0 * @license AGPL-3.0
* *
use OC\Authentication\Token\DefaultToken; use OC\Authentication\Token\DefaultToken;
use OC\Authentication\Token\DefaultTokenProvider; use OC\Authentication\Token\DefaultTokenProvider;
use OC\Authentication\Token\IToken; use OC\Authentication\Token\IToken;
use OCP\AppFramework\Db\Mapper;
use OCP\AppFramework\Utility\ITimeFactory; use OCP\AppFramework\Utility\ITimeFactory;
use OCP\IConfig; use OCP\IConfig;
use OCP\ILogger; use OCP\ILogger;


class DefaultTokenProviderTest extends TestCase { class DefaultTokenProviderTest extends TestCase {


/** @var DefaultTokenProvider */
/** @var DefaultTokenProvider|\PHPUnit_Framework_MockObject_MockObject */
private $tokenProvider; private $tokenProvider;
/** @var Mapper|\PHPUnit_Framework_MockObject_MockObject */
private $mapper; private $mapper;
/** @var ICrypto|\PHPUnit_Framework_MockObject_MockObject */
private $crypto; private $crypto;
/** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */
private $config; private $config;
/** @var ILogger|\PHPUnit_Framework_MockObject_MockObject */
private $logger; private $logger;
/** @var ITimeFactory|\PHPUnit_Framework_MockObject_MockObject */
private $timeFactory; private $timeFactory;
/** @var int */
private $time; private $time;


protected function setUp() { protected function setUp() {
$this->tokenProvider->invalidateOldTokens(); $this->tokenProvider->invalidateOldTokens();
} }


public function testRenewSessionTokenWithoutPassword() {
$token = $this->getMockBuilder(DefaultToken::class)
->disableOriginalConstructor()
->setMethods(['getUID', 'getLoginName', 'getPassword', 'getName'])
->getMock();
$token
->expects($this->at(0))
->method('getUID')
->willReturn('UserUid');
$token
->expects($this->at(1))
->method('getLoginName')
->willReturn('UserLoginName');
$token
->expects($this->at(2))
->method('getPassword')
->willReturn(null);
$token
->expects($this->at(3))
->method('getName')
->willReturn('MyTokenName');
$this->config
->expects($this->exactly(2))
->method('getSystemValue')
->with('secret')
->willReturn('MyInstanceSecret');
$this->mapper
->expects($this->at(0))
->method('getToken')
->with(hash('sha512', 'oldId' . 'MyInstanceSecret'))
->willReturn($token);
$newToken = new DefaultToken();
$newToken->setUid('UserUid');
$newToken->setLoginName('UserLoginName');
$newToken->setName('MyTokenName');
$newToken->setToken(hash('sha512', 'newId' . 'MyInstanceSecret'));
$newToken->setType(IToken::TEMPORARY_TOKEN);
$newToken->setLastActivity(1313131);
$this->mapper
->expects($this->at(1))
->method('insert')
->with($newToken);

$this->tokenProvider->renewSessionToken('oldId', 'newId');
}

public function testRenewSessionTokenWithPassword() {
$token = $this->getMockBuilder(DefaultToken::class)
->disableOriginalConstructor()
->setMethods(['getUID', 'getLoginName', 'getPassword', 'getName'])
->getMock();
$token
->expects($this->at(0))
->method('getUID')
->willReturn('UserUid');
$token
->expects($this->at(1))
->method('getLoginName')
->willReturn('UserLoginName');
$token
->expects($this->at(2))
->method('getPassword')
->willReturn('EncryptedPassword');
$token
->expects($this->at(3))
->method('getPassword')
->willReturn('EncryptedPassword');
$token
->expects($this->at(4))
->method('getName')
->willReturn('MyTokenName');
$this->crypto
->expects($this->any(0))
->method('decrypt')
->with('EncryptedPassword', 'oldIdMyInstanceSecret')
->willReturn('ClearTextPassword');
$this->crypto
->expects($this->any(1))
->method('encrypt')
->with('ClearTextPassword', 'newIdMyInstanceSecret')
->willReturn('EncryptedPassword');
$this->config
->expects($this->exactly(4))
->method('getSystemValue')
->with('secret')
->willReturn('MyInstanceSecret');
$this->mapper
->expects($this->at(0))
->method('getToken')
->with(hash('sha512', 'oldId' . 'MyInstanceSecret'))
->willReturn($token);
$newToken = new DefaultToken();
$newToken->setUid('UserUid');
$newToken->setLoginName('UserLoginName');
$newToken->setName('MyTokenName');
$newToken->setToken(hash('sha512', 'newId' . 'MyInstanceSecret'));
$newToken->setType(IToken::TEMPORARY_TOKEN);
$newToken->setLastActivity(1313131);
$newToken->setPassword('EncryptedPassword');
$this->mapper
->expects($this->at(1))
->method('insert')
->with($newToken);

$this->tokenProvider->renewSessionToken('oldId', 'newId');
}

} }

+ 44
- 1
tests/lib/User/SessionTest.php View File

<?php <?php

/** /**
* Copyright (c) 2013 Robin Appelman <icewind@owncloud.com> * Copyright (c) 2013 Robin Appelman <icewind@owncloud.com>
* This file is licensed under the Affero General Public License version 3 or * This file is licensed under the Affero General Public License version 3 or
private $throttler; private $throttler;
/** @var ISecureRandom|\PHPUnit_Framework_MockObject_MockObject */ /** @var ISecureRandom|\PHPUnit_Framework_MockObject_MockObject */
private $random; private $random;
/** @var IUserManager|\PHPUnit_Framework_MockObject_MockObject */
private $manager;
/** @var ISession|\PHPUnit_Framework_MockObject_MockObject */
private $session;
/** @var Session|\PHPUnit_Framework_MockObject_MockObject */
private $userSession;


protected function setUp() { protected function setUp() {
parent::setUp(); parent::setUp();
$this->config = $this->createMock(IConfig::class); $this->config = $this->createMock(IConfig::class);
$this->throttler = $this->createMock(Throttler::class); $this->throttler = $this->createMock(Throttler::class);
$this->random = $this->createMock(ISecureRandom::class); $this->random = $this->createMock(ISecureRandom::class);
$this->manager = $this->createMock(IUserManager::class);
$this->session = $this->createMock(ISession::class);
$this->userSession = $this->getMockBuilder(Session::class)
->setConstructorArgs([
$this->manager,
$this->session,
$this->timeFactory,
$this->tokenProvider,
$this->config,
$this->random,
])
->setMethods([
'setMagicInCookie',
])
->getMock();


\OC_User::setIncognitoMode(false); \OC_User::setIncognitoMode(false);
} }


$userSession->logClientIn('john', 'doe', $request, $this->throttler); $userSession->logClientIn('john', 'doe', $request, $this->throttler);
} }

public function testCreateRememberMeToken() {
$user = $this->createMock(IUser::class);
$user
->expects($this->exactly(2))
->method('getUID')
->willReturn('UserUid');
$this->random
->expects($this->once())
->method('generate')
->with(32)
->willReturn('LongRandomToken');
$this->config
->expects($this->once())
->method('setUserValue')
->with('UserUid', 'login_token', 'LongRandomToken', 10000);
$this->userSession
->expects($this->once())
->method('setMagicInCookie')
->with('UserUid', 'LongRandomToken');

$this->userSession->createRememberMeToken($user);
}
} }

Loading…
Cancel
Save