owl-blogs/cmd/owl/editor_test.go

137 lines
3.6 KiB
Go
Raw Normal View History

2023-07-06 20:16:52 +00:00
package main
import (
"bytes"
"io"
2023-07-08 10:03:10 +00:00
"io/ioutil"
2023-07-06 20:16:52 +00:00
"mime/multipart"
2023-07-08 11:28:06 +00:00
"net/http"
2023-07-06 20:16:52 +00:00
"net/http/httptest"
"os"
2023-07-08 11:28:06 +00:00
"owl-blogs/app"
2023-07-07 19:04:25 +00:00
"owl-blogs/domain/model"
"owl-blogs/infra"
2023-07-08 11:28:06 +00:00
"owl-blogs/test"
2023-07-06 20:16:52 +00:00
"path"
"path/filepath"
2023-07-07 19:04:25 +00:00
"strings"
2023-07-06 20:16:52 +00:00
"testing"
"github.com/stretchr/testify/require"
)
2023-07-08 11:28:06 +00:00
func getUserToken(service *app.AuthorService) string {
_, err := service.Create("test", "test")
if err != nil {
panic(err)
2023-07-07 19:04:25 +00:00
}
2023-07-08 11:28:06 +00:00
token, err := service.CreateToken("test")
if err != nil {
panic(err)
}
return token
2023-07-07 19:04:25 +00:00
}
2023-07-06 20:16:52 +00:00
func TestEditorFormGet(t *testing.T) {
2023-07-08 11:28:06 +00:00
db := test.NewMockDb()
owlApp := App(db)
app := owlApp.FiberApp
token := getUserToken(owlApp.AuthorService)
2023-07-06 20:16:52 +00:00
req := httptest.NewRequest("GET", "/editor/ImageEntry", nil)
2023-07-08 11:28:06 +00:00
req.AddCookie(&http.Cookie{Name: "token", Value: token})
2023-07-06 20:16:52 +00:00
resp, err := app.Test(req)
require.NoError(t, err)
require.Equal(t, 200, resp.StatusCode)
}
2023-07-08 11:28:06 +00:00
func TestEditorFormGetNoAuth(t *testing.T) {
db := test.NewMockDb()
owlApp := App(db)
app := owlApp.FiberApp
req := httptest.NewRequest("GET", "/editor/ImageEntry", nil)
req.AddCookie(&http.Cookie{Name: "token", Value: "invalid"})
resp, err := app.Test(req)
require.NoError(t, err)
require.Equal(t, 302, resp.StatusCode)
}
2023-07-06 20:16:52 +00:00
func TestEditorFormPost(t *testing.T) {
2023-07-08 11:28:06 +00:00
db := test.NewMockDb()
2023-07-07 19:04:25 +00:00
owlApp := App(db)
app := owlApp.FiberApp
2023-07-08 11:28:06 +00:00
token := getUserToken(owlApp.AuthorService)
2023-07-07 19:04:25 +00:00
repo := infra.NewEntryRepository(db, owlApp.Registry)
2023-07-08 10:03:10 +00:00
binRepo := infra.NewBinaryFileRepo(db)
2023-07-06 20:16:52 +00:00
fileDir, _ := os.Getwd()
fileName := "../../test/fixtures/test.png"
filePath := path.Join(fileDir, fileName)
file, err := os.Open(filePath)
require.NoError(t, err)
2023-07-08 10:03:10 +00:00
fileBytes, err := ioutil.ReadFile(filePath)
require.NoError(t, err)
2023-07-06 20:16:52 +00:00
defer file.Close()
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
2023-07-08 10:03:10 +00:00
part, _ := writer.CreateFormFile("ImageId", filepath.Base(file.Name()))
2023-07-06 20:16:52 +00:00
io.Copy(part, file)
part, _ = writer.CreateFormField("Content")
io.WriteString(part, "test content")
writer.Close()
2023-07-07 19:04:25 +00:00
req := httptest.NewRequest("POST", "/editor/ImageEntry", body)
req.Header.Set("Content-Type", writer.FormDataContentType())
2023-07-08 11:28:06 +00:00
req.AddCookie(&http.Cookie{Name: "token", Value: token})
2023-07-06 20:16:52 +00:00
resp, err := app.Test(req)
require.NoError(t, err)
2023-07-07 19:04:25 +00:00
require.Equal(t, 302, resp.StatusCode)
require.Contains(t, resp.Header.Get("Location"), "/posts/")
id := strings.Split(resp.Header.Get("Location"), "/")[2]
entry, err := repo.FindById(id)
require.NoError(t, err)
require.Equal(t, "test content", entry.MetaData().(*model.ImageEntryMetaData).Content)
2023-07-08 10:03:10 +00:00
imageId := entry.MetaData().(*model.ImageEntryMetaData).ImageId
require.NotZero(t, imageId)
bin, err := binRepo.FindById(imageId)
require.NoError(t, err)
require.Equal(t, bin.Name, "test.png")
require.Equal(t, fileBytes, bin.Data)
2023-07-07 19:04:25 +00:00
2023-07-06 20:16:52 +00:00
}
2023-07-08 11:28:06 +00:00
func TestEditorFormPostNoAuth(t *testing.T) {
db := test.NewMockDb()
owlApp := App(db)
app := owlApp.FiberApp
fileDir, _ := os.Getwd()
fileName := "../../test/fixtures/test.png"
filePath := path.Join(fileDir, fileName)
file, err := os.Open(filePath)
require.NoError(t, err)
defer file.Close()
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
part, _ := writer.CreateFormFile("ImageId", filepath.Base(file.Name()))
io.Copy(part, file)
part, _ = writer.CreateFormField("Content")
io.WriteString(part, "test content")
writer.Close()
req := httptest.NewRequest("POST", "/editor/ImageEntry", body)
req.Header.Set("Content-Type", writer.FormDataContentType())
req.AddCookie(&http.Cookie{Name: "token", Value: "invalid"})
resp, err := app.Test(req)
require.NoError(t, err)
require.Equal(t, 302, resp.StatusCode)
require.Contains(t, resp.Header.Get("Location"), "/auth/login")
}