Skip to content
quarry Artifact [3b2f4e718d]
Login

Artifact [3b2f4e718d]

Artifact 3b2f4e718d4a825c05c7f945a646ab288e734b2f1fdbef68a303c5d92be81fe4:


package web

import (
	"net/http"
	"net/http/httptest"
	"net/url"
	"strings"
	"sync"
	"testing"

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

// packForge is a forge with one store, "tcl", whose pack requests the
// returned counter sees. A request for a pack already asked for reports
// not queued, as the maintenance lane does.
func packForge(t *testing.T) (*httptest.Server, func() (asked, queued int)) {
	t.Helper()
	accts := newBetaAccts()
	_ = accts.AddAccount("root", "root-pw", true)
	_ = accts.AddAccount("eve", "longenough1", false)
	sess := &betaSessions{accts: accts, admin: "root"}
	var mu sync.Mutex
	var asked, queued int
	held := map[string]bool{}
	r := ops.NewRegistry()
	if err := api.RegisterAll(r, api.Deps{Version: "v", Accounts: accts,
		Repos: api.NewRepos(map[string]*api.Repo{"tcl": {Zit: zitcli.New("tcl")}}),
		Pack: func(repo string) (bool, error) {
			mu.Lock()
			defer mu.Unlock()
			asked++
			if held[repo] {
				return false, nil
			}
			held[repo] = true
			queued++
			return true, nil
		}}); err != nil {
		t.Fatal(err)
	}
	srv := httptest.NewServer(HandlerWith(r, nil, nil,
		[]Option{WithSessions(sess), WithCaps(betaCaps{admin: "root"})},
		Verify(func(string, string, string, ops.Auth) api.Decision { return api.Denied })))
	t.Cleanup(srv.Close)
	return srv, func() (int, int) { mu.Lock(); defer mu.Unlock(); return asked, queued }
}

// Only a site admin can ask for a pack, an unknown store is not found,
// and asking twice queues it once, with the second answer saying so.
func TestPackPostIsSiteAdminOnlyAndNotQueuedTwice(t *testing.T) {
	srv, counts := packForge(t)
	form := url.Values{"repo": {"tcl"}}

	for _, who := range []string{"eve", ""} {
		res := as(t, srv, who, "POST", "/fleet/ops/pack", form)
		body(res)
		// A signed-in non-admin is forbidden, a stranger is sent to sign in.
		sentToLogin := res.StatusCode == http.StatusSeeOther && strings.HasPrefix(res.Header.Get("Location"), "/login")
		if res.StatusCode != http.StatusForbidden && !sentToLogin {
			t.Errorf("FAIL: %q was let through: %d %s", who, res.StatusCode, res.Header.Get("Location"))
		}
	}
	if asked, _ := counts(); asked != 0 {
		t.Errorf("FAIL: a refused caller reached the lane: %d requests", asked)
	}

	res := as(t, srv, "root", "POST", "/fleet/ops/pack", url.Values{"repo": {"nope"}})
	body(res)
	if res.StatusCode != http.StatusNotFound {
		t.Errorf("FAIL: an unknown store answered %d, want 404", res.StatusCode)
	}
	if asked, _ := counts(); asked != 0 {
		t.Errorf("FAIL: an unknown store reached the lane")
	}

	first := as(t, srv, "root", "POST", "/fleet/ops/pack", form)
	body(first)
	if first.StatusCode != http.StatusSeeOther {
		t.Fatalf("FAIL: the first request answered %d, want 303", first.StatusCode)
	}
	if c := flashOf(first); c != "" {
		t.Errorf("FAIL: a queued request carried a note: %q", c)
	}
	second := as(t, srv, "root", "POST", "/fleet/ops/pack", form)
	body(second)
	if second.StatusCode != http.StatusSeeOther {
		t.Fatalf("FAIL: the second request answered %d, want 303", second.StatusCode)
	}
	if flashOf(second) == "" {
		t.Errorf("FAIL: a duplicate request was not told it added nothing")
	}
	page := func() string {
		req, _ := http.NewRequest("GET", srv.URL+"/fleet/ops", nil)
		req.AddCookie(&http.Cookie{Name: sessionCookie, Value: "tok-root"})
		req.AddCookie(&http.Cookie{Name: flashCookie, Value: flashOf(second)})
		res, err := http.DefaultClient.Do(req)
		if err != nil {
			t.Fatal(err)
		}
		return body(res)
	}()
	if !strings.Contains(page, `data-hook="ops-note"`) {
		t.Errorf("FAIL: the operations page did not carry the note")
	}
	if asked, queued := counts(); asked != 2 || queued != 1 {
		t.Errorf("FAIL: asked %d queued %d, want 2 asks and 1 queued", asked, queued)
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

func flashOf(res *http.Response) string {
	for _, c := range res.Cookies() {
		if c.Name == flashCookie {
			return strings.TrimSpace(c.Value)
		}
	}
	return ""
}