package httpx
import (
"net/http"
"net/http/httptest"
"net/url"
"path/filepath"
"regexp"
"strings"
"testing"
"github.com/marqdo/marqdo/plugins/web/internal/app"
"github.com/marqdo/marqdo/plugins/web/internal/artifact"
"github.com/marqdo/marqdo/plugins/web/internal/db"
"github.com/marqdo/marqdo/plugins/web/internal/form"
"github.com/marqdo/marqdo/plugins/web/internal/session"
)
func resourceNode(resource map[string]any) map[string]any {
return map[string]any{"type": "resource", "value": resource, "slot": "content"}
}
func TestViewResourcesObserveRequestsFormsAndNavigation(t *testing.T) {
uri := "sqlite:" + filepath.Join(t.TempDir(), "view.db")
fields := []any{
map[string]any{"name": "id", "type": "integer", "nullable": false},
map[string]any{"name": "title", "type": "text", "nullable": false},
map[string]any{"name": "slug", "type": "text", "nullable": true},
map[string]any{"name": "summary", "type": "text", "nullable": true},
map[string]any{"name": "body", "type": "text", "nullable": true},
}
if _, err := db.Init(uri, "articles", fields); err != nil {
t.Fatal(err)
}
if _, err := db.Insert(uri, "articles", map[string]any{
"title": "Original
", "slug": "first", "summary": "Short summary", "body": "Full article body",
}); err != nil {
t.Fatal(err)
}
query := map[string]any{"_type": "data_query", "source": "articles", "limit": float64(50), "link_prefix": "/post/"}
navigation := map[string]any{"_type": "view_navigation", "region": "nav", "items": []any{
map[string]any{"label": "Guest login", "href": "/admin/login", "when": "guest"},
map[string]any{"label": "Private desk", "href": "/admin", "when": "auth"},
}}
page := map[string]any{"title": "View resources", "artifact_nodes": []any{
resourceNode(navigation), resourceNode(query),
resourceNode(map[string]any{"_type": "form_ref", "id": "article"}),
}}
bag := app.New(map[string]any{"page": page, "db": map[string]any{"url": uri}})
var err error
bag, err = app.Auth(bag, []any{map[string]any{"username": "admin", "password": "secret", "role": "admin"}}, nil)
if err != nil {
t.Fatal(err)
}
selected := form.SetFields(form.New("articles", "insert", ""), fields[1:2])
selected = form.SetRules(selected, []any{map[string]any{"field": "title", "rule": "required", "message": "Title required"}})
selected = form.SetLabels(selected, "Publish", "Cancel", "/")
bag, err = app.MountForm(bag, "article", selected)
if err != nil {
t.Fatal(err)
}
detailed := map[string]any{"_type": "data_query", "source": "articles", "detail": true,
"where": map[string]any{"slug": "{slug}"}}
bag, err = app.Route(bag, "/post/{slug}", map[string]any{"title": "Article", "artifact_nodes": []any{resourceNode(detailed)}})
if err != nil {
t.Fatal(err)
}
handler, err := NewHandler(bag, "")
if err != nil {
t.Fatal(err)
}
call := func(method, path, body, cookie string) *httptest.ResponseRecorder {
request := httptest.NewRequest(method, path, strings.NewReader(body))
if body != "" {
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
}
if cookie != "" {
request.Header.Set("Cookie", cookie)
}
recorder := httptest.NewRecorder()
handler.ServeHTTP(recorder, request)
return recorder
}
initial := call("GET", "/", "", "")
if initial.Code != http.StatusOK {
t.Fatalf("initial: %d %s", initial.Code, initial.Body.String())
}
text := initial.Body.String()
if !strings.Contains(text, "Guest login") || strings.Contains(text, "Private desk") ||
!strings.Contains(text, "Short summary") || strings.Contains(text, "Full article body") ||
!strings.Contains(text, "Original <title>") {
t.Fatal(text)
}
cookies := initial.Result().Cookies()
if len(cookies) == 0 {
t.Fatal("rendered form has no actual session cookie")
}
cookie := cookies[0].Name + "=" + cookies[0].Value
match := regexp.MustCompile(`name="_csrf" value="([^"]+)"`).FindStringSubmatch(text)
if len(match) != 2 {
t.Fatal("resource form omitted its CSRF token")
}
token := match[1]
bad := call("POST", "/_form/article", "title=Rejected&_csrf=wrong", cookie)
if bad.Code != http.StatusForbidden {
t.Fatalf("bad csrf accepted: %d %s", bad.Code, bad.Body.String())
}
invalid := call("POST", "/_form/article", url.Values{"_csrf": {token}, "title": {""}}.Encode(), cookie)
if invalid.Code != http.StatusOK || !strings.Contains(invalid.Body.String(), "Title required") ||
!strings.Contains(invalid.Body.String(), `name="_csrf" value="`+token+`"`) {
t.Fatalf("validation retry: %d %s", invalid.Code, invalid.Body.String())
}
valid := call("POST", "/_form/article", url.Values{"_csrf": {token}, "title": {"Inserted after startup"}, "_mq_return": {"/new"}}.Encode(), cookie)
if valid.Code != http.StatusSeeOther || valid.Header().Get("Location") != "/" {
t.Fatalf("submit: %d %s", valid.Code, valid.Body.String())
}
current := call("GET", "/", "", cookie)
if !strings.Contains(current.Body.String(), "Inserted after startup") {
t.Fatal("query cached startup rows")
}
fragment := call("GET", "/_part/main", "", cookie)
if fragment.Code != http.StatusOK || !strings.Contains(fragment.Body.String(), "Inserted after startup") ||
!strings.Contains(fragment.Body.String(), `name="_csrf" value="`+token+`"`) ||
strings.Contains(fragment.Body.String(), "")
if !strings.Contains(head, `