Skip to content
quarry Artifact [d5059cbbb7]
Login

Artifact [d5059cbbb7]

Artifact d5059cbbb7d6c26529de187e0613483e7103ca8732dae5e693a448a105c75560:


package web

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

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

// An accept refused because this forge's zit is too old is a 503 page
// carrying the refusal's code.
func TestAnOldZitAcceptRendersItsRefusalHook(t *testing.T) {
	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: api.RefusalZitTooOld, Message: "old",
				Class: ops.ErrUnavailable, Fields: map[string]string{"found": "0.3.30", "required": "0.3.31"}}
		},
	}); 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)
	if resp.StatusCode != http.StatusServiceUnavailable {
		t.Fatalf("FAIL: status %d, want 503:\n%.600s", resp.StatusCode, b)
	}
	if !strings.Contains(string(b), `data-refusal="zit_too_old"`) {
		t.Fatalf("FAIL: no data-refusal hook for the old zit:\n%.1200s", b)
	}
	t.Log("ok")
}

// The operations page says when the zit the forge runs is older than
// the one it needs, and says nothing when it is not.
func TestTheOpsPageShowsTheZitGap(t *testing.T) {
	for _, c := range []struct {
		d    pageData
		want bool
	}{
		{pageData{ZitVersion: "0.3.30", ZitMin: "0.3.31", ZitTooOld: true}, true},
		{pageData{ZitVersion: "0.3.31", ZitMin: "0.3.31"}, false},
	} {
		var buf bytes.Buffer
		if err := opsPage.ExecuteTemplate(&buf, "content", c.d); err != nil {
			t.Fatal(err)
		}
		if got := strings.Contains(buf.String(), `data-hook="zit-too-old"`); got != c.want {
			t.Errorf("FAIL: zit %s: hook shown %v, want %v", c.d.ZitVersion, got, c.want)
		}
	}
}