-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Image System Tests, Fix & Refactor (#72)
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
1 parent
5656fc0
commit e28a20d
Showing
9 changed files
with
152 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters