package main import ( "net/http" "net/http/httptest" "path/filepath" "testing" ) func TestSeenStoreKeepsConnectAndDropTimesAcrossARestart(t *testing.T) { path := filepath.Join(t.TempDir(), "machines_seen.json") s := newSeenStore(path) s.up("pgx") if r := s.snapshot()["pgx"]; r.Up == 0 || r.Down != 0 { t.Fatalf("after a connect: %+v", r) } s.down("pgx") again := newSeenStore(path) // the server restarted r := again.snapshot()["pgx"] if r.Up == 0 || r.Down == 0 || r.Down < r.Up { t.Fatalf("after a drop and a restart: %+v", r) } var nilStore *seenStore // tests build a Server without one nilStore.up("x") nilStore.down("x") if len(nilStore.snapshot()) != 0 { t.Fatal("a missing store must stay empty and not panic") } } func TestMachinesSeenNeedsALogin(t *testing.T) { s := &Server{seen: newSeenStore("")} w := httptest.NewRecorder() s.machinesSeen(w, httptest.NewRequest("GET", "/api/machines-seen", nil)) if w.Code != http.StatusUnauthorized { t.Fatalf("without a login: %d", w.Code) } }