85 lines
4.8 KiB
Gherkin
85 lines
4.8 KiB
Gherkin
Feature: Deleting an article also deletes its images
|
|
|
|
As an authenticated RenewCMS user managing the CMS
|
|
I want deleting an article to also delete every image attached to it
|
|
So that removing an article doesn't leave orphaned image rows and files behind,
|
|
and so that an id that doesn't exist still answers 404 the same way every other
|
|
article write does
|
|
|
|
Background:
|
|
Given internal/domain/article.Article carries Images []*image.Image, populated by
|
|
ArticleRepository.Get via Preload("Images")
|
|
And internal/domain/image.Repository already exposes Delete(id uint32) error, which
|
|
on the real ImageRepository also removes the uploaded file from UPLOAD_DIR
|
|
And before this slice, DeleteArticleUseCase.DeleteArticle only called
|
|
articleRepository.Delete(id) and never touched image.Repository, so an article's
|
|
images were never deleted along with it
|
|
And this slice changes NewDeleteArticleUseCase to also take an image.Repository, and
|
|
changes DeleteArticleUseCase.DeleteArticle(id) to look up the article, delete every
|
|
image in its Images slice through image.Repository.Delete, and only then delete the
|
|
article itself through article.Repository.Delete
|
|
And this slice rewires api/dependecyInjection.go so
|
|
useCases.NewDeleteArticleUseCase(repos.ArticleRepo, repos.ImageRepo) supplies both
|
|
repositories
|
|
And DELETE /v1/article/{id} keeps sitting behind the jwtauth Verifier and
|
|
Authenticator group in api/router.go, unchanged from
|
|
features/write_not_found_errors.feature
|
|
And DELETE /v1/article/{id} keeps answering 204 with an empty body on success and 404
|
|
with body apperr.ArticleNotFoundErrorMessage for an id absent from the article
|
|
repository, backed by errors.Is(err, article.ErrArticleNotFound), unchanged from
|
|
features/write_not_found_errors.feature
|
|
And no image HTTP endpoint is mounted in api/router.go as of this slice, so cascading
|
|
image deletion is only observable through the image repository's state, not through
|
|
a separate HTTP call
|
|
And the use case deletes images in the order they appear in the article's Images
|
|
slice, and aborts on the first image.Repository.Delete error, leaving the article
|
|
and any not-yet-deleted images untouched, and answering the same generic 500 the
|
|
DeleteArticle handler already falls back to on any non-not-found error
|
|
|
|
Scenario: Deleting an article with images removes the article and every one of its images
|
|
Given a valid jwt cookie for a user with id 1
|
|
And the article repository contains an article with id 1 whose Images slice holds
|
|
image id 10 and image id 11
|
|
And the image repository contains images with id 10 and id 11
|
|
When the client sends DELETE /v1/article/1 with that cookie
|
|
Then the response status is 204
|
|
And the article repository no longer contains an article with id 1
|
|
And the image repository no longer contains an image with id 10 or id 11
|
|
|
|
Scenario: Deleting an article with no images still succeeds
|
|
Given a valid jwt cookie for a user with id 1
|
|
And the article repository contains an article with id 2 whose Images slice is empty
|
|
When the client sends DELETE /v1/article/2 with that cookie
|
|
Then the response status is 204
|
|
And the article repository no longer contains an article with id 2
|
|
And no call reaches the image repository's Delete method
|
|
|
|
Scenario: Deleting an unknown article still answers 404 and deletes no images
|
|
Given a valid jwt cookie for a user with id 1
|
|
And the article repository has no article with id 9999
|
|
When the client sends DELETE /v1/article/9999 with that cookie
|
|
Then the response status is 404
|
|
And the response body is the text "Article not found"
|
|
And no call reaches the image repository's Delete method
|
|
|
|
Scenario: Deleting an article still requires authentication
|
|
Given no jwt cookie is sent
|
|
And the article repository contains an article with id 1 whose Images slice holds
|
|
image id 10
|
|
When the client sends DELETE /v1/article/1 without a cookie
|
|
Then the response status is 401
|
|
And the article repository still contains the article with id 1
|
|
And the image repository still contains the image with id 10
|
|
|
|
Scenario: A failure deleting one of an article's images aborts the whole delete
|
|
Given a valid jwt cookie for a user with id 1
|
|
And the article repository contains an article with id 3 whose Images slice holds
|
|
image id 20 followed by image id 21
|
|
And the image repository contains images with id 20 and id 21
|
|
And the image repository's Delete fails for image id 20
|
|
When the client sends DELETE /v1/article/3 with that cookie
|
|
Then the response status is 500
|
|
And the response body is the text "Failed to delete article"
|
|
And the article repository still contains the article with id 3
|
|
And the image repository still contains the image with id 21
|