Skip to content
quarry Artifact [24fe356ec2]
Login

Artifact [24fe356ec2]

Artifact 24fe356ec28736eda5ef4f1329fb8c5d3d401e28992de86e017c1082c9c57b11:


package main

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

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

func (g doorRig) bearerAPI(tok string) *httptest.ResponseRecorder {
	req := httptest.NewRequest(http.MethodPost, "/api/v1/r/vault/door-test", nil)
	req.Header.Set("Authorization", "Bearer "+tok)
	w := httptest.NewRecorder()
	g.api.ServeHTTP(w, req)
	return w
}

// A token that proved itself but lacks the api scope is told which
// scope it lacks, and is not a failed sign-in.
func TestAWrongScopeTokenAtTheAPIIsToldAndNotCounted(t *testing.T) {
	g := newDoorRig(t)
	syncTok, _ := g.mint(t, oltp.TokenSpec{Scopes: scopes(oltp.ScopeSync)})
	for i := 0; i < 50; i++ {
		w := g.bearerAPI(syncTok)
		var body map[string]any
		json.Unmarshal(w.Body.Bytes(), &body)
		if w.Code != http.StatusUnauthorized || body["code"] != "wrong_scope" {
			t.Fatalf("FAIL: try %d: status %d code %q, want 401 wrong_scope", i, w.Code, body["code"])
		}
		next, _ := body["next"].(map[string]any)
		if next["say"] == nil || next["say"] == "" || next["help"] == nil || next["help"] == "" || next["call"] == nil || body["error"] == "" {
			t.Fatalf("FAIL: try %d: body lacks error or a full next: %v", i, body)
		}
		if _, has := next["cmd"]; has {
			t.Fatalf("FAIL: try %d: next carries cmd before the client exists: %v", i, next)
		}
	}
	apiTok, _ := g.mint(t, oltp.TokenSpec{Scopes: scopes(oltp.ScopeAPI)})
	if w := g.bearerAPI(apiTok); w.Code != http.StatusOK {
		t.Errorf("FAIL: an api token after fifty wrong-scope tries answered %d, want 200", w.Code)
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

// A value that has not proved itself is still a miss and still counts.
func TestAGarbageTokenAtTheAPIStillCounts(t *testing.T) {
	g := newDoorRig(t)
	_, tok := g.mint(t, oltp.TokenSpec{Scopes: scopes(oltp.ScopeSync)})
	bad := "qat_" + tok.ID + "_WRONGWRONGWRONGWRONGWRONGWRONGWR"
	for i := 0; i < 6; i++ {
		w := g.bearerAPI(bad)
		var body map[string]any
		json.Unmarshal(w.Body.Bytes(), &body)
		if w.Code != http.StatusUnauthorized || body["code"] == "wrong_scope" {
			t.Errorf("FAIL: garbage try %d: status %d code %q", i, w.Code, body["code"])
		}
	}
	w := g.bearerAPI(bad)
	if w.Code != http.StatusTooManyRequests {
		t.Errorf("FAIL: after six garbage tries got %d, want 429", w.Code)
	}
	var held map[string]any
	json.Unmarshal(w.Body.Bytes(), &held)
	next, _ := held["next"].(map[string]any)
	if _, has := next["call"]; has {
		t.Errorf("FAIL: the 429 next carries a call: %v", next)
	}
	if held["error"] == nil || next["say"] == nil || next["say"] == "" || next["help"] == nil || w.Header().Get("Retry-After") == "" {
		t.Errorf("FAIL: the 429 hold lacks error, next or Retry-After: %v", held)
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

// The sync door's hold keeps the bytes a stock Fossil client has always
// read: plain text, no JSON, no next.
func TestTheSyncDoorsHoldIsUnchanged(t *testing.T) {
	h, _ := anonymRig(t)
	call := func() *httptest.ResponseRecorder {
		req := httptest.NewRequest(http.MethodPost, "/r/vault/xfer", nil)
		req.RemoteAddr = "203.0.113.20:1234"
		req.SetBasicAuth("jan", "wrong-"+doorPassword)
		w := httptest.NewRecorder()
		h.ServeHTTP(w, req)
		return w
	}
	for i := 0; i < 6; i++ {
		call()
	}
	w := call()
	if w.Code != http.StatusTooManyRequests {
		t.Fatalf("FAIL: the held sync door answered %d, want 429", w.Code)
	}
	if ct := w.Header().Get("Content-Type"); ct != "text/plain; charset=utf-8" {
		t.Errorf("FAIL: content type %q changed", ct)
	}
	if !regexp.MustCompile(`^too many failed sign-ins; try again in [0-9a-zµ.]+\n$`).MatchString(w.Body.String()) {
		t.Errorf("FAIL: the sync door's body changed: %q", w.Body.String())
	}
	if !t.Failed() {
		t.Log("ok")
	}
}