Phase 6 part 3: website lead forms into EspoCRM
Sites → Lead forms sets up the site's forms to create EspoCRM Leads through its Lead Capture: the address saved and tested with a CORS preflight, and a pull request adding a script that maps the form's fields onto a Lead (campaign, page and form in the description). Plausible's Lead goal is imported per signal as interested. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -519,3 +519,95 @@ func TestSitesPublishing(t *testing.T) {
|
||||
t.Errorf("page: %s", body)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLeadForms(t *testing.T) {
|
||||
if hugorepo.HugoVersion(context.Background()) == "" {
|
||||
t.Skip("no hugo")
|
||||
}
|
||||
partial := "{{/*\n A lead form.\n*/}}\n<div class=\"lead\"><form class=\"lead-form\" action=\"{{ site.Params.leads.action }}\"></form></div>\n" +
|
||||
"<script>\n (function () {\n var form = document.currentScript.previousElementSibling.querySelector('form');\n" +
|
||||
" form.addEventListener('submit', function () { var data = new FormData(form);\n" +
|
||||
" fetch(form.action, { method: 'POST', body: data, headers: { Accept: 'application/json' } }); });\n })();\n</script>\n"
|
||||
clone := hugotest.Site(t, map[string]string{
|
||||
"hugo.toml": "baseURL = 'https://site.example/'\ntitle = 'Site'\ndisableKinds = ['taxonomy', 'term', 'RSS', 'sitemap']\n\n[params.leads]\n action = \"\"\n email = \"info@site.example\"\n",
|
||||
"layouts/partials/render-sections.html": `{{ range .Params.sections }}{{ partial (printf "sections/%s.html" .type) . }}{{ end }}`,
|
||||
"layouts/partials/sections/form.html": partial,
|
||||
"layouts/_default/single.html": `{{ define "main" }}<h1>{{ .Title }}</h1>{{ partial "render-sections.html" . }}{{ end }}`,
|
||||
"content/churches.md": "---\ntitle: Churches\nsections:\n - type: form\n subject: Church\n---\n",
|
||||
})
|
||||
var opened []map[string]string
|
||||
g := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
switch {
|
||||
case r.URL.Path == "/api/v1/repos/Us/site":
|
||||
fmt.Fprintf(w, `{"full_name":"Us/site","default_branch":"main","clone_url":%q,"permissions":{"push":true}}`, clone)
|
||||
case r.URL.Path == "/api/v1/repos/Us/site/pulls" && r.Method == http.MethodPost:
|
||||
var body map[string]string
|
||||
json.NewDecoder(r.Body).Decode(&body)
|
||||
opened = append(opened, body)
|
||||
fmt.Fprint(w, `{"number":9,"state":"open","html_url":"https://gitea.example/pulls/9"}`)
|
||||
case r.URL.Path == "/api/v1/repos/Us/site/pulls/9":
|
||||
fmt.Fprint(w, `{"number":9,"state":"open"}`)
|
||||
default:
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
}))
|
||||
defer g.Close()
|
||||
crm := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodOptions {
|
||||
t.Errorf("the test sent a %s", r.Method)
|
||||
}
|
||||
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||
}))
|
||||
defer crm.Close()
|
||||
captureClient = crm.Client
|
||||
defer func() { captureClient = sites.TestClient }()
|
||||
capture := crm.URL + "/api/v1/LeadCapture/key123"
|
||||
|
||||
s, h := newTestServer(t, nil)
|
||||
profile.Save(s.DB, &profile.Profile{Name: "Us", Websites: []profile.Website{{URL: "https://site.example", Repo: "Us/site"}}})
|
||||
postForm(h, "/settings/search", url.Values{"gitea_url": {g.URL}, secrets.GiteaToken: {"tok"}})
|
||||
postForm(h, "/sites/sync", url.Values{"site": {"0"}})
|
||||
|
||||
body := do(h, http.MethodGet, "/sites/forms", nil, nil).Body.String()
|
||||
if !strings.Contains(body, "The forms open an email") || !strings.Contains(body, "Pages with a form: /churches/") ||
|
||||
!strings.Contains(body, "First Name, Last Name, Email, Account Name, Phone, Description") {
|
||||
t.Errorf("before: %s", body)
|
||||
}
|
||||
if body := postForm(h, "/sites/forms/save", url.Values{"site": {"0"}, "capture": {"https://crm.example/api/v1/Lead"}}).Body.String(); !strings.Contains(body, "isn't a Lead Capture address") {
|
||||
t.Errorf("bad address: %s", body)
|
||||
}
|
||||
if body := postForm(h, "/sites/forms/save", url.Values{"site": {"0"}, "capture": {capture}}).Body.String(); !strings.Contains(body, "Saved and tested") {
|
||||
t.Errorf("save: %s", body)
|
||||
}
|
||||
if rec := postForm(h, "/sites/forms/connect", url.Values{"site": {"0"}}); rec.Code != http.StatusSeeOther {
|
||||
t.Fatalf("connect: %d %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
if len(opened) != 1 || opened[0]["head"] != "supermarket/lead-forms" || !strings.Contains(opened[0]["body"], "/churches/ loads the script and posts to the Lead Capture") {
|
||||
t.Fatalf("pull request: %v", opened)
|
||||
}
|
||||
show := func(path string) string {
|
||||
out, err := exec.Command("git", "--git-dir", strings.TrimPrefix(clone, "file://"), "show", "supermarket/lead-forms:"+path).CombinedOutput()
|
||||
if err != nil {
|
||||
t.Fatalf("%s: %s", path, out)
|
||||
}
|
||||
return string(out)
|
||||
}
|
||||
if cfg := show("hugo.toml"); !strings.Contains(cfg, " action = \""+capture+"\"\n email = \"info@site.example\"") {
|
||||
t.Errorf("hugo.toml: %s", cfg)
|
||||
}
|
||||
if p := show("layouts/partials/sections/form.html"); !strings.Contains(p, "window.leadPost(form, data)") || !strings.Contains(p, "supermarket-leads.js") {
|
||||
t.Errorf("partial: %s", p)
|
||||
}
|
||||
if js := show("static/js/supermarket-leads.js"); !strings.Contains(js, "window.leadPost = function") {
|
||||
t.Errorf("script: %s", js)
|
||||
}
|
||||
if body := do(h, http.MethodGet, "/sites/forms", nil, nil).Body.String(); !strings.Contains(body, "A pull request is open") || !strings.Contains(body, "https://gitea.example/pulls/9") {
|
||||
t.Errorf("after: %s", body)
|
||||
}
|
||||
// Once merged and read again, the page says they're connected.
|
||||
exec.Command("git", "--git-dir", strings.TrimPrefix(clone, "file://"), "update-ref", "refs/heads/main", "refs/heads/supermarket/lead-forms").Run()
|
||||
postForm(h, "/sites/sync", url.Values{"site": {"0"}})
|
||||
if body := do(h, http.MethodGet, "/sites/forms", nil, nil).Body.String(); !strings.Contains(body, "Connected: the forms on https://site.example send their answers to EspoCRM") {
|
||||
t.Errorf("connected: %s", body)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user