Skip to content

Commit

Permalink
Test added, Local not showed at admin settings if config param not set
Browse files Browse the repository at this point in the history
  • Loading branch information
noveens committed Apr 7, 2017
1 parent eedd8f5 commit a8c1b8e
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 68 deletions.
58 changes: 34 additions & 24 deletions apps/files_external/lib/Controller/GlobalStoragesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,33 +87,44 @@ public function create(
$applicableGroups,
$priority
) {
$newStorage = $this->createStorage(
$mountPoint,
$backend,
$authMechanism,
$backendOptions,
$mountOptions,
$applicableUsers,
$applicableGroups,
$priority
);
if ($newStorage instanceof DataResponse) {
return $newStorage;
}
$canCreateNewLocalStorage = \OC::$server->getConfig()->getSystemValue('files_external_allow_create_new_local', false);

if ($canCreateNewLocalStorage === true) {
$newStorage = $this->createStorage(
$mountPoint,
$backend,
$authMechanism,
$backendOptions,
$mountOptions,
$applicableUsers,
$applicableGroups,
$priority
);
if ($newStorage instanceof DataResponse) {
return $newStorage;
}

$response = $this->validate($newStorage);
if (!empty($response)) {
return $response;
}
$response = $this->validate($newStorage);
if (!empty($response)) {
return $response;
}

$newStorage = $this->service->addStorage($newStorage);
$newStorage = $this->service->addStorage($newStorage);

$this->updateStorageStatus($newStorage);
$this->updateStorageStatus($newStorage);

return new DataResponse(
$newStorage,
Http::STATUS_CREATED
);
return new DataResponse(
$newStorage,
Http::STATUS_CREATED
);
}

else {
return new DataResponse(
null,
Http::STATUS_FORBIDDEN
);
}
}

/**
Expand Down Expand Up @@ -181,7 +192,6 @@ public function update(
$storage,
Http::STATUS_OK
);

}


Expand Down
44 changes: 20 additions & 24 deletions apps/files_external/lib/Controller/StoragesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,30 +111,26 @@ protected function createStorage(
$applicableGroups = null,
$priority = null
) {
$canCreateNewLocalStorage = \OC::$server->getConfig()->getSystemValue('files_external_allow_create_new_local', false);

if ($canCreateNewLocalStorage === true) {
try {
return $this->service->createStorage(
$mountPoint,
$backend,
$authMechanism,
$backendOptions,
$mountOptions,
$applicableUsers,
$applicableGroups,
$priority
);
} catch (\InvalidArgumentException $e) {
$this->logger->logException($e);
return new DataResponse(
[
'message' => (string)$this->l10n->t('Invalid backend or authentication mechanism class')
],
Http::STATUS_UNPROCESSABLE_ENTITY
);
}
};
try {
return $this->service->createStorage(
$mountPoint,
$backend,
$authMechanism,
$backendOptions,
$mountOptions,
$applicableUsers,
$applicableGroups,
$priority
);
} catch (\InvalidArgumentException $e) {
$this->logger->logException($e);
return new DataResponse(
[
'message' => (string)$this->l10n->t('Invalid backend or authentication mechanism class')
],
Http::STATUS_UNPROCESSABLE_ENTITY
);
}
}

/**
Expand Down
51 changes: 31 additions & 20 deletions apps/files_external/lib/Controller/UserStoragesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,29 +126,40 @@ public function create(
$backendOptions,
$mountOptions
) {
$newStorage = $this->createStorage(
$mountPoint,
$backend,
$authMechanism,
$backendOptions,
$mountOptions
);
if ($newStorage instanceOf DataResponse) {
return $newStorage;
}
$canCreateNewLocalStorage = \OC::$server->getConfig()->getSystemValue('files_external_allow_create_new_local', false);

if ($canCreateNewLocalStorage === true) {
$newStorage = $this->createStorage(
$mountPoint,
$backend,
$authMechanism,
$backendOptions,
$mountOptions
);
if ($newStorage instanceOf DataResponse) {
return $newStorage;
}

$response = $this->validate($newStorage);
if (!empty($response)) {
return $response;
}
$response = $this->validate($newStorage);
if (!empty($response)) {
return $response;
}

$newStorage = $this->service->addStorage($newStorage);
$this->updateStorageStatus($newStorage);
$newStorage = $this->service->addStorage($newStorage);
$this->updateStorageStatus($newStorage);

return new DataResponse(
$newStorage,
Http::STATUS_CREATED
);
return new DataResponse(
$newStorage,
Http::STATUS_CREATED
);
}

else {
return new DataResponse(
null,
Http::STATUS_FORBIDDEN
);
}
}

/**
Expand Down
4 changes: 4 additions & 0 deletions apps/files_external/templates/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,12 @@
return strcasecmp($a->getText(), $b->getText());
});
?>
<?php
$canCreateNewLocalStorage = \OC::$server->getConfig()->getSystemValue('files_external_allow_create_new_local', false);
?>
<?php foreach ($sortedBackends as $backend): ?>
<?php if ($backend->getDeprecateTo()) continue; // ignore deprecated backends ?>
<?php if (!$canCreateNewLocalStorage && $backend->getIdentifier() == "local") continue; // if the "files_external_allow_create_new_local" config param isn't set to to true ?>
<option value="<?php p($backend->getIdentifier()); ?>"><?php p($backend->getText()); ?></option>
<?php endforeach; ?>
</select>
Expand Down
45 changes: 45 additions & 0 deletions apps/files_external/tests/Controller/StoragesControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,12 @@ abstract class StoragesControllerTest extends \Test\TestCase {

public function setUp() {
\OC_Mount_Config::$skipTest = true;
\OC::$server->getSystemConfig()->setValue('files_external_allow_create_new_local', true);
}

public function tearDown() {
\OC_Mount_Config::$skipTest = false;
\OC::$server->getSystemConfig()->setValue('files_external_allow_create_new_local', false);
}

/**
Expand Down Expand Up @@ -120,6 +122,49 @@ public function testAddStorage() {
$this->assertEquals($storageConfig, $data);
}

public function testAddStorageWithoutConfig() {
\OC::$server->getSystemConfig()->setValue('files_external_allow_create_new_local', false);

$authMech = $this->getAuthMechMock();
$authMech->method('validateStorage')
->willReturn(true);
$authMech->method('isVisibleFor')
->willReturn(true);
$backend = $this->getBackendMock();
$backend->method('validateStorage')
->willReturn(true);
$backend->method('isVisibleFor')
->willReturn(true);

$storageConfig = new StorageConfig(1);
$storageConfig->setMountPoint('mount');
$storageConfig->setBackend($backend);
$storageConfig->setAuthMechanism($authMech);
$storageConfig->setBackendOptions([]);

$this->service->expects($this->never())
->method('createStorage')
->will($this->returnValue($storageConfig));
$this->service->expects($this->never())
->method('addStorage')
->will($this->returnValue($storageConfig));

$response = $this->controller->create(
'mount',
'\OCA\Files_External\Lib\Storage\SMB',
'\OCA\Files_External\Lib\Auth\NullMechanism',
[],
[],
[],
[],
null
);

$data = $response->getData();
$this->assertEquals(Http::STATUS_FORBIDDEN, $response->getStatus());
$this->assertEquals(null, $data);
}

public function testUpdateStorage() {
$authMech = $this->getAuthMechMock();
$authMech->method('validateStorage')
Expand Down

0 comments on commit a8c1b8e

Please sign in to comment.