feat(invitations): check contact grants on every read, cap pending invitations, and expire them at the deadline

This commit is contained in:
shira committed 2026-10-09 19:06:50 +03:00
1 parent 676b3c6e24
commit 12a7213d4a
67 files changed
+4255 -149

No files matched your search

@@ -418,8 +418,10 @@ func TestCandidateInvitationViewAndDeclineOverHTTP(t *testing.T) {
if counts := readInvitationAnswerCounts(ctx, t, harness, sent.ID, target.ID); counts != (invitationAnswerCounts{viewedEvents: 1, declinedEvents: 1, declines: 1}) {
t.Fatalf("decline from viewed stored %+v", counts)
}
// The comment rules of the contract and an unstorable value.
fresh := deliver(orgA, sessionA, target, nil)
// The comment rules of the contract and an unstorable value. The pause
// after the declines above holds their candidates for thirty days, so
// the new invitation goes to a cohort candidate that never declined.
fresh := deliver(orgA, sessionA, cohort[len(cohort)-1], nil)
tooLong := strings.Repeat("я", 501)
for name, body := range map[string]string{
"white space": `{"expected_version":` + fmt.Sprint(fresh.Version) + `,"reason":"other","comment":" "}`,
@@ -441,7 +443,10 @@ func TestCandidateInvitationViewAndDeclineOverHTTP(t *testing.T) {
})
t.Run("another candidate and an employer learn nothing of the invitation", func(t *testing.T) {
sent := deliver(orgA, sessionA, target, nil)
// The sent invitation goes to a cohort candidate that never declined, so
// the pause after the declines above does not apply to it. Only the draft
// below is addressed to the candidate who declined.
sent := deliver(orgA, sessionA, cohort[len(cohort)-2], nil)
draft := draftFor(orgA, sessionA, target, nil)
for name, invitationID := range map[string]uuid.UUID{"another candidate's": sent.ID, "unknown": uuid.New()} {
for _, response := range []*httptest.ResponseRecorder{
@@ -496,10 +501,12 @@ func TestCandidateInvitationViewAndDeclineOverHTTP(t *testing.T) {
})
t.Run("a stale version is not stored and a late decline is refused whatever the version", func(t *testing.T) {
sent := deliver(orgA, sessionA, other, func(body, terms map[string]any) { body["response_period_days"] = 3 })
// Hider has no other invitation of this organization, so the pause after
// the declines above does not apply to it.
sent := deliver(orgA, sessionA, hider, func(body, terms map[string]any) { body["response_period_days"] = 3 })
key := nextKey()
requireSessionProblem(t, decline(sessionOther, sent.ID, declineBody(sent.Version+1, "tasks", nil), key), http.StatusPreconditionFailed, "version_conflict")
if counts := readInvitationAnswerCounts(ctx, t, harness, sent.ID, other.ID); counts.declines != 0 {
requireSessionProblem(t, decline(sessionHider, sent.ID, declineBody(sent.Version+1, "tasks", nil), key), http.StatusPreconditionFailed, "version_conflict")
if counts := readInvitationAnswerCounts(ctx, t, harness, sent.ID, hider.ID); counts.declines != 0 {
t.Fatalf("a stale decline stored %+v", counts)
}
var stored int
@@ -516,17 +523,20 @@ func TestCandidateInvitationViewAndDeclineOverHTTP(t *testing.T) {
t.Fatalf("read the deadline: %v", err)
}
harness.clock.Set(exactDeadline.UTC())
lateSession := login(other)
lateSession := login(hider)
// The late view applies the due expiry, so it answers with the expired
// invitation and records no view.
late := decodeCandidateInvitationHTTP(t, request(http.MethodPost, myPath(sent.ID)+"/view", "", lateSession, ""), http.StatusOK)
if late.State != "delivered" || late.Version != sent.Version || late.ViewedAt != nil {
t.Fatalf("late view = %+v, want the invitation unchanged", late)
if late.State != "expired" || late.ExpiryReason == nil || *late.ExpiryReason != "invitation_timeout" ||
late.Version != sent.Version+1 || late.ViewedAt != nil {
t.Fatalf("late view = %+v, want the invitation expired by the timeout without a view", late)
}
lateKey := nextKey()
for range 2 {
requireSessionProblem(t, decline(lateSession, sent.ID, declineBody(sent.Version+3, "tasks", nil), lateKey), http.StatusConflict, "state_conflict")
requireSessionProblem(t, decline(lateSession, sent.ID, declineBody(late.Version+2, "tasks", nil), lateKey), http.StatusConflict, "state_conflict")
}
requireSessionProblem(t, decline(lateSession, sent.ID, declineBody(sent.Version, "tasks", nil), key), http.StatusConflict, "state_conflict")
if counts := readInvitationAnswerCounts(ctx, t, harness, sent.ID, other.ID); counts != (invitationAnswerCounts{}) {
if counts := readInvitationAnswerCounts(ctx, t, harness, sent.ID, hider.ID); counts != (invitationAnswerCounts{}) {
t.Fatalf("late commands stored %+v", counts)
}
if err := harness.pool.QueryRow(ctx, `SELECT count(*) FROM invitation_decline_results WHERE invitation_id = $1 AND outcome = 'state_conflict'`, sent.ID).Scan(&stored); err != nil || stored != 2 {