feat(api): add invitation, terms version, acceptance, and contact grant contract

This commit is contained in:
shira committed 2026-10-06 13:48:35 +03:00
1 parent b856d4ff54
commit d2c6f1375a
140 files changed
+39767 -303

No files matched your search

@@ -483,12 +483,12 @@ func TestQuestionnaireSchemaValidationPrecedesImplementation(t *testing.T) {
code: "invalid",
},
{
// The decoder does not record the path of an unknown field in a
// nested object, so the pointer is the whole body.
// An unknown field inside an array element points at the array:
// the decoder does not report the element index.
name: "confirmed grade set by the candidate",
body: questionnaireBody(strings.Replace(backendWithGo, `"stack"`, `"confirmed_grade":"senior","stack"`, 1)),
statusCode: http.StatusBadRequest,
pointer: "",
pointer: "/specializations",
code: "invalid",
},
{
@@ -670,12 +670,12 @@ func TestAssessmentSchemaValidationPrecedesImplementation(t *testing.T) {
{
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",
location: api.ProblemFieldLocationBody, pointer: "/answer", 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",
location: api.ProblemFieldLocationBody, pointer: "/answer", code: "invalid",
},
{
name: "unknown answer format",
@@ -881,22 +881,22 @@ func TestNeedSchemaValidationPrecedesImplementation(t *testing.T) {
location: api.ProblemFieldLocationBody, pointer: "/salary/value/period", code: "invalid",
},
{
// The decoder does not record the path of a missing field in a
// nested object, so the pointer is the whole body.
// The responder rebuilds the path of a missing nested field from
// the decoder's field wrappers.
name: "salary without a tax basis",
request: create(withSalary(`{"value":{"salary_from":120000,"salary_to":160000,"currency":"RUB","period":"month"},"status":"confirmed"}`)),
location: api.ProblemFieldLocationBody, pointer: "", code: "invalid",
location: api.ProblemFieldLocationBody, pointer: "/salary/value/tax_basis", code: "required",
},
{
// A bare range would confirm it implicitly; the status is explicit.
name: "salary range without a decision",
request: create(withSalary(netSalaryRange)),
location: api.ProblemFieldLocationBody, pointer: "", code: "invalid",
location: api.ProblemFieldLocationBody, pointer: "/salary", code: "invalid",
},
{
name: "grade as a bare value",
request: create(strings.Replace(validNeedCreate, `{"value":"junior","status":"confirmed"}`, `"junior"`, 1)),
location: api.ProblemFieldLocationBody, pointer: "", code: "invalid",
location: api.ProblemFieldLocationBody, pointer: "/grade", code: "invalid",
},
{
name: "grade with an unknown status",
@@ -916,13 +916,20 @@ func TestNeedSchemaValidationPrecedesImplementation(t *testing.T) {
{
name: "candidate minimum inside the salary range",
request: create(withSalary(`{"value":{"salary_from":120000,"salary_to":160000,"currency":"RUB","period":"month","tax_basis":"net","candidate_minimum":130000},"status":"confirmed"}`)),
location: api.ProblemFieldLocationBody, pointer: "", code: "invalid",
location: api.ProblemFieldLocationBody, pointer: "/salary/value", code: "invalid",
},
{
name: "skill with an unknown status",
request: create(strings.Replace(validNeedCreate, `{"name":"Go","status":"confirmed"}`, `{"name":"Go","status":"unknown"}`, 1)),
location: api.ProblemFieldLocationBody, pointer: "/required_skills/0/status", code: "invalid",
},
{
// The decoder reports no index for an array element, so the
// pointer stops at the array.
name: "skill without a status",
request: create(strings.Replace(validNeedCreate, `{"name":"Go","status":"confirmed"}`, `{"name":"Go"}`, 1)),
location: api.ProblemFieldLocationBody, pointer: "/required_skills", code: "invalid",
},
{
name: "empty list of work formats",
request: create(strings.Replace(validNeedCreate, `["remote"]`, `[]`, 1)),
@@ -951,12 +958,12 @@ func TestNeedSchemaValidationPrecedesImplementation(t *testing.T) {
{
name: "update with a list sent as null",
request: update(`"tasks":null`),
location: api.ProblemFieldLocationBody, pointer: "", code: "invalid",
location: api.ProblemFieldLocationBody, pointer: "/tasks", code: "invalid",
},
{
name: "update with a bare grade",
request: update(`"grade":"middle"`),
location: api.ProblemFieldLocationBody, pointer: "", code: "invalid",
location: api.ProblemFieldLocationBody, pointer: "/grade", code: "invalid",
},
{
name: "update without an expected version",
@@ -971,7 +978,7 @@ func TestNeedSchemaValidationPrecedesImplementation(t *testing.T) {
{
name: "update with a version as text",
request: identityRequest{method: http.MethodPatch, path: organizationNeedPath, body: `{"expected_version":"1","grade":null}`, headers: session},
location: api.ProblemFieldLocationBody, pointer: "", code: "invalid",
location: api.ProblemFieldLocationBody, pointer: "/expected_version", code: "invalid",
},
{
name: "update that sets the version itself",
@@ -1288,7 +1295,7 @@ func TestSupportCaseSchemaValidationPrecedesImplementation(t *testing.T) {
{
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",
location: api.ProblemFieldLocationBody, pointer: "/object/id", code: "invalid",
},
{
name: "grade appeal without a reference",
@@ -1370,6 +1377,513 @@ func TestSupportCaseAccessBeforeImplementation(t *testing.T) {
}
}
// Invitation request fixtures shared by the invitation tests.
const (
organizationInvitationsPath = "/api/v1/organizations/01928f7a-1a2b-7c3d-8e4f-5a6b7c8d9e01/invitations"
organizationInvitationPath = organizationInvitationsPath + "/01928f7a-7a8b-7c9d-8e0f-1a2b3c4d5e6f"
myInvitationsPath = "/api/v1/me/invitations"
myInvitationPath = myInvitationsPath + "/01928f7a-7a8b-7c9d-8e0f-1a2b3c4d5e6f"
invitationSalary = `{"salary_from":200000,"salary_to":260000,"currency":"RUB","period":"month","tax_basis":"net"}`
invitationCommunication = `{"channel":"email","details":null}`
invitationPackage = `{"bonus":{"status":"provided","description":"Годовой бонус"},` +
`"equipment":{"status":"provided","description":"Ноутбук"},"training":{"status":"not_provided"},` +
`"health_insurance":{"status":"not_provided"}}`
validInvitationTerms = `{"role_title":"Middle Go-разработчик","specialization":"backend","grade":"middle",` +
`"offer_type":"full_time","tasks":["Развивать сервис выплат"],"team":"Команда платежей из шести человек",` +
`"tech_stack":["Go","PostgreSQL"],"work_formats":["remote"],"location":null,` +
`"communication_method":` + invitationCommunication + `,"salary":` + invitationSalary + `,"package":` + invitationPackage + `}`
validInvitationCreate = `{"candidate_handle":"cand_7Qm2xVb9Lr4Kp0Zt",` +
`"need":{"need_id":"01928f7a-2b3c-7d4e-9f5a-6b7c8d9e0f12","requirements_version":3},` +
`"response_period_days":7,"terms":` + validInvitationTerms + `}`
validInvitationAccept = `{"expected_version":5,"terms_version":3,"consent_c4":{"text_version":"c4-2026-10-01","accepted":true,"fields":["email"]}}`
)
// invitationCreateWithTerms returns a CreateOrganizationInvitation body whose
// terms are the valid terms with one replacement applied.
func invitationCreateWithTerms(old, replacement string) string {
return `{"candidate_handle":"cand_7Qm2xVb9Lr4Kp0Zt","terms":` + strings.Replace(validInvitationTerms, old, replacement, 1) + `}`
}
// The schema alone must accept a complete invitation and reject what the
// contract never allows: terms without a required field, a package condition
// left out instead of not_provided, a salary range without a valid RUB base,
// a response period outside 3, 7, and 14, client-set state, versions, salary
// indicator, or candidate contacts, an acceptance without terms_version or
// with an empty or implicit C4 choice, and a decline without a known reason.
// Security accepts the session here, so the request reaches validation; a
// valid request then answers 501 until invitations are implemented.
// Cross-field rules (A < B, the 10-40% width, a place of work for office or
// hybrid) and the current terms version pass the schema; the server checks
// them.
func TestInvitationSchemaValidationPrecedesImplementation(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}
create := func(body string) identityRequest {
return identityRequest{method: http.MethodPost, path: organizationInvitationsPath, body: body, headers: command}
}
update := func(body string) identityRequest {
return identityRequest{method: http.MethodPatch, path: organizationInvitationPath, body: body, headers: session}
}
post := func(path, body string) identityRequest {
return identityRequest{method: http.MethodPost, path: path, body: body, headers: command}
}
accept := func(body string) identityRequest {
return post(myInvitationPath+"/accept", body)
}
decline := func(body string) identityRequest {
return post(myInvitationPath+"/decline", body)
}
for _, test := range []struct {
name string
request identityRequest
location api.ProblemFieldLocation
pointer string
code string
}{
{
name: "list of an organization",
request: identityRequest{method: http.MethodGet, path: organizationInvitationsPath + "?limit=20", headers: session},
},
{
name: "list for an organization identifier that is not a UUID",
request: identityRequest{method: http.MethodGet, path: "/api/v1/organizations/acme/invitations", headers: session},
location: api.ProblemFieldLocationPath, pointer: "/org_id", code: "invalid",
},
{
name: "create a complete draft from a need",
request: create(validInvitationCreate),
},
{
name: "create a direct draft with the default response period",
request: create(`{"candidate_handle":"cand_7Qm2xVb9Lr4Kp0Zt","need":null,"terms":` + validInvitationTerms + `}`),
},
{
// The cross-field salary rules and the place of work for an
// office format are domain rules that the server checks after
// the schema.
name: "create with a reversed salary range",
request: create(invitationCreateWithTerms(`"salary_from":200000,"salary_to":260000`, `"salary_from":260000,"salary_to":200000`)),
},
{
name: "create with a salary range wider than 40%",
request: create(invitationCreateWithTerms(`"salary_to":260000`, `"salary_to":400000`)),
},
{
name: "create an office role without a place of work",
request: create(invitationCreateWithTerms(`"work_formats":["remote"]`, `"work_formats":["office"]`)),
},
{
name: "create without an Idempotency-Key",
request: identityRequest{method: http.MethodPost, path: organizationInvitationsPath, body: validInvitationCreate, headers: session},
location: api.ProblemFieldLocationHeader, pointer: "/Idempotency-Key", code: "required",
},
{
name: "create without a candidate handle",
request: create(`{"terms":` + validInvitationTerms + `}`),
location: api.ProblemFieldLocationBody, pointer: "/candidate_handle", code: "required",
},
{
name: "create for a candidate named by an email address",
request: create(strings.Replace(validInvitationCreate, `"cand_7Qm2xVb9Lr4Kp0Zt"`, `"candidate@example.com"`, 1)),
location: api.ProblemFieldLocationBody, pointer: "/candidate_handle", code: "invalid",
},
{
name: "create without terms",
request: create(`{"candidate_handle":"cand_7Qm2xVb9Lr4Kp0Zt"}`),
location: api.ProblemFieldLocationBody, pointer: "/terms", code: "required",
},
{
// The responder rebuilds the path of a missing nested field from
// the decoder's field wrappers.
name: "terms without a tax basis",
request: create(invitationCreateWithTerms(`,"tax_basis":"net"`, ``)),
location: api.ProblemFieldLocationBody, pointer: "/terms/salary/tax_basis", code: "required",
},
{
name: "terms without a communication method",
request: create(invitationCreateWithTerms(`"communication_method":`+invitationCommunication+`,`, ``)),
location: api.ProblemFieldLocationBody, pointer: "/terms/communication_method", code: "required",
},
{
name: "terms with an unknown communication channel",
request: create(invitationCreateWithTerms(`"channel":"email"`, `"channel":"carrier_pigeon"`)),
location: api.ProblemFieldLocationBody, pointer: "/terms/communication_method/channel", code: "invalid",
},
{
name: "terms without a salary",
request: create(invitationCreateWithTerms(`,"salary":`+invitationSalary, ``)),
location: api.ProblemFieldLocationBody, pointer: "/terms/salary", code: "required",
},
{
name: "terms without tasks",
request: create(invitationCreateWithTerms(`["Развивать сервис выплат"]`, `[]`)),
location: api.ProblemFieldLocationBody, pointer: "/terms/tasks", code: "invalid",
},
{
name: "terms with a task that is not a text",
request: create(invitationCreateWithTerms(`["Развивать сервис выплат"]`, `["Развивать сервис выплат",5]`)),
location: api.ProblemFieldLocationBody, pointer: "/terms/tasks", code: "invalid",
},
{
name: "terms with an unknown field",
request: create(invitationCreateWithTerms(`"role_title"`, `"benefits":"много","role_title"`)),
location: api.ProblemFieldLocationBody, pointer: "/terms", code: "invalid",
},
{
name: "terms with an empty stack",
request: create(invitationCreateWithTerms(`["Go","PostgreSQL"]`, `[]`)),
location: api.ProblemFieldLocationBody, pointer: "/terms/tech_stack", code: "invalid",
},
{
name: "terms with a place of work left out instead of null",
request: create(invitationCreateWithTerms(`"location":null,`, ``)),
location: api.ProblemFieldLocationBody, pointer: "/terms/location", code: "required",
},
{
name: "package condition left out instead of not provided",
request: create(invitationCreateWithTerms(`"training":{"status":"not_provided"},`, ``)),
location: api.ProblemFieldLocationBody, pointer: "/terms/package/training", code: "required",
},
{
name: "package condition provided without a description",
request: create(invitationCreateWithTerms(`{"status":"provided","description":"Ноутбук"}`, `{"status":"provided"}`)),
location: api.ProblemFieldLocationBody, pointer: "/terms/package/equipment/description", code: "required",
},
{
name: "package condition not provided with a description",
request: create(invitationCreateWithTerms(`"training":{"status":"not_provided"}`, `"training":{"status":"not_provided","description":"Курсы"}`)),
location: api.ProblemFieldLocationBody, pointer: "/terms/package/training", code: "invalid",
},
{
name: "package condition as a bare text",
request: create(invitationCreateWithTerms(`{"status":"provided","description":"Ноутбук"}`, `"Ноутбук"`)),
location: api.ProblemFieldLocationBody, pointer: "/terms/package/equipment", code: "invalid",
},
{
name: "salary bound that is not a multiple of 1000",
request: create(invitationCreateWithTerms(`"salary_to":260000`, `"salary_to":260500`)),
location: api.ProblemFieldLocationBody, pointer: "/terms/salary/salary_to", code: "invalid",
},
{
name: "salary bound of zero",
request: create(invitationCreateWithTerms(`"salary_from":200000`, `"salary_from":0`)),
location: api.ProblemFieldLocationBody, pointer: "/terms/salary/salary_from", code: "invalid",
},
{
name: "salary with an unknown tax basis",
request: create(invitationCreateWithTerms(`"tax_basis":"net"`, `"tax_basis":"net_or_gross"`)),
location: api.ProblemFieldLocationBody, pointer: "/terms/salary/tax_basis", code: "invalid",
},
{
name: "salary in another currency",
request: create(invitationCreateWithTerms(`"currency":"RUB"`, `"currency":"USD"`)),
location: api.ProblemFieldLocationBody, pointer: "/terms/salary/currency", code: "invalid",
},
{
name: "response period outside the policy options",
request: create(strings.Replace(validInvitationCreate, `"response_period_days":7`, `"response_period_days":10`, 1)),
location: api.ProblemFieldLocationBody, pointer: "/response_period_days", code: "invalid",
},
{
name: "candidate minimum inside the salary range",
request: create(invitationCreateWithTerms(`"tax_basis":"net"`, `"tax_basis":"net","candidate_minimum":230000`)),
location: api.ProblemFieldLocationBody, pointer: "/terms/salary", code: "invalid",
},
{
name: "candidate email in the body",
request: create(strings.Replace(validInvitationCreate, `{"candidate_handle"`, `{"candidate_email":"candidate@example.com","candidate_handle"`, 1)),
location: api.ProblemFieldLocationBody, pointer: "", code: "invalid",
},
{
name: "state set by the employer",
request: create(strings.Replace(validInvitationCreate, `{"candidate_handle"`, `{"state":"accepted","candidate_handle"`, 1)),
location: api.ProblemFieldLocationBody, pointer: "", code: "invalid",
},
{
name: "salary indicator set by the employer",
request: create(strings.Replace(validInvitationCreate, `{"candidate_handle"`, `{"salary_fit":"within_range","candidate_handle"`, 1)),
location: api.ProblemFieldLocationBody, pointer: "", code: "invalid",
},
{
name: "read of an organization",
request: identityRequest{method: http.MethodGet, path: organizationInvitationPath, headers: session},
},
{
name: "read with an invitation identifier that is not a UUID",
request: identityRequest{method: http.MethodGet, path: organizationInvitationsPath + "/1", headers: session},
location: api.ProblemFieldLocationPath, pointer: "/invitation_id", code: "invalid",
},
{
name: "update the terms of a draft",
request: update(`{"expected_version":1,"terms":` + validInvitationTerms + `}`),
},
{
name: "unlink the need of a draft",
request: update(`{"expected_version":2,"need":null}`),
},
{
name: "update without an expected version",
request: update(`{"terms":` + validInvitationTerms + `}`),
location: api.ProblemFieldLocationBody, pointer: "/expected_version", code: "required",
},
{
name: "update with version zero",
request: update(`{"expected_version":0}`),
location: api.ProblemFieldLocationBody, pointer: "/expected_version", code: "invalid",
},
{
name: "update that sets the terms version itself",
request: update(`{"expected_version":1,"terms_version":7}`),
location: api.ProblemFieldLocationBody, pointer: "", code: "invalid",
},
{
name: "update that changes the candidate",
request: update(`{"expected_version":1,"candidate_handle":"cand_Hx3Tn8Wq1Ys6Fd2Vb"}`),
location: api.ProblemFieldLocationBody, pointer: "", code: "invalid",
},
{
name: "send",
request: post(organizationInvitationPath+"/send", `{"expected_version":2}`),
},
{
name: "send without an expected version",
request: post(organizationInvitationPath+"/send", `{}`),
location: api.ProblemFieldLocationBody, pointer: "/expected_version", code: "required",
},
{
name: "send without an Idempotency-Key",
request: identityRequest{method: http.MethodPost, path: organizationInvitationPath + "/send", body: `{"expected_version":2}`, headers: session},
location: api.ProblemFieldLocationHeader, pointer: "/Idempotency-Key", code: "required",
},
{
name: "raise the upper salary bound",
request: post(organizationInvitationPath+"/salary-raise", `{"expected_version":4,"salary_to":280000}`),
},
{
name: "raise that lowers the lower bound too",
request: post(organizationInvitationPath+"/salary-raise", `{"expected_version":4,"salary_from":150000,"salary_to":280000}`),
location: api.ProblemFieldLocationBody, pointer: "", code: "invalid",
},
{
name: "raise to a bound that is not a multiple of 1000",
request: post(organizationInvitationPath+"/salary-raise", `{"expected_version":4,"salary_to":280500}`),
location: api.ProblemFieldLocationBody, pointer: "/salary_to", code: "invalid",
},
{
name: "withdraw",
request: post(organizationInvitationPath+"/withdraw", `{"expected_version":3}`),
},
{
name: "read the contacts",
request: identityRequest{method: http.MethodGet, path: organizationInvitationPath + "/contacts", headers: session},
},
{
name: "list of the candidate",
request: identityRequest{method: http.MethodGet, path: myInvitationsPath + "?limit=20", headers: session},
},
{
name: "open the terms",
request: identityRequest{method: http.MethodGet, path: myInvitationPath, headers: session},
},
{
name: "open an invitation identifier that is not a UUID",
request: identityRequest{method: http.MethodGet, path: myInvitationsPath + "/1", headers: session},
location: api.ProblemFieldLocationPath, pointer: "/invitation_id", code: "invalid",
},
{
// The view command has no body, no Idempotency-Key, and no
// expected_version: a repeat returns the current invitation.
name: "record a view",
request: identityRequest{method: http.MethodPost, path: myInvitationPath + "/view", headers: session},
},
{
name: "record a view of an identifier that is not a UUID",
request: identityRequest{method: http.MethodPost, path: myInvitationsPath + "/1/view", headers: session},
location: api.ProblemFieldLocationPath, pointer: "/invitation_id", code: "invalid",
},
{
// A stale terms version passes the schema; the server answers 412.
name: "accept with the email address only",
request: accept(validInvitationAccept),
},
{
name: "accept without a terms version",
request: accept(`{"expected_version":5,"consent_c4":{"text_version":"c4-2026-10-01","accepted":true,"fields":["email"]}}`),
location: api.ProblemFieldLocationBody, pointer: "/terms_version", code: "required",
},
{
name: "accept with terms version zero",
request: accept(strings.Replace(validInvitationAccept, `"terms_version":3`, `"terms_version":0`, 1)),
location: api.ProblemFieldLocationBody, pointer: "/terms_version", code: "invalid",
},
{
name: "accept without an expected version",
request: accept(strings.Replace(validInvitationAccept, `"expected_version":5,`, ``, 1)),
location: api.ProblemFieldLocationBody, pointer: "/expected_version", code: "required",
},
{
name: "accept without the consent C4",
request: accept(`{"expected_version":5,"terms_version":3}`),
location: api.ProblemFieldLocationBody, pointer: "/consent_c4", code: "required",
},
{
name: "accept with an empty choice of contact fields",
request: accept(strings.Replace(validInvitationAccept, `["email"]`, `[]`, 1)),
location: api.ProblemFieldLocationBody, pointer: "/consent_c4/fields", code: "invalid",
},
{
// A missing field choice is not a hidden default of every field.
name: "accept without a choice of contact fields",
request: accept(strings.Replace(validInvitationAccept, `,"fields":["email"]`, ``, 1)),
location: api.ProblemFieldLocationBody, pointer: "/consent_c4/fields", code: "required",
},
{
name: "accept with an unknown contact field",
request: accept(strings.Replace(validInvitationAccept, `["email"]`, `["passport"]`, 1)),
location: api.ProblemFieldLocationBody, pointer: "/consent_c4/fields/0", code: "invalid",
},
{
name: "accept with a repeated contact field",
request: accept(strings.Replace(validInvitationAccept, `["email"]`, `["email","email"]`, 1)),
location: api.ProblemFieldLocationBody, pointer: "/consent_c4/fields", code: "invalid",
},
{
name: "accept with consent not given",
request: accept(strings.Replace(validInvitationAccept, `"accepted":true`, `"accepted":false`, 1)),
location: api.ProblemFieldLocationBody, pointer: "/consent_c4/accepted", code: "invalid",
},
{
name: "accept that carries a contact value",
request: accept(strings.Replace(validInvitationAccept, `"fields":["email"]`, `"fields":["email"],"email":"candidate@example.com"`, 1)),
location: api.ProblemFieldLocationBody, pointer: "/consent_c4", code: "invalid",
},
{
name: "decline with the reason other and no comment",
request: decline(`{"expected_version":4,"reason":"other"}`),
},
{
name: "decline with a comment",
request: decline(`{"expected_version":4,"reason":"salary","comment":"Вилка ниже ожиданий."}`),
},
{
name: "decline without a reason",
request: decline(`{"expected_version":4,"comment":"Не подходит."}`),
location: api.ProblemFieldLocationBody, pointer: "/reason", code: "required",
},
{
name: "decline with an unknown reason",
request: decline(`{"expected_version":4,"reason":"too_old"}`),
location: api.ProblemFieldLocationBody, pointer: "/reason", code: "invalid",
},
{
name: "decline with a blank comment",
request: decline(`{"expected_version":4,"reason":"other","comment":" "}`),
location: api.ProblemFieldLocationBody, pointer: "/comment", code: "invalid",
},
{
name: "decline with a comment over 500 characters",
request: decline(`{"expected_version":4,"reason":"other","comment":"` + strings.Repeat("а", 501) + `"}`),
location: api.ProblemFieldLocationBody, pointer: "/comment", code: "invalid",
},
{
name: "decline with a comment of exactly 500 characters",
request: decline(`{"expected_version":4,"reason":"other","comment":"` + strings.Repeat("а", 500) + `"}`),
},
{
name: "cancel the participation",
request: post(myInvitationPath+"/cancel", `{"expected_version":6}`),
},
{
name: "cancel without an expected version",
request: post(myInvitationPath+"/cancel", `{}`),
location: api.ProblemFieldLocationBody, 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, 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 invitation operation requires a session; DenyAllSecurity grants none,
// with or without a cookie and a CSRF token.
func TestInvitationAccessBeforeImplementation(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 []identityRequest{
{method: http.MethodGet, path: organizationInvitationsPath},
{method: http.MethodGet, path: organizationInvitationsPath, headers: map[string]string{"Cookie": testSessionCookie}},
{method: http.MethodPost, path: organizationInvitationsPath, body: validInvitationCreate, headers: command},
{method: http.MethodGet, path: organizationInvitationPath, headers: session},
{method: http.MethodPatch, path: organizationInvitationPath, body: `{"expected_version":1,"need":null}`, headers: session},
{method: http.MethodPost, path: organizationInvitationPath + "/send", body: `{"expected_version":2}`, headers: command},
{method: http.MethodPost, path: organizationInvitationPath + "/salary-raise", body: `{"expected_version":4,"salary_to":280000}`, headers: command},
{method: http.MethodPost, path: organizationInvitationPath + "/withdraw", body: `{"expected_version":3}`, headers: command},
{method: http.MethodGet, path: organizationInvitationPath + "/contacts", headers: session},
{method: http.MethodGet, path: myInvitationsPath, headers: session},
{method: http.MethodGet, path: myInvitationPath, headers: session},
{method: http.MethodPost, path: myInvitationPath + "/view", headers: session},
{method: http.MethodPost, path: myInvitationPath + "/accept", body: validInvitationAccept, headers: command},
{method: http.MethodPost, path: myInvitationPath + "/decline", body: `{"expected_version":4,"reason":"other"}`, headers: command},
{method: http.MethodPost, path: myInvitationPath + "/cancel", body: `{"expected_version":6}`, headers: command},
} {
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)
})
}
}
// invitationResponsePrivateFields are fields that would disclose a candidate's
// contact or hidden salary minimum, or the reason of an individual decline,
// before a grant allows it. Only ContactDisclosure carries contact values.
var invitationResponsePrivateFields = []string{
"email", "phone", "telegram", "full_name", "name", "contacts", "contact",
"salary_minimum", "candidate_minimum", "minimum", "minimum_salary",
"candidate_id", "profile_id", "account_id", "user_id",
}
// The invitation DTOs that either side reads before an acceptance must not be
// able to carry a contact or the hidden minimum, and the organization's DTOs
// must not carry the candidate's decline reason or comment.
func TestInvitationDTOsCarryNoContactOrMinimum(t *testing.T) {
for _, dto := range []any{
api.OrganizationInvitation{}, api.OrganizationInvitationSummary{}, api.InvitationTerms{},
api.InvitationContactAccess{}, api.CandidateInvitation{}, api.CandidateInvitationSummary{},
api.ContactGrant{}, api.ContactConsentOffer{},
} {
assertNoJSONFields(t, dto, invitationResponsePrivateFields)
}
for _, dto := range []any{api.OrganizationInvitation{}, api.OrganizationInvitationSummary{}} {
assertNoJSONFields(t, dto, []string{"decline", "decline_reason", "reason", "comment"})
}
for _, dto := range []any{
api.OrganizationInvitationCreate{}, api.OrganizationInvitationUpdate{}, api.InvitationCommand{},
api.InvitationSalaryRaise{}, api.InvitationAcceptance{}, api.ContactConsentAcceptance{}, api.InvitationDeclineRequest{},
} {
assertNoJSONFields(t, dto, append([]string{"state", "version", "salary_fit", "contact_access", "grant_id"}, invitationResponsePrivateFields...))
}
}
func TestPublicOperationInternalErrorIsLoggedButNotExposed(t *testing.T) {
var logs bytes.Buffer
logger := slog.New(slog.NewJSONHandler(&logs, nil))