| 1 | package mapa |
| 2 | |
| 3 | import ( |
| 4 | "io" |
| 5 | "log/slog" |
| 6 | "net/http" |
| 7 | "net/http/httptest" |
| 8 | "net/url" |
| 9 | "strconv" |
| 10 | "strings" |
| 11 | "testing" |
| 12 | ) |
| 13 | |
| 14 | // The first scratch commit's token map fails closed (review L): when a |
| 15 | // person holds firstCap live tokens, their new token is refused and |
| 16 | // makes nothing, where it once made a page and forgot it, so every later |
| 17 | // autosave with that token made another. The cap is per person (review |
| 18 | // round 4): Bo's first scratch commit still works while Ana's is full. |
| 19 | func TestAFullTokenMapRefusesANewPage(t *testing.T) { |
| 20 | store := testStore(t) |
| 21 | built := &app{store: store, allowUnauthenticated: true, |
| 22 | testIdentity: "ana@example.com", |
| 23 | logger: slog.New(slog.NewTextHandler(io.Discard, nil))} |
| 24 | built.firsts.made = map[string]map[string]firstMade{ |
| 25 | "ana@example.com": {}} |
| 26 | for index := range firstCap { |
| 27 | built.firsts.made["ana@example.com"]["held"+strconv.Itoa(index)] = |
| 28 | firstMade{at: store.now()} |
| 29 | } |
| 30 | for range 2 { |
| 31 | form := url.Values{"body": {"Hello."}} |
| 32 | request := httptest.NewRequest(http.MethodPost, "/scratch", |
| 33 | strings.NewReader(form.Encode())) |
| 34 | request.Header.Set("Content-Type", |
| 35 | "application/x-www-form-urlencoded") |
| 36 | recorder := httptest.NewRecorder() |
| 37 | built.firstScratch(recorder, request, "TOKENTOKENTOKEN0001") |
| 38 | if recorder.Code != http.StatusServiceUnavailable { |
| 39 | t.Fatalf("a full token map answered %d", recorder.Code) |
| 40 | } |
| 41 | } |
| 42 | var pages int64 |
| 43 | if err := store.database.Model(&Page{}).Count(&pages).Error; err != nil || |
| 44 | pages != 0 { |
| 45 | t.Fatalf("a full token map made %d pages (%v)", pages, err) |
| 46 | } |
| 47 | built.testIdentity = "bo@example.com" |
| 48 | form := url.Values{"body": {"Bo's."}} |
| 49 | request := httptest.NewRequest(http.MethodPost, "/scratch", |
| 50 | strings.NewReader(form.Encode())) |
| 51 | request.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| 52 | recorder := httptest.NewRecorder() |
| 53 | built.firstScratch(recorder, request, "TOKENTOKENTOKEN0002") |
| 54 | if recorder.Code != http.StatusOK { |
| 55 | t.Fatalf("Ana's full token map refused Bo: %d", recorder.Code) |
| 56 | } |
| 57 | } |
| 58 | |