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
This commit is contained in:
bryanpedini committed 2026-11-19 15:46:12 +01:00
1 parent a884219512
commit 9251c85fd8
14 files changed
+1631 -123

No files matched your search

+68 -65
View File
@@ -709,73 +709,71 @@ func TestAuditRawJSONFallback(t *testing.T) {
}
type matrixPersona struct {
name string
token string
name string
username string
token string
}
type matrixRoute struct {
method, path, body string
ifMatch int64
op string
access string
allowed int
}
func matrixRoutes() []matrixRoute {
out := []matrixRoute{
{"GET", "/account", "", opAccountRead, accessSession, 200},
{"PUT", "/account/password", `{"current":"` + testPassword + `","password":"another-password"}`, opPasswordChange, accessSession, 204},
{"GET", "/zones/example.org./users", "", opDelegationList, accessAdmin, 200},
{"PUT", "/zones/example.org./users/bob", "", opDelegationGrant, accessAdmin, 204},
{"DELETE", "/zones/example.org./users/carol", "", opDelegationRevoke, accessAdmin, 204},
{"GET", "/users", "", opUserList, accessAdmin, 200},
{"POST", "/users", `{"username":"newbie","role":"user","password":"long-enough-pw"}`, opUserCreate, accessAdmin, 201},
{"GET", "/users/bob", "", opUserRead, accessAdmin, 200},
{"PUT", "/users/bob/role", `{"role":"admin"}`, opUserRole, accessAdmin, 204},
{"PUT", "/users/bob/password", `{"password":"long-enough-pw"}`, opUserPassword, accessAdmin, 204},
{"DELETE", "/users/bob", `{"confirm":"bob"}`, opUserDelete, accessAdmin, 204},
{"GET", "/audit", "", opAuditRead, accessSession, 200},
return []matrixRoute{
{"GET", "/account", "", 0, opAccountRead, accessSession, 200},
{"PUT", "/account/password", `{"current":"` + testPassword + `","password":"another-password"}`, 0, opPasswordChange, accessSession, 204},
{"GET", "/zones", "", 0, opZoneList, accessSession, 200},
{"POST", "/zones", mxCreateNew, 0, opZoneCreate, accessAdmin, 201},
{"GET", "/zones/example.org.", "", 0, opZoneRead, accessZone, 200},
{"DELETE", "/zones/example.org.", `{"confirm":"example.org."}`, 0, opZoneDelete, accessAdmin, 204},
{"GET", "/zones/example.org./records", "", 0, opRecordList, accessZone, 200},
{"POST", "/zones/example.org./records", mxRecordBody, 0, opRecordCreate, accessZone, 201},
{"PUT", "/zones/example.org./records/3", mxUpdateBody, 3, opRecordUpdate, accessZone, 200},
{"DELETE", "/zones/example.org./records/4", "", 4, opRecordDelete, accessZone, 200},
{"POST", "/zones/example.org./records/delete", mxBulk(3, 4), 0, opRecordBulkDelete, accessZone, 200},
{"PUT", "/zones/example.org./soa", mxSOABody, 1, opSOAUpdate, accessZone, 200},
{"GET", "/zones/example.org./check", "", 0, opZoneCheck, accessZone, 200},
{"GET", "/check", "", 0, opCheckAll, accessSession, 200},
{"GET", "/zones/example.org./export", "", 0, opZoneExport, accessZone, 200},
{"POST", "/zones/example.org./import/preview", `{"mode":"merge","content":"new 60 A 192.0.2.10\n"}`, 0, opZoneImportPreview, accessZone, 200},
{"POST", "/zones/example.org./import/apply", `{"mode":"merge","content":"new 60 A 192.0.2.10\n","fingerprint":"` +
zoneFingerprint(matrixSeedRecords()[:4]) + `"}`, 0, opZoneImport, accessZone, 200},
{"GET", "/zones/example.org./users", "", 0, opDelegationList, accessAdmin, 200},
{"PUT", "/zones/example.org./users/carol", "", 0, opDelegationGrant, accessAdmin, 204},
{"DELETE", "/zones/example.org./users/bob", "", 0, opDelegationRevoke, accessAdmin, 204},
{"GET", "/users", "", 0, opUserList, accessAdmin, 200},
{"POST", "/users", `{"username":"newbie","role":"user","password":"long-enough-pw"}`, 0, opUserCreate, accessAdmin, 201},
{"GET", "/users/bob", "", 0, opUserRead, accessAdmin, 200},
{"PUT", "/users/bob/role", `{"role":"admin"}`, 0, opUserRole, accessAdmin, 204},
{"PUT", "/users/bob/password", `{"password":"long-enough-pw"}`, 0, opUserPassword, accessAdmin, 204},
{"DELETE", "/users/bob", `{"confirm":"bob"}`, 0, opUserDelete, accessAdmin, 204},
{"GET", "/audit", "", 0, opAuditRead, accessSession, 200},
}
for _, r := range apiRoutes {
if !slices.Contains(zoneOperations, r.Operation) {
continue
}
path := strings.NewReplacer("{zone}", "example.org.", "{id}", "7").Replace(r.Path)
out = append(out, matrixRoute{r.Method, path, "", r.Operation, r.Access, 200})
}
return out
}
func matrixHarness(t *testing.T) (*webHarness, []matrixPersona) {
h := newWebHarness(t)
h.withFakeZoneHandlers()
admin := h.user("admin", roleAdmin)
h.user("root", roleAdmin)
h.user("bob", roleUser)
carol := h.user("carol", roleUser)
delegated := h.user("alice", roleUser)
plain := h.user("dave", roleUser)
env := newMatrixEnv(t)
h := env.h
promoted := h.user("pat", roleUser)
demoted := h.user("dan", roleAdmin)
gone := h.user("gone", roleUser)
h.store.addZone(testZone)
for _, u := range []userRow{carol, delegated, promoted, gone} {
h.store.delegate(testZone, u)
}
h.store.delegate(testZone, promoted)
personas := []matrixPersona{
{"admin", h.login(admin)},
{"delegated user", h.login(delegated)},
{"user without delegation", h.login(plain)},
{"promoted user", h.login(promoted)},
{"demoted admin", h.login(demoted)},
{"deleted user", h.login(gone)},
{"no session", ""},
{"admin", "admin", env.tokens["admin"]},
{"delegated user", "alice", env.tokens["alice"]},
{"user without delegation", "dave", env.tokens["dave"]},
{"promoted user", "pat", h.login(promoted)},
{"demoted admin", "dan", h.login(demoted)},
{"deleted user", "gone", env.tokens["gone"]},
{"no session", "", ""},
}
promoted.Role = roleAdmin
h.store.setUser(promoted)
demoted.Role = roleUser
h.store.setUser(demoted)
gone.DeletedAt = sql.NullTime{Time: webNow, Valid: true}
h.store.setUser(gone)
return h, personas
}
@@ -799,11 +797,18 @@ func expectedMatrixStatus(persona string, r matrixRoute) (int, string) {
}
func TestAPIAuthorizationMatrix(t *testing.T) {
if len(matrixRoutes()) != len(apiRoutes) {
t.Fatalf("authorization matrix has %d routes, the route table %d", len(matrixRoutes()), len(apiRoutes))
}
for _, r := range matrixRoutes() {
for i := range 7 {
h, personas := matrixHarness(t)
p := personas[i]
res := h.api(r.method, r.path, p.token, r.body)
req := webRequest{method: r.method, path: apiPrefix + r.path, token: p.token, body: r.body}
if r.ifMatch != 0 {
req.header = map[string]string{"If-Match": recordETag(matrixSeed(r.ifMatch))}
}
res := h.do(req)
status, body := expectedMatrixStatus(p.name, r)
if res.status != status || (body != "" && res.body != body) {
t.Errorf("%s %s as %s: %d %s, want %d", r.method, r.path, p.name, res.status, res.body, status)
@@ -816,10 +821,10 @@ func TestAPIAuthorizationMatrix(t *testing.T) {
continue
}
e := expectOneAudit(t, res, r.op, status)
if e.Actor == "" || !e.UserID.Valid || e.Origin != "192.0.2.10" {
if e.Actor != p.username || !e.UserID.Valid || e.UserID.Int64 != h.users[p.username].ID || e.Origin != "192.0.2.10" || !e.Time.Equal(webNow) {
t.Errorf("%s as %s: %+v", r.op, p.name, e)
}
if strings.Contains(r.path, "example.org.") && (e.Zone.String != testZone || !e.Zone.Valid) && status != 403 {
if strings.Contains(r.path, "example.org.") != (e.Zone.Valid && e.Zone.String == testZone) && r.op != opZoneCreate {
t.Errorf("%s as %s: zone %v", r.op, p.name, e.Zone)
}
}
@@ -828,23 +833,21 @@ func TestAPIAuthorizationMatrix(t *testing.T) {
func TestZoneNotFoundResponsesIdentical(t *testing.T) {
h, personas := matrixHarness(t)
plain := personas[2].token
admin := personas[0].token
h.withHandlers(frameworkTestHandlers(map[string]apiHandler{
opZoneRead: func(ctx context.Context, c *apiContext) (apiResponse, error) {
ok, err := c.Server.store.ZoneExists(ctx, c.Zone)
if err != nil || !ok {
return apiResponse{}, errZoneNotFound()
}
return okJSON(map[string]string{}), nil
},
}))
a := h.api("GET", "/zones/example.org.", plain, "")
b := h.api("GET", "/zones/missing.org.", plain, "")
c := h.api("GET", "/zones/missing.org.", admin, "")
for _, r := range []webResult{a, b, c} {
if r.status != 404 || r.body != a.body || r.header.Get("Content-Type") != a.header.Get("Content-Type") || len(r.header) != len(a.header) {
t.Fatalf("%d %s %v", r.status, r.body, r.header)
admin, delegated, plain := personas[0].token, personas[1].token, personas[2].token
results := []webResult{
h.api("GET", "/zones/example.org.", plain, ""),
h.api("GET", "/zones/missing.org.", plain, ""),
h.api("GET", "/zones/missing.org.", admin, ""),
h.api("GET", "/zones/stale.org.", delegated, ""),
h.api("GET", "/zones/stale.org./records", delegated, ""),
h.api("POST", "/zones/stale.org./records", delegated, mxRecordBody),
h.api("POST", "/zones/example.org./records", plain, mxRecordBody),
h.api("PUT", "/zones/missing.org./soa", admin, mxSOABody),
}
a := results[0]
for i, r := range results {
if r.status != 404 || r.body != `{"error":"zone not found"}` || r.header.Get("Content-Type") != a.header.Get("Content-Type") || len(r.header) != len(a.header) {
t.Fatalf("response %d: %d %s %v", i, r.status, r.body, r.header)
}
}
}