Skip to content
quarry Artifact [f969049563]
Login

Artifact [f969049563]

Artifact f969049563bbe8e6cfdc002faea9523dd970c03e3b425f421c30248a1af33ca6:


package ci

import (
	"fmt"
	"net/http"
	"net/http/httptest"
	"strings"
	"testing"
)

type fakeLedger map[string]string // hash -> id

func (l fakeLedger) RecordWorkerToken(hash, id, group string) error {
	l[hash] = id + "@" + group
	return nil
}
func (l fakeLedger) ForgetWorkerToken(id string) error {
	for h, v := range l {
		if strings.HasPrefix(v, id+"@") {
			delete(l, h)
		}
	}
	return nil
}

// A group-scoped mint is recorded under the hash of its value, a fleet
// mint is not, and a revoke removes the record.
func TestMintRecordsTheTokenAndRevokeForgetsIt(t *testing.T) {
	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if r.Method == "DELETE" {
			return
		}
		fmt.Fprint(w, `{"id":"t1","token":"VALUE","label":"l","task_type_prefixes":[]}`)
	}))
	defer srv.Close()
	e := New(srv.URL)
	e.SetAdminToken("adm")
	l := fakeLedger{}
	e.SetLedger(l)
	if _, err := e.MintWorkerToken("l", nil, nil); err != nil {
		t.Fatal(err)
	}
	if len(l) != 0 {
		t.Errorf("FAIL: a fleet token was recorded: %v", l)
	}
	if _, err := e.MintWorkerToken("l", nil, []string{"orchard"}); err != nil {
		t.Fatal(err)
	}
	if l[TokenHash("VALUE")] != "t1@orchard" {
		t.Errorf("FAIL: the mint recorded %v", l)
	}
	if err := e.RevokeWorkerToken("t1"); err != nil {
		t.Fatal(err)
	}
	if len(l) != 0 {
		t.Errorf("FAIL: a revoked token is still recorded: %v", l)
	}
	if !t.Failed() {
		t.Log("ok")
	}
}