79 lines
2.3 KiB
Go
79 lines
2.3 KiB
Go
package httpapi
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/base64"
|
|
"mime/multipart"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/descrybe/descrybe-v2/apps/api/internal/catalog"
|
|
)
|
|
|
|
func TestParseSyncA1JSON_Base64Upload(t *testing.T) {
|
|
t.Parallel()
|
|
sql := "INSERT INTO `wp_product_categories` (`Name`, `Prompt`) VALUES\n('A','GPT predloga:\\n<name>{x}</name><metaDescription>{y}</metaDescription>');\n"
|
|
b64 := base64.StdEncoding.EncodeToString([]byte(sql))
|
|
body := `{"confirm":true,"wp_product_categories_sql_b64":"` + b64 + `"}`
|
|
req := httptest.NewRequest(http.MethodPost, "/sync", strings.NewReader(body))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
parsed, err := parseSyncA1JSON(req)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !parsed.Confirm {
|
|
t.Fatal("confirm")
|
|
}
|
|
if string(parsed.WPCategoriesSQL) != sql {
|
|
t.Fatalf("sql mismatch len=%d", len(parsed.WPCategoriesSQL))
|
|
}
|
|
}
|
|
|
|
func TestParseSyncA1JSON_RejectsOversizedBase64(t *testing.T) {
|
|
t.Parallel()
|
|
raw := make([]byte, catalog.MaxWPCategorySQLBytes+1)
|
|
b64 := base64.StdEncoding.EncodeToString(raw)
|
|
body := `{"confirm":true,"wp_product_categories_sql_b64":"` + b64 + `"}`
|
|
req := httptest.NewRequest(http.MethodPost, "/sync", strings.NewReader(body))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
_, err := parseSyncA1JSON(req)
|
|
if err == nil {
|
|
t.Fatal("expected too-large error")
|
|
}
|
|
}
|
|
|
|
func TestParseSyncA1Multipart_FileUpload(t *testing.T) {
|
|
t.Parallel()
|
|
sql := "INSERT INTO `wp_product_categories` (`Name`, `Prompt`) VALUES\n('B','GPT predloga:\\n<name>{x}</name><metaDescription>{y}</metaDescription>');\n"
|
|
var buf bytes.Buffer
|
|
w := multipart.NewWriter(&buf)
|
|
if err := w.WriteField("confirm", "true"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
part, err := w.CreateFormFile("wp_product_categories", "wp_product_categories.sql")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := part.Write([]byte(sql)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
ct := w.FormDataContentType()
|
|
if err := w.Close(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
req := httptest.NewRequest(http.MethodPost, "/sync", bytes.NewReader(buf.Bytes()))
|
|
req.Header.Set("Content-Type", ct)
|
|
parsed, err := parseSyncA1Multipart(req)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !parsed.Confirm {
|
|
t.Fatal("confirm")
|
|
}
|
|
if string(parsed.WPCategoriesSQL) != sql {
|
|
t.Fatalf("sql mismatch len=%d", len(parsed.WPCategoriesSQL))
|
|
}
|
|
}
|