aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/core/lostpassword/controller/lostcontrollertest.php64
-rw-r--r--tests/lib/config.php30
-rw-r--r--tests/lib/connector/sabre/file.php25
-rw-r--r--tests/lib/files/filesystem.php22
-rw-r--r--tests/lib/files/mapper.php9
-rw-r--r--tests/lib/files/mount/mountpoint.php69
-rw-r--r--tests/lib/files/node/integration.php4
-rw-r--r--tests/lib/files/view.php90
-rw-r--r--tests/lib/security/certificatemanager.php8
-rw-r--r--tests/settings/controller/logsettingscontrollertest.php1
-rw-r--r--tests/settings/controller/mailsettingscontrollertest.php63
-rw-r--r--tests/settings/controller/userscontrollertest.php793
-rw-r--r--tests/settings/middleware/subadminmiddlewaretest.php6
13 files changed, 1049 insertions, 135 deletions
diff --git a/tests/core/lostpassword/controller/lostcontrollertest.php b/tests/core/lostpassword/controller/lostcontrollertest.php
index 2ed7692a32f..b03cbd7c42f 100644
--- a/tests/core/lostpassword/controller/lostcontrollertest.php
+++ b/tests/core/lostpassword/controller/lostcontrollertest.php
@@ -29,6 +29,12 @@ class LostControllerTest extends \PHPUnit_Framework_TestCase {
->disableOriginalConstructor()->getMock();
$this->container['L10N'] = $this->getMockBuilder('\OCP\IL10N')
->disableOriginalConstructor()->getMock();
+ $this->container['L10N']
+ ->expects($this->any())
+ ->method('t')
+ ->will($this->returnCallback(function($text, $parameters = array()) {
+ return vsprintf($text, $parameters);
+ }));
$this->container['Defaults'] = $this->getMockBuilder('\OC_Defaults')
->disableOriginalConstructor()->getMock();
$this->container['UserManager'] = $this->getMockBuilder('\OCP\IUserManager')
@@ -73,21 +79,13 @@ class LostControllerTest extends \PHPUnit_Framework_TestCase {
array(true, $existingUser),
array(false, $nonExistingUser)
)));
- $this->container['L10N']
- ->expects($this->any())
- ->method('t')
- ->will(
- $this->returnValueMap(
- array(
- array('Couldn\'t send reset email. Please make sure your username is correct.', array(),
- 'Couldn\'t send reset email. Please make sure your username is correct.'),
-
- )
- ));
// With a non existing user
$response = $this->lostController->email($nonExistingUser);
- $expectedResponse = array('status' => 'error', 'msg' => 'Couldn\'t send reset email. Please make sure your username is correct.');
+ $expectedResponse = [
+ 'status' => 'error',
+ 'msg' => 'Couldn\'t send reset email. Please make sure your username is correct.'
+ ];
$this->assertSame($expectedResponse, $response);
// With no mail address
@@ -97,7 +95,10 @@ class LostControllerTest extends \PHPUnit_Framework_TestCase {
->with($existingUser, 'settings', 'email')
->will($this->returnValue(null));
$response = $this->lostController->email($existingUser);
- $expectedResponse = array('status' => 'error', 'msg' => 'Couldn\'t send reset email. Please make sure your username is correct.');
+ $expectedResponse = [
+ 'status' => 'error',
+ 'msg' => 'Couldn\'t send reset email. Please make sure your username is correct.'
+ ];
$this->assertSame($expectedResponse, $response);
}
@@ -146,31 +147,24 @@ class LostControllerTest extends \PHPUnit_Framework_TestCase {
}
public function testSetPasswordUnsuccessful() {
- $this->container['L10N']
- ->expects($this->any())
- ->method('t')
- ->will(
- $this->returnValueMap(
- array(
- array('Couldn\'t reset password because the token is invalid', array(),
- 'Couldn\'t reset password because the token is invalid'),
- )
- ));
$this->container['Config']
->expects($this->once())
->method('getUserValue')
- ->with('InvalidTokenUser', 'owncloud', 'lostpassword')
+ ->with('InvalidTokenUser', 'owncloud', 'lostpassword', null)
->will($this->returnValue('TheOnlyAndOnlyOneTokenToResetThePassword'));
// With an invalid token
$userName = 'InvalidTokenUser';
$response = $this->lostController->setPassword('wrongToken', $userName, 'NewPassword', true);
- $expectedResponse = array('status' => 'error', 'msg' => 'Couldn\'t reset password because the token is invalid');
+ $expectedResponse = [
+ 'status' => 'error',
+ 'msg' => 'Couldn\'t reset password because the token is invalid'
+ ];
$this->assertSame($expectedResponse, $response);
// With a valid token and no proceed
$response = $this->lostController->setPassword('TheOnlyAndOnlyOneTokenToResetThePassword!', $userName, 'NewPassword', false);
- $expectedResponse = array('status' => 'error', 'msg' => '', 'encryption' => true);
+ $expectedResponse = ['status' => 'error', 'msg' => '', 'encryption' => true];
$this->assertSame($expectedResponse, $response);
}
@@ -178,7 +172,7 @@ class LostControllerTest extends \PHPUnit_Framework_TestCase {
$this->container['Config']
->expects($this->once())
->method('getUserValue')
- ->with('ValidTokenUser', 'owncloud', 'lostpassword')
+ ->with('ValidTokenUser', 'owncloud', 'lostpassword', null)
->will($this->returnValue('TheOnlyAndOnlyOneTokenToResetThePassword'));
$user = $this->getMockBuilder('\OCP\IUser')
->disableOriginalConstructor()->getMock();
@@ -200,4 +194,20 @@ class LostControllerTest extends \PHPUnit_Framework_TestCase {
$expectedResponse = array('status' => 'success');
$this->assertSame($expectedResponse, $response);
}
+
+ public function testIsSetPasswordWithoutTokenFailing() {
+ $this->container['Config']
+ ->expects($this->once())
+ ->method('getUserValue')
+ ->with('ValidTokenUser', 'owncloud', 'lostpassword', null)
+ ->will($this->returnValue(null));
+
+ $response = $this->lostController->setPassword('', 'ValidTokenUser', 'NewPassword', true);
+ $expectedResponse = [
+ 'status' => 'error',
+ 'msg' => 'Couldn\'t reset password because the token is invalid'
+ ];
+ $this->assertSame($expectedResponse, $response);
+ }
+
}
diff --git a/tests/lib/config.php b/tests/lib/config.php
index 6adba356a1c..91154579ab5 100644
--- a/tests/lib/config.php
+++ b/tests/lib/config.php
@@ -71,6 +71,36 @@ class Test_Config extends \Test\TestCase {
$this->assertEquals($expected, $content);
}
+ public function testSetValues() {
+ $content = file_get_contents($this->configFile);
+ $this->assertEquals(self::TESTCONTENT, $content);
+
+ // Changing configs to existing values and deleting non-existing once
+ // should not rewrite the config.php
+ $this->config->setValues([
+ 'foo' => 'bar',
+ 'not_exists' => null,
+ ]);
+
+ $this->assertAttributeEquals($this->initialConfig, 'cache', $this->config);
+ $content = file_get_contents($this->configFile);
+ $this->assertEquals(self::TESTCONTENT, $content);
+
+ $this->config->setValues([
+ 'foo' => 'moo',
+ 'alcohol_free' => null,
+ ]);
+ $expectedConfig = $this->initialConfig;
+ $expectedConfig['foo'] = 'moo';
+ unset($expectedConfig['alcohol_free']);
+ $this->assertAttributeEquals($expectedConfig, 'cache', $this->config);
+
+ $content = file_get_contents($this->configFile);
+ $expected = "<?php\n\$CONFIG = array (\n 'foo' => 'moo',\n 'beers' => \n array (\n 0 => 'Appenzeller',\n " .
+ " 1 => 'Guinness',\n 2 => 'Kölsch',\n ),\n);\n";
+ $this->assertEquals($expected, $content);
+ }
+
public function testDeleteKey() {
$this->config->deleteKey('foo');
$expectedConfig = $this->initialConfig;
diff --git a/tests/lib/connector/sabre/file.php b/tests/lib/connector/sabre/file.php
index 6bb1b4e75d1..33dc78f87d8 100644
--- a/tests/lib/connector/sabre/file.php
+++ b/tests/lib/connector/sabre/file.php
@@ -32,6 +32,31 @@ class Test_OC_Connector_Sabre_File extends \Test\TestCase {
$file->put('test data');
}
+ public function testPutSingleFileShare() {
+ // setup
+ $storage = $this->getMock('\OCP\Files\Storage');
+ $view = $this->getMock('\OC\Files\View', array('file_put_contents', 'getRelativePath'), array());
+ $view->expects($this->any())
+ ->method('resolvePath')
+ ->with('')
+ ->will($this->returnValue(array($storage, '')));
+ $view->expects($this->any())
+ ->method('getRelativePath')
+ ->will($this->returnValue(''));
+ $view->expects($this->any())
+ ->method('file_put_contents')
+ ->with('')
+ ->will($this->returnValue(true));
+
+ $info = new \OC\Files\FileInfo('/foo.txt', null, null, array(
+ 'permissions' => \OCP\Constants::PERMISSION_ALL
+ ), null);
+
+ $file = new OC_Connector_Sabre_File($view, $info);
+
+ $this->assertNotEmpty($file->put('test data'));
+ }
+
/**
* @expectedException \Sabre\DAV\Exception
*/
diff --git a/tests/lib/files/filesystem.php b/tests/lib/files/filesystem.php
index 888690adb0e..7bf59315d77 100644
--- a/tests/lib/files/filesystem.php
+++ b/tests/lib/files/filesystem.php
@@ -187,6 +187,28 @@ class Filesystem extends \Test\TestCase {
$this->assertSame($expected, \OC\Files\Filesystem::isValidPath($path));
}
+ public function isFileBlacklistedData() {
+ return array(
+ array('/etc/foo/bar/foo.txt', false),
+ array('\etc\foo/bar\foo.txt', false),
+ array('.htaccess', true),
+ array('.htaccess/', true),
+ array('.htaccess\\', true),
+ array('/etc/foo\bar/.htaccess\\', true),
+ array('/etc/foo\bar/.htaccess/', true),
+ array('/etc/foo\bar/.htaccess/foo', false),
+ array('//foo//bar/\.htaccess/', true),
+ array('\foo\bar\.HTAccess', true),
+ );
+ }
+
+ /**
+ * @dataProvider isFileBlacklistedData
+ */
+ public function testIsFileBlacklisted($path, $expected) {
+ $this->assertSame($expected, \OC\Files\Filesystem::isFileBlacklisted($path));
+ }
+
public function normalizePathWindowsAbsolutePathData() {
return array(
array('C:/', 'C:\\'),
diff --git a/tests/lib/files/mapper.php b/tests/lib/files/mapper.php
index 18161734b60..cd35d4f8fc3 100644
--- a/tests/lib/files/mapper.php
+++ b/tests/lib/files/mapper.php
@@ -68,6 +68,15 @@ class Mapper extends \Test\TestCase {
*/
array('D:/' . md5('ありがとう'), 'D:/ありがとう'),
array('D:/' . md5('ありがとう') . '/issue6722.txt', 'D:/ありがとう/issue6722.txt'),
+ array('D:/' . md5('.htaccess'), 'D:/.htaccess'),
+ array('D:/' . md5('.htaccess.'), 'D:/.htaccess.'),
+ array('D:/' . md5('.htAccess'), 'D:/.htAccess'),
+ array('D:/' . md5('.htAccess\\…\\') . '/a', 'D:/.htAccess\…\/とa'),
+ array('D:/' . md5('.htaccess-'), 'D:/.htaccess-'),
+ array('D:/' . md5('.htaあccess'), 'D:/.htaあccess'),
+ array('D:/' . md5(' .htaccess'), 'D:/ .htaccess'),
+ array('D:/' . md5('.htaccess '), 'D:/.htaccess '),
+ array('D:/' . md5(' .htaccess '), 'D:/ .htaccess '),
);
}
diff --git a/tests/lib/files/mount/mountpoint.php b/tests/lib/files/mount/mountpoint.php
new file mode 100644
index 00000000000..5a9c6de3e0a
--- /dev/null
+++ b/tests/lib/files/mount/mountpoint.php
@@ -0,0 +1,69 @@
+<?php
+/**
+ * Copyright (c) 2015 Vincent Petry <pvince81@owncloud.com>
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ */
+
+namespace Test\Files\Mount;
+
+class MountPoint extends \Test\TestCase {
+
+ public function testGetStorage() {
+ $storage = $this->getMock('\OCP\Files\Storage');
+ $storage->expects($this->once())
+ ->method('getId')
+ ->will($this->returnValue(123));
+
+ $loader = $this->getMock('\OCP\Files\Storage\IStorageFactory');
+ $loader->expects($this->once())
+ ->method('getInstance')
+ ->will($this->returnValue($storage));
+
+ $mountPoint = new \OC\Files\Mount\MountPoint(
+ // just use this because a real class is needed
+ '\Test\Files\Mount\MountPoint',
+ '/mountpoint',
+ null,
+ $loader
+ );
+
+ $this->assertEquals($storage, $mountPoint->getStorage());
+ $this->assertEquals(123, $mountPoint->getStorageId());
+ }
+
+ public function testInvalidStorage() {
+ $loader = $this->getMock('\OCP\Files\Storage\IStorageFactory');
+ $loader->expects($this->once())
+ ->method('getInstance')
+ ->will($this->throwException(new \Exception('Test storage init exception')));
+
+ $called = false;
+ $wrapper = function($mountPoint, $storage) use ($called) {
+ $called = true;
+ };
+
+ $mountPoint = new \OC\Files\Mount\MountPoint(
+ // just use this because a real class is needed
+ '\Test\Files\Mount\MountPoint',
+ '/mountpoint',
+ null,
+ $loader
+ );
+
+ $this->assertNull($mountPoint->getStorage());
+ // call it again to make sure the init code only ran once
+ $this->assertNull($mountPoint->getStorage());
+
+ $this->assertNull($mountPoint->getStorageId());
+
+ // wrapping doesn't fail
+ $mountPoint->wrapStorage($wrapper);
+
+ $this->assertNull($mountPoint->getStorage());
+
+ // storage wrapper never called
+ $this->assertFalse($called);
+ }
+}
diff --git a/tests/lib/files/node/integration.php b/tests/lib/files/node/integration.php
index d8c180cc844..456a4a0e287 100644
--- a/tests/lib/files/node/integration.php
+++ b/tests/lib/files/node/integration.php
@@ -80,7 +80,9 @@ class IntegrationTests extends \Test\TestCase {
$this->assertEquals('text/plain', $file->getMimeType());
$this->assertEquals('qwerty', $file->getContent());
$this->assertFalse($this->root->nodeExists('/bar.txt'));
- $file->move('/bar.txt');
+ $target = $file->move('/bar.txt');
+ $this->assertEquals($id, $target->getId());
+ $this->assertEquals($id, $file->getId());
$this->assertFalse($this->root->nodeExists('/foo.txt'));
$this->assertTrue($this->root->nodeExists('/bar.txt'));
$this->assertEquals('bar.txt', $file->getName());
diff --git a/tests/lib/files/view.php b/tests/lib/files/view.php
index 3ff19d7385d..f6af59d52be 100644
--- a/tests/lib/files/view.php
+++ b/tests/lib/files/view.php
@@ -613,7 +613,7 @@ class View extends \Test\TestCase {
if (\OC_Util::runningOnWindows()) {
$this->markTestSkipped('[Windows] ');
$depth = ((260 - $tmpdirLength) / 57);
- }elseif(\OC_Util::runningOnMac()){
+ } elseif (\OC_Util::runningOnMac()) {
$depth = ((1024 - $tmpdirLength) / 57);
} else {
$depth = ((4000 - $tmpdirLength) / 57);
@@ -730,6 +730,44 @@ class View extends \Test\TestCase {
}
/**
+ * @dataProvider relativePathProvider
+ */
+ function testGetRelativePath($absolutePath, $expectedPath) {
+ $view = new \OC\Files\View('/files');
+ // simulate a external storage mount point which has a trailing slash
+ $view->chroot('/files/');
+ $this->assertEquals($expectedPath, $view->getRelativePath($absolutePath));
+ }
+
+ function relativePathProvider() {
+ return array(
+ array('/files/', '/'),
+ array('/files', '/'),
+ array('/files/0', '0'),
+ array('/files/false', 'false'),
+ array('/files/true', 'true'),
+ array('/files/test', 'test'),
+ array('/files/test/foo', 'test/foo'),
+ );
+ }
+
+ public function testFileView() {
+ $storage = new Temporary(array());
+ $scanner = $storage->getScanner();
+ $storage->file_put_contents('foo.txt', 'bar');
+ \OC\Files\Filesystem::mount($storage, array(), '/test/');
+ $scanner->scan('');
+ $view = new \OC\Files\View('/test/foo.txt');
+
+ $this->assertEquals('bar', $view->file_get_contents(''));
+ $fh = tmpfile();
+ fwrite($fh, 'foo');
+ rewind($fh);
+ $view->file_put_contents('', $fh);
+ $this->assertEquals('foo', $view->file_get_contents(''));
+ }
+
+ /**
* @dataProvider tooLongPathDataProvider
* @expectedException \OCP\Files\InvalidPathException
*/
@@ -806,4 +844,54 @@ class View extends \Test\TestCase {
array('putFileInfo', array()),
);
}
+
+ public function testRenameCrossStoragePreserveMtime() {
+ $storage1 = new Temporary(array());
+ $storage2 = new Temporary(array());
+ $scanner1 = $storage1->getScanner();
+ $scanner2 = $storage2->getScanner();
+ $storage1->mkdir('sub');
+ $storage1->mkdir('foo');
+ $storage1->file_put_contents('foo.txt', 'asd');
+ $storage1->file_put_contents('foo/bar.txt', 'asd');
+ \OC\Files\Filesystem::mount($storage1, array(), '/test/');
+ \OC\Files\Filesystem::mount($storage2, array(), '/test/sub/storage');
+
+ $view = new \OC\Files\View('');
+ $time = time() - 200;
+ $view->touch('/test/foo.txt', $time);
+ $view->touch('/test/foo', $time);
+ $view->touch('/test/foo/bar.txt', $time);
+
+ $view->rename('/test/foo.txt', '/test/sub/storage/foo.txt');
+
+ $this->assertEquals($time, $view->filemtime('/test/sub/storage/foo.txt'));
+
+ $view->rename('/test/foo', '/test/sub/storage/foo');
+
+ $this->assertEquals($time, $view->filemtime('/test/sub/storage/foo/bar.txt'));
+ }
+
+ public function testDeleteFailKeepCache() {
+ /**
+ * @var \PHPUnit_Framework_MockObject_MockObject | \OC\Files\Storage\Temporary $storage
+ */
+ $storage = $this->getMockBuilder('\OC\Files\Storage\Temporary')
+ ->setConstructorArgs(array(array()))
+ ->setMethods(array('unlink'))
+ ->getMock();
+ $storage->expects($this->once())
+ ->method('unlink')
+ ->will($this->returnValue(false));
+ $scanner = $storage->getScanner();
+ $cache = $storage->getCache();
+ $storage->file_put_contents('foo.txt', 'asd');
+ $scanner->scan('');
+ \OC\Files\Filesystem::mount($storage, array(), '/test/');
+
+ $view = new \OC\Files\View('/test');
+
+ $this->assertFalse($view->unlink('foo.txt'));
+ $this->assertTrue($cache->inCache('foo.txt'));
+ }
}
diff --git a/tests/lib/security/certificatemanager.php b/tests/lib/security/certificatemanager.php
index cff6932b670..1167fe3d868 100644
--- a/tests/lib/security/certificatemanager.php
+++ b/tests/lib/security/certificatemanager.php
@@ -28,9 +28,7 @@ class CertificateManagerTest extends \Test\TestCase {
\OC\Files\Filesystem::tearDown();
\OC_Util::setupFS($this->username);
- $this->user = \OC::$server->getUserManager()->get($this->username);
-
- $this->certificateManager = new CertificateManager($this->user);
+ $this->certificateManager = new CertificateManager($this->username, new \OC\Files\View());
}
protected function tearDown() {
@@ -84,7 +82,7 @@ class CertificateManagerTest extends \Test\TestCase {
}
function testGetCertificateBundle() {
- $this->assertSame($this->user->getHome().'/files_external/rootcerts.crt', $this->certificateManager->getCertificateBundle());
+ $this->assertSame('/' . $this->username . '/files_external/rootcerts.crt', $this->certificateManager->getCertificateBundle());
}
-} \ No newline at end of file
+}
diff --git a/tests/settings/controller/logsettingscontrollertest.php b/tests/settings/controller/logsettingscontrollertest.php
index e80acfa75b5..84581bf5782 100644
--- a/tests/settings/controller/logsettingscontrollertest.php
+++ b/tests/settings/controller/logsettingscontrollertest.php
@@ -10,6 +10,7 @@
namespace Test\Settings\Controller;
use \OC\Settings\Application;
+use OC\Settings\Controller\LogSettingsController;
/**
* @package OC\Settings\Controller
diff --git a/tests/settings/controller/mailsettingscontrollertest.php b/tests/settings/controller/mailsettingscontrollertest.php
index f6ebade7b17..ed33d7fbe49 100644
--- a/tests/settings/controller/mailsettingscontrollertest.php
+++ b/tests/settings/controller/mailsettingscontrollertest.php
@@ -69,26 +69,37 @@ class MailSettingsControllerTest extends \Test\TestCase {
);
*/
- $this->container['Config']
- ->expects($this->exactly(15))
- ->method('setSystemValue');
-
+ /** @var \PHPUnit_Framework_MockObject_MockObject $config */
+ $config = $this->container['Config'];
+ $config->expects($this->exactly(2))
+ ->method('setSystemValues');
/**
* FIXME: Use the following block once Jenkins uses PHPUnit >= 4.1
- */
- /*
- $this->container['Config']
- ->expects($this->exactly(3))
- ->method('deleteSystemValue')
->withConsecutive(
- array($this->equalTo('mail_smtpauth')),
- array($this->equalTo('mail_smtpname')),
- array($this->equalTo('mail_smtppassword'))
+ [[
+ 'mail_domain' => 'owncloud.com',
+ 'mail_from_address' => 'demo@owncloud.com',
+ 'mail_smtpmode' => 'smtp',
+ 'mail_smtpsecure' => 'ssl',
+ 'mail_smtphost' => 'mx.owncloud.org',
+ 'mail_smtpauthtype' => 'NTLM',
+ 'mail_smtpauth' => 1,
+ 'mail_smtpport' => '25',
+ ]],
+ [[
+ 'mail_domain' => 'owncloud.com',
+ 'mail_from_address' => 'demo@owncloud.com',
+ 'mail_smtpmode' => 'smtp',
+ 'mail_smtpsecure' => 'ssl',
+ 'mail_smtphost' => 'mx.owncloud.org',
+ 'mail_smtpauthtype' => 'NTLM',
+ 'mail_smtpauth' => null,
+ 'mail_smtpport' => '25',
+ 'mail_smtpname' => null,
+ 'mail_smtppassword' => null,
+ ]]
);
- */
- $this->container['Config']
- ->expects($this->exactly(3))
- ->method('deleteSystemValue');
+ */
// With authentication
$response = $this->container['MailSettingsController']->setMailSettings(
@@ -126,21 +137,13 @@ class MailSettingsControllerTest extends \Test\TestCase {
->method('t')
->will($this->returnValue('Saved'));
- /**
- * FIXME: Use this block once Jenkins uses PHPUnit >= 4.1
- */
- /*
$this->container['Config']
- ->expects($this->exactly(2))
- ->method('setSystemValue')
- ->withConsecutive(
- array($this->equalTo('mail_smtpname'), $this->equalTo('UsernameToStore')),
- array($this->equalTo('mail_smtppassword'), $this->equalTo('PasswordToStore'))
- );
- */
- $this->container['Config']
- ->expects($this->exactly(2))
- ->method('setSystemValue');
+ ->expects($this->once())
+ ->method('setSystemValues')
+ ->with([
+ 'mail_smtpname' => 'UsernameToStore',
+ 'mail_smtppassword' => 'PasswordToStore',
+ ]);
$response = $this->container['MailSettingsController']->storeCredentials('UsernameToStore', 'PasswordToStore');
$expectedResponse = array('data' => array('message' =>'Saved'), 'status' => 'success');
diff --git a/tests/settings/controller/userscontrollertest.php b/tests/settings/controller/userscontrollertest.php
index c6506ee440b..53a42de62ab 100644
--- a/tests/settings/controller/userscontrollertest.php
+++ b/tests/settings/controller/userscontrollertest.php
@@ -1,7 +1,7 @@
<?php
/**
* @author Lukas Reschke
- * @copyright 2014 Lukas Reschke lukas@owncloud.com
+ * @copyright 2014-2015 Lukas Reschke lukas@owncloud.com
*
* This file is licensed under the Affero General Public License version 3 or
* later.
@@ -21,9 +21,6 @@ class UsersControllerTest extends \Test\TestCase {
/** @var \OCP\AppFramework\IAppContainer */
private $container;
- /** @var UsersController */
- private $usersController;
-
protected function setUp() {
$app = new Application();
$this->container = $app->getContainer();
@@ -36,9 +33,10 @@ class UsersControllerTest extends \Test\TestCase {
->disableOriginalConstructor()->getMock();
$this->container['L10N'] = $this->getMockBuilder('\OCP\IL10N')
->disableOriginalConstructor()->getMock();
+ $this->container['SubAdminFactory'] = $this->getMockBuilder('\OC\Settings\Factory\SubAdminFactory')
+ ->disableOriginalConstructor()->getMock();
$this->container['Config'] = $this->getMockBuilder('\OCP\IConfig')
->disableOriginalConstructor()->getMock();
- $this->container['IsAdmin'] = true;
$this->container['L10N']
->expects($this->any())
->method('t')
@@ -54,16 +52,13 @@ class UsersControllerTest extends \Test\TestCase {
->disableOriginalConstructor()->getMock();
$this->container['URLGenerator'] = $this->getMockBuilder('\OCP\IURLGenerator')
->disableOriginalConstructor()->getMock();
-
- $this->usersController = $this->container['UsersController'];
-
+ $this->container['OCP\\App\\IAppManager'] = $this->getMockBuilder('OCP\\App\\IAppManager')
+ ->disableOriginalConstructor()->getMock();
}
- /**
- * TODO: Since the function uses the static OC_Subadmin class it can't be mocked
- * to test for subadmins. Thus the test always assumes you have admin permissions...
- */
- public function testIndex() {
+ public function testIndexAdmin() {
+ $this->container['IsAdmin'] = true;
+
$foo = $this->getMockBuilder('\OC\User\User')
->disableOriginalConstructor()->getMock();
$foo
@@ -169,7 +164,8 @@ class UsersControllerTest extends \Test\TestCase {
'storageLocation' => '/home/foo',
'lastLogin' => 500,
'backend' => 'OC_User_Database',
- 'email' => 'foo@bar.com'
+ 'email' => 'foo@bar.com',
+ 'isRestoreDisabled' => false,
),
1 => array(
'name' => 'admin',
@@ -180,7 +176,8 @@ class UsersControllerTest extends \Test\TestCase {
'storageLocation' => '/home/admin',
'lastLogin' => 12,
'backend' => 'OC_User_Dummy',
- 'email' => 'admin@bar.com'
+ 'email' => 'admin@bar.com',
+ 'isRestoreDisabled' => false,
),
2 => array(
'name' => 'bar',
@@ -191,11 +188,181 @@ class UsersControllerTest extends \Test\TestCase {
'storageLocation' => '/home/bar',
'lastLogin' => 3999,
'backend' => 'OC_User_Dummy',
- 'email' => 'bar@dummy.com'
+ 'email' => 'bar@dummy.com',
+ 'isRestoreDisabled' => false,
),
)
);
- $response = $this->usersController->index(0, 10, 'gid', 'pattern');
+ $response = $this->container['UsersController']->index(0, 10, 'gid', 'pattern');
+ $this->assertEquals($expectedResponse, $response);
+ }
+
+ public function testIndexSubAdmin() {
+ $this->container['IsAdmin'] = false;
+ $this->container['SubAdminFactory']
+ ->expects($this->once())
+ ->method('getSubAdminsOfGroups')
+ ->with('username')
+ ->will($this->returnValue(['SubGroup1', 'SubGroup2']));
+
+ $user = $this->getMockBuilder('\OC\User\User')
+ ->disableOriginalConstructor()->getMock();
+ $user
+ ->expects($this->once())
+ ->method('getUID')
+ ->will($this->returnValue('username'));
+ $this->container['UserSession']
+ ->expects($this->once())
+ ->method('getUser')
+ ->will($this->returnValue($user));
+
+ $foo = $this->getMockBuilder('\OC\User\User')
+ ->disableOriginalConstructor()->getMock();
+ $foo
+ ->expects($this->exactly(4))
+ ->method('getUID')
+ ->will($this->returnValue('foo'));
+ $foo
+ ->expects($this->once())
+ ->method('getDisplayName')
+ ->will($this->returnValue('M. Foo'));
+ $foo
+ ->method('getLastLogin')
+ ->will($this->returnValue(500));
+ $foo
+ ->method('getHome')
+ ->will($this->returnValue('/home/foo'));
+ $foo
+ ->expects($this->once())
+ ->method('getBackendClassName')
+ ->will($this->returnValue('OC_User_Database'));
+ $admin = $this->getMockBuilder('\OC\User\User')
+ ->disableOriginalConstructor()->getMock();
+ $admin
+ ->expects($this->exactly(4))
+ ->method('getUID')
+ ->will($this->returnValue('admin'));
+ $admin
+ ->expects($this->once())
+ ->method('getDisplayName')
+ ->will($this->returnValue('S. Admin'));
+ $admin
+ ->expects($this->once())
+ ->method('getLastLogin')
+ ->will($this->returnValue(12));
+ $admin
+ ->expects($this->once())
+ ->method('getHome')
+ ->will($this->returnValue('/home/admin'));
+ $admin
+ ->expects($this->once())
+ ->method('getBackendClassName')
+ ->will($this->returnValue('OC_User_Dummy'));
+ $bar = $this->getMockBuilder('\OC\User\User')
+ ->disableOriginalConstructor()->getMock();
+ $bar
+ ->expects($this->exactly(4))
+ ->method('getUID')
+ ->will($this->returnValue('bar'));
+ $bar
+ ->expects($this->once())
+ ->method('getDisplayName')
+ ->will($this->returnValue('B. Ar'));
+ $bar
+ ->method('getLastLogin')
+ ->will($this->returnValue(3999));
+ $bar
+ ->method('getHome')
+ ->will($this->returnValue('/home/bar'));
+ $bar
+ ->expects($this->once())
+ ->method('getBackendClassName')
+ ->will($this->returnValue('OC_User_Dummy'));
+
+ $this->container['GroupManager']
+ ->expects($this->at(0))
+ ->method('displayNamesInGroup')
+ ->with('SubGroup1', 'pattern')
+ ->will($this->returnValue(['foo' => 'M. Foo', 'admin' => 'S. Admin']));
+ $this->container['GroupManager']
+ ->expects($this->at(1))
+ ->method('displayNamesInGroup')
+ ->with('SubGroup2', 'pattern')
+ ->will($this->returnValue(['bar' => 'B. Ar']));
+ $this->container['GroupManager']
+ ->expects($this->exactly(3))
+ ->method('getUserGroupIds')
+ ->will($this->onConsecutiveCalls(
+ ['SubGroup2', 'SubGroup1'],
+ ['SubGroup2', 'Foo'],
+ ['admin', 'SubGroup1', 'testGroup']
+ ));
+ $this->container['UserManager']
+ ->expects($this->at(0))
+ ->method('get')
+ ->with('foo')
+ ->will($this->returnValue($foo));
+ $this->container['UserManager']
+ ->expects($this->at(1))
+ ->method('get')
+ ->with('admin')
+ ->will($this->returnValue($admin));
+ $this->container['UserManager']
+ ->expects($this->at(2))
+ ->method('get')
+ ->with('bar')
+ ->will($this->returnValue($bar));
+ $this->container['Config']
+ ->expects($this->exactly(6))
+ ->method('getUserValue')
+ ->will($this->onConsecutiveCalls(
+ 1024, 'foo@bar.com',
+ 404, 'admin@bar.com',
+ 2323, 'bar@dummy.com'
+ ));
+
+ $expectedResponse = new DataResponse(
+ [
+ 0 => [
+ 'name' => 'foo',
+ 'displayname' => 'M. Foo',
+ 'groups' => ['SubGroup2', 'SubGroup1'],
+ 'subadmin' => [],
+ 'quota' => 1024,
+ 'storageLocation' => '/home/foo',
+ 'lastLogin' => 500,
+ 'backend' => 'OC_User_Database',
+ 'email' => 'foo@bar.com',
+ 'isRestoreDisabled' => false,
+ ],
+ 1 => [
+ 'name' => 'admin',
+ 'displayname' => 'S. Admin',
+ 'groups' => ['SubGroup2'],
+ 'subadmin' => [],
+ 'quota' => 404,
+ 'storageLocation' => '/home/admin',
+ 'lastLogin' => 12,
+ 'backend' => 'OC_User_Dummy',
+ 'email' => 'admin@bar.com',
+ 'isRestoreDisabled' => false,
+ ],
+ 2 => [
+ 'name' => 'bar',
+ 'displayname' => 'B. Ar',
+ 'groups' => ['SubGroup1'],
+ 'subadmin' => [],
+ 'quota' => 2323,
+ 'storageLocation' => '/home/bar',
+ 'lastLogin' => 3999,
+ 'backend' => 'OC_User_Dummy',
+ 'email' => 'bar@dummy.com',
+ 'isRestoreDisabled' => false,
+ ],
+ ]
+ );
+
+ $response = $this->container['UsersController']->index(0, 10, '', 'pattern');
$this->assertEquals($expectedResponse, $response);
}
@@ -204,6 +371,8 @@ class UsersControllerTest extends \Test\TestCase {
* to test for subadmins. Thus the test always assumes you have admin permissions...
*/
public function testIndexWithSearch() {
+ $this->container['IsAdmin'] = true;
+
$foo = $this->getMockBuilder('\OC\User\User')
->disableOriginalConstructor()->getMock();
$foo
@@ -294,7 +463,8 @@ class UsersControllerTest extends \Test\TestCase {
'storageLocation' => '/home/foo',
'lastLogin' => 500,
'backend' => 'OC_User_Database',
- 'email' => 'foo@bar.com'
+ 'email' => 'foo@bar.com',
+ 'isRestoreDisabled' => false,
),
1 => array(
'name' => 'admin',
@@ -305,7 +475,8 @@ class UsersControllerTest extends \Test\TestCase {
'storageLocation' => '/home/admin',
'lastLogin' => 12,
'backend' => 'OC_User_Dummy',
- 'email' => 'admin@bar.com'
+ 'email' => 'admin@bar.com',
+ 'isRestoreDisabled' => false,
),
2 => array(
'name' => 'bar',
@@ -316,16 +487,18 @@ class UsersControllerTest extends \Test\TestCase {
'storageLocation' => '/home/bar',
'lastLogin' => 3999,
'backend' => 'OC_User_Dummy',
- 'email' => 'bar@dummy.com'
+ 'email' => 'bar@dummy.com',
+ 'isRestoreDisabled' => false,
),
)
);
- $response = $this->usersController->index(0, 10, '', 'pattern');
+ $response = $this->container['UsersController']->index(0, 10, '', 'pattern');
$this->assertEquals($expectedResponse, $response);
}
-
public function testIndexWithBackend() {
+ $this->container['IsAdmin'] = true;
+
$user = $this->getMockBuilder('\OC\User\User')
->disableOriginalConstructor()->getMock();
$user
@@ -374,15 +547,18 @@ class UsersControllerTest extends \Test\TestCase {
'storageLocation' => '/home/foo',
'lastLogin' => 500,
'backend' => 'OC_User_Database',
- 'email' => null
+ 'email' => null,
+ 'isRestoreDisabled' => false,
)
)
);
- $response = $this->usersController->index(0, 10, '','', 'OC_User_Dummy');
+ $response = $this->container['UsersController']->index(0, 10, '','', 'OC_User_Dummy');
$this->assertEquals($expectedResponse, $response);
}
public function testIndexWithBackendNoUser() {
+ $this->container['IsAdmin'] = true;
+
$this->container['UserManager']
->expects($this->once())
->method('getBackends')
@@ -394,15 +570,13 @@ class UsersControllerTest extends \Test\TestCase {
->will($this->returnValue([]));
$expectedResponse = new DataResponse([]);
- $response = $this->usersController->index(0, 10, '','', 'OC_User_Dummy');
+ $response = $this->container['UsersController']->index(0, 10, '','', 'OC_User_Dummy');
$this->assertEquals($expectedResponse, $response);
}
- /**
- * TODO: Since the function uses the static OC_Subadmin class it can't be mocked
- * to test for subadmins. Thus the test always assumes you have admin permissions...
- */
- public function testCreateSuccessfulWithoutGroup() {
+ public function testCreateSuccessfulWithoutGroupAdmin() {
+ $this->container['IsAdmin'] = true;
+
$user = $this->getMockBuilder('\OC\User\User')
->disableOriginalConstructor()->getMock();
$user
@@ -432,19 +606,97 @@ class UsersControllerTest extends \Test\TestCase {
'displayname' => null,
'quota' => null,
'subadmin' => array(),
- 'email' => null
+ 'email' => null,
+ 'isRestoreDisabled' => false,
),
Http::STATUS_CREATED
);
- $response = $this->usersController->create('foo', 'password', array());
+ $response = $this->container['UsersController']->create('foo', 'password', array());
$this->assertEquals($expectedResponse, $response);
}
- /**
- * TODO: Since the function uses the static OC_Subadmin class it can't be mocked
- * to test for subadmins. Thus the test always assumes you have admin permissions...
- */
- public function testCreateSuccessfulWithGroup() {
+ public function testCreateSuccessfulWithoutGroupSubAdmin() {
+ $this->container['IsAdmin'] = false;
+ $this->container['SubAdminFactory']
+ ->expects($this->once())
+ ->method('getSubAdminsOfGroups')
+ ->with('username')
+ ->will($this->returnValue(['SubGroup1', 'SubGroup2']));
+ $user = $this->getMockBuilder('\OC\User\User')
+ ->disableOriginalConstructor()->getMock();
+ $user
+ ->expects($this->once())
+ ->method('getUID')
+ ->will($this->returnValue('username'));
+ $this->container['UserSession']
+ ->expects($this->once())
+ ->method('getUser')
+ ->will($this->returnValue($user));
+
+ $user = $this->getMockBuilder('\OC\User\User')
+ ->disableOriginalConstructor()->getMock();
+ $user
+ ->method('getHome')
+ ->will($this->returnValue('/home/user'));
+ $user
+ ->method('getHome')
+ ->will($this->returnValue('/home/user'));
+ $user
+ ->method('getUID')
+ ->will($this->returnValue('foo'));
+ $user
+ ->expects($this->once())
+ ->method('getBackendClassName')
+ ->will($this->returnValue('bar'));
+ $subGroup1 = $this->getMockBuilder('\OCP\IGroup')
+ ->disableOriginalConstructor()->getMock();
+ $subGroup1
+ ->expects($this->once())
+ ->method('addUser')
+ ->with($user);
+ $subGroup2 = $this->getMockBuilder('\OCP\IGroup')
+ ->disableOriginalConstructor()->getMock();
+ $subGroup2
+ ->expects($this->once())
+ ->method('addUser')
+ ->with($user);
+
+ $this->container['UserManager']
+ ->expects($this->once())
+ ->method('createUser')
+ ->will($this->onConsecutiveCalls($user));
+ $this->container['GroupManager']
+ ->expects($this->exactly(2))
+ ->method('get')
+ ->will($this->onConsecutiveCalls($subGroup1, $subGroup2));
+ $this->container['GroupManager']
+ ->expects($this->once())
+ ->method('getUserGroupIds')
+ ->with($user)
+ ->will($this->onConsecutiveCalls(['SubGroup1', 'SubGroup2']));
+
+ $expectedResponse = new DataResponse(
+ array(
+ 'name' => 'foo',
+ 'groups' => ['SubGroup1', 'SubGroup2'],
+ 'storageLocation' => '/home/user',
+ 'backend' => 'bar',
+ 'lastLogin' => null,
+ 'displayname' => null,
+ 'quota' => null,
+ 'subadmin' => [],
+ 'email' => null,
+ 'isRestoreDisabled' => false,
+ ),
+ Http::STATUS_CREATED
+ );
+ $response = $this->container['UsersController']->create('foo', 'password');
+ $this->assertEquals($expectedResponse, $response);
+ }
+
+ public function testCreateSuccessfulWithGroupAdmin() {
+ $this->container['IsAdmin'] = true;
+
$user = $this->getMockBuilder('\OC\User\User')
->disableOriginalConstructor()->getMock();
$user
@@ -502,19 +754,97 @@ class UsersControllerTest extends \Test\TestCase {
'displayname' => null,
'quota' => null,
'subadmin' => array(),
- 'email' => null
+ 'email' => null,
+ 'isRestoreDisabled' => false,
),
Http::STATUS_CREATED
);
- $response = $this->usersController->create('foo', 'password', array('NewGroup', 'ExistingGroup'));
+ $response = $this->container['UsersController']->create('foo', 'password', array('NewGroup', 'ExistingGroup'));
$this->assertEquals($expectedResponse, $response);
}
- /**
- * TODO: Since the function uses the static OC_Subadmin class it can't be mocked
- * to test for subadmins. Thus the test always assumes you have admin permissions...
- */
- public function testCreateUnsuccessful() {
+ public function testCreateSuccessfulWithGroupSubAdmin() {
+ $this->container['IsAdmin'] = false;
+ $this->container['SubAdminFactory']
+ ->expects($this->once())
+ ->method('getSubAdminsOfGroups')
+ ->with('username')
+ ->will($this->returnValue(['SubGroup1', 'SubGroup2']));
+ $user = $this->getMockBuilder('\OC\User\User')
+ ->disableOriginalConstructor()->getMock();
+ $user
+ ->expects($this->once())
+ ->method('getUID')
+ ->will($this->returnValue('username'));
+ $this->container['UserSession']
+ ->expects($this->once())
+ ->method('getUser')
+ ->will($this->returnValue($user));
+
+ $user = $this->getMockBuilder('\OC\User\User')
+ ->disableOriginalConstructor()->getMock();
+ $user
+ ->method('getHome')
+ ->will($this->returnValue('/home/user'));
+ $user
+ ->method('getHome')
+ ->will($this->returnValue('/home/user'));
+ $user
+ ->method('getUID')
+ ->will($this->returnValue('foo'));
+ $user
+ ->expects($this->once())
+ ->method('getBackendClassName')
+ ->will($this->returnValue('bar'));
+ $subGroup1 = $this->getMockBuilder('\OCP\IGroup')
+ ->disableOriginalConstructor()->getMock();
+ $subGroup1
+ ->expects($this->once())
+ ->method('addUser')
+ ->with($user);
+ $subGroup2 = $this->getMockBuilder('\OCP\IGroup')
+ ->disableOriginalConstructor()->getMock();
+ $subGroup2
+ ->expects($this->once())
+ ->method('addUser')
+ ->with($user);
+
+ $this->container['UserManager']
+ ->expects($this->once())
+ ->method('createUser')
+ ->will($this->onConsecutiveCalls($user));
+ $this->container['GroupManager']
+ ->expects($this->exactly(2))
+ ->method('get')
+ ->will($this->onConsecutiveCalls($subGroup1, $subGroup2));
+ $this->container['GroupManager']
+ ->expects($this->once())
+ ->method('getUserGroupIds')
+ ->with($user)
+ ->will($this->onConsecutiveCalls(['SubGroup1']));
+
+ $expectedResponse = new DataResponse(
+ array(
+ 'name' => 'foo',
+ 'groups' => ['SubGroup1'],
+ 'storageLocation' => '/home/user',
+ 'backend' => 'bar',
+ 'lastLogin' => null,
+ 'displayname' => null,
+ 'quota' => null,
+ 'subadmin' => [],
+ 'email' => null,
+ 'isRestoreDisabled' => false,
+ ),
+ Http::STATUS_CREATED
+ );
+ $response = $this->container['UsersController']->create('foo', 'password', ['SubGroup1', 'ExistingGroup']);
+ $this->assertEquals($expectedResponse, $response);
+ }
+
+ public function testCreateUnsuccessfulAdmin() {
+ $this->container['IsAdmin'] = true;
+
$this->container['UserManager']
->method('createUser')
->will($this->throwException(new \Exception()));
@@ -525,15 +855,45 @@ class UsersControllerTest extends \Test\TestCase {
),
Http::STATUS_FORBIDDEN
);
- $response = $this->usersController->create('foo', 'password', array());
+ $response = $this->container['UsersController']->create('foo', 'password', array());
$this->assertEquals($expectedResponse, $response);
}
- /**
- * TODO: Since the function uses the static OC_Subadmin class it can't be mocked
- * to test for subadmins. Thus the test always assumes you have admin permissions...
- */
- public function testDestroySelf() {
+ public function testCreateUnsuccessfulSubAdmin() {
+ $this->container['IsAdmin'] = false;
+ $this->container['SubAdminFactory']
+ ->expects($this->once())
+ ->method('getSubAdminsOfGroups')
+ ->with('username')
+ ->will($this->returnValue(['SubGroup1', 'SubGroup2']));
+ $user = $this->getMockBuilder('\OC\User\User')
+ ->disableOriginalConstructor()->getMock();
+ $user
+ ->expects($this->once())
+ ->method('getUID')
+ ->will($this->returnValue('username'));
+ $this->container['UserSession']
+ ->expects($this->once())
+ ->method('getUser')
+ ->will($this->returnValue($user));
+
+ $this->container['UserManager']
+ ->method('createUser')
+ ->will($this->throwException(new \Exception()));
+
+ $expectedResponse = new DataResponse(
+ [
+ 'message' => 'Unable to create user.'
+ ],
+ Http::STATUS_FORBIDDEN
+ );
+ $response = $this->container['UsersController']->create('foo', 'password', array());
+ $this->assertEquals($expectedResponse, $response);
+ }
+
+ public function testDestroySelfAdmin() {
+ $this->container['IsAdmin'] = true;
+
$user = $this->getMockBuilder('\OC\User\User')
->disableOriginalConstructor()->getMock();
$user
@@ -553,15 +913,39 @@ class UsersControllerTest extends \Test\TestCase {
),
Http::STATUS_FORBIDDEN
);
- $response = $this->usersController->destroy('myself');
+ $response = $this->container['UsersController']->destroy('myself');
$this->assertEquals($expectedResponse, $response);
}
- /**
- * TODO: Since the function uses the static OC_Subadmin class it can't be mocked
- * to test for subadmins. Thus the test always assumes you have admin permissions...
- */
- public function testDestroy() {
+ public function testDestroySelfSubadmin() {
+ $this->container['IsAdmin'] = false;
+
+ $user = $this->getMockBuilder('\OC\User\User')
+ ->disableOriginalConstructor()->getMock();
+ $user
+ ->expects($this->once())
+ ->method('getUID')
+ ->will($this->returnValue('myself'));
+ $this->container['UserSession']
+ ->method('getUser')
+ ->will($this->returnValue($user));
+
+ $expectedResponse = new DataResponse(
+ array(
+ 'status' => 'error',
+ 'data' => array(
+ 'message' => 'Unable to delete user.'
+ )
+ ),
+ Http::STATUS_FORBIDDEN
+ );
+ $response = $this->container['UsersController']->destroy('myself');
+ $this->assertEquals($expectedResponse, $response);
+ }
+
+ public function testDestroyAdmin() {
+ $this->container['IsAdmin'] = true;
+
$user = $this->getMockBuilder('\OC\User\User')
->disableOriginalConstructor()->getMock();
$user
@@ -591,14 +975,59 @@ class UsersControllerTest extends \Test\TestCase {
),
Http::STATUS_NO_CONTENT
);
- $response = $this->usersController->destroy('UserToDelete');
+ $response = $this->container['UsersController']->destroy('UserToDelete');
$this->assertEquals($expectedResponse, $response);
}
- /**
- * TODO: Since the function uses the static OC_Subadmin class it can't be mocked
- * to test for subadmins. Thus the test always assumes you have admin permissions...
- */
- public function testDestroyUnsuccessful() {
+
+ public function testDestroySubAdmin() {
+ $this->container['IsAdmin'] = false;
+ $this->container['SubAdminFactory']
+ ->expects($this->once())
+ ->method('isUserAccessible')
+ ->with('myself', 'UserToDelete')
+ ->will($this->returnValue(true));
+ $user = $this->getMockBuilder('\OC\User\User')
+ ->disableOriginalConstructor()->getMock();
+ $user
+ ->expects($this->once())
+ ->method('getUID')
+ ->will($this->returnValue('myself'));
+ $this->container['UserSession']
+ ->method('getUser')
+ ->will($this->returnValue($user));
+
+ $user = $this->getMockBuilder('\OC\User\User')
+ ->disableOriginalConstructor()->getMock();
+ $toDeleteUser = $this->getMockBuilder('\OC\User\User')
+ ->disableOriginalConstructor()->getMock();
+ $toDeleteUser
+ ->expects($this->once())
+ ->method('delete')
+ ->will($this->returnValue(true));
+ $this->container['UserSession']
+ ->method('getUser')
+ ->will($this->returnValue($user));
+ $this->container['UserManager']
+ ->method('get')
+ ->with('UserToDelete')
+ ->will($this->returnValue($toDeleteUser));
+
+ $expectedResponse = new DataResponse(
+ [
+ 'status' => 'success',
+ 'data' => [
+ 'username' => 'UserToDelete'
+ ]
+ ],
+ Http::STATUS_NO_CONTENT
+ );
+ $response = $this->container['UsersController']->destroy('UserToDelete');
+ $this->assertEquals($expectedResponse, $response);
+ }
+
+ public function testDestroyUnsuccessfulAdmin() {
+ $this->container['IsAdmin'] = true;
+
$user = $this->getMockBuilder('\OC\User\User')
->disableOriginalConstructor()->getMock();
$user
@@ -628,14 +1057,100 @@ class UsersControllerTest extends \Test\TestCase {
),
Http::STATUS_FORBIDDEN
);
- $response = $this->usersController->destroy('UserToDelete');
+ $response = $this->container['UsersController']->destroy('UserToDelete');
+ $this->assertEquals($expectedResponse, $response);
+ }
+
+ public function testDestroyUnsuccessfulSubAdmin() {
+ $this->container['IsAdmin'] = false;
+ $this->container['SubAdminFactory']
+ ->expects($this->once())
+ ->method('isUserAccessible')
+ ->with('myself', 'UserToDelete')
+ ->will($this->returnValue(true));
+ $user = $this->getMockBuilder('\OC\User\User')
+ ->disableOriginalConstructor()->getMock();
+ $user
+ ->expects($this->once())
+ ->method('getUID')
+ ->will($this->returnValue('myself'));
+ $this->container['UserSession']
+ ->method('getUser')
+ ->will($this->returnValue($user));
+
+ $toDeleteUser = $this->getMockBuilder('\OC\User\User')
+ ->disableOriginalConstructor()->getMock();
+ $toDeleteUser
+ ->expects($this->once())
+ ->method('delete')
+ ->will($this->returnValue(false));
+ $this->container['UserSession']
+ ->method('getUser')
+ ->will($this->returnValue($user));
+ $this->container['UserManager']
+ ->method('get')
+ ->with('UserToDelete')
+ ->will($this->returnValue($toDeleteUser));
+
+ $expectedResponse = new DataResponse(
+ [
+ 'status' => 'error',
+ 'data' => [
+ 'message' => 'Unable to delete user.'
+ ]
+ ],
+ Http::STATUS_FORBIDDEN
+ );
+ $response = $this->container['UsersController']->destroy('UserToDelete');
+ $this->assertEquals($expectedResponse, $response);
+ }
+
+ public function testDestroyNotAccessibleToSubAdmin() {
+ $this->container['IsAdmin'] = false;
+ $this->container['SubAdminFactory']
+ ->expects($this->once())
+ ->method('isUserAccessible')
+ ->with('myself', 'UserToDelete')
+ ->will($this->returnValue(false));
+ $user = $this->getMockBuilder('\OC\User\User')
+ ->disableOriginalConstructor()->getMock();
+ $user
+ ->expects($this->once())
+ ->method('getUID')
+ ->will($this->returnValue('myself'));
+ $this->container['UserSession']
+ ->method('getUser')
+ ->will($this->returnValue($user));
+
+ $toDeleteUser = $this->getMockBuilder('\OC\User\User')
+ ->disableOriginalConstructor()->getMock();
+ $this->container['UserSession']
+ ->method('getUser')
+ ->will($this->returnValue($user));
+ $this->container['UserManager']
+ ->method('get')
+ ->with('UserToDelete')
+ ->will($this->returnValue($toDeleteUser));
+
+ $expectedResponse = new DataResponse(
+ [
+ 'status' => 'error',
+ 'data' => [
+ 'message' => 'Authentication error'
+ ]
+ ],
+ Http::STATUS_FORBIDDEN
+ );
+ $response = $this->container['UsersController']->destroy('UserToDelete');
$this->assertEquals($expectedResponse, $response);
}
/**
* test if an invalid mail result in a failure response
*/
- public function testCreateUnsuccessfulWithInvalidEMail() {
+ public function testCreateUnsuccessfulWithInvalidEmailAdmin() {
+ $this->container['IsAdmin'] = true;
+
/**
* FIXME: Disabled due to missing DI on mail class.
* TODO: Re-enable when https://github.com/owncloud/core/pull/12085 is merged.
@@ -653,14 +1168,16 @@ class UsersControllerTest extends \Test\TestCase {
),
Http::STATUS_UNPROCESSABLE_ENTITY
);
- $response = $this->usersController->create('foo', 'password', array(), 'invalidMailAdress');
+ $response = $this->container['UsersController']->create('foo', 'password', array(), 'invalidMailAdress');
$this->assertEquals($expectedResponse, $response);
}
/**
* test if a valid mail result in a successful mail send
*/
- public function testCreateSuccessfulWithValidEMail() {
+ public function testCreateSuccessfulWithValidEmailAdmin() {
+ $this->container['IsAdmin'] = true;
+
/**
* FIXME: Disabled due to missing DI on mail class.
* TODO: Re-enable when https://github.com/owncloud/core/pull/12085 is merged.
@@ -688,8 +1205,150 @@ class UsersControllerTest extends \Test\TestCase {
->expects($this->never())
->method('error');
- $response = $this->usersController->create('foo', 'password', array(), 'validMail@Adre.ss');
+ $response = $this->container['UsersController']->create('foo', 'password', array(), 'validMail@Adre.ss');
$this->assertEquals(Http::STATUS_CREATED, $response->getStatus());
}
+ private function mockUser($userId = 'foo', $displayName = 'M. Foo',
+ $lastLogin = 500, $home = '/home/foo', $backend = 'OC_User_Database') {
+ $user = $this->getMockBuilder('\OC\User\User')
+ ->disableOriginalConstructor()->getMock();
+ $user
+ ->expects($this->any())
+ ->method('getUID')
+ ->will($this->returnValue($userId));
+ $user
+ ->expects($this->once())
+ ->method('getDisplayName')
+ ->will($this->returnValue($displayName));
+ $user
+ ->method('getLastLogin')
+ ->will($this->returnValue($lastLogin));
+ $user
+ ->method('getHome')
+ ->will($this->returnValue($home));
+ $user
+ ->expects($this->once())
+ ->method('getBackendClassName')
+ ->will($this->returnValue($backend));
+
+ $result = [
+ 'name' => $userId,
+ 'displayname' => $displayName,
+ 'groups' => null,
+ 'subadmin' => array(),
+ 'quota' => null,
+ 'storageLocation' => $home,
+ 'lastLogin' => $lastLogin,
+ 'backend' => $backend,
+ 'email' => null,
+ 'isRestoreDisabled' => false,
+ ];
+
+ return [$user, $result];
+ }
+
+ public function testRestorePossibleWithoutEncryption() {
+ $this->container['IsAdmin'] = true;
+
+ list($user, $expectedResult) = $this->mockUser();
+
+ $result = \Test_Helper::invokePrivate($this->container['UsersController'], 'formatUserForIndex', [$user]);
+ $this->assertEquals($expectedResult, $result);
+ }
+
+ public function testRestorePossibleWithAdminAndUserRestore() {
+ $this->container['IsAdmin'] = true;
+
+ list($user, $expectedResult) = $this->mockUser();
+
+ $this->container['OCP\\App\\IAppManager']
+ ->expects($this->once())
+ ->method('isEnabledForUser')
+ ->with(
+ $this->equalTo('files_encryption')
+ )
+ ->will($this->returnValue(true));
+ $this->container['Config']
+ ->expects($this->once())
+ ->method('getAppValue')
+ ->with(
+ $this->equalTo('files_encryption'),
+ $this->equalTo('recoveryAdminEnabled'),
+ $this->anything()
+ )
+ ->will($this->returnValue('1'));
+
+ $this->container['Config']
+ ->expects($this->at(1))
+ ->method('getUserValue')
+ ->with(
+ $this->anything(),
+ $this->equalTo('files_encryption'),
+ $this->equalTo('recovery_enabled'),
+ $this->anything()
+ )
+ ->will($this->returnValue('1'));
+
+ $result = \Test_Helper::invokePrivate($this->container['UsersController'], 'formatUserForIndex', [$user]);
+ $this->assertEquals($expectedResult, $result);
+ }
+
+ public function testRestoreNotPossibleWithoutAdminRestore() {
+ $this->container['IsAdmin'] = true;
+
+ list($user, $expectedResult) = $this->mockUser();
+
+ $this->container['OCP\\App\\IAppManager']
+ ->method('isEnabledForUser')
+ ->with(
+ $this->equalTo('files_encryption')
+ )
+ ->will($this->returnValue(true));
+
+ $expectedResult['isRestoreDisabled'] = true;
+
+ $result = \Test_Helper::invokePrivate($this->container['UsersController'], 'formatUserForIndex', [$user]);
+ $this->assertEquals($expectedResult, $result);
+ }
+
+ public function testRestoreNotPossibleWithoutUserRestore() {
+ $this->container['IsAdmin'] = true;
+
+ list($user, $expectedResult) = $this->mockUser();
+
+ $this->container['OCP\\App\\IAppManager']
+ ->expects($this->once())
+ ->method('isEnabledForUser')
+ ->with(
+ $this->equalTo('files_encryption')
+ )
+ ->will($this->returnValue(true));
+ $this->container['Config']
+ ->expects($this->once())
+ ->method('getAppValue')
+ ->with(
+ $this->equalTo('files_encryption'),
+ $this->equalTo('recoveryAdminEnabled'),
+ $this->anything()
+ )
+ ->will($this->returnValue('1'));
+
+ $this->container['Config']
+ ->expects($this->at(1))
+ ->method('getUserValue')
+ ->with(
+ $this->anything(),
+ $this->equalTo('files_encryption'),
+ $this->equalTo('recovery_enabled'),
+ $this->anything()
+ )
+ ->will($this->returnValue('0'));
+
+ $expectedResult['isRestoreDisabled'] = true;
+
+ $result = \Test_Helper::invokePrivate($this->container['UsersController'], 'formatUserForIndex', [$user]);
+ $this->assertEquals($expectedResult, $result);
+ }
+
}
diff --git a/tests/settings/middleware/subadminmiddlewaretest.php b/tests/settings/middleware/subadminmiddlewaretest.php
index e5572cfba52..d0da19f60e1 100644
--- a/tests/settings/middleware/subadminmiddlewaretest.php
+++ b/tests/settings/middleware/subadminmiddlewaretest.php
@@ -81,11 +81,9 @@ class SubadminMiddlewareTest extends \Test\TestCase {
$this->subadminMiddlewareAsSubAdmin->beforeController($this->controller, 'foo');
}
-
-
-
public function testAfterException() {
$expectedResponse = new TemplateResponse('core', '403', array(), 'guest');
+ $expectedResponse->setStatus(403);
$this->assertEquals($expectedResponse, $this->subadminMiddleware->afterException($this->controller, 'foo', new \Exception()));
}
-} \ No newline at end of file
+}