623 lines
45 KiB
Go
623 lines
45 KiB
Go
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
// Copyright (C) 2026 Bryan Joshua Pedini
|
|
|
|
package main
|
|
|
|
import (
|
|
"errors"
|
|
"strconv"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func mxSQLFail(err error, match ...string) func(*matrixEnv) {
|
|
return func(env *matrixEnv) { env.sql.fail(err, -1, match...) }
|
|
}
|
|
|
|
func mxStoreFail(method string, err error) func(*matrixEnv) {
|
|
return func(env *matrixEnv) { env.h.store.failOn[method] = err }
|
|
}
|
|
|
|
func mxBusy(env *matrixEnv) {
|
|
env.sql.answer(fdbResponse{err: errLockTimeout}, "webui_locks")
|
|
}
|
|
|
|
func mxCommitFails(env *matrixEnv) {
|
|
env.sql.fdb.commitErr = errMatrixInternal
|
|
}
|
|
|
|
func mxUnchanged(t *testing.T, env *matrixEnv, _ webResult) {
|
|
t.Helper()
|
|
if got, want := env.sql.rows(), matrixSeedRecords(); len(got) != len(want) {
|
|
t.Fatalf("records changed: %d rows", len(got))
|
|
} else {
|
|
for i := range got {
|
|
if got[i] != want[i] {
|
|
t.Fatalf("record %d changed: %+v", want[i].ID, got[i])
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
const (
|
|
mxRecordBody = `{"name":"new","type":"A","ttl":60,"data":{"ip":"192.0.2.10"}}`
|
|
mxUpdateBody = `{"name":"www","type":"A","ttl":300,"data":{"ip":"192.0.2.9"}}`
|
|
mxSOABody = `{"ns":"ns.example.net","mbox":"hostmaster@example.org","ttl":3600,"refresh":3600,"retry":700,"expire":604800,"minttl":300}`
|
|
mxCreateStale = `{"name":"Stale.ORG","ns":"ns.example.net","mbox":"hostmaster@stale.org","extra_ns":["backup.example.net"],"refresh":3600,"retry":600,"expire":604800,"minttl":300,"ttl":3600}`
|
|
mxCreateNew = `{"name":"new.org","ns":"ns.example.net","mbox":"hostmaster@new.org","refresh":3600,"retry":600,"expire":604800,"minttl":300,"ttl":3600}`
|
|
)
|
|
|
|
func mxNewRecord(id int64) string {
|
|
return `{"id":` + itoa(id) + `,"zone":"example.org.","name":"new","ttl":60,"record_type":"A","content":"{\"ip\":\"192.0.2.10\"}"}`
|
|
}
|
|
|
|
func itoa(n int64) string {
|
|
return strconv.FormatInt(n, 10)
|
|
}
|
|
|
|
func mxBulk(ids ...int64) string {
|
|
var items []string
|
|
for _, id := range ids {
|
|
items = append(items, `{"id":`+itoa(id)+`,"etag":`+mxJSON(recordETag(matrixSeed(id)))+`}`)
|
|
}
|
|
return `{"records":[` + strings.Join(items, ",") + `]}`
|
|
}
|
|
|
|
func matrixAccountCases() []matrixCase {
|
|
alice := mxUserID("alice")
|
|
pw := `{"current":"` + testPassword + `","password":"brand-new-password"}`
|
|
return []matrixCase{
|
|
{name: "admin", op: opAccountRead, method: "GET", path: "/account", persona: "admin", status: 200,
|
|
resp: `{"username":"admin","role":"admin","zones":[]}`},
|
|
{name: "user without stale zones", op: opAccountRead, method: "GET", path: "/account", persona: "alice", status: 200,
|
|
resp: `{"username":"alice","role":"user","zones":["example.org."]}`},
|
|
{name: "zones failure", op: opAccountRead, method: "GET", path: "/account", persona: "alice", status: 500, msg: msgInternalError,
|
|
setup: mxStoreFail("UserZones", errMatrixInternal)},
|
|
{name: "zones unavailable", op: opAccountRead, method: "GET", path: "/account", persona: "alice", status: 503, msg: msgDatabaseDown,
|
|
setup: mxStoreFail("UserZones", errDatabase)},
|
|
|
|
{name: "changed", op: opPasswordChange, method: "PUT", path: "/account/password", persona: "alice", body: pw, status: 204,
|
|
target: alice, before: mxSeedUser("alice"), after: mxSeedUser("alice"),
|
|
check: func(t *testing.T, env *matrixEnv, _ webResult) {
|
|
if env.h.store.user(alice).PasswordHash != fakeHash("brand-new-password") {
|
|
t.Fatal("password not changed")
|
|
}
|
|
}},
|
|
{name: "malformed", op: opPasswordChange, method: "PUT", path: "/account/password", persona: "alice", body: `{"current":`, status: 400, msg: "malformed JSON"},
|
|
{name: "unknown field", op: opPasswordChange, method: "PUT", path: "/account/password", persona: "alice", body: `{"x":1}`, status: 400, msg: "request body has an unknown field"},
|
|
{name: "missing body", op: opPasswordChange, method: "PUT", path: "/account/password", persona: "alice", status: 400, msg: "request body is required"},
|
|
{name: "wrong current", op: opPasswordChange, method: "PUT", path: "/account/password", persona: "alice",
|
|
body: `{"current":"wrong-password-1","password":"brand-new-password"}`, status: 422, msg: "invalid password change"},
|
|
{name: "short new", op: opPasswordChange, method: "PUT", path: "/account/password", persona: "alice",
|
|
body: `{"current":"` + testPassword + `","password":"short"}`, status: 422, msg: "invalid password change"},
|
|
{name: "changed meanwhile", op: opPasswordChange, method: "PUT", path: "/account/password", persona: "alice", body: pw,
|
|
status: 409, msg: msgPasswordChanged, target: alice,
|
|
setup: func(env *matrixEnv) {
|
|
env.h.store.onTx = func(s *fakeWebState) { s.users[alice-1].PasswordHash = fakeHash("reset-by-admin") }
|
|
}},
|
|
{name: "deleted meanwhile", tag: "in handler", op: opPasswordChange, method: "PUT", path: "/account/password", persona: "alice", body: pw,
|
|
status: 401, msg: msgAuthRequired, target: alice,
|
|
setup: func(env *matrixEnv) {
|
|
env.h.store.onTx = func(s *fakeWebState) { s.users[alice-1].DeletedAt.Valid = true }
|
|
}},
|
|
{name: "hash failure", op: opPasswordChange, method: "PUT", path: "/account/password", persona: "alice", body: pw, status: 500, msg: msgInternalError,
|
|
setup: func(env *matrixEnv) {
|
|
env.h.s.hashPassword = func(string) (string, error) { return "", errMatrixInternal }
|
|
}},
|
|
{name: "change audit failure", op: opPasswordChange, method: "PUT", path: "/account/password", persona: "alice", body: pw,
|
|
status: 500, msg: msgInternalError, target: alice,
|
|
setup: func(env *matrixEnv) {
|
|
env.h.store.failTimes["InsertAudit"] = 1
|
|
env.h.store.failOn["InsertAudit"] = errMatrixInternal
|
|
},
|
|
check: func(t *testing.T, env *matrixEnv, _ webResult) {
|
|
if env.h.store.user(alice).PasswordHash != fakeHash(testPassword) {
|
|
t.Fatal("password changed without audit")
|
|
}
|
|
}},
|
|
{name: "unavailable", op: opPasswordChange, method: "PUT", path: "/account/password", persona: "alice", body: pw,
|
|
status: 503, msg: msgDatabaseDown, target: alice, setup: mxStoreFail("InTx", errDatabase)},
|
|
|
|
{name: "admin", op: opAuditRead, method: "GET", path: "/audit?limit=1", persona: "admin", status: 200,
|
|
resp: `{"entries":[],"next_before_id":null}`},
|
|
{name: "user", op: opAuditRead, method: "GET", path: "/audit", persona: "alice", status: 200,
|
|
resp: `{"entries":[],"next_before_id":null}`},
|
|
{name: "bad kind", op: opAuditRead, method: "GET", path: "/audit?kind=other", persona: "alice", status: 400, msg: "invalid parameter kind"},
|
|
{name: "bad before id", op: opAuditRead, method: "GET", path: "/audit?before_id=9007199254740992", persona: "admin", status: 400, msg: "invalid parameter before_id"},
|
|
{name: "list failure", op: opAuditRead, method: "GET", path: "/audit", persona: "admin", status: 500, msg: msgInternalError,
|
|
setup: mxStoreFail("ListAudit", errMatrixInternal)},
|
|
{name: "zones unavailable", op: opAuditRead, method: "GET", path: "/audit", persona: "alice", status: 503, msg: msgDatabaseDown,
|
|
setup: mxStoreFail("UserZones", errDatabase)},
|
|
}
|
|
}
|
|
|
|
func matrixZoneCases() []matrixCase {
|
|
zoneRows := []string{"SELECT `id`", zoneRowsWhere}
|
|
newSOA := func(id int64, zone, mbox string) string {
|
|
return `{"id":` + itoa(id) + `,"zone":"` + zone + `","name":"","ttl":3600,"record_type":"SOA","content":"{\"ns\":\"ns.example.net.\",\"MBox\":\"` +
|
|
mbox + `\",\"refresh\":3600,\"retry\":600,\"expire\":604800,\"minttl\":300,\"serial\":2026111800}"}`
|
|
}
|
|
newNS := func(id int64, zone, host string) string {
|
|
return `{"id":` + itoa(id) + `,"zone":"` + zone + `","name":"","ttl":3600,"record_type":"NS","content":"{\"host\":\"` + host + `\"}"}`
|
|
}
|
|
all := []string{mxRecord(matrixSeed(1)), mxRecord(matrixSeed(2)), mxRecord(matrixSeed(3)), mxRecord(matrixSeed(4))}
|
|
return []matrixCase{
|
|
{name: "admin", op: opZoneList, method: "GET", path: "/zones", persona: "admin", status: 200,
|
|
resp: `{"zones":[{"name":"example.org.","records":4,"serial":2026111700},{"name":"other.org.","records":2,"serial":2026111700}]}`},
|
|
{name: "user", op: opZoneList, method: "GET", path: "/zones", persona: "alice", status: 200,
|
|
resp: `{"zones":[{"name":"example.org.","records":4,"serial":2026111700}]}`},
|
|
{name: "user without zones", op: opZoneList, method: "GET", path: "/zones", persona: "dave", status: 200, resp: `{"zones":[]}`},
|
|
{name: "query failure", op: opZoneList, method: "GET", path: "/zones", persona: "admin", status: 500, msg: msgInternalError,
|
|
setup: mxSQLFail(errMatrixInternal, "GROUP BY")},
|
|
{name: "connection failure", op: opZoneList, method: "GET", path: "/zones", persona: "alice", status: 503, msg: msgDatabaseDown,
|
|
setup: mxSQLFail(errMatrixNet, "GROUP BY")},
|
|
|
|
{name: "over stale delegations", op: opZoneCreate, method: "POST", path: "/zones", persona: "admin", body: mxCreateStale, status: 201,
|
|
zone: mxStale, before: mxArray(mxDelegation(mxStale, "alice")),
|
|
after: mxArray(newSOA(7, mxStale, "hostmaster.stale.org."), newNS(8, mxStale, "ns.example.net."), newNS(9, mxStale, "backup.example.net.")),
|
|
check: func(t *testing.T, env *matrixEnv, _ webResult) {
|
|
if env.h.store.hasDelegation(mxStale, mxUserID("alice")) || len(env.sql.rows()) != 9 {
|
|
t.Fatal("stale delegation kept or rows missing")
|
|
}
|
|
}},
|
|
{name: "fresh", op: opZoneCreate, method: "POST", path: "/zones", persona: "admin", body: mxCreateNew, status: 201,
|
|
zone: "new.org.", after: mxArray(newSOA(7, "new.org.", "hostmaster.new.org."), newNS(8, "new.org.", "ns.example.net."))},
|
|
{name: "malformed", op: opZoneCreate, method: "POST", path: "/zones", persona: "admin", body: `{"name":`, status: 400, msg: "malformed JSON"},
|
|
{name: "exists", op: opZoneCreate, method: "POST", path: "/zones", persona: "admin", body: strings.Replace(mxCreateNew, "new.org", "EXAMPLE.org", 1),
|
|
status: 409, msg: "zone already exists", zone: mxZone, check: mxUnchanged},
|
|
{name: "invalid name", op: opZoneCreate, method: "POST", path: "/zones", persona: "admin", body: strings.Replace(mxCreateNew, `"new.org"`, `"bad..name"`, 1),
|
|
status: 422, msg: "validation failed"},
|
|
{name: "invalid mailbox", op: opZoneCreate, method: "POST", path: "/zones", persona: "admin", body: strings.Replace(mxCreateNew, "hostmaster@new.org", "bad mailbox", 1),
|
|
status: 422, msg: "validation failed", zone: "new.org."},
|
|
{name: "addressed zone of a refusal", op: opZoneCreate, method: "POST", path: "/zones", persona: "alice", body: mxCreateNew,
|
|
status: 403, msg: msgAdminRequired, zone: "new.org."},
|
|
{name: "record insert failure", op: opZoneCreate, method: "POST", path: "/zones", persona: "admin", body: mxCreateStale,
|
|
status: 500, msg: msgInternalError, zone: mxStale, setup: mxSQLFail(errMatrixInternal, "INSERT INTO `coredns_records`"),
|
|
check: func(t *testing.T, env *matrixEnv, res webResult) {
|
|
mxUnchanged(t, env, res)
|
|
if !env.h.store.hasDelegation(mxStale, mxUserID("alice")) {
|
|
t.Fatal("rolled back creation removed the stale delegation")
|
|
}
|
|
}},
|
|
{name: "change audit failure", op: opZoneCreate, method: "POST", path: "/zones", persona: "admin", body: mxCreateNew,
|
|
status: 500, msg: msgInternalError, zone: "new.org.", setup: mxSQLFail(errMatrixInternal, "INSERT INTO `coredns_webui_audit`"), check: mxUnchanged},
|
|
{name: "zone busy", op: opZoneCreate, method: "POST", path: "/zones", persona: "admin", body: mxCreateNew,
|
|
status: 503, msg: msgZoneBusy, zone: "new.org.", setup: mxBusy},
|
|
{name: "connection failure", op: opZoneCreate, method: "POST", path: "/zones", persona: "admin", body: mxCreateNew,
|
|
status: 503, msg: msgDatabaseDown, zone: "new.org.", setup: mxSQLFail(errMatrixNet, "FOR UPDATE")},
|
|
|
|
{name: "admin", op: opZoneRead, method: "GET", path: "/zones/EXAMPLE.org", persona: "admin", status: 200, zone: mxZone,
|
|
check: matrixHeader("Etag", recordETag(matrixSeed(1)))},
|
|
{name: "user", op: opZoneRead, method: "GET", path: "/zones/example.org.", persona: "alice", status: 200, zone: mxZone},
|
|
{name: "query failure", op: opZoneRead, method: "GET", path: "/zones/example.org.", persona: "admin", status: 500, msg: msgInternalError,
|
|
zone: mxZone, setup: mxSQLFail(errMatrixInternal, zoneRows...)},
|
|
{name: "connection failure", op: opZoneRead, method: "GET", path: "/zones/example.org.", persona: "alice", status: 503, msg: msgDatabaseDown,
|
|
zone: mxZone, setup: mxSQLFail(errMatrixNet, zoneRows...)},
|
|
|
|
{name: "deleted", op: opZoneDelete, method: "DELETE", path: "/zones/example.org", persona: "admin", body: `{"confirm":"EXAMPLE.ORG"}`, status: 204,
|
|
zone: mxZone, before: mxArray(append(all, mxDelegation(mxZone, "alice"), mxDelegation(mxZone, "bob"), mxDelegation(mxZone, "gone"))...),
|
|
check: func(t *testing.T, env *matrixEnv, _ webResult) {
|
|
if len(env.sql.rows()) != 2 || env.h.store.hasDelegation(mxZone, mxUserID("alice")) {
|
|
t.Fatal("zone rows or delegations remain")
|
|
}
|
|
}},
|
|
{name: "malformed", op: opZoneDelete, method: "DELETE", path: "/zones/example.org.", persona: "admin", body: `{"confirm"`, status: 400,
|
|
msg: "malformed JSON", zone: mxZone, check: mxUnchanged},
|
|
{name: "missing body", op: opZoneDelete, method: "DELETE", path: "/zones/example.org.", persona: "admin", status: 400,
|
|
msg: "request body is required", zone: mxZone, check: mxUnchanged},
|
|
{name: "missing zone", tag: "missing", op: opZoneDelete, method: "DELETE", path: "/zones/missing.org.", persona: "admin", body: `{"confirm":"missing.org"}`,
|
|
status: 404, msg: msgZoneNotFound, zone: mxMissing},
|
|
{name: "stale zone", tag: "stale", op: opZoneDelete, method: "DELETE", path: "/zones/stale.org.", persona: "admin", body: `{"confirm":"stale.org"}`,
|
|
status: 404, msg: msgZoneNotFound, zone: mxStale,
|
|
check: func(t *testing.T, env *matrixEnv, _ webResult) {
|
|
if !env.h.store.hasDelegation(mxStale, mxUserID("alice")) {
|
|
t.Fatal("refused deletion removed the stale delegation")
|
|
}
|
|
}},
|
|
{name: "confirmation mismatch", op: opZoneDelete, method: "DELETE", path: "/zones/example.org.", persona: "admin", body: `{"confirm":"other.org"}`,
|
|
status: 422, msg: "validation failed", zone: mxZone, check: mxUnchanged},
|
|
{name: "change audit failure", op: opZoneDelete, method: "DELETE", path: "/zones/example.org.", persona: "admin", body: `{"confirm":"example.org"}`,
|
|
status: 500, msg: msgInternalError, zone: mxZone, setup: mxSQLFail(errMatrixInternal, "INSERT INTO `coredns_webui_audit`"),
|
|
check: func(t *testing.T, env *matrixEnv, res webResult) {
|
|
mxUnchanged(t, env, res)
|
|
if !env.h.store.hasDelegation(mxZone, mxUserID("alice")) {
|
|
t.Fatal("rolled back deletion removed the delegations")
|
|
}
|
|
}},
|
|
{name: "zone busy", op: opZoneDelete, method: "DELETE", path: "/zones/example.org.", persona: "admin", body: `{"confirm":"example.org"}`,
|
|
status: 503, msg: msgZoneBusy, zone: mxZone, setup: mxBusy, check: mxUnchanged},
|
|
{name: "connection failure", op: opZoneDelete, method: "DELETE", path: "/zones/example.org.", persona: "admin", body: `{"confirm":"example.org"}`,
|
|
status: 503, msg: msgDatabaseDown, zone: mxZone, setup: mxSQLFail(errMatrixNet, "DELETE FROM `coredns_records`"), check: mxUnchanged},
|
|
|
|
{name: "admin", op: opZoneCheck, method: "GET", path: "/zones/example.org./check", persona: "admin", status: 200, zone: mxZone},
|
|
{name: "user", op: opZoneCheck, method: "GET", path: "/zones/example.org./check", persona: "alice", status: 200, zone: mxZone},
|
|
{name: "query failure", op: opZoneCheck, method: "GET", path: "/zones/example.org./check", persona: "admin", status: 500, msg: msgInternalError,
|
|
zone: mxZone, setup: mxSQLFail(errMatrixInternal, zoneRows...)},
|
|
{name: "connection failure", op: opZoneCheck, method: "GET", path: "/zones/example.org./check", persona: "alice", status: 503, msg: msgDatabaseDown,
|
|
zone: mxZone, setup: mxSQLFail(errMatrixNet, zoneRows...)},
|
|
|
|
{name: "admin", op: opCheckAll, method: "GET", path: "/check", persona: "admin", status: 200,
|
|
check: func(t *testing.T, _ *matrixEnv, res webResult) {
|
|
if !strings.HasPrefix(res.body, `{"problems":[`) {
|
|
t.Fatal(res.body)
|
|
}
|
|
}},
|
|
{name: "user", op: opCheckAll, method: "GET", path: "/check", persona: "alice", status: 200,
|
|
check: func(t *testing.T, _ *matrixEnv, res webResult) {
|
|
if strings.Contains(res.body, mxOther) {
|
|
t.Fatalf("check of all zones shows a foreign zone: %s", res.body)
|
|
}
|
|
}},
|
|
{name: "query failure", op: opCheckAll, method: "GET", path: "/check", persona: "admin", status: 500, msg: msgInternalError,
|
|
setup: mxSQLFail(errMatrixInternal, zoneRows...)},
|
|
{name: "connection failure", op: opCheckAll, method: "GET", path: "/check", persona: "alice", status: 503, msg: msgDatabaseDown,
|
|
setup: mxSQLFail(errMatrixNet, "GROUP BY")},
|
|
}
|
|
}
|
|
|
|
func matrixRecordCases() []matrixCase {
|
|
soa := mxRecord(matrixSeed(1))
|
|
soaNext := mxRecordWith(1, mxSOANext)
|
|
rec := func(id int64) string { return mxRecord(matrixSeed(id)) }
|
|
updated := mxRecordWith(3, `{"ip":"192.0.2.9"}`)
|
|
bulk := mxBulk
|
|
writeFail := func(op, method, path, body string, ifMatch, changed, record int64) []matrixCase {
|
|
return []matrixCase{
|
|
{name: "change audit failure", op: op, method: method, path: path, persona: "admin", body: body, ifMatch: ifMatch,
|
|
status: 500, msg: msgInternalError, zone: mxZone, record: changed,
|
|
setup: mxSQLFail(errMatrixInternal, "INSERT INTO `coredns_webui_audit`"), check: mxUnchanged},
|
|
{name: "zone busy", op: op, method: method, path: path, persona: "alice", body: body, ifMatch: ifMatch,
|
|
status: 503, msg: msgZoneBusy, zone: mxZone, record: record, setup: mxBusy, check: mxUnchanged},
|
|
{name: "connection failure", op: op, method: method, path: path, persona: "admin", body: body, ifMatch: ifMatch,
|
|
status: 503, msg: msgDatabaseDown, zone: mxZone, record: record, setup: mxSQLFail(errMatrixNet, "FOR UPDATE"), check: mxUnchanged},
|
|
{name: "store failure", op: op, method: method, path: path, persona: "admin", body: body, ifMatch: ifMatch,
|
|
status: 500, msg: msgInternalError, zone: mxZone, record: record, setup: mxSQLFail(errMatrixInternal, "LOCK IN SHARE MODE"), check: mxUnchanged},
|
|
}
|
|
}
|
|
zoneRows := []string{"SELECT `id`", zoneRowsWhere}
|
|
cases := []matrixCase{
|
|
{name: "admin", op: opRecordList, method: "GET", path: "/zones/example.org./records", persona: "admin", status: 200, zone: mxZone},
|
|
{name: "user", op: opRecordList, method: "GET", path: "/zones/example.org/records", persona: "alice", status: 200, zone: mxZone},
|
|
{name: "query failure", op: opRecordList, method: "GET", path: "/zones/example.org./records", persona: "admin", status: 500, msg: msgInternalError,
|
|
zone: mxZone, setup: mxSQLFail(errMatrixInternal, zoneRows...)},
|
|
{name: "connection failure", op: opRecordList, method: "GET", path: "/zones/example.org./records", persona: "alice", status: 503, msg: msgDatabaseDown,
|
|
zone: mxZone, setup: mxSQLFail(errMatrixNet, zoneRows...)},
|
|
|
|
{name: "admin", op: opRecordCreate, method: "POST", path: "/zones/example.org./records", persona: "admin", body: mxRecordBody, status: 201,
|
|
zone: mxZone, record: 7, before: mxArray(soa), after: mxArray(soaNext, mxNewRecord(7))},
|
|
{name: "delegated user", op: opRecordCreate, method: "POST", path: "/zones/Example.Org/records", persona: "alice", body: mxRecordBody, status: 201,
|
|
zone: mxZone, record: 7, before: mxArray(soa), after: mxArray(soaNext, mxNewRecord(7))},
|
|
{name: "malformed", op: opRecordCreate, method: "POST", path: "/zones/example.org./records", persona: "alice", body: `{"name":`, status: 400,
|
|
msg: "malformed JSON", zone: mxZone, check: mxUnchanged},
|
|
{name: "invalid address", op: opRecordCreate, method: "POST", path: "/zones/example.org./records", persona: "alice",
|
|
body: strings.Replace(mxRecordBody, "192.0.2.10", "2001:db8::1", 1), status: 422, msg: "validation failed", zone: mxZone, check: mxUnchanged},
|
|
{name: "string ttl", op: opRecordCreate, method: "POST", path: "/zones/example.org./records", persona: "alice",
|
|
body: strings.Replace(mxRecordBody, "60", `"60"`, 1), status: 422, msg: "validation failed", zone: mxZone, check: mxUnchanged},
|
|
{name: "CNAME next to an address", op: opRecordCreate, method: "POST", path: "/zones/example.org./records", persona: "admin",
|
|
body: `{"name":"www","type":"CNAME","ttl":60,"data":{"host":"target.example.net."}}`, status: 422, msg: "validation failed", zone: mxZone, check: mxUnchanged},
|
|
{name: "commit failure", op: opRecordCreate, method: "POST", path: "/zones/example.org./records", persona: "admin", body: mxRecordBody,
|
|
status: 500, msg: msgInternalError, zone: mxZone,
|
|
setup: mxCommitFails, check: mxUnchanged},
|
|
|
|
{name: "replaced", op: opRecordUpdate, method: "PUT", path: "/zones/example.org./records/3", persona: "alice", body: mxUpdateBody, ifMatch: 3,
|
|
status: 200, zone: mxZone, record: 3, before: mxArray(soa, rec(3)), after: mxArray(soaNext, updated),
|
|
check: matrixHeader("Etag", recordETag(recordRow{ID: 3, Zone: mxZone, Name: "www", TTL: matrixSeed(3).TTL, RecordType: "A",
|
|
Content: matrixText(`{"ip":"192.0.2.9"}`, true)}))},
|
|
{name: "unchanged", op: opRecordUpdate, method: "PUT", path: "/zones/example.org./records/3", persona: "admin",
|
|
body: `{"name":"www","type":"A","ttl":300,"data":{"ip":"192.0.2.1"}}`, ifMatch: 3, status: 200, zone: mxZone, record: 3, check: mxUnchanged},
|
|
{name: "invalid id", op: opRecordUpdate, method: "PUT", path: "/zones/example.org./records/x", persona: "admin", body: mxUpdateBody, ifMatch: 3,
|
|
status: 400, msg: "invalid record id", zone: mxZone, check: mxUnchanged},
|
|
{name: "id out of range", op: opRecordUpdate, method: "PUT", path: "/zones/example.org./records/4294967296", persona: "admin", body: mxUpdateBody, ifMatch: 3,
|
|
status: 400, msg: "invalid record id", zone: mxZone, check: mxUnchanged},
|
|
{name: "malformed", op: opRecordUpdate, method: "PUT", path: "/zones/example.org./records/3", persona: "admin", body: `{"name"`, ifMatch: 3,
|
|
status: 400, msg: "malformed JSON", zone: mxZone, record: 3, check: mxUnchanged},
|
|
{name: "record of another zone", tag: "record", op: opRecordUpdate, method: "PUT", path: "/zones/example.org./records/6", persona: "admin", body: mxUpdateBody, ifMatch: 6,
|
|
status: 404, msg: "record not found", zone: mxZone, record: 6, check: mxUnchanged},
|
|
{name: "record gone", op: opRecordUpdate, method: "PUT", path: "/zones/example.org./records/99", persona: "admin", body: mxUpdateBody, ifMatch: 3,
|
|
status: 412, msg: "record changed", zone: mxZone, record: 99, check: mxUnchanged},
|
|
{name: "stale ETag", op: opRecordUpdate, method: "PUT", path: "/zones/example.org./records/3", persona: "alice", body: mxUpdateBody, ifMatch: 4,
|
|
status: 412, msg: "record changed", zone: mxZone, record: 3, check: mxUnchanged},
|
|
{name: "no If-Match", op: opRecordUpdate, method: "PUT", path: "/zones/example.org./records/3", persona: "alice", body: mxUpdateBody,
|
|
status: 428, msg: "If-Match is required", zone: mxZone, record: 3, check: mxUnchanged},
|
|
{name: "invalid value", op: opRecordUpdate, method: "PUT", path: "/zones/example.org./records/3", persona: "alice",
|
|
body: strings.Replace(mxUpdateBody, "192.0.2.9", "999.0.2.9", 1), ifMatch: 3, status: 422, msg: "validation failed", zone: mxZone, record: 3, check: mxUnchanged},
|
|
|
|
{name: "deleted", op: opRecordDelete, method: "DELETE", path: "/zones/example.org./records/4", persona: "alice", ifMatch: 4,
|
|
status: 200, zone: mxZone, record: 4, before: mxArray(soa, rec(4)), after: mxArray(soaNext)},
|
|
{name: "invalid id", op: opRecordDelete, method: "DELETE", path: "/zones/example.org./records/0", persona: "admin", ifMatch: 4,
|
|
status: 400, msg: "invalid record id", zone: mxZone, check: mxUnchanged},
|
|
{name: "record of another zone", tag: "record", op: opRecordDelete, method: "DELETE", path: "/zones/example.org./records/5", persona: "admin", ifMatch: 5,
|
|
status: 404, msg: "record not found", zone: mxZone, record: 5, check: mxUnchanged},
|
|
{name: "stale ETag", op: opRecordDelete, method: "DELETE", path: "/zones/example.org./records/4", persona: "admin", ifMatch: 3,
|
|
status: 412, msg: "record changed", zone: mxZone, record: 4, check: mxUnchanged},
|
|
{name: "no If-Match", op: opRecordDelete, method: "DELETE", path: "/zones/example.org./records/4", persona: "admin",
|
|
status: 428, msg: "If-Match is required", zone: mxZone, record: 4, check: mxUnchanged},
|
|
{name: "last SOA", op: opRecordDelete, method: "DELETE", path: "/zones/example.org./records/1", persona: "admin", ifMatch: 1,
|
|
status: 422, msg: "validation failed", zone: mxZone, record: 1, check: mxUnchanged},
|
|
|
|
{name: "deleted", op: opRecordBulkDelete, method: "POST", path: "/zones/example.org./records/delete", persona: "alice", body: bulk(3, 4),
|
|
status: 200, zone: mxZone, before: mxArray(soa, rec(3), rec(4)), after: mxArray(soaNext), resp: `{"deleted":2,"warnings":[]}`},
|
|
{name: "malformed", op: opRecordBulkDelete, method: "POST", path: "/zones/example.org./records/delete", persona: "alice", body: `{"records":[`,
|
|
status: 400, msg: "malformed JSON", zone: mxZone, check: mxUnchanged},
|
|
{name: "record of another zone", tag: "record", op: opRecordBulkDelete, method: "POST", path: "/zones/example.org./records/delete", persona: "alice",
|
|
body: bulk(3, 6), status: 404, msg: "record not found", zone: mxZone, check: mxUnchanged},
|
|
{name: "stale ETag", op: opRecordBulkDelete, method: "POST", path: "/zones/example.org./records/delete", persona: "alice",
|
|
body: `{"records":[{"id":4,"etag":` + mxJSON(recordETag(matrixSeed(3))) + `}]}`, status: 412, msg: "record changed", zone: mxZone, check: mxUnchanged},
|
|
{name: "empty", op: opRecordBulkDelete, method: "POST", path: "/zones/example.org./records/delete", persona: "alice", body: `{"records":[]}`,
|
|
status: 422, msg: "validation failed", zone: mxZone, check: mxUnchanged},
|
|
{name: "last apex NS", op: opRecordBulkDelete, method: "POST", path: "/zones/example.org./records/delete", persona: "admin", body: bulk(2),
|
|
status: 422, msg: "validation failed", zone: mxZone, check: mxUnchanged},
|
|
|
|
{name: "updated", op: opSOAUpdate, method: "PUT", path: "/zones/example.org./soa", persona: "alice", body: mxSOABody, ifMatch: 1,
|
|
status: 200, zone: mxZone, record: 1, before: soa, after: mxRecordWith(1, strings.Replace(mxSOANext, `"retry":600`, `"retry":700`, 1))},
|
|
{name: "unchanged", op: opSOAUpdate, method: "PUT", path: "/zones/example.org./soa", persona: "admin",
|
|
body: strings.Replace(mxSOABody, "700", "600", 1), ifMatch: 1, status: 200, zone: mxZone, record: 1, check: mxUnchanged},
|
|
{name: "malformed", op: opSOAUpdate, method: "PUT", path: "/zones/example.org./soa", persona: "admin", body: `[`, ifMatch: 1,
|
|
status: 400, msg: "request body must be a JSON object", zone: mxZone, record: 1, check: mxUnchanged},
|
|
{name: "stale ETag", op: opSOAUpdate, method: "PUT", path: "/zones/example.org./soa", persona: "admin", body: mxSOABody, ifMatch: 2,
|
|
status: 412, msg: "record changed", zone: mxZone, record: 1, check: mxUnchanged},
|
|
{name: "no If-Match", op: opSOAUpdate, method: "PUT", path: "/zones/example.org./soa", persona: "admin", body: mxSOABody,
|
|
status: 428, msg: "If-Match is required", zone: mxZone, record: 1, check: mxUnchanged},
|
|
{name: "invalid timer", op: opSOAUpdate, method: "PUT", path: "/zones/example.org./soa", persona: "admin",
|
|
body: strings.Replace(mxSOABody, "700", "2147483648", 1), ifMatch: 1, status: 422, msg: "validation failed", zone: mxZone, record: 1, check: mxUnchanged},
|
|
}
|
|
cases = append(cases, writeFail(opRecordCreate, "POST", "/zones/example.org./records", mxRecordBody, 0, 0, 0)...)
|
|
cases = append(cases, writeFail(opRecordUpdate, "PUT", "/zones/example.org./records/3", mxUpdateBody, 3, 3, 3)...)
|
|
cases = append(cases, writeFail(opRecordDelete, "DELETE", "/zones/example.org./records/4", "", 4, 4, 4)...)
|
|
cases = append(cases, writeFail(opRecordBulkDelete, "POST", "/zones/example.org./records/delete", bulk(3, 4), 0, 0, 0)...)
|
|
cases = append(cases, writeFail(opSOAUpdate, "PUT", "/zones/example.org./soa", mxSOABody, 1, 1, 0)...)
|
|
return cases
|
|
}
|
|
|
|
func matrixFileCases() []matrixCase {
|
|
fingerprint := zoneFingerprint(matrixSeedRecords()[:4])
|
|
merge := `{"mode":"merge","content":"new 60 A 192.0.2.10\n"}`
|
|
apply := `{"mode":"merge","content":"new 60 A 192.0.2.10\n","fingerprint":"` + fingerprint + `"}`
|
|
zoneRows := []string{"SELECT `id`", zoneRowsWhere}
|
|
cases := []matrixCase{
|
|
{name: "admin", op: opZoneExport, method: "GET", path: "/zones/example.org./export", persona: "admin", status: 200, zone: mxZone,
|
|
check: matrixHeader("Content-Disposition", `attachment; filename="example.org.zone"`)},
|
|
{name: "user", op: opZoneExport, method: "GET", path: "/zones/example.org./export", persona: "alice", status: 200, zone: mxZone,
|
|
check: matrixHeader("Content-Type", "text/plain")},
|
|
{name: "query failure", op: opZoneExport, method: "GET", path: "/zones/example.org./export", persona: "admin", status: 500, msg: msgInternalError,
|
|
zone: mxZone, setup: mxSQLFail(errMatrixInternal, zoneRows...)},
|
|
{name: "connection failure", op: opZoneExport, method: "GET", path: "/zones/example.org./export", persona: "alice", status: 503, msg: msgDatabaseDown,
|
|
zone: mxZone, setup: mxSQLFail(errMatrixNet, zoneRows...)},
|
|
|
|
{name: "merge", op: opZoneImportPreview, method: "POST", path: "/zones/example.org./import/preview", persona: "alice", body: merge,
|
|
status: 200, zone: mxZone, check: func(t *testing.T, env *matrixEnv, res webResult) {
|
|
mxUnchanged(t, env, res)
|
|
if !strings.Contains(res.body, `"fingerprint":"`+fingerprint+`"`) {
|
|
t.Fatal(res.body)
|
|
}
|
|
}},
|
|
{name: "malformed", op: opZoneImportPreview, method: "POST", path: "/zones/example.org./import/preview", persona: "alice", body: `{"mode":`,
|
|
status: 400, msg: "malformed JSON", zone: mxZone},
|
|
{name: "missing content", op: opZoneImportPreview, method: "POST", path: "/zones/example.org./import/preview", persona: "alice", body: `{"mode":"merge"}`,
|
|
status: 422, msg: "validation failed", zone: mxZone},
|
|
{name: "parse error", op: opZoneImportPreview, method: "POST", path: "/zones/example.org./import/preview", persona: "alice",
|
|
body: `{"mode":"merge","content":"new 60 A not-an-address\n"}`, status: 422, msg: "the file has errors", zone: mxZone},
|
|
{name: "query failure", op: opZoneImportPreview, method: "POST", path: "/zones/example.org./import/preview", persona: "admin", body: merge,
|
|
status: 500, msg: msgInternalError, zone: mxZone, setup: mxSQLFail(errMatrixInternal, zoneRows...)},
|
|
{name: "connection failure", op: opZoneImportPreview, method: "POST", path: "/zones/example.org./import/preview", persona: "admin", body: merge,
|
|
status: 503, msg: msgDatabaseDown, zone: mxZone, setup: mxSQLFail(errMatrixNet, zoneRows...)},
|
|
|
|
{name: "merge", op: opZoneImport, method: "POST", path: "/zones/example.org./import/apply", persona: "alice", body: apply,
|
|
status: 200, zone: mxZone, before: mxArray(mxRecord(matrixSeed(1))), after: mxArray(mxRecordWith(1, mxSOANext), mxNewRecord(7)),
|
|
resp: `{"added":1,"deleted":0,"updated":0,"warnings":[]}`},
|
|
{name: "malformed", op: opZoneImport, method: "POST", path: "/zones/example.org./import/apply", persona: "alice", body: `{"mode":`,
|
|
status: 400, msg: "malformed JSON", zone: mxZone, check: mxUnchanged},
|
|
{name: "fingerprint changed", op: opZoneImport, method: "POST", path: "/zones/example.org./import/apply", persona: "alice",
|
|
body: strings.Replace(apply, fingerprint, strings.Repeat("0", len(fingerprint)), 1), status: 409, msg: "zone changed since preview", zone: mxZone, check: mxUnchanged},
|
|
{name: "invalid mode", op: opZoneImport, method: "POST", path: "/zones/example.org./import/apply", persona: "alice",
|
|
body: strings.Replace(apply, "merge", "append", 1), status: 422, msg: "validation failed", zone: mxZone, check: mxUnchanged},
|
|
{name: "parse error", op: opZoneImport, method: "POST", path: "/zones/example.org./import/apply", persona: "alice",
|
|
body: strings.Replace(apply, "192.0.2.10", "not-an-address", 1), status: 422, msg: "the file has errors", zone: mxZone, check: mxUnchanged},
|
|
}
|
|
for _, c := range []matrixCase{
|
|
{name: "change audit failure", persona: "admin", status: 500, msg: msgInternalError, setup: mxSQLFail(errMatrixInternal, "INSERT INTO `coredns_webui_audit`")},
|
|
{name: "zone busy", persona: "alice", status: 503, msg: msgZoneBusy, setup: mxBusy},
|
|
{name: "connection failure", persona: "admin", status: 503, msg: msgDatabaseDown, setup: mxSQLFail(errMatrixNet, "INSERT INTO `coredns_records`")},
|
|
} {
|
|
c.op, c.method, c.path, c.body, c.zone, c.check = opZoneImport, "POST", "/zones/example.org./import/apply", apply, mxZone, mxUnchanged
|
|
cases = append(cases, c)
|
|
}
|
|
return cases
|
|
}
|
|
|
|
func matrixDelegationCases() []matrixCase {
|
|
carol, alice, bob, gone, root, dave := mxUserID("carol"), mxUserID("alice"), mxUserID("bob"), mxUserID("gone"), mxUserID("root"), mxUserID("dave")
|
|
demoteActor := func(env *matrixEnv) {
|
|
env.h.store.onLock = func(s *fakeWebState) { s.users[0].Role = roleUser }
|
|
}
|
|
return []matrixCase{
|
|
{name: "listed", op: opDelegationList, method: "GET", path: "/zones/example.org./users", persona: "admin", status: 200, zone: mxZone,
|
|
resp: `{"users":[{"username":"alice","role":"user"},{"username":"bob","role":"user"}]}`},
|
|
{name: "missing zone", tag: "missing", op: opDelegationList, method: "GET", path: "/zones/missing.org./users", persona: "admin", status: 404,
|
|
msg: msgZoneNotFound, zone: mxMissing},
|
|
{name: "stale zone", tag: "stale", op: opDelegationList, method: "GET", path: "/zones/stale.org./users", persona: "admin", status: 404,
|
|
msg: msgZoneNotFound, zone: mxStale},
|
|
{name: "list failure", op: opDelegationList, method: "GET", path: "/zones/example.org./users", persona: "admin", status: 500,
|
|
msg: msgInternalError, zone: mxZone, setup: mxStoreFail("ZoneUsers", errMatrixInternal)},
|
|
{name: "unavailable", op: opDelegationList, method: "GET", path: "/zones/example.org./users", persona: "admin", status: 503,
|
|
msg: msgDatabaseDown, zone: mxZone, setup: mxStoreFail("ZoneExists", errDatabase)},
|
|
|
|
{name: "granted", op: opDelegationGrant, method: "PUT", path: "/zones/Example.org/users/carol", persona: "admin", status: 204,
|
|
zone: mxZone, target: carol, after: mxDelegation(mxZone, "carol"),
|
|
check: func(t *testing.T, env *matrixEnv, _ webResult) {
|
|
if !env.h.store.hasDelegation(mxZone, carol) {
|
|
t.Fatal("delegation missing")
|
|
}
|
|
}},
|
|
{name: "already granted", op: opDelegationGrant, method: "PUT", path: "/zones/example.org./users/alice", persona: "admin", status: 204,
|
|
zone: mxZone, target: alice},
|
|
{name: "missing zone", tag: "missing", op: opDelegationGrant, method: "PUT", path: "/zones/missing.org./users/carol", persona: "admin", status: 404,
|
|
msg: msgZoneNotFound, zone: mxMissing},
|
|
{name: "stale zone", tag: "stale", op: opDelegationGrant, method: "PUT", path: "/zones/stale.org./users/carol", persona: "admin", status: 404,
|
|
msg: msgZoneNotFound, zone: mxStale},
|
|
{name: "unknown user", tag: "user", op: opDelegationGrant, method: "PUT", path: "/zones/example.org./users/nobody", persona: "admin", status: 404,
|
|
msg: msgUserNotFound, zone: mxZone},
|
|
{name: "invalid username", tag: "user", op: opDelegationGrant, method: "PUT", path: "/zones/example.org./users/Bad%21", persona: "admin", status: 404,
|
|
msg: msgUserNotFound, zone: mxZone},
|
|
{name: "deleted user", tag: "user", op: opDelegationGrant, method: "PUT", path: "/zones/example.org./users/gone", persona: "admin", status: 404,
|
|
msg: msgUserNotFound, zone: mxZone, target: gone},
|
|
{name: "admin target", op: opDelegationGrant, method: "PUT", path: "/zones/example.org./users/root", persona: "admin", status: 422,
|
|
msg: "zones cannot be delegated to admins", zone: mxZone, target: root},
|
|
{name: "actor demoted meanwhile", op: opDelegationGrant, method: "PUT", path: "/zones/example.org./users/carol", persona: "admin", status: 403,
|
|
msg: msgAdminRequired, zone: mxZone, target: carol, setup: demoteActor},
|
|
{name: "change audit failure", op: opDelegationGrant, method: "PUT", path: "/zones/example.org./users/carol", persona: "admin", status: 500,
|
|
msg: msgInternalError, zone: mxZone, target: carol,
|
|
setup: func(env *matrixEnv) {
|
|
env.h.store.failTimes["InsertAudit"] = 1
|
|
env.h.store.failOn["InsertAudit"] = errMatrixInternal
|
|
},
|
|
check: func(t *testing.T, env *matrixEnv, _ webResult) {
|
|
if env.h.store.hasDelegation(mxZone, carol) {
|
|
t.Fatal("delegation kept without audit")
|
|
}
|
|
}},
|
|
{name: "zone busy", op: opDelegationGrant, method: "PUT", path: "/zones/example.org./users/carol", persona: "admin", status: 503,
|
|
msg: msgZoneBusy, zone: mxZone, setup: mxStoreFail("InTx", errLockTimeout)},
|
|
{name: "unavailable", op: opDelegationGrant, method: "PUT", path: "/zones/example.org./users/carol", persona: "admin", status: 503,
|
|
msg: msgDatabaseDown, zone: mxZone, setup: mxStoreFail("UserByUsernameForUpdate", errDatabase)},
|
|
|
|
{name: "revoked", op: opDelegationRevoke, method: "DELETE", path: "/zones/example.org./users/bob", persona: "admin", status: 204,
|
|
zone: mxZone, target: bob, before: mxDelegation(mxZone, "bob")},
|
|
{name: "stale delegation revoked", op: opDelegationRevoke, method: "DELETE", path: "/zones/stale.org./users/alice", persona: "admin", status: 204,
|
|
zone: mxStale, target: alice, before: mxDelegation(mxStale, "alice")},
|
|
{name: "no delegation", tag: "delegation", op: opDelegationRevoke, method: "DELETE", path: "/zones/example.org./users/carol", persona: "admin", status: 404,
|
|
msg: msgDelegationAbsent, zone: mxZone, target: carol},
|
|
{name: "no delegation in a missing zone", tag: "delegation", op: opDelegationRevoke, method: "DELETE", path: "/zones/missing.org./users/dave",
|
|
persona: "admin", status: 404, msg: msgDelegationAbsent, zone: mxMissing, target: dave},
|
|
{name: "unknown user", tag: "user", op: opDelegationRevoke, method: "DELETE", path: "/zones/example.org./users/nobody", persona: "admin", status: 404,
|
|
msg: msgUserNotFound, zone: mxZone},
|
|
{name: "deleted user", tag: "user", op: opDelegationRevoke, method: "DELETE", path: "/zones/example.org./users/gone", persona: "admin", status: 404,
|
|
msg: msgUserNotFound, zone: mxZone, target: gone},
|
|
{name: "actor demoted meanwhile", op: opDelegationRevoke, method: "DELETE", path: "/zones/example.org./users/bob", persona: "admin", status: 403,
|
|
msg: msgAdminRequired, zone: mxZone, target: bob, setup: demoteActor},
|
|
{name: "revoke failure", op: opDelegationRevoke, method: "DELETE", path: "/zones/example.org./users/bob", persona: "admin", status: 500,
|
|
msg: msgInternalError, zone: mxZone, target: bob, setup: mxStoreFail("RevokeDelegation", errMatrixInternal)},
|
|
{name: "unavailable", op: opDelegationRevoke, method: "DELETE", path: "/zones/example.org./users/bob", persona: "admin", status: 503,
|
|
msg: msgDatabaseDown, zone: mxZone, setup: mxStoreFail("InTx", errDatabase)},
|
|
}
|
|
}
|
|
|
|
func matrixUserCases() []matrixCase {
|
|
dave, gone, admin := mxUserID("dave"), mxUserID("gone"), mxUserID("admin")
|
|
daveAdmin := mxUser(dave, "dave", roleAdmin, mxCreated, "")
|
|
daveDeleted := mxUser(dave, "dave", roleUser, mxCreated, mxNow)
|
|
newbie := `{"username":"newbie","role":"user","password":"long-enough-password"}`
|
|
auditFails := func(env *matrixEnv) {
|
|
env.h.store.failTimes["InsertAudit"] = 1
|
|
env.h.store.failOn["InsertAudit"] = errMatrixInternal
|
|
}
|
|
demoteActor := func(env *matrixEnv) {
|
|
env.h.store.onLock = func(s *fakeWebState) { s.users[0].Role = roleUser }
|
|
}
|
|
unchanged := func(name string) func(t *testing.T, env *matrixEnv, _ webResult) {
|
|
return func(t *testing.T, env *matrixEnv, _ webResult) {
|
|
t.Helper()
|
|
if env.h.store.user(mxUserID(name)) != env.users[name] {
|
|
t.Fatalf("user %s changed", name)
|
|
}
|
|
}
|
|
}
|
|
var cases []matrixCase
|
|
cases = append(cases, []matrixCase{
|
|
{name: "active", op: opUserList, method: "GET", path: "/users", persona: "admin", status: 200,
|
|
check: func(t *testing.T, _ *matrixEnv, res webResult) {
|
|
if strings.Contains(res.body, `"gone"`) || !strings.Contains(res.body, `{"username":"alice","role":"user","zone_count":2,`) {
|
|
t.Fatal(res.body)
|
|
}
|
|
}},
|
|
{name: "with deleted", op: opUserList, method: "GET", path: "/users?deleted=true", persona: "admin", status: 200,
|
|
check: func(t *testing.T, _ *matrixEnv, res webResult) {
|
|
if !strings.Contains(res.body, `"username":"gone"`) {
|
|
t.Fatal(res.body)
|
|
}
|
|
}},
|
|
{name: "bad flag", op: opUserList, method: "GET", path: "/users?deleted=maybe", persona: "admin", status: 400, msg: "invalid parameter deleted"},
|
|
{name: "list failure", op: opUserList, method: "GET", path: "/users", persona: "admin", status: 500, msg: msgInternalError,
|
|
setup: mxStoreFail("ListUsers", errMatrixInternal)},
|
|
{name: "unavailable", op: opUserList, method: "GET", path: "/users", persona: "admin", status: 503, msg: msgDatabaseDown,
|
|
setup: mxStoreFail("ListUsers", errDatabase)},
|
|
|
|
{name: "created", op: opUserCreate, method: "POST", path: "/users", persona: "admin", body: newbie, status: 201,
|
|
target: 8, after: mxUser(8, "newbie", roleUser, mxNow, "")},
|
|
{name: "malformed", op: opUserCreate, method: "POST", path: "/users", persona: "admin", body: `{"username"`, status: 400, msg: "malformed JSON"},
|
|
{name: "username taken", op: opUserCreate, method: "POST", path: "/users", persona: "admin", body: strings.Replace(newbie, "newbie", "gone", 1),
|
|
status: 409, msg: msgUsernameTaken},
|
|
{name: "invalid", op: opUserCreate, method: "POST", path: "/users", persona: "admin", body: `{"username":"cli","role":"owner","password":"x"}`,
|
|
status: 422, msg: "invalid user"},
|
|
{name: "change audit failure", op: opUserCreate, method: "POST", path: "/users", persona: "admin", body: newbie, status: 500,
|
|
msg: msgInternalError, setup: auditFails,
|
|
check: func(t *testing.T, env *matrixEnv, _ webResult) {
|
|
if _, err := env.h.store.view().userByUsername("newbie"); !errors.Is(err, errRowNotFound) {
|
|
t.Fatal("user created without audit")
|
|
}
|
|
}},
|
|
{name: "actor demoted meanwhile", op: opUserCreate, method: "POST", path: "/users", persona: "admin", body: newbie, status: 403,
|
|
msg: msgAdminRequired, setup: demoteActor},
|
|
{name: "unavailable", op: opUserCreate, method: "POST", path: "/users", persona: "admin", body: newbie, status: 503,
|
|
msg: msgDatabaseDown, setup: mxStoreFail("CreateUser", errDatabase)},
|
|
|
|
{name: "active", op: opUserRead, method: "GET", path: "/users/alice", persona: "admin", status: 200, target: mxUserID("alice"),
|
|
check: func(t *testing.T, _ *matrixEnv, res webResult) {
|
|
if !strings.Contains(res.body, `"zones":[{"zone":"example.org.","exists":true},{"zone":"stale.org.","exists":false}]`) {
|
|
t.Fatal(res.body)
|
|
}
|
|
}},
|
|
{name: "deleted", op: opUserRead, method: "GET", path: "/users/gone", persona: "admin", status: 200, target: gone},
|
|
{name: "unknown", tag: "user", op: opUserRead, method: "GET", path: "/users/nobody", persona: "admin", status: 404, msg: msgUserNotFound},
|
|
{name: "invalid username", tag: "user", op: opUserRead, method: "GET", path: "/users/-x", persona: "admin", status: 404, msg: msgUserNotFound},
|
|
{name: "zones failure", op: opUserRead, method: "GET", path: "/users/alice", persona: "admin", status: 500, msg: msgInternalError,
|
|
target: mxUserID("alice"), setup: mxStoreFail("UserZones", errMatrixInternal)},
|
|
{name: "unavailable", op: opUserRead, method: "GET", path: "/users/alice", persona: "admin", status: 503, msg: msgDatabaseDown,
|
|
setup: mxStoreFail("UserByUsername", errDatabase)},
|
|
}...)
|
|
type userWrite struct {
|
|
op, method, suffix, body string
|
|
status int
|
|
before, after string
|
|
}
|
|
for _, w := range []userWrite{
|
|
{opUserRole, "PUT", "/role", `{"role":"admin"}`, 204, mxSeedUser("dave"), daveAdmin},
|
|
{opUserPassword, "PUT", "/password", `{"password":"another-long-password"}`, 204, mxSeedUser("dave"), mxSeedUser("dave")},
|
|
{opUserDelete, "DELETE", "", `{"confirm":"dave"}`, 204, mxSeedUser("dave"), daveDeleted},
|
|
} {
|
|
forUser := func(name string) (string, string) {
|
|
return "/users/" + name + w.suffix, strings.Replace(w.body, `"dave"`, `"`+name+`"`, 1)
|
|
}
|
|
path, body := forUser("dave")
|
|
ownPath, ownBody := forUser("admin")
|
|
nobodyPath, nobodyBody := forUser("nobody")
|
|
gonePath, goneBody := forUser("gone")
|
|
cases = append(cases, []matrixCase{
|
|
{name: "changed", op: w.op, method: w.method, path: path, persona: "admin", body: body, status: w.status,
|
|
target: dave, before: w.before, after: w.after},
|
|
{name: "malformed", op: w.op, method: w.method, path: path, persona: "admin", body: `{"x"`, status: 400, msg: "malformed JSON", check: unchanged("dave")},
|
|
{name: "own account", op: w.op, method: w.method, path: ownPath, persona: "admin", body: ownBody, status: 403, msg: msgOwnAccount,
|
|
target: admin, check: unchanged("admin")},
|
|
{name: "unknown", tag: "user", op: w.op, method: w.method, path: nobodyPath, persona: "admin", body: nobodyBody, status: 404, msg: msgUserNotFound},
|
|
{name: "deleted", tag: "user", op: w.op, method: w.method, path: gonePath, persona: "admin", body: goneBody, status: 404, msg: msgUserNotFound,
|
|
target: gone, check: unchanged("gone")},
|
|
{name: "actor demoted meanwhile", op: w.op, method: w.method, path: path, persona: "admin", body: body, status: 403, msg: msgAdminRequired,
|
|
target: dave, setup: demoteActor, check: unchanged("dave")},
|
|
{name: "change audit failure", op: w.op, method: w.method, path: path, persona: "admin", body: body, status: 500, msg: msgInternalError,
|
|
target: dave, setup: auditFails, check: unchanged("dave")},
|
|
{name: "unavailable", op: w.op, method: w.method, path: path, persona: "admin", body: body, status: 503, msg: msgDatabaseDown,
|
|
setup: mxStoreFail("InTx", errDatabase), check: unchanged("dave")},
|
|
}...)
|
|
}
|
|
return append(cases, []matrixCase{
|
|
{name: "unchanged role", op: opUserRole, method: "PUT", path: "/users/dave/role", persona: "admin", body: `{"role":"user"}`, status: 204,
|
|
target: dave, check: unchanged("dave")},
|
|
{name: "invalid role", op: opUserRole, method: "PUT", path: "/users/dave/role", persona: "admin", body: `{"role":"owner"}`, status: 422,
|
|
msg: "invalid role", check: unchanged("dave")},
|
|
{name: "short password", op: opUserPassword, method: "PUT", path: "/users/dave/password", persona: "admin", body: `{"password":"short"}`, status: 422,
|
|
msg: "invalid password", check: unchanged("dave")},
|
|
{name: "confirmation mismatch", op: opUserDelete, method: "DELETE", path: "/users/dave", persona: "admin", body: `{"confirm":"Dave"}`, status: 422,
|
|
msg: "invalid confirm", check: unchanged("dave")},
|
|
}...)
|
|
}
|