oficina / calendar Public

Clone
git clone https://amadan.net/oficina/calendar

Plain git — no account needed to clone.

Download

Download this file

1package calendartest
2
3import (
4 "context"
5 "errors"
6 "strings"
7 "testing"
8
9 calendar "amadan.net/oficina/calendar/internal/calendar"
10)
11
12// A handle is lowercased on save, held by one person at a time, never a
13// reserved path segment, and cleared by saving nothing.
14func TestAHandleIsUniqueLowercaseAndNotReserved(t *testing.T) {
15 app := newApp(t)
16 paul := aHost(t, app, "paul@example.com")
17 other := aHost(t, app, "other@example.com")
18 store := app.StoreForTest()
19 ctx := context.Background()
20
21 if err := store.SaveHandle(ctx, paul.ID, "Paul"); err != nil {
22 t.Fatalf("SaveHandle(Paul): %v", err)
23 }
24 p, err := store.PersonByHandle(ctx, "paul")
25 if err != nil || p.ID != paul.ID {
26 t.Fatalf("PersonByHandle(paul) = %d, %v; want %d", p.ID, err, paul.ID)
27 }
28 if p.Handle == nil || *p.Handle != "paul" {
29 t.Errorf("saved handle = %v, want paul", p.Handle)
30 }
31 // The same handle again for the same person is not a conflict.
32 if err := store.SaveHandle(ctx, paul.ID, "paul"); err != nil {
33 t.Errorf("re-saving my own handle: %v", err)
34 }
35 for _, spelling := range []string{"paul", "PAUL"} {
36 if err := store.SaveHandle(ctx, other.ID, spelling); !errors.Is(err, calendar.ErrHandleTaken) {
37 t.Errorf("SaveHandle(other, %q) = %v, want ErrHandleTaken", spelling, err)
38 }
39 }
40
41 for _, bad := range []string{"m", "new", "static", "a", "has space", "üñí", strings.Repeat("x", 33)} {
42 err := store.SaveHandle(ctx, other.ID, bad)
43 if _, ok := calendar.Invalid(err); !ok {
44 t.Errorf("SaveHandle(%q) = %v, want a ValidationError", bad, err)
45 }
46 if err := calendar.ValidateHandle(bad); err == nil {
47 t.Errorf("ValidateHandle(%q) accepted it", bad)
48 }
49 }
50 if err := calendar.ValidateHandle("Paul-2"); err != nil {
51 t.Errorf("ValidateHandle(Paul-2): %v", err)
52 }
53
54 // Clearing frees the name for somebody else.
55 if err := store.SaveHandle(ctx, paul.ID, ""); err != nil {
56 t.Fatalf("SaveHandle(\"\"): %v", err)
57 }
58 if _, err := store.PersonByHandle(ctx, "paul"); !calendar.NotFound(err) {
59 t.Errorf("a cleared handle still resolves: %v", err)
60 }
61 if err := store.SaveHandle(ctx, other.ID, "paul"); err != nil {
62 t.Errorf("taking a freed handle: %v", err)
63 }
64}
65