~bigbes/sr-ht-spec

d4b6373d153470b30c9bb6882f71d9bee0a8f416 — Eugene Blikh 9 days ago e756d50
graph: seed the test keyset from ecoretest

The same fernet-key-and-webhook-seed TestMain the web package had, and the same
reason to delete it: ecore's keys are constants, so both packages of this
service can install them without the second rotating what the first sealed
with.
1 files changed, 7 insertions(+), 22 deletions(-)

M graph/graph_test.go
M graph/graph_test.go => graph/graph_test.go +7 -22
@@ 2,9 2,7 @@ package graph

import (
	"context"
	"crypto/rand"
	"crypto/sha1"
	"encoding/base64"
	"encoding/hex"
	"encoding/json"
	"fmt"


@@ 16,12 14,11 @@ import (
	"testing"
	"time"

	"github.com/fernet/fernet-go"
	"github.com/go-chi/chi/v5"
	"github.com/vaughan0/go-ini"
	"sourcecraft.dev/bigbes/sr-ht-core/auth"
	"sourcecraft.dev/bigbes/sr-ht-core/crypto"
	"sourcecraft.dev/bigbes/sr-ht-ecore/bearer"
	"sourcecraft.dev/bigbes/sr-ht-ecore/ecoretest"

	"sourcecraft.dev/bigbes/sr-ht-spec/authn"
	"sourcecraft.dev/bigbes/sr-ht-spec/core"


@@ 30,25 27,13 @@ import (
	"sourcecraft.dev/bigbes/sr-ht-spec/service"
)

// testConf carries the crypto keys established in TestMain, so a test can seal
// a unified-login cookie the way meta.sr.ht does and drive the schema through
// the same middleware the daemon installs.
var testConf ini.File

// TestMain installs sr-ht-ecore's fixed test keyset, so a test can seal a
// unified-login cookie the way meta.sr.ht does and drive the schema through the
// same middleware the daemon installs. The keys are constants there, so this
// package and web/ can both call it without the second rotating what the first
// sealed with.
func TestMain(m *testing.M) {
	var fk fernet.Key
	if err := fk.Generate(); err != nil {
		panic("generate fernet key: " + err.Error())
	}
	seed := make([]byte, 32)
	if _, err := rand.Read(seed); err != nil {
		panic("generate webhook seed: " + err.Error())
	}
	testConf = ini.File{
		"sr.ht":    ini.Section{"network-key": fk.Encode()},
		"webhooks": ini.Section{"private-key": base64.StdEncoding.EncodeToString(seed)},
	}
	crypto.InitCrypto(testConf)
	ecoretest.InitCrypto()
	os.Exit(m.Run())
}