owl-blogs/web/forms/form_test.go

109 lines
2.6 KiB
Go
Raw Permalink Normal View History

2023-07-25 19:31:03 +00:00
package forms_test
2023-07-05 20:03:00 +00:00
import (
2023-07-08 10:03:10 +00:00
"bytes"
"io"
2023-07-06 17:36:24 +00:00
"mime/multipart"
2023-07-08 10:03:10 +00:00
"os"
"owl-blogs/app"
"owl-blogs/infra"
"owl-blogs/test"
2023-07-25 19:31:03 +00:00
"owl-blogs/web/forms"
2023-07-08 10:03:10 +00:00
"path"
"path/filepath"
2023-07-05 20:03:00 +00:00
"reflect"
"testing"
"github.com/stretchr/testify/require"
)
2023-07-25 19:31:03 +00:00
type MockData struct {
2023-07-05 20:03:00 +00:00
Image string `owl:"inputType=file"`
Content string `owl:"inputType=text"`
}
2023-07-06 17:36:24 +00:00
type MockFormData struct {
2023-07-08 10:03:10 +00:00
fileHeader *multipart.FileHeader
}
func NewMockFormData() *MockFormData {
fileDir, _ := os.Getwd()
fileName := "../../test/fixtures/test.png"
filePath := path.Join(fileDir, fileName)
file, err := os.Open(filePath)
if err != nil {
panic(err)
}
defer file.Close()
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
part, err := writer.CreateFormFile("ImagePath", filepath.Base(file.Name()))
if err != nil {
panic(err)
}
io.Copy(part, file)
writer.Close()
multipartForm := multipart.NewReader(body, writer.Boundary())
formData, err := multipartForm.ReadForm(0)
if err != nil {
panic(err)
}
fileHeader := formData.File["ImagePath"][0]
return &MockFormData{fileHeader: fileHeader}
2023-07-06 17:36:24 +00:00
}
func (f *MockFormData) FormFile(key string) (*multipart.FileHeader, error) {
2023-07-08 10:03:10 +00:00
return f.fileHeader, nil
2023-07-06 17:36:24 +00:00
}
func (f *MockFormData) FormValue(key string, defaultValue ...string) string {
return key
}
2023-07-05 20:03:00 +00:00
func TestFieldToFormField(t *testing.T) {
2023-07-25 19:31:03 +00:00
field := reflect.TypeOf(&MockData{}).Elem().Field(0)
2023-08-08 17:41:07 +00:00
formField, err := forms.FieldToFormField(field, reflect.Value{})
2023-07-05 20:03:00 +00:00
require.NoError(t, err)
require.Equal(t, "Image", formField.Name)
require.Equal(t, "file", formField.Params.InputType)
}
func TestStructToFields(t *testing.T) {
2023-07-25 19:31:03 +00:00
fields, err := forms.StructToFormFields(&MockData{})
2023-07-05 20:03:00 +00:00
require.NoError(t, err)
require.Len(t, fields, 2)
require.Equal(t, "Image", fields[0].Name)
require.Equal(t, "file", fields[0].Params.InputType)
require.Equal(t, "Content", fields[1].Name)
require.Equal(t, "text", fields[1].Params.InputType)
}
2023-07-25 19:31:03 +00:00
func TestForm_HtmlForm(t *testing.T) {
form := forms.NewForm(&MockData{}, nil)
2023-07-06 17:36:24 +00:00
html, err := form.HtmlForm()
2023-07-05 20:03:00 +00:00
require.NoError(t, err)
2023-07-06 17:36:24 +00:00
require.Contains(t, html, "<input type=\"file\" name=\"Image\"")
require.Contains(t, html, "<input type=\"text\" name=\"Content\"")
2023-07-05 20:03:00 +00:00
}
2023-07-06 17:36:24 +00:00
func TestFormParseNil(t *testing.T) {
2023-07-25 19:31:03 +00:00
form := forms.NewForm(&MockData{}, nil)
2023-07-06 17:36:24 +00:00
_, err := form.Parse(nil)
require.Error(t, err)
}
func TestFormParse(t *testing.T) {
2023-07-08 10:03:10 +00:00
binRepo := infra.NewBinaryFileRepo(test.NewMockDb())
binService := app.NewBinaryFileService(binRepo)
2023-07-25 19:31:03 +00:00
form := forms.NewForm(&MockData{}, binService)
data, err := form.Parse(NewMockFormData())
2023-07-06 17:36:24 +00:00
require.NoError(t, err)
2023-07-25 19:31:03 +00:00
require.NotZero(t, data.(*MockData).Image)
require.Equal(t, "Content", data.(*MockData).Content)
2023-07-06 17:36:24 +00:00
}