package console import ( "context" "net/http" "testing" "time" ) // The GeoIP publisher keeps the Releases handle it was built with. After // ConfigureClaimRuntime that must be the mutation boundary, never the raw // bucket, or every publish, off, sweep, expiry and feed write skips it. func TestClaimGeoipPublisherUsesTheBoundary(t *testing.T) { cx := newClaimCase(t) s := cx.srv if s.geoip == nil { t.Fatal("the enabled console has no GeoIP publisher") } if any(s.geoip.store) == any(cx.rawStore) { t.Fatal("the GeoIP publisher writes to the raw bucket") } if any(s.geoip.store) != any(s.cfg.Releases) { t.Fatalf("the GeoIP publisher holds %T, not the installed Config.Releases %T", s.geoip.store, s.cfg.Releases) } } // The three operator routes and the app route, each through the real mux with // a real bearer, so a fixture's nonApp or read reason is checked against a // request that reached its handler (an unauthenticated 401 reaches nothing). func TestClaimGeoipRoutesReachTheirHandlers(t *testing.T) { cx := newClaimCase(t) for _, c := range []struct { method, path, body string want []int }{ {http.MethodGet, "/api/cli/system/geoip", "", []int{http.StatusOK}}, {http.MethodDelete, "/api/cli/system/geoip", "", []int{http.StatusOK}}, // Not a database: staged, then refused by verification (the job is // asynchronous), or refused outright. Either way the handler ran. {http.MethodPut, "/api/cli/system/geoip", "not a database", []int{http.StatusAccepted, http.StatusUnprocessableEntity}}, } { code, _, _ := rawReq(t, c.method, cx.ts.URL+c.path, "", c.body) if code != http.StatusUnauthorized { t.Errorf("%s %s without a token: %d, want 401", c.method, c.path, code) } code, _, out := rawReq(t, c.method, cx.ts.URL+c.path, cx.bearer, c.body) ok := false for _, w := range c.want { ok = ok || code == w } if !ok { t.Errorf("%s %s as the owner: %d %s, want one of %v", c.method, c.path, code, out, c.want) } } cx.quiesce() // The app route needs an instance-family caller, not an operator token // alone, but it must still be a registered, authenticated route. code, _, _ := rawReq(t, http.MethodGet, cx.ts.URL+"/api/cli/apps/bab/titogo/geo?ip=8.8.8.8", "", "") if code != http.StatusUnauthorized { t.Errorf("GET apps geo without a token: %d, want 401", code) } code, _, out := rawReq(t, http.MethodGet, cx.ts.URL+"/api/cli/apps/bab/titogo/geo?ip=8.8.8.8", cx.bearer, "") if code == http.StatusUnauthorized || code == http.StatusNotFound { t.Errorf("GET apps geo as the owner: %d %s, want the handler's own answer", code, out) } } // StartGeoipJobs is a recorded worker: it blocks until its context ends and // joins every goroutine it started, so a bounded call leaves nothing running. func TestClaimGeoipWorkerJoinsItsGoroutines(t *testing.T) { cx := newClaimCase(t) ctx, cancel := context.WithTimeout(context.Background(), 300*time.Millisecond) defer cancel() done := make(chan struct{}) go func() { cx.srv.RunClaimWorker(ctx, "StartGeoipJobs") close(done) }() select { case <-done: case <-time.After(10 * time.Second): t.Fatal("StartGeoipJobs did not return after its context ended") } cx.quiesce() } // No bucket, no publisher: GeoIP is off and the worker returns at once. func TestClaimGeoipWorkerOffReturnsImmediately(t *testing.T) { cx := newClaimCase(t) cx.srv.geoip = nil done := make(chan struct{}) go func() { cx.srv.RunClaimWorker(context.Background(), "StartGeoipJobs") close(done) }() select { case <-done: case <-time.After(5 * time.Second): t.Fatal("StartGeoipJobs with no publisher blocked") } }