| new file mode 100644 |
| index 000000000..0313e0606 |
| --- /dev/null |
| +++ b/internal/console/lastseen_test.go |
| @@ -0,0 +1,248 @@ |
| +package console |
| + |
| +import ( |
| + "net/http" |
| + "sync" |
| + "testing" |
| + "time" |
| + |
| + "github.com/carlosframework/platform/internal/svcrole" |
| +) |
| + |
| +// lastSeenPaul is the fixture person's address: the one signInPaul signs in. |
| +const lastSeenPaul = "paul@keymail.dev" |
| + |
| +// lastSeenOf reads address's stored last_seen (Unix seconds); ok is false |
| +// when there is no users row. |
| +func lastSeenOf(t *testing.T, fx *consoleFixture, address string) (seen int64, ok bool) { |
| + t.Helper() |
| + err := fx.st.db.QueryRow(`SELECT last_seen FROM users WHERE address = ?`, address).Scan(&seen) |
| + if err != nil { |
| + return 0, false |
| + } |
| + return seen, true |
| +} |
| + |
| +// expireSeen makes address's reserved hour an hour and a minute old, as if |
| +// that much time had passed since the last write. moveBack also moves the |
| +// stored last_seen back an hour, so the next write can be seen moving it |
| +// (last_seen is whole seconds and the test runs inside one). |
| +func expireSeen(t *testing.T, fx *consoleFixture, address string, moveBack bool) { |
| + t.Helper() |
| + fx.srv.seenMu.Lock() |
| + fx.srv.seen[address] = time.Now().Add(-61 * time.Minute) |
| + fx.srv.seenMu.Unlock() |
| + if moveBack { |
| + if _, err := fx.st.db.Exec(`UPDATE users SET last_seen = last_seen - 3600 WHERE address = ?`, address); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| +} |
| + |
| +func getKass(t *testing.T, fx *consoleFixture, c *http.Client) int { |
| + t.Helper() |
| + status, _, _ := get(t, c, fx.u("/apps/kass")) |
| + return status |
| +} |
| + |
| +// TestLastSeenIsWrittenOncePerHour: signing in writes last_seen, browsing |
| +// within the hour writes nothing more, and the first request after the hour |
| +// writes again and moves it. |
| +func TestLastSeenIsWrittenOncePerHour(t *testing.T) { |
| + fx := newConsoleFixture(t) |
| + c := signInPaul(t, fx) |
| + if n := fx.srv.lastSeenWrites.Load(); n != 1 { |
| + t.Fatalf("lastSeenWrites after sign-in = %d, want 1", n) |
| + } |
| + first, ok := lastSeenOf(t, fx, lastSeenPaul) |
| + if !ok || first == 0 { |
| + t.Fatalf("last_seen after sign-in = %d (row %v), want set", first, ok) |
| + } |
| + for i := 0; i < 10; i++ { |
| + if s := getKass(t, fx, c); s != http.StatusOK { |
| + t.Fatalf("GET /apps/kass = %d", s) |
| + } |
| + } |
| + if n := fx.srv.lastSeenWrites.Load(); n != 1 { |
| + t.Fatalf("lastSeenWrites after ten GETs = %d, want 1", n) |
| + } |
| + expireSeen(t, fx, lastSeenPaul, true) |
| + before, _ := lastSeenOf(t, fx, lastSeenPaul) |
| + if s := getKass(t, fx, c); s != http.StatusOK { |
| + t.Fatalf("GET /apps/kass = %d", s) |
| + } |
| + if n := fx.srv.lastSeenWrites.Load(); n != 2 { |
| + t.Fatalf("lastSeenWrites after the hour = %d, want 2", n) |
| + } |
| + if after, _ := lastSeenOf(t, fx, lastSeenPaul); after <= before { |
| + t.Fatalf("last_seen after the hour = %d, want past %d", after, before) |
| + } |
| +} |
| + |
| +// TestLastSeenReservationIsAtomic: once the hour has passed, a burst of |
| +// requests (and a burst of direct calls) writes exactly once. Run with -race. |
| +func TestLastSeenReservationIsAtomic(t *testing.T) { |
| + fx := newConsoleFixture(t) |
| + c := signInPaul(t, fx) |
| + const n = 64 |
| + |
| + expireSeen(t, fx, lastSeenPaul, false) |
| + base := fx.srv.lastSeenWrites.Load() |
| + start := make(chan struct{}) |
| + var wg sync.WaitGroup |
| + statuses := make([]int, n) |
| + for i := 0; i < n; i++ { |
| + wg.Add(1) |
| + go func(i int) { |
| + defer wg.Done() |
| + <-start |
| + res, err := c.Get(fx.u("/apps/kass")) |
| + if err != nil { |
| + return |
| + } |
| + res.Body.Close() |
| + statuses[i] = res.StatusCode |
| + }(i) |
| + } |
| + close(start) |
| + wg.Wait() |
| + for i, s := range statuses { |
| + if s != http.StatusOK { |
| + t.Fatalf("GET %d = %d", i, s) |
| + } |
| + } |
| + if got := fx.srv.lastSeenWrites.Load() - base; got != 1 { |
| + t.Fatalf("%d concurrent GETs after the hour wrote %d times, want 1", n, got) |
| + } |
| + |
| + expireSeen(t, fx, lastSeenPaul, false) |
| + base = fx.srv.lastSeenWrites.Load() |
| + now := time.Now() |
| + start = make(chan struct{}) |
| + for i := 0; i < n; i++ { |
| + wg.Add(1) |
| + go func() { |
| + defer wg.Done() |
| + <-start |
| + fx.srv.noteSeen(lastSeenPaul, now) |
| + }() |
| + } |
| + close(start) |
| + wg.Wait() |
| + if got := fx.srv.lastSeenWrites.Load() - base; got != 1 { |
| + t.Fatalf("%d concurrent noteSeen calls after the hour wrote %d times, want 1", n, got) |
| + } |
| +} |
| + |
| +// TestLastSeenFromTheCLI: a person's CLI token counts as being seen, through |
| +// both a cliRoute door and a cliUser door; a service credential is not a |
| +// person and writes nothing. |
| +func TestLastSeenFromTheCLI(t *testing.T) { |
| + fx := newConsoleFixture(t) |
| + tok := cliToken(t, fx, lastSeenPaul, "code-"+t.Name()) |
| + |
| + for _, door := range []string{"/api/cli/apps", "/auth/cli/whoami"} { |
| + expireSeen(t, fx, lastSeenPaul, true) |
| + before, _ := lastSeenOf(t, fx, lastSeenPaul) |
| + base := fx.srv.lastSeenWrites.Load() |
| + if status, _ := getJSON(t, fx.ts.URL+door, tok); status != http.StatusOK { |
| + t.Fatalf("GET %s with Paul's CLI token = %d", door, status) |
| + } |
| + if got := fx.srv.lastSeenWrites.Load() - base; got != 1 { |
| + t.Fatalf("GET %s with Paul's CLI token wrote %d times, want 1", door, got) |
| + } |
| + if after, _ := lastSeenOf(t, fx, lastSeenPaul); after <= before { |
| + t.Fatalf("GET %s: last_seen = %d, want past %d", door, after, before) |
| + } |
| + } |
| + |
| + // A service credential: its request reaches the door (or is refused at |
| + // the person-only one) and writes nothing for anyone. |
| + svc := serviceToken(t, fx.st, OpsAccountID, "hub", "", svcrole.Publish) |
| + expireSeen(t, fx, lastSeenPaul, false) |
| + snapshot := func() map[string]int64 { |
| + rows, err := fx.st.db.Query(`SELECT address, last_seen FROM users`) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + defer rows.Close() |
| + out := map[string]int64{} |
| + for rows.Next() { |
| + var a string |
| + var s int64 |
| + if err := rows.Scan(&a, &s); err != nil { |
| + t.Fatal(err) |
| + } |
| + out[a] = s |
| + } |
| + return out |
| + } |
| + before := snapshot() |
| + base := fx.srv.lastSeenWrites.Load() |
| + if status, _ := getJSON(t, fx.ts.URL+"/api/cli/apps", svc); status != http.StatusOK { |
| + t.Fatalf("GET /api/cli/apps with a publish credential = %d", status) |
| + } |
| + if status, _ := getJSON(t, fx.ts.URL+"/auth/cli/whoami", svc); status != http.StatusUnauthorized { |
| + t.Fatalf("GET /auth/cli/whoami with a service credential = %d, want 401", status) |
| + } |
| + if got := fx.srv.lastSeenWrites.Load() - base; got != 0 { |
| + t.Fatalf("service credential requests wrote %d times, want 0", got) |
| + } |
| + after := snapshot() |
| + if len(after) != len(before) { |
| + t.Fatalf("users rows %v after a service credential's requests, was %v", after, before) |
| + } |
| + for a, s := range before { |
| + if after[a] != s { |
| + t.Fatalf("%s last_seen moved %d -> %d on a service credential's request", a, s, after[a]) |
| + } |
| + } |
| +} |
| + |
| +// TestLastSeenWithNoUserRowIsHarmless: a session whose person has no users |
| +// row still answers as it does today; the write creates no row, and the |
| +// hour stays reserved so the next request does not try again. |
| +func TestLastSeenWithNoUserRowIsHarmless(t *testing.T) { |
| + fx := newConsoleFixture(t) |
| + c := signInPaul(t, fx) |
| + want := getKass(t, fx, c) |
| + if _, err := fx.st.db.Exec(`DELETE FROM users WHERE address = ?`, lastSeenPaul); err != nil { |
| + t.Fatal(err) |
| + } |
| + fx.srv.seenMu.Lock() |
| + clear(fx.srv.seen) |
| + fx.srv.seenMu.Unlock() |
| + base := fx.srv.lastSeenWrites.Load() |
| + if got := getKass(t, fx, c); got != want { |
| + t.Fatalf("GET /apps/kass with no users row = %d, want %d as before", got, want) |
| + } |
| + if got := fx.srv.lastSeenWrites.Load() - base; got != 1 { |
| + t.Fatalf("lastSeenWrites = +%d, want +1", got) |
| + } |
| + // The page's own landing-spot upsert (SetLastAccount) has always made a |
| + // users row for a session without one, and it runs after the gate. The |
| + // touch ran first, against no row, so a row the page made carries no |
| + // last_seen. |
| + if seen, ok := lastSeenOf(t, fx, lastSeenPaul); ok && seen != 0 { |
| + t.Fatalf("last_seen = %d on a row the touch found missing, want 0", seen) |
| + } |
| + if got := getKass(t, fx, c); got != want { |
| + t.Fatalf("second GET /apps/kass = %d, want %d", got, want) |
| + } |
| + if got := fx.srv.lastSeenWrites.Load() - base; got != 1 { |
| + t.Fatalf("lastSeenWrites after a second GET within the hour = +%d, want +1", got) |
| + } |
| + |
| + // The touch on its own, with no page after it, creates no users row. |
| + if _, err := fx.st.db.Exec(`DELETE FROM users WHERE address = ?`, lastSeenPaul); err != nil { |
| + t.Fatal(err) |
| + } |
| + expireSeen(t, fx, lastSeenPaul, false) |
| + fx.srv.noteSeen(lastSeenPaul, time.Now()) |
| + if got := fx.srv.lastSeenWrites.Load() - base; got != 2 { |
| + t.Fatalf("lastSeenWrites after a direct noteSeen past the hour = +%d, want +2", got) |
| + } |
| + if _, ok := lastSeenOf(t, fx, lastSeenPaul); ok { |
| + t.Fatal("touching last_seen created a users row") |
| + } |
| +} |