Adds TokenProvider and Mapper tests

Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
This commit is contained in:
Roeland Jago Douma 2016-11-11 09:25:42 +01:00 committed by Robin Appelman
parent 91851c37be
commit e5bc80b31d
No known key found for this signature in database
GPG Key ID: 425003AC385454C5
3 changed files with 60 additions and 7 deletions

View File

@ -74,8 +74,7 @@ class DefaultTokenMapper extends Mapper {
$qb = $this->db->getQueryBuilder();
$result = $qb->select('id', 'uid', 'login_name', 'password', 'name', 'type', 'remember', 'token', 'last_activity', 'last_check', 'scope')
->from('authtoken')
->where($qb->expr()->eq('token', $qb->createParameter('token')))
->setParameter('token', $token)
->where($qb->expr()->eq('token', $qb->createNamedParameter($token)))
->execute();
$data = $result->fetch();
@ -88,19 +87,18 @@ class DefaultTokenMapper extends Mapper {
}
/**
* Get the user UID for the given token
* Get the token for $id
*
* @param string $token
* @param string $id
* @throws DoesNotExistException
* @return DefaultToken
*/
public function getTokenById($token) {
public function getTokenById($id) {
/* @var $qb IQueryBuilder */
$qb = $this->db->getQueryBuilder();
$result = $qb->select('id', 'uid', 'login_name', 'password', 'name', 'type', 'token', 'last_activity', 'last_check', 'scope')
->from('authtoken')
->where($qb->expr()->eq('id', $qb->createParameter('id')))
->setParameter('id', $token)
->where($qb->expr()->eq('id', $qb->createNamedParameter($id)))
->execute();
$data = $result->fetch();

View File

@ -27,6 +27,7 @@ use OC\Authentication\Token\DefaultToken;
use OC\Authentication\Token\DefaultTokenMapper;
use OC\Authentication\Token\IToken;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;
use OCP\IUser;
use Test\TestCase;
@ -40,6 +41,8 @@ class DefaultTokenMapperTest extends TestCase {
/** @var DefaultTokenMapper */
private $mapper;
/** @var IDBConnection */
private $dbConnection;
private $time;
@ -150,6 +153,35 @@ class DefaultTokenMapperTest extends TestCase {
$this->mapper->getToken($token);
}
public function testGetTokenById() {
$token = new DefaultToken();
$token->setUid('user2');
$token->setLoginName('User2');
$token->setPassword('971a337057853344700bbeccf836519f|UwOQwyb34sJHtqPV|036d4890f8c21d17bbc7b88072d8ef049a5c832a38e97f3e3d5f9186e896c2593aee16883f617322fa242728d0236ff32d163caeb4bd45e14ca002c57a88665f');
$token->setName('Firefox on Android');
$token->setToken('1504445f1524fc801035448a95681a9378ba2e83930c814546c56e5d6ebde221198792fd900c88ed5ead0555780dad1ebce3370d7e154941cd5de87eb419899b');
$token->setType(IToken::TEMPORARY_TOKEN);
$token->setRemember(IToken::DO_NOT_REMEMBER);
$token->setLastActivity($this->time - 60 * 60 * 24 * 3);
$token->setLastCheck($this->time - 10);
$dbToken = $this->mapper->getToken($token->getToken());
$token->setId($dbToken->getId()); // We don't know the ID
$token->resetUpdatedFields();
$dbToken = $this->mapper->getTokenById($token->getId());
$this->assertEquals($token, $dbToken);
}
/**
* @expectedException \OCP\AppFramework\Db\DoesNotExistException
*/
public function testGetInvalidTokenById() {
$id = 42;
$this->mapper->getToken($id);
}
public function testGetTokenByUser() {
$user = $this->createMock(IUser::class);
$user->expects($this->once())

View File

@ -22,9 +22,11 @@
namespace Test\Authentication\Token;
use OC\Authentication\Exceptions\InvalidTokenException;
use OC\Authentication\Token\DefaultToken;
use OC\Authentication\Token\DefaultTokenProvider;
use OC\Authentication\Token\IToken;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\Mapper;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\IConfig;
@ -376,4 +378,25 @@ class DefaultTokenProviderTest extends TestCase {
$this->tokenProvider->renewSessionToken('oldId', 'newId');
}
public function testGetTokenById() {
$token = $this->createMock(DefaultToken::class);
$this->mapper->expects($this->once())
->method('getTokenById')
->with($this->equalTo(42))
->willReturn($token);
$this->assertSame($token, $this->tokenProvider->getTokenById(42));
}
public function testGetInvalidTokenById() {
$this->expectException(InvalidTokenException::class);
$this->mapper->expects($this->once())
->method('getTokenById')
->with($this->equalTo(42))
->willThrowException(new DoesNotExistException('nope'));
$this->tokenProvider->getTokenById(42);
}
}