package test import ( "RenewCMS/api/pkg/apperr" "net/http" "testing" domainImage "RenewCMS/internal/domain/image" ) func TestUploadArticleImageHttp(t *testing.T) { newEnv := func(t *testing.T) *mediaEnv { env := newMediaEnv(t) env.seedArticle(1, "Backyard Renovation Update", true, "unchanged body") return env } t.Run("Uploading through the alias creates a library image", func(t *testing.T) { env := newEnv(t) req := newMultipartFileRequest(t, http.MethodPost, "/v1/article/1/image", "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 len(env.images.images) != 1 { t.Fatalf("expected exactly one library image, got %d", len(env.images.images)) } created := env.images.images[1] if numberOf(got["id"]) != created.ID { t.Errorf("expected id %d, got %v", created.ID, got["id"]) } if got["location"] != created.Path { t.Errorf("expected location %q, got %v", created.Path, got["location"]) } if created.UploadedByID != mediaVerifiedUserId { t.Errorf("expected UploadedByID 1, got %d", created.UploadedByID) } listing := env.send(t, http.MethodGet, "/v1/media/", "", mediaVerifiedUserId) items := decodeObject(t, listing.Body.String())["items"].([]any) if len(items) != 1 || items[0].(map[string]any)["uploaded_by"] != "Inès Duarte" { t.Errorf("expected the media list to hold the image uploaded by user 1, got %v", items) } if env.articles.articles[1].Body != "unchanged body" { t.Errorf("expected article 1 to be unchanged, got %+v", env.articles.articles[1]) } }) t.Run("An unknown article is 404 and creates no image", func(t *testing.T) { env := newEnv(t) req := newMultipartFileRequest(t, http.MethodPost, "/v1/article/9999/image", "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 rec.Body.String() != apperr.ArticleNotFoundErrorMessage+"\n" { t.Errorf("unexpected body: %q", rec.Body.String()) } if env.images.createCalls != 0 { t.Errorf("expected Create to never be called, got %d calls", env.images.createCalls) } }) t.Run("Rejected files use the media status codes", func(t *testing.T) { examples := []struct { name string filename string contentType string content []byte status int }{ {"notes.pdf as application/pdf", "notes.pdf", "application/pdf", []byte("%PDF"), http.StatusUnsupportedMediaType}, {"a valid PNG padded to 10485761 bytes", "huge.png", "image/png", paddedPngBytes(t, int(domainImage.MaxImageSize)+1), http.StatusRequestEntityTooLarge}, {"broken.png as image/png with text bytes", "broken.png", "image/png", []byte("not an image"), http.StatusBadRequest}, } for _, example := range examples { t.Run(example.name, func(t *testing.T) { env := newEnv(t) req := newMultipartFileRequest(t, http.MethodPost, "/v1/article/1/image", example.filename, example.contentType, example.content) rec := env.serve(t, req, mediaVerifiedUserId) if rec.Code != example.status { t.Fatalf("expected status %d, got %d", example.status, rec.Code) } if env.images.createCalls != 0 { t.Errorf("expected Create to never be called, got %d calls", env.images.createCalls) } }) } }) t.Run("The alias still requires authentication", func(t *testing.T) { env := newEnv(t) req := newMultipartFileRequest(t, http.MethodPost, "/v1/article/1/image", "before.png", "image/png", pngBytes(t, 3, 2)) rec := env.serve(t, req, 0) if rec.Code != http.StatusUnauthorized { t.Fatalf("expected status 401, got %d", rec.Code) } if env.images.createCalls != 0 { t.Errorf("expected Create to never be called, got %d calls", env.images.createCalls) } }) t.Run("The removed delete route is gone", func(t *testing.T) { env := newEnv(t) env.seedImage(10, "before.png", 10, "image/png", 3, 2, sha256Hex([]byte("x")), 1) rec := env.send(t, http.MethodDelete, "/v1/article/1/image/10", "", mediaVerifiedUserId) if rec.Code != http.StatusNotFound && rec.Code != http.StatusMethodNotAllowed { t.Fatalf("expected status 404 or 405, got %d", rec.Code) } if _, ok := env.images.images[10]; !ok { t.Errorf("expected image 10 to still exist") } if len(env.images.deleteCalls) != 0 { t.Errorf("expected Delete to never be called, got %v", env.images.deleteCalls) } }) t.Run("Uploading with a non-numeric article id answers 400", func(t *testing.T) { env := newEnv(t) req := newMultipartFileRequest(t, http.MethodPost, "/v1/article/abc/image", "before.png", "image/png", pngBytes(t, 3, 2)) rec := env.serve(t, req, mediaVerifiedUserId) if rec.Code != http.StatusBadRequest || rec.Body.String() != apperr.IdUint32ErrorMessage+"\n" { t.Fatalf("expected 400 with the id message, got %d %q", rec.Code, rec.Body.String()) } }) t.Run("Uploading without a file field answers 400", func(t *testing.T) { env := newEnv(t) rec := env.serve(t, newMultipartWithoutFileRequest(t, "/v1/article/1/image"), mediaVerifiedUserId) if rec.Code != http.StatusBadRequest { t.Fatalf("expected status 400, got %d", rec.Code) } if env.images.createCalls != 0 { t.Errorf("expected Create to never be called, got %d calls", env.images.createCalls) } }) t.Run("A repository failure while fetching the article answers 500", func(t *testing.T) { env := newEnv(t) env.articles.fail.failNext("Get") req := newMultipartFileRequest(t, http.MethodPost, "/v1/article/1/image", "before.png", "image/png", pngBytes(t, 3, 2)) rec := env.serve(t, req, mediaVerifiedUserId) if rec.Code != http.StatusInternalServerError { t.Fatalf("expected status 500, got %d", rec.Code) } }) t.Run("A repository failure while creating the image answers 500", func(t *testing.T) { env := newEnv(t) env.images.fail.failNext("Create") req := newMultipartFileRequest(t, http.MethodPost, "/v1/article/1/image", "before.png", "image/png", pngBytes(t, 3, 2)) rec := env.serve(t, req, mediaVerifiedUserId) if rec.Code != http.StatusInternalServerError { t.Fatalf("expected status 500, got %d", rec.Code) } }) }