136 lines
6.9 KiB
Go
136 lines
6.9 KiB
Go
//go:build integration
|
|
|
|
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"
|
|
|
|
type signalNoticeProjection struct {
|
|
NoticeVersion int `json:"notice_version"`
|
|
Acknowledged bool `json:"acknowledged"`
|
|
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 {
|
|
t.Fatalf("signal notice response = %d %s, want 200", response.Code, response.Body.String())
|
|
}
|
|
if response.Header().Get("Cache-Control") != "no-store" {
|
|
t.Fatalf("signal notice Cache-Control = %q, want no-store", response.Header().Get("Cache-Control"))
|
|
}
|
|
var notice signalNoticeProjection
|
|
if err := json.Unmarshal(response.Body.Bytes(), ¬ice); err != nil {
|
|
t.Fatalf("decode signal notice: %v", err)
|
|
}
|
|
return notice
|
|
}
|
|
|
|
func TestAssessmentSignalNoticeOverHTTP(t *testing.T) {
|
|
env := bootstrap(t, postgresService, redisService)
|
|
ctx, cancel := context.WithTimeout(t.Context(), 3*time.Minute)
|
|
defer cancel()
|
|
databaseURL := env.createIsolatedDatabase(ctx, t)
|
|
applyMigrations(ctx, t, databaseURL)
|
|
isolatedEnv := *env
|
|
isolatedEnv.Config.DatabaseURL = databaseURL
|
|
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)
|
|
}
|
|
candidate := createSessionHTTPUser(ctx, t, harness.sessionHTTPHarness, "notice-owner@example.test", passwordHash, true)
|
|
other := createSessionHTTPUser(ctx, t, harness.sessionHTTPHarness, "notice-other@example.test", passwordHash, true)
|
|
employer := createAssessmentAvailabilityEmployer(ctx, t, harness.sessionHTTPHarness, "notice-employer@example.test", passwordHash)
|
|
cookie, csrf := loginConsentHTTP(t, harness.sessionHTTPHarness, candidate.Email.Address(), "192.0.2.231:1234")
|
|
otherCookie, _ := loginConsentHTTP(t, harness.sessionHTTPHarness, other.Email.Address(), "192.0.2.232:1234")
|
|
employerCookie, employerCSRF := loginConsentHTTP(t, harness.sessionHTTPHarness, employer.Email.Address(), "192.0.2.233:1234")
|
|
request := func(method, body, origin, sessionCookie, token string) *httptest.ResponseRecorder {
|
|
return assessmentAttemptHTTPRequest(t, harness, method, assessmentSignalNoticePath, body, origin, sessionCookie, token, "", "192.0.2.234:1234")
|
|
}
|
|
|
|
anonymous := request(http.MethodGet, "", "", "", "")
|
|
requireSessionProblem(t, anonymous, http.StatusUnauthorized, "unauthenticated")
|
|
|
|
before := decodeSignalNotice(t, request(http.MethodGet, "", "", cookie.Value, ""))
|
|
if before.NoticeVersion != current || before.Acknowledged || before.AcknowledgedAt != nil {
|
|
t.Fatalf("notice before acknowledgement = %+v", before)
|
|
}
|
|
|
|
requireSessionProblem(t, request(http.MethodPut, `{"notice_version":1}`, sessionHTTPTestOrigin, cookie.Value, ""),
|
|
http.StatusForbidden, "csrf_check_failed")
|
|
requireSessionProblem(t, request(http.MethodPut, `{"notice_version":1}`, "https://evil.example", cookie.Value, csrf),
|
|
http.StatusForbidden, "csrf_check_failed")
|
|
if rejected := request(http.MethodPut, `{"notice_version":0}`, sessionHTTPTestOrigin, cookie.Value, csrf); rejected.Code != http.StatusBadRequest {
|
|
t.Fatalf("version 0 = %d %s, want 400", rejected.Code, rejected.Body.String())
|
|
}
|
|
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, 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, 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, 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, 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)
|
|
}
|
|
read := decodeSignalNotice(t, request(http.MethodGet, "", "", cookie.Value, ""))
|
|
if !read.Acknowledged || read.AcknowledgedAt == nil || *read.AcknowledgedAt != *first.AcknowledgedAt {
|
|
t.Fatalf("read after acknowledgement = %+v, want %+v", read, first)
|
|
}
|
|
|
|
var rows int
|
|
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 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)
|
|
}
|
|
if foreign := decodeSignalNotice(t, request(http.MethodGet, "", "", otherCookie.Value, "")); foreign.Acknowledged {
|
|
t.Fatalf("another account inherited the acknowledgement: %+v", foreign)
|
|
}
|
|
|
|
requireSessionProblem(t, request(http.MethodGet, "", "", employerCookie.Value, ""), http.StatusForbidden, "forbidden")
|
|
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.
|
|
var consentRows int
|
|
if err := harness.pool.QueryRow(ctx, "SELECT count(*) FROM consent_records WHERE subject_id = $1 AND purpose = 'c3'", candidate.ID).Scan(&consentRows); err != nil || consentRows != 0 {
|
|
t.Fatalf("C3 rows after acknowledgement = %d, error %v, want none", consentRows, err)
|
|
}
|
|
}
|