Skip to content
quarry Artifact [4548509171]
Login

Artifact [4548509171]

Artifact 4548509171d8d838cd0b2819acf927b0cd09ec645c1e232a6f630f33481dc972:


package api

import (
	"errors"
	"testing"

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

// Retargeting decides where the next merge is written, so it takes
// accept's letter rather than the forum one. With the forum letter,
// anyone who may post in a record's thread could aim somebody else's
// work at a branch they never chose.
func TestRetargetSurfaceTakesTheCheckinLetter(t *testing.T) {
	r := ops.NewRegistry()
	if err := registerLandingRetarget(r, Deps{}); err != nil {
		t.Fatal(err)
	}
	op, ok := r.Lookup("landing.retarget")
	if !ok {
		t.Fatal("landing.retarget is not registered")
	}
	if op.Caps != "i" {
		t.Fatalf("caps = %q, want i — the letter that writing a merge takes", op.Caps)
	}
	if op.Auth != ops.Token {
		t.Fatalf("auth = %v, want Token", op.Auth)
	}
	if op.Method != "POST" || op.Pattern != "/api/v1/r/{repo}/landing/{id}/retarget" {
		t.Fatalf("route = %s %s", op.Method, op.Pattern)
	}
}

// An unknown record is not found rather than a fault, so the page can
// say so instead of apologising.
func TestRetargetOfAnUnknownRecordIsNotFound(t *testing.T) {
	r := ops.NewRegistry()
	ix := wikiIndex(t)
	stub := &Repo{Index: ix}
	if err := registerLandingRetarget(r, Deps{Repos: NewRepos(map[string]*Repo{"test": stub})}); err != nil {
		t.Fatal(err)
	}
	_, err := r.Invoke(t.Context(), "landing.retarget",
		landingRetargetIn{Repo: "test", ID: "nosuchrecord", To: "trunk"})
	if !errors.Is(err, ops.ErrNotFound) {
		t.Fatalf("err = %v, want ErrNotFound", err)
	}
}