diff --git a/Makefile b/Makefile index 5a6852a..92754d5 100644 --- a/Makefile +++ b/Makefile @@ -12,7 +12,7 @@ ADMIN_BINARY_NAME=urchin-admin all: build test prepare_env: - cp -r migrations tests/system_tests/helpers/ + cp -r migrations tests/helpers/ build: prepare_env $(TEMPL) generate diff --git a/admin-app/admin_responses.go b/admin-app/admin_responses.go index dbcaeea..c593f30 100644 --- a/admin-app/admin_responses.go +++ b/admin-app/admin_responses.go @@ -16,7 +16,7 @@ type ImageIdResponse struct { } type GetImageResponse struct { - Id string `json:"id"` + Id string `json:"uuid"` Name string `json:"name"` AltText string `json:"alt_text"` Extension string `json:"extension"` diff --git a/tests/admin_app_tests/endpoint_tests/images_test.go b/tests/admin_app_tests/endpoint_tests/images_test.go index 53cc97a..c1293c6 100644 --- a/tests/admin_app_tests/endpoint_tests/images_test.go +++ b/tests/admin_app_tests/endpoint_tests/images_test.go @@ -5,18 +5,17 @@ import ( "encoding/json" "errors" "fmt" - "image" "image/png" "io" "mime/multipart" "net/http" "net/http/httptest" - "net/textproto" "testing" "github.com/fossoreslp/go-uuid-v4" admin_app "github.com/matheusgomes28/urchin/admin-app" "github.com/matheusgomes28/urchin/common" + "github.com/matheusgomes28/urchin/tests/helpers" "github.com/matheusgomes28/urchin/tests/mocks" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -42,20 +41,11 @@ func TestPostImage(t *testing.T) { go func() { defer writer.Close() - part, err := createTestForm(writer, "file", "test.png", "image/png") - if err != nil { - t.Error(err) - } + part, err := helpers.CreateFormImagePart(writer, "file", "test.png", "image/png") + require.Nil(t, err) - img := createImage() - if err != nil { - t.Error(err) - } - - err = png.Encode(part, img) - if err != nil { - t.Error(err) - } + err = png.Encode(part, helpers.CreateImage()) + require.Nil(t, err) }() req, _ := http.NewRequest("POST", "/images", pr) @@ -87,16 +77,12 @@ func TestPostImageNotAnImageFile(t *testing.T) { go func() { defer writer.Close() - part, err := createTestForm(writer, "file", "test.png", "image/png") - if err != nil { - t.Error(err) - } + part, err := helpers.CreateFormImagePart(writer, "file", "test.png", "image/png") + require.Nil(t, err) text := bytes.NewBufferString("This is some dumy text to check the content test") _, err = io.Copy(part, text) - if err != nil { - t.Error(err) - } + require.Nil(t, err) }() req, _ := http.NewRequest("POST", "/images", pr) @@ -122,21 +108,14 @@ func TestPostImageWrongFileContentType(t *testing.T) { go func() { defer writer.Close() - part, err := createTestForm(writer, "file", "test.png", "application/json") - if err != nil { - t.Error(err) - } - - img := createImage() + part, err := helpers.CreateFormImagePart(writer, "file", "test.png", "application/json") + require.Nil(t, err) + require.Nil(t, err) - if err != nil { - t.Error(err) - } + img := helpers.CreateImage() err = png.Encode(part, img) - if err != nil { - t.Error(err) - } + require.Nil(t, err) }() req, _ := http.NewRequest("POST", "/images", pr) @@ -162,20 +141,13 @@ func TestPostImageFailedToCreateDatabaseEntry(t *testing.T) { go func() { defer writer.Close() - part, err := createTestForm(writer, "file", "test.png", "image/png") - if err != nil { - t.Error(err) - } + part, err := helpers.CreateFormImagePart(writer, "file", "test.png", "image/png") + require.Nil(t, err) - img := createImage() - if err != nil { - t.Error(err) - } + img := helpers.CreateImage() err = png.Encode(part, img) - if err != nil { - t.Error(err) - } + require.Nil(t, err) }() req, _ := http.NewRequest("POST", "/images", pr) @@ -210,20 +182,13 @@ func TestGetImage(t *testing.T) { go func() { defer writer.Close() - part, err := createTestForm(writer, "file", "test.png", "image/png") - if err != nil { - t.Error(err) - } + part, err := helpers.CreateFormImagePart(writer, "file", "test.png", "image/png") + require.Nil(t, err) - img := createImage() - if err != nil { - t.Error(err) - } + img := helpers.CreateImage() err = png.Encode(part, img) - if err != nil { - t.Error(err) - } + require.Nil(t, err) }() // TODO: We have to create the image first. Maybe there's a better way to do this? @@ -315,20 +280,13 @@ func TestDeleteImage(t *testing.T) { go func() { defer writer.Close() - part, err := createTestForm(writer, "file", "test.png", "image/png") - if err != nil { - t.Error(err) - } + part, err := helpers.CreateFormImagePart(writer, "file", "test.png", "image/png") + require.Nil(t, err) - img := createImage() - if err != nil { - t.Error(err) - } + img := helpers.CreateImage() err = png.Encode(part, img) - if err != nil { - t.Error(err) - } + require.Nil(t, err) }() // TODO: We have to create the image first. Maybe there's a better way to do this? @@ -402,22 +360,3 @@ func TestDeleteImageNoImageFile(t *testing.T) { assert.Equal(t, 200, delete_recorder.Code) } - -func createTestForm(writer *multipart.Writer, fieldname string, filename string, contentType string) (io.Writer, error) { - h := make(textproto.MIMEHeader) - h.Set("Content-Disposition", - fmt.Sprintf(`form-data; name="%s"; filename="%s"`, fieldname, filename)) - h.Set("Content-Type", contentType) - return writer.CreatePart(h) -} - -// Creating an image in memory for testing: https://yourbasic.org/golang/create-image/ -func createImage() image.Image { - width := 1 - height := 1 - - upLeft := image.Point{0, 0} - lowRight := image.Point{width, height} - - return image.NewRGBA(image.Rectangle{upLeft, lowRight}) -} diff --git a/tests/system_tests/helpers/helpers.go b/tests/helpers/helpers.go similarity index 100% rename from tests/system_tests/helpers/helpers.go rename to tests/helpers/helpers.go diff --git a/tests/helpers/images.go b/tests/helpers/images.go new file mode 100644 index 0000000..5ec7054 --- /dev/null +++ b/tests/helpers/images.go @@ -0,0 +1,35 @@ +package helpers + +import ( + "fmt" + "image" + "io" + "mime/multipart" + "net/textproto" +) + +func CreateFormImagePart(writer *multipart.Writer, fieldname string, filename string, contentType string) (io.Writer, error) { + h := make(textproto.MIMEHeader) + h.Set("Content-Disposition", + fmt.Sprintf(`form-data; name="%s"; filename="%s"`, fieldname, filename)) + h.Set("Content-Type", contentType) + return writer.CreatePart(h) +} + +func CreateTextFormHeader(writer *multipart.Writer, fieldname string) (io.Writer, error) { + h := make(textproto.MIMEHeader) + h.Set("Content-Disposition", + fmt.Sprintf(`form-data; name="%s"`, fieldname)) + return writer.CreatePart(h) +} + +// Creating an image in memory for testing: https://yourbasic.org/golang/create-image/ +func CreateImage() image.Image { + width := 1 + height := 1 + + upLeft := image.Point{0, 0} + lowRight := image.Point{width, height} + + return image.NewRGBA(image.Rectangle{upLeft, lowRight}) +} diff --git a/tests/system_tests/admin_app/endpoint_tests/image_test.go b/tests/system_tests/admin_app/endpoint_tests/image_test.go new file mode 100644 index 0000000..c7636d6 --- /dev/null +++ b/tests/system_tests/admin_app/endpoint_tests/image_test.go @@ -0,0 +1,86 @@ +package admin_endpoint_tests + +import ( + _ "database/sql" + "encoding/json" + "fmt" + "image/png" + "io" + "mime/multipart" + "net/http" + "net/http/httptest" + "testing" + + _ "github.com/go-sql-driver/mysql" + "github.com/stretchr/testify/require" + + admin_app "github.com/matheusgomes28/urchin/admin-app" + "github.com/matheusgomes28/urchin/tests/helpers" + "github.com/pressly/goose/v3" +) + +func TestImageUpload(t *testing.T) { + + // This is gonna be the in-memory mysql + app_settings := helpers.GetAppSettings(30) + go helpers.RunDatabaseServer(app_settings) + database, err := helpers.WaitForDb(app_settings) + require.Nil(t, err) + goose.SetBaseFS(helpers.EmbedMigrations) + + err = goose.SetDialect("mysql") + require.Nil(t, err) + + err = goose.Up(database.Connection, "migrations") + require.Nil(t, err) + + // Multipart image form creation + pr, pw := io.Pipe() + writer := multipart.NewWriter(pw) + + go func() { + defer writer.Close() + + // Create the image part + image_part, err := helpers.CreateFormImagePart(writer, "file", "test.png", "image/png") + require.Nil(t, err) + err = png.Encode(image_part, helpers.CreateImage()) + require.Nil(t, err) + + // Create the alt part + text_part, err := helpers.CreateTextFormHeader(writer, "alt") + require.Nil(t, err) + _, err = text_part.Write([]byte("test alt")) + require.Nil(t, err) + }() + + // Execute multiform request + post_recorder := httptest.NewRecorder() + r := admin_app.SetupRoutes(app_settings, database) + require.Nil(t, err) + + req, _ := http.NewRequest("POST", "/images", pr) + req.Header.Add("Content-Type", writer.FormDataContentType()) + r.ServeHTTP(post_recorder, req) + + require.Equal(t, http.StatusOK, post_recorder.Code) + + // Make sure returned an ID + var image_id_response admin_app.ImageIdResponse + err = json.Unmarshal(post_recorder.Body.Bytes(), &image_id_response) + require.Nil(t, err) + + // Make sure that we can request the image details from the DB + get_recorder := httptest.NewRecorder() + req, _ = http.NewRequest("GET", fmt.Sprintf("/images/%s", image_id_response.Id), nil) + r.ServeHTTP(get_recorder, req) + + var image_response admin_app.GetImageResponse + err = json.Unmarshal(get_recorder.Body.Bytes(), &image_response) + require.Nil(t, err) + + require.Equal(t, image_id_response.Id, image_response.Id) + require.Equal(t, image_response.AltText, "test alt") + require.Equal(t, image_response.Extension, ".png") + require.Equal(t, image_response.Name, "test.png") +} diff --git a/tests/system_tests/admin_app/endpoint_tests/post_test.go b/tests/system_tests/admin_app/endpoint_tests/post_test.go index 4bc3b0f..b5ae28e 100644 --- a/tests/system_tests/admin_app/endpoint_tests/post_test.go +++ b/tests/system_tests/admin_app/endpoint_tests/post_test.go @@ -1,4 +1,4 @@ -package admin_post_tests +package admin_endpoint_tests import ( "bytes" @@ -13,7 +13,7 @@ import ( "github.com/stretchr/testify/require" admin_app "github.com/matheusgomes28/urchin/admin-app" - "github.com/matheusgomes28/urchin/tests/system_tests/helpers" + "github.com/matheusgomes28/urchin/tests/helpers" "github.com/pressly/goose/v3" ) diff --git a/tests/system_tests/app/endpoint_tests/index_test.go b/tests/system_tests/app/endpoint_tests/index_test.go index 73150e2..6210540 100644 --- a/tests/system_tests/app/endpoint_tests/index_test.go +++ b/tests/system_tests/app/endpoint_tests/index_test.go @@ -10,7 +10,7 @@ import ( "github.com/stretchr/testify/require" "github.com/matheusgomes28/urchin/app" - "github.com/matheusgomes28/urchin/tests/system_tests/helpers" + "github.com/matheusgomes28/urchin/tests/helpers" "github.com/pressly/goose/v3" ) diff --git a/tests/system_tests/app/endpoint_tests/post_test.go b/tests/system_tests/app/endpoint_tests/post_test.go index bb08ba9..44b9c4f 100644 --- a/tests/system_tests/app/endpoint_tests/post_test.go +++ b/tests/system_tests/app/endpoint_tests/post_test.go @@ -10,7 +10,7 @@ import ( "github.com/stretchr/testify/require" "github.com/matheusgomes28/urchin/app" - "github.com/matheusgomes28/urchin/tests/system_tests/helpers" + "github.com/matheusgomes28/urchin/tests/helpers" "github.com/pressly/goose/v3" )