Files
RenewCMS/test/remove_legacy_article_image_alias_http_test.go

77 lines
2.4 KiB
Go

package test
import (
"net/http"
"testing"
)
func TestRemoveLegacyArticleImageAliasHttp(t *testing.T) {
newEnv := func(t *testing.T) *mediaEnv {
env := newMediaEnv(t)
env.seedArticle(1, "Backyard Renovation Update", false, "unchanged body")
return env
}
t.Run("The removed routes are not found", func(t *testing.T) {
examples := []struct {
name string
method string
path string
}{
{"POST /v1/article/1/image", http.MethodPost, "/v1/article/1/image"},
{"DELETE /v1/article/1/image/10", http.MethodDelete, "/v1/article/1/image/10"},
}
for _, example := range examples {
t.Run(example.name, func(t *testing.T) {
env := newEnv(t)
req := newMultipartFileRequest(t, example.method, example.path, "before.png", "image/png", pngBytes(t, 3, 2))
rec := env.serve(t, req, mediaVerifiedUserId)
if rec.Code != http.StatusNotFound {
t.Fatalf("expected status 404, got %d", rec.Code)
}
if env.images.createCalls != 0 {
t.Errorf("expected Create to never be called, got %d calls", env.images.createCalls)
}
if len(env.images.images) != 0 {
t.Errorf("expected no image to exist, got %d", len(env.images.images))
}
})
}
})
t.Run("Uploading still works through the media route", func(t *testing.T) {
env := newEnv(t)
req := newMultipartFileRequest(t, http.MethodPost, "/v1/media/", "before.png", "image/png", pngBytes(t, 3, 2))
rec := env.serve(t, req, mediaVerifiedUserId)
if rec.Code != http.StatusCreated {
t.Fatalf("expected status 201, got %d, body %q", rec.Code, rec.Body.String())
}
got := decodeObject(t, rec.Body.String())
if got["url"] == nil || numberOf(got["id"]) != 1 {
t.Errorf("expected a library item with a url and id 1, got %v", got)
}
listing := env.send(t, http.MethodGet, "/v1/media/", "", mediaVerifiedUserId)
items := decodeObject(t, listing.Body.String())["items"].([]any)
if len(items) != 1 {
t.Errorf("expected the list to hold the upload, got %v", items)
}
})
t.Run("The rest of the article router is unchanged", func(t *testing.T) {
env := newEnv(t)
rec := env.send(t, http.MethodPatch, "/v1/article/1/publish", "", mediaVerifiedUserId)
if rec.Code != http.StatusOK {
t.Fatalf("expected status 200, got %d, body %q", rec.Code, rec.Body.String())
}
if !env.articles.articles[1].IsOnline {
t.Errorf("expected article 1 to be online")
}
})
}