feat(api): add specialization catalog and candidate questionnaire contract

This commit is contained in:
shira committed 2026-10-06 02:51:23 +03:00
1 parent c338c0c188
commit faeeb5f157
64 files changed
+8339 -47

No files matched your search

@@ -411,6 +411,159 @@ func TestGeneratedServerSecurityOrder(t *testing.T) {
}
}
const questionnairePath = "/api/v1/me/questionnaire"
// Questionnaire specialization items shared by the questionnaire tests.
const (
backendWithGo = `{"specialization":"backend","self_assessed_grade":"middle","task_languages":["go"],"stack":["PostgreSQL"],"experience":{"commercial_months":30,"academic_months":12}}`
networkEngineer = `{"specialization":"network_engineer","self_assessed_grade":"junior","task_languages":[],"stack":[],"experience":null}`
frontendInterest = `{"specialization":"frontend","self_assessed_grade":"junior","task_languages":[],"stack":["React"],"experience":null}`
)
// questionnaireBody returns a SaveMyQuestionnaire body with the given
// specialization items.
func questionnaireBody(specializations ...string) string {
return `{"expected_version":0,"specializations":[` + strings.Join(specializations, ",") + `],"work_context":{"work_formats":["remote"]}}`
}
// The schema alone must reject the questionnaire values that FR03 forbids:
// more than two specializations, a grade outside the scale, a task language
// that is not a bank language, and a confirmed grade set by the candidate.
// Security accepts the session here, so the request reaches body validation;
// a valid body then answers 501 until the questionnaire is implemented.
func TestQuestionnaireSchemaValidationPrecedesImplementation(t *testing.T) {
for _, test := range []struct {
name string
body string
statusCode int
pointer string
code string
}{
{
// Go is a task language of the backend specialization, not a
// category of its own.
name: "Backend with Go and Network Engineer",
body: questionnaireBody(backendWithGo, networkEngineer),
statusCode: http.StatusNotImplemented,
},
{
name: "three specializations",
body: questionnaireBody(backendWithGo, networkEngineer, frontendInterest),
statusCode: http.StatusBadRequest,
pointer: "/specializations",
code: "invalid",
},
{
name: "no specialization",
body: questionnaireBody(),
statusCode: http.StatusBadRequest,
pointer: "/specializations",
code: "invalid",
},
{
name: "grade outside the MVP scale",
body: questionnaireBody(strings.Replace(backendWithGo, `"middle"`, `"lead"`, 1)),
statusCode: http.StatusBadRequest,
pointer: "/specializations/0/self_assessed_grade",
code: "invalid",
},
{
// A language is never a specialization code.
name: "language as a specialization",
body: questionnaireBody(strings.Replace(backendWithGo, `"backend"`, `"go"`, 1)),
statusCode: http.StatusBadRequest,
pointer: "/specializations/0/specialization",
code: "invalid",
},
{
name: "task language outside the bank languages",
body: questionnaireBody(strings.Replace(backendWithGo, `["go"]`, `["rust"]`, 1)),
statusCode: http.StatusBadRequest,
pointer: "/specializations/0/task_languages/0",
code: "invalid",
},
{
// The decoder does not record the path of an unknown field in a
// nested object, so the pointer is the whole body.
name: "confirmed grade set by the candidate",
body: questionnaireBody(strings.Replace(backendWithGo, `"stack"`, `"confirmed_grade":"senior","stack"`, 1)),
statusCode: http.StatusBadRequest,
pointer: "",
code: "invalid",
},
{
name: "missing expected version",
body: strings.Replace(questionnaireBody(backendWithGo), `"expected_version":0,`, "", 1),
statusCode: http.StatusBadRequest,
pointer: "/expected_version",
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, identityRequest{
method: http.MethodPut, path: questionnairePath, body: test.body,
headers: map[string]string{"Cookie": testSessionCookie, "X-CSRF-Token": testCSRFToken},
})
if test.statusCode == http.StatusNotImplemented {
requireProblem(t, response, http.StatusNotImplemented, api.ProblemCodeNotImplemented)
return
}
body := requireProblem(t, response, test.statusCode, api.ProblemCodeValidationFailed)
want := api.ProblemFieldError{In: api.ProblemFieldLocationBody, 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)
})
}
}
// The catalog is public; the questionnaire stays closed without a session.
func TestCatalogAndQuestionnaireAccessBeforeImplementation(t *testing.T) {
for _, test := range []struct {
name string
request identityRequest
statusCode int
code api.ProblemCode
}{
{
name: "public catalog",
request: identityRequest{method: http.MethodGet, path: "/api/v1/specializations"},
statusCode: http.StatusNotImplemented,
code: api.ProblemCodeNotImplemented,
},
{
name: "questionnaire read without a session",
request: identityRequest{method: http.MethodGet, path: questionnairePath},
statusCode: http.StatusUnauthorized,
code: api.ProblemCodeUnauthenticated,
},
{
name: "questionnaire save with a denied session",
request: identityRequest{
method: http.MethodPut, path: questionnairePath, body: questionnaireBody(backendWithGo),
headers: map[string]string{"Cookie": testSessionCookie, "X-CSRF-Token": testCSRFToken},
},
statusCode: http.StatusUnauthorized,
code: api.ProblemCodeUnauthenticated,
},
} {
t.Run(test.name, func(t *testing.T) {
router := newTestRouter(t, func(context.Context) error { return nil })
requireProblem(t, serveIdentityRequest(t, router, test.request), test.statusCode, test.code)
})
}
}
func TestPublicOperationInternalErrorIsLoggedButNotExposed(t *testing.T) {
var logs bytes.Buffer
logger := slog.New(slog.NewJSONHandler(&logs, nil))