Files
coredns-web-ui/apimatrix_env_test.go
bryanpedini 9251c85fd8 test: require review fix assertions
- test: require review fix assertions
- test: cover zone repair and imported dns records
- test: wait for dns and tighten assertions
- fix(api): drop the record id of a rolled back record creation
- test(audit): run every api route through the real handlers in an audit matrix
- test(api): run the authorization matrix through the real zone handlers
- test: repair duplicate apex soa through ui
2026-11-19 15:46:12 +01:00

144 lines
4.0 KiB
Go

// SPDX-License-Identifier: GPL-2.0-or-later
// Copyright (C) 2026 Bryan Joshua Pedini
package main
import (
"database/sql"
"encoding/json"
"errors"
"fmt"
"net"
"strings"
"testing"
"time"
)
const (
mxOrigin = "192.0.2.10"
mxZone = "example.org."
mxStale = "stale.org."
mxOther = "other.org."
mxMissing = "missing.org."
mxCreated = "2026-11-16T12:00:00.000000Z"
mxNow = "2026-11-18T12:00:00.000000Z"
mxSOA = `{"ns":"ns.example.net.","MBox":"hostmaster.example.org.","refresh":3600,"retry":600,"expire":604800,"minttl":300,"serial":2026111700}`
mxSOANext = `{"ns":"ns.example.net.","MBox":"hostmaster.example.org.","refresh":3600,"retry":600,"expire":604800,"minttl":300,"serial":2026111800}`
mxNS = `{"host":"ns.example.net."}`
)
var (
errMatrixInternal = errors.New("matrix internal failure")
errMatrixNet = &net.OpError{Op: "read", Net: "tcp", Err: errors.New("connection reset by peer")}
)
var matrixUserSeed = []struct{ name, role string }{
{"admin", roleAdmin}, {"root", roleAdmin}, {"alice", roleUser}, {"dave", roleUser},
{"carol", roleUser}, {"bob", roleUser}, {"gone", roleUser},
}
func matrixSeedRecords() []recordRow {
rec := func(id int64, zone, name string, ttl int64, typ, content string) recordRow {
return recordRow{ID: id, Zone: zone, Name: name, TTL: sql.NullInt64{Int64: ttl, Valid: true},
Content: sql.NullString{String: content, Valid: true}, RecordType: typ}
}
return []recordRow{
rec(1, mxZone, "", 3600, "SOA", mxSOA),
rec(2, mxZone, "", 3600, "NS", mxNS),
rec(3, mxZone, "www", 300, "A", `{"ip":"192.0.2.1"}`),
rec(4, mxZone, "mail", 300, "A", `{"ip":"192.0.2.2"}`),
rec(5, mxOther, "", 3600, "SOA", strings.Replace(mxSOA, "example.org", "other.org", 1)),
rec(6, mxOther, "", 3600, "NS", mxNS),
}
}
func matrixSeed(id int64) recordRow {
return matrixSeedRecords()[id-1]
}
type matrixEnv struct {
t *testing.T
h *webHarness
sql *matrixSQL
users map[string]userRow
tokens map[string]string
}
func newMatrixEnv(t *testing.T) *matrixEnv {
t.Helper()
h := newWebHarness(t)
m := newMatrixSQL(t, h.store)
h.s.db = m.open(t)
h.s.tables = fdbTables(t)
h.handler = h.s.Handler()
env := &matrixEnv{t: t, h: h, sql: m, users: map[string]userRow{}, tokens: map[string]string{}}
for _, u := range matrixUserSeed {
env.users[u.name] = h.user(u.name, u.role)
env.tokens[u.name] = h.login(env.users[u.name])
}
gone := env.users["gone"]
gone.DeletedAt = sql.NullTime{Time: webNow.Add(-time.Hour), Valid: true}
h.store.setUser(gone)
env.users["gone"] = gone
for _, d := range []struct{ zone, user string }{{mxZone, "alice"}, {mxZone, "bob"}, {mxZone, "gone"}, {mxStale, "alice"}} {
h.store.delegate(d.zone, env.users[d.user])
}
for _, r := range matrixSeedRecords() {
m.addRecord(r)
}
return env
}
func mxJSON(v any) string {
b, err := json.Marshal(v)
if err != nil {
panic(err)
}
return string(b)
}
func mxRecord(r recordRow) string {
return fmt.Sprintf(`{"id":%d,"zone":%s,"name":%s,"ttl":%d,"record_type":%s,"content":%s}`,
r.ID, mxJSON(r.Zone), mxJSON(r.Name), r.TTL.Int64, mxJSON(r.RecordType), mxJSON(r.Content.String))
}
func mxRecordWith(id int64, content string) string {
r := matrixSeed(id)
r.Content.String = content
return mxRecord(r)
}
func mxArray(items ...string) string {
return "[" + strings.Join(items, ",") + "]"
}
func mxUser(id int64, name, role, created string, deleted string) string {
d := "null"
if deleted != "" {
d = mxJSON(deleted)
}
return fmt.Sprintf(`{"id":%d,"username":%s,"role":%s,"created_at":%s,"deleted_at":%s}`, id, mxJSON(name), mxJSON(role), mxJSON(created), d)
}
func mxDelegation(zone, username string) string {
return fmt.Sprintf(`{"zone":%s,"username":%s}`, mxJSON(zone), mxJSON(username))
}
func mxSeedUser(name string) string {
for i, u := range matrixUserSeed {
if u.name == name {
return mxUser(int64(i+1), u.name, u.role, mxCreated, "")
}
}
panic(name)
}
func mxUserID(name string) int64 {
for i, u := range matrixUserSeed {
if u.name == name {
return int64(i + 1)
}
}
panic(name)
}