From 7036a1407c3dce54a6ac5e283ba55720f85ba7de Mon Sep 17 00:00:00 2001 From: Bernhard Posselt Date: Sat, 31 Aug 2013 02:58:56 +0200 Subject: Throw proper error codes when creating invalid folders through the API, fix #297 --- .../unit/businesslayer/FolderBusinessLayerTest.php | 32 +++++++++++ tests/unit/controller/FolderControllerTest.php | 6 +- tests/unit/external/FeedAPITest.php | 4 +- tests/unit/external/FolderAPITest.php | 64 +++++++++++++++++++++- 4 files changed, 98 insertions(+), 8 deletions(-) (limited to 'tests') diff --git a/tests/unit/businesslayer/FolderBusinessLayerTest.php b/tests/unit/businesslayer/FolderBusinessLayerTest.php index d28c078a5..7f512920e 100644 --- a/tests/unit/businesslayer/FolderBusinessLayerTest.php +++ b/tests/unit/businesslayer/FolderBusinessLayerTest.php @@ -115,6 +115,22 @@ class FolderBusinessLayerTest extends \OCA\AppFramework\Utility\TestUtility { } + public function testCreateThrowsExWhenFolderNameEmpty(){ + $folderName = ''; + $rows = array( + array('id' => 1) + ); + + $this->folderMapper->expects($this->once()) + ->method('findByName') + ->with($this->equalTo($folderName)) + ->will($this->returnValue(array())); + + $this->setExpectedException('\OCA\News\BusinessLayer\BusinessLayerValidationException'); + $result = $this->folderBusinessLayer->create($folderName, 'john', 3); + } + + public function testOpen(){ $folder = new Folder(); @@ -175,6 +191,22 @@ class FolderBusinessLayerTest extends \OCA\AppFramework\Utility\TestUtility { } + public function testRenameThrowsExWhenFolderNameEmpty(){ + $folderName = ''; + $rows = array( + array('id' => 1) + ); + + $this->folderMapper->expects($this->once()) + ->method('findByName') + ->with($this->equalTo($folderName)) + ->will($this->returnValue(array())); + + $this->setExpectedException('\OCA\News\BusinessLayer\BusinessLayerException'); + $result = $this->folderBusinessLayer->rename(3, $folderName, 'john'); + } + + public function testMarkDeleted() { $id = 3; $folder = new Folder(); diff --git a/tests/unit/controller/FolderControllerTest.php b/tests/unit/controller/FolderControllerTest.php index f273c9ec7..e4295583b 100644 --- a/tests/unit/controller/FolderControllerTest.php +++ b/tests/unit/controller/FolderControllerTest.php @@ -34,7 +34,7 @@ use \OCA\AppFramework\Db\MultipleObjectsReturnedException; use \OCA\News\Db\Folder; use \OCA\News\Db\Feed; use \OCA\News\BusinessLayer\BusinessLayerException; -use \OCA\News\BusinessLayer\BusinessLayerExistsException; +use \OCA\News\BusinessLayer\BusinessLayerConflictException; require_once(__DIR__ . "/../../classloader.php"); @@ -256,7 +256,7 @@ class FolderControllerTest extends ControllerTestUtility { public function testCreateReturnsErrorForInvalidCreate(){ $msg = 'except'; - $ex = new BusinessLayerExistsException($msg); + $ex = new BusinessLayerConflictException($msg); $this->api->expects($this->once()) ->method('getUserId') ->will($this->returnValue($this->user)); @@ -342,7 +342,7 @@ class FolderControllerTest extends ControllerTestUtility { public function testRenameReturnsErrorForInvalidCreate(){ $msg = 'except'; - $ex = new BusinessLayerExistsException($msg); + $ex = new BusinessLayerConflictException($msg); $this->folderBusinessLayer->expects($this->once()) ->method('rename') ->will($this->throwException($ex)); diff --git a/tests/unit/external/FeedAPITest.php b/tests/unit/external/FeedAPITest.php index fbc608cac..ec6aedf19 100644 --- a/tests/unit/external/FeedAPITest.php +++ b/tests/unit/external/FeedAPITest.php @@ -31,7 +31,7 @@ use \OCA\AppFramework\Http\Http; use \OCA\AppFramework\Utility\ControllerTestUtility; use \OCA\News\BusinessLayer\BusinessLayerException; -use \OCA\News\BusinessLayer\BusinessLayerExistsException; +use \OCA\News\BusinessLayer\BusinessLayerConflictException; use \OCA\News\Db\Folder; use \OCA\News\Db\Feed; use \OCA\News\Db\Item; @@ -343,7 +343,7 @@ class FeedAPITest extends ControllerTestUtility { ->with($this->equalTo($this->user), $this->equalTo(false)); $this->feedBusinessLayer->expects($this->once()) ->method('create') - ->will($this->throwException(new BusinessLayerExistsException($this->msg))); + ->will($this->throwException(new BusinessLayerConflictException($this->msg))); $response = $this->feedAPI->create(); diff --git a/tests/unit/external/FolderAPITest.php b/tests/unit/external/FolderAPITest.php index 3e5bbabb6..145cc6720 100644 --- a/tests/unit/external/FolderAPITest.php +++ b/tests/unit/external/FolderAPITest.php @@ -31,7 +31,8 @@ use \OCA\AppFramework\Utility\ControllerTestUtility; use \OCA\AppFramework\Http\Http; use \OCA\News\BusinessLayer\BusinessLayerException; -use \OCA\News\BusinessLayer\BusinessLayerExistsException; +use \OCA\News\BusinessLayer\BusinessLayerConflictException; +use \OCA\News\BusinessLayer\BusinessLayerValidationException; use \OCA\News\Db\Folder; use \OCA\News\Db\Feed; @@ -177,7 +178,7 @@ class FolderAPITest extends ControllerTestUtility { ->with($this->equalTo($this->user), $this->equalTo(false)); $this->folderBusinessLayer->expects($this->once()) ->method('create') - ->will($this->throwException(new BusinessLayerExistsException($msg))); + ->will($this->throwException(new BusinessLayerConflictException($msg))); $response = $this->folderAPI->create(); @@ -187,6 +188,27 @@ class FolderAPITest extends ControllerTestUtility { } + public function testCreateInvalidFolderName() { + $msg = 'exists'; + + $this->api->expects($this->once()) + ->method('getUserId') + ->will($this->returnValue($this->user)); + $this->folderBusinessLayer->expects($this->once()) + ->method('purgeDeleted') + ->with($this->equalTo($this->user), $this->equalTo(false)); + $this->folderBusinessLayer->expects($this->once()) + ->method('create') + ->will($this->throwException(new BusinessLayerValidationException($msg))); + + $response = $this->folderAPI->create(); + + $data = $response->getData(); + $this->assertEquals($msg, $data['message']); + $this->assertEquals(Http::STATUS_UNPROCESSABLE_ENTITY, $response->getStatus()); + } + + public function testDelete() { $folderId = 23; @@ -337,7 +359,7 @@ class FolderAPITest extends ControllerTestUtility { ->will($this->returnValue($this->user)); $this->folderBusinessLayer->expects($this->once()) ->method('rename') - ->will($this->throwException(new BusinessLayerExistsException($this->msg))); + ->will($this->throwException(new BusinessLayerConflictException($this->msg))); $response = $this->folderAPI->update(); @@ -347,6 +369,42 @@ class FolderAPITest extends ControllerTestUtility { } + public function testUpdateInvalidFolderName() { + $folderId = 23; + $folderName = ''; + + $this->folderAPI = new FolderAPI( + $this->api, + new Request( + array( + 'urlParams' => array( + 'folderId' => $folderId + ), + + 'params' => array( + 'name' => $folderName + ) + ) + ), + $this->folderBusinessLayer, + $this->itemBusinessLayer + ); + + $this->api->expects($this->once()) + ->method('getUserId') + ->will($this->returnValue($this->user)); + $this->folderBusinessLayer->expects($this->once()) + ->method('rename') + ->will($this->throwException(new BusinessLayerValidationException($this->msg))); + + $response = $this->folderAPI->update(); + + $data = $response->getData(); + $this->assertEquals($this->msg, $data['message']); + $this->assertEquals(Http::STATUS_UNPROCESSABLE_ENTITY, $response->getStatus()); + } + + public function testRead() { $request = new Request(array( 'urlParams' => array( -- cgit v1.2.3