85 lines
2.6 KiB
Go
85 lines
2.6 KiB
Go
package test
|
|
|
|
import (
|
|
"RenewCMS/api/pkg/apperr"
|
|
"net/http"
|
|
"reflect"
|
|
"testing"
|
|
)
|
|
|
|
func TestDeleteArticleKeepsImages(t *testing.T) {
|
|
newEnv := func(t *testing.T) *mediaEnv {
|
|
env := newMediaEnv(t)
|
|
env.seedImage(10, "ten.png", 1, "image/png", 1, 1, hashA, 1)
|
|
env.seedImage(11, "eleven.png", 1, "image/png", 1, 1, hashB, 1)
|
|
env.seedArticle(1, "Kitchen Renovation", true, env.urlOf(10)+" "+env.urlOf(11))
|
|
return env
|
|
}
|
|
|
|
t.Run("Deleting an article leaves every image it used", func(t *testing.T) {
|
|
env := newEnv(t)
|
|
|
|
rec := env.send(t, http.MethodDelete, "/v1/article/1", "", mediaVerifiedUserId)
|
|
|
|
if rec.Code != http.StatusNoContent {
|
|
t.Fatalf("expected status 204, got %d", rec.Code)
|
|
}
|
|
if _, ok := env.articles.articles[1]; ok {
|
|
t.Errorf("expected article 1 to be gone")
|
|
}
|
|
expectIds(t, env.imageIds(), []uint32{10, 11})
|
|
if len(env.images.deleteCalls) != 0 {
|
|
t.Errorf("expected image Delete to never be called, got %v", env.images.deleteCalls)
|
|
}
|
|
})
|
|
|
|
t.Run("Deleting an unknown article is 404 and touches no image", func(t *testing.T) {
|
|
env := newEnv(t)
|
|
|
|
rec := env.send(t, http.MethodDelete, "/v1/article/9999", "", mediaVerifiedUserId)
|
|
|
|
if rec.Code != http.StatusNotFound || rec.Body.String() != apperr.ArticleNotFoundErrorMessage+"\n" {
|
|
t.Errorf("expected 404 Article not found, got %d %q", rec.Code, rec.Body.String())
|
|
}
|
|
expectIds(t, env.imageIds(), []uint32{10, 11})
|
|
if len(env.images.deleteCalls) != 0 {
|
|
t.Errorf("expected the image repository to be untouched")
|
|
}
|
|
})
|
|
|
|
t.Run("Deleting still requires authentication", func(t *testing.T) {
|
|
env := newEnv(t)
|
|
|
|
rec := env.send(t, http.MethodDelete, "/v1/article/1", "", 0)
|
|
|
|
if rec.Code != http.StatusUnauthorized {
|
|
t.Fatalf("expected status 401, got %d", rec.Code)
|
|
}
|
|
if _, ok := env.articles.articles[1]; !ok {
|
|
t.Errorf("expected article 1 to still exist")
|
|
}
|
|
if _, ok := env.images.images[10]; !ok {
|
|
t.Errorf("expected image 10 to still exist")
|
|
}
|
|
})
|
|
|
|
t.Run("An article repository failure is 500 and everything stays", func(t *testing.T) {
|
|
env := newEnv(t)
|
|
bodiesBefore := env.bodies()
|
|
env.articles.fail.failNext("Delete")
|
|
|
|
rec := env.send(t, http.MethodDelete, "/v1/article/1", "", mediaVerifiedUserId)
|
|
|
|
if rec.Code != http.StatusInternalServerError || rec.Body.String() != "Failed to delete article\n" {
|
|
t.Errorf("expected 500 Failed to delete article, got %d %q", rec.Code, rec.Body.String())
|
|
}
|
|
if !reflect.DeepEqual(env.bodies(), bodiesBefore) {
|
|
t.Errorf("expected the article to stay")
|
|
}
|
|
expectIds(t, env.imageIds(), []uint32{10, 11})
|
|
if len(env.images.deleteCalls) != 0 {
|
|
t.Errorf("expected the image repository to be untouched")
|
|
}
|
|
})
|
|
}
|