test(18-07): framework unit coverage of money, settings secrets, tabs, richeditor and danger actions (task 2)
- money_field_test.go: compile gates (moved from the smoke file and extended), model column gate, save-time value check, list projection - settings secret projection and put without a database; tabs, settings richeditor sanitize and the danger flag as internal unit tests - Postgres: TestMoneyFieldSave, TestSettingsSecretPostgres and the Phase 18 payload cases of TestPhase10OpenAPIConformance - vitest: money.ts helpers spec, MoneyField blur-only and server-error cases - gate arrays and the cabana README Testing section name every test
This commit is contained in:
@@ -229,6 +229,9 @@ func TestPhase10OpenAPIConformance(t *testing.T) {
|
||||
if rec := env.send(t, http.MethodPost, "/auth/login", map[string]string{"login": env.login, "password": adminTestPassword}, false); rec.Code != http.StatusOK {
|
||||
t.Fatalf("re-login status=%d body=%s", rec.Code, rec.Body.String())
|
||||
}
|
||||
// Phase 18 payloads on the acme.payments fixture.
|
||||
conformPhase18(t, spec)
|
||||
|
||||
for _, tc := range companionsErrorCases() {
|
||||
t.Run(fmt.Sprintf("%s %d %s", tc.key, tc.status, tc.name), func(t *testing.T) {
|
||||
rec := tc.call(t, env)
|
||||
@@ -252,6 +255,66 @@ func TestPhase10OpenAPIConformance(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// conformPhase18 checks the Phase 18 payload shapes against admin.json on
|
||||
// the acme.payments fixture (D-07, D-13): the settings GET and PUT with
|
||||
// meta.secrets, and a form schema with a money field, each decoded with
|
||||
// unknown fields disallowed into the type admin.json documents.
|
||||
func conformPhase18(t *testing.T, spec conformSpecDoc) {
|
||||
t.Helper()
|
||||
e := newPaymentsEnv(t)
|
||||
strict := func(t *testing.T, key string, rec *httptest.ResponseRecorder, out any, ref string) {
|
||||
t.Helper()
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("%s status=%d body=%s", key, rec.Code, rec.Body.String())
|
||||
}
|
||||
dec := json.NewDecoder(bytes.NewReader(rec.Body.Bytes()))
|
||||
dec.DisallowUnknownFields()
|
||||
if err := dec.Decode(out); err != nil {
|
||||
t.Fatalf("%s body does not match its documented type: %v\n%s", key, err, rec.Body.String())
|
||||
}
|
||||
method, path, _ := strings.Cut(key, " ")
|
||||
if got := spec.ref(path, strings.ToLower(method), "200"); got != ref {
|
||||
t.Fatalf("admin.json documents %q for %s 200, the handler writes %s", got, key, ref)
|
||||
}
|
||||
}
|
||||
t.Run("PUT /settings/{code} meta.secrets", func(t *testing.T) {
|
||||
var body cabana.Envelope[cabana.SettingsResult]
|
||||
strict(t, "PUT /settings/{code}", e.send(t, http.MethodPut, "/settings/vault", map[string]any{"api_key": "sk_conform", "notes": "<p onclick=\"x()\">n</p>"}, true), &body, "cabana.Envelope-cabana_SettingsResult")
|
||||
if body.Data.Meta == nil || body.Data.Meta.Secrets["api_key"] != cabana.SecretStored || len(body.Data.Meta.Notices) != 1 {
|
||||
t.Fatalf("meta = %+v", body.Data.Meta)
|
||||
}
|
||||
})
|
||||
t.Run("GET /settings/{code} meta.secrets", func(t *testing.T) {
|
||||
var body cabana.Envelope[cabana.SettingsResult]
|
||||
strict(t, "GET /settings/{code}", e.send(t, http.MethodGet, "/settings/vault", nil, true), &body, "cabana.Envelope-cabana_SettingsResult")
|
||||
if _, leaked := body.Data.Data["api_key"]; leaked || body.Data.Meta == nil || body.Data.Meta.Secrets["api_key"] != cabana.SecretStored || body.Data.Meta.Secrets["test_api_key"] != cabana.SecretEmpty {
|
||||
t.Fatalf("settings = %+v", body.Data)
|
||||
}
|
||||
})
|
||||
t.Run("GET /{vendor}/{plugin}/{controller}/schema/form money", func(t *testing.T) {
|
||||
var body cabana.Envelope[cabana.FormView]
|
||||
strict(t, "GET /{vendor}/{plugin}/{controller}/schema/form", e.send(t, http.MethodGet, "/acme/payments/invoices/schema/form", nil, true), &body, "cabana.Envelope-cabana_FormView")
|
||||
found := 0
|
||||
for _, f := range body.Data.Fields {
|
||||
switch f.Name {
|
||||
case "total":
|
||||
if f.Type != "money" || f.CurrencyFrom != "currency" || f.Scale != 4 || f.Max != "999999.9999" {
|
||||
t.Fatalf("total = %+v", f)
|
||||
}
|
||||
found++
|
||||
case "fee":
|
||||
if f.Type != "money" || f.Currency != "EUR" || f.Scale != 2 {
|
||||
t.Fatalf("fee = %+v", f)
|
||||
}
|
||||
found++
|
||||
}
|
||||
}
|
||||
if found != 2 {
|
||||
t.Fatalf("money fields = %+v", body.Data.Fields)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// companionsErrorCase is one documented error answer of a Phase 17 route.
|
||||
type companionsErrorCase struct {
|
||||
key string
|
||||
|
||||
Reference in New Issue
Block a user