Added tests to check mount point list for a target user

This commit is contained in:
Vincent Petry 2014-03-19 14:23:36 +01:00
parent 4cb53f77b2
commit 8e0a5ed5df
2 changed files with 132 additions and 7 deletions

View File

@ -167,12 +167,25 @@ class OC_Mount_Config {
} }
/** /**
* Init mount points hook * Hook that mounts the given user's visible mount points
* @param array $data * @param array $data
*/ */
public static function initMountPointsHook($data) { public static function initMountPointsHook($data) {
$user = $data['user']; $mountPoints = self::getAbsoluteMountPoints($data['user']);
$root = $data['user_dir']; foreach ($mountPoints as $mountPoints => $options) {
\OC\Files\Filesystem::mount($options['class'], $options['options'], $mountPoint);
}
}
/**
* Returns the mount points for the given user.
* The mount point is relative to the data directory.
*
* @param string $user user
* @return array of mount point string as key, mountpoint config as value
*/
public static function getAbsoluteMountPoints($user) {
$mountPoints = array();
$datadir = \OC_Config::getValue("datadirectory", \OC::$SERVERROOT . "/data"); $datadir = \OC_Config::getValue("datadirectory", \OC::$SERVERROOT . "/data");
$mount_file = \OC_Config::getValue("mount_file", $datadir . "/mount.json"); $mount_file = \OC_Config::getValue("mount_file", $datadir . "/mount.json");
@ -187,7 +200,7 @@ class OC_Mount_Config {
if (isset($mountConfig[self::MOUNT_TYPE_GLOBAL])) { if (isset($mountConfig[self::MOUNT_TYPE_GLOBAL])) {
foreach ($mountConfig[self::MOUNT_TYPE_GLOBAL] as $mountPoint => $options) { foreach ($mountConfig[self::MOUNT_TYPE_GLOBAL] as $mountPoint => $options) {
$options['options'] = self::decryptPasswords($options['options']); $options['options'] = self::decryptPasswords($options['options']);
\OC\Files\Filesystem::mount($options['class'], $options['options'], $mountPoint); $mountPoints[$mountPoint] = $options;
} }
} }
if (isset($mountConfig[self::MOUNT_TYPE_GROUP])) { if (isset($mountConfig[self::MOUNT_TYPE_GROUP])) {
@ -199,7 +212,7 @@ class OC_Mount_Config {
$option = self::setUserVars($user, $option); $option = self::setUserVars($user, $option);
} }
$options['options'] = self::decryptPasswords($options['options']); $options['options'] = self::decryptPasswords($options['options']);
\OC\Files\Filesystem::mount($options['class'], $options['options'], $mountPoint); $mountPoints[$mountPoint] = $options;
} }
} }
} }
@ -213,7 +226,7 @@ class OC_Mount_Config {
$option = self::setUserVars($user, $option); $option = self::setUserVars($user, $option);
} }
$options['options'] = self::decryptPasswords($options['options']); $options['options'] = self::decryptPasswords($options['options']);
\OC\Files\Filesystem::mount($options['class'], $options['options'], $mountPoint); $mountPoints[$mountPoint] = $options;
} }
} }
} }
@ -224,9 +237,11 @@ class OC_Mount_Config {
if (isset($mountConfig[self::MOUNT_TYPE_USER][$user])) { if (isset($mountConfig[self::MOUNT_TYPE_USER][$user])) {
foreach ($mountConfig[self::MOUNT_TYPE_USER][$user] as $mountPoint => $options) { foreach ($mountConfig[self::MOUNT_TYPE_USER][$user] as $mountPoint => $options) {
$options['options'] = self::decryptPasswords($options['options']); $options['options'] = self::decryptPasswords($options['options']);
\OC\Files\Filesystem::mount($options['class'], $options['options'], $mountPoint); $mountPoints[$mountPoint] = $options;
} }
} }
return $mountPoints;
} }
/** /**

View File

@ -345,4 +345,114 @@ class Test_Mount_Config extends \PHPUnit_Framework_TestCase {
$savedMountConfig = $config['ext']['configuration']; $savedMountConfig = $config['ext']['configuration'];
$this->assertEquals($mountConfig, $savedMountConfig); $this->assertEquals($mountConfig, $savedMountConfig);
} }
public function mountDataProvider() {
return array(
// Tests for visible mount points
// system mount point for all users
array(
false,
OC_Mount_Config::MOUNT_TYPE_USER,
'all',
'test',
true,
),
// system mount point for a specific user
array(
false,
OC_Mount_Config::MOUNT_TYPE_USER,
'test',
'test',
true,
),
// system mount point for a specific group
array(
false,
OC_Mount_Config::MOUNT_TYPE_GROUP,
'testgroup1',
'test',
true,
),
// user mount point
array(
true,
OC_Mount_Config::MOUNT_TYPE_USER,
'test',
'test',
true,
),
// Tests for non-visible mount points
// system mount point for another user
array(
false,
OC_Mount_Config::MOUNT_TYPE_USER,
'anotheruser',
'test',
false,
),
// system mount point for a specific group
array(
false,
OC_Mount_Config::MOUNT_TYPE_GROUP,
'anothergroup',
'test',
false,
),
// user mount point
array(
true,
OC_Mount_Config::MOUNT_TYPE_USER,
'test',
'anotheruser',
false,
),
);
}
/**
* Test mount points used at mount time, making sure
* the configuration is prepared properly.
*
* @dataProvider mountDataProvider
* @param bool $isPersonal true for personal mount point, false for system mount point
* @param string $mountType mount type
* @param string $applicable target user/group or "all"
* @param string $testUser user for which to retrieve the mount points
* @param bool $expectVisible whether to expect the mount point to be visible for $testUser
*/
public function testMount($isPersonal, $mountType, $applicable, $testUser, $expectVisible) {
$mountConfig = array(
'host' => 'someost',
'user' => 'someuser',
'password' => 'somepassword',
'root' => 'someroot'
);
// add mount point as "test" user
$this->assertTrue(
OC_Mount_Config::addMountPoint(
'/ext',
'\OC\Files\Storage\SMB',
$mountConfig,
$mountType,
$applicable,
$isPersonal
)
);
// check mount points in the perspective of user $testUser
\OC_User::setUserId($testUser);
$mountPoints = OC_Mount_Config::getAbsoluteMountPoints($testUser);
if ($expectVisible) {
$this->assertEquals(1, count($mountPoints));
$this->assertTrue(isset($mountPoints['/test/files/ext']));
$this->assertEquals('\OC\Files\Storage\SMB', $mountPoints['/test/files/ext']['class']);
$this->assertEquals($mountConfig, $mountPoints['/test/files/ext']['options']);
}
else {
$this->assertEquals(0, count($mountPoints));
}
}
} }