summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorLukas Reschke <lukas@owncloud.com>2016-06-21 11:18:22 +0200
committerLukas Reschke <lukas@owncloud.com>2016-06-21 11:18:22 +0200
commit2b493e2f9dca674ba11f88a1d182d6872e04eaaa (patch)
treeef7fa75d1b4da812de80e93c3590dbb5404d7f06 /tests
parentb4df57f3f02f65ed71d1072280751170379a53e8 (diff)
parent0e575c7eeadc6c8eb11b0be2ed1d39cdcf6cfcb8 (diff)
downloadnextcloud-server-2b493e2f9dca674ba11f88a1d182d6872e04eaaa.tar.gz
nextcloud-server-2b493e2f9dca674ba11f88a1d182d6872e04eaaa.zip
Merge remote-tracking branch 'upstream/master' into master-sync-upstream
Diffstat (limited to 'tests')
-rw-r--r--tests/lib/AppFramework/Middleware/Security/CORSMiddlewareTest.php25
-rw-r--r--tests/lib/Authentication/TwoFactorAuth/ManagerTest.php12
-rw-r--r--tests/lib/Files/ViewTest.php7
-rw-r--r--tests/lib/Repair/CleanTagsTest.php28
-rw-r--r--tests/lib/Repair/RemoveRootSharesTest.php194
-rw-r--r--tests/lib/User/SessionTest.php54
6 files changed, 313 insertions, 7 deletions
diff --git a/tests/lib/AppFramework/Middleware/Security/CORSMiddlewareTest.php b/tests/lib/AppFramework/Middleware/Security/CORSMiddlewareTest.php
index a398dc2320c..54d2831d25f 100644
--- a/tests/lib/AppFramework/Middleware/Security/CORSMiddlewareTest.php
+++ b/tests/lib/AppFramework/Middleware/Security/CORSMiddlewareTest.php
@@ -164,6 +164,31 @@ class CORSMiddlewareTest extends \Test\TestCase {
* @CORS
* @expectedException \OC\AppFramework\Middleware\Security\Exceptions\SecurityException
*/
+ public function testCORSShouldFailIfPasswordLoginIsForbidden() {
+ $request = new Request(
+ ['server' => [
+ 'PHP_AUTH_USER' => 'user',
+ 'PHP_AUTH_PW' => 'pass'
+ ]],
+ $this->getMock('\OCP\Security\ISecureRandom'),
+ $this->getMock('\OCP\IConfig')
+ );
+ $this->session->expects($this->once())
+ ->method('logout');
+ $this->session->expects($this->once())
+ ->method('logClientIn')
+ ->with($this->equalTo('user'), $this->equalTo('pass'))
+ ->will($this->throwException(new \OC\Authentication\Exceptions\PasswordLoginForbiddenException));
+ $this->reflector->reflect($this, __FUNCTION__);
+ $middleware = new CORSMiddleware($request, $this->reflector, $this->session);
+
+ $middleware->beforeController($this, __FUNCTION__, new Response());
+ }
+
+ /**
+ * @CORS
+ * @expectedException \OC\AppFramework\Middleware\Security\Exceptions\SecurityException
+ */
public function testCORSShouldNotAllowCookieAuth() {
$request = new Request(
['server' => [
diff --git a/tests/lib/Authentication/TwoFactorAuth/ManagerTest.php b/tests/lib/Authentication/TwoFactorAuth/ManagerTest.php
index 363229b01bc..586fd3aaa2e 100644
--- a/tests/lib/Authentication/TwoFactorAuth/ManagerTest.php
+++ b/tests/lib/Authentication/TwoFactorAuth/ManagerTest.php
@@ -55,7 +55,10 @@ class ManagerTest extends TestCase {
$this->session = $this->getMock('\OCP\ISession');
$this->config = $this->getMock('\OCP\IConfig');
- $this->manager = new Manager($this->appManager, $this->session, $this->config);
+ $this->manager = $this->getMockBuilder('\OC\Authentication\TwoFactorAuth\Manager')
+ ->setConstructorArgs([$this->appManager, $this->session, $this->config])
+ ->setMethods(['loadTwoFactorApp']) // Do not actually load the apps
+ ->getMock();
$this->fakeProvider = $this->getMock('\OCP\Authentication\TwoFactorAuth\IProvider');
$this->fakeProvider->expects($this->any())
@@ -83,6 +86,10 @@ class ManagerTest extends TestCase {
'\OCA\MyCustom2faApp\FakeProvider',
],
]));
+
+ $this->manager->expects($this->once())
+ ->method('loadTwoFactorApp')
+ ->with('mycustom2faapp');
}
/**
@@ -94,6 +101,9 @@ class ManagerTest extends TestCase {
->method('getEnabledAppsForUser')
->with($this->user)
->will($this->returnValue(['faulty2faapp']));
+ $this->manager->expects($this->once())
+ ->method('loadTwoFactorApp')
+ ->with('faulty2faapp');
$this->appManager->expects($this->once())
->method('getAppInfo')
diff --git a/tests/lib/Files/ViewTest.php b/tests/lib/Files/ViewTest.php
index 59b17b83958..87feb63888d 100644
--- a/tests/lib/Files/ViewTest.php
+++ b/tests/lib/Files/ViewTest.php
@@ -7,6 +7,7 @@
namespace Test\Files;
+use OC\Cache\CappedMemoryCache;
use OC\Files\Cache\Watcher;
use OC\Files\Storage\Common;
use OC\Files\Mount\MountPoint;
@@ -269,6 +270,9 @@ class ViewTest extends \Test\TestCase {
* @dataProvider sharingDisabledPermissionProvider
*/
public function testRemoveSharePermissionWhenSharingDisabledForUser($excludeGroups, $excludeGroupsList, $expectedShareable) {
+ // Reset sharing disabled for users cache
+ $this->invokePrivate(\OC::$server->getShareManager(), 'sharingDisabledForUsersCache', [new CappedMemoryCache()]);
+
$appConfig = \OC::$server->getAppConfig();
$oldExcludeGroupsFlag = $appConfig->getValue('core', 'shareapi_exclude_groups', 'no');
$oldExcludeGroupsList = $appConfig->getValue('core', 'shareapi_exclude_groups_list', '');
@@ -290,6 +294,9 @@ class ViewTest extends \Test\TestCase {
$appConfig->setValue('core', 'shareapi_exclude_groups', $oldExcludeGroupsFlag);
$appConfig->setValue('core', 'shareapi_exclude_groups_list', $oldExcludeGroupsList);
+
+ // Reset sharing disabled for users cache
+ $this->invokePrivate(\OC::$server->getShareManager(), 'sharingDisabledForUsersCache', [new CappedMemoryCache()]);
}
public function testCacheIncompleteFolder() {
diff --git a/tests/lib/Repair/CleanTagsTest.php b/tests/lib/Repair/CleanTagsTest.php
index 804fa4f66c9..ac79907c525 100644
--- a/tests/lib/Repair/CleanTagsTest.php
+++ b/tests/lib/Repair/CleanTagsTest.php
@@ -25,6 +25,9 @@ class CleanTagsTest extends \Test\TestCase {
/** @var \OCP\IDBConnection */
protected $connection;
+ /** @var \OCP\IUserManager|\PHPUnit_Framework_MockObject_MockObject */
+ protected $userManager;
+
/** @var int */
protected $createdFile;
@@ -38,8 +41,12 @@ class CleanTagsTest extends \Test\TestCase {
->disableOriginalConstructor()
->getMock();
+ $this->userManager = $this->getMockBuilder('\OCP\IUserManager')
+ ->disableOriginalConstructor()
+ ->getMock();
+
$this->connection = \OC::$server->getDatabaseConnection();
- $this->repair = new \OC\Repair\CleanTags($this->connection);
+ $this->repair = new \OC\Repair\CleanTags($this->connection, $this->userManager);
$this->cleanUpTables();
}
@@ -86,6 +93,20 @@ class CleanTagsTest extends \Test\TestCase {
self::invokePrivate($this->repair, 'deleteOrphanCategoryEntries', [$this->outputMock]);
$this->assertEntryCount('vcategory_to_object', 2, 'Assert tag entries count after cleaning category entries');
$this->assertEntryCount('vcategory', 2, 'Assert tag categories count after cleaning category entries');
+
+
+ $this->addTagCategory('TestRepairCleanTags', 'contacts', 'userExists'); // Retained
+ $this->assertEntryCount('vcategory', 3, 'Assert tag categories count before cleaning categories by users');
+
+ $this->userManager->expects($this->exactly(2))
+ ->method('userExists')
+ ->willReturnMap([
+ ['userExists', true],
+ ['TestRepairCleanTags', false],
+ ]);
+
+ self::invokePrivate($this->repair, 'deleteOrphanTags', [$this->outputMock]);
+ $this->assertEntryCount('vcategory', 1, 'Assert tag categories count after cleaning categories by users');
}
/**
@@ -107,13 +128,14 @@ class CleanTagsTest extends \Test\TestCase {
*
* @param string $category
* @param string $type
+ * @param string $user
* @return int
*/
- protected function addTagCategory($category, $type) {
+ protected function addTagCategory($category, $type, $user = 'TestRepairCleanTags') {
$qb = $this->connection->getQueryBuilder();
$qb->insert('vcategory')
->values([
- 'uid' => $qb->createNamedParameter('TestRepairCleanTags'),
+ 'uid' => $qb->createNamedParameter($user),
'category' => $qb->createNamedParameter($category),
'type' => $qb->createNamedParameter($type),
])
diff --git a/tests/lib/Repair/RemoveRootSharesTest.php b/tests/lib/Repair/RemoveRootSharesTest.php
new file mode 100644
index 00000000000..bf255fc7e9b
--- /dev/null
+++ b/tests/lib/Repair/RemoveRootSharesTest.php
@@ -0,0 +1,194 @@
+<?php
+/**
+ * @author Roeland Jago Douma <rullzer@owncloud.com>
+ *
+ * @copyright Copyright (c) 2016, ownCloud, Inc.
+ * @license AGPL-3.0
+ *
+ * This code is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License, version 3,
+ * as published by the Free Software Foundation.
+ *
+ * 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, version 3,
+ * along with this program. If not, see <http://www.gnu.org/licenses/>
+ *
+ */
+namespace Test\Repair;
+
+use OC\Repair\RemoveRootShares;
+use OCP\Files\IRootFolder;
+use OCP\IDBConnection;
+use OCP\IUserManager;
+use OCP\Migration\IOutput;
+use Test\Traits\UserTrait;
+
+/**
+ * Class RemoveOldSharesTest
+ *
+ * @package Test\Repair
+ * @group DB
+ */
+class RemoveRootSharesTest extends \Test\TestCase {
+ use UserTrait;
+
+ /** @var RemoveRootShares */
+ protected $repair;
+
+ /** @var IDBConnection */
+ protected $connection;
+
+ /** @var IOutput */
+ private $outputMock;
+
+ /** @var IUserManager */
+ private $userManager;
+
+ /** @var IRootFolder */
+ private $rootFolder;
+
+ protected function setUp() {
+ parent::setUp();
+
+ $this->outputMock = $this->getMockBuilder('\OCP\Migration\IOutput')
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ $this->userManager = \OC::$server->getUserManager();
+ $this->rootFolder = \OC::$server->getRootFolder();
+
+ $this->connection = \OC::$server->getDatabaseConnection();
+ $this->repair = new RemoveRootShares($this->connection, $this->userManager, $this->rootFolder);
+ }
+
+ protected function tearDown() {
+ $qb = $this->connection->getQueryBuilder();
+ $qb->delete('share');
+ $qb->execute();
+
+ return parent::tearDown();
+ }
+
+ public function testRootSharesExist() {
+ //Add test user
+ $user = $this->userManager->createUser('test', 'test');
+ $userFolder = $this->rootFolder->getUserFolder('test');
+ $fileId = $userFolder->getId();
+
+ //Now insert cyclic share
+ $qb = $this->connection->getQueryBuilder();
+ $qb->insert('share')
+ ->values([
+ 'share_type' => $qb->createNamedParameter(0),
+ 'share_with' => $qb->createNamedParameter('foo'),
+ 'uid_owner' => $qb->createNamedParameter('owner'),
+ 'item_type' => $qb->createNamedParameter('file'),
+ 'item_source' => $qb->createNamedParameter($fileId),
+ 'item_target' => $qb->createNamedParameter('/target'),
+ 'file_source' => $qb->createNamedParameter($fileId),
+ 'file_target' => $qb->createNamedParameter('/target'),
+ 'permissions' => $qb->createNamedParameter(1),
+ ]);
+ $qb->execute();
+
+ $res = $this->invokePrivate($this->repair, 'rootSharesExist', []);
+ $this->assertTrue($res);
+
+ $user->delete();
+ }
+
+ public function testRootSharesDontExist() {
+ //Add test user
+ $user = $this->userManager->createUser('test', 'test');
+ $userFolder = $this->rootFolder->getUserFolder('test');
+ $fileId = $userFolder->getId();
+
+ //Now insert cyclic share
+ $qb = $this->connection->getQueryBuilder();
+ $qb->insert('share')
+ ->values([
+ 'share_type' => $qb->createNamedParameter(0),
+ 'share_with' => $qb->createNamedParameter('foo'),
+ 'uid_owner' => $qb->createNamedParameter('owner'),
+ 'item_type' => $qb->createNamedParameter('file'),
+ 'item_source' => $qb->createNamedParameter($fileId+1),
+ 'item_target' => $qb->createNamedParameter('/target'),
+ 'file_source' => $qb->createNamedParameter($fileId+1),
+ 'file_target' => $qb->createNamedParameter('/target'),
+ 'permissions' => $qb->createNamedParameter(1),
+ ]);
+ $qb->execute();
+
+ $res = $this->invokePrivate($this->repair, 'rootSharesExist', []);
+ $this->assertFalse($res);
+
+ $user->delete();
+ }
+
+ public function testRun() {
+ //Add test user
+ $user1 = $this->userManager->createUser('test1', 'test1');
+ $userFolder = $this->rootFolder->getUserFolder('test1');
+ $fileId = $userFolder->getId();
+
+ //Now insert cyclic share
+ $qb = $this->connection->getQueryBuilder();
+ $qb->insert('share')
+ ->values([
+ 'share_type' => $qb->createNamedParameter(0),
+ 'share_with' => $qb->createNamedParameter('foo'),
+ 'uid_owner' => $qb->createNamedParameter('owner'),
+ 'item_type' => $qb->createNamedParameter('file'),
+ 'item_source' => $qb->createNamedParameter($fileId),
+ 'item_target' => $qb->createNamedParameter('/target'),
+ 'file_source' => $qb->createNamedParameter($fileId),
+ 'file_target' => $qb->createNamedParameter('/target'),
+ 'permissions' => $qb->createNamedParameter(1),
+ ]);
+ $qb->execute();
+
+ //Add test user
+ $user2 = $this->userManager->createUser('test2', 'test2');
+ $userFolder = $this->rootFolder->getUserFolder('test2');
+ $folder = $userFolder->newFolder('foo');
+ $fileId = $folder->getId();
+
+ //Now insert cyclic share
+ $qb = $this->connection->getQueryBuilder();
+ $qb->insert('share')
+ ->values([
+ 'share_type' => $qb->createNamedParameter(0),
+ 'share_with' => $qb->createNamedParameter('foo'),
+ 'uid_owner' => $qb->createNamedParameter('owner'),
+ 'item_type' => $qb->createNamedParameter('file'),
+ 'item_source' => $qb->createNamedParameter($fileId),
+ 'item_target' => $qb->createNamedParameter('/target'),
+ 'file_source' => $qb->createNamedParameter($fileId),
+ 'file_target' => $qb->createNamedParameter('/target'),
+ 'permissions' => $qb->createNamedParameter(1),
+ ]);
+ $qb->execute();
+
+ $this->repair->run($this->outputMock);
+
+ //Verify
+ $qb = $this->connection->getQueryBuilder();
+ $qb->selectAlias($qb->createFunction('COUNT(*)'), 'count')
+ ->from('share');
+
+ $cursor = $qb->execute();
+ $data = $cursor->fetch();
+ $cursor->closeCursor();
+
+ $count = (int)$data['count'];
+
+ $this->assertEquals(1, $count);
+
+ $user1->delete();
+ $user2->delete();
+ }
+}
diff --git a/tests/lib/User/SessionTest.php b/tests/lib/User/SessionTest.php
index eac38ebba16..7a34d42a2bc 100644
--- a/tests/lib/User/SessionTest.php
+++ b/tests/lib/User/SessionTest.php
@@ -306,16 +306,21 @@ class SessionTest extends \Test\TestCase {
$userSession->login('foo', 'bar');
}
+ /**
+ * @expectedException \OC\Authentication\Exceptions\PasswordLoginForbiddenException
+ */
public function testLogClientInNoTokenPasswordWith2fa() {
$manager = $this->getMockBuilder('\OC\User\Manager')
->disableOriginalConstructor()
->getMock();
$session = $this->getMock('\OCP\ISession');
+ $request = $this->getMock('\OCP\IRequest');
+ $user = $this->getMock('\OCP\IUser');
/** @var \OC\User\Session $userSession */
$userSession = $this->getMockBuilder('\OC\User\Session')
->setConstructorArgs([$manager, $session, $this->timeFactory, $this->tokenProvider, $this->config])
- ->setMethods(['login'])
+ ->setMethods(['login', 'supportsCookies', 'createSessionToken', 'getUser'])
->getMock();
$this->tokenProvider->expects($this->once())
@@ -327,15 +332,58 @@ class SessionTest extends \Test\TestCase {
->with('token_auth_enforced', false)
->will($this->returnValue(true));
- $this->assertFalse($userSession->logClientIn('john', 'doe'));
+ $userSession->logClientIn('john', 'doe', $request);
+ }
+
+ public function testLogClientInWithTokenPassword() {
+ $manager = $this->getMockBuilder('\OC\User\Manager')
+ ->disableOriginalConstructor()
+ ->getMock();
+ $session = $this->getMock('\OCP\ISession');
+ $request = $this->getMock('\OCP\IRequest');
+ $user = $this->getMock('\OCP\IUser');
+
+ /** @var \OC\User\Session $userSession */
+ $userSession = $this->getMockBuilder('\OC\User\Session')
+ ->setConstructorArgs([$manager, $session, $this->timeFactory, $this->tokenProvider, $this->config])
+ ->setMethods(['isTokenPassword', 'login', 'supportsCookies', 'createSessionToken', 'getUser'])
+ ->getMock();
+
+ $userSession->expects($this->once())
+ ->method('isTokenPassword')
+ ->will($this->returnValue(true));
+ $userSession->expects($this->once())
+ ->method('login')
+ ->with('john', 'doe')
+ ->will($this->returnValue(true));
+
+ $userSession->expects($this->once())
+ ->method('supportsCookies')
+ ->with($request)
+ ->will($this->returnValue(true));
+ $userSession->expects($this->once())
+ ->method('getUser')
+ ->will($this->returnValue($user));
+ $user->expects($this->once())
+ ->method('getUID')
+ ->will($this->returnValue('user123'));
+ $userSession->expects($this->once())
+ ->method('createSessionToken')
+ ->with($request, 'user123', 'john', 'doe');
+
+ $this->assertTrue($userSession->logClientIn('john', 'doe', $request));
}
+ /**
+ * @expectedException \OC\Authentication\Exceptions\PasswordLoginForbiddenException
+ */
public function testLogClientInNoTokenPasswordNo2fa() {
$manager = $this->getMockBuilder('\OC\User\Manager')
->disableOriginalConstructor()
->getMock();
$session = $this->getMock('\OCP\ISession');
$user = $this->getMock('\OCP\IUser');
+ $request = $this->getMock('\OCP\IRequest');
/** @var \OC\User\Session $userSession */
$userSession = $this->getMockBuilder('\OC\User\Session')
@@ -357,7 +405,7 @@ class SessionTest extends \Test\TestCase {
->with('john')
->will($this->returnValue(true));
- $this->assertFalse($userSession->logClientIn('john', 'doe'));
+ $userSession->logClientIn('john', 'doe', $request);
}
public function testRememberLoginValidToken() {