diff --git a/backend/internal/adapter/primary/http/support_dto_test.go b/backend/internal/adapter/primary/http/support_dto_test.go new file mode 100644 index 0000000..f109b19 --- /dev/null +++ b/backend/internal/adapter/primary/http/support_dto_test.go @@ -0,0 +1,115 @@ +package httpadapter + +import ( + "encoding/json" + "os" + "reflect" + "strings" + "testing" + + "github.com/ogen-go/ogen" + + "lincodex/backend/internal/adapter/primary/http/api" +) + +// supportResponseInternals are fields that belong to staff-side moderation +// and must never reach the public support case DTO the server encodes. +var supportResponseInternals = []string{ + "moderator", "moderator_id", "moderator_name", "assignee", "assignee_id", + "actor_id", "actor_name", "internal_note", "internal_notes", "internal_comment", + "evidence", "risk_signals", "answers", "raw_answers", "contacts", "email", "phone", +} + +// supportRequestDecisions are fields that only staff may set; an own create +// must not be able to carry them. +var supportRequestDecisions = []string{ + "status", "decision", "outcome", "moderator", "moderator_id", "assignee", + "assignee_id", "priority", "response_target", "due_at", "rubric_version", + "number", "history", "user_id", "account_id", "email", "reply_to", "contact", +} + +func TestOwnSupportCaseRoundTripsWithoutInternalFields(t *testing.T) { + fixture := supportCaseExample(t, "SupportCaseGradeAppealResolved") + + var decoded api.SupportCase + if err := decoded.UnmarshalJSON(fixture); err != nil { + t.Fatalf("decode the own-case example: %v", err) + } + if err := decoded.Validate(); err != nil { + t.Fatalf("validate the own-case example: %v", err) + } + encoded, err := decoded.MarshalJSON() + if err != nil { + t.Fatalf("encode the own case: %v", err) + } + + var want, got map[string]any + if err := json.Unmarshal(fixture, &want); err != nil { + t.Fatalf("read the example: %v", err) + } + if err := json.Unmarshal(encoded, &got); err != nil { + t.Fatalf("read the encoded case: %v", err) + } + for _, field := range []string{"number", "status", "response_target"} { + if !reflect.DeepEqual(got[field], want[field]) { + t.Errorf("%s changed in the round trip: got %v, want %v", field, got[field], want[field]) + } + } + if !reflect.DeepEqual(got, want) { + t.Errorf("the round trip changed the case:\n got %s\nwant %s", encoded, fixture) + } + + // The server encodes only generated fields, and the decoder refuses a + // field the contract does not declare. + withInternal := strings.Replace(string(fixture), "{", `{"internal_note":"escalated",`, 1) + if err := new(api.SupportCase).UnmarshalJSON([]byte(withInternal)); err == nil { + t.Error("a case with an internal note decoded without an error") + } +} + +func TestSupportDTOsHaveNoInternalOrDecisionFields(t *testing.T) { + for _, dto := range []any{ + api.SupportCase{}, api.SupportCaseSummary{}, api.SupportCaseEvent{}, api.SupportCaseDecision{}, + } { + assertNoJSONFields(t, dto, supportResponseInternals) + } + for _, dto := range []any{ + api.SupportCaseCreateSupport{}, api.SupportCaseCreateComplaint{}, + api.SupportCaseCreateGradeAppeal{}, api.SupportCaseCreateAnticheatAppeal{}, + api.SupportCaseCreateManualAssessment{}, + } { + assertNoJSONFields(t, dto, supportRequestDecisions) + } +} + +func assertNoJSONFields(t *testing.T, dto any, forbidden []string) { + t.Helper() + typ := reflect.TypeOf(dto) + for i := range typ.NumField() { + name, _, _ := strings.Cut(typ.Field(i).Tag.Get("json"), ",") + for _, field := range forbidden { + if name == field { + t.Errorf("%s exposes the field %q", typ.Name(), name) + } + } + } +} + +// supportCaseExample returns a named example from the public contract bundle, +// so the fixture cannot drift from the documented contract. +func supportCaseExample(t *testing.T, name string) []byte { + t.Helper() + data, err := os.ReadFile(publicContractBundle) + if err != nil { + t.Fatalf("read the public contract bundle: %v", err) + } + spec, err := ogen.Parse(data) + if err != nil { + t.Fatalf("parse the public contract bundle: %v", err) + } + example, ok := spec.Components.Examples[name] + if !ok || example == nil || len(example.Value) == 0 { + t.Fatalf("the contract has no value for the example %s", name) + } + return []byte(example.Value) +}