Skip to content

Commit

Permalink
Image System Tests, Fix & Refactor (#72)
Browse files Browse the repository at this point in the history
Implements #69 as well as some minor refactoring of functions to be
reused by system tests and the unit tests.

Bugfix: we were not previously returning the `uuid` field for the `GET
images/{id}` request as the marshalling field name differed from the
value of the response.
  • Loading branch information
matheusgomes28 authored Mar 29, 2024
1 parent 5656fc0 commit e28a20d
Show file tree
Hide file tree
Showing 9 changed files with 152 additions and 92 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion admin-app/admin_responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
111 changes: 25 additions & 86 deletions tests/admin_app_tests/endpoint_tests/images_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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?
Expand Down Expand Up @@ -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?
Expand Down Expand Up @@ -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})
}
File renamed without changes.
35 changes: 35 additions & 0 deletions tests/helpers/images.go
Original file line number Diff line number Diff line change
@@ -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})
}
86 changes: 86 additions & 0 deletions tests/system_tests/admin_app/endpoint_tests/image_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
4 changes: 2 additions & 2 deletions tests/system_tests/admin_app/endpoint_tests/post_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package admin_post_tests
package admin_endpoint_tests

import (
"bytes"
Expand All @@ -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"
)

Expand Down
2 changes: 1 addition & 1 deletion tests/system_tests/app/endpoint_tests/index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down
2 changes: 1 addition & 1 deletion tests/system_tests/app/endpoint_tests/post_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down

0 comments on commit e28a20d

Please sign in to comment.