123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439 |
- <?php
- /**
- * @copyright Copyright (c) 2016, ownCloud, Inc.
- *
- * @author Christoph Wurst <christoph@winzerhof-wurst.at>
- * @author Joas Schilling <coding@schilljs.com>
- * @author Morris Jobke <hey@morrisjobke.de>
- * @author Robin Appelman <robin@icewind.nl>
- * @author Robin McCorkell <robin@mccorkell.me.uk>
- * @author Roeland Jago Douma <roeland@famdouma.nl>
- * @author Vincent Petry <vincent@nextcloud.com>
- *
- * @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\Controller;
- use OCA\Files_External\Controller\GlobalStoragesController;
- use OCA\Files_External\Lib\Auth\AuthMechanism;
- use OCA\Files_External\Lib\Backend\Backend;
- use OCA\Files_External\Lib\StorageConfig;
- use OCA\Files_External\NotFoundException;
- use OCA\Files_External\Service\GlobalStoragesService;
- use OCA\Files_External\Service\UserStoragesService;
- use OCP\AppFramework\Http;
- use PHPUnit\Framework\MockObject\MockObject;
- abstract class StoragesControllerTest extends \Test\TestCase {
- /**
- * @var GlobalStoragesController
- */
- protected $controller;
- /**
- * @var GlobalStoragesService|UserStoragesService|MockObject
- */
- protected $service;
- protected function setUp(): void {
- \OCA\Files_External\MountConfig::$skipTest = true;
- }
- protected function tearDown(): void {
- \OCA\Files_External\MountConfig::$skipTest = false;
- }
- /**
- * @return \OCA\Files_External\Lib\Backend\Backend|MockObject
- */
- protected function getBackendMock($class = '\OCA\Files_External\Lib\Backend\SMB', $storageClass = '\OCA\Files_External\Lib\Storage\SMB') {
- $backend = $this->getMockBuilder(Backend::class)
- ->disableOriginalConstructor()
- ->getMock();
- $backend->method('getStorageClass')
- ->willReturn($storageClass);
- $backend->method('getIdentifier')
- ->willReturn('identifier:'.$class);
- $backend->method('getParameters')
- ->willReturn([]);
- return $backend;
- }
- /**
- * @return \OCA\Files_External\Lib\Auth\AuthMechanism|MockObject
- */
- protected function getAuthMechMock($scheme = 'null', $class = '\OCA\Files_External\Lib\Auth\NullMechanism') {
- $authMech = $this->getMockBuilder(AuthMechanism::class)
- ->disableOriginalConstructor()
- ->getMock();
- $authMech->method('getScheme')
- ->willReturn($scheme);
- $authMech->method('getIdentifier')
- ->willReturn('identifier:'.$class);
- $authMech->method('getParameters')
- ->willReturn([]);
- return $authMech;
- }
- public function testAddStorage() {
- $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->once())
- ->method('createStorage')
- ->willReturn($storageConfig);
- $this->service->expects($this->once())
- ->method('addStorage')
- ->willReturn($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_CREATED, $response->getStatus());
- $this->assertEquals($storageConfig->jsonSerialize(), $data);
- }
- public function testAddLocalStorageWhenDisabled() {
- $authMech = $this->getAuthMechMock();
- $backend = $this->getBackendMock();
- $storageConfig = new StorageConfig(1);
- $storageConfig->setMountPoint('mount');
- $storageConfig->setBackend($backend);
- $storageConfig->setAuthMechanism($authMech);
- $storageConfig->setBackendOptions([]);
- $this->service->expects($this->never())
- ->method('createStorage');
- $this->service->expects($this->never())
- ->method('addStorage');
- $response = $this->controller->create(
- 'mount',
- 'local',
- '\OCA\Files_External\Lib\Auth\NullMechanism',
- [],
- [],
- [],
- [],
- null
- );
- $data = $response->getData();
- $this->assertEquals(Http::STATUS_FORBIDDEN, $response->getStatus());
- }
- public function testUpdateStorage() {
- $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->once())
- ->method('createStorage')
- ->willReturn($storageConfig);
- $this->service->expects($this->once())
- ->method('updateStorage')
- ->willReturn($storageConfig);
- $response = $this->controller->update(
- 1,
- 'mount',
- '\OCA\Files_External\Lib\Storage\SMB',
- '\OCA\Files_External\Lib\Auth\NullMechanism',
- [],
- [],
- [],
- [],
- null
- );
- $data = $response->getData();
- $this->assertEquals(Http::STATUS_OK, $response->getStatus());
- $this->assertEquals($storageConfig->jsonSerialize(), $data);
- }
- public function mountPointNamesProvider() {
- return [
- [''],
- ['/'],
- ['//'],
- ];
- }
- /**
- * @dataProvider mountPointNamesProvider
- */
- public function testAddOrUpdateStorageInvalidMountPoint($mountPoint) {
- $storageConfig = new StorageConfig(1);
- $storageConfig->setMountPoint($mountPoint);
- $storageConfig->setBackend($this->getBackendMock());
- $storageConfig->setAuthMechanism($this->getAuthMechMock());
- $storageConfig->setBackendOptions([]);
- $this->service->expects($this->exactly(2))
- ->method('createStorage')
- ->willReturn($storageConfig);
- $this->service->expects($this->never())
- ->method('addStorage');
- $this->service->expects($this->never())
- ->method('updateStorage');
- $response = $this->controller->create(
- $mountPoint,
- '\OCA\Files_External\Lib\Storage\SMB',
- '\OCA\Files_External\Lib\Auth\NullMechanism',
- [],
- [],
- [],
- [],
- null
- );
- $this->assertEquals(Http::STATUS_UNPROCESSABLE_ENTITY, $response->getStatus());
- $response = $this->controller->update(
- 1,
- $mountPoint,
- '\OCA\Files_External\Lib\Storage\SMB',
- '\OCA\Files_External\Lib\Auth\NullMechanism',
- [],
- [],
- [],
- [],
- null
- );
- $this->assertEquals(Http::STATUS_UNPROCESSABLE_ENTITY, $response->getStatus());
- }
- public function testAddOrUpdateStorageInvalidBackend() {
- $this->service->expects($this->exactly(2))
- ->method('createStorage')
- ->will($this->throwException(new \InvalidArgumentException()));
- $this->service->expects($this->never())
- ->method('addStorage');
- $this->service->expects($this->never())
- ->method('updateStorage');
- $response = $this->controller->create(
- 'mount',
- '\OC\Files\Storage\InvalidStorage',
- '\OCA\Files_External\Lib\Auth\NullMechanism',
- [],
- [],
- [],
- [],
- null
- );
- $this->assertEquals(Http::STATUS_UNPROCESSABLE_ENTITY, $response->getStatus());
- $response = $this->controller->update(
- 1,
- 'mount',
- '\OC\Files\Storage\InvalidStorage',
- '\OCA\Files_External\Lib\Auth\NullMechanism',
- [],
- [],
- [],
- [],
- null
- );
- $this->assertEquals(Http::STATUS_UNPROCESSABLE_ENTITY, $response->getStatus());
- }
- public function testUpdateStorageNonExisting() {
- $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(255);
- $storageConfig->setMountPoint('mount');
- $storageConfig->setBackend($backend);
- $storageConfig->setAuthMechanism($authMech);
- $storageConfig->setBackendOptions([]);
- $this->service->expects($this->once())
- ->method('createStorage')
- ->willReturn($storageConfig);
- $this->service->expects($this->once())
- ->method('updateStorage')
- ->will($this->throwException(new NotFoundException()));
- $response = $this->controller->update(
- 255,
- 'mount',
- '\OCA\Files_External\Lib\Storage\SMB',
- '\OCA\Files_External\Lib\Auth\NullMechanism',
- [],
- [],
- [],
- [],
- 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() {
- $backend = $this->getBackendMock();
- $authMech = $this->getAuthMechMock();
- $storageConfig = new StorageConfig(1);
- $storageConfig->setMountPoint('test');
- $storageConfig->setBackend($backend);
- $storageConfig->setAuthMechanism($authMech);
- $storageConfig->setBackendOptions(['user' => 'test', 'password', 'password123']);
- $storageConfig->setMountOptions(['priority' => false]);
- $this->service->expects($this->once())
- ->method('getStorage')
- ->with(1)
- ->willReturn($storageConfig);
- $response = $this->controller->show(1);
- $this->assertEquals(Http::STATUS_OK, $response->getStatus());
- $expected = $storageConfig->jsonSerialize();
- $expected['can_edit'] = false;
- $this->assertEquals($expected, $response->getData());
- }
- public function validateStorageProvider() {
- return [
- [true, true, true],
- [false, true, false],
- [true, false, false],
- [false, false, false]
- ];
- }
- /**
- * @dataProvider validateStorageProvider
- */
- public function testValidateStorage($backendValidate, $authMechValidate, $expectSuccess) {
- $backend = $this->getBackendMock();
- $backend->method('validateStorage')
- ->willReturn($backendValidate);
- $backend->method('isVisibleFor')
- ->willReturn(true);
- $authMech = $this->getAuthMechMock();
- $authMech->method('validateStorage')
- ->willReturn($authMechValidate);
- $authMech->method('isVisibleFor')
- ->willReturn(true);
- $storageConfig = new StorageConfig();
- $storageConfig->setMountPoint('mount');
- $storageConfig->setBackend($backend);
- $storageConfig->setAuthMechanism($authMech);
- $storageConfig->setBackendOptions([]);
- $this->service->expects($this->once())
- ->method('createStorage')
- ->willReturn($storageConfig);
- if ($expectSuccess) {
- $this->service->expects($this->once())
- ->method('addStorage')
- ->with($storageConfig)
- ->willReturn($storageConfig);
- } else {
- $this->service->expects($this->never())
- ->method('addStorage');
- }
- $response = $this->controller->create(
- 'mount',
- '\OCA\Files_External\Lib\Storage\SMB',
- '\OCA\Files_External\Lib\Auth\NullMechanism',
- [],
- [],
- [],
- [],
- null
- );
- if ($expectSuccess) {
- $this->assertEquals(Http::STATUS_CREATED, $response->getStatus());
- } else {
- $this->assertEquals(Http::STATUS_UNPROCESSABLE_ENTITY, $response->getStatus());
- }
- }
- }
|