| 1 | package fichastest |
| 2 | |
| 3 | import ( |
| 4 | "net/http" |
| 5 | "net/url" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | ) |
| 9 | |
| 10 | // An empty "Add a customer" is refused beside the name, and makes no card |
| 11 | // (UX review, 2026-11-12: it made one called "Card 305"). |
| 12 | func TestACardNeedsAName(t *testing.T) { |
| 13 | h := newApp(t) |
| 14 | for _, name := range []string{"", " "} { |
| 15 | rec := post(t, h, "/cards", url.Values{"f_name": {name}, "f_email": {"ana@example.com"}}) |
| 16 | if rec.Code != http.StatusUnprocessableEntity { |
| 17 | t.Fatalf("name %q: status %d, want 422", name, rec.Code) |
| 18 | } |
| 19 | if !strings.Contains(rec.Body.String(), "Give it a name.") { |
| 20 | t.Errorf("name %q: the form does not say what is missing", name) |
| 21 | } |
| 22 | } |
| 23 | if body := get(t, h, "/").Body.String(); strings.Contains(body, "fichas-row-link") { |
| 24 | t.Error("a nameless card was created") |
| 25 | } |
| 26 | // Editing a card to an empty name is refused the same way. |
| 27 | path := createACustomer(t, h, "Cnoc Teoranta") |
| 28 | rec := post(t, h, path, url.Values{"f_name": {""}}) |
| 29 | if rec.Code != http.StatusUnprocessableEntity || !strings.Contains(rec.Body.String(), "Give it a name.") { |
| 30 | t.Errorf("an edit to an empty name: status %d %s", rec.Code, rec.Body.String()) |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | // Anything that is not there is the app's own 404 page, never the |
| 35 | // router's plain "404 page not found". |
| 36 | func TestNotFoundIsTheAppsOwnPage(t *testing.T) { |
| 37 | h := newApp(t) |
| 38 | for _, path := range []string{"/no/such/page", "/cards/999999", "/cards/not-a-number", "/t/t_nosuch"} { |
| 39 | rec := get(t, h, path) |
| 40 | if rec.Code != http.StatusNotFound { |
| 41 | t.Errorf("%s: status %d, want 404", path, rec.Code) |
| 42 | continue |
| 43 | } |
| 44 | body := rec.Body.String() |
| 45 | if strings.Contains(body, "404 page not found") || !strings.Contains(body, "<html") { |
| 46 | t.Errorf("%s: not the app's error page:\n%.300s", path, body) |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | // A new field's findability follows its kind unless someone chooses under |
| 52 | // Advanced: Email, Phone and Web address are findable, the rest private. |
| 53 | func TestANewFieldsTierFollowsItsKind(t *testing.T) { |
| 54 | h := newApp(t) |
| 55 | base := "/t/t_customer/fields" |
| 56 | add := func(label, kind, tier string) { |
| 57 | t.Helper() |
| 58 | form := url.Values{"label": {label}, "kind": {kind}} |
| 59 | if tier != "" { |
| 60 | form.Set("tier", tier) |
| 61 | } |
| 62 | if kind == "enum" { |
| 63 | form.Set("options", "Small\nLarge") |
| 64 | } |
| 65 | if rec := post(t, h, base, form); rec.Code != http.StatusSeeOther { |
| 66 | t.Fatalf("add %s: status %d\n%s", label, rec.Code, rec.Body.String()) |
| 67 | } |
| 68 | } |
| 69 | add("Billing email", "email", "auto") |
| 70 | add("Mobile", "phone", "") |
| 71 | add("Seats", "number", "auto") |
| 72 | add("Size", "enum", "auto") |
| 73 | add("Private email", "email", "sealed") |
| 74 | page := get(t, h, base).Body.String() |
| 75 | for label, findable := range map[string]bool{"Billing email": true, "Mobile": true, "Seats": false, "Size": false, "Private email": false} { |
| 76 | i := strings.Index(page, "<td>"+label) |
| 77 | if i < 0 { |
| 78 | t.Fatalf("%s is not listed", label) |
| 79 | } |
| 80 | row := page[i:] |
| 81 | row = row[:strings.Index(row, "</tr>")] |
| 82 | if got := strings.Contains(row, "<td>Yes</td>"); got != findable { |
| 83 | t.Errorf("%s: findable %v, want %v", label, got, findable) |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | |