Skip to content

Commit

Permalink
Unit tests for BackendService, BackendConfig and BackendParameter
Browse files Browse the repository at this point in the history
Two potential bugs pre-empted and fixed
  • Loading branch information
Robin McCorkell committed Jul 23, 2015
1 parent 4a8fb4c commit 7455ba1
Show file tree
Hide file tree
Showing 7 changed files with 431 additions and 2 deletions.
4 changes: 3 additions & 1 deletion apps/files_external/lib/backendconfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,9 @@ public function checkDependencies() {
public function validateStorage(StorageConfig $storage) {
$options = $storage->getBackendOptions();
foreach ($this->parameters as $parameter) {
if (!$parameter->validateValue($options[$parameter->getName()])) {
$value = isset($options[$parameter->getName()]) ?
$options[$parameter->getName()] : null;
if (!$parameter->validateValue($value)) {
return false;
}
}
Expand Down
2 changes: 1 addition & 1 deletion apps/files_external/lib/backendparameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public function validateValue($value) {
}
break;
default:
if (!isset($value)) {
if (empty($value)) {
return false;
}
break;
Expand Down
105 changes: 105 additions & 0 deletions apps/files_external/tests/backendconfigtest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php
/**
* @author Robin McCorkell <rmccorkell@owncloud.com>
*
* @copyright Copyright (c) 2015, 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 OCA\Files_External\Tests;

use \OCA\Files_External\Lib\BackendConfig;
use \OCA\Files_External\Lib\BackendDependency;

class BackendConfigTest extends \Test\TestCase {

public function testJsonSerialization() {
$param = $this->getMockBuilder('\OCA\Files_External\Lib\BackendParameter')
->disableOriginalConstructor()
->getMock();
$param->method('getName')->willReturn('foo');

$backendConfig = new BackendConfig('\OC\Files\Storage\SMB', 'smb', [$param]);
$backendConfig->setPriority(123);
$backendConfig->setCustomJs('foo/bar.js');

$json = $backendConfig->jsonSerialize();

$this->assertEquals('smb', $json['backend']);
$this->assertEquals(123, $json['priority']);
$this->assertEquals('foo/bar.js', $json['custom']);

$configuration = $json['configuration'];
$this->assertArrayHasKey('foo', $configuration);
}

public function validateStorageProvider() {
return [
[true, ['foo' => true, 'bar' => true, 'baz' => true]],
[false, ['foo' => true, 'bar' => false]]
];
}

/**
* @dataProvider validateStorageProvider
*/
public function testValidateStorage($expectedSuccess, $params) {
$backendParams = [];
foreach ($params as $name => $valid) {
$param = $this->getMockBuilder('\OCA\Files_External\Lib\BackendParameter')
->disableOriginalConstructor()
->getMock();
$param->method('getName')
->willReturn($name);
$param->expects($this->once())
->method('validateValue')
->willReturn($valid);
$backendParams[] = $param;
}

$storageConfig = $this->getMockBuilder('\OCA\Files_External\Lib\StorageConfig')
->disableOriginalConstructor()
->getMock();
$storageConfig->expects($this->once())
->method('getBackendOptions')
->willReturn([]);

$backendConfig = new BackendConfig('\OC\Files\Storage\SMB', 'smb', $backendParams);
$this->assertEquals($expectedSuccess, $backendConfig->validateStorage($storageConfig));
}

/**
* Static method for testCheckDependencies()
* Do not stare at for too long, keep all limbs well away from the function
*
* @return array
*/
public static function checkDependencies() {
return ['dependency' => 'missing dependency'];
}

public function testCheckDependencies() {
$backend = new BackendConfig('\OCA\Files_External\Tests\BackendConfigTest', 'test', []);
$backend->setHasDependencies(true);

$dependencies = $backend->checkDependencies();
$this->assertCount(1, $dependencies);
$this->assertEquals('dependency', $dependencies[0]->getDependency());
$this->assertEquals('missing dependency', $dependencies[0]->getMessage());
$this->assertEquals($backend, $dependencies[0]->getBackend());
}

}
70 changes: 70 additions & 0 deletions apps/files_external/tests/backendparametertest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php
/**
* @author Robin McCorkell <rmccorkell@owncloud.com>
*
* @copyright Copyright (c) 2015, 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 OCA\Files_External\Tests;

use \OCA\Files_External\Lib\BackendParameter as Param;

class BackendParameterTest extends \Test\TestCase {

public function testJsonSerialization() {
$param = new Param('foo', 'bar');
$this->assertEquals('bar', $param->jsonSerialize());

$param->setType(Param::VALUE_BOOLEAN);
$this->assertEquals('!bar', $param->jsonSerialize());

$param->setType(Param::VALUE_PASSWORD);
$param->setFlag(Param::FLAG_OPTIONAL);
$this->assertEquals('&*bar', $param->jsonSerialize());

$param->setType(Param::VALUE_HIDDEN);
$param->setFlags(Param::FLAG_NONE);
$this->assertEquals('#bar', $param->jsonSerialize());
}

public function validateValueProvider() {
return [
[Param::VALUE_TEXT, Param::FLAG_NONE, 'abc', true],
[Param::VALUE_TEXT, Param::FLAG_NONE, '', false],
[Param::VALUE_TEXT, Param::FLAG_OPTIONAL, '', true],

[Param::VALUE_BOOLEAN, Param::FLAG_NONE, false, true],
[Param::VALUE_BOOLEAN, Param::FLAG_NONE, 123, false],

[Param::VALUE_PASSWORD, Param::FLAG_NONE, 'foobar', true],
[Param::VALUE_PASSWORD, Param::FLAG_NONE, '', false],

[Param::VALUE_HIDDEN, Param::FLAG_NONE, '', false]
];
}

/**
* @dataProvider validateValueProvider
*/
public function testValidateValue($type, $flags, $value, $success) {
$param = new Param('foo', 'bar');
$param->setType($type);
$param->setFlags($flags);

$this->assertEquals($success, $param->validateValue($value));
}
}
34 changes: 34 additions & 0 deletions apps/files_external/tests/controller/storagescontrollertest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
/**
* @author Vincent Petry <pvince81@owncloud.com>
* @author Robin McCorkell <rmccorkell@owncloud.com>
*
* @copyright Copyright (c) 2015, ownCloud, Inc.
* @license AGPL-3.0
Expand Down Expand Up @@ -252,4 +253,37 @@ public function testGetStorage() {
$this->assertEquals(Http::STATUS_OK, $response->getStatus());
$this->assertEquals($storageConfig, $response->getData());
}

public function testValidateStorage() {
$backendConfig = $this->getMockBuilder('\OCA\Files_External\Lib\BackendConfig')
->setConstructorArgs(['\OC\Files\Storage\SMB', 'smb', []])
->getMock();
$backendConfig->expects($this->once())
->method('validateStorage')
->will($this->returnValue(false));

$storageConfig = new StorageConfig();
$storageConfig->setMountPoint('mount');
$storageConfig->setBackend($backendConfig);
$storageConfig->setBackendOptions([]);

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

$response = $this->controller->create(
'mount',
'\OC\Files\Storage\SMB',
array(),
[],
[],
[],
null
);

$this->assertEquals(Http::STATUS_UNPROCESSABLE_ENTITY, $response->getStatus());
}

}
Loading

0 comments on commit 7455ba1

Please sign in to comment.