feat(api): add candidate assessment attempt contract

This commit is contained in:
shira committed 2026-10-06 03:28:44 +03:00
1 parent 6c86c4e559
commit 565ef8d4f5
100 files changed
+30314 -74

No files matched your search

@@ -564,6 +564,198 @@ func TestCatalogAndQuestionnaireAccessBeforeImplementation(t *testing.T) {
}
}
// Assessment request fixtures shared by the assessment tests.
const (
assessmentAttemptsPath = "/api/v1/me/assessment-attempts"
assessmentAttemptPath = assessmentAttemptsPath + "/01928f7a-3c4d-7e5f-8a9b-0c1d2e3f4a5b"
assessmentAnswersPath = assessmentAttemptPath + "/answers"
assessmentFinalizePath = assessmentAttemptPath + "/finalize"
assessmentResultPath = assessmentAttemptPath + "/result"
assessmentItemID = "01928f7a-4a10-7b2c-9d3e-5f6a7b8c9d01"
validAttemptCreate = `{"specialization":"backend","target_grade":"junior","task_language":"go"}`
)
// assessmentAnswerBody returns a SubmitMyAssessmentAnswer body for version 1
// of the attempt with the given answer.
func assessmentAnswerBody(answer string) string {
return `{"expected_version":1,"item_id":"` + assessmentItemID + `","answer":` + answer + `}`
}
// The schema alone must accept every answer format of FR05 and reject what a
// candidate may never send: a score, a seed, a mode, a format mixed with the
// fields of another format, or a missing version or Idempotency-Key. Security
// accepts the session here, so the request reaches validation; a valid request
// then answers 501 until the assessment is implemented.
func TestAssessmentSchemaValidationPrecedesImplementation(t *testing.T) {
session := map[string]string{"Cookie": testSessionCookie, "X-CSRF-Token": testCSRFToken}
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: "availability",
request: identityRequest{method: http.MethodGet, path: "/api/v1/me/assessment-availability", headers: session},
},
{
name: "create",
request: identityRequest{method: http.MethodPost, path: assessmentAttemptsPath, body: validAttemptCreate, headers: command},
},
{
name: "create without a task language choice",
request: identityRequest{method: http.MethodPost, path: assessmentAttemptsPath, body: `{"specialization":"network_engineer","target_grade":"middle","task_language":null}`, headers: command},
},
{
// The server picks the mode from the published bank.
name: "create with a mode chosen by the client",
request: identityRequest{method: http.MethodPost, path: assessmentAttemptsPath, body: strings.Replace(validAttemptCreate, `}`, `,"mode":"demo_basic"}`, 1), headers: command},
location: api.ProblemFieldLocationBody, pointer: "", code: "invalid",
},
{
name: "create for a grade outside the MVP scale",
request: identityRequest{method: http.MethodPost, path: assessmentAttemptsPath, body: strings.Replace(validAttemptCreate, `"junior"`, `"lead"`, 1), headers: command},
location: api.ProblemFieldLocationBody, pointer: "/target_grade", code: "invalid",
},
{
name: "create without an Idempotency-Key",
request: identityRequest{method: http.MethodPost, path: assessmentAttemptsPath, body: validAttemptCreate, headers: session},
location: api.ProblemFieldLocationHeader, pointer: "/Idempotency-Key", code: "required",
},
{
name: "resume",
request: identityRequest{method: http.MethodGet, path: assessmentAttemptPath, headers: session},
},
{
name: "resume with an identifier that is not a UUID",
request: identityRequest{method: http.MethodGet, path: assessmentAttemptsPath + "/1", headers: session},
location: api.ProblemFieldLocationPath, pointer: "/attempt_id", code: "invalid",
},
{
name: "choice answer",
request: identityRequest{method: http.MethodPost, path: assessmentAnswersPath, body: assessmentAnswerBody(`{"format":"choice","option_ids":["x9"]}`), headers: command},
},
{
name: "number answer",
request: identityRequest{method: http.MethodPost, path: assessmentAnswersPath, body: assessmentAnswerBody(`{"format":"number","number":254}`), headers: command},
},
{
name: "string answer",
request: identityRequest{method: http.MethodPost, path: assessmentAnswersPath, body: assessmentAnswerBody(`{"format":"string","text":"Retry-After"}`), headers: command},
},
{
name: "order answer",
request: identityRequest{method: http.MethodPost, path: assessmentAnswersPath, body: assessmentAnswerBody(`{"format":"order","element_ids":["b1","a7","d3"]}`), headers: command},
},
{
name: "find-error answer",
request: identityRequest{method: http.MethodPost, path: assessmentAnswersPath, body: assessmentAnswerBody(`{"format":"find_error","line_numbers":[5]}`), headers: command},
},
{
name: "configuration answer",
request: identityRequest{method: http.MethodPost, path: assessmentAnswersPath, body: assessmentAnswerBody(`{"format":"configuration","config":"interface Gi0/1"}`), headers: command},
},
{
name: "practice answer",
request: identityRequest{method: http.MethodPost, path: assessmentAnswersPath, body: assessmentAnswerBody(`{"format":"practice","language":"python","source_code":"print(1)"}`), headers: command},
},
{
// The server scores the answer with the key of the item instance.
name: "answer with a score set by the candidate",
request: identityRequest{method: http.MethodPost, path: assessmentAnswersPath, body: strings.Replace(assessmentAnswerBody(`{"format":"choice","option_ids":["x9"]}`), `{"expected_version"`, `{"score":1,"expected_version"`, 1), headers: command},
location: api.ProblemFieldLocationBody, pointer: "", code: "invalid",
},
{
name: "practice answer with a seed",
request: identityRequest{method: http.MethodPost, path: assessmentAnswersPath, body: assessmentAnswerBody(`{"format":"practice","language":"python","source_code":"print(1)","seed":42}`), headers: command},
location: api.ProblemFieldLocationBody, pointer: "", code: "invalid",
},
{
name: "format with the fields of another format",
request: identityRequest{method: http.MethodPost, path: assessmentAnswersPath, body: assessmentAnswerBody(`{"format":"choice","number":254}`), headers: command},
location: api.ProblemFieldLocationBody, pointer: "", code: "invalid",
},
{
name: "unknown answer format",
request: identityRequest{method: http.MethodPost, path: assessmentAnswersPath, body: assessmentAnswerBody(`{"format":"essay","text":"..."}`), headers: command},
location: api.ProblemFieldLocationBody, pointer: "/answer/format", code: "invalid",
},
{
name: "choice answer without an option",
request: identityRequest{method: http.MethodPost, path: assessmentAnswersPath, body: assessmentAnswerBody(`{"format":"choice","option_ids":[]}`), headers: command},
location: api.ProblemFieldLocationBody, pointer: "/answer/option_ids", code: "invalid",
},
{
name: "answer without an expected version",
request: identityRequest{method: http.MethodPost, path: assessmentAnswersPath, body: `{"item_id":"` + assessmentItemID + `","answer":{"format":"number","number":1}}`, headers: command},
location: api.ProblemFieldLocationBody, pointer: "/expected_version", code: "required",
},
{
name: "answer without an Idempotency-Key",
request: identityRequest{method: http.MethodPost, path: assessmentAnswersPath, body: assessmentAnswerBody(`{"format":"number","number":1}`), headers: session},
location: api.ProblemFieldLocationHeader, pointer: "/Idempotency-Key", code: "required",
},
{
name: "finalize",
request: identityRequest{method: http.MethodPost, path: assessmentFinalizePath, body: `{"expected_version":17}`, headers: command},
},
{
name: "finalize with version zero",
request: identityRequest{method: http.MethodPost, path: assessmentFinalizePath, body: `{"expected_version":0}`, headers: command},
location: api.ProblemFieldLocationBody, pointer: "/expected_version", code: "invalid",
},
{
name: "result",
request: identityRequest{method: http.MethodGet, path: assessmentResultPath, headers: session},
},
} {
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 assessment operation requires a candidate session; DenyAllSecurity
// grants none, with or without a cookie and a CSRF token.
func TestAssessmentAccessBeforeImplementation(t *testing.T) {
command := map[string]string{"Cookie": testSessionCookie, "X-CSRF-Token": testCSRFToken, "Idempotency-Key": testIdempotencyKey}
for _, test := range []identityRequest{
{method: http.MethodGet, path: "/api/v1/me/assessment-availability"},
{method: http.MethodPost, path: assessmentAttemptsPath, body: validAttemptCreate, headers: command},
{method: http.MethodGet, path: assessmentAttemptPath},
{method: http.MethodGet, path: assessmentAttemptPath, headers: map[string]string{"Cookie": testSessionCookie}},
{method: http.MethodPost, path: assessmentAnswersPath, body: assessmentAnswerBody(`{"format":"number","number":1}`), headers: command},
{method: http.MethodPost, path: assessmentFinalizePath, body: `{"expected_version":1}`, headers: command},
{method: http.MethodGet, path: assessmentResultPath},
} {
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))