~gitpocket/gitpocket

4
bff19f1cf617d2869f06d2cc1add27ba1523281f
mainfeed-discovery
gitpocket/flow_test.go -rw-r--r--19.5 KiB ViewLogBlameView rawPermalink 3e6b98c68f feat(inbox): notification panel under the bell 6 days ago
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"image"
	"image/png"
	"net/http"
	"net/http/httptest"
	"net/url"
	"os"
	"os/exec"
	"path/filepath"
	"strings"
	"testing"
)

func testServer(t *testing.T) (*Server, http.Handler) {
	t.Helper()
	root := t.TempDir()
	store, err := openStore(filepath.Join(root, "gitpocket.db"), false)
	if err != nil {
		t.Fatal(err)
	}
	s := newServer(store, root, "example.org", "gitpocket", "git")
	return s, s.handler()
}

func post(h http.Handler, path string, form url.Values, cookie string) *httptest.ResponseRecorder {
	r := httptest.NewRequest("POST", path, strings.NewReader(form.Encode()))
	r.Header.Set("Content-Type", "application/x-www-form-urlencoded")
	r.Header.Set("Sec-Fetch-Site", "same-origin")
	r.RemoteAddr = "192.0.2.1:1000"
	if cookie != "" {
		r.Header.Set("Cookie", "session="+cookie)
	}
	w := httptest.NewRecorder()
	h.ServeHTTP(w, r)
	return w
}

func get(h http.Handler, path, cookie string) *httptest.ResponseRecorder {
	r := httptest.NewRequest("GET", path, nil)
	r.RemoteAddr = "192.0.2.1:1000"
	if cookie != "" {
		r.Header.Set("Cookie", "session="+cookie)
	}
	w := httptest.NewRecorder()
	h.ServeHTTP(w, r)
	return w
}

func session(w *httptest.ResponseRecorder) string {
	for _, c := range w.Result().Cookies() {
		if c.Name == "session" {
			return c.Value
		}
	}
	return ""
}

func TestRegistrationModes(t *testing.T) {
	s, h := testServer(t)
	if w := post(h, "/register", url.Values{"name": {"alice"}, "password": {"correct horse"}, "email": {"alice@example.org"}}, ""); w.Code != http.StatusSeeOther || session(w) == "" {
		t.Fatalf("open registration failed: %d", w.Code)
	}
	s.store.SetSetting("registration", "invite")
	if w := post(h, "/register", url.Values{"name": {"bob"}, "password": {"correct horse"}, "email": {"bob@example.org"}}, ""); w.Code != 400 {
		t.Errorf("invite mode let bob in without a code: %d", w.Code)
	}
	code, _ := s.store.CreateInvite()
	if w := post(h, "/register", url.Values{"name": {"bob"}, "password": {"correct horse"}, "email": {"bob@example.org"}, "invite": {code}}, ""); w.Code != http.StatusSeeOther {
		t.Errorf("invite mode refused a valid code: %d", w.Code)
	}
	s.store.SetSetting("registration", "closed")
	if w := post(h, "/register", url.Values{"name": {"carol"}, "password": {"correct horse"}, "email": {"carol@example.org"}}, ""); w.Code != 403 {
		t.Errorf("closed mode allowed registration: %d", w.Code)
	}
	if w := get(h, "/", ""); strings.Contains(w.Body.String(), `href="/register"`) {
		t.Error("closed mode still links to registration")
	}
}

func TestLoginAndHeaders(t *testing.T) {
	s, h := testServer(t)
	s.store.CreateUser("alice", "correct horse")
	if w := post(h, "/login", url.Values{"name": {"alice"}, "password": {"wrong password"}}, ""); w.Code != 401 {
		t.Errorf("wrong password gave %d", w.Code)
	}
	w := post(h, "/login", url.Values{"name": {"alice"}, "password": {"correct horse"}, "email": {"alice@example.org"}}, "")
	if session(w) == "" {
		t.Fatal("login did not start a session")
	}
	for _, header := range []string{"Content-Security-Policy", "X-Frame-Options", "X-Content-Type-Options"} {
		if w.Header().Get(header) == "" {
			t.Errorf("missing %s", header)
		}
	}
}

func TestCrossOriginPostRefused(t *testing.T) {
	s, h := testServer(t)
	u, _ := s.store.CreateUser("alice", "correct horse")
	id, _ := s.store.CreateSession(u.ID)
	r := httptest.NewRequest("POST", "/settings/profile", strings.NewReader("display=pwned"))
	r.Header.Set("Content-Type", "application/x-www-form-urlencoded")
	r.Header.Set("Sec-Fetch-Site", "cross-site")
	r.Header.Set("Cookie", "session="+id)
	w := httptest.NewRecorder()
	h.ServeHTTP(w, r)
	if w.Code != 403 || s.store.Profile(u.ID).Display == "pwned" {
		t.Errorf("cross site post accepted: %d", w.Code)
	}
}

func TestPermissions(t *testing.T) {
	s, h := testServer(t)
	alice, _ := s.store.CreateUser("alice", "correct horse")
	bob, _ := s.store.CreateUser("bob", "correct horse")
	secret, err := s.store.CreateRepo(alice, "secret", "", Private, "")
	if err != nil {
		t.Fatal(err)
	}
	os.MkdirAll(filepath.Dir(s.repoDir(secret)), 0o755)
	initRepo(s.repoDir(secret))
	if w := get(h, "/~alice/secret", ""); w.Code != 404 {
		t.Errorf("anonymous read of a private repo: %d", w.Code)
	}
	bobSession, _ := s.store.CreateSession(bob.ID)
	if w := get(h, "/~alice/secret", bobSession); w.Code != 404 {
		t.Errorf("stranger read a private repo: %d", w.Code)
	}
	if s.store.CanWrite(secret, &bob) {
		t.Error("stranger can write")
	}
	org, _ := s.store.CreateOrg("team")
	s.store.SetMember(org.ID, bob.ID, true)
	shared, _ := s.store.CreateRepo(org, "shared", "", Private, "")
	if !s.store.CanWrite(shared, &bob) || !s.store.Owns(shared, &bob) || s.store.CanWrite(shared, &alice) {
		t.Error("org membership not respected")
	}
	if _, ok := s.store.CheckPassword("team", ""); ok {
		t.Error("an organisation can log in")
	}
	if w := post(h, "/settings/profile", url.Values{"org": {"team"}, "display": {"x"}}, ""); w.Code != http.StatusSeeOther {
		t.Errorf("anonymous org edit not redirected to login: %d", w.Code)
	}
	aliceSession, _ := s.store.CreateSession(alice.ID)
	if w := post(h, "/settings/profile", url.Values{"org": {"team"}, "display": {"x"}}, aliceSession); w.Code != 403 {
		t.Errorf("non member edited an org: %d", w.Code)
	}
	s.store.AddRedirect("alice", "old", shared.ID)
	if w := get(h, "/~alice/old", ""); w.Code != 404 {
		t.Errorf("redirect leaked a private repo: %d", w.Code)
	}
	if w := get(h, "/~alice/old", bobSession); w.Code != http.StatusMovedPermanently || w.Header().Get("Location") != "/~team/shared" {
		t.Errorf("redirect for a member: %d %s", w.Code, w.Header().Get("Location"))
	}
	if w := get(h, "/admin", bobSession); w.Code != 404 {
		t.Errorf("admin page open to a normal user: %d", w.Code)
	}
}

func TestMergeStrategies(t *testing.T) {
	if _, err := exec.LookPath("git"); err != nil {
		t.Skip("git not installed")
	}
	dir := filepath.Join(t.TempDir(), "r.git")
	if err := initRepo(dir); err != nil {
		t.Fatal(err)
	}
	g := Git{Dir: dir}
	base := commitFile(t, g, "main", "", "a.txt", "one\n")
	feature := commitFile(t, g, "feature", base, "b.txt", "two\n")
	if !g.Mergeable("main", feature) {
		t.Fatal("clean merge reported as conflicting")
	}
	if _, err := g.Merge("main", feature, "merge", "T", "t@example.org"); err != nil {
		t.Fatal(err)
	}
	if _, err := g.Merge("main", feature, "merge", "T", "t@example.org"); err == nil {
		t.Error("merged the same head twice")
	}
	left := commitFile(t, g, "left", base, "a.txt", "left\n")
	right := commitFile(t, g, "right", base, "a.txt", "right\n")
	if _, err := g.mergeTree(base, left, right); err == nil {
		t.Error("conflict not detected")
	}
}

func commitFile(t *testing.T, g Git, branch, parent, name, content string) string {
	t.Helper()
	blob, err := g.run([]byte(content), "hash-object", "-w", "--stdin")
	if err != nil {
		t.Fatal(err)
	}
	index := filepath.Join(t.TempDir(), "index")
	env := []string{"GIT_INDEX_FILE=" + index}
	if parent != "" {
		g.runEnv(env, nil, "read-tree", parent)
	}
	if _, err := g.runEnv(env, nil, "update-index", "--add", "--cacheinfo", "100644,"+strings.TrimSpace(string(blob))+","+name); err != nil {
		t.Fatal(err)
	}
	tree, _ := g.runEnv(env, nil, "write-tree")
	args := []string{"commit-tree", strings.TrimSpace(string(tree))}
	if parent != "" {
		args = append(args, "-p", parent)
	}
	out, err := g.runEnv(ident("T", "t@example.org", timeNow()), []byte("c"), args...)
	if err != nil {
		t.Fatal(err)
	}
	hash := strings.TrimSpace(string(out))
	g.run(nil, "update-ref", "refs/heads/"+branch, hash)
	return hash
}

func TestWorkplaceLink(t *testing.T) {
	s, h := testServer(t)
	alice, _ := s.store.CreateUser("alice", "correct horse")
	s.store.CreateOrg("team")
	s.store.UpdateProfile(alice.ID, Profile{Workplace: "@Team"})
	if body := get(h, "/~alice", "").Body.String(); !strings.Contains(body, `<a href="/~team">@Team</a>`) {
		t.Error("workplace naming an org is not linked")
	}
	s.store.UpdateProfile(alice.ID, Profile{Workplace: "Nowhere Ltd"})
	if body := get(h, "/~alice", "").Body.String(); !strings.Contains(body, "Nowhere Ltd") || strings.Contains(body, `">Nowhere Ltd</a>`) {
		t.Error("unknown workplace should be plain text")
	}
}

func TestSignupEmail(t *testing.T) {
	s, h := testServer(t)
	if w := post(h, "/register", url.Values{"name": {"bob"}, "password": {"correct horse"}}, ""); w.Code != 400 {
		t.Errorf("registration without an email allowed: %d", w.Code)
	}
	if w := post(h, "/register", url.Values{"name": {"bob"}, "password": {"correct horse"}, "email": {"b@example.org"}}, ""); w.Code != http.StatusSeeOther || session(w) == "" {
		t.Errorf("registration with an email failed: %d", w.Code)
	}
	bob, _ := s.store.UserByName("bob")
	if a := s.store.Account(bob.ID); a.Email != "b@example.org" || a.Verified {
		t.Error("email not stored unverified")
	}
}

func TestAdminSections(t *testing.T) {
	s, h := testServer(t)
	admin, _ := s.store.CreateUser("root", "correct horse")
	s.store.SetAdmin("root", true)
	id, _ := s.store.CreateSession(admin.ID)
	for _, sec := range []string{"overview", "people", "signup", "projects", "storage", "bogus"} {
		if w := get(h, "/admin?s="+sec, id); w.Code != 200 {
			t.Errorf("section %s: %d", sec, w.Code)
		}
	}
	if w := post(h, "/admin", url.Values{"s": {"signup"}, "action": {"signup"}, "mode": {"closed"}}, id); w.Code != http.StatusSeeOther || s.store.Registration() != "closed" {
		t.Errorf("registration mode not saved: %d %s", w.Code, s.store.Registration())
	}
	if w := post(h, "/admin", url.Values{"s": {"signup"}, "action": {"signup"}, "mode": {"sometimes"}}, id); !strings.Contains(w.Header().Get("Location"), "error") {
		t.Error("bad registration mode accepted")
	}
}

func TestPins(t *testing.T) {
	s, h := testServer(t)
	alice, _ := s.store.CreateUser("alice", "correct horse")
	mallory, _ := s.store.CreateUser("mallory", "correct horse")
	var ids []int64
	for _, n := range []string{"a", "b", "c", "d", "e", "f", "g"} {
		r, _ := s.store.CreateRepo(alice, n, "", Public, "")
		ids = append(ids, r.ID)
	}
	if strings.Contains(get(h, "/~alice", "").Body.String(), `href="/~alice/a"`) {
		t.Error("unpinned repos shown on the profile")
	}
	own, _ := s.store.CreateSession(alice.ID)
	other, _ := s.store.CreateSession(mallory.ID)
	names := map[int64]string{}
	for i, id := range ids {
		names[id] = string(rune('a' + i))
	}
	pin := func(id int64, cookie string) *httptest.ResponseRecorder {
		return post(h, "/~alice/"+names[id]+"/settings", url.Values{"visibility": {"public"}, "pinned": {"on"}}, cookie)
	}
	if pin(ids[0], other); len(s.store.Pins(alice.ID)) != 0 {
		t.Error("someone else pinned alice's repo")
	}
	pin(ids[2], own)
	pin(ids[0], own)
	body := get(h, "/~alice", "").Body.String()
	if !strings.Contains(body, `href="/~alice/c"`) || strings.Index(body, `href="/~alice/c"`) > strings.Index(body, `href="/~alice/a"`) || strings.Contains(body, `href="/~alice/b"`) {
		t.Error("profile does not show exactly the pinned repos in pin order")
	}
	for _, id := range ids[1:] {
		pin(id, own)
	}
	if n := len(s.store.Pins(alice.ID)); n != maxPins {
		t.Errorf("%d pins, want the cap of %d", n, maxPins)
	}
	post(h, "/~alice/a/settings", url.Values{"visibility": {"public"}}, own)
	if s.store.Pins(alice.ID)[ids[0]] != 0 {
		t.Error("unticking the box did not unpin")
	}
	if w := get(h, "/~alice/repos", ""); w.Code != 200 || !strings.Contains(w.Body.String(), `href="/~alice/g"`) {
		t.Error("repos page does not list every repo")
	}
}

func TestProfileReadme(t *testing.T) {
	if _, err := exec.LookPath("git"); err != nil {
		t.Skip("git not installed")
	}
	s, h := testServer(t)
	alice, _ := s.store.CreateUser("alice", "correct horse")
	repo, _ := s.store.CreateRepo(alice, "alice", "", Public, "")
	g := Git{Dir: s.repoDir(repo)}
	if err := initRepo(g.Dir); err != nil {
		t.Fatal(err)
	}
	commitFile(t, g, g.Head(), "", "README.md", "# Hello\n\n<script>alert(1)</script>\n")
	body := get(h, "/~alice", "").Body.String()
	if !strings.Contains(body, `class="intro"`) || !strings.Contains(body, "Hello</h1>") || strings.Contains(body, "<script>alert") {
		t.Error("profile README not rendered safely")
	}
	s.store.UpdateRepo(repo.ID, "", Private, "")
	if strings.Contains(get(h, "/~alice", "").Body.String(), `class="intro"`) {
		t.Error("private profile README shown")
	}
}

func TestBranchPushAndPullMerge(t *testing.T) {
	if _, err := exec.LookPath("git"); err != nil {
		t.Skip("git not installed")
	}
	s, h := testServer(t)
	alice, _ := s.store.CreateUser("alice", "correct horse")
	repo, _ := s.store.CreateRepo(alice, "proj", "", Public, "")
	g := Git{Dir: s.repoDir(repo)}
	if err := initRepo(g.Dir); err != nil {
		t.Fatal(err)
	}
	base := commitFile(t, g, g.Head(), "", "a.txt", "one\n")
	feature := commitFile(t, g, "feature", base, "b.txt", "two\n")
	own, _ := s.store.CreateSession(alice.ID)
	if body := get(h, "/~alice/proj", own).Body.String(); !strings.Contains(body, `class="pushnote"`) || !strings.Contains(body, "<strong>feature</strong>") {
		t.Fatal("recent push notice missing for the owner")
	}
	if strings.Contains(get(h, "/~alice/proj", "").Body.String(), `class="pushnote"`) {
		t.Error("recent push notice shown to a visitor")
	}
	if body := get(h, "/~alice/proj/tree/feature/", own).Body.String(); !strings.Contains(body, `class="branchpick"`) || !strings.Contains(body, `href="/~alice/proj/tree/main/"`) {
		t.Error("branch switcher missing")
	}
	w := post(h, "/~alice/proj/pulls/new", url.Values{"title": {"Add b"}, "source": {"~alice/proj:feature"}, "target": {g.Head()}}, own)
	if w.Code != http.StatusSeeOther {
		t.Fatalf("opening the pull request failed: %d %s", w.Code, w.Body.String())
	}
	if strings.Contains(get(h, "/~alice/proj", own).Body.String(), `class="pushnote"`) {
		t.Error("notice still shown after a pull request was opened")
	}
	if w := post(h, "/~alice/proj/pulls/1/merge", url.Values{"strategy": {"merge"}, "title": {"Merge feature"}}, own); w.Code != http.StatusSeeOther {
		t.Fatalf("merge failed: %d", w.Code)
	}
	head, _ := g.Resolve(g.Head())
	if out, err := g.run(nil, "merge-base", "--is-ancestor", feature, head); err != nil {
		t.Errorf("main does not contain the feature branch after merging: %s", out)
	}
	if body := get(h, "/~alice/proj/pulls/1", own).Body.String(); !strings.Contains(strings.ToLower(body), "merged") {
		t.Error("pull request not shown as merged")
	}
}

func TestAppearance(t *testing.T) {
	s, h := testServer(t)
	alice, _ := s.store.CreateUser("alice", "correct horse")
	c, _ := s.store.CreateSession(alice.ID)
	if strings.Contains(get(h, "/", c).Body.String(), "data-theme") {
		t.Error("theme set before choosing one")
	}
	if w := post(h, "/settings/appearance", url.Values{"theme": {"dark"}}, c); w.Code != http.StatusSeeOther {
		t.Fatalf("saving theme gave %d", w.Code)
	}
	if !strings.Contains(get(h, "/", c).Body.String(), `data-theme="dark"`) {
		t.Error("dark theme not applied")
	}
	post(h, "/settings/appearance", url.Values{"theme": {`x" onload="`}}, c)
	if !strings.Contains(get(h, "/", c).Body.String(), `data-theme="dark"`) {
		t.Error("unknown theme replaced the saved one")
	}
	if strings.Contains(get(h, "/", "").Body.String(), "data-theme") {
		t.Error("theme leaked to logged out visitors")
	}
}

func TestAttachments(t *testing.T) {
	s, h := testServer(t)
	alice, _ := s.store.CreateUser("alice", "correct horse")
	c, _ := s.store.CreateSession(alice.ID)
	send := func(body []byte, cookie string) *httptest.ResponseRecorder {
		r := httptest.NewRequest("POST", "/attachments", bytes.NewReader(body))
		r.Header.Set("Sec-Fetch-Site", "same-origin")
		r.RemoteAddr = "192.0.2.1:1000"
		if cookie != "" {
			r.Header.Set("Cookie", "session="+cookie)
		}
		w := httptest.NewRecorder()
		h.ServeHTTP(w, r)
		return w
	}
	var img bytes.Buffer
	png.Encode(&img, image.NewRGBA(image.Rect(0, 0, 4, 4)))
	if w := send(img.Bytes(), ""); w.Code != http.StatusUnauthorized {
		t.Errorf("anonymous upload gave %d", w.Code)
	}
	if w := send([]byte("<svg onload=alert(1)>"), c); w.Code != http.StatusUnsupportedMediaType {
		t.Errorf("non image accepted: %d", w.Code)
	}
	w := send(img.Bytes(), c)
	var got struct{ URL string }
	if w.Code != 200 || json.NewDecoder(w.Body).Decode(&got) != nil || !strings.HasSuffix(got.URL, ".png") {
		t.Fatalf("upload gave %d %q", w.Code, got.URL)
	}
	served := get(h, got.URL, "")
	if served.Code != 200 || served.Header().Get("Content-Type") != "image/png" || !strings.Contains(served.Header().Get("Content-Security-Policy"), "sandbox") || !bytes.Equal(served.Body.Bytes(), img.Bytes()) {
		t.Errorf("attachment served as %d %v", served.Code, served.Header())
	}
	if w := get(h, "/attachments/..%2fgitpocket.db", ""); w.Code != 404 {
		t.Errorf("traversal gave %d", w.Code)
	}
	p := post(h, "/preview", url.Values{"body": {"**hi** <script>x</script> ![a](" + got.URL + ")"}}, c)
	if body := p.Body.String(); !strings.Contains(body, "<strong>hi</strong>") || strings.Contains(body, "<script") || !strings.Contains(body, `src="`+got.URL+`"`) {
		t.Errorf("preview gave %q", body)
	}
}

func TestNotifications(t *testing.T) {
	s, h := testServer(t)
	alice, _ := s.store.CreateUser("alice", "correct horse")
	bob, _ := s.store.CreateUser("bob", "correct horse")
	carol, _ := s.store.CreateUser("carol", "correct horse")
	repo, _ := s.store.CreateRepo(alice, "proj", "", Public, "")
	repo, _ = s.store.RepoByID(repo.ID)
	issue, err := s.store.CreateIssue(repo, bob, "Crash on start", "")
	if err != nil {
		t.Fatal(err)
	}
	watch := func() []int64 { return s.store.Watchers(repo, "issue", issue.ID, issue.Author.ID) }
	s.store.Notify(watch(), bob, repo, "issue", issue.Number, issue.Title, "opened")
	ca, _ := s.store.CreateSession(alice.ID)
	cb, _ := s.store.CreateSession(bob.ID)
	cc, _ := s.store.CreateSession(carol.ID)
	if body := get(h, "/notifications", ca).Body.String(); !strings.Contains(body, "~bob opened this") || !strings.Contains(body, `class="badge">1<`) {
		t.Fatalf("owner not notified:\n%s", body)
	}
	if strings.Contains(get(h, "/", cb).Body.String(), `class="badge"`) {
		t.Error("actor notified about their own action")
	}
	s.store.AddIssueComment(issue.ID, carol, "same here")
	s.store.Notify(watch(), carol, repo, "issue", issue.Number, issue.Title, "commented on")
	if n := s.store.Notifications(alice.ID, 100); len(n) != 1 || n[0].Actor != "carol" || !n[0].Unread {
		t.Errorf("thread not collapsed into one entry: %+v", n)
	}
	s.store.Notify(watch(), alice, repo, "issue", issue.Number, issue.Title, "closed")
	if n := s.store.Notifications(carol.ID, 100); len(n) != 1 || n[0].Verb != "closed" {
		t.Errorf("participant not notified: %+v", n)
	}
	get(h, fmt.Sprintf("/%s/issues/%d", repo.Path(), issue.Number), cb)
	if n := s.store.Notifications(bob.ID, 100); len(n) != 1 || n[0].Unread {
		t.Errorf("viewing the issue left it unread: %+v", n)
	}
	if w := post(h, "/notifications", url.Values{}, cc); w.Code != http.StatusSeeOther || s.store.Notifications(carol.ID, 100)[0].Unread {
		t.Error("mark all as read did nothing")
	}
	s.store.UpdateRepo(repo.ID, "", Private, "")
	if n := s.store.Notifications(carol.ID, 100); len(n) != 0 {
		t.Errorf("private repository leaked through notifications: %+v", n)
	}
}

func TestProfileLinks(t *testing.T) {
	s, h := testServer(t)
	alice, _ := s.store.CreateUser("alice", "correct horse")
	c, _ := s.store.CreateSession(alice.ID)
	form := url.Values{"display": {"Alice"}, "website": {"example.org", "", "https://a.example/x", "http://b.example", "c.example", "d.example"}}
	if w := post(h, "/settings/profile", form, c); w.Code != http.StatusSeeOther {
		t.Fatalf("saving profile gave %d", w.Code)
	}
	links := s.store.Profile(alice.ID).Links()
	if len(links) != 4 || links[0] != "https://example.org" || links[2] != "http://b.example" {
		t.Errorf("links stored as %q", links)
	}
	if body := get(h, "/~alice", "").Body.String(); strings.Count(body, `rel="nofollow me"`) != 4 {
		t.Error("profile does not show four links")
	}
}