feat(assessment): watermark the task text of real attempts

This commit is contained in:
shira committed 2026-10-11 10:44:30 +03:00
1 parent 24b64d698a
commit c975a0d506
57 files changed
+2429 -92

No files matched your search

@@ -5,10 +5,13 @@ package integration_test
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"time"
"lincodex/backend/internal/usecase/assessment"
)
const assessmentSignalNoticePath = "/api/v1/me/assessment-signal-notice"
@@ -19,6 +22,11 @@ type signalNoticeProjection struct {
AcknowledgedAt *string `json:"acknowledged_at"`
}
// noticeBody is the request body that acknowledges the given notice version.
func noticeBody(version int) string {
return fmt.Sprintf(`{"notice_version":%d}`, version)
}
func decodeSignalNotice(t *testing.T, response *httptest.ResponseRecorder) signalNoticeProjection {
t.Helper()
if response.Code != http.StatusOK {
@@ -45,6 +53,9 @@ func TestAssessmentSignalNoticeOverHTTP(t *testing.T) {
harness := newAssessmentAttemptHTTPHarness(ctx, t, &isolatedEnv)
t.Cleanup(harness.pool.Close)
// The tests follow the version the server requires, so a new text version
// needs no edit here.
current := int(assessment.CurrentSignalNoticeVersion)
passwordHash, err := harness.credentials.HashNewPassword(ctx, sessionHTTPTestPass)
if err != nil {
t.Fatalf("HashNewPassword() error = %v", err)
@@ -63,7 +74,7 @@ func TestAssessmentSignalNoticeOverHTTP(t *testing.T) {
requireSessionProblem(t, anonymous, http.StatusUnauthorized, "unauthenticated")
before := decodeSignalNotice(t, request(http.MethodGet, "", "", cookie.Value, ""))
if before.NoticeVersion != 2 || before.Acknowledged || before.AcknowledgedAt != nil {
if before.NoticeVersion != current || before.Acknowledged || before.AcknowledgedAt != nil {
t.Fatalf("notice before acknowledgement = %+v", before)
}
@@ -77,21 +88,21 @@ func TestAssessmentSignalNoticeOverHTTP(t *testing.T) {
if rejected := request(http.MethodPut, `{"notice_version":1,"accepted":true}`, sessionHTTPTestOrigin, cookie.Value, csrf); rejected.Code != http.StatusBadRequest {
t.Fatalf("unknown field = %d %s, want 400", rejected.Code, rejected.Body.String())
}
requireSessionProblem(t, request(http.MethodPut, `{"notice_version":3}`, sessionHTTPTestOrigin, cookie.Value, csrf),
requireSessionProblem(t, request(http.MethodPut, noticeBody(current+1), sessionHTTPTestOrigin, cookie.Value, csrf),
http.StatusConflict, "state_conflict")
// The previous version is no longer the one the server requires.
requireSessionProblem(t, request(http.MethodPut, `{"notice_version":1}`, sessionHTTPTestOrigin, cookie.Value, csrf),
requireSessionProblem(t, request(http.MethodPut, noticeBody(current-1), sessionHTTPTestOrigin, cookie.Value, csrf),
http.StatusConflict, "state_conflict")
if untouched := decodeSignalNotice(t, request(http.MethodGet, "", "", cookie.Value, "")); untouched.Acknowledged {
t.Fatalf("a refused request stored an acknowledgement: %+v", untouched)
}
first := decodeSignalNotice(t, request(http.MethodPut, `{"notice_version":2}`, sessionHTTPTestOrigin, cookie.Value, csrf))
if !first.Acknowledged || first.AcknowledgedAt == nil || first.NoticeVersion != 2 {
first := decodeSignalNotice(t, request(http.MethodPut, noticeBody(current), sessionHTTPTestOrigin, cookie.Value, csrf))
if !first.Acknowledged || first.AcknowledgedAt == nil || first.NoticeVersion != current {
t.Fatalf("first acknowledgement = %+v", first)
}
time.Sleep(5 * time.Millisecond)
repeat := decodeSignalNotice(t, request(http.MethodPut, `{"notice_version":2}`, sessionHTTPTestOrigin, cookie.Value, csrf))
repeat := decodeSignalNotice(t, request(http.MethodPut, noticeBody(current), sessionHTTPTestOrigin, cookie.Value, csrf))
if repeat.AcknowledgedAt == nil || *repeat.AcknowledgedAt != *first.AcknowledgedAt {
t.Fatalf("a repeat changed the first acknowledgement time: first %+v, repeat %+v", first, repeat)
}
@@ -104,7 +115,7 @@ func TestAssessmentSignalNoticeOverHTTP(t *testing.T) {
if err := harness.pool.QueryRow(ctx, "SELECT count(*) FROM assessment_signal_notice_acknowledgements WHERE user_id = $1", candidate.ID).Scan(&rows); err != nil || rows != 1 {
t.Fatalf("stored acknowledgement rows = %d, error %v, want exactly one", rows, err)
}
// An acknowledgement of version 1 stays stored but does not count for version 2.
// An acknowledgement of version 1 stays stored but does not count for the current version.
if _, err := harness.pool.Exec(ctx, "INSERT INTO assessment_signal_notice_acknowledgements (user_id, notice_version, acknowledged_at) VALUES ($1, 1, now())", other.ID); err != nil {
t.Fatalf("store a version 1 acknowledgement: %v", err)
}
@@ -113,7 +124,7 @@ func TestAssessmentSignalNoticeOverHTTP(t *testing.T) {
}
requireSessionProblem(t, request(http.MethodGet, "", "", employerCookie.Value, ""), http.StatusForbidden, "forbidden")
requireSessionProblem(t, request(http.MethodPut, `{"notice_version":2}`, sessionHTTPTestOrigin, employerCookie.Value, employerCSRF),
requireSessionProblem(t, request(http.MethodPut, noticeBody(current), sessionHTTPTestOrigin, employerCookie.Value, employerCSRF),
http.StatusForbidden, "forbidden")
// The acknowledgement is a record, not a consent: it creates no ledger row.