Skip to content
quarry Artifact [9c922a5871]
Login

Artifact [9c922a5871]

Artifact 9c922a587110a81990b8916faaa29cc43b1a01b54116cd9e0c43540bfae7a8f6:


package web

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

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

type versionIn struct{}
type versionOut struct {
	Version string `json:"version"`
}

// Every page asks for the forge's version, which is public and about no
// repository. A token limited to repositories is refused for an op that
// names none, so asking as the reader made every page a 500 for an
// agent on such a token.
func TestAPageRendersForATokenLimitedToRepositories(t *testing.T) {
	r := ops.NewRegistry()
	if err := ops.Register(r, ops.Op[versionIn, versionOut]{
		Name: "meta.version", Kind: ops.Query, Auth: ops.Anonymous, Route: "GET /api/v1/version", Doc: "test double",
		Run: func(context.Context, versionIn) (versionOut, error) { return versionOut{Version: "t"}, nil },
	}); err != nil {
		t.Fatal(err)
	}
	h := HandlerWith(r, nil, nil, nil)
	req := httptest.NewRequest("GET", "/login", nil)
	req = req.WithContext(ops.WithToken(ops.WithUser(req.Context(), "bot"), ops.TokenAuth{ID: "T",
		Caps: func(string) ops.Caps { return nil }, Limited: true}))
	w := httptest.NewRecorder()
	h.ServeHTTP(w, req)
	if w.Code == http.StatusInternalServerError {
		t.Fatalf("FAIL: a page on a repository-limited token answered %d: %s", w.Code, w.Body.String())
	}
}