package calendartest import ( "context" "errors" "strings" "testing" calendar "amadan.net/oficina/calendar/internal/calendar" ) // A handle is lowercased on save, held by one person at a time, never a // reserved path segment, and cleared by saving nothing. func TestAHandleIsUniqueLowercaseAndNotReserved(t *testing.T) { app := newApp(t) paul := aHost(t, app, "paul@example.com") other := aHost(t, app, "other@example.com") store := app.StoreForTest() ctx := context.Background() if err := store.SaveHandle(ctx, paul.ID, "Paul"); err != nil { t.Fatalf("SaveHandle(Paul): %v", err) } p, err := store.PersonByHandle(ctx, "paul") if err != nil || p.ID != paul.ID { t.Fatalf("PersonByHandle(paul) = %d, %v; want %d", p.ID, err, paul.ID) } if p.Handle == nil || *p.Handle != "paul" { t.Errorf("saved handle = %v, want paul", p.Handle) } // The same handle again for the same person is not a conflict. if err := store.SaveHandle(ctx, paul.ID, "paul"); err != nil { t.Errorf("re-saving my own handle: %v", err) } for _, spelling := range []string{"paul", "PAUL"} { if err := store.SaveHandle(ctx, other.ID, spelling); !errors.Is(err, calendar.ErrHandleTaken) { t.Errorf("SaveHandle(other, %q) = %v, want ErrHandleTaken", spelling, err) } } for _, bad := range []string{"m", "new", "static", "a", "has space", "üñí", strings.Repeat("x", 33)} { err := store.SaveHandle(ctx, other.ID, bad) if _, ok := calendar.Invalid(err); !ok { t.Errorf("SaveHandle(%q) = %v, want a ValidationError", bad, err) } if err := calendar.ValidateHandle(bad); err == nil { t.Errorf("ValidateHandle(%q) accepted it", bad) } } if err := calendar.ValidateHandle("Paul-2"); err != nil { t.Errorf("ValidateHandle(Paul-2): %v", err) } // Clearing frees the name for somebody else. if err := store.SaveHandle(ctx, paul.ID, ""); err != nil { t.Fatalf("SaveHandle(\"\"): %v", err) } if _, err := store.PersonByHandle(ctx, "paul"); !calendar.NotFound(err) { t.Errorf("a cleared handle still resolves: %v", err) } if err := store.SaveHandle(ctx, other.ID, "paul"); err != nil { t.Errorf("taking a freed handle: %v", err) } }