2014-10-31 13:41:07 +03:00
|
|
|
<?php
|
|
|
|
/**
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Vincent Petry <pvince81@owncloud.com>
|
2014-10-31 13:41:07 +03:00
|
|
|
*
|
2015-03-26 13:44:34 +03:00
|
|
|
* @copyright Copyright (c) 2015, ownCloud, Inc.
|
|
|
|
* @license AGPL-3.0
|
2014-10-31 13:41:07 +03:00
|
|
|
*
|
2015-03-26 13:44:34 +03:00
|
|
|
* 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.
|
2014-10-31 13:41:07 +03:00
|
|
|
*
|
2015-03-26 13:44:34 +03:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2014-10-31 13:41:07 +03:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-03-26 13:44:34 +03:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
2014-10-31 13:41:07 +03:00
|
|
|
*
|
2015-03-26 13:44:34 +03:00
|
|
|
* 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/>
|
2014-10-31 13:41:07 +03:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
namespace OCA\Files_external\Tests\Controller;
|
|
|
|
|
|
|
|
use \OCP\AppFramework\Http;
|
|
|
|
|
|
|
|
use \OCA\Files_external\Controller\GlobalStoragesController;
|
|
|
|
use \OCA\Files_external\Service\GlobalStoragesService;
|
|
|
|
use \OCA\Files_external\Lib\StorageConfig;
|
|
|
|
use \OCA\Files_external\NotFoundException;
|
|
|
|
|
|
|
|
abstract class StoragesControllerTest extends \Test\TestCase {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var GlobalStoragesController
|
|
|
|
*/
|
|
|
|
protected $controller;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var GlobalStoragesService
|
|
|
|
*/
|
|
|
|
protected $service;
|
|
|
|
|
|
|
|
public function setUp() {
|
|
|
|
\OC_Mount_Config::$skipTest = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function tearDown() {
|
|
|
|
\OC_Mount_Config::$skipTest = false;
|
|
|
|
}
|
|
|
|
|
2015-08-11 20:45:07 +03:00
|
|
|
protected function getBackendMock($class = '\OCA\Files_External\Lib\Backend\SMB', $storageClass = '\OC\Files\Storage\SMB') {
|
|
|
|
$backend = $this->getMockBuilder('\OCA\Files_External\Lib\Backend\Backend')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$backend->method('getStorageClass')
|
|
|
|
->willReturn($storageClass);
|
|
|
|
$backend->method('getClass')
|
|
|
|
->willReturn($storageClass);
|
|
|
|
return $backend;
|
|
|
|
}
|
|
|
|
|
2014-10-31 13:41:07 +03:00
|
|
|
public function testAddStorage() {
|
2015-08-11 20:45:07 +03:00
|
|
|
$backend = $this->getBackendMock();
|
|
|
|
$backend->method('validateStorage')
|
|
|
|
->willReturn(true);
|
|
|
|
$backend->method('isVisibleFor')
|
|
|
|
->willReturn(true);
|
|
|
|
|
2014-10-31 13:41:07 +03:00
|
|
|
$storageConfig = new StorageConfig(1);
|
|
|
|
$storageConfig->setMountPoint('mount');
|
2015-08-11 20:45:07 +03:00
|
|
|
$storageConfig->setBackend($backend);
|
|
|
|
$storageConfig->setBackendOptions([]);
|
2014-10-31 13:41:07 +03:00
|
|
|
|
2015-08-11 20:45:07 +03:00
|
|
|
$this->service->expects($this->once())
|
|
|
|
->method('createStorage')
|
|
|
|
->will($this->returnValue($storageConfig));
|
2014-10-31 13:41:07 +03:00
|
|
|
$this->service->expects($this->once())
|
|
|
|
->method('addStorage')
|
|
|
|
->will($this->returnValue($storageConfig));
|
|
|
|
|
|
|
|
$response = $this->controller->create(
|
|
|
|
'mount',
|
|
|
|
'\OC\Files\Storage\SMB',
|
|
|
|
array(),
|
|
|
|
[],
|
|
|
|
[],
|
2015-03-13 14:49:11 +03:00
|
|
|
[],
|
2014-10-31 13:41:07 +03:00
|
|
|
null
|
|
|
|
);
|
|
|
|
|
|
|
|
$data = $response->getData();
|
|
|
|
$this->assertEquals($storageConfig, $data);
|
|
|
|
$this->assertEquals(Http::STATUS_CREATED, $response->getStatus());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testUpdateStorage() {
|
2015-08-11 20:45:07 +03:00
|
|
|
$backend = $this->getBackendMock();
|
|
|
|
$backend->method('validateStorage')
|
|
|
|
->willReturn(true);
|
|
|
|
$backend->method('isVisibleFor')
|
|
|
|
->willReturn(true);
|
|
|
|
|
2014-10-31 13:41:07 +03:00
|
|
|
$storageConfig = new StorageConfig(1);
|
|
|
|
$storageConfig->setMountPoint('mount');
|
2015-08-11 20:45:07 +03:00
|
|
|
$storageConfig->setBackend($backend);
|
|
|
|
$storageConfig->setBackendOptions([]);
|
2014-10-31 13:41:07 +03:00
|
|
|
|
2015-08-11 20:45:07 +03:00
|
|
|
$this->service->expects($this->once())
|
|
|
|
->method('createStorage')
|
|
|
|
->will($this->returnValue($storageConfig));
|
2014-10-31 13:41:07 +03:00
|
|
|
$this->service->expects($this->once())
|
|
|
|
->method('updateStorage')
|
|
|
|
->will($this->returnValue($storageConfig));
|
|
|
|
|
|
|
|
$response = $this->controller->update(
|
|
|
|
1,
|
|
|
|
'mount',
|
|
|
|
'\OC\Files\Storage\SMB',
|
|
|
|
array(),
|
|
|
|
[],
|
|
|
|
[],
|
2015-03-13 14:49:11 +03:00
|
|
|
[],
|
2014-10-31 13:41:07 +03:00
|
|
|
null
|
|
|
|
);
|
|
|
|
|
|
|
|
$data = $response->getData();
|
|
|
|
$this->assertEquals($storageConfig, $data);
|
|
|
|
$this->assertEquals(Http::STATUS_OK, $response->getStatus());
|
|
|
|
}
|
|
|
|
|
|
|
|
function mountPointNamesProvider() {
|
|
|
|
return array(
|
|
|
|
array(''),
|
|
|
|
array('/'),
|
|
|
|
array('//'),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider mountPointNamesProvider
|
|
|
|
*/
|
|
|
|
public function testAddOrUpdateStorageInvalidMountPoint($mountPoint) {
|
2015-08-11 20:45:07 +03:00
|
|
|
$storageConfig = new StorageConfig(1);
|
|
|
|
$storageConfig->setMountPoint($mountPoint);
|
|
|
|
$storageConfig->setBackend($this->getBackendMock());
|
|
|
|
$storageConfig->setBackendOptions([]);
|
|
|
|
|
|
|
|
$this->service->expects($this->exactly(2))
|
|
|
|
->method('createStorage')
|
|
|
|
->will($this->returnValue($storageConfig));
|
2014-10-31 13:41:07 +03:00
|
|
|
$this->service->expects($this->never())
|
|
|
|
->method('addStorage');
|
|
|
|
$this->service->expects($this->never())
|
|
|
|
->method('updateStorage');
|
|
|
|
|
|
|
|
$response = $this->controller->create(
|
|
|
|
$mountPoint,
|
|
|
|
'\OC\Files\Storage\SMB',
|
|
|
|
array(),
|
|
|
|
[],
|
|
|
|
[],
|
2015-03-13 14:49:11 +03:00
|
|
|
[],
|
2014-10-31 13:41:07 +03:00
|
|
|
null
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(Http::STATUS_UNPROCESSABLE_ENTITY, $response->getStatus());
|
|
|
|
|
|
|
|
$response = $this->controller->update(
|
|
|
|
1,
|
|
|
|
$mountPoint,
|
|
|
|
'\OC\Files\Storage\SMB',
|
|
|
|
array(),
|
|
|
|
[],
|
|
|
|
[],
|
2015-03-13 14:49:11 +03:00
|
|
|
[],
|
2014-10-31 13:41:07 +03:00
|
|
|
null
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(Http::STATUS_UNPROCESSABLE_ENTITY, $response->getStatus());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAddOrUpdateStorageInvalidBackend() {
|
2015-08-11 20:45:07 +03:00
|
|
|
$this->service->expects($this->exactly(2))
|
|
|
|
->method('createStorage')
|
|
|
|
->will($this->throwException(new \InvalidArgumentException()));
|
2014-10-31 13:41:07 +03:00
|
|
|
$this->service->expects($this->never())
|
|
|
|
->method('addStorage');
|
|
|
|
$this->service->expects($this->never())
|
|
|
|
->method('updateStorage');
|
|
|
|
|
|
|
|
$response = $this->controller->create(
|
|
|
|
'mount',
|
|
|
|
'\OC\Files\Storage\InvalidStorage',
|
|
|
|
array(),
|
|
|
|
[],
|
|
|
|
[],
|
2015-03-13 14:49:11 +03:00
|
|
|
[],
|
2014-10-31 13:41:07 +03:00
|
|
|
null
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(Http::STATUS_UNPROCESSABLE_ENTITY, $response->getStatus());
|
|
|
|
|
|
|
|
$response = $this->controller->update(
|
|
|
|
1,
|
|
|
|
'mount',
|
|
|
|
'\OC\Files\Storage\InvalidStorage',
|
|
|
|
array(),
|
|
|
|
[],
|
|
|
|
[],
|
2015-03-13 14:49:11 +03:00
|
|
|
[],
|
2014-10-31 13:41:07 +03:00
|
|
|
null
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(Http::STATUS_UNPROCESSABLE_ENTITY, $response->getStatus());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testUpdateStorageNonExisting() {
|
2015-08-11 20:45:07 +03:00
|
|
|
$backend = $this->getBackendMock();
|
|
|
|
$backend->method('validateStorage')
|
|
|
|
->willReturn(true);
|
|
|
|
$backend->method('isVisibleFor')
|
|
|
|
->willReturn(true);
|
|
|
|
|
|
|
|
$storageConfig = new StorageConfig(255);
|
|
|
|
$storageConfig->setMountPoint('mount');
|
|
|
|
$storageConfig->setBackend($backend);
|
|
|
|
$storageConfig->setBackendOptions([]);
|
|
|
|
|
|
|
|
$this->service->expects($this->once())
|
|
|
|
->method('createStorage')
|
|
|
|
->will($this->returnValue($storageConfig));
|
2014-10-31 13:41:07 +03:00
|
|
|
$this->service->expects($this->once())
|
|
|
|
->method('updateStorage')
|
|
|
|
->will($this->throwException(new NotFoundException()));
|
|
|
|
|
|
|
|
$response = $this->controller->update(
|
|
|
|
255,
|
|
|
|
'mount',
|
|
|
|
'\OC\Files\Storage\SMB',
|
|
|
|
array(),
|
|
|
|
[],
|
|
|
|
[],
|
2015-03-13 14:49:11 +03:00
|
|
|
[],
|
2014-10-31 13:41:07 +03:00
|
|
|
null
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(Http::STATUS_NOT_FOUND, $response->getStatus());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDeleteStorage() {
|
|
|
|
$this->service->expects($this->once())
|
|
|
|
->method('removeStorage');
|
|
|
|
|
|
|
|
$response = $this->controller->destroy(1);
|
|
|
|
$this->assertEquals(Http::STATUS_NO_CONTENT, $response->getStatus());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDeleteStorageNonExisting() {
|
|
|
|
$this->service->expects($this->once())
|
|
|
|
->method('removeStorage')
|
|
|
|
->will($this->throwException(new NotFoundException()));
|
|
|
|
|
|
|
|
$response = $this->controller->destroy(255);
|
|
|
|
$this->assertEquals(Http::STATUS_NOT_FOUND, $response->getStatus());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetStorage() {
|
2015-08-11 20:45:07 +03:00
|
|
|
$backend = $this->getBackendMock();
|
2014-10-31 13:41:07 +03:00
|
|
|
$storageConfig = new StorageConfig(1);
|
|
|
|
$storageConfig->setMountPoint('test');
|
2015-08-11 20:45:07 +03:00
|
|
|
$storageConfig->setBackend($backend);
|
2014-10-31 13:41:07 +03:00
|
|
|
$storageConfig->setBackendOptions(['user' => 'test', 'password', 'password123']);
|
2015-03-13 14:49:11 +03:00
|
|
|
$storageConfig->setMountOptions(['priority' => false]);
|
2014-10-31 13:41:07 +03:00
|
|
|
|
|
|
|
$this->service->expects($this->once())
|
|
|
|
->method('getStorage')
|
|
|
|
->with(1)
|
|
|
|
->will($this->returnValue($storageConfig));
|
|
|
|
$response = $this->controller->show(1);
|
|
|
|
|
|
|
|
$this->assertEquals(Http::STATUS_OK, $response->getStatus());
|
|
|
|
$this->assertEquals($storageConfig, $response->getData());
|
|
|
|
}
|
|
|
|
}
|