owl-blogs/web/editor/entry_form_test.go

134 lines
3.3 KiB
Go
Raw Permalink Normal View History

2023-07-05 20:03:00 +00:00
package editor_test
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"
2023-07-05 20:03:00 +00:00
"owl-blogs/domain/model"
2023-07-08 10:03:10 +00:00
"owl-blogs/infra"
"owl-blogs/test"
2023-07-05 20:03:00 +00:00
"owl-blogs/web/editor"
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"
)
type MockEntryMetaData struct {
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
type MockEntry struct {
2023-07-08 13:56:42 +00:00
model.EntryBase
metaData MockEntryMetaData
2023-07-05 20:03:00 +00:00
}
func (e *MockEntry) Content() model.EntryContent {
2023-07-06 16:56:43 +00:00
return model.EntryContent(e.metaData.Content)
2023-07-05 20:03:00 +00:00
}
func (e *MockEntry) MetaData() interface{} {
2023-07-06 17:36:24 +00:00
return &e.metaData
2023-07-05 20:03:00 +00:00
}
2023-07-08 13:56:42 +00:00
func (e *MockEntry) SetMetaData(metaData interface{}) {
2023-07-06 17:36:24 +00:00
e.metaData = *metaData.(*MockEntryMetaData)
2023-07-05 20:03:00 +00:00
}
2023-07-09 18:36:35 +00:00
func (e *MockEntry) Title() string {
return ""
}
2023-07-05 20:03:00 +00:00
func TestFieldToFormField(t *testing.T) {
field := reflect.TypeOf(&MockEntryMetaData{}).Elem().Field(0)
formField, err := editor.FieldToFormField(field)
require.NoError(t, err)
require.Equal(t, "Image", formField.Name)
require.Equal(t, "file", formField.Params.InputType)
}
func TestStructToFields(t *testing.T) {
fields, err := editor.StructToFormFields(&MockEntryMetaData{})
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)
}
func TestEditorEntryForm_HtmlForm(t *testing.T) {
2023-07-08 10:03:10 +00:00
form := editor.NewEntryForm(&MockEntry{}, 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, "<form")
require.Contains(t, html, "method=\"POST\"")
require.Contains(t, html, "<input type=\"file\" name=\"Image\"")
require.Contains(t, html, "<input type=\"text\" name=\"Content\"")
require.Contains(t, html, "<input type=\"submit\" value=\"Submit\"")
2023-07-05 20:03:00 +00:00
}
2023-07-06 17:36:24 +00:00
func TestFormParseNil(t *testing.T) {
2023-07-08 10:03:10 +00:00
form := editor.NewEntryForm(&MockEntry{}, 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)
form := editor.NewEntryForm(&MockEntry{}, binService)
entry, err := form.Parse(NewMockFormData())
2023-07-06 17:36:24 +00:00
require.NoError(t, err)
2023-07-08 10:03:10 +00:00
require.NotZero(t, entry.MetaData().(*MockEntryMetaData).Image)
2023-07-06 17:36:24 +00:00
require.Equal(t, "Content", entry.MetaData().(*MockEntryMetaData).Content)
}