| 1 | package server |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "context" |
| 6 | "database/sql" |
| 7 | "image/png" |
| 8 | "net/http" |
| 9 | "net/http/cookiejar" |
| 10 | "net/http/httptest" |
| 11 | "net/url" |
| 12 | "path/filepath" |
| 13 | "strconv" |
| 14 | "strings" |
| 15 | "testing" |
| 16 | |
| 17 | "gitlab.com/Timfa/bluegit/internal/access" |
| 18 | "gitlab.com/Timfa/bluegit/internal/auth" |
| 19 | "gitlab.com/Timfa/bluegit/internal/card" |
| 20 | "gitlab.com/Timfa/bluegit/internal/config" |
| 21 | "gitlab.com/Timfa/bluegit/internal/store" |
| 22 | |
| 23 | _ "modernc.org/sqlite" |
| 24 | ) |
| 25 | |
| 26 | func TestLinkPreviews(t *testing.T) { |
| 27 | storage := t.TempDir() |
| 28 | st, err := store.Open(filepath.Join(storage, "bluegit.db"), bytes32()) |
| 29 | if err != nil { |
| 30 | t.Fatal(err) |
| 31 | } |
| 32 | t.Cleanup(func() { _ = st.Close() }) |
| 33 | cfg := config.Config{ |
| 34 | BaseURL: "http://git.example", Listen: "127.0.0.1:0", StoragePath: storage, |
| 35 | Auth: config.Auth{Mode: "dev", DevUser: "tim", DevAuthID: 1, UserAgent: "test", BaseURL: "https://auth.timfalken.com"}, |
| 36 | Git: config.Git{Binary: "git", DefaultBranch: "master"}, |
| 37 | } |
| 38 | srv := &Server{Cfg: cfg, Store: st, Auth: auth.Dev{Username: "tim", AuthID: 1}, Dev: true, Git: "git", HookBin: "/bin/true"} |
| 39 | ts := httptest.NewServer(srv.Handler()) |
| 40 | t.Cleanup(ts.Close) |
| 41 | ctx := context.Background() |
| 42 | |
| 43 | jar, _ := cookiejar.New(nil) |
| 44 | tim := &http.Client{Jar: jar} |
| 45 | anon := &http.Client{} |
| 46 | page := read(t, mustGet(t, tim, ts.URL+"/login")) |
| 47 | page = read(t, mustPost(t, tim, ts.URL+"/new/repo", url.Values{ |
| 48 | "csrf": {csrfOf(t, page)}, "name": {"widgets"}, "visibility": {"public"}, |
| 49 | })) |
| 50 | page = read(t, mustPost(t, tim, ts.URL+"/settings/tokens", url.Values{"csrf": {csrfOf(t, page)}, "name": {"tim"}})) |
| 51 | token := between(page, `<p class="code">`, "</p>") |
| 52 | me, err := st.UserByUsername(ctx, "tim") |
| 53 | if err != nil { |
| 54 | t.Fatal(err) |
| 55 | } |
| 56 | |
| 57 | work := t.TempDir() |
| 58 | git(t, work, "init", "-b", "master") |
| 59 | git(t, work, "config", "user.email", "tim@example") |
| 60 | git(t, work, "config", "user.name", "tim") |
| 61 | var lines []string |
| 62 | for i := 1; i <= 25; i++ { |
| 63 | lines = append(lines, "L"+twoDigits(i)+"-marker") |
| 64 | } |
| 65 | writeFile(t, filepath.Join(work, "main.go"), strings.Join(lines, "\n")+"\n") |
| 66 | writeFile(t, filepath.Join(work, "README.md"), "# Widgets\n\nEen widget die telt.\n\nTweede alinea.\n") |
| 67 | git(t, work, "add", ".") |
| 68 | git(t, work, "commit", "-m", "Fix the gate") |
| 69 | remote := strings.Replace(ts.URL, "http://", "http://tim:"+token+"@", 1) + "/tim/widgets.git" |
| 70 | git(t, work, "remote", "add", "origin", remote) |
| 71 | git(t, work, "push", "-u", "origin", "master") |
| 72 | sha := strings.TrimSpace(mustGit(t, work, "rev-parse", "HEAD")) |
| 73 | |
| 74 | page = read(t, mustGet(t, tim, ts.URL+"/tim/widgets")) |
| 75 | read(t, mustPost(t, tim, ts.URL+"/tim/widgets/-/star", url.Values{"csrf": {csrfOf(t, page)}})) |
| 76 | |
| 77 | home := read(t, mustGet(t, anon, ts.URL+"/tim/widgets")) |
| 78 | blob := socialBlob(home) |
| 79 | for _, want := range []string{ |
| 80 | "tim/widgets", "Een widget die telt.", "Go", "1 star", "Fix the gate", |
| 81 | "BlueGit", "summary_large_image", "#1f6feb", "application/json+oembed", "/assets/logo.svg", |
| 82 | } { |
| 83 | if !strings.Contains(home, want) { |
| 84 | t.Fatalf("repo-kaart mist %q\n%s", want, home) |
| 85 | } |
| 86 | } |
| 87 | if strings.Contains(blob, "Tweede alinea") { |
| 88 | t.Fatal("tweede alinea hoort niet in de kaart") |
| 89 | } |
| 90 | img := ts.URL + "/-/card.png?path=" + url.QueryEscape("/tim/widgets") |
| 91 | first := mustGet(t, anon, img) |
| 92 | if first.Header.Get("X-Card-Cache") != "miss" || first.Header.Get("Content-Type") != "image/png" { |
| 93 | t.Fatalf("cache %s type %s", first.Header.Get("X-Card-Cache"), first.Header.Get("Content-Type")) |
| 94 | } |
| 95 | png1 := []byte(read(t, first)) |
| 96 | cfgPNG, err := png.DecodeConfig(bytes.NewReader(png1)) |
| 97 | if err != nil || cfgPNG.Width != card.Width || cfgPNG.Height != card.Height { |
| 98 | t.Fatalf("png %v %v", cfgPNG, err) |
| 99 | } |
| 100 | second := mustGet(t, anon, img) |
| 101 | if second.Header.Get("X-Card-Cache") != "hit" { |
| 102 | t.Fatalf("tweede keer %s", second.Header.Get("X-Card-Cache")) |
| 103 | } |
| 104 | if !bytes.Equal(png1, []byte(read(t, second))) { |
| 105 | t.Fatal("cache levert andere bytes") |
| 106 | } |
| 107 | |
| 108 | filePage := read(t, mustGet(t, anon, ts.URL+"/tim/widgets/-/blob/main.go?L=10-12")) |
| 109 | fileBlob := socialBlob(filePage) |
| 110 | for _, want := range []string{"main.go", "L10-marker", "L12-marker", "L10-12"} { |
| 111 | if !strings.Contains(fileBlob, want) { |
| 112 | t.Fatalf("bestand mist %q\n%s", want, fileBlob) |
| 113 | } |
| 114 | } |
| 115 | if strings.Contains(fileBlob, "L01-marker") || strings.Contains(fileBlob, "L25-marker") { |
| 116 | t.Fatalf("verkeerde regels:\n%s", fileBlob) |
| 117 | } |
| 118 | frag := srv.cardForPath(ctx, "/tim/widgets/-/blob/main.go", "", "#L10-12") |
| 119 | if !strings.Contains(frag.Description, "L10-marker") || strings.Contains(frag.Description, "L01-marker") { |
| 120 | t.Fatalf("fragment %q", frag.Description) |
| 121 | } |
| 122 | |
| 123 | commitPage := read(t, mustGet(t, anon, ts.URL+"/tim/widgets/-/commit/"+sha)) |
| 124 | if !strings.Contains(socialBlob(commitPage), "Fix the gate") { |
| 125 | t.Fatalf("commit:\n%s", socialBlob(commitPage)) |
| 126 | } |
| 127 | |
| 128 | git(t, work, "checkout", "-b", "feature") |
| 129 | writeFile(t, filepath.Join(work, "main.go"), "package main\nextra\n") |
| 130 | git(t, work, "add", "main.go") |
| 131 | git(t, work, "commit", "-m", "extra regel") |
| 132 | git(t, work, "push", "-u", "origin", "feature") |
| 133 | repo, err := st.RepoByPath(ctx, "tim", "widgets") |
| 134 | if err != nil { |
| 135 | t.Fatal(err) |
| 136 | } |
| 137 | mr, err := st.CreateMergeRequest(ctx, repo.ID, me.ID, "Fix the gate", "Eerste stuk van de merge request.", "feature", "master") |
| 138 | if err != nil { |
| 139 | t.Fatal(err) |
| 140 | } |
| 141 | pipeID, err := st.CreatePipeline(ctx, repo.ID, sha, "refs/heads/feature", "merge_request", "tim", "Fix the gate", []store.NewJob{{ |
| 142 | Name: "test", Stage: "test", Image: "alpine", Script: "true", |
| 143 | }}) |
| 144 | if err != nil { |
| 145 | t.Fatal(err) |
| 146 | } |
| 147 | if err := st.AttachPipelineMR(ctx, pipeID, mr.ID); err != nil { |
| 148 | t.Fatal(err) |
| 149 | } |
| 150 | mrPage := read(t, mustGet(t, anon, ts.URL+"/tim/widgets/-/merge-requests/1")) |
| 151 | mrBlob := socialBlob(mrPage) |
| 152 | for _, want := range []string{"!1 Fix the gate", "open", "feature → master", "tim", "Eerste stuk", "pending", "+2 −25"} { |
| 153 | if !strings.Contains(mrBlob, want) { |
| 154 | t.Fatalf("mr mist %q\n%s", want, mrBlob) |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | lab, err := st.UpsertLabel(ctx, repo.ID, "bug", "#428fdc") |
| 159 | if err != nil { |
| 160 | t.Fatal(err) |
| 161 | } |
| 162 | if _, err := st.CreateIssue(ctx, repo.ID, me.ID, store.NewIssue{ |
| 163 | Title: "Login fails", Description: "Het formulier weigert.", LabelIDs: []int64{lab.ID}, |
| 164 | }); err != nil { |
| 165 | t.Fatal(err) |
| 166 | } |
| 167 | issuePage := read(t, mustGet(t, anon, ts.URL+"/tim/widgets/-/issues/1")) |
| 168 | issueBlob := socialBlob(issuePage) |
| 169 | for _, want := range []string{"#1 Login fails", "open", "bug", "tim", "Het formulier weigert."} { |
| 170 | if !strings.Contains(issueBlob, want) { |
| 171 | t.Fatalf("issue mist %q\n%s", want, issueBlob) |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | if _, err := st.CreateIssue(ctx, repo.ID, me.ID, store.NewIssue{ |
| 176 | Title: "ZXQCONF", Description: "ZXQCONFBODY", Confidential: true, |
| 177 | }); err != nil { |
| 178 | t.Fatal(err) |
| 179 | } |
| 180 | secretIssue := read(t, mustGet(t, tim, ts.URL+"/tim/widgets/-/issues/2")) |
| 181 | if !strings.Contains(secretIssue, "ZXQCONF") { |
| 182 | t.Fatal("de auteur ziet het issue niet") |
| 183 | } |
| 184 | if strings.Contains(socialBlob(secretIssue), "ZXQCONF") || strings.Contains(socialBlob(secretIssue), "ZXQCONFBODY") { |
| 185 | t.Fatalf("vertrouwelijk lekt in de kaart:\n%s", socialBlob(secretIssue)) |
| 186 | } |
| 187 | if !strings.Contains(socialBlob(secretIssue), card.LockedTitle) { |
| 188 | t.Fatal("geen generieke kaart") |
| 189 | } |
| 190 | crawl := crawlerGet(t, tim, ts.URL+"/tim/widgets/-/issues/2") |
| 191 | if crawl.StatusCode != http.StatusOK { |
| 192 | t.Fatalf("crawler %d", crawl.StatusCode) |
| 193 | } |
| 194 | crawlBody := read(t, crawl) |
| 195 | if strings.Contains(crawlBody, "ZXQCONF") || strings.Contains(crawlBody, "ZXQCONFBODY") || !strings.Contains(crawlBody, card.LockedTitle) { |
| 196 | t.Fatalf("crawler-issue:\n%s", crawlBody) |
| 197 | } |
| 198 | |
| 199 | pipePage := read(t, mustGet(t, anon, ts.URL+"/tim/widgets/-/pipelines/"+strconv.FormatInt(pipeID, 10))) |
| 200 | if !strings.Contains(socialBlob(pipePage), "pending") || !strings.Contains(socialBlob(pipePage), "Fix the gate") { |
| 201 | t.Fatalf("pipeline:\n%s", socialBlob(pipePage)) |
| 202 | } |
| 203 | if _, err := st.CreateRelease(ctx, repo.ID, "v1.0.0", sha, "Eerste uitgave", "Een release voor de kaart.", me.ID, 0, nil); err != nil { |
| 204 | t.Fatal(err) |
| 205 | } |
| 206 | relPage := read(t, mustGet(t, anon, ts.URL+"/tim/widgets/-/releases")) |
| 207 | relBlob := socialBlob(relPage) |
| 208 | for _, want := range []string{"v1.0.0", "Eerste uitgave", "Een release voor de kaart."} { |
| 209 | if !strings.Contains(relBlob, want) { |
| 210 | t.Fatalf("release mist %q\n%s", want, relBlob) |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | g, err := st.CreateGroup(ctx, me.ID, "Kaartgroep", "kaartgroep", "Groep voor kaarten", access.RoleDeveloper) |
| 215 | if err != nil { |
| 216 | t.Fatal(err) |
| 217 | } |
| 218 | if err := st.UpdateGroup(ctx, g.ID, "Groep voor kaarten", "public", access.RoleDeveloper); err != nil { |
| 219 | t.Fatal(err) |
| 220 | } |
| 221 | if _, err := st.CreateRepo(ctx, "group", g.ID, "publiek", "een repo", "master", "public"); err != nil { |
| 222 | t.Fatal(err) |
| 223 | } |
| 224 | groupPage := read(t, mustGet(t, anon, ts.URL+"/groups/kaartgroep")) |
| 225 | groupBlob := socialBlob(groupPage) |
| 226 | for _, want := range []string{"Kaartgroep", "Groep voor kaarten", "1 repository"} { |
| 227 | if !strings.Contains(groupBlob, want) { |
| 228 | t.Fatalf("groep mist %q\n%s", want, groupBlob) |
| 229 | } |
| 230 | } |
| 231 | if _, err := st.CreateGroup(ctx, me.ID, "ZXQGROUP", "zxqgroup", "ZXQGROUPBODY", access.RoleDeveloper); err != nil { |
| 232 | t.Fatal(err) |
| 233 | } |
| 234 | hiddenGroup := crawlerGet(t, tim, ts.URL+"/groups/zxqgroup") |
| 235 | hiddenBody := read(t, hiddenGroup) |
| 236 | if hiddenGroup.StatusCode != http.StatusOK || strings.Contains(hiddenBody, "ZXQGROUP") || !strings.Contains(hiddenBody, card.LockedTitle) { |
| 237 | t.Fatalf("groep-crawler %d\n%s", hiddenGroup.StatusCode, hiddenBody) |
| 238 | } |
| 239 | |
| 240 | userPage := read(t, mustGet(t, anon, ts.URL+"/users/tim")) |
| 241 | if !strings.Contains(socialBlob(userPage), "tim") || !strings.Contains(socialBlob(userPage), "1 repository") { |
| 242 | t.Fatalf("gebruiker:\n%s", socialBlob(userPage)) |
| 243 | } |
| 244 | |
| 245 | page = read(t, mustGet(t, tim, ts.URL+"/new")) |
| 246 | read(t, mustPost(t, tim, ts.URL+"/new/repo", url.Values{ |
| 247 | "csrf": {csrfOf(t, page)}, "name": {"zxqpriv"}, "description": {"ZXQPRIVREADME"}, "visibility": {"private"}, |
| 248 | })) |
| 249 | privWork := t.TempDir() |
| 250 | git(t, privWork, "init", "-b", "master") |
| 251 | git(t, privWork, "config", "user.email", "tim@example") |
| 252 | git(t, privWork, "config", "user.name", "tim") |
| 253 | writeFile(t, filepath.Join(privWork, "README.md"), "ZXQPRIVREADME\n") |
| 254 | git(t, privWork, "add", ".") |
| 255 | git(t, privWork, "commit", "-m", "geheim") |
| 256 | privRemote := strings.Replace(ts.URL, "http://", "http://tim:"+token+"@", 1) + "/tim/zxqpriv.git" |
| 257 | git(t, privWork, "remote", "add", "origin", privRemote) |
| 258 | git(t, privWork, "push", "-u", "origin", "master") |
| 259 | |
| 260 | owned := read(t, mustGet(t, tim, ts.URL+"/tim/zxqpriv")) |
| 261 | if !strings.Contains(owned, "zxqpriv") { |
| 262 | t.Fatal("eigenaar ziet de repository niet") |
| 263 | } |
| 264 | ownedBlob := socialBlob(owned) |
| 265 | if strings.Contains(ownedBlob, "zxqpriv") || strings.Contains(ownedBlob, "ZXQPRIVREADME") || !strings.Contains(ownedBlob, card.LockedTitle) { |
| 266 | t.Fatalf("prive-kaart met cookie:\n%s", ownedBlob) |
| 267 | } |
| 268 | plain, err := anon.Get(ts.URL + "/tim/zxqpriv") |
| 269 | if err != nil { |
| 270 | t.Fatal(err) |
| 271 | } |
| 272 | if plain.StatusCode != http.StatusNotFound { |
| 273 | t.Fatalf("anoniem %d", plain.StatusCode) |
| 274 | } |
| 275 | plain.Body.Close() |
| 276 | bot := crawlerGet(t, anon, ts.URL+"/tim/zxqpriv") |
| 277 | botBody := read(t, bot) |
| 278 | if bot.StatusCode != http.StatusOK || strings.Contains(botBody, "zxqpriv") || strings.Contains(botBody, "ZXQPRIVREADME") || !strings.Contains(botBody, card.LockedTitle) { |
| 279 | t.Fatalf("crawler-prive %d\n%s", bot.StatusCode, botBody) |
| 280 | } |
| 281 | lockPNG := []byte(read(t, mustGet(t, tim, ts.URL+"/-/card.png?locked=1"))) |
| 282 | privPNG := []byte(read(t, mustGet(t, tim, ts.URL+"/-/card.png?path="+url.QueryEscape("/tim/zxqpriv")))) |
| 283 | if !bytes.Equal(lockPNG, privPNG) { |
| 284 | t.Fatal("prive-afbeelding wijkt af van de generieke kaart") |
| 285 | } |
| 286 | confPNG := []byte(read(t, mustGet(t, tim, ts.URL+"/-/card.png?path="+url.QueryEscape("/tim/widgets/-/issues/2")))) |
| 287 | if !bytes.Equal(lockPNG, confPNG) { |
| 288 | t.Fatal("vertrouwelijke afbeelding wijkt af") |
| 289 | } |
| 290 | |
| 291 | if _, err := st.CreateRepo(ctx, "user", me.ID, "zxqint", "ZXQINTBODY", "master", "private"); err != nil { |
| 292 | t.Fatal(err) |
| 293 | } |
| 294 | db, err := sql.Open("sqlite", "file:"+filepath.Join(storage, "bluegit.db")+"?_pragma=busy_timeout(5000)") |
| 295 | if err != nil { |
| 296 | t.Fatal(err) |
| 297 | } |
| 298 | defer db.Close() |
| 299 | if _, err := db.Exec(`UPDATE repos SET visibility = 'internal' WHERE name = 'zxqint'`); err != nil { |
| 300 | t.Fatal(err) |
| 301 | } |
| 302 | internalPage := read(t, mustGet(t, tim, ts.URL+"/tim/zxqint")) |
| 303 | internalBlob := socialBlob(internalPage) |
| 304 | if strings.Contains(internalBlob, "zxqint") || strings.Contains(internalBlob, "ZXQINTBODY") || !strings.Contains(internalBlob, card.LockedTitle) { |
| 305 | t.Fatalf("internal:\n%s", internalBlob) |
| 306 | } |
| 307 | bot = crawlerGet(t, tim, ts.URL+"/tim/zxqint") |
| 308 | botBody = read(t, bot) |
| 309 | if strings.Contains(botBody, "zxqint") || strings.Contains(botBody, "ZXQINTBODY") || !strings.Contains(botBody, card.LockedTitle) { |
| 310 | t.Fatalf("crawler-internal:\n%s", botBody) |
| 311 | } |
| 312 | |
| 313 | oem := read(t, mustGet(t, tim, ts.URL+"/api/v1/oembed?url="+url.QueryEscape("http://git.example/tim/widgets"))) |
| 314 | if !strings.Contains(oem, `"provider_name":"BlueGit"`) || !strings.Contains(oem, "tim/widgets") || !strings.Contains(oem, "Een widget die telt.") { |
| 315 | t.Fatalf("oembed:\n%s", oem) |
| 316 | } |
| 317 | oemPriv := read(t, mustGet(t, tim, ts.URL+"/api/v1/oembed?url="+url.QueryEscape("http://git.example/tim/zxqpriv"))) |
| 318 | if strings.Contains(oemPriv, "zxqpriv") || strings.Contains(oemPriv, "ZXQPRIVREADME") || !strings.Contains(oemPriv, card.LockedTitle) { |
| 319 | t.Fatalf("oembed prive:\n%s", oemPriv) |
| 320 | } |
| 321 | |
| 322 | info := read(t, mustGet(t, anon, ts.URL+"/-/info")) |
| 323 | for _, want := range []string{`id="previews"`, "Open Graph", card.LockedTitle, "#L10-20"} { |
| 324 | if !strings.Contains(info, want) { |
| 325 | t.Fatalf("info mist %q", want) |
| 326 | } |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | func twoDigits(n int) string { |
| 331 | if n < 10 { |
| 332 | return "0" + string(rune('0'+n)) |
| 333 | } |
| 334 | return string(rune('0'+n/10)) + string(rune('0'+n%10)) |
| 335 | } |
| 336 | |
| 337 | func socialBlob(page string) string { |
| 338 | var b strings.Builder |
| 339 | for _, key := range []string{"og:title", "og:description", "og:url", "og:image", "og:site_name", "twitter:title", "twitter:description", "twitter:image", "description", "theme-color"} { |
| 340 | b.WriteString(metaContent(page, "property", key)) |
| 341 | b.WriteString("\n") |
| 342 | b.WriteString(metaContent(page, "name", key)) |
| 343 | b.WriteString("\n") |
| 344 | } |
| 345 | const needle = `application/json+oembed" href="` |
| 346 | if i := strings.Index(page, needle); i >= 0 { |
| 347 | rest := page[i+len(needle):] |
| 348 | if j := strings.Index(rest, `"`); j >= 0 { |
| 349 | b.WriteString(rest[:j]) |
| 350 | } |
| 351 | } |
| 352 | return b.String() |
| 353 | } |
| 354 | |
| 355 | func metaContent(page, attr, key string) string { |
| 356 | tag := attr + `="` + key + `" content="` |
| 357 | i := strings.Index(page, tag) |
| 358 | if i < 0 { |
| 359 | return "" |
| 360 | } |
| 361 | rest := page[i+len(tag):] |
| 362 | j := strings.Index(rest, `"`) |
| 363 | if j < 0 { |
| 364 | return "" |
| 365 | } |
| 366 | return rest[:j] |
| 367 | } |
| 368 | |
| 369 | func crawlerGet(t *testing.T, c *http.Client, raw string) *http.Response { |
| 370 | t.Helper() |
| 371 | req, err := http.NewRequest(http.MethodGet, raw, nil) |
| 372 | if err != nil { |
| 373 | t.Fatal(err) |
| 374 | } |
| 375 | req.Header.Set("User-Agent", "Mozilla/5.0 (compatible; Discordbot/2.0; +https://discordapp.com)") |
| 376 | res, err := c.Do(req) |
| 377 | if err != nil { |
| 378 | t.Fatal(err) |
| 379 | } |
| 380 | return res |
| 381 | } |