feat(api): add employer need contract with requirement versions and recommendations
This commit is contained in:
1 parent
3adb0ef9e4
commit
a7d5781f2d
95 files changed
+25352
-26
No files matched your search
@@ -756,6 +756,330 @@ func TestAssessmentAccessBeforeImplementation(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// Need request fixtures shared by the need tests.
|
||||
const (
|
||||
organizationNeedsPath = "/api/v1/organizations/01928f7a-1a2b-7c3d-8e4f-5a6b7c8d9e01/needs"
|
||||
organizationNeedPath = organizationNeedsPath + "/01928f7a-2b3c-7d4e-9f5a-6b7c8d9e0f12"
|
||||
netSalaryRange = `{"salary_from":120000,"salary_to":160000,"currency":"RUB","period":"month","tax_basis":"net"}`
|
||||
validNeedCreate = `{"title":"Backend","specialization":{"value":"backend","status":"confirmed"},` +
|
||||
`"grade":{"value":"junior","status":"confirmed"},"required_skills":[{"name":"Go","status":"confirmed"}],` +
|
||||
`"work_formats":{"value":["remote"],"status":"confirmed"},"salary":{"value":` + netSalaryRange + `,"status":"confirmed"}}`
|
||||
)
|
||||
|
||||
// needUpdateBody returns an UpdateOrganizationNeed body over version 1 with
|
||||
// the given extra fields, for example `"grade":null`.
|
||||
func needUpdateBody(fields string) string {
|
||||
return `{"expected_version":1,` + fields + `}`
|
||||
}
|
||||
|
||||
// needSalary returns a confirmed salary decision for the given range bounds
|
||||
// and tax basis in RUB per month.
|
||||
func needSalary(from, to, basis string) string {
|
||||
return `{"value":{"salary_from":` + from + `,"salary_to":` + to + `,"currency":"RUB","period":"month","tax_basis":"` + basis + `"},"status":"confirmed"}`
|
||||
}
|
||||
|
||||
// The schema alone must accept a need without a vacancy and reject what an
|
||||
// employer may never send: membership, a recommendation score, or a
|
||||
// candidate's minimum, a bare value without an explicit confirmation status,
|
||||
// a salary range without a valid RUB base, and a missing or invalid
|
||||
// expected_version. Security accepts the session here, so the request
|
||||
// reaches validation; a valid request then answers 501 until needs are
|
||||
// implemented. Cross-field salary rules (A < B, a width of 10-40%) and
|
||||
// `extracted` with another value pass the schema; the server rejects them.
|
||||
func TestNeedSchemaValidationPrecedesImplementation(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: organizationNeedsPath, body: body, headers: command}
|
||||
}
|
||||
update := func(fields string) identityRequest {
|
||||
return identityRequest{method: http.MethodPatch, path: organizationNeedPath, body: needUpdateBody(fields), headers: session}
|
||||
}
|
||||
withSalary := func(salary string) string {
|
||||
return strings.Replace(validNeedCreate, `{"value":`+netSalaryRange+`,"status":"confirmed"}`, salary, 1)
|
||||
}
|
||||
for _, test := range []struct {
|
||||
name string
|
||||
request identityRequest
|
||||
location api.ProblemFieldLocation
|
||||
pointer string
|
||||
code string
|
||||
}{
|
||||
{
|
||||
name: "list",
|
||||
request: identityRequest{method: http.MethodGet, path: organizationNeedsPath + "?limit=20", headers: session},
|
||||
},
|
||||
{
|
||||
name: "list with a limit above the maximum",
|
||||
request: identityRequest{method: http.MethodGet, path: organizationNeedsPath + "?limit=101", headers: session},
|
||||
location: api.ProblemFieldLocationQuery, pointer: "/limit", code: "invalid",
|
||||
},
|
||||
{
|
||||
name: "list for an organization identifier that is not a UUID",
|
||||
request: identityRequest{method: http.MethodGet, path: "/api/v1/organizations/acme/needs", headers: session},
|
||||
location: api.ProblemFieldLocationPath, pointer: "/org_id", code: "invalid",
|
||||
},
|
||||
{
|
||||
name: "create a manual need without a vacancy",
|
||||
request: create(validNeedCreate),
|
||||
},
|
||||
{
|
||||
name: "create from a text only",
|
||||
request: create(`{"title":"Backend","source_text":"Ищем бэкенд-разработчика. Обязательно: Go."}`),
|
||||
},
|
||||
{
|
||||
name: "create with an explicit null vacancy and unknown fields",
|
||||
request: create(`{"title":"Backend","vacancy_id":null,"grade":null,"salary":null,"work_formats":null,"tasks":[]}`),
|
||||
},
|
||||
{
|
||||
// The cross-field salary rules are domain rules that the server
|
||||
// checks after the schema.
|
||||
name: "create with a reversed salary range",
|
||||
request: create(withSalary(needSalary("160000", "120000", "net"))),
|
||||
},
|
||||
{
|
||||
name: "create with equal salary bounds",
|
||||
request: create(withSalary(needSalary("150000", "150000", "net"))),
|
||||
},
|
||||
{
|
||||
name: "create with a salary range wider than 40%",
|
||||
request: create(withSalary(needSalary("100000", "200000", "net"))),
|
||||
},
|
||||
{
|
||||
name: "create without a title",
|
||||
request: create(`{"specialization":{"value":"backend","status":"confirmed"}}`),
|
||||
location: api.ProblemFieldLocationBody, pointer: "/title", code: "required",
|
||||
},
|
||||
{
|
||||
name: "create without an Idempotency-Key",
|
||||
request: identityRequest{method: http.MethodPost, path: organizationNeedsPath, body: validNeedCreate, headers: session},
|
||||
location: api.ProblemFieldLocationHeader, pointer: "/Idempotency-Key", code: "required",
|
||||
},
|
||||
{
|
||||
name: "salary bound of zero",
|
||||
request: create(withSalary(needSalary("0", "160000", "net"))),
|
||||
location: api.ProblemFieldLocationBody, pointer: "/salary/value/salary_from", code: "invalid",
|
||||
},
|
||||
{
|
||||
name: "salary bound that is not a multiple of 1000",
|
||||
request: create(withSalary(needSalary("120000", "160500", "net"))),
|
||||
location: api.ProblemFieldLocationBody, pointer: "/salary/value/salary_to", code: "invalid",
|
||||
},
|
||||
{
|
||||
name: "salary in another currency",
|
||||
request: create(withSalary(strings.Replace(needSalary("120000", "160000", "net"), "RUB", "USD", 1))),
|
||||
location: api.ProblemFieldLocationBody, pointer: "/salary/value/currency", code: "invalid",
|
||||
},
|
||||
{
|
||||
name: "salary with an unknown tax basis",
|
||||
request: create(withSalary(needSalary("120000", "160000", "after_tax"))),
|
||||
location: api.ProblemFieldLocationBody, pointer: "/salary/value/tax_basis", code: "invalid",
|
||||
},
|
||||
{
|
||||
name: "salary per hour",
|
||||
request: create(withSalary(strings.Replace(needSalary("1000", "2000", "net"), "month", "hour", 1))),
|
||||
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.
|
||||
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",
|
||||
},
|
||||
{
|
||||
// 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",
|
||||
},
|
||||
{
|
||||
name: "grade as a bare value",
|
||||
request: create(strings.Replace(validNeedCreate, `{"value":"junior","status":"confirmed"}`, `"junior"`, 1)),
|
||||
location: api.ProblemFieldLocationBody, pointer: "", code: "invalid",
|
||||
},
|
||||
{
|
||||
name: "grade with an unknown status",
|
||||
request: create(strings.Replace(validNeedCreate, `{"value":"junior","status":"confirmed"}`, `{"value":"junior","status":"unknown"}`, 1)),
|
||||
location: api.ProblemFieldLocationBody, pointer: "/grade/status", code: "invalid",
|
||||
},
|
||||
{
|
||||
name: "recommendation score set by the employer",
|
||||
request: create(strings.Replace(validNeedCreate, `{"title"`, `{"rec_score":0.99,"title"`, 1)),
|
||||
location: api.ProblemFieldLocationBody, pointer: "", code: "invalid",
|
||||
},
|
||||
{
|
||||
name: "organization membership in the body",
|
||||
request: create(strings.Replace(validNeedCreate, `{"title"`, `{"members":[{"user_id":"01928f7a-1a2b-7c3d-8e4f-5a6b7c8d9e01","role":"owner"}],"title"`, 1)),
|
||||
location: api.ProblemFieldLocationBody, pointer: "", code: "invalid",
|
||||
},
|
||||
{
|
||||
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",
|
||||
},
|
||||
{
|
||||
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",
|
||||
},
|
||||
{
|
||||
name: "empty list of work formats",
|
||||
request: create(strings.Replace(validNeedCreate, `["remote"]`, `[]`, 1)),
|
||||
location: api.ProblemFieldLocationBody, pointer: "/work_formats/value", code: "invalid",
|
||||
},
|
||||
{
|
||||
name: "read",
|
||||
request: identityRequest{method: http.MethodGet, path: organizationNeedPath, headers: session},
|
||||
},
|
||||
{
|
||||
name: "read with a need identifier that is not a UUID",
|
||||
request: identityRequest{method: http.MethodGet, path: organizationNeedsPath + "/1", headers: session},
|
||||
location: api.ProblemFieldLocationPath, pointer: "/need_id", code: "invalid",
|
||||
},
|
||||
{
|
||||
name: "confirm suggestions, keep two open, and answer missing fields",
|
||||
request: update(`"specialization":{"value":"backend","status":"confirmed"},"grade":{"value":"middle","status":"confirmed"},` +
|
||||
`"required_skills":[{"name":"Go","status":"confirmed"},{"name":"Temporal","status":"confirmed"}],` +
|
||||
`"desired_skills":[{"name":"Kafka","status":"extracted"}],"domain":{"value":"платежи","status":"extracted"},` +
|
||||
`"salary":` + needSalary("250000", "320000", "gross")),
|
||||
},
|
||||
{
|
||||
name: "delete requirements",
|
||||
request: update(`"grade":null,"salary":null,"vacancy_id":null,"tasks":[]`),
|
||||
},
|
||||
{
|
||||
name: "update with a list sent as null",
|
||||
request: update(`"tasks":null`),
|
||||
location: api.ProblemFieldLocationBody, pointer: "", code: "invalid",
|
||||
},
|
||||
{
|
||||
name: "update with a bare grade",
|
||||
request: update(`"grade":"middle"`),
|
||||
location: api.ProblemFieldLocationBody, pointer: "", code: "invalid",
|
||||
},
|
||||
{
|
||||
name: "update without an expected version",
|
||||
request: identityRequest{method: http.MethodPatch, path: organizationNeedPath, body: `{"grade":null}`, headers: session},
|
||||
location: api.ProblemFieldLocationBody, pointer: "/expected_version", code: "required",
|
||||
},
|
||||
{
|
||||
name: "update with version zero",
|
||||
request: identityRequest{method: http.MethodPatch, path: organizationNeedPath, body: `{"expected_version":0,"grade":null}`, headers: session},
|
||||
location: api.ProblemFieldLocationBody, pointer: "/expected_version", code: "invalid",
|
||||
},
|
||||
{
|
||||
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",
|
||||
},
|
||||
{
|
||||
name: "update that sets the version itself",
|
||||
request: update(`"version":7`),
|
||||
location: api.ProblemFieldLocationBody, pointer: "", code: "invalid",
|
||||
},
|
||||
{
|
||||
name: "update that sets the requirements version",
|
||||
request: update(`"requirements_version":7`),
|
||||
location: api.ProblemFieldLocationBody, pointer: "", code: "invalid",
|
||||
},
|
||||
{
|
||||
name: "update with recommendations",
|
||||
request: update(`"recommendations":{"status":"computed"}`),
|
||||
location: api.ProblemFieldLocationBody, pointer: "", code: "invalid",
|
||||
},
|
||||
{
|
||||
name: "update with a salary in gross and net at once",
|
||||
request: update(`"salary":` + needSalary("120000", "160000", "net_or_gross")),
|
||||
location: api.ProblemFieldLocationBody, pointer: "/salary/value/tax_basis", code: "invalid",
|
||||
},
|
||||
{
|
||||
name: "update with a negative salary bound",
|
||||
request: update(`"salary":` + needSalary("-1000", "160000", "gross")),
|
||||
location: api.ProblemFieldLocationBody, pointer: "/salary/value/salary_from", code: "invalid",
|
||||
},
|
||||
} {
|
||||
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)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// A non-nullable optional list decodes to a plain slice, so an
|
||||
// UpdateOrganizationNeed handler tells an absent list (unchanged) from an
|
||||
// empty one (delete every item) only by nil; null is not a list and fails.
|
||||
func TestNeedUpdateDecodingKeepsAbsentAndEmptyListsApart(t *testing.T) {
|
||||
var empty api.NeedUpdate
|
||||
if err := empty.UnmarshalJSON([]byte(`{"expected_version":1,"tasks":[]}`)); err != nil {
|
||||
t.Fatalf("decode an empty list: %v", err)
|
||||
}
|
||||
if empty.Tasks == nil || len(empty.Tasks) != 0 {
|
||||
t.Fatalf("tasks = %#v, want a non-nil empty slice", empty.Tasks)
|
||||
}
|
||||
|
||||
var absent api.NeedUpdate
|
||||
if err := absent.UnmarshalJSON([]byte(`{"expected_version":1}`)); err != nil {
|
||||
t.Fatalf("decode an absent list: %v", err)
|
||||
}
|
||||
if absent.Tasks != nil || absent.RequiredSkills != nil || absent.DesiredSkills != nil {
|
||||
t.Fatalf("lists = %#v %#v %#v, want nil for absent fields", absent.Tasks, absent.RequiredSkills, absent.DesiredSkills)
|
||||
}
|
||||
if absent.Grade.IsSet() || absent.Salary.IsSet() {
|
||||
t.Fatal("absent field decisions are set, want unset")
|
||||
}
|
||||
|
||||
var cleared api.NeedUpdate
|
||||
if err := cleared.UnmarshalJSON([]byte(`{"expected_version":1,"grade":null}`)); err != nil {
|
||||
t.Fatalf("decode a null field decision: %v", err)
|
||||
}
|
||||
if !cleared.Grade.IsSet() || !cleared.Grade.IsNull() {
|
||||
t.Fatal("grade sent as null is not a set null, want a removal")
|
||||
}
|
||||
|
||||
var null api.NeedUpdate
|
||||
if err := null.UnmarshalJSON([]byte(`{"expected_version":1,"tasks":null}`)); err == nil {
|
||||
t.Fatal("decode a list sent as null succeeded, want an error")
|
||||
}
|
||||
}
|
||||
|
||||
// Every need operation requires an employer session; DenyAllSecurity grants
|
||||
// none, with or without a cookie and a CSRF token.
|
||||
func TestNeedAccessBeforeImplementation(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: organizationNeedsPath},
|
||||
{method: http.MethodGet, path: organizationNeedsPath, headers: map[string]string{"Cookie": testSessionCookie}},
|
||||
{method: http.MethodPost, path: organizationNeedsPath, body: validNeedCreate, headers: command},
|
||||
{method: http.MethodGet, path: organizationNeedPath},
|
||||
{method: http.MethodPatch, path: organizationNeedPath, body: needUpdateBody(`"grade":{"value":"middle","status":"confirmed"}`), headers: session},
|
||||
} {
|
||||
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))
|
||||
|
||||
Reference in new issue
Block a user