| 1 | package fichastest |
| 2 | |
| 3 | import ( |
| 4 | "html" |
| 5 | "net/http" |
| 6 | "net/url" |
| 7 | "regexp" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | ) |
| 11 | |
| 12 | func TestAddingAFieldPutsItEverywhere(t *testing.T) { |
| 13 | h := newApp(t) |
| 14 | createACustomer(t, h, "Cnoc Teoranta") |
| 15 | |
| 16 | rec := post(t, h, "/t/t_customer/fields", url.Values{"label": {"Contract value"}, "kind": {"number"}, "tier": {"sealed"}}) |
| 17 | if rec.Code != http.StatusSeeOther { |
| 18 | t.Fatalf("adding a field: %d\n%s", rec.Code, rec.Body.String()) |
| 19 | } |
| 20 | if !strings.Contains(get(t, h, "/t/t_customer/fields").Body.String(), "Contract value") { |
| 21 | t.Error("the field is not on the fields page") |
| 22 | } |
| 23 | if !strings.Contains(get(t, h, "/").Body.String(), ">Contract value<") { |
| 24 | t.Error("the field is not a column in the table") |
| 25 | } |
| 26 | if !strings.Contains(get(t, h, "/cards/new?type=t_customer").Body.String(), "Contract value") { |
| 27 | t.Error("the field is not on the new-customer form") |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | // A refusal comes back to the same form, with the message and what was typed. |
| 32 | func TestARefusedFieldKeepsTheForm(t *testing.T) { |
| 33 | h := newApp(t) |
| 34 | rec := post(t, h, "/t/t_customer/fields", url.Values{ |
| 35 | "label": {"Region"}, "kind": {"enum"}, "tier": {"print"}, "options": {"North\nSouth"}}) |
| 36 | if rec.Code != http.StatusUnprocessableEntity { |
| 37 | t.Fatalf("a findable choice: status %d, want 422", rec.Code) |
| 38 | } |
| 39 | body := rec.Body.String() |
| 40 | if !strings.Contains(body, "give the values away") { |
| 41 | t.Error("the refusal does not say why") |
| 42 | } |
| 43 | if !strings.Contains(body, `value="Region"`) || !strings.Contains(body, "North") { |
| 44 | t.Error("the form lost what was typed") |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | func TestANewTypeIsSomewhereToGo(t *testing.T) { |
| 49 | h := newApp(t) |
| 50 | rec := post(t, h, "/types", url.Values{"label": {"Supplier"}}) |
| 51 | if rec.Code != http.StatusSeeOther { |
| 52 | t.Fatalf("creating a type: %d\n%s", rec.Code, rec.Body.String()) |
| 53 | } |
| 54 | fields := rec.Header().Get("Location") |
| 55 | m := regexp.MustCompile(`^/t/(t_[0-9a-f]+)/fields$`).FindStringSubmatch(fields) |
| 56 | if m == nil { |
| 57 | t.Fatalf("created a type and went to %q", fields) |
| 58 | } |
| 59 | key := m[1] |
| 60 | if !strings.Contains(get(t, h, "/").Body.String(), "Suppliers") { |
| 61 | t.Error("the new type is not in the sidebar") |
| 62 | } |
| 63 | list := get(t, h, "/t/"+key) |
| 64 | if list.Code != http.StatusOK || !strings.Contains(list.Body.String(), "No suppliers yet") { |
| 65 | t.Fatalf("the new type's list: %d", list.Code) |
| 66 | } |
| 67 | // Its one field is Name, with a generated key; find it on the form. |
| 68 | form := get(t, h, "/cards/new?type="+key).Body.String() |
| 69 | fk := regexp.MustCompile(`name="(f_[0-9a-f]+)"`).FindStringSubmatch(form) |
| 70 | if fk == nil { |
| 71 | t.Fatalf("no name field on the new-supplier form:\n%s", form) |
| 72 | } |
| 73 | rec = post(t, h, "/cards", url.Values{"type": {key}, fk[1]: {"Harbour Timber"}}) |
| 74 | if rec.Code != http.StatusSeeOther { |
| 75 | t.Fatalf("adding a supplier: %d\n%s", rec.Code, rec.Body.String()) |
| 76 | } |
| 77 | if !strings.Contains(get(t, h, "/t/"+key).Body.String(), "Harbour Timber") { |
| 78 | t.Error("the supplier is not on the suppliers list") |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | func TestTheCardLayoutChangesTheCards(t *testing.T) { |
| 83 | h := newApp(t) |
| 84 | // Not ana@example.com: that is the signed-in address, printed in the |
| 85 | // sidebar on every page, so its absence could never be asserted. |
| 86 | post(t, h, "/cards", url.Values{"f_name": {"Cnoc Teoranta"}, "f_email": {"orders@cnoc.example"}, "f_phone": {"+353 1 555 0100"}}) |
| 87 | // Unescaped: html/template writes "+" as +, so a raw match on a |
| 88 | // phone number can never succeed — which is how this test first failed. |
| 89 | before := html.UnescapeString(get(t, h, "/?view=cards").Body.String()) |
| 90 | if strings.Contains(before, "+353 1 555 0100") || !strings.Contains(before, "orders@cnoc.example") { |
| 91 | t.Fatal("the default Customer face is not what this test assumes; it would prove nothing") |
| 92 | } |
| 93 | rec := post(t, h, "/t/t_customer/layout", url.Values{"face": {"f_phone"}}) |
| 94 | if rec.Code != http.StatusSeeOther { |
| 95 | t.Fatalf("saving the layout: %d", rec.Code) |
| 96 | } |
| 97 | after := html.UnescapeString(get(t, h, "/?view=cards").Body.String()) |
| 98 | if !strings.Contains(after, "+353 1 555 0100") || strings.Contains(after, "orders@cnoc.example") { |
| 99 | t.Error("the saved layout did not change what the cards show") |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | // A bad value comes back beside the field, not as a 500 — and keeps the rest. |
| 104 | func TestABadValueIsExplainedNotA500(t *testing.T) { |
| 105 | h := newApp(t) |
| 106 | rec := post(t, h, "/cards", url.Values{"f_name": {"Cnoc Teoranta"}, "f_next_action": {"next tuesday"}}) |
| 107 | if rec.Code != http.StatusUnprocessableEntity { |
| 108 | t.Fatalf("a bad date: status %d, want 422", rec.Code) |
| 109 | } |
| 110 | body := rec.Body.String() |
| 111 | if !strings.Contains(body, "Enter a date.") { |
| 112 | t.Error("the bad date has no message") |
| 113 | } |
| 114 | if !strings.Contains(body, `value="Cnoc Teoranta"`) { |
| 115 | t.Error("the form lost the name that was typed") |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | func TestTheDomainIsALinkAndForgiving(t *testing.T) { |
| 120 | h := newApp(t) |
| 121 | rec := post(t, h, "/cards", url.Values{"f_name": {"Brightwater Labs"}, "f_domain": {"Brightwater.example/"}}) |
| 122 | if rec.Code != http.StatusSeeOther { |
| 123 | t.Fatalf("a domain without https://: status %d\n%s", rec.Code, rec.Body.String()) |
| 124 | } |
| 125 | show := get(t, h, rec.Header().Get("Location")).Body.String() |
| 126 | // The card's field is the editor (level-up §5.1), with a link beside |
| 127 | // it to visit the address; the table's cell edits, so it links the |
| 128 | // edit form instead. |
| 129 | if !strings.Contains(show, `href="https://brightwater.example"`) || !strings.Contains(show, `value="brightwater.example"`) { |
| 130 | t.Error("the card does not link the address, or shows the protocol") |
| 131 | } |
| 132 | if list := get(t, h, "/").Body.String(); !strings.Contains(list, ">brightwater.example<") || strings.Contains(list, `href="https://brightwater.example"`) { |
| 133 | t.Error("the table does not show the address as typed, or links out of a cell that edits") |
| 134 | } |
| 135 | edit := get(t, h, rec.Header().Get("Location")+"/edit").Body.String() |
| 136 | if !strings.Contains(edit, `inputmode="url"`) || strings.Contains(edit, `type="url"`) { |
| 137 | t.Error("the input is not a forgiving URL field") |
| 138 | } |
| 139 | |
| 140 | bad := post(t, h, "/cards", url.Values{"f_name": {"X"}, "f_domain": {"javascript:alert(1)"}}) |
| 141 | if bad.Code != http.StatusUnprocessableEntity || !strings.Contains(bad.Body.String(), "Enter a web address") { |
| 142 | t.Errorf("a script URL: status %d, want 422 with a message", bad.Code) |
| 143 | } |
| 144 | } |
| 145 | |