Skip to content
quarry Artifact [0fd5d59c16]
Login

Artifact [0fd5d59c16]

Artifact 0fd5d59c163aeb184b6df44095852925e0e50471ba71584c0e84f97f1bf2081f:


package web

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

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

func ciBoxServer(t *testing.T, plane *memCaps) (*httptest.Server, *editRecorder) {
	t.Helper()
	rec := &editRecorder{}
	r := ops.NewRegistry()
	if err := api.RegisterAll(r, api.Deps{Version: "v", Registry: rec,
		Repos:  api.NewRepos(map[string]*api.Repo{"alpha": {Chat: "alpha", Index: landingIndex(t, false)}}),
		Roster: capsRoster{plane}}); err != nil {
		t.Fatal(err)
	}
	srv := httptest.NewServer(HandlerWith(r, nil, nil, []Option{
		WithSessions(fakeSessions{user: "dan", roles: map[string]ops.Auth{"alpha": ops.Admin}}),
		WithCaps(plane)}))
	t.Cleanup(srv.Close)
	return srv, rec
}

// The visibility section draws one box, with its presence marker, under
// the public preset only, checked when visitors hold 8.
func TestVisibilityDrawsTheCIBoxUnderThePublicPreset(t *testing.T) {
	plane := &memCaps{stored: map[string]string{"dan": "a"}}
	srv, _ := ciBoxServer(t, plane)
	draws := func() (drawn, checked bool) {
		body := asDan(t, srv, "/r/alpha/settings")
		i := strings.Index(body, `name="cipublic"`)
		if i < 0 {
			return false, false
		}
		tag := body[strings.LastIndex(body[:i], "<input"):]
		tag = tag[:strings.Index(tag, ">")]
		return strings.Contains(body, `name="has" value="cipublic"`), strings.Contains(tag, "checked")
	}
	if drawn, checked := draws(); !drawn || checked {
		t.Errorf("FAIL: public preset: drawn with marker=%v checked=%v, want drawn and unchecked", drawn, checked)
	}
	plane.cats = map[string]string{"alpha/nobody": "gjorzh28"}
	if drawn, checked := draws(); !drawn || !checked {
		t.Errorf("FAIL: nobody holding 8: drawn=%v checked=%v, want both", drawn, checked)
	}
	plane.cats = map[string]string{"alpha/nobody": ""}
	if drawn, _ := draws(); drawn {
		t.Errorf("FAIL: the box is drawn where visitors read nothing")
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

// The box goes through repo.update as ciPublic, read only when the form
// drew it, and only for the public preset; a preset save keeps the 8.
func TestVisibilityFormCarriesTheCIBox(t *testing.T) {
	plane := &memCaps{stored: map[string]string{"dan": "a"}}
	srv, rec := ciBoxServer(t, plane)
	post := func(f url.Values) {
		t.Helper()
		if resp := postAsDan(t, srv, "/r/alpha/settings/visibility", f); resp.StatusCode >= 400 {
			t.Fatalf("FAIL: the visibility form answered %d", resp.StatusCode)
		}
	}
	post(url.Values{"visibility": {"Public project"}, "has": {"cipublic"}, "cipublic": {"on"}})
	if rec.count() != 1 || rec.last().CIPublic == nil || !*rec.last().CIPublic {
		t.Fatalf("FAIL: a ticked box did not arrive as on: %d patches", rec.count())
	}
	post(url.Values{"visibility": {"Public project"}, "has": {"cipublic"}})
	if rec.count() != 2 || rec.last().CIPublic == nil || *rec.last().CIPublic {
		t.Errorf("FAIL: a drawn, unticked box did not arrive as off")
	}
	n := rec.count()
	post(url.Values{"visibility": {"Public project"}})
	if rec.count() != n {
		t.Errorf("FAIL: a form that did not draw the box wrote it")
	}
	post(url.Values{"visibility": {"Members only"}, "has": {"cipublic"}, "cipublic": {"on"}})
	if rec.count() != n {
		t.Errorf("FAIL: the box was written beside a preset visitors cannot read")
	}
	plane.cats = map[string]string{"alpha/nobody": "gjorzh28"}
	post(url.Values{"visibility": {"Public project"}})
	if got := plane.cat("alpha", "nobody"); !strings.Contains(got, "8") {
		t.Errorf("FAIL: saving the preset again dropped the 8: %q", got)
	}
	if !t.Failed() {
		t.Log("ok")
	}
}