diff options
author | Christoph Wurst <christoph@winzerhof-wurst.at> | 2017-07-04 13:02:05 +0300 |
---|---|---|
committer | Christoph Wurst <christoph@winzerhof-wurst.at> | 2017-07-04 13:02:05 +0300 |
commit | fb511b6c4755ffc4b23a361aba1bbafb2d49d7ef (patch) | |
tree | 612041e62973fc0bca10e7ac288c586e10b6a30c /tests | |
parent | fda6e7f45bc0b4a6e54fd5d52d30aa1563aa2ecd (diff) |
Use app data to store uploaded files
* Restructure code into separate, testable units
* Add unit tests
Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/Controller/LocalAttachmentsControllerTest.php | 89 | ||||
-rw-r--r-- | tests/Service/Attachment/AttachmentServiceTest.php | 110 | ||||
-rw-r--r-- | tests/Service/Attachment/AttachmentStorageTest.php | 172 | ||||
-rw-r--r-- | tests/Service/Attachment/UploadedFileTest.php | 57 |
4 files changed, 428 insertions, 0 deletions
diff --git a/tests/Controller/LocalAttachmentsControllerTest.php b/tests/Controller/LocalAttachmentsControllerTest.php new file mode 100644 index 000000000..0e8381a0a --- /dev/null +++ b/tests/Controller/LocalAttachmentsControllerTest.php @@ -0,0 +1,89 @@ +<?php + +/** + * @author Christoph Wurst <christoph@winzerhof-wurst.at> + * + * Mail + * + * 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\Mail\Tests\Controller; + +use OCA\Mail\Contracts\IAttachmentService; +use OCA\Mail\Controller\LocalAttachmentsController; +use OCA\Mail\Db\LocalAttachment; +use OCA\Mail\Service\Attachment\UploadedFile; +use OCP\AppFramework\Http\JSONResponse; +use OCP\IRequest; +use PHPUnit_Framework_MockObject_MockObject; +use PHPUnit_Framework_TestCase; + +class LocalAttachmentsControllerTest extends PHPUnit_Framework_TestCase { + + /** @var IRequest|PHPUnit_Framework_MockObject_MockObject */ + private $request; + + /** @var IAttachmentService|PHPUnit_Framework_MockObject_MockObject */ + private $service; + + /** @var string */ + private $userId; + + /** @var LocalAttachmentsController */ + private $controller; + + protected function setUp() { + parent::setUp(); + + $this->request = $this->createMock(IRequest::class); + $this->service = $this->createMock(IAttachmentService::class); + $this->userId = 'jane'; + + $this->controller = new LocalAttachmentsController('mail', $this->request, $this->service, $this->userId); + } + + public function testCreateWithoutFile() { + $this->request->expects($this->once()) + ->method('getUploadedFile') + ->with('attachment') + ->willReturn(null); + $expected = new JSONResponse(null, 400); + + $actual = $this->controller->create(); + + $this->assertEquals($expected, $actual); + } + + public function testCreate() { + $fileData = [ + 'name' => 'cat.jpg', + ]; + $this->request->expects($this->once()) + ->method('getUploadedFile') + ->with('attachment') + ->willReturn($fileData); + $attachment = new LocalAttachment(); + $uploadedFile = new UploadedFile($fileData); + $this->service->expects($this->once()) + ->method('addFile') + ->with($this->equalTo($this->userId), $this->equalTo($uploadedFile)) + ->willReturn($attachment); + + $actual = $this->controller->create(); + + $this->assertEquals(new JSONResponse($attachment, 201), $actual); + } + +} diff --git a/tests/Service/Attachment/AttachmentServiceTest.php b/tests/Service/Attachment/AttachmentServiceTest.php new file mode 100644 index 000000000..7cf8bd38d --- /dev/null +++ b/tests/Service/Attachment/AttachmentServiceTest.php @@ -0,0 +1,110 @@ +<?php + +/** + * @author Christoph Wurst <christoph@winzerhof-wurst.at> + * + * Mail + * + * 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\Mail\Tests\Service\Attachment; + +use OCA\Mail\Db\LocalAttachment; +use OCA\Mail\Db\LocalAttachmentMapper; +use OCA\Mail\Exception\UploadException; +use OCA\Mail\Service\Attachment\AttachmentService; +use OCA\Mail\Service\Attachment\AttachmentStorage; +use OCA\Mail\Service\Attachment\UploadedFile; +use PHPUnit_Framework_MockObject_MockObject; +use PHPUnit_Framework_TestCase; + +class AttachmentServiceTest extends PHPUnit_Framework_TestCase { + + /** @var LocalAttachmentMapper|PHPUnit_Framework_MockObject_MockObject */ + private $mapper; + + /** @var AttachmentStorage|PHPUnit_Framework_MockObject_MockObject */ + private $storage; + + /** @var AttachmentService */ + private $service; + + protected function setUp() { + parent::setUp(); + + $this->mapper = $this->createMock(LocalAttachmentMapper::class); + $this->storage = $this->createMock(AttachmentStorage::class); + + $this->service = new AttachmentService($this->mapper, $this->storage); + } + + public function testAddFileWithUploadException() { + $userId = 'jan'; + $uploadedFile = $this->createMock(UploadedFile::class); + $uploadedFile->expects($this->once()) + ->method('getFileName') + ->willReturn('cat.jpg'); + $attachment = LocalAttachment::fromParams([ + 'userId' => $userId, + 'fileName' => 'cat.jpg', + ]); + $persistedAttachment = LocalAttachment::fromParams([ + 'id' => 123, + 'userId' => $userId, + 'fileName' => 'cat.jpg', + ]); + $this->mapper->expects($this->once()) + ->method('insert') + ->with($this->equalTo($attachment)) + ->willReturn($persistedAttachment); + $this->storage->expects($this->once()) + ->method('save') + ->with($this->equalTo($userId), $this->equalTo($persistedAttachment), $this->equalTo($uploadedFile)) + ->willThrowException(new UploadException()); + $this->mapper->expects($this->once()) + ->method('delete') + ->with($this->equalTo($persistedAttachment)); + $this->expectException(UploadException::class); + + $this->service->addFile($userId, $uploadedFile); + } + + public function testAddFile() { + $userId = 'jan'; + $uploadedFile = $this->createMock(UploadedFile::class); + $uploadedFile->expects($this->once()) + ->method('getFileName') + ->willReturn('cat.jpg'); + $attachment = LocalAttachment::fromParams([ + 'userId' => $userId, + 'fileName' => 'cat.jpg', + ]); + $persistedAttachment = LocalAttachment::fromParams([ + 'id' => 123, + 'userId' => $userId, + 'fileName' => 'cat.jpg', + ]); + $this->mapper->expects($this->once()) + ->method('insert') + ->with($this->equalTo($attachment)) + ->willReturn($persistedAttachment); + $this->storage->expects($this->once()) + ->method('save') + ->with($this->equalTo($userId), $this->equalTo($persistedAttachment), $this->equalTo($uploadedFile)); + + $this->service->addFile($userId, $uploadedFile); + } + +} diff --git a/tests/Service/Attachment/AttachmentStorageTest.php b/tests/Service/Attachment/AttachmentStorageTest.php new file mode 100644 index 000000000..4bebeff0f --- /dev/null +++ b/tests/Service/Attachment/AttachmentStorageTest.php @@ -0,0 +1,172 @@ +<?php + +/** + * @author Christoph Wurst <christoph@winzerhof-wurst.at> + * + * Mail + * + * 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\Mail\Tests\Service\Attachment; + +use OCA\Mail\Exception\UploadException; +use OCA\Mail\Service\Attachment\AttachmentStorage; +use OCA\Mail\Service\Attachment\UploadedFile; +use OCP\Files\IAppData; +use OCP\Files\NotFoundException; +use OCP\Files\NotPermittedException; +use OCP\Files\SimpleFS\ISimpleFile; +use OCP\Files\SimpleFS\ISimpleFolder; +use PHPUnit_Framework_MockObject_MockObject; +use PHPUnit_Framework_TestCase; + +class AttachmentStorageTest extends PHPUnit_Framework_TestCase { + + private $tmpFilePath = '/tmp/nc_mail_attachment_test'; + + /** @var IAppData|PHPUnit_Framework_MockObject_MockObject */ + private $appData; + + /** @var AttachmentStorage */ + private $storage; + + protected function setUp() { + parent::setUp(); + + file_put_contents($this->tmpFilePath, 'test test'); + + $this->appData = $this->createMock(IAppData::class); + $this->storage = new AttachmentStorage($this->appData); + } + + public function testSaveWithFolderNotExisting() { + $folder = $this->createMock(ISimpleFolder::class); + $file = $this->createMock(ISimpleFile::class); + $uploadedFile = $this->createMock(UploadedFile::class); + + $this->appData->expects($this->once()) + ->method('getFolder') + ->with($this->equalTo('mail_fritz')) + ->willThrowException(new NotFoundException()); + $this->appData->expects($this->once()) + ->method('newFolder') + ->with($this->equalTo('mail_fritz')) + ->willReturn($folder); + $folder->expects($this->once()) + ->method('newFile') + ->with(123) + ->willReturn($file); + $uploadedFile->expects($this->once()) + ->method('getTempPath') + ->willReturn($this->tmpFilePath); + $file->expects($this->once()) + ->method('putContent') + ->with($this->equalTo('test test')); + + $this->storage->save('fritz', 123, $uploadedFile); + } + + public function testSaveWithPermissionProblems() { + $folder = $this->createMock(ISimpleFolder::class); + $file = $this->createMock(ISimpleFile::class); + $uploadedFile = $this->createMock(UploadedFile::class); + + $this->appData->expects($this->once()) + ->method('getFolder') + ->with($this->equalTo('mail_fritz')) + ->willThrowException(new NotFoundException()); + $this->appData->expects($this->once()) + ->method('newFolder') + ->with($this->equalTo('mail_fritz')) + ->willThrowException(new NotPermittedException()); + $this->expectException(NotPermittedException::class); + + $this->storage->save('fritz', 123, $uploadedFile); + } + + public function testSaveWithoutTempPath() { + $folder = $this->createMock(ISimpleFolder::class); + $file = $this->createMock(ISimpleFile::class); + $uploadedFile = $this->createMock(UploadedFile::class); + + $this->appData->expects($this->once()) + ->method('getFolder') + ->with($this->equalTo('mail_fritz')) + ->willThrowException(new NotFoundException()); + $this->appData->expects($this->once()) + ->method('newFolder') + ->with($this->equalTo('mail_fritz')) + ->willReturn($folder); + $folder->expects($this->once()) + ->method('newFile') + ->with(123) + ->willReturn($file); + $uploadedFile->expects($this->once()) + ->method('getTempPath') + ->willReturn(null); + $this->expectException(UploadException::class); + + $this->storage->save('fritz', 123, $uploadedFile); + } + + public function testSaveWithFileReadError() { + $folder = $this->createMock(ISimpleFolder::class); + $file = $this->createMock(ISimpleFile::class); + $uploadedFile = $this->createMock(UploadedFile::class); + + $this->appData->expects($this->once()) + ->method('getFolder') + ->with($this->equalTo('mail_fritz')) + ->willThrowException(new NotFoundException()); + $this->appData->expects($this->once()) + ->method('newFolder') + ->with($this->equalTo('mail_fritz')) + ->willReturn($folder); + $folder->expects($this->once()) + ->method('newFile') + ->with(123) + ->willReturn($file); + $uploadedFile->expects($this->once()) + ->method('getTempPath') + ->willReturn('/doesntexist'); + $this->expectException(UploadException::class); + + $this->storage->save('fritz', 123, $uploadedFile); + } + + public function testSave() { + $folder = $this->createMock(ISimpleFolder::class); + $file = $this->createMock(ISimpleFile::class); + $uploadedFile = $this->createMock(UploadedFile::class); + + $this->appData->expects($this->once()) + ->method('getFolder') + ->with($this->equalTo('mail_fritz')) + ->willReturn($folder); + $folder->expects($this->once()) + ->method('newFile') + ->with(123) + ->willReturn($file); + $uploadedFile->expects($this->once()) + ->method('getTempPath') + ->willReturn($this->tmpFilePath); + $file->expects($this->once()) + ->method('putContent') + ->with($this->equalTo('test test')); + + $this->storage->save('fritz', 123, $uploadedFile); + } + +} diff --git a/tests/Service/Attachment/UploadedFileTest.php b/tests/Service/Attachment/UploadedFileTest.php new file mode 100644 index 000000000..f81550253 --- /dev/null +++ b/tests/Service/Attachment/UploadedFileTest.php @@ -0,0 +1,57 @@ +<?php + +/** + * @author Christoph Wurst <christoph@winzerhof-wurst.at> + * + * Mail + * + * 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\Mail\Tests\Service\Attachment; + +use OCA\Mail\Service\Attachment\UploadedFile; +use PHPUnit_Framework_TestCase; + +class UploadedFileTest extends PHPUnit_Framework_TestCase { + + public function testGetFileNameNotExisting() { + $file = new UploadedFile([]); + + $this->assertEquals(null, $file->getFileName()); + } + + public function testGetFileName() { + $file = new UploadedFile([ + 'name' => 'cat.jpg', + ]); + + $this->assertEquals('cat.jpg', $file->getFileName()); + } + + public function testGetTempPathNotExisting() { + $file = new UploadedFile([]); + + $this->assertEquals(null, $file->getTempPath()); + } + + public function testGetTempPath() { + $file = new UploadedFile([ + 'tmp_path' => '/tmp/path', + ]); + + $this->assertEquals('/tmp/path', $file->getTempPath()); + } + +} |