Skip to content
quarry Artifact [92a9a26a89]
Login

Artifact [92a9a26a89]

Artifact 92a9a26a893203b46deddafe1c9fc6666a6bec5efc03194908cf45ac761e1f21:


package web

import (
	"net/http"
	"net/http/httptest"
	"testing"

	"craftdesign.group/quarry/internal/api"
	"craftdesign.group/quarry/internal/imgproxy"
	"craftdesign.group/quarry/internal/ops"
)

// /img is the proxy when one is configured, and absent otherwise; an
// unsigned request through the real handler is refused before any fetch.
func TestImageProxyRouteIsMountedOnlyWithAProxy(t *testing.T) {
	r := ops.NewRegistry()
	if err := api.RegisterAll(r, api.Deps{Version: "v", Repos: api.NewRepos(map[string]*api.Repo{})}); err != nil {
		t.Fatal(err)
	}
	p, err := imgproxy.New(t.TempDir(), false)
	if err != nil {
		t.Fatal(err)
	}
	status := func(h http.Handler, path string) int {
		rec := httptest.NewRecorder()
		h.ServeHTTP(rec, httptest.NewRequest("GET", path, nil))
		return rec.Code
	}
	with := HandlerWith(r, nil, nil, []Option{WithImageProxy(p)})
	if got := status(with, "/img?u=https%3A%2F%2Fexample.org%2Fa.png&s=forged"); got != http.StatusForbidden {
		t.Errorf("unsigned /img through the handler = %d, want 403", got)
	}
	without := HandlerWith(r, nil, nil, nil)
	if got := status(without, "/img?u=https%3A%2F%2Fexample.org%2Fa.png&s=forged"); got == http.StatusForbidden || got == http.StatusOK {
		t.Errorf("/img answered %d on a forge with no proxy", got)
	}
}