Add unit tests
Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
This commit is contained in:
parent
c9d2e31d52
commit
685f272212
4 changed files with 437 additions and 16 deletions
|
@ -20,17 +20,19 @@ class ShareInfoController extends ApiController {
|
|||
/** @var IManager */
|
||||
private $shareManager;
|
||||
|
||||
/** @var ILogger */
|
||||
private $logger;
|
||||
|
||||
/**
|
||||
* ShareInfoController constructor.
|
||||
*
|
||||
* @param string $appName
|
||||
* @param IRequest $request
|
||||
* @param IManager $shareManager
|
||||
*/
|
||||
public function __construct($appName,
|
||||
IRequest $request,
|
||||
IManager $shareManager,
|
||||
ILogger $logger) {
|
||||
IManager $shareManager) {
|
||||
parent::__construct($appName, $request);
|
||||
|
||||
$this->shareManager = $shareManager;
|
||||
$this->logger = $logger;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -58,15 +60,9 @@ class ShareInfoController extends ApiController {
|
|||
return new JSONResponse([], Http::STATUS_FORBIDDEN);
|
||||
}
|
||||
|
||||
// TODO FIX!!!
|
||||
$isWritable = $share->getPermissions() & (\OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_CREATE);
|
||||
if (!$isWritable) {
|
||||
// FIXME: should not add storage wrappers outside of preSetup, need to find a better way
|
||||
$previousLog = \OC\Files\Filesystem::logWarningWhenAddingStorageWrapper(false);
|
||||
\OC\Files\Filesystem::addStorageWrapper('readonly', function ($mountPoint, $storage) {
|
||||
return new \OC\Files\Storage\Wrapper\PermissionsMask(array('storage' => $storage, 'mask' => \OCP\Constants::PERMISSION_READ + \OCP\Constants::PERMISSION_SHARE));
|
||||
});
|
||||
\OC\Files\Filesystem::logWarningWhenAddingStorageWrapper($previousLog);
|
||||
$this->addROWrapper();
|
||||
}
|
||||
|
||||
$node = $share->getNode();
|
||||
|
@ -122,4 +118,13 @@ class ShareInfoController extends ApiController {
|
|||
|
||||
return $entry;
|
||||
}
|
||||
|
||||
protected function addROWrapper() {
|
||||
// FIXME: should not add storage wrappers outside of preSetup, need to find a better way
|
||||
$previousLog = \OC\Files\Filesystem::logWarningWhenAddingStorageWrapper(false);
|
||||
\OC\Files\Filesystem::addStorageWrapper('readonly', function ($mountPoint, $storage) {
|
||||
return new \OC\Files\Storage\Wrapper\PermissionsMask(array('storage' => $storage, 'mask' => \OCP\Constants::PERMISSION_READ + \OCP\Constants::PERMISSION_SHARE));
|
||||
});
|
||||
\OC\Files\Filesystem::logWarningWhenAddingStorageWrapper($previousLog);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -25,7 +25,7 @@ class ShareInfoMiddleware extends Middleware {
|
|||
* @param string $methodName
|
||||
* @throws S2SException
|
||||
*/
|
||||
public function beforeController(Controller $controller, $methodName) {
|
||||
public function beforeController($controller, $methodName) {
|
||||
if (!($controller instanceof ShareInfoController)) {
|
||||
return;
|
||||
}
|
||||
|
@ -42,7 +42,7 @@ class ShareInfoMiddleware extends Middleware {
|
|||
* @throws \Exception
|
||||
* @return Response
|
||||
*/
|
||||
public function afterException(Controller $controller, $methodName, \Exception $exception) {
|
||||
public function afterException($controller, $methodName, \Exception $exception) {
|
||||
if (!($controller instanceof ShareInfoController)) {
|
||||
throw $exception;
|
||||
}
|
||||
|
@ -50,6 +50,8 @@ class ShareInfoMiddleware extends Middleware {
|
|||
if ($exception instanceof S2SException) {
|
||||
return new JSONResponse([], Http::STATUS_NOT_FOUND);
|
||||
}
|
||||
|
||||
throw $exception;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -58,7 +60,7 @@ class ShareInfoMiddleware extends Middleware {
|
|||
* @param Response $response
|
||||
* @return Response
|
||||
*/
|
||||
public function afterController(Controller $controller, $methodName, Response $response) {
|
||||
public function afterController($controller, $methodName, Response $response) {
|
||||
if (!($controller instanceof ShareInfoController)) {
|
||||
return $response;
|
||||
}
|
||||
|
|
278
apps/files_sharing/tests/Controller/ShareInfoControllerTest.php
Normal file
278
apps/files_sharing/tests/Controller/ShareInfoControllerTest.php
Normal file
|
@ -0,0 +1,278 @@
|
|||
<?php
|
||||
|
||||
namespace OCA\Files_Sharing\Tests\Controller;
|
||||
|
||||
use OCA\Files_Sharing\Controller\ShareInfoController;
|
||||
use OCP\AppFramework\Http;
|
||||
use OCP\AppFramework\Http\JSONResponse;
|
||||
use OCP\Constants;
|
||||
use OCP\Files\File;
|
||||
use OCP\Files\Folder;
|
||||
use OCP\IRequest;
|
||||
use OCP\Share\Exceptions\ShareNotFound;
|
||||
use OCP\Share\IManager as ShareManager;
|
||||
use OCP\Share\IShare;
|
||||
use Test\TestCase;
|
||||
|
||||
class ShareInfoControllerTest extends TestCase {
|
||||
|
||||
/** @var ShareInfoController */
|
||||
private $controller;
|
||||
|
||||
/** @var ShareManager|\PHPUnit_Framework_MockObject_MockObject */
|
||||
private $shareManager;
|
||||
|
||||
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->shareManager = $this->createMock(ShareManager::class);
|
||||
|
||||
$this->controller = $this->getMockBuilder(ShareInfoController::class)
|
||||
->setConstructorArgs([
|
||||
'files_sharing',
|
||||
$this->createMock(IRequest::class),
|
||||
$this->shareManager
|
||||
])
|
||||
->setMethods(['addROWrapper'])
|
||||
->getMock();
|
||||
}
|
||||
|
||||
public function testNoShare() {
|
||||
$this->shareManager->method('getShareByToken')
|
||||
->with('token')
|
||||
->willThrowException(new ShareNotFound());
|
||||
|
||||
$expected = new JSONResponse([], Http::STATUS_NOT_FOUND);
|
||||
$this->assertEquals($expected, $this->controller->info('token'));
|
||||
}
|
||||
|
||||
public function testWrongPassword() {
|
||||
$share = $this->createMock(IShare::class);
|
||||
$share->method('getPassword')
|
||||
->willReturn('sharePass');
|
||||
|
||||
$this->shareManager->method('getShareByToken')
|
||||
->with('token')
|
||||
->willReturn($share);
|
||||
$this->shareManager->method('checkPassword')
|
||||
->with($share, 'pass')
|
||||
->willReturn(false);
|
||||
|
||||
$expected = new JSONResponse([], Http::STATUS_FORBIDDEN);
|
||||
$this->assertEquals($expected, $this->controller->info('token', 'pass'));
|
||||
}
|
||||
|
||||
public function testNoReadPermissions() {
|
||||
$share = $this->createMock(IShare::class);
|
||||
$share->method('getPassword')
|
||||
->willReturn('sharePass');
|
||||
$share->method('getPermissions')
|
||||
->willReturn(Constants::PERMISSION_CREATE);
|
||||
|
||||
$this->shareManager->method('getShareByToken')
|
||||
->with('token')
|
||||
->willReturn($share);
|
||||
$this->shareManager->method('checkPassword')
|
||||
->with($share, 'pass')
|
||||
->willReturn(true);
|
||||
|
||||
$expected = new JSONResponse([], Http::STATUS_FORBIDDEN);
|
||||
$this->assertEquals($expected, $this->controller->info('token', 'pass'));
|
||||
}
|
||||
|
||||
private function prepareFile() {
|
||||
$file = $this->createMock(File::class);
|
||||
|
||||
$file->method('getId')->willReturn(42);
|
||||
|
||||
$parent = $this->createMock(Folder::class);
|
||||
$parent->method('getId')->willReturn(41);
|
||||
$file->method('getParent')->willReturn($parent);
|
||||
|
||||
$file->method('getMTime')->willReturn(1337);
|
||||
$file->method('getName')->willReturn('file');
|
||||
$file->method('getPermissions')->willReturn(Constants::PERMISSION_READ);
|
||||
$file->method('getMimeType')->willReturn('mime/type');
|
||||
$file->method('getSize')->willReturn(1);
|
||||
$file->method('getType')->willReturn('file');
|
||||
$file->method('getEtag')->willReturn('etag');
|
||||
|
||||
return $file;
|
||||
}
|
||||
|
||||
public function testInfoFile() {
|
||||
$file = $this->prepareFile();
|
||||
|
||||
$share = $this->createMock(IShare::class);
|
||||
$share->method('getPassword')
|
||||
->willReturn('sharePass');
|
||||
$share->method('getPermissions')
|
||||
->willReturn(Constants::PERMISSION_READ | Constants::PERMISSION_UPDATE);
|
||||
$share->method('getNode')
|
||||
->willReturn($file);
|
||||
|
||||
$this->shareManager->method('getShareByToken')
|
||||
->with('token')
|
||||
->willReturn($share);
|
||||
$this->shareManager->method('checkPassword')
|
||||
->with($share, 'pass')
|
||||
->willReturn(true);
|
||||
|
||||
$expected = new JSONResponse([
|
||||
'id' => 42,
|
||||
'parentId' => 41,
|
||||
'mtime' => 1337 ,
|
||||
'name' => 'file',
|
||||
'permissions' => 1,
|
||||
'mimetype' => 'mime/type',
|
||||
'size' => 1,
|
||||
'type' => 'file',
|
||||
'etag' => 'etag',
|
||||
]);
|
||||
$this->assertEquals($expected, $this->controller->info('token', 'pass'));
|
||||
}
|
||||
|
||||
public function testInfoFileRO() {
|
||||
$file = $this->prepareFile();
|
||||
|
||||
$share = $this->createMock(IShare::class);
|
||||
$share->method('getPassword')
|
||||
->willReturn('sharePass');
|
||||
$share->method('getPermissions')
|
||||
->willReturn(Constants::PERMISSION_READ);
|
||||
$share->method('getNode')
|
||||
->willReturn($file);
|
||||
|
||||
$this->shareManager->method('getShareByToken')
|
||||
->with('token')
|
||||
->willReturn($share);
|
||||
$this->shareManager->method('checkPassword')
|
||||
->with($share, 'pass')
|
||||
->willReturn(true);
|
||||
|
||||
$this->controller->expects($this->once())
|
||||
->method('addROWrapper');
|
||||
|
||||
$expected = new JSONResponse([
|
||||
'id' => 42,
|
||||
'parentId' => 41,
|
||||
'mtime' => 1337 ,
|
||||
'name' => 'file',
|
||||
'permissions' => 1,
|
||||
'mimetype' => 'mime/type',
|
||||
'size' => 1,
|
||||
'type' => 'file',
|
||||
'etag' => 'etag',
|
||||
]);
|
||||
$this->assertEquals($expected, $this->controller->info('token', 'pass'));
|
||||
}
|
||||
|
||||
private function prepareFolder() {
|
||||
$root = $this->createMock(Folder::class);
|
||||
|
||||
$root->method('getId')->willReturn(42);
|
||||
|
||||
$parent = $this->createMock(Folder::class);
|
||||
$parent->method('getId')->willReturn(41);
|
||||
$root->method('getParent')->willReturn($parent);
|
||||
|
||||
$root->method('getMTime')->willReturn(1337);
|
||||
$root->method('getName')->willReturn('root');
|
||||
$root->method('getPermissions')->willReturn(Constants::PERMISSION_READ);
|
||||
$root->method('getMimeType')->willReturn('mime/type');
|
||||
$root->method('getSize')->willReturn(1);
|
||||
$root->method('getType')->willReturn('folder');
|
||||
$root->method('getEtag')->willReturn('etag');
|
||||
|
||||
|
||||
//Subfolder
|
||||
$sub = $this->createMock(Folder::class);
|
||||
|
||||
$sub->method('getId')->willReturn(43);
|
||||
$sub->method('getParent')->willReturn($root);
|
||||
$sub->method('getMTime')->willReturn(1338);
|
||||
$sub->method('getName')->willReturn('sub');
|
||||
$sub->method('getPermissions')->willReturn(Constants::PERMISSION_READ | Constants::PERMISSION_UPDATE);
|
||||
$sub->method('getMimeType')->willReturn('mime/type');
|
||||
$sub->method('getSize')->willReturn(2);
|
||||
$sub->method('getType')->willReturn('folder');
|
||||
$sub->method('getEtag')->willReturn('etag2');
|
||||
|
||||
$root->method('getDirectoryListing')->willReturn([$sub]);
|
||||
|
||||
//Subfile
|
||||
$file = $this->createMock(File::class);
|
||||
$file->method('getId')->willReturn(88);
|
||||
$file->method('getParent')->willReturn($sub);
|
||||
$file->method('getMTime')->willReturn(1339);
|
||||
$file->method('getName')->willReturn('file');
|
||||
$file->method('getPermissions')->willReturn(Constants::PERMISSION_READ | Constants::PERMISSION_DELETE);
|
||||
$file->method('getMimeType')->willReturn('mime/type');
|
||||
$file->method('getSize')->willReturn(3);
|
||||
$file->method('getType')->willReturn('file');
|
||||
$file->method('getEtag')->willReturn('etag3');
|
||||
|
||||
$sub->method('getDirectoryListing')->willReturn([$file]);
|
||||
|
||||
return $root;
|
||||
}
|
||||
|
||||
public function testInfoFolder() {
|
||||
$file = $this->prepareFolder();
|
||||
|
||||
$share = $this->createMock(IShare::class);
|
||||
$share->method('getPassword')
|
||||
->willReturn('sharePass');
|
||||
$share->method('getPermissions')
|
||||
->willReturn(Constants::PERMISSION_READ | Constants::PERMISSION_UPDATE);
|
||||
$share->method('getNode')
|
||||
->willReturn($file);
|
||||
|
||||
$this->shareManager->method('getShareByToken')
|
||||
->with('token')
|
||||
->willReturn($share);
|
||||
$this->shareManager->method('checkPassword')
|
||||
->with($share, 'pass')
|
||||
->willReturn(true);
|
||||
|
||||
$expected = new JSONResponse([
|
||||
'id' => 42,
|
||||
'parentId' => 41,
|
||||
'mtime' => 1337,
|
||||
'name' => 'root',
|
||||
'permissions' => 1,
|
||||
'mimetype' => 'mime/type',
|
||||
'size' => 1,
|
||||
'type' => 'folder',
|
||||
'etag' => 'etag',
|
||||
'children' => [
|
||||
[
|
||||
'id' => 43,
|
||||
'parentId' => 42,
|
||||
'mtime' => 1338,
|
||||
'name' => 'sub',
|
||||
'permissions' => 3,
|
||||
'mimetype' => 'mime/type',
|
||||
'size' => 2,
|
||||
'type' => 'folder',
|
||||
'etag' => 'etag2',
|
||||
'children' => [
|
||||
[
|
||||
'id' => 88,
|
||||
'parentId' => 43,
|
||||
'mtime' => 1339,
|
||||
'name' => 'file',
|
||||
'permissions' => 9,
|
||||
'mimetype' => 'mime/type',
|
||||
'size' => 3,
|
||||
'type' => 'file',
|
||||
'etag' => 'etag3',
|
||||
]
|
||||
],
|
||||
]
|
||||
],
|
||||
]);
|
||||
$this->assertEquals($expected, $this->controller->info('token', 'pass'));
|
||||
}
|
||||
}
|
136
apps/files_sharing/tests/Middleware/ShareInfoMiddlewareTest.php
Normal file
136
apps/files_sharing/tests/Middleware/ShareInfoMiddlewareTest.php
Normal file
|
@ -0,0 +1,136 @@
|
|||
<?php
|
||||
|
||||
namespace OCA\Files_Sharing\Tests\Middleware;
|
||||
|
||||
use OCA\Files_Sharing\Controller\ShareInfoController;
|
||||
use OCA\Files_Sharing\Exceptions\S2SException;
|
||||
use OCA\Files_Sharing\Middleware\ShareInfoMiddleware;
|
||||
use OCP\AppFramework\Controller;
|
||||
use OCP\AppFramework\Http;
|
||||
use OCP\AppFramework\Http\JSONResponse;
|
||||
use OCP\Share\IManager as ShareManager;
|
||||
use Test\TestCase;
|
||||
|
||||
class ShareInfoMiddlewareTest extends TestCase {
|
||||
|
||||
/** @var ShareManager|\PHPUnit_Framework_MockObject_MockObject */
|
||||
private $shareManager;
|
||||
|
||||
/** @var ShareInfoMiddleware */
|
||||
private $middleware;
|
||||
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->shareManager = $this->createMock(ShareManager::class);
|
||||
$this->middleware = new ShareInfoMiddleware($this->shareManager);
|
||||
}
|
||||
|
||||
public function testBeforeControllerNoShareInfo() {
|
||||
$this->shareManager->expects($this->never())
|
||||
->method($this->anything());
|
||||
|
||||
$this->middleware->beforeController($this->createMock(ShareInfoMiddlewareTestController::class), 'foo');
|
||||
}
|
||||
|
||||
public function testBeforeControllerShareInfoNoS2s() {
|
||||
$this->shareManager->expects($this->once())
|
||||
->method('outgoingServer2ServerSharesAllowed')
|
||||
->willReturn(false);
|
||||
|
||||
$this->expectException(S2SException::class);
|
||||
$this->middleware->beforeController($this->createMock(ShareInfoController::class), 'foo');
|
||||
}
|
||||
|
||||
public function testBeforeControllerShareInfo() {
|
||||
$this->shareManager->expects($this->once())
|
||||
->method('outgoingServer2ServerSharesAllowed')
|
||||
->willReturn(true);
|
||||
|
||||
$this->middleware->beforeController($this->createMock(ShareInfoController::class), 'foo');
|
||||
}
|
||||
|
||||
public function testAfterExceptionNoShareInfo() {
|
||||
$exeption = new \Exception();
|
||||
|
||||
try {
|
||||
$this->middleware->afterException($this->createMock(ShareInfoMiddlewareTestController::class), 'foo', $exeption);
|
||||
$this->fail();
|
||||
} catch (\Exception $e) {
|
||||
$this->assertSame($exeption, $e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public function testAfterExceptionNoS2S() {
|
||||
$exeption = new \Exception();
|
||||
|
||||
try {
|
||||
$this->middleware->afterException($this->createMock(ShareInfoController::class), 'foo', $exeption);
|
||||
$this->fail();
|
||||
} catch (\Exception $e) {
|
||||
$this->assertSame($exeption, $e);
|
||||
}
|
||||
}
|
||||
|
||||
public function testAfterExceptionS2S() {
|
||||
$expected = new JSONResponse([], Http::STATUS_NOT_FOUND);
|
||||
|
||||
$this->assertEquals(
|
||||
$expected,
|
||||
$this->middleware->afterException($this->createMock(ShareInfoController::class), 'foo', new S2SException())
|
||||
);
|
||||
}
|
||||
|
||||
public function testAfterControllerNoShareInfo() {
|
||||
$response = $this->createMock(Http\Response::class);
|
||||
|
||||
$this->assertEquals(
|
||||
$response,
|
||||
$this->middleware->afterController($this->createMock(ShareInfoMiddlewareTestController::class), 'foo', $response)
|
||||
);
|
||||
}
|
||||
|
||||
public function testAfterControllerNoJSON() {
|
||||
$response = $this->createMock(Http\Response::class);
|
||||
|
||||
$this->assertEquals(
|
||||
$response,
|
||||
$this->middleware->afterController($this->createMock(ShareInfoController::class), 'foo', $response)
|
||||
);
|
||||
}
|
||||
|
||||
public function testAfterControllerJSONok() {
|
||||
$data = ['foo' => 'bar'];
|
||||
$response = new JSONResponse($data);
|
||||
|
||||
$expected = new JSONResponse([
|
||||
'data' => $data,
|
||||
'status' => 'success',
|
||||
]);
|
||||
|
||||
$this->assertEquals(
|
||||
$expected,
|
||||
$this->middleware->afterController($this->createMock(ShareInfoController::class), 'foo', $response)
|
||||
);
|
||||
}
|
||||
|
||||
public function testAfterControllerJSONerror() {
|
||||
$data = ['foo' => 'bar'];
|
||||
$response = new JSONResponse($data, Http::STATUS_FORBIDDEN);
|
||||
|
||||
$expected = new JSONResponse([
|
||||
'data' => $data,
|
||||
'status' => 'error',
|
||||
], Http::STATUS_FORBIDDEN);
|
||||
|
||||
$this->assertEquals(
|
||||
$expected,
|
||||
$this->middleware->afterController($this->createMock(ShareInfoController::class), 'foo', $response)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class ShareInfoMiddlewareTestController extends Controller {
|
||||
|
||||
}
|
Loading…
Reference in a new issue