Skip to content
quarry Artifact [09e0ce959f]
Login

Artifact [09e0ce959f]

Artifact 09e0ce959ff4a3c5315d36dc6a9a9c9c3c569c7dc63a96de0c851b8ab7b8206f:


package harness

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

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

// Fossil's default ticket schema has ten authorable columns. quarry must
// be able to WRITE the ones it renders a control for: the new-ticket
// form has always offered type, severity, priority and subsystem, and
// the handler behind it forwarded only title, status and comment — so a
// reader filled four fields, was told the ticket was filed, and the
// values were gone. A control that discards what you type is worse than
// one that is absent, because the reader believes they filed it.
//
// foundin is here too: it is the tenth column, readable since the fold
// was written and writable by nothing until this test existed.
func TestFilingATicketKeepsEveryFieldTheFormOffers(t *testing.T) {
	stores := map[string]string{"alpha": buildStore(t, "fieldseed")}
	srv := ingestAndServeAuth(t, stores, t.TempDir(),
		func(repo, user, pass string, _ ops.Auth) api.Decision {
			if repo == "alpha" && user == "dan" && pass == "hunter2" {
				return api.Allowed
			}
			return api.Denied
		})
	client := srv.Client()
	client.CheckRedirect = func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }

	resp, err := func() (*http.Response, error) {
		form := url.Values{
			"title": {"every field set"}, "status": {"Open"}, "comment": {"body"},
			"type": {"Code_Defect"}, "severity": {"Critical"},
			"priority": {"Immediate"}, "subsystem": {"web"}, "foundin": {"0.0.0-dev"},
		}
		req, _ := http.NewRequest("POST", srv.URL+"/r/alpha/ticket/new",
			strings.NewReader(form.Encode()))
		req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
		req.SetBasicAuth("dan", "hunter2")
		return client.Do(req)
	}()
	if err != nil {
		t.Fatal(err)
	}
	resp.Body.Close()
	if resp.StatusCode != http.StatusSeeOther {
		t.Fatalf("filing a ticket: %d", resp.StatusCode)
	}

	req, _ := http.NewRequest("GET", srv.URL+resp.Header.Get("Location"), nil)
	req.SetBasicAuth("dan", "hunter2")
	page, err := client.Do(req)
	if err != nil {
		t.Fatal(err)
	}
	defer page.Body.Close()
	b, _ := io.ReadAll(page.Body)
	html := string(b)

	for field, want := range map[string]string{
		"type": "Code_Defect", "severity": "Critical", "priority": "Immediate",
		"subsystem": "web", "foundin": "0.0.0-dev",
	} {
		if !strings.Contains(html, want) {
			t.Errorf("%s was dropped on the way in: the filed ticket has no %q", field, want)
		}
	}
}