package sites import ( "context" "encoding/json" "net/http" "net/http/httptest" "os" "os/exec" "path/filepath" "strings" "testing" ) func TestCheckCaptureURL(t *testing.T) { for u, ok := range map[string]bool{ "https://espocrm.federated.computer/api/v1/LeadCapture/a1b2c3": true, "https://crm.example.com/espo/api/v1/LeadCapture/ABC123": true, "http://espocrm.example.com/api/v1/LeadCapture/abc": false, "https://espocrm.example.com/api/v1/Lead": false, "https://espocrm.example.com/api/v1/LeadCapture/": false, "https://espocrm.example.com/api/v1/LeadCapture/abc?x=1": false, "javascript:alert(1)": false, } { if err := CheckCaptureURL(u); (err == nil) != ok { t.Errorf("%s: %v", u, err) } } } func TestSetLeadsAction(t *testing.T) { const cap = "https://crm.example/api/v1/LeadCapture/k1" fed := "title = 'Site'\n\n# where forms go\n[params.leads]\n action = \"\" # empty opens an email\n email = \"info@example.com\"\n\n[params.order]\n api = \"\"\n" got := SetLeadsAction(fed, cap) want := "title = 'Site'\n\n# where forms go\n[params.leads]\n action = \"https://crm.example/api/v1/LeadCapture/k1\" # empty opens an email\n email = \"info@example.com\"\n\n[params.order]\n api = \"\"\n" if got != want { t.Errorf("existing:\n%s\nwant\n%s", got, want) } if LeadsAction(got) != cap || LeadsAction(fed) != "" { t.Errorf("read back: %q", LeadsAction(got)) } // No action line in the table: added; no table: appended. got = SetLeadsAction("[params.leads]\n email = \"x@y\"\n[other]\n action = \"keep\"\n", cap) if !strings.HasPrefix(got, "[params.leads]\n action = \""+cap+"\"\n email") || !strings.Contains(got, "[other]\n action = \"keep\"") { t.Errorf("no action line:\n%s", got) } got = SetLeadsAction("title = 'x'\n", cap) if !strings.HasSuffix(got, "[params.leads]\n action = \""+cap+"\"\n") || LeadsAction(got) != cap { t.Errorf("no table:\n%s", got) } } const formPartial = `{{/* A lead form. */}} {{ $s := . }}
` func TestPatchFormPartial(t *testing.T) { out, ok := PatchFormPartial(formPartial) if !ok { t.Fatal("not patched") } if !strings.HasPrefix(out, "{{/*\n A lead form.\n*/}}\n\n{{ $s := . }}") { t.Errorf("script tag:\n%s", out) } if !strings.Contains(out, "(window.leadPost ? window.leadPost(form, data) : fetch(form.action, { method: 'POST', body: data, headers: { Accept: 'application/json' } }))") { t.Errorf("fetch:\n%s", out) } // The form's script still follows the form's div directly. if !strings.Contains(out, "\n