feat(api): add support case and manual assessment request contract

This commit is contained in:
shira committed 2026-10-06 10:57:36 +03:00
1 parent 2db585fbff
commit ad4bb94426
65 files changed
+14629 -23

No files matched your search

@@ -1080,6 +1080,296 @@ func TestNeedAccessBeforeImplementation(t *testing.T) {
}
}
// Support case request fixtures shared by the support case tests.
const (
supportCasesPath = "/api/v1/me/support-cases"
supportCasePath = supportCasesPath + "/01928f7a-6e7f-7a8b-9c0d-1e2f3a4b5c6d"
supportAttemptRef = `{"type":"assessment_attempt","id":"01928f7a-3c4d-7e5f-8a9b-0c1d2e3f4a5b"}`
supportOrganizationRef = `{"type":"organization","id":"01928f7a-1a2b-7c3d-8e4f-5a6b7c8d9e01"}`
supportInvitationRef = `{"type":"invitation","id":"01928f7a-7f8a-7b9c-8d0e-1f2a3b4c5d6e"}`
supportCaseRef = `{"type":"support_case","id":"01928f7a-6e7f-7a8b-9c0d-1e2f3a4b5c6d"}`
validManualAssessment = `{"type":"manual_assessment","description":"Прошу ручную оценку.","assessment_target":{"specialization":"backend","grade":"middle"}}`
)
// supportCaseBody returns a CreateMySupportCase body of the given type with a
// valid description and the given extra fields, for example `"object":{...}`.
func supportCaseBody(caseType, fields string) string {
body := `{"type":"` + caseType + `","description":"Тест на Senior недоступен."`
if fields != "" {
body += `,` + fields
}
return body + `}`
}
// The schema alone must accept the five case types with the object types each
// allows, and reject what the client may never send: an empty or blank
// description, an unknown type, an object type that the case type does not
// accept, a missing field of the type, and a status, decision, moderator, or
// contact set by the client. Security accepts the session here, so the
// request reaches validation; a valid request then answers 501 until support
// cases are implemented. Ownership of the object and the complaint reason
// that requires an invitation pass the schema; the server checks them.
func TestSupportCaseSchemaValidationPrecedesImplementation(t *testing.T) {
session := map[string]string{"Cookie": testSessionCookie}
command := map[string]string{"Cookie": testSessionCookie, "X-CSRF-Token": testCSRFToken, "Idempotency-Key": testIdempotencyKey}
for _, test := range []struct {
name string
request identityRequest
location api.ProblemFieldLocation
pointer string
code string
}{
{
name: "list",
request: identityRequest{method: http.MethodGet, path: supportCasesPath + "?limit=50&cursor=c2MtMDAwMTIz", headers: session},
},
{
name: "list with limit zero",
request: identityRequest{method: http.MethodGet, path: supportCasesPath + "?limit=0", headers: session},
location: api.ProblemFieldLocationQuery, pointer: "/limit", code: "invalid",
},
{
name: "read",
request: identityRequest{method: http.MethodGet, path: supportCasePath, headers: session},
},
{
name: "read with an identifier that is not a UUID",
request: identityRequest{method: http.MethodGet, path: supportCasesPath + "/SC-2026-000123", headers: session},
location: api.ProblemFieldLocationPath, pointer: "/case_id", code: "invalid",
},
{
name: "support without context",
request: identityRequest{method: http.MethodPost, path: supportCasesPath, body: supportCaseBody("support", ""), headers: command},
},
{
name: "support about an unavailable test",
request: identityRequest{method: http.MethodPost, path: supportCasesPath, body: supportCaseBody("support", `"assessment_target":{"specialization":"backend","grade":"senior"}`), headers: command},
},
{
name: "support about an organization",
request: identityRequest{method: http.MethodPost, path: supportCasesPath, body: supportCaseBody("support", `"object":`+supportOrganizationRef), headers: command},
},
{
name: "complaint about an invitation",
request: identityRequest{method: http.MethodPost, path: supportCasesPath, body: supportCaseBody("complaint", `"complaint_reason":"salary_terms_violation","object":`+supportInvitationRef), headers: command},
},
{
name: "grade appeal",
request: identityRequest{method: http.MethodPost, path: supportCasesPath, body: supportCaseBody("grade_appeal", `"object":`+supportAttemptRef), headers: command},
},
{
// A category that an expert assigned without an attempt is
// disputed through the resolved manual assessment case.
name: "grade appeal of a manual assessment case",
request: identityRequest{method: http.MethodPost, path: supportCasesPath, body: supportCaseBody("grade_appeal", `"object":`+supportCaseRef), headers: command},
},
{
name: "support follow-up of another case",
request: identityRequest{method: http.MethodPost, path: supportCasesPath, body: supportCaseBody("support", `"object":`+supportCaseRef), headers: command},
},
{
name: "anti-cheat appeal",
request: identityRequest{method: http.MethodPost, path: supportCasesPath, body: supportCaseBody("anticheat_appeal", `"object":`+supportAttemptRef), headers: command},
},
{
name: "manual assessment without an attempt",
request: identityRequest{method: http.MethodPost, path: supportCasesPath, body: validManualAssessment, headers: command},
},
{
name: "manual assessment with a prior attempt",
request: identityRequest{method: http.MethodPost, path: supportCasesPath, body: strings.Replace(validManualAssessment, `}}`, `},"object":`+supportAttemptRef+`}`, 1), headers: command},
},
{
name: "description with line breaks",
request: identityRequest{method: http.MethodPost, path: supportCasesPath, body: `{"type":"support","description":"Строка 1\nСтрока 2"}`, headers: command},
},
{
// ogen converts `\S` to the ECMAScript white space set, so Go and
// generated Zod both reject Unicode spaces such as U+00A0.
name: "description of only Unicode spaces",
request: identityRequest{method: http.MethodPost, path: supportCasesPath, body: `{"type":"support","description":"\u00a0\u3000\u2003"}`, headers: command},
location: api.ProblemFieldLocationBody, pointer: "/description", code: "invalid",
},
{
// U+0085 (NEL) is unicode.IsSpace but not ECMAScript white space,
// so it passes the schema. The implementation must reject it with
// a unicode.IsSpace domain check: 400 and the pointer /description.
name: "description of only NEL passes the schema",
request: identityRequest{method: http.MethodPost, path: supportCasesPath, body: `{"type":"support","description":"\u0085"}`, headers: command},
},
{
name: "empty description",
request: identityRequest{method: http.MethodPost, path: supportCasesPath, body: `{"type":"support","description":""}`, headers: command},
location: api.ProblemFieldLocationBody, pointer: "/description", code: "invalid",
},
{
name: "blank description",
request: identityRequest{method: http.MethodPost, path: supportCasesPath, body: `{"type":"support","description":" \n\t "}`, headers: command},
location: api.ProblemFieldLocationBody, pointer: "/description", code: "invalid",
},
{
name: "description longer than 4000 characters",
request: identityRequest{method: http.MethodPost, path: supportCasesPath, body: `{"type":"support","description":"` + strings.Repeat("a", 4001) + `"}`, headers: command},
location: api.ProblemFieldLocationBody, pointer: "/description", code: "invalid",
},
{
name: "missing description",
request: identityRequest{method: http.MethodPost, path: supportCasesPath, body: `{"type":"support"}`, headers: command},
location: api.ProblemFieldLocationBody, pointer: "/description", code: "required",
},
{
// The decoder selects the variant by `type` before it records a
// field path, so an unknown type points at the whole body.
name: "unknown type",
request: identityRequest{method: http.MethodPost, path: supportCasesPath, body: supportCaseBody("feedback", ""), headers: command},
location: api.ProblemFieldLocationBody, pointer: "", code: "invalid",
},
{
name: "missing type",
request: identityRequest{method: http.MethodPost, path: supportCasesPath, body: `{"description":"Тест недоступен."}`, headers: command},
location: api.ProblemFieldLocationBody, pointer: "", code: "invalid",
},
{
name: "status set by the client",
request: identityRequest{method: http.MethodPost, path: supportCasesPath, body: supportCaseBody("support", `"status":"resolved"`), headers: command},
location: api.ProblemFieldLocationBody, pointer: "", code: "invalid",
},
{
name: "decision set by the client",
request: identityRequest{method: http.MethodPost, path: supportCasesPath, body: supportCaseBody("grade_appeal", `"object":`+supportAttemptRef+`,"decision":{"outcome":"upheld","reason":"ok"}`), headers: command},
location: api.ProblemFieldLocationBody, pointer: "", code: "invalid",
},
{
name: "moderator set by the client",
request: identityRequest{method: http.MethodPost, path: supportCasesPath, body: supportCaseBody("support", `"moderator_id":"01928f7a-1a2b-7c3d-8e4f-5a6b7c8d9e01"`), headers: command},
location: api.ProblemFieldLocationBody, pointer: "", code: "invalid",
},
{
name: "response target set by the client",
request: identityRequest{method: http.MethodPost, path: supportCasesPath, body: supportCaseBody("support", `"response_target":{"due_at":"2026-10-07T09:00:00Z"}`), headers: command},
location: api.ProblemFieldLocationBody, pointer: "", code: "invalid",
},
{
// Replies go to the case and to the account address only.
name: "reply address set by the client",
request: identityRequest{method: http.MethodPost, path: supportCasesPath, body: supportCaseBody("support", `"reply_to":"other@example.com"`), headers: command},
location: api.ProblemFieldLocationBody, pointer: "", code: "invalid",
},
{
name: "grade appeal of an organization",
request: identityRequest{method: http.MethodPost, path: supportCasesPath, body: supportCaseBody("grade_appeal", `"object":`+supportOrganizationRef), headers: command},
location: api.ProblemFieldLocationBody, pointer: "/object/type", code: "invalid",
},
{
name: "complaint about an assessment attempt",
request: identityRequest{method: http.MethodPost, path: supportCasesPath, body: supportCaseBody("complaint", `"complaint_reason":"other","object":`+supportAttemptRef), headers: command},
location: api.ProblemFieldLocationBody, pointer: "/object/type", code: "invalid",
},
{
name: "anti-cheat appeal of a support case",
request: identityRequest{method: http.MethodPost, path: supportCasesPath, body: supportCaseBody("anticheat_appeal", `"object":`+supportCaseRef), headers: command},
location: api.ProblemFieldLocationBody, pointer: "/object/type", code: "invalid",
},
{
name: "complaint about a support case",
request: identityRequest{method: http.MethodPost, path: supportCasesPath, body: supportCaseBody("complaint", `"complaint_reason":"other","object":`+supportCaseRef), headers: command},
location: api.ProblemFieldLocationBody, pointer: "/object/type", code: "invalid",
},
{
name: "manual assessment based on a support case",
request: identityRequest{method: http.MethodPost, path: supportCasesPath, body: strings.Replace(validManualAssessment, `}}`, `},"object":`+supportCaseRef+`}`, 1), headers: command},
location: api.ProblemFieldLocationBody, pointer: "/object/type", code: "invalid",
},
{
name: "support about an unknown object type",
request: identityRequest{method: http.MethodPost, path: supportCasesPath, body: supportCaseBody("support", `"object":{"type":"user","id":"01928f7a-1a2b-7c3d-8e4f-5a6b7c8d9e01"}`), headers: command},
location: api.ProblemFieldLocationBody, pointer: "/object/type", code: "invalid",
},
{
name: "object reference that is not a UUID",
request: identityRequest{method: http.MethodPost, path: supportCasesPath, body: supportCaseBody("grade_appeal", `"object":{"type":"assessment_attempt","id":"42"}`), headers: command},
location: api.ProblemFieldLocationBody, pointer: "", code: "invalid",
},
{
name: "grade appeal without a reference",
request: identityRequest{method: http.MethodPost, path: supportCasesPath, body: supportCaseBody("grade_appeal", ""), headers: command},
location: api.ProblemFieldLocationBody, pointer: "/object", code: "required",
},
{
name: "complaint without a reason",
request: identityRequest{method: http.MethodPost, path: supportCasesPath, body: supportCaseBody("complaint", `"object":`+supportInvitationRef), headers: command},
location: api.ProblemFieldLocationBody, pointer: "/complaint_reason", code: "required",
},
{
name: "support with a complaint reason",
request: identityRequest{method: http.MethodPost, path: supportCasesPath, body: supportCaseBody("support", `"complaint_reason":"other"`), headers: command},
location: api.ProblemFieldLocationBody, pointer: "", code: "invalid",
},
{
name: "manual assessment without a category",
request: identityRequest{method: http.MethodPost, path: supportCasesPath, body: supportCaseBody("manual_assessment", ""), headers: command},
location: api.ProblemFieldLocationBody, pointer: "/assessment_target", code: "required",
},
{
name: "manual assessment for a grade outside the MVP scale",
request: identityRequest{method: http.MethodPost, path: supportCasesPath, body: strings.Replace(validManualAssessment, `"middle"`, `"lead"`, 1), headers: command},
location: api.ProblemFieldLocationBody, pointer: "/assessment_target/grade", code: "invalid",
},
{
name: "manual assessment with a rubric chosen by the client",
request: identityRequest{method: http.MethodPost, path: supportCasesPath, body: strings.Replace(validManualAssessment, `{"type"`, `{"rubric_version":1,"type"`, 1), headers: command},
location: api.ProblemFieldLocationBody, pointer: "", code: "invalid",
},
{
name: "create without an Idempotency-Key",
request: identityRequest{method: http.MethodPost, path: supportCasesPath, body: validManualAssessment, headers: map[string]string{"Cookie": testSessionCookie, "X-CSRF-Token": testCSRFToken}},
location: api.ProblemFieldLocationHeader, pointer: "/Idempotency-Key", code: "required",
},
} {
t.Run(test.name, func(t *testing.T) {
logger := slog.New(slog.DiscardHandler)
healthHandler := newTestHealthHandler(func(context.Context) error { return nil })
router, err := NewRouter(healthHandler, v1.NewHandler(healthHandler, problem.NewResponder(logger)), &recordingSecurity{}, problem.NewResponder(logger), metrics.New(), logger)
if err != nil {
t.Fatalf("NewRouter() error = %v", err)
}
response := serveIdentityRequest(t, router, test.request)
if test.code == "" {
requireProblem(t, response, http.StatusNotImplemented, api.ProblemCodeNotImplemented)
return
}
body := requireProblem(t, response, http.StatusBadRequest, api.ProblemCodeValidationFailed)
want := api.ProblemFieldError{In: test.location, Pointer: test.pointer, Code: test.code}
for _, field := range body.Errors {
if field.In == want.In && field.Pointer == want.Pointer && field.Code == want.Code {
return
}
}
t.Fatalf("errors = %+v, want an entry %+v", body.Errors, want)
})
}
}
// Every support case operation requires a session; DenyAllSecurity grants
// none, with or without a cookie and a CSRF token.
func TestSupportCaseAccessBeforeImplementation(t *testing.T) {
command := map[string]string{"Cookie": testSessionCookie, "X-CSRF-Token": testCSRFToken, "Idempotency-Key": testIdempotencyKey}
for _, test := range []identityRequest{
{method: http.MethodGet, path: supportCasesPath},
{method: http.MethodGet, path: supportCasesPath, headers: map[string]string{"Cookie": testSessionCookie}},
{method: http.MethodPost, path: supportCasesPath, body: validManualAssessment, headers: command},
{method: http.MethodPost, path: supportCasesPath, body: validManualAssessment, headers: map[string]string{"Idempotency-Key": testIdempotencyKey}},
{method: http.MethodGet, path: supportCasePath},
{method: http.MethodGet, path: supportCasePath, headers: map[string]string{"Cookie": testSessionCookie}},
} {
t.Run(test.method+" "+test.path, func(t *testing.T) {
router := newTestRouter(t, func(context.Context) error { return nil })
requireProblem(t, serveIdentityRequest(t, router, test), http.StatusUnauthorized, api.ProblemCodeUnauthenticated)
})
}
}
func TestPublicOperationInternalErrorIsLoggedButNotExposed(t *testing.T) {
var logs bytes.Buffer
logger := slog.New(slog.NewJSONHandler(&logs, nil))