Skip to content
quarry Artifact [45eec694ae]
Login

Artifact [45eec694ae]

Artifact 45eec694aeca430ceaf5ed3874037d837472da40a12e2b324aaf4d74341bc598:


package web

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

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

type acceptStubIn struct {
	Repo     string `json:"repo" path:"repo"`
	ID       string `json:"id" path:"id"`
	Message  string `json:"message"`
	Override string `json:"override"`
}

type acceptStubOut struct {
	Hash string `json:"hash"`
}

// An accept refused because the branch moved since it was checked
// comes back as a refusal page carrying the code, with a way back to
// the landing to review the new tip.
func TestAMovedBranchAcceptRendersItsRefusalHook(t *testing.T) {
	movedAcceptRendersItsHook(t, api.RefusalBranchMoved)
}

func movedAcceptRendersItsHook(t *testing.T, code string) {
	r := viewRegistry(t, nil)
	if err := ops.Register(r, ops.Op[acceptStubIn, acceptStubOut]{
		Name: "landing.accept", Caps: "i", Doc: "test stub", Kind: ops.Command, Auth: ops.Token,
		Route: "POST /api/v1/r/{repo}/landing/{id}/accept",
		Run: func(context.Context, acceptStubIn) (acceptStubOut, error) {
			return acceptStubOut{}, &ops.Refusal{Code: code, Message: "moved",
				Fields: map[string]string{"expected": "aaa", "current": "bbb"}}
		},
	}); err != nil {
		t.Fatal(err)
	}
	srv := httptest.NewServer(HandlerWith(r, nil, nil, nil))
	defer srv.Close()
	c := srv.Client()
	c.CheckRedirect = func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }
	resp, err := c.PostForm(srv.URL+"/r/alpha/landing/a1a1a1a1a1a1/accept", url.Values{})
	if err != nil {
		t.Fatal(err)
	}
	defer resp.Body.Close()
	b, _ := io.ReadAll(resp.Body)
	html := string(b)
	if resp.StatusCode != http.StatusBadRequest {
		t.Fatalf("FAIL: status %d, want 400:\n%.600s", resp.StatusCode, html)
	}
	if !strings.Contains(html, `data-refusal="`+code+`"`) {
		t.Fatalf("FAIL: no data-refusal hook for %s:\n%.1200s", code, html)
	}
	if !strings.Contains(html, `data-hook="refusal-back" href="/r/alpha/landing/a1a1a1a1a1a1"`) {
		t.Fatalf("FAIL: no way back to the landing to review the new tip:\n%.1200s", html)
	}
	t.Log("ok")
}