diff --git a/CHANGELOG.md b/CHANGELOG.md index 7de1dbd..92557e1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ This project adheres to [Semantic Versioning](https://semver.org/). ## x.y.z ### Added +- REST API endpoint for deleting a list (#98) - REST API endpoint for getting list details (#89) - System tests for the test and dev environment (#81) - REST action for getting all subscribers for a list (#83) diff --git a/docs/Api/RestApi.apib b/docs/Api/RestApi.apib index 5fbfa81..d55379e 100644 --- a/docs/Api/RestApi.apib +++ b/docs/Api/RestApi.apib @@ -148,6 +148,30 @@ provided as basic auth password. (The basic auth user name can be any string.) "id": 1 } ++ Response 403 (application/json) + + + Body + + { + "code": 403, + "message": "No valid session key was provided as basic auth password." + } + ++ Response 404 (application/json) + + + Body + + { + "code": 404, + "message": "There is no list with that ID." + } + +### Delete a the list [DELETE] + ++ Request (application/json) + ++ Response 204 (application/json) + + Response 403 (application/json) + Body diff --git a/src/Controller/ListController.php b/src/Controller/ListController.php index 1fb276a..a0e2cb2 100644 --- a/src/Controller/ListController.php +++ b/src/Controller/ListController.php @@ -65,6 +65,23 @@ public function getAction(Request $request, SubscriberList $list): View return View::create()->setData($list); } + /** + * Deletes a subscriber list. + * + * @param Request $request + * @param SubscriberList $list + * + * @return View + */ + public function deleteAction(Request $request, SubscriberList $list): View + { + $this->requireAuthentication($request); + + $this->subscriberListRepository->remove($list); + + return View::create(); + } + /** * Gets a list of all subscribers (members) of a subscriber list. * diff --git a/tests/Integration/Controller/AbstractControllerTest.php b/tests/Integration/Controller/AbstractControllerTest.php index 3c5c307..654b9ed 100644 --- a/tests/Integration/Controller/AbstractControllerTest.php +++ b/tests/Integration/Controller/AbstractControllerTest.php @@ -149,6 +149,18 @@ protected function assertHttpCreated() $this->assertHttpStatusWithJsonContentType(Response::HTTP_CREATED); } + /** + * Asserts that the current client response has a HTTP NO CONTENT status. + * + * @return void + */ + protected function assertHttpNoContent() + { + $response = $this->client->getResponse(); + + static::assertSame(Response::HTTP_NO_CONTENT, $response->getStatusCode()); + } + /** * Asserts that the current client response has a HTTP BAD REQUEST status (and the application/json content type). * diff --git a/tests/Integration/Controller/ListControllerTest.php b/tests/Integration/Controller/ListControllerTest.php index c57cb4a..5862531 100644 --- a/tests/Integration/Controller/ListControllerTest.php +++ b/tests/Integration/Controller/ListControllerTest.php @@ -3,6 +3,7 @@ namespace PhpList\RestBundle\Tests\Integration\Controller; +use PhpList\PhpList4\Domain\Repository\Messaging\SubscriberListRepository; use PhpList\RestBundle\Controller\ListController; /** @@ -171,6 +172,56 @@ public function getListWithCurrentSessionKeyReturnsListData() ); } + /** + * @test + */ + public function deleteListWithoutSessionKeyForExistingListReturnsForbiddenStatus() + { + $this->getDataSet()->addTable(static::LISTS_TABLE_NAME, __DIR__ . '/Fixtures/SubscriberList.csv'); + $this->applyDatabaseChanges(); + + $this->client->request('delete', '/api/v2/lists/1'); + + $this->assertHttpForbidden(); + } + + /** + * @test + */ + public function deleteListWithCurrentSessionKeyForExistingListReturnsNoContentStatus() + { + $this->getDataSet()->addTable(static::LISTS_TABLE_NAME, __DIR__ . '/Fixtures/SubscriberList.csv'); + $this->applyDatabaseChanges(); + + $this->authenticatedJsonRequest('delete', '/api/v2/lists/1'); + + $this->assertHttpNoContent(); + } + + /** + * @test + */ + public function deleteListWithCurrentSessionKeyForInexistentListReturnsNotFoundStatus() + { + $this->authenticatedJsonRequest('delete', '/api/v2/lists/999'); + + $this->assertHttpNotFound(); + } + + /** + * @test + */ + public function deleteListWithCurrentSessionKeyDeletesList() + { + $this->getDataSet()->addTable(static::LISTS_TABLE_NAME, __DIR__ . '/Fixtures/SubscriberList.csv'); + $this->applyDatabaseChanges(); + + $this->authenticatedJsonRequest('delete', '/api/v2/lists/1'); + + $listRepository = $this->container->get(SubscriberListRepository::class); + static::assertNull($listRepository->find(1)); + } + /** * @test */