| 1 | package mapatest |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "net/http" |
| 6 | "net/url" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | "time" |
| 10 | |
| 11 | "amadan.net/oficina/mapa/internal/mapa" |
| 12 | ) |
| 13 | |
| 14 | // Pins are the viewer's own (level-up §3.2, §11 Q1 proposed): Bo's pin |
| 15 | // is in Bo's sidebar and nobody else's, and a page cannot be pinned by |
| 16 | // someone who may not open it. |
| 17 | func TestPinsAreYoursAlone(t *testing.T) { |
| 18 | rig := newRig(t) |
| 19 | acme := rig.createPage(t, Ana, "Acme", "One.") |
| 20 | rig.post(t, Bo, acme+"/pin", url.Values{"pin": {"1"}}) |
| 21 | bo, ana := rig.page(t, Bo, "/pages"), rig.page(t, Ana, "/pages") |
| 22 | if !strings.Contains(between(bo, "Pinned", "</nav>"), "Acme") { |
| 23 | t.Fatal("Bo's pin is not in his sidebar") |
| 24 | } |
| 25 | if strings.Contains(ana, ">Pinned<") { |
| 26 | t.Fatal("Bo's pin showed in Ana's sidebar") |
| 27 | } |
| 28 | if !strings.Contains(rig.page(t, Bo, acme), "Unpin from sidebar") { |
| 29 | t.Fatal("a pinned page does not offer Unpin") |
| 30 | } |
| 31 | rig.post(t, Bo, acme+"/pin", url.Values{"pin": {"0"}}) |
| 32 | if strings.Contains(rig.page(t, Bo, "/pages"), ">Pinned<") { |
| 33 | t.Fatal("Unpin left the page pinned") |
| 34 | } |
| 35 | scratch := rig.post(t, Ana, "/scratch", url.Values{"body": {"Mine."}}) |
| 36 | private := scratch.Header().Get("Location") |
| 37 | if got := rig.post(t, Bo, private+"/pin", |
| 38 | url.Values{"pin": {"1"}}); got.Code != http.StatusNotFound { |
| 39 | t.Fatalf("Bo pinned Ana's scratch page: %d", got.Code) |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | // Recently edited is the pages you last wrote in, newest first, and |
| 44 | // "Update links" is not writing (level-up §3.2). |
| 45 | func TestRecentlyEditedIsWhatYouWroteIn(t *testing.T) { |
| 46 | rig := newRig(t) |
| 47 | acme := rig.createPage(t, Bo, "Acme", "One.") |
| 48 | rig.createPage(t, Bo, "Beta", "Two.") |
| 49 | recent := between(rig.page(t, Bo, acme), "Recently edited", "</nav>") |
| 50 | if strings.Index(recent, "Beta") > strings.Index(recent, "Acme") || |
| 51 | !strings.Contains(recent, "Acme") { |
| 52 | t.Fatalf("Recently edited is not newest first:\n%s", recent) |
| 53 | } |
| 54 | if strings.Contains(rig.page(t, Ana, acme), "Recently edited") { |
| 55 | t.Fatal("Ana, who wrote nothing, has Recently edited") |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | // Recently edited is yours only (review L2): what a teammate wrote, and |
| 60 | // a teammate's later edit to a page you wrote, change nothing in it. |
| 61 | func TestRecentlyEditedIsYoursOnly(t *testing.T) { |
| 62 | rig := newRig(t) |
| 63 | mine := rig.createPage(t, Bo, "Mine", "Bo's.") |
| 64 | rig.createPage(t, Ana, "Theirs", "Ana's.") |
| 65 | revision := revisionAs(t, rig, Ana, mine) |
| 66 | rig.post(t, Ana, mine, url.Values{"revision": {revision}, |
| 67 | "body": {"Ana was here."}}) |
| 68 | recent := between(rig.page(t, Bo, "/pages"), "Recently edited", |
| 69 | "</nav>") |
| 70 | if strings.Contains(recent, "Theirs") || !strings.Contains(recent, |
| 71 | "Mine") { |
| 72 | t.Fatalf("Bo's Recently edited:\n%s", recent) |
| 73 | } |
| 74 | anas := between(rig.page(t, Ana, "/pages"), "Recently edited", |
| 75 | "</nav>") |
| 76 | if strings.Index(anas, "Mine") > strings.Index(anas, "Theirs") { |
| 77 | t.Fatalf("Ana's edit to Mine is not her newest:\n%s", anas) |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | // The sealed pins are one length however many pages are pinned (review |
| 82 | // L11), so the file does not count them. |
| 83 | func TestSealedPinsAreOneLength(t *testing.T) { |
| 84 | rig := newRig(t) |
| 85 | one := rig.createPage(t, Ana, "One", "1.") |
| 86 | two := rig.createPage(t, Ana, "Two", "2.") |
| 87 | rig.post(t, Ana, one+"/pin", url.Values{"pin": {"1"}}) |
| 88 | rig.post(t, Bo, one+"/pin", url.Values{"pin": {"1"}}) |
| 89 | rig.post(t, Bo, two+"/pin", url.Values{"pin": {"1"}}) |
| 90 | var lengths []int |
| 91 | err := rig.db.G.Raw("SELECT length(sealed_pins) FROM members " + |
| 92 | "WHERE sealed_pins IS NOT NULL").Scan(&lengths).Error |
| 93 | if err != nil || len(lengths) != 2 || lengths[0] != lengths[1] { |
| 94 | t.Fatalf("sealed pins lengths %v (%v)", lengths, err) |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | // The scoped accessor's own guard counts (review L1): asked by id for a |
| 99 | // page outside the scope, it answers nothing, even when the caller's |
| 100 | // list of ids was wrong. Without the guard this test fails. |
| 101 | func TestTheScopedAccessorGuardsEveryRead(t *testing.T) { |
| 102 | rig := newRig(t) |
| 103 | rig.createPage(t, Bo, "Bo's", "So Bo is a member.") |
| 104 | rig.post(t, Ana, "/scratch", url.Values{"body": {"Ana alone."}}) |
| 105 | var private []int64 |
| 106 | err := rig.db.G.Raw("SELECT id FROM pages WHERE rung = ?", |
| 107 | "private").Scan(&private).Error |
| 108 | if err != nil || len(private) != 1 { |
| 109 | t.Fatalf("Ana's private page: %v (%v)", private, err) |
| 110 | } |
| 111 | ctx := context.Background() |
| 112 | bo, err := rig.built.Store.Viewer(ctx, Bo, time.UTC) |
| 113 | if err != nil { |
| 114 | t.Fatal(err) |
| 115 | } |
| 116 | if got := rig.built.Index.SideLinks(ctx, mapa.ScopeOf(bo), private, |
| 117 | 0, ""); len(got) != 0 { |
| 118 | t.Fatal("the accessor read a page outside its scope") |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | // Pin and Unpin from a sidebar row's ⋯ come back to the screen the row |
| 123 | // was on, never off the site; the bar's Pin still goes to the page |
| 124 | // (build note 88). |
| 125 | func TestPinFromARowComesBack(t *testing.T) { |
| 126 | rig := newRig(t) |
| 127 | acme := rig.createPage(t, Ana, "Acme", "One.") |
| 128 | cases := map[string]string{"/pages": "/pages", |
| 129 | "/d/2026-11-14?x=1": "/d/2026-11-14?x=1", "": acme, |
| 130 | "//evil.example/": acme, "https://evil.example": acme} |
| 131 | for back, want := range cases { |
| 132 | got := rig.post(t, Bo, acme+"/pin", url.Values{"pin": {"1"}, |
| 133 | "return": {back}}) |
| 134 | if where := got.Header().Get("Location"); where != want { |
| 135 | t.Errorf("return %q: went to %q, want %q", back, where, want) |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | |