package handlers import ( "net/http" "path/filepath" "testing" "github.com/go-chi/chi/v5" "go.uber.org/fx" "go.uber.org/fx/fxtest" "sneak.berlin/go/pixa/internal/config" "sneak.berlin/go/pixa/internal/database" "sneak.berlin/go/pixa/internal/globals" "sneak.berlin/go/pixa/internal/healthcheck" "sneak.berlin/go/pixa/internal/logger" ) // TestHandlersBuildTheirOwnFetcherWhenNoneIsProvided builds the handlers as // pixad does, in an fx app that provides no fetcher, and requests an image // from localhost, which is on the allowlist. The fetcher the handlers build // from the config refuses localhost, so the answer is 403. func TestHandlersBuildTheirOwnFetcherWhenNoneIsProvided(t *testing.T) { t.Parallel() stateDir := t.TempDir() cfg := &config.Config{ SigningKey: testSigningKey, StateDir: stateDir, DBURL: "file:" + filepath.Join(stateDir, "state.sqlite3"), AllowlistHosts: []string{"localhost"}, } var h *Handlers app := fxtest.New(t, fx.Supply(cfg), fx.Provide(globals.New, logger.New, database.New, healthcheck.New, New), fx.Populate(&h), ) app.RequireStart() t.Cleanup(app.RequireStop) r := chi.NewRouter() r.Get("/v1/image/*", h.HandleImage()) rec := sendGet(t, r, photoURL("localhost")) checkErrorBody(t, rec, http.StatusForbidden, "forbidden") }