package fichastest import ( "html" "net/http" "net/url" "regexp" "strings" "testing" ) func TestAddingAFieldPutsItEverywhere(t *testing.T) { h := newApp(t) createACustomer(t, h, "Cnoc Teoranta") rec := post(t, h, "/t/t_customer/fields", url.Values{"label": {"Contract value"}, "kind": {"number"}, "tier": {"sealed"}}) if rec.Code != http.StatusSeeOther { t.Fatalf("adding a field: %d\n%s", rec.Code, rec.Body.String()) } if !strings.Contains(get(t, h, "/t/t_customer/fields").Body.String(), "Contract value") { t.Error("the field is not on the fields page") } if !strings.Contains(get(t, h, "/").Body.String(), ">Contract value<") { t.Error("the field is not a column in the table") } if !strings.Contains(get(t, h, "/cards/new?type=t_customer").Body.String(), "Contract value") { t.Error("the field is not on the new-customer form") } } // A refusal comes back to the same form, with the message and what was typed. func TestARefusedFieldKeepsTheForm(t *testing.T) { h := newApp(t) rec := post(t, h, "/t/t_customer/fields", url.Values{ "label": {"Region"}, "kind": {"enum"}, "tier": {"print"}, "options": {"North\nSouth"}}) if rec.Code != http.StatusUnprocessableEntity { t.Fatalf("a findable choice: status %d, want 422", rec.Code) } body := rec.Body.String() if !strings.Contains(body, "give the values away") { t.Error("the refusal does not say why") } if !strings.Contains(body, `value="Region"`) || !strings.Contains(body, "North") { t.Error("the form lost what was typed") } } func TestANewTypeIsSomewhereToGo(t *testing.T) { h := newApp(t) rec := post(t, h, "/types", url.Values{"label": {"Supplier"}}) if rec.Code != http.StatusSeeOther { t.Fatalf("creating a type: %d\n%s", rec.Code, rec.Body.String()) } fields := rec.Header().Get("Location") m := regexp.MustCompile(`^/t/(t_[0-9a-f]+)/fields$`).FindStringSubmatch(fields) if m == nil { t.Fatalf("created a type and went to %q", fields) } key := m[1] if !strings.Contains(get(t, h, "/").Body.String(), "Suppliers") { t.Error("the new type is not in the sidebar") } list := get(t, h, "/t/"+key) if list.Code != http.StatusOK || !strings.Contains(list.Body.String(), "No suppliers yet") { t.Fatalf("the new type's list: %d", list.Code) } // Its one field is Name, with a generated key; find it on the form. form := get(t, h, "/cards/new?type="+key).Body.String() fk := regexp.MustCompile(`name="(f_[0-9a-f]+)"`).FindStringSubmatch(form) if fk == nil { t.Fatalf("no name field on the new-supplier form:\n%s", form) } rec = post(t, h, "/cards", url.Values{"type": {key}, fk[1]: {"Harbour Timber"}}) if rec.Code != http.StatusSeeOther { t.Fatalf("adding a supplier: %d\n%s", rec.Code, rec.Body.String()) } if !strings.Contains(get(t, h, "/t/"+key).Body.String(), "Harbour Timber") { t.Error("the supplier is not on the suppliers list") } } func TestTheCardLayoutChangesTheCards(t *testing.T) { h := newApp(t) // Not ana@example.com: that is the signed-in address, printed in the // sidebar on every page, so its absence could never be asserted. post(t, h, "/cards", url.Values{"f_name": {"Cnoc Teoranta"}, "f_email": {"orders@cnoc.example"}, "f_phone": {"+353 1 555 0100"}}) // Unescaped: html/template writes "+" as +, so a raw match on a // phone number can never succeed — which is how this test first failed. before := html.UnescapeString(get(t, h, "/?view=cards").Body.String()) if strings.Contains(before, "+353 1 555 0100") || !strings.Contains(before, "orders@cnoc.example") { t.Fatal("the default Customer face is not what this test assumes; it would prove nothing") } rec := post(t, h, "/t/t_customer/layout", url.Values{"face": {"f_phone"}}) if rec.Code != http.StatusSeeOther { t.Fatalf("saving the layout: %d", rec.Code) } after := html.UnescapeString(get(t, h, "/?view=cards").Body.String()) if !strings.Contains(after, "+353 1 555 0100") || strings.Contains(after, "orders@cnoc.example") { t.Error("the saved layout did not change what the cards show") } } // A bad value comes back beside the field, not as a 500 — and keeps the rest. func TestABadValueIsExplainedNotA500(t *testing.T) { h := newApp(t) rec := post(t, h, "/cards", url.Values{"f_name": {"Cnoc Teoranta"}, "f_next_action": {"next tuesday"}}) if rec.Code != http.StatusUnprocessableEntity { t.Fatalf("a bad date: status %d, want 422", rec.Code) } body := rec.Body.String() if !strings.Contains(body, "Enter a date.") { t.Error("the bad date has no message") } if !strings.Contains(body, `value="Cnoc Teoranta"`) { t.Error("the form lost the name that was typed") } } func TestTheDomainIsALinkAndForgiving(t *testing.T) { h := newApp(t) rec := post(t, h, "/cards", url.Values{"f_name": {"Brightwater Labs"}, "f_domain": {"Brightwater.example/"}}) if rec.Code != http.StatusSeeOther { t.Fatalf("a domain without https://: status %d\n%s", rec.Code, rec.Body.String()) } show := get(t, h, rec.Header().Get("Location")).Body.String() // The card's field is the editor (level-up §5.1), with a link beside // it to visit the address; the table's cell edits, so it links the // edit form instead. if !strings.Contains(show, `href="https://brightwater.example"`) || !strings.Contains(show, `value="brightwater.example"`) { t.Error("the card does not link the address, or shows the protocol") } if list := get(t, h, "/").Body.String(); !strings.Contains(list, ">brightwater.example<") || strings.Contains(list, `href="https://brightwater.example"`) { t.Error("the table does not show the address as typed, or links out of a cell that edits") } edit := get(t, h, rec.Header().Get("Location")+"/edit").Body.String() if !strings.Contains(edit, `inputmode="url"`) || strings.Contains(edit, `type="url"`) { t.Error("the input is not a forgiving URL field") } bad := post(t, h, "/cards", url.Values{"f_name": {"X"}, "f_domain": {"javascript:alert(1)"}}) if bad.Code != http.StatusUnprocessableEntity || !strings.Contains(bad.Body.String(), "Enter a web address") { t.Errorf("a script URL: status %d, want 422 with a message", bad.Code) } }