package main import ( "net/http" "net/http/httptest" "testing" ) func TestAPIHandlerExposesHealthWithoutDatabase(t *testing.T) { req := httptest.NewRequest(http.MethodGet, "/healthz", nil) rec := httptest.NewRecorder() newAPIHandler(nil, "").ServeHTTP(rec, req) if rec.Code != http.StatusOK { t.Fatalf("health status = %d", rec.Code) } } // TestServerRoutesRequireWorkloadVerifyToBeWired pins the deployment // misconfiguration case: newAPIHandler wires ServerRegistrar and // ResultSubmitter, but WorkloadVerifierFromSignedToken deliberately returns // nil whenever the secret or the database is missing (see // api.WorkloadVerifierFromSignedToken) rather than silently accepting every // caller. Service.serverMutation treats a nil WorkloadVerify as fatal for // BOTH the register and result routes. This test should start failing (and // be updated, not deleted) the day this path stops 503ing with an empty // secret and a nil database -- that's the intended signal, not a bug in the // test. See TestWorkloadVerifierFromSignedTokenAcceptsARealAllocation in the // api package's Postgres integration suite for the wired, working path. func TestServerRoutesRequireWorkloadVerifyToBeWired(t *testing.T) { handler := newAPIHandler(nil, "") for _, path := range []string{"/v1/servers/server-1/register", "/v1/servers/server-1/result"} { req := httptest.NewRequest(http.MethodPost, path, nil) req.Header.Set("Idempotency-Key", "regression-pin-key-123456") req.Header.Set("Authorization", "Bearer anything") rec := httptest.NewRecorder() handler.ServeHTTP(rec, req) if rec.Code != http.StatusServiceUnavailable { t.Fatalf("%s status = %d, want 503 (WorkloadVerify still unwired) -- if this changed, update this test rather than deleting it", path, rec.Code) } } }