summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorArthur Schiwon <blizzz@arthur-schiwon.de>2016-06-09 18:45:12 +0200
committerArthur Schiwon <blizzz@arthur-schiwon.de>2016-06-09 18:45:12 +0200
commita636e4ff28b25797d6cc7750bc1efe52437ec67f (patch)
treeef385b9ef924088b6d6c1404d659e6be450aaf1f /tests
parent28193732ea24094335cccddf5fe03aeeeb6f5894 (diff)
parent6ba18934e6f095de08bec7bdc10c45485eeb5cc7 (diff)
downloadnextcloud-server-a636e4ff28b25797d6cc7750bc1efe52437ec67f.tar.gz
nextcloud-server-a636e4ff28b25797d6cc7750bc1efe52437ec67f.zip
Downstream 2016-06-09
Merge branch 'master' of https://github.com/owncloud/core into downstream-160609
Diffstat (limited to 'tests')
-rw-r--r--tests/Core/Controller/TokenControllerTest.php40
-rw-r--r--tests/Core/Controller/TwoFactorChallengeControllerTest.php21
-rw-r--r--tests/lib/AllConfigTest.php19
-rw-r--r--tests/lib/AppFramework/Middleware/Security/CORSMiddlewareTest.php11
-rw-r--r--tests/lib/Encryption/DecryptAllTest.php35
-rw-r--r--tests/lib/Files/Storage/LocalTest.php31
-rw-r--r--tests/lib/Files/ViewTest.php51
-rw-r--r--tests/lib/LoggerTest.php6
-rw-r--r--tests/lib/User/SessionTest.php159
9 files changed, 317 insertions, 56 deletions
diff --git a/tests/Core/Controller/TokenControllerTest.php b/tests/Core/Controller/TokenControllerTest.php
index 386140a8a4f..b6b54b14fad 100644
--- a/tests/Core/Controller/TokenControllerTest.php
+++ b/tests/Core/Controller/TokenControllerTest.php
@@ -23,8 +23,9 @@
namespace Tests\Core\Controller;
use OC\AppFramework\Http;
+use OC\Authentication\Token\IToken;
use OC\Core\Controller\TokenController;
-use OCP\AppFramework\Http\Response;
+use OCP\AppFramework\Http\JSONResponse;
use Test\TestCase;
class TokenControllerTest extends TestCase {
@@ -34,6 +35,7 @@ class TokenControllerTest extends TestCase {
private $request;
private $userManager;
private $tokenProvider;
+ private $twoFactorAuthManager;
private $secureRandom;
protected function setUp() {
@@ -43,17 +45,17 @@ class TokenControllerTest extends TestCase {
$this->userManager = $this->getMockBuilder('\OC\User\Manager')
->disableOriginalConstructor()
->getMock();
- $this->tokenProvider = $this->getMockBuilder('\OC\Authentication\Token\DefaultTokenProvider')
+ $this->tokenProvider = $this->getMock('\OC\Authentication\Token\IProvider');
+ $this->twoFactorAuthManager = $this->getMockBuilder('\OC\Authentication\TwoFactorAuth\Manager')
->disableOriginalConstructor()
->getMock();
$this->secureRandom = $this->getMock('\OCP\Security\ISecureRandom');
- $this->tokenController = new TokenController('core', $this->request, $this->userManager, $this->tokenProvider,
- $this->secureRandom);
+ $this->tokenController = new TokenController('core', $this->request, $this->userManager, $this->tokenProvider, $this->twoFactorAuthManager, $this->secureRandom);
}
public function testWithoutCredentials() {
- $expected = new Response();
+ $expected = new JSONResponse();
$expected->setStatus(Http::STATUS_UNPROCESSABLE_ENTITY);
$actual = $this->tokenController->generateToken(null, null);
@@ -66,7 +68,7 @@ class TokenControllerTest extends TestCase {
->method('checkPassword')
->with('john', 'passme')
->will($this->returnValue(false));
- $expected = new Response();
+ $expected = new JSONResponse();
$expected->setStatus(Http::STATUS_UNAUTHORIZED);
$actual = $this->tokenController->generateToken('john', 'passme');
@@ -83,13 +85,17 @@ class TokenControllerTest extends TestCase {
$user->expects($this->once())
->method('getUID')
->will($this->returnValue('john'));
+ $this->twoFactorAuthManager->expects($this->once())
+ ->method('isTwoFactorAuthenticated')
+ ->with($user)
+ ->will($this->returnValue(false));
$this->secureRandom->expects($this->once())
->method('generate')
->with(128)
->will($this->returnValue('verysecurerandomtoken'));
$this->tokenProvider->expects($this->once())
->method('generateToken')
- ->with('verysecurerandomtoken', 'john', 'john', '123456', 'unknown client', \OC\Authentication\Token\IToken::PERMANENT_TOKEN);
+ ->with('verysecurerandomtoken', 'john', 'john', '123456', 'unknown client', IToken::PERMANENT_TOKEN);
$expected = [
'token' => 'verysecurerandomtoken'
];
@@ -99,4 +105,24 @@ class TokenControllerTest extends TestCase {
$this->assertEquals($expected, $actual);
}
+ public function testWithValidCredentialsBut2faEnabled() {
+ $user = $this->getMock('\OCP\IUser');
+ $this->userManager->expects($this->once())
+ ->method('checkPassword')
+ ->with('john', '123456')
+ ->will($this->returnValue($user));
+ $this->twoFactorAuthManager->expects($this->once())
+ ->method('isTwoFactorAuthenticated')
+ ->with($user)
+ ->will($this->returnValue(true));
+ $this->secureRandom->expects($this->never())
+ ->method('generate');
+ $expected = new JSONResponse();
+ $expected->setStatus(Http::STATUS_UNAUTHORIZED);
+
+ $actual = $this->tokenController->generateToken('john', '123456');
+
+ $this->assertEquals($expected, $actual);
+ }
+
}
diff --git a/tests/Core/Controller/TwoFactorChallengeControllerTest.php b/tests/Core/Controller/TwoFactorChallengeControllerTest.php
index 2da6dcd52ac..08d8dd1452c 100644
--- a/tests/Core/Controller/TwoFactorChallengeControllerTest.php
+++ b/tests/Core/Controller/TwoFactorChallengeControllerTest.php
@@ -33,7 +33,7 @@ class TwoFactorChallengeControllerTest extends TestCase {
private $session;
private $urlGenerator;
- /** TwoFactorChallengeController */
+ /** @var TwoFactorChallengeController|\PHPUnit_Framework_MockObject_MockObject */
private $controller;
protected function setUp() {
@@ -47,9 +47,20 @@ class TwoFactorChallengeControllerTest extends TestCase {
$this->session = $this->getMock('\OCP\ISession');
$this->urlGenerator = $this->getMock('\OCP\IURLGenerator');
- $this->controller = new TwoFactorChallengeController(
- 'core', $this->request, $this->twoFactorManager, $this->userSession, $this->session, $this->urlGenerator
- );
+ $this->controller = $this->getMockBuilder('OC\Core\Controller\TwoFactorChallengeController')
+ ->setConstructorArgs([
+ 'core',
+ $this->request,
+ $this->twoFactorManager,
+ $this->userSession,
+ $this->session,
+ $this->urlGenerator,
+ ])
+ ->setMethods(['getLogoutAttribute'])
+ ->getMock();
+ $this->controller->expects($this->any())
+ ->method('getLogoutAttribute')
+ ->willReturn('logoutAttribute');
}
public function testSelectChallenge() {
@@ -70,6 +81,7 @@ class TwoFactorChallengeControllerTest extends TestCase {
$expected = new \OCP\AppFramework\Http\TemplateResponse('core', 'twofactorselectchallenge', [
'providers' => $providers,
'redirect_url' => '/some/url',
+ 'logout_attribute' => 'logoutAttribute',
], 'guest');
$this->assertEquals($expected, $this->controller->selectChallenge('/some/url'));
@@ -110,6 +122,7 @@ class TwoFactorChallengeControllerTest extends TestCase {
$expected = new \OCP\AppFramework\Http\TemplateResponse('core', 'twofactorshowchallenge', [
'error' => true,
'provider' => $provider,
+ 'logout_attribute' => 'logoutAttribute',
'template' => '<html/>',
], 'guest');
diff --git a/tests/lib/AllConfigTest.php b/tests/lib/AllConfigTest.php
index 4f8b0658b80..3d0a9cb0827 100644
--- a/tests/lib/AllConfigTest.php
+++ b/tests/lib/AllConfigTest.php
@@ -123,6 +123,25 @@ class AllConfigTest extends \Test\TestCase {
$config->deleteUserValue('userPreCond', 'appPreCond', 'keyPreCond');
}
+ public function dataSetUserValueUnexpectedValue() {
+ return [
+ [true],
+ [false],
+ [null],
+ [new \stdClass()],
+ ];
+ }
+
+ /**
+ * @dataProvider dataSetUserValueUnexpectedValue
+ * @param mixed $value
+ * @expectedException \UnexpectedValueException
+ */
+ public function testSetUserValueUnexpectedValue($value) {
+ $config = $this->getConfig();
+ $config->setUserValue('userSetBool', 'appSetBool', 'keySetBool', $value);
+ }
+
/**
* @expectedException \OCP\PreConditionNotMetException
*/
diff --git a/tests/lib/AppFramework/Middleware/Security/CORSMiddlewareTest.php b/tests/lib/AppFramework/Middleware/Security/CORSMiddlewareTest.php
index 8e53c9202cf..a398dc2320c 100644
--- a/tests/lib/AppFramework/Middleware/Security/CORSMiddlewareTest.php
+++ b/tests/lib/AppFramework/Middleware/Security/CORSMiddlewareTest.php
@@ -16,7 +16,6 @@ use OC\AppFramework\Http\Request;
use OC\AppFramework\Middleware\Security\CORSMiddleware;
use OC\AppFramework\Utility\ControllerMethodReflector;
use OC\AppFramework\Middleware\Security\Exceptions\SecurityException;
-use OCP\AppFramework\Http;
use OCP\AppFramework\Http\JSONResponse;
use OCP\AppFramework\Http\Response;
@@ -29,7 +28,9 @@ class CORSMiddlewareTest extends \Test\TestCase {
protected function setUp() {
parent::setUp();
$this->reflector = new ControllerMethodReflector();
- $this->session = $this->getMock('\OCP\IUserSession');
+ $this->session = $this->getMockBuilder('\OC\User\Session')
+ ->disableOriginalConstructor()
+ ->getMock();
}
/**
@@ -127,7 +128,7 @@ class CORSMiddlewareTest extends \Test\TestCase {
$this->session->expects($this->never())
->method('logout');
$this->session->expects($this->never())
- ->method('login')
+ ->method('logClientIn')
->with($this->equalTo('user'), $this->equalTo('pass'))
->will($this->returnValue(true));
$this->reflector->reflect($this, __FUNCTION__);
@@ -150,7 +151,7 @@ class CORSMiddlewareTest extends \Test\TestCase {
$this->session->expects($this->once())
->method('logout');
$this->session->expects($this->once())
- ->method('login')
+ ->method('logClientIn')
->with($this->equalTo('user'), $this->equalTo('pass'))
->will($this->returnValue(true));
$this->reflector->reflect($this, __FUNCTION__);
@@ -175,7 +176,7 @@ class CORSMiddlewareTest extends \Test\TestCase {
$this->session->expects($this->once())
->method('logout');
$this->session->expects($this->once())
- ->method('login')
+ ->method('logClientIn')
->with($this->equalTo('user'), $this->equalTo('pass'))
->will($this->returnValue(false));
$this->reflector->reflect($this, __FUNCTION__);
diff --git a/tests/lib/Encryption/DecryptAllTest.php b/tests/lib/Encryption/DecryptAllTest.php
index ffcbbc74a99..d7cf2fb7baf 100644
--- a/tests/lib/Encryption/DecryptAllTest.php
+++ b/tests/lib/Encryption/DecryptAllTest.php
@@ -86,13 +86,25 @@ class DecryptAllTest extends TestCase {
$this->invokePrivate($this->instance, 'output', [$this->outputInterface]);
}
+ public function dataDecryptAll() {
+ return [
+ [true, 'user1', true],
+ [false, 'user1', true],
+ [true, '0', true],
+ [false, '0', true],
+ [true, '', false],
+ ];
+ }
+
/**
- * @dataProvider dataTrueFalse
+ * @dataProvider dataDecryptAll
* @param bool $prepareResult
+ * @param string $user
+ * @param bool $userExistsChecked
*/
- public function testDecryptAll($prepareResult, $user) {
+ public function testDecryptAll($prepareResult, $user, $userExistsChecked) {
- if (!empty($user)) {
+ if ($userExistsChecked) {
$this->userManager->expects($this->once())->method('userExists')->willReturn(true);
} else {
$this->userManager->expects($this->never())->method('userExists');
@@ -125,15 +137,6 @@ class DecryptAllTest extends TestCase {
$instance->decryptAll($this->inputInterface, $this->outputInterface, $user);
}
- public function dataTrueFalse() {
- return [
- [true, 'user1'],
- [false, 'user1'],
- [true, ''],
- [true, null]
- ];
- }
-
/**
* test decrypt all call with a user who doesn't exists
*/
@@ -147,8 +150,16 @@ class DecryptAllTest extends TestCase {
);
}
+ public function dataTrueFalse() {
+ return [
+ [true],
+ [false],
+ ];
+ }
+
/**
* @dataProvider dataTrueFalse
+ * @param bool $success
*/
public function testPrepareEncryptionModules($success) {
diff --git a/tests/lib/Files/Storage/LocalTest.php b/tests/lib/Files/Storage/LocalTest.php
index 7b8ae6a24b2..cca4d6a6676 100644
--- a/tests/lib/Files/Storage/LocalTest.php
+++ b/tests/lib/Files/Storage/LocalTest.php
@@ -84,5 +84,36 @@ class LocalTest extends Storage {
public function testInvalidArgumentsNoArray() {
new \OC\Files\Storage\Local(null);
}
+
+ /**
+ * @expectedException \OCP\Files\ForbiddenException
+ */
+ public function testDisallowSymlinksOutsideDatadir() {
+ $subDir1 = $this->tmpDir . 'sub1';
+ $subDir2 = $this->tmpDir . 'sub2';
+ $sym = $this->tmpDir . 'sub1/sym';
+ mkdir($subDir1);
+ mkdir($subDir2);
+
+ symlink($subDir2, $sym);
+
+ $storage = new \OC\Files\Storage\Local(['datadir' => $subDir1]);
+
+ $storage->file_put_contents('sym/foo', 'bar');
+ }
+
+ public function testDisallowSymlinksInsideDatadir() {
+ $subDir1 = $this->tmpDir . 'sub1';
+ $subDir2 = $this->tmpDir . 'sub1/sub2';
+ $sym = $this->tmpDir . 'sub1/sym';
+ mkdir($subDir1);
+ mkdir($subDir2);
+
+ symlink($subDir2, $sym);
+
+ $storage = new \OC\Files\Storage\Local(['datadir' => $subDir1]);
+
+ $storage->file_put_contents('sym/foo', 'bar');
+ }
}
diff --git a/tests/lib/Files/ViewTest.php b/tests/lib/Files/ViewTest.php
index 2c27bb64a70..59b17b83958 100644
--- a/tests/lib/Files/ViewTest.php
+++ b/tests/lib/Files/ViewTest.php
@@ -2417,7 +2417,7 @@ class ViewTest extends \Test\TestCase {
$content = $view->getDirectoryContent('', $filter);
- $files = array_map(function(FileInfo $info) {
+ $files = array_map(function (FileInfo $info) {
return $info->getName();
}, $content);
sort($files);
@@ -2444,4 +2444,53 @@ class ViewTest extends \Test\TestCase {
$data = $view->getFileInfo('.');
$this->assertEquals('', $data->getChecksum());
}
+
+ public function testDeleteGhostFile() {
+ $storage = new Temporary(array());
+ $scanner = $storage->getScanner();
+ $cache = $storage->getCache();
+ $storage->file_put_contents('foo.txt', 'bar');
+ \OC\Files\Filesystem::mount($storage, array(), '/test/');
+ $scanner->scan('');
+
+ $storage->unlink('foo.txt');
+
+ $this->assertTrue($cache->inCache('foo.txt'));
+
+ $view = new \OC\Files\View('/test');
+ $rootInfo = $view->getFileInfo('');
+ $this->assertEquals(3, $rootInfo->getSize());
+ $view->unlink('foo.txt');
+ $newInfo = $view->getFileInfo('');
+
+ $this->assertFalse($cache->inCache('foo.txt'));
+ $this->assertNotEquals($rootInfo->getEtag(), $newInfo->getEtag());
+ $this->assertEquals(0, $newInfo->getSize());
+ }
+
+ public function testDeleteGhostFolder() {
+ $storage = new Temporary(array());
+ $scanner = $storage->getScanner();
+ $cache = $storage->getCache();
+ $storage->mkdir('foo');
+ $storage->file_put_contents('foo/foo.txt', 'bar');
+ \OC\Files\Filesystem::mount($storage, array(), '/test/');
+ $scanner->scan('');
+
+ $storage->rmdir('foo');
+
+ $this->assertTrue($cache->inCache('foo'));
+ $this->assertTrue($cache->inCache('foo/foo.txt'));
+
+ $view = new \OC\Files\View('/test');
+ $rootInfo = $view->getFileInfo('');
+ $this->assertEquals(3, $rootInfo->getSize());
+ $view->rmdir('foo');
+ $newInfo = $view->getFileInfo('');
+
+ $this->assertFalse($cache->inCache('foo'));
+ $this->assertFalse($cache->inCache('foo/foo.txt'));
+ $this->assertNotEquals($rootInfo->getEtag(), $newInfo->getEtag());
+ $this->assertEquals(0, $newInfo->getSize());
+ }
}
diff --git a/tests/lib/LoggerTest.php b/tests/lib/LoggerTest.php
index 4eb04b00f58..4b80c01f343 100644
--- a/tests/lib/LoggerTest.php
+++ b/tests/lib/LoggerTest.php
@@ -89,7 +89,7 @@ class LoggerTest extends TestCase {
foreach($logLines as $logLine) {
$this->assertNotContains($user, $logLine);
$this->assertNotContains($password, $logLine);
- $this->assertContains('login(*** username and password replaced ***)', $logLine);
+ $this->assertContains('login(*** sensitive parameters replaced ***)', $logLine);
}
}
@@ -104,7 +104,7 @@ class LoggerTest extends TestCase {
foreach($logLines as $logLine) {
$this->assertNotContains($user, $logLine);
$this->assertNotContains($password, $logLine);
- $this->assertContains('checkPassword(*** username and password replaced ***)', $logLine);
+ $this->assertContains('checkPassword(*** sensitive parameters replaced ***)', $logLine);
}
}
@@ -119,7 +119,7 @@ class LoggerTest extends TestCase {
foreach($logLines as $logLine) {
$this->assertNotContains($user, $logLine);
$this->assertNotContains($password, $logLine);
- $this->assertContains('validateUserPass(*** username and password replaced ***)', $logLine);
+ $this->assertContains('validateUserPass(*** sensitive parameters replaced ***)', $logLine);
}
}
}
diff --git a/tests/lib/User/SessionTest.php b/tests/lib/User/SessionTest.php
index 36f14e85492..eac38ebba16 100644
--- a/tests/lib/User/SessionTest.php
+++ b/tests/lib/User/SessionTest.php
@@ -22,7 +22,7 @@ class SessionTest extends \Test\TestCase {
private $timeFactory;
/** @var \OC\Authentication\Token\DefaultTokenProvider */
- protected $defaultProvider;
+ protected $tokenProvider;
/** @var \OCP\IConfig */
private $config;
@@ -34,9 +34,7 @@ class SessionTest extends \Test\TestCase {
$this->timeFactory->expects($this->any())
->method('getTime')
->will($this->returnValue(10000));
- $this->defaultProvider = $this->getMockBuilder('\OC\Authentication\Token\DefaultTokenProvider')
- ->disableOriginalConstructor()
- ->getMock();
+ $this->tokenProvider = $this->getMock('\OC\Authentication\Token\IProvider');
$this->config = $this->getMock('\OCP\IConfig');
}
@@ -61,14 +59,14 @@ class SessionTest extends \Test\TestCase {
$session->expects($this->once())
->method('getId')
->will($this->returnValue($sessionId));
- $this->defaultProvider->expects($this->once())
+ $this->tokenProvider->expects($this->once())
->method('getToken')
->will($this->returnValue($token));
$session->expects($this->at(2))
->method('get')
->with('last_login_check')
->will($this->returnValue(null)); // No check has been run yet
- $this->defaultProvider->expects($this->once())
+ $this->tokenProvider->expects($this->once())
->method('getPassword')
->with($token, $sessionId)
->will($this->returnValue('password123'));
@@ -87,7 +85,7 @@ class SessionTest extends \Test\TestCase {
->method('get')
->with('last_token_update')
->will($this->returnValue(null)); // No check run so far
- $this->defaultProvider->expects($this->once())
+ $this->tokenProvider->expects($this->once())
->method('updateToken')
->with($token);
$session->expects($this->at(5))
@@ -99,7 +97,7 @@ class SessionTest extends \Test\TestCase {
->with($expectedUser->getUID())
->will($this->returnValue($expectedUser));
- $userSession = new \OC\User\Session($manager, $session, $this->timeFactory, $this->defaultProvider, $this->config);
+ $userSession = new \OC\User\Session($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config);
$user = $userSession->getUser();
$this->assertSame($expectedUser, $user);
}
@@ -122,7 +120,7 @@ class SessionTest extends \Test\TestCase {
->getMock();
$userSession = $this->getMockBuilder('\OC\User\Session')
- ->setConstructorArgs([$manager, $session, $this->timeFactory, $this->defaultProvider, $this->config])
+ ->setConstructorArgs([$manager, $session, $this->timeFactory, $this->tokenProvider, $this->config])
->setMethods([
'getUser'
])
@@ -149,7 +147,7 @@ class SessionTest extends \Test\TestCase {
->method('getUID')
->will($this->returnValue('foo'));
- $userSession = new \OC\User\Session($manager, $session, $this->timeFactory, $this->defaultProvider, $this->config);
+ $userSession = new \OC\User\Session($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config);
$userSession->setUser($user);
}
@@ -201,7 +199,7 @@ class SessionTest extends \Test\TestCase {
->will($this->returnValue($user));
$userSession = $this->getMockBuilder('\OC\User\Session')
- ->setConstructorArgs([$manager, $session, $this->timeFactory, $this->defaultProvider, $this->config])
+ ->setConstructorArgs([$manager, $session, $this->timeFactory, $this->tokenProvider, $this->config])
->setMethods([
'prepareUserLogin'
])
@@ -248,7 +246,7 @@ class SessionTest extends \Test\TestCase {
->with('foo', 'bar')
->will($this->returnValue($user));
- $userSession = new \OC\User\Session($manager, $session, $this->timeFactory, $this->defaultProvider, $this->config);
+ $userSession = new \OC\User\Session($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config);
$userSession->login('foo', 'bar');
}
@@ -284,7 +282,7 @@ class SessionTest extends \Test\TestCase {
->with('foo', 'bar')
->will($this->returnValue(false));
- $userSession = new \OC\User\Session($manager, $session, $this->timeFactory, $this->defaultProvider, $this->config);
+ $userSession = new \OC\User\Session($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config);
$userSession->login('foo', 'bar');
}
@@ -304,7 +302,7 @@ class SessionTest extends \Test\TestCase {
->with('foo', 'bar')
->will($this->returnValue(false));
- $userSession = new \OC\User\Session($manager, $session, $this->timeFactory, $this->defaultProvider, $this->config);
+ $userSession = new \OC\User\Session($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config);
$userSession->login('foo', 'bar');
}
@@ -316,11 +314,11 @@ class SessionTest extends \Test\TestCase {
/** @var \OC\User\Session $userSession */
$userSession = $this->getMockBuilder('\OC\User\Session')
- ->setConstructorArgs([$manager, $session, $this->timeFactory, $this->defaultProvider, $this->config])
+ ->setConstructorArgs([$manager, $session, $this->timeFactory, $this->tokenProvider, $this->config])
->setMethods(['login'])
->getMock();
- $this->defaultProvider->expects($this->once())
+ $this->tokenProvider->expects($this->once())
->method('getToken')
->with('doe')
->will($this->throwException(new \OC\Authentication\Exceptions\InvalidTokenException()));
@@ -341,11 +339,11 @@ class SessionTest extends \Test\TestCase {
/** @var \OC\User\Session $userSession */
$userSession = $this->getMockBuilder('\OC\User\Session')
- ->setConstructorArgs([$manager, $session, $this->timeFactory, $this->defaultProvider, $this->config])
+ ->setConstructorArgs([$manager, $session, $this->timeFactory, $this->tokenProvider, $this->config])
->setMethods(['login', 'isTwoFactorEnforced'])
->getMock();
- $this->defaultProvider->expects($this->once())
+ $this->tokenProvider->expects($this->once())
->method('getToken')
->with('doe')
->will($this->throwException(new \OC\Authentication\Exceptions\InvalidTokenException()));
@@ -413,7 +411,7 @@ class SessionTest extends \Test\TestCase {
//override, otherwise tests will fail because of setcookie()
array('setMagicInCookie'),
//there are passed as parameters to the constructor
- array($manager, $session, $this->timeFactory, $this->defaultProvider, $this->config));
+ array($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config));
$granted = $userSession->loginWithCookie('foo', $token);
@@ -458,7 +456,7 @@ class SessionTest extends \Test\TestCase {
$token = 'goodToken';
\OC::$server->getConfig()->setUserValue('foo', 'login_token', $token, time());
- $userSession = new \OC\User\Session($manager, $session, $this->timeFactory, $this->defaultProvider, $this->config);
+ $userSession = new \OC\User\Session($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config);
$granted = $userSession->loginWithCookie('foo', 'badToken');
$this->assertSame($granted, false);
@@ -501,7 +499,7 @@ class SessionTest extends \Test\TestCase {
$token = 'goodToken';
\OC::$server->getConfig()->setUserValue('foo', 'login_token', $token, time());
- $userSession = new \OC\User\Session($manager, $session, $this->timeFactory, $this->defaultProvider, $this->config);
+ $userSession = new \OC\User\Session($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config);
$granted = $userSession->loginWithCookie('foo', $token);
$this->assertSame($granted, false);
@@ -526,7 +524,7 @@ class SessionTest extends \Test\TestCase {
$session = new Memory('');
$session->set('user_id', 'foo');
$userSession = $this->getMockBuilder('\OC\User\Session')
- ->setConstructorArgs([$manager, $session, $this->timeFactory, $this->defaultProvider, $this->config])
+ ->setConstructorArgs([$manager, $session, $this->timeFactory, $this->tokenProvider, $this->config])
->setMethods([
'validateSession'
])
@@ -542,6 +540,119 @@ class SessionTest extends \Test\TestCase {
$this->assertEquals($users['bar'], $userSession->getUser());
}
+ public function testCreateSessionToken() {
+ $manager = $this->getMockBuilder('\OC\User\Manager')
+ ->disableOriginalConstructor()
+ ->getMock();
+ $session = $this->getMock('\OCP\ISession');
+ $token = $this->getMock('\OC\Authentication\Token\IToken');
+ $user = $this->getMock('\OCP\IUser');
+ $userSession = new \OC\User\Session($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config);
+
+ $random = $this->getMock('\OCP\Security\ISecureRandom');
+ $config = $this->getMock('\OCP\IConfig');
+ $csrf = $this->getMockBuilder('\OC\Security\CSRF\CsrfTokenManager')
+ ->disableOriginalConstructor()
+ ->getMock();
+ $request = new \OC\AppFramework\Http\Request([
+ 'server' => [
+ 'HTTP_USER_AGENT' => 'Firefox',
+ ]
+ ], $random, $config, $csrf);
+
+ $uid = 'user123';
+ $loginName = 'User123';
+ $password = 'passme';
+ $sessionId = 'abcxyz';
+
+ $manager->expects($this->once())
+ ->method('get')
+ ->with($uid)
+ ->will($this->returnValue($user));
+ $session->expects($this->once())
+ ->method('getId')
+ ->will($this->returnValue($sessionId));
+ $this->tokenProvider->expects($this->once())
+ ->method('getToken')
+ ->with($password)
+ ->will($this->throwException(new \OC\Authentication\Exceptions\InvalidTokenException()));
+
+ $this->tokenProvider->expects($this->once())
+ ->method('generateToken')
+ ->with($sessionId, $uid, $loginName, $password, 'Firefox');
+
+ $this->assertTrue($userSession->createSessionToken($request, $uid, $loginName, $password));
+ }
+
+ public function testCreateSessionTokenWithTokenPassword() {
+ $manager = $this->getMockBuilder('\OC\User\Manager')
+ ->disableOriginalConstructor()
+ ->getMock();
+ $session = $this->getMock('\OCP\ISession');
+ $token = $this->getMock('\OC\Authentication\Token\IToken');
+ $user = $this->getMock('\OCP\IUser');
+ $userSession = new \OC\User\Session($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config);
+
+ $random = $this->getMock('\OCP\Security\ISecureRandom');
+ $config = $this->getMock('\OCP\IConfig');
+ $csrf = $this->getMockBuilder('\OC\Security\CSRF\CsrfTokenManager')
+ ->disableOriginalConstructor()
+ ->getMock();
+ $request = new \OC\AppFramework\Http\Request([
+ 'server' => [
+ 'HTTP_USER_AGENT' => 'Firefox',
+ ]
+ ], $random, $config, $csrf);
+
+ $uid = 'user123';
+ $loginName = 'User123';
+ $password = 'iamatoken';
+ $realPassword = 'passme';
+ $sessionId = 'abcxyz';
+
+ $manager->expects($this->once())
+ ->method('get')
+ ->with($uid)
+ ->will($this->returnValue($user));
+ $session->expects($this->once())
+ ->method('getId')
+ ->will($this->returnValue($sessionId));
+ $this->tokenProvider->expects($this->once())
+ ->method('getToken')
+ ->with($password)
+ ->will($this->returnValue($token));
+ $this->tokenProvider->expects($this->once())
+ ->method('getPassword')
+ ->with($token, $password)
+ ->will($this->returnValue($realPassword));
+
+ $this->tokenProvider->expects($this->once())
+ ->method('generateToken')
+ ->with($sessionId, $uid, $loginName, $realPassword, 'Firefox');
+
+ $this->assertTrue($userSession->createSessionToken($request, $uid, $loginName, $password));
+ }
+
+ public function testCreateSessionTokenWithNonExistentUser() {
+ $manager = $this->getMockBuilder('\OC\User\Manager')
+ ->disableOriginalConstructor()
+ ->getMock();
+ $session = $this->getMock('\OCP\ISession');
+ $userSession = new \OC\User\Session($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config);
+ $request = $this->getMock('\OCP\IRequest');
+
+ $uid = 'user123';
+ $loginName = 'User123';
+ $password = 'passme';
+
+ $manager->expects($this->once())
+ ->method('get')
+ ->with($uid)
+ ->will($this->returnValue(null));
+
+ $this->assertFalse($userSession->createSessionToken($request, $uid, $loginName, $password));
+ }
+
public function testTryTokenLoginWithDisabledUser() {
$manager = $this->getMockBuilder('\OC\User\Manager')
->disableOriginalConstructor()
@@ -549,14 +660,14 @@ class SessionTest extends \Test\TestCase {
$session = new Memory('');
$token = $this->getMock('\OC\Authentication\Token\IToken');
$user = $this->getMock('\OCP\IUser');
- $userSession = new \OC\User\Session($manager, $session, $this->timeFactory, $this->defaultProvider, $this->config);
+ $userSession = new \OC\User\Session($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config);
$request = $this->getMock('\OCP\IRequest');
$request->expects($this->once())
->method('getHeader')
->with('Authorization')
->will($this->returnValue('token xxxxx'));
- $this->defaultProvider->expects($this->once())
+ $this->tokenProvider->expects($this->once())
->method('validateToken')
->with('xxxxx')
->will($this->returnValue($token));