owl-blogs/cmd/owl/editor_test.go

137 lines
3.6 KiB
Go
Raw Permalink 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-16 19:48:39 +00:00
entrytypes "owl-blogs/entry_types"
2023-07-07 19:04:25 +00:00
"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
2023-07-25 19:31:03 +00:00
req := httptest.NewRequest("GET", "/editor/new/Image", 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
2023-07-25 19:31:03 +00:00
req := httptest.NewRequest("GET", "/editor/new/Image", nil)
2023-07-08 11:28:06 +00:00
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-25 19:31:03 +00:00
req := httptest.NewRequest("POST", "/editor/new/Image", body)
2023-07-07 19:04:25 +00:00
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)
2023-07-16 19:48:39 +00:00
require.Equal(t, "test content", entry.MetaData().(*entrytypes.ImageMetaData).Content)
imageId := entry.MetaData().(*entrytypes.ImageMetaData).ImageId
2023-07-08 10:03:10 +00:00
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()
2023-07-25 19:31:03 +00:00
req := httptest.NewRequest("POST", "/editor/new/Image", body)
2023-07-08 11:28:06 +00:00
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")
}