oficina / fichas Public

Clone
git clone https://amadan.net/oficina/fichas

Plain git — no account needed to clone.

Download

Download this file

1package fichastest
2
3import (
4 "net/http"
5 "net/http/httptest"
6 "net/url"
7 "strings"
8 "testing"
9
10 "amadan.net/oficina/fichas/internal/fichas"
11)
12
13// withCookies GETs target carrying the cookies a response set.
14func withCookies(t *testing.T, h http.Handler, target string, from *httptest.ResponseRecorder) string {
15 t.Helper()
16 req := httptest.NewRequest(http.MethodGet, target, nil)
17 for _, c := range from.Result().Cookies() {
18 if c.MaxAge >= 0 {
19 req.AddCookie(c)
20 }
21 }
22 rec := httptest.NewRecorder()
23 h.ServeHTTP(rec, req)
24 if rec.Code != http.StatusOK {
25 t.Fatalf("GET %s: status %d", target, rec.Code)
26 }
27 return rec.Body.String()
28}
29
30// Comfortable is the default; Compact is a choice that sticks, and the
31// change lands back on the list it was made on, its search kept.
32func TestDensityIsTheReadersChoice(t *testing.T) {
33 h := newApp(t)
34 createACustomer(t, h, "Cnoc Teoranta")
35 if body := get(t, h, "/").Body.String(); !strings.Contains(body, `data-density="comfortable"`) {
36 t.Fatal("the table is not comfortable by default")
37 }
38 back := "/t/" + fichas.TypeCustomer + "?q=cnoc"
39 rec := post(t, h, "/t/"+fichas.TypeCustomer+"/view", url.Values{"density": {"compact"}, "return_to": {back}})
40 if rec.Code != http.StatusSeeOther || rec.Header().Get("Location") != back {
41 t.Fatalf("POST view: %d to %q, want 303 to %q", rec.Code, rec.Header().Get("Location"), back)
42 }
43 body := withCookies(t, h, back, rec)
44 if !strings.Contains(body, `data-density="compact"`) {
45 t.Fatal("Compact did not stick")
46 }
47 if !strings.Contains(body, `value="compact" aria-pressed="true"`) {
48 t.Fatal("the View menu does not show Compact as chosen")
49 }
50}
51
52// Hiding a column drops its header and its cells; the title column cannot
53// be hidden, because every row is named by it and opens its card from it.
54func TestColumnsAreTheReadersChoice(t *testing.T) {
55 h := newApp(t)
56 createACustomer(t, h, "Cnoc Teoranta") // email ana@example.com
57 base := "/t/" + fichas.TypeCustomer
58 // Show only Stage: Name, Email, Domain and the rest unticked.
59 rec := post(t, h, base+"/view", url.Values{"columns": {"1"}, "show": {fichas.FieldStage}, "return_to": {base}})
60 if rec.Code != http.StatusSeeOther {
61 t.Fatalf("POST view: %d", rec.Code)
62 }
63 body := withCookies(t, h, base, rec)
64 grid := body[strings.Index(body, "<table fichas-grid"):]
65 grid = grid[:strings.Index(grid, "</table>")]
66 if strings.Contains(grid, "ana@example.com") || strings.Contains(grid, ">Email<") {
67 t.Errorf("a hidden column is still drawn:\n%s", grid)
68 }
69 if !strings.Contains(grid, "Cnoc Teoranta") || !strings.Contains(grid, ">Name<") {
70 t.Errorf("the title column was hidden:\n%s", grid)
71 }
72 if !strings.Contains(grid, ">Stage<") || !strings.Contains(grid, "Lead") {
73 t.Errorf("a column left ticked is gone:\n%s", grid)
74 }
75 if strings.Contains(grid, "Last call") {
76 t.Error("Last call was unticked but still shows")
77 }
78 // The checklist says what is shown, and the title is locked on.
79 if !strings.Contains(body, `value="`+fichas.FieldName+`" checked disabled`) {
80 t.Error("the title column is not locked on in the checklist")
81 }
82 if strings.Contains(body, `value="`+fichas.FieldEmail+`" checked`) {
83 t.Error("the checklist shows Email as shown")
84 }
85
86 // Ticking everything again forgets the choice.
87 all := url.Values{"columns": {"1"}, "return_to": {base}}
88 for _, k := range []string{fichas.FieldDomain, fichas.FieldEmail, fichas.FieldPhone, fichas.FieldStage, fichas.FieldNextAction, fichas.SortLastCall} {
89 all.Add("show", k)
90 }
91 rec = post(t, h, base+"/view", all)
92 var cleared bool
93 for _, c := range rec.Result().Cookies() {
94 if strings.HasPrefix(c.Name, "fichas-hide-") && c.MaxAge < 0 {
95 cleared = true
96 }
97 }
98 if !cleared {
99 t.Error("showing every column did not clear the cookie")
100 }
101}
102
103// A view change goes back only to a list: never another path, never
104// another origin.
105func TestAViewChangeOnlyReturnsToAList(t *testing.T) {
106 h := newApp(t)
107 base := "/t/" + fichas.TypeCustomer
108 for _, to := range []string{"//evil.example/", "https://evil.example/", "/cards/1/delete", "/\\evil.example", "/t/other",
109 // The list's own path, carrying a control character or a backslash.
110 base + "?x=\t//evil.example", base + "?x=\\evil.example"} {
111 rec := post(t, h, base+"/view", url.Values{"density": {"compact"}, "return_to": {to}})
112 if loc := rec.Header().Get("Location"); loc != base {
113 t.Errorf("return_to %q went to %q, want %q", to, loc, base)
114 }
115 }
116}
117
118// The name opens the card beside the list (level-up §5.4, D1); the
119// stretched row link of 2026-11-12 is gone, because every other cell
120// edits its own field.
121func TestTheNameOpensTheCardBesideTheList(t *testing.T) {
122 h := newApp(t)
123 path := createACustomer(t, h, "Cnoc Teoranta")
124 id := strings.TrimPrefix(path, "/cards/")
125 body := get(t, h, "/").Body.String()
126 if !strings.Contains(body, `<a fichas-title-link href="/t/t_customer?card=`+id+`">Cnoc Teoranta</a>`) {
127 t.Fatal("the name is not a link to the card beside the list")
128 }
129 if strings.Contains(body, "fichas-row-link") {
130 t.Fatal("the stretched row link is still there")
131 }
132}
133
134// A sort by a hidden column is dropped: rows are never ordered by
135// something the reader cannot see, and no link on the page carries it on.
136func TestAHiddenColumnDoesNotSort(t *testing.T) {
137 h := newApp(t)
138 createACustomer(t, h, "Cnoc Teoranta")
139 base := "/t/" + fichas.TypeCustomer
140 sorted := base + "?sort=" + fichas.FieldEmail
141 if body := get(t, h, sorted).Body.String(); !strings.Contains(body, "sort="+fichas.FieldEmail) {
142 t.Fatal("precondition: a shown column's sort is carried by the page's links")
143 }
144 rec := post(t, h, base+"/view", url.Values{"columns": {"1"}, "show": {fichas.FieldStage}, "return_to": {base}})
145 // The list's own links (the page content; the account menu's Appearance
146 // return is the raw URL, and a return there drops the sort again).
147 body := withCookies(t, h, sorted, rec)
148 page := body[strings.Index(body, `id="main"`):]
149 if i := strings.Index(page, "sort="+fichas.FieldEmail); i >= 0 {
150 t.Errorf("the page still sorts by the hidden Email column: …%s…", page[max(0, i-200):i+40])
151 }
152}
153
154// Served over HTTPS, the preference cookies are Secure, as every other
155// cookie the app and estilo set is; always HttpOnly and SameSite=Lax.
156func TestPreferenceCookiesAreSecureOverHTTPS(t *testing.T) {
157 const origin = "https://fichas.example"
158 h := appWith(t, func(c *fichas.Config) { c.Origin = origin })
159 base := "/t/" + fichas.TypeCustomer
160 for _, form := range []url.Values{
161 {"density": {"compact"}},
162 {"columns": {"1"}, "show": {fichas.FieldStage}},
163 {"columns": {"1"}, "show": {fichas.FieldDomain, fichas.FieldEmail, fichas.FieldPhone, fichas.FieldStage, fichas.FieldNextAction, fichas.SortLastCall}},
164 } {
165 req := httptest.NewRequest(http.MethodPost, origin+base+"/view", strings.NewReader(form.Encode()))
166 req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
167 req.Header.Set("Origin", origin)
168 rec := httptest.NewRecorder()
169 h.ServeHTTP(rec, req)
170 cookies := rec.Result().Cookies()
171 if rec.Code != http.StatusSeeOther || len(cookies) == 0 {
172 t.Fatalf("POST %v: %d, %d cookies", form, rec.Code, len(cookies))
173 }
174 for _, c := range cookies {
175 if !c.Secure || !c.HttpOnly || c.SameSite != http.SameSiteLaxMode {
176 t.Errorf("%s is not Secure, HttpOnly and SameSite=Lax over HTTPS: %s", c.Name, c.String())
177 }
178 }
179 }
180}
181