Skip to content
quarry Artifact [7acacbb6c4]
Login

Artifact [7acacbb6c4]

Artifact 7acacbb6c4dc0f0d245c1f1323450a72fe56a3ab13983159ee15bd119703a445:


package api

import (
	"encoding/json"
	"net/http"
	"net/http/httptest"
	"testing"
	"time"

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

// Every refusal the router writes by itself carries a next of the one
// shape (RFC 0033): an object with a sentence and a help anchor, and an
// open, where it has one, on the forge. The refusals the hard stops
// write are held to it in TestALoginTokenIsRefusedTheBrowserOnlyActs.
func TestEveryRouterRefusalCarriesTheOneNextShape(t *testing.T) {
	const origin = "https://forge.test"
	for name, c := range map[string]struct {
		write func(w http.ResponseWriter)
		code  string
	}{
		"no login":    {func(w http.ResponseWriter) { writeDenied(w) }, noLoginCode},
		"wrong scope": {func(w http.ResponseWriter) { WriteWrongScope(w, "sync") }, "wrong_scope"},
		"held":        {func(w http.ResponseWriter) { WriteHeld(w, 90*time.Second) }, "held"},
	} {
		w := httptest.NewRecorder()
		c.write(w)
		var body struct {
			Code string         `json:"code"`
			Next *nextstep.Next `json:"next"`
		}
		var raw map[string]json.RawMessage
		json.Unmarshal(w.Body.Bytes(), &raw)
		if err := json.Unmarshal(w.Body.Bytes(), &body); err != nil || body.Code != c.code {
			t.Errorf("FAIL: %s: %v %s", name, err, w.Body.String())
			continue
		}
		if err := body.Next.Validate(origin); err != nil || body.Next.Help == "" {
			t.Errorf("FAIL: %s: next %s: %v", name, w.Body.String(), err)
		}
		if len(raw["next"]) == 0 || raw["next"][0] != '{' {
			t.Errorf("FAIL: %s: next is not an object: %s", name, raw["next"])
		}
	}
	if !t.Failed() {
		t.Log("ok")
	}
}