Skip to content
quarry Artifact [ba1cfb7e0b]
Login

Artifact [ba1cfb7e0b]

Artifact ba1cfb7e0b10db24fb0e4d2ae48b55e5144be579565a8b98e93ec579a134dde2:


package api

import (
	"context"
	"encoding/json"
	"errors"
	"net/http"
	"net/http/httptest"
	"reflect"
	"strings"
	"testing"

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

// loginAskCall posts ci.checks.set as ctx's caller, with Quarry-Ask when
// ask is not empty.
func loginAskCall(t *testing.T, r *ops.Registry, ctx context.Context, ask string) *httptest.ResponseRecorder {
	t.Helper()
	holds := func(string, string) CapSet { return oltp.CapSet("a") }
	h, err := RouterWithCaps(r, "v", holds, nil)
	if err != nil {
		t.Fatal(err)
	}
	req := httptest.NewRequest("POST", "/api/v1/r/test/ci/required-checks", strings.NewReader(`{"branch":"trunk","checks":["go","vet"]}`)).WithContext(ctx)
	req.Header.Set("Content-Type", "application/json")
	if ask != "" {
		req.Header.Set("Quarry-Ask", ask)
	}
	w := httptest.NewRecorder()
	h.ServeHTTP(w, req)
	return w
}

func tokenAs(user string, login bool) context.Context {
	return ops.WithToken(ops.WithUser(context.Background(), user),
		ops.TokenAuth{ID: "T", Login: login, Caps: func(string) ops.Caps { return oltp.CapSet("a") }})
}

// A person's own login token is refused a hard stop; with Quarry-Ask it
// files a request for themselves, runs nothing, and approving runs it.
func TestALoginTokenAsksItsOwnPersonForAHardStop(t *testing.T) {
	r, db, _ := requiredChecksFixture(t)
	w := loginAskCall(t, r, tokenAs("jva", true), "")
	var refusal struct{ Code string }
	json.Unmarshal(w.Body.Bytes(), &refusal)
	if w.Code != http.StatusForbidden || !strings.Contains(w.Body.String(), browserOnlyCode) {
		t.Errorf("FAIL: without the header: %d %s", w.Code, w.Body)
	}
	w = loginAskCall(t, r, tokenAs("jva", true), "gate trunk")
	if w.Code != http.StatusAccepted {
		t.Fatalf("FAIL: asking answered %d: %s", w.Code, w.Body)
	}
	var asked struct {
		SignOff SignOffRequest `json:"signoff"`
	}
	if err := json.Unmarshal(w.Body.Bytes(), &asked); err != nil || asked.SignOff.ID == "" ||
		asked.SignOff.Owner != "jva" || asked.SignOff.State != "pending" || !IsSelfAsker(asked.SignOff.Agent) {
		t.Fatalf("FAIL: the request: %s", w.Body)
	}
	if got := db.RequiredChecks("test", "trunk"); len(got) != 0 {
		t.Fatalf("FAIL: the ask wrote before approval: %v", got)
	}
	// The person's own token reads it back to poll.
	if _, err := r.Invoke(tokenAs("jva", true), "signoff.get", signOffGetIn{ID: asked.SignOff.ID}); err != nil {
		t.Errorf("FAIL: the asker could not read its request: %v", err)
	}
	// A token never decides; the person's session does.
	if _, err := r.Invoke(tokenAs("jva", true), "signoff.decide", signOffDecideIn{ID: asked.SignOff.ID, Approve: true}); !errors.Is(err, ops.ErrForbidden) {
		t.Errorf("FAIL: a token decided: %v", err)
	}
	if _, err := r.Invoke(asUser("jva"), "signoff.decide", signOffDecideIn{ID: asked.SignOff.ID, Approve: true}); err != nil {
		t.Fatalf("FAIL: approving: %v", err)
	}
	if got := db.RequiredChecks("test", "trunk"); !reflect.DeepEqual(got, []string{"go", "vet"}) {
		t.Errorf("FAIL: approval left %v", got)
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

// A token that is not a login, and a call no hard stop refuses, are not
// asked: the plain answer stands.
func TestOnlyALoginTokenAsksItself(t *testing.T) {
	r, db, _ := requiredChecksFixture(t)
	w := loginAskCall(t, r, tokenAs("jva", false), "gate trunk")
	if w.Code == http.StatusAccepted {
		t.Errorf("FAIL: a non-login token asked: %s", w.Body)
	}
	// It holds the letters, so the call simply ran, as it always did.
	if got := db.RequiredChecks("test", "trunk"); !reflect.DeepEqual(got, []string{"go", "vet"}) {
		t.Errorf("FAIL: the plain token's call left %v", got)
	}
	if !t.Failed() {
		t.Log("ok")
	}
}