]> source.dussan.org Git - nextcloud-server.git/commitdiff
Add missing tests and fix PHPDoc
authorLukas Reschke <lukas@statuscode.ch>
Wed, 2 Nov 2016 12:37:39 +0000 (13:37 +0100)
committerLukas Reschke <lukas@statuscode.ch>
Wed, 2 Nov 2016 12:39:17 +0000 (13:39 +0100)
Signed-off-by: Lukas Reschke <lukas@statuscode.ch>
lib/private/Authentication/Token/DefaultTokenProvider.php
lib/private/Authentication/Token/IProvider.php
tests/lib/Authentication/Token/DefaultTokenProviderTest.php
tests/lib/User/SessionTest.php

index af1d600e4c3f5788a1134f73dfc391df6a541079..87f434c684c56bd12b9ce02055a8bd933b0d40cc 100644 (file)
@@ -1,6 +1,7 @@
 <?php
 /**
  * @copyright Copyright (c) 2016, ownCloud, Inc.
+ * @copyright Copyright (c) 2016, Christoph Wurst <christoph@winzerhof-wurst.at>
  *
  * @author Christoph Wurst <christoph@owncloud.com>
  *
@@ -56,7 +57,11 @@ class DefaultTokenProvider implements IProvider {
         * @param ILogger $logger
         * @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->crypto = $crypto;
                $this->config = $config;
@@ -98,6 +103,7 @@ class DefaultTokenProvider implements IProvider {
         * Save the updated token
         *
         * @param IToken $token
+        * @throws InvalidTokenException
         */
        public function updateToken(IToken $token) {
                if (!($token instanceof DefaultToken)) {
@@ -156,6 +162,7 @@ class DefaultTokenProvider implements IProvider {
        /**
         * @param string $oldSessionId
         * @param string $sessionId
+        * @throws InvalidTokenException
         */
        public function renewSessionToken($oldSessionId, $sessionId) {
                $token = $this->getToken($oldSessionId);
index b8c15571df197c9b136009364ba20a5874449c31..6260555470dd15f9e3abf8f7bd3f31329266be74 100644 (file)
@@ -55,6 +55,7 @@ interface IProvider {
        /**
         * @param string $oldSessionId
         * @param string $sessionId
+        * @throws InvalidTokenException
         */
        public function renewSessionToken($oldSessionId, $sessionId);
 
index cd6bf7bad5788482777970d2e7fd9c90e6644411..5e4d4f9436664b8db8037151d03a48dc2c8ffb3b 100644 (file)
@@ -1,8 +1,8 @@
 <?php
-
 /**
  * @author Christoph Wurst <christoph@owncloud.com>
  *
+ * @copyright Copyright (c) 2016, Lukas Reschke <lukas@statuscode.ch>
  * @copyright Copyright (c) 2016, ownCloud, Inc.
  * @license AGPL-3.0
  *
@@ -25,6 +25,7 @@ namespace Test\Authentication\Token;
 use OC\Authentication\Token\DefaultToken;
 use OC\Authentication\Token\DefaultTokenProvider;
 use OC\Authentication\Token\IToken;
+use OCP\AppFramework\Db\Mapper;
 use OCP\AppFramework\Utility\ITimeFactory;
 use OCP\IConfig;
 use OCP\ILogger;
@@ -34,13 +35,19 @@ use Test\TestCase;
 
 class DefaultTokenProviderTest extends TestCase {
 
-       /** @var DefaultTokenProvider */
+       /** @var DefaultTokenProvider|\PHPUnit_Framework_MockObject_MockObject */
        private $tokenProvider;
+       /** @var Mapper|\PHPUnit_Framework_MockObject_MockObject */
        private $mapper;
+       /** @var ICrypto|\PHPUnit_Framework_MockObject_MockObject */
        private $crypto;
+       /** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */
        private $config;
+       /** @var ILogger|\PHPUnit_Framework_MockObject_MockObject */
        private $logger;
+       /** @var ITimeFactory|\PHPUnit_Framework_MockObject_MockObject */
        private $timeFactory;
+       /** @var int */
        private $time;
 
        protected function setUp() {
@@ -262,4 +269,111 @@ class DefaultTokenProviderTest extends TestCase {
                $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');
+       }
+
 }
index f4237e94cdedf8d2d03b3ef1514a68fabb0630cf..ee9ed737cf5551dd27b40c98f41d3c12149605b0 100644 (file)
@@ -1,5 +1,4 @@
 <?php
-
 /**
  * Copyright (c) 2013 Robin Appelman <icewind@owncloud.com>
  * This file is licensed under the Affero General Public License version 3 or
@@ -43,6 +42,12 @@ class SessionTest extends \Test\TestCase {
        private $throttler;
        /** @var ISecureRandom|\PHPUnit_Framework_MockObject_MockObject */
        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() {
                parent::setUp();
@@ -55,6 +60,21 @@ class SessionTest extends \Test\TestCase {
                $this->config = $this->createMock(IConfig::class);
                $this->throttler = $this->createMock(Throttler::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);
        }
@@ -1136,4 +1156,27 @@ class SessionTest extends \Test\TestCase {
 
                $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);
+       }
 }