| 1 | package mapatest |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "net/http" |
| 6 | "net/http/httptest" |
| 7 | "net/url" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | ) |
| 11 | |
| 12 | // Where the reader's locale comes from (level-up build note 85, review |
| 13 | // round 8): their own choice in Settings, then their browser's |
| 14 | // Accept-Language, then the team's. A /de/ path prefix and rastrillo's |
| 15 | // locale cookie are not read. |
| 16 | |
| 17 | // requestIn is a request from Ana's browser asking for language, with |
| 18 | // any cookies. |
| 19 | func (rig *rig) requestIn(method, target, language string, form url.Values, |
| 20 | cookies ...*http.Cookie) *httptest.ResponseRecorder { |
| 21 | var body *strings.Reader |
| 22 | if form != nil { |
| 23 | body = strings.NewReader(form.Encode()) |
| 24 | } else { |
| 25 | body = strings.NewReader("") |
| 26 | } |
| 27 | request := httptest.NewRequest(method, target, body) |
| 28 | if form != nil { |
| 29 | request.Header.Set("Content-Type", |
| 30 | "application/x-www-form-urlencoded") |
| 31 | request.Header.Set("Origin", testOrigin) |
| 32 | } |
| 33 | request.Header.Set("Accept-Language", language) |
| 34 | for _, cookie := range cookies { |
| 35 | request.AddCookie(cookie) |
| 36 | } |
| 37 | recorder := httptest.NewRecorder() |
| 38 | rig.built.As(Ana).ServeHTTP(recorder, request) |
| 39 | return recorder |
| 40 | } |
| 41 | |
| 42 | func TestNoPathPrefixOrCookieChoosesTheLocale(t *testing.T) { |
| 43 | rig := newRig(t) |
| 44 | if got := rig.requestIn(http.MethodGet, "/de/d/2026-11-17", "en-IE", |
| 45 | nil); got.Code != http.StatusNotFound { |
| 46 | t.Errorf("/de/d/2026-11-17 answered %d, not 404", got.Code) |
| 47 | } |
| 48 | cookie := &http.Cookie{Name: "rastrillo_locale", Value: "de"} |
| 49 | day := rig.requestIn(http.MethodGet, "/d/2026-11-17", "en-US", nil, |
| 50 | cookie).Body.String() |
| 51 | if !strings.Contains(day, "Tuesday, November 17<") || |
| 52 | strings.Contains(day, "Dienstag") { |
| 53 | t.Error("rastrillo's locale cookie outranked Accept-Language") |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | func TestThePersonsOwnFormatOutranksTheBrowser(t *testing.T) { |
| 58 | rig := newRig(t) |
| 59 | settings := rig.requestIn(http.MethodGet, "/settings", "en-US", nil) |
| 60 | wantIn(t, "Settings", "en-US", settings.Body.String(), |
| 61 | `<option value="" selected>Your browser's: English (US)</option>`, |
| 62 | `<option value="de">German: `, "until you choose, your browser") |
| 63 | before := tableRowsHash(t, rig) |
| 64 | rig.requestIn(http.MethodGet, "/d/2026-11-17", "en-US", nil) |
| 65 | if tableRowsHash(t, rig) != before { |
| 66 | t.Fatal("reading wrote") |
| 67 | } |
| 68 | saved := rig.requestIn(http.MethodPost, "/settings/locale", "en-US", |
| 69 | url.Values{"locale": {"de"}}) |
| 70 | if saved.Code != http.StatusSeeOther || |
| 71 | saved.Header().Get("Location") != "/settings?done=locale" { |
| 72 | t.Fatalf("saving: %d %s", saved.Code, saved.Header().Get("Location")) |
| 73 | } |
| 74 | var logged int64 |
| 75 | rig.db.G.Raw("SELECT count(*) FROM events WHERE verb = " + |
| 76 | "'locale_set'").Row().Scan(&logged) |
| 77 | if logged != 1 { |
| 78 | t.Fatalf("the setting logged %d locale_set events", logged) |
| 79 | } |
| 80 | var sealed []byte |
| 81 | rig.db.G.Raw("SELECT sealed_locale FROM members").Row().Scan(&sealed) |
| 82 | if len(sealed) < 16 || bytes.Equal(sealed, []byte("de")) { |
| 83 | t.Fatalf("the setting is not sealed: %q", sealed) |
| 84 | } |
| 85 | for _, language := range []string{"en-US", "fr", ""} { |
| 86 | day := rig.requestIn(http.MethodGet, "/d/2026-11-17", language, |
| 87 | nil).Body.String() |
| 88 | wantIn(t, "an own German format's day", language, day, |
| 89 | "Dienstag, 17. November<", `<th abbr="Montag">M</th>`) |
| 90 | } |
| 91 | done := rig.requestIn(http.MethodGet, "/settings?done=locale", "en-US", |
| 92 | nil).Body.String() |
| 93 | wantIn(t, "the saved notice", "de", done, |
| 94 | "Dates, times and numbers are now written as in German.", |
| 95 | `<option value="de" selected>`, "your own setting") |
| 96 | rig.requestIn(http.MethodPost, "/settings/locale", "en-US", |
| 97 | url.Values{"locale": {""}}) |
| 98 | day := rig.requestIn(http.MethodGet, "/d/2026-11-17", "en-US", nil) |
| 99 | wantIn(t, "the browser's day again", "en-US", day.Body.String(), |
| 100 | "Tuesday, November 17<") |
| 101 | } |
| 102 | |
| 103 | func TestAFormatMapaDoesNotOfferIsRefused(t *testing.T) { |
| 104 | rig := newRig(t) |
| 105 | for _, value := range []string{"pt-PT", "de-DE", "en_US", "x"} { |
| 106 | refused := rig.requestIn(http.MethodPost, "/settings/locale", |
| 107 | "en-IE", url.Values{"locale": {value}}) |
| 108 | if refused.Code != http.StatusUnprocessableEntity || |
| 109 | !strings.Contains(refused.Body.String(), |
| 110 | "Choose one of the formats listed.") { |
| 111 | t.Errorf("%q: %d", value, refused.Code) |
| 112 | } |
| 113 | } |
| 114 | if rig.count(t, "members") != 0 { |
| 115 | t.Error("a refused format wrote a member row") |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | // Saving what is already set writes nothing (review round 8, diff |
| 120 | // check): no event, and for a person with no row, no row. |
| 121 | func TestSavingTheSameFormatWritesNothing(t *testing.T) { |
| 122 | rig := newRig(t) |
| 123 | rig.requestIn(http.MethodPost, "/settings/locale", "en-IE", |
| 124 | url.Values{"locale": {""}}) |
| 125 | if rig.count(t, "members") != 0 || rig.count(t, "events") != 0 { |
| 126 | t.Fatal("following the browser, already, wrote") |
| 127 | } |
| 128 | for range 2 { |
| 129 | rig.requestIn(http.MethodPost, "/settings/locale", "en-IE", |
| 130 | url.Values{"locale": {"fr"}}) |
| 131 | } |
| 132 | var logged int64 |
| 133 | rig.db.G.Raw("SELECT count(*) FROM events WHERE verb = " + |
| 134 | "'locale_set'").Row().Scan(&logged) |
| 135 | if logged != 1 { |
| 136 | t.Fatalf("saving French twice logged %d locale_set events", logged) |
| 137 | } |
| 138 | } |
| 139 | |