Files
lincodex/backend/integration_test/consent_usecase_test.go
T

558 lines
25 KiB
Go

//go:build integration
package integration_test
import (
"context"
"errors"
"sync/atomic"
"testing"
"time"
"github.com/google/uuid"
"github.com/jackc/pgx/v5/pgxpool"
postgresadapter "lincodex/backend/internal/adapter/secondary/postgres"
"lincodex/backend/internal/domain"
"lincodex/backend/internal/port/outbound"
"lincodex/backend/internal/usecase/consent"
)
const (
consentGrantsTestTimeout = 3 * time.Minute
// concurrentConsentRepeats repeats one grant at once, each on its own
// connection.
concurrentConsentRepeats = 8
// consentScheduledC5Version is a text that is published after the test
// clock, so it is scheduled and not yet current.
consentScheduledC5Version domain.ConsentTextVersion = "c5-2027-01-01"
)
// consentGrantsFixture runs the consent usecase on the bootstrap database.
type consentGrantsFixture struct {
ctx context.Context
pool *pgxpool.Pool
identity identityFixture
transactor *postgresadapter.Transactor
service *consent.Service
store *postgresadapter.ConsentStore
// nowNanos is the application clock, as Unix nanoseconds.
nowNanos *atomic.Int64
}
func (fixture consentGrantsFixture) now() time.Time {
return time.Unix(0, fixture.nowNanos.Load()).UTC()
}
func (fixture consentGrantsFixture) advance(by time.Duration) {
fixture.nowNanos.Store(fixture.now().Add(by).UnixNano())
}
// currentVersion returns the version of the text that is current at the
// application time.
func (fixture consentGrantsFixture) currentVersion(t *testing.T, purpose domain.ConsentPurpose) domain.ConsentTextVersion {
t.Helper()
text, err := fixture.store.ConsentTexts().Current(fixture.ctx, purpose, fixture.now())
if err != nil {
t.Fatalf("ConsentTexts().Current(%s) error = %v", purpose, err)
}
return text.Version()
}
func (fixture consentGrantsFixture) grantInput(t *testing.T, subject uuid.UUID, purpose domain.ConsentPurpose) consent.SettingsGrantInput {
t.Helper()
return consent.SettingsGrantInput{SubjectID: subject, Purpose: purpose, TextVersion: fixture.currentVersion(t, purpose)}
}
// registerC1 records the registration C1 of an existing account in its own
// transaction.
func (fixture consentGrantsFixture) registerC1(t *testing.T, user domain.User) domain.ConsentRecord {
t.Helper()
var result consent.GrantResult
err := fixture.transactor.WithinTransaction(fixture.ctx, func(ctx context.Context, unit outbound.UnitOfWork) error {
var err error
result, err = fixture.service.GrantAtRegistration(ctx, unit, user.ID, user.RegistrationVersion, fixture.currentVersion(t, domain.ConsentPurposePersonalData))
return err
})
if err != nil || !result.Created {
t.Fatalf("GrantAtRegistration() = %v, %v, want a new record", result.Created, err)
}
return result.Record
}
// grant records a new consent in its own transaction.
func (fixture consentGrantsFixture) grant(t *testing.T, input consent.SettingsGrantInput) domain.ConsentRecord {
t.Helper()
result, err := fixture.service.GrantInSettingsInNewTransaction(fixture.ctx, input)
if err != nil || !result.Created {
t.Fatalf("GrantInSettingsInNewTransaction(%s %s) = %v, %v, want a new record", input.Purpose, input.TextVersion, result.Created, err)
}
return result.Record
}
func (fixture consentGrantsFixture) list(t *testing.T, subject uuid.UUID) outbound.ConsentPage {
t.Helper()
page, err := fixture.store.ConsentLedger().List(fixture.ctx, outbound.ConsentListQuery{SubjectID: subject, Limit: consentPageSize})
if err != nil {
t.Fatalf("List() error = %v", err)
}
return page
}
func (fixture consentGrantsFixture) revoke(t *testing.T, ledger outbound.ConsentLedger, subject uuid.UUID, purpose domain.ConsentPurpose) []domain.ConsentRecord {
t.Helper()
revocation, err := domain.NewConsentRevocation(domain.ConsentRevocationInput{
SubjectID: subject, Purpose: purpose, Source: domain.ConsentRevocationSourceConsentSettings, RevokedAt: fixture.now(),
})
if err != nil {
t.Fatalf("NewConsentRevocation() error = %v", err)
}
revoked, err := ledger.Revoke(fixture.ctx, revocation)
if err != nil {
t.Fatalf("Revoke(%s) error = %v", purpose, err)
}
return revoked
}
func TestConsentGrants(t *testing.T) {
env := bootstrap(t, postgresService)
ctx, cancel := context.WithTimeout(t.Context(), consentGrantsTestTimeout)
defer cancel()
pool := env.newPool(ctx, t)
start := time.Date(2026, 10, 6, 9, 0, 0, 0, time.UTC)
nowNanos := &atomic.Int64{}
nowNanos.Store(start.UnixNano())
transactor := postgresadapter.NewTransactor(pool)
fixture := consentGrantsFixture{
ctx: ctx,
pool: pool,
identity: identityFixture{
ctx: ctx, pool: pool, store: postgresadapter.NewIdentityStore(pool), now: start,
},
transactor: transactor,
store: postgresadapter.NewConsentStore(pool),
nowNanos: nowNanos,
}
fixture.service = consent.NewService(transactor, fixture.now)
// A second C1 text, so that the first one is outdated, and a C5 text
// that is scheduled after the test clock. Both are published the way a
// migration would publish them.
for _, text := range []struct {
purpose, version, body string
publishedAt time.Time
}{
{"c1", string(consentSecondC1Version), consentSecondC1Body, start.Add(-24 * time.Hour)},
{"c5", string(consentScheduledC5Version), "I consent to the import of my FSP results, version two.", start.Add(72 * time.Hour)},
} {
if _, err := pool.Exec(ctx, `
INSERT INTO consent_texts (purpose, version, body, body_sha256, kind, published_at)
VALUES ($1, $2, $3, sha256(convert_to($3, 'UTF8')), 'demo', $4)`,
text.purpose, text.version, text.body, text.publishedAt); err != nil {
t.Fatalf("publish the %s text %s: %v", text.purpose, text.version, err)
}
}
reset := func(t *testing.T) {
t.Helper()
fixture.nowNanos.Store(start.UnixNano())
truncateTables(ctx, t, pool, usersDependentTables...)
}
t.Run("C1 is written in the registration transaction and the other purposes separately", func(t *testing.T) {
reset(t)
user := fixture.identity.newUser(t, "register@example.com", domain.ScenarioCandidate, start)
c1 := fixture.currentVersion(t, domain.ConsentPurposePersonalData)
// The registration has no session: the account and its C1 commit
// together on the subject ID alone.
var registered consent.GrantResult
err := transactor.WithinTransaction(ctx, func(ctx context.Context, unit outbound.UnitOfWork) error {
if err := unit.Users().Create(ctx, user); err != nil {
return err
}
var err error
registered, err = fixture.service.GrantAtRegistration(ctx, unit, user.ID, user.RegistrationVersion, c1)
return err
})
if err != nil || !registered.Created {
t.Fatalf("registration transaction = %v, %v, want an account with a new C1", registered.Created, err)
}
assertRowCount(ctx, t, pool, usersTable, 1)
assertRowCount(ctx, t, pool, sessionsTable, 0)
assertRowCount(ctx, t, pool, consentRecordsTable, 1)
if record := registered.Record; record.SubjectID() != user.ID || record.Source() != domain.ConsentSourceRegistration ||
record.RegistrationVersion() != user.RegistrationVersion ||
record.TextVersion() != consentSecondC1Version || !record.GrantedAt().Equal(start) {
t.Fatalf("registration C1 = %s %s %v, want the current text at the application time", record.Source(), record.TextVersion(), record.GrantedAt())
}
// The user withholds C3 and grants C2 and C5 one by one.
fixture.advance(time.Minute)
publication := fixture.grant(t, fixture.grantInput(t, user.ID, domain.ConsentPurposePublication))
fixture.advance(time.Minute)
fsp := fixture.grant(t, fixture.grantInput(t, user.ID, domain.ConsentPurposeFSPImport))
ledger := fixture.store.ConsentLedger()
for purpose, want := range map[domain.ConsentPurpose]uuid.UUID{
domain.ConsentPurposePersonalData: registered.Record.ID(),
domain.ConsentPurposePublication: publication.ID(),
domain.ConsentPurposeFSPImport: fsp.ID(),
} {
if current, err := ledger.Current(ctx, user.ID, purpose); err != nil || current.ID() != want || current.State() != domain.ConsentStateActive {
t.Fatalf("Current(%s) = %s, %v, want its own active record", purpose, current, err)
}
}
// C3 was not given: no record, and the other purposes did not need it.
if _, err := ledger.Current(ctx, user.ID, domain.ConsentPurposeAutoCategorization); !errors.Is(err, outbound.ErrConsentNotFound) {
t.Fatalf("Current(c3) error = %v, want ErrConsentNotFound", err)
}
// Granting C3 later, then revoking it, changes no other purpose.
fixture.advance(time.Minute)
categorization := fixture.grant(t, fixture.grantInput(t, user.ID, domain.ConsentPurposeAutoCategorization))
fixture.advance(time.Minute)
if revoked := fixture.revoke(t, ledger, user.ID, domain.ConsentPurposeAutoCategorization); len(revoked) != 1 || revoked[0].ID() != categorization.ID() {
t.Fatalf("Revoke(c3) = %v, want the C3 record only", revoked)
}
page, err := ledger.List(ctx, outbound.ConsentListQuery{SubjectID: user.ID, Limit: consentPageSize})
if err != nil {
t.Fatalf("List() error = %v", err)
}
assertConsentIDs(t, page.Records, categorization.ID(), fsp.ID(), publication.ID(), registered.Record.ID())
assertConsentStates(t, page, domain.ConsentStateRevoked, domain.ConsentStateActive, domain.ConsentStateActive, domain.ConsentStateActive)
})
t.Run("a failed registration takes its C1 with it", func(t *testing.T) {
reset(t)
user := fixture.identity.newUser(t, "rollback@example.com", domain.ScenarioCandidate, start)
c1 := fixture.currentVersion(t, domain.ConsentPurposePersonalData)
err := transactor.WithinTransaction(ctx, func(ctx context.Context, unit outbound.UnitOfWork) error {
if err := unit.Users().Create(ctx, user); err != nil {
return err
}
if _, err := fixture.service.GrantAtRegistration(ctx, unit, user.ID, user.RegistrationVersion, c1); err != nil {
return err
}
return errForcedRollback
})
if !errors.Is(err, errForcedRollback) {
t.Fatalf("registration transaction error = %v, want the forced rollback", err)
}
assertRowCount(ctx, t, pool, usersTable, 0)
assertRowCount(ctx, t, pool, consentRecordsTable, 0)
// A grant for a subject that has no account is refused by the ledger
// and stores nothing.
err = transactor.WithinTransaction(ctx, func(ctx context.Context, unit outbound.UnitOfWork) error {
_, err := fixture.service.GrantAtRegistration(ctx, unit, uuid.New(), 1, c1)
return err
})
if !errors.Is(err, outbound.ErrUserNotFound) {
t.Fatalf("GrantAtRegistration(unknown subject) error = %v, want ErrUserNotFound", err)
}
assertRowCount(ctx, t, pool, consentRecordsTable, 0)
})
t.Run("a repeat stores nothing and an outdated version is rejected", func(t *testing.T) {
reset(t)
user := fixture.identity.createUser(t, "repeat@example.com", domain.ScenarioCandidate, start.Add(-72*time.Hour))
input := fixture.grantInput(t, user.ID, domain.ConsentPurposePublication)
first := fixture.grant(t, input)
fixture.advance(time.Hour)
repeat, err := fixture.service.GrantInSettingsInNewTransaction(ctx, input)
if err != nil || repeat.Created || repeat.Record.ID() != first.ID() || !repeat.Record.GrantedAt().Equal(start) {
t.Fatalf("GrantInSettingsInNewTransaction(repeat) = %s, %v, %v, want the first record at %v", repeat.Record, repeat.Created, err, start)
}
assertRowCount(ctx, t, pool, consentRecordsTable, 1)
// The repeat inside a business transaction does not abort it.
err = transactor.WithinTransaction(ctx, func(ctx context.Context, unit outbound.UnitOfWork) error {
again, err := fixture.service.GrantInSettings(ctx, unit, input)
if err != nil || again.Created {
return errors.Join(err, errors.New("repeat in a transaction created a record"))
}
_, err = unit.ConsentLedger().Current(ctx, user.ID, domain.ConsentPurposePublication)
return err
})
if err != nil {
t.Fatalf("repeat in a transaction error = %v", err)
}
// C1 has a newer text: the first one is outdated. An unknown version
// and a scheduled one are not current either.
outdated := fixture.grantInput(t, user.ID, domain.ConsentPurposePersonalData)
outdated.TextVersion = "c1-2026-10-01"
scheduled := fixture.grantInput(t, user.ID, domain.ConsentPurposeFSPImport)
scheduled.TextVersion = consentScheduledC5Version
unknown := fixture.grantInput(t, user.ID, domain.ConsentPurposeAutoCategorization)
unknown.TextVersion = "c3-1999-01-01"
for name, stale := range map[string]struct {
input consent.SettingsGrantInput
current domain.ConsentTextVersion
}{
"outdated": {outdated, consentSecondC1Version},
"scheduled": {scheduled, "c5-2026-10-01"},
"unknown": {unknown, "c3-2026-10-01"},
} {
_, err := fixture.service.GrantInSettingsInNewTransaction(ctx, stale.input)
var staleError *consent.StaleConsentTextError
if !errors.Is(err, consent.ErrStaleConsentText) || !errors.As(err, &staleError) || staleError.Current != stale.current {
t.Fatalf("GrantInSettingsInNewTransaction(%s) error = %v, want a stale error naming %s", name, err, stale.current)
}
}
assertRowCount(ctx, t, pool, consentRecordsTable, 1)
// The current version of the same purpose goes through; the outdated
// text of C1 stays refused after it.
fixture.grant(t, fixture.grantInput(t, user.ID, domain.ConsentPurposePersonalData))
if _, err := fixture.service.GrantInSettingsInNewTransaction(ctx, outdated); !errors.Is(err, consent.ErrStaleConsentText) {
t.Fatalf("GrantInSettingsInNewTransaction(outdated after a current grant) error = %v, want ErrStaleConsentText", err)
}
assertRowCount(ctx, t, pool, consentRecordsTable, 2)
// A grant after a revocation is a new record, not a repeat.
fixture.advance(time.Minute)
fixture.revoke(t, fixture.store.ConsentLedger(), user.ID, domain.ConsentPurposePublication)
fixture.advance(time.Minute)
regranted := fixture.grant(t, input)
if regranted.ID() == first.ID() {
t.Fatal("a grant after a revocation reused the revoked record")
}
assertRowCount(ctx, t, pool, consentRecordsTable, 3)
})
t.Run("concurrent repeats create one record", func(t *testing.T) {
reset(t)
user := fixture.identity.createUser(t, "concurrent@example.com", domain.ScenarioCandidate, start.Add(-72*time.Hour))
input := fixture.grantInput(t, user.ID, domain.ConsentPurposePublication)
var created atomic.Int32
functions := make([]func() error, concurrentConsentRepeats)
for index := range functions {
functions[index] = func() error {
result, err := fixture.service.GrantInSettingsInNewTransaction(ctx, input)
if err == nil && result.Created {
created.Add(1)
}
return err
}
}
for index, err := range runAtOnce(functions...) {
if err != nil {
t.Fatalf("concurrent grant %d error = %v", index, err)
}
}
if created.Load() != 1 {
t.Fatalf("%d concurrent grants created a record, want 1", created.Load())
}
assertRowCount(ctx, t, pool, consentRecordsTable, 1)
})
t.Run("a repeat of an outdated version after a new publication is stale", func(t *testing.T) {
reset(t)
user := fixture.identity.createUser(t, "republished@example.com", domain.ScenarioCandidate, start.Add(-72*time.Hour))
input := fixture.grantInput(t, user.ID, domain.ConsentPurposeAutoCategorization)
first := fixture.grant(t, input)
// Version two of C3 is published the way a migration would, after
// version one was granted and before the repeat.
const secondC3Version = "c3-2026-10-05"
if _, err := pool.Exec(ctx, `
INSERT INTO consent_texts (purpose, version, body, body_sha256, kind, published_at)
VALUES ('c3', $1, $2, sha256(convert_to($2, 'UTF8')), 'demo', $3)`,
secondC3Version, "I consent to the automatic categorization, version two.", start.Add(-time.Hour)); err != nil {
t.Fatalf("publish the second C3 text: %v", err)
}
_, err := fixture.service.GrantInSettingsInNewTransaction(ctx, input)
var stale *consent.StaleConsentTextError
if !errors.Is(err, consent.ErrStaleConsentText) || !errors.As(err, &stale) || stale.Current != secondC3Version {
t.Fatalf("repeat of version one error = %v, want a stale error naming %s", err, secondC3Version)
}
assertRowCount(ctx, t, pool, consentRecordsTable, 1)
if current, err := fixture.store.ConsentLedger().Current(ctx, user.ID, domain.ConsentPurposeAutoCategorization); err != nil || current.ID() != first.ID() {
t.Fatalf("Current(c3) = %s, %v, want the first record", current, err)
}
})
t.Run("a re-registration records its C1 after UpdateRegistration", func(t *testing.T) {
reset(t)
reregister := func(t *testing.T, rawEmail string, grantFirst bool) (granted consent.GrantResult, earlier domain.ConsentRecord) {
t.Helper()
first := fixture.identity.createUser(t, rawEmail, domain.ScenarioCandidate, start.Add(-72*time.Hour))
earlier = fixture.registerC1(t, first)
changed, err := first.ReRegister(fixture.identity.registration(t, rawEmail, domain.ScenarioEmployer, start))
if err != nil {
t.Fatalf("ReRegister() error = %v", err)
}
version := fixture.currentVersion(t, domain.ConsentPurposePersonalData)
err = transactor.WithinTransaction(ctx, func(ctx context.Context, unit outbound.UnitOfWork) error {
grant := func(registrationVersion int64) error {
var err error
granted, err = fixture.service.GrantAtRegistration(ctx, unit, first.ID, registrationVersion, version)
return err
}
if grantFirst {
if err := grant(first.RegistrationVersion); err != nil {
return err
}
}
registered, err := unit.Users().UpdateRegistration(ctx, changed)
if err != nil {
return err
}
if grantFirst {
return nil
}
return grant(registered.RegistrationVersion)
})
if err != nil {
t.Fatalf("re-registration transaction error = %v", err)
}
return granted, earlier
}
// Correct order: UpdateRegistration deletes the earlier registrant's
// C1, then the grant creates the new one.
granted, earlier := reregister(t, "after@example.com", false)
if !granted.Created || granted.Record.ID() == earlier.ID() {
t.Fatalf("GrantAtRegistration after UpdateRegistration = %s, created %v, want a new record", granted.Record, granted.Created)
}
assertConsentIDs(t, fixture.list(t, granted.Record.SubjectID()).Records, granted.Record.ID())
// The hazard: a grant before UpdateRegistration finds the earlier
// C1 and reports Created false, and the update then deletes it, so
// the account is left without any C1.
before, earlierBefore := reregister(t, "before@example.com", true)
if before.Created || before.Record.ID() != earlierBefore.ID() {
t.Fatalf("GrantAtRegistration before UpdateRegistration = %s, created %v, want the earlier record", before.Record, before.Created)
}
if page := fixture.list(t, before.Record.SubjectID()); len(page.Records) != 0 {
t.Fatalf("a grant before UpdateRegistration left %d records, want none: the earlier C1 is deleted", len(page.Records))
}
})
t.Run("C4 is granted only through its scoped operation", func(t *testing.T) {
reset(t)
user := fixture.identity.createUser(t, "contact@example.com", domain.ScenarioCandidate, start.Add(-72*time.Hour))
generic := fixture.grantInput(t, user.ID, domain.ConsentPurposeContactDisclosure)
_, err := fixture.service.GrantInSettingsInNewTransaction(ctx, generic)
if !errors.Is(err, consent.ErrScopedConsentRequired) {
t.Fatalf("GrantInSettingsInNewTransaction(C4) error = %v, want ErrScopedConsentRequired", err)
}
assertRowCount(ctx, t, pool, consentRecordsTable, 0)
fields, err := domain.NewContactFieldSet(domain.ContactFieldEmail)
if err != nil {
t.Fatalf("NewContactFieldSet() error = %v", err)
}
organization, invitation := uuid.New(), uuid.New()
contact := consent.ContactGrantInput{
SubjectID: user.ID, OrganizationID: organization, OrganizationName: consentOrganizationName, Fields: fields,
InvitationID: invitation, InvitationTermsVersion: 2, TextVersion: generic.TextVersion,
}
var granted consent.GrantResult
err = transactor.WithinTransaction(ctx, func(ctx context.Context, unit outbound.UnitOfWork) error {
var err error
granted, err = fixture.service.GrantContact(ctx, unit, contact)
return err
})
if err != nil || !granted.Created {
t.Fatalf("GrantContact() = %v, %v, want a new record", granted.Created, err)
}
record := granted.Record
if record.Purpose() != domain.ConsentPurposeContactDisclosure || record.Source() != domain.ConsentSourceInvitationAcceptance ||
record.OrganizationID() != organization || record.OrganizationName() != consentOrganizationName ||
!record.Fields().Equal(fields) || record.InvitationID() != invitation || !record.GrantedAt().Equal(start) {
t.Fatalf("GrantContact() record = %s, want the scope of its input at the application time", record)
}
// A repeat returns the record; an outdated C4 text is refused.
err = transactor.WithinTransaction(ctx, func(ctx context.Context, unit outbound.UnitOfWork) error {
repeat, err := fixture.service.GrantContact(ctx, unit, contact)
if err == nil && (repeat.Created || repeat.Record.ID() != record.ID()) {
err = errors.New("repeat did not return the existing record")
}
return err
})
if err != nil {
t.Fatalf("GrantContact(repeat) error = %v", err)
}
outdated := contact
outdated.InvitationID, outdated.TextVersion = uuid.New(), "c4-1999-01-01"
err = transactor.WithinTransaction(ctx, func(ctx context.Context, unit outbound.UnitOfWork) error {
_, err := fixture.service.GrantContact(ctx, unit, outdated)
return err
})
if !errors.Is(err, consent.ErrStaleConsentText) {
t.Fatalf("GrantContact(outdated) error = %v, want ErrStaleConsentText", err)
}
assertRowCount(ctx, t, pool, consentRecordsTable, 1)
// C4 gives no other purpose.
if _, err := fixture.store.ConsentLedger().Current(ctx, user.ID, domain.ConsentPurposePublication); !errors.Is(err, outbound.ErrConsentNotFound) {
t.Fatalf("Current(c2) after C4 error = %v, want ErrConsentNotFound", err)
}
})
// This subtest must stay last: it closes the shared pool to simulate a
// restart, so no later subtest could use the fixture.
t.Run("a new repository instance reads the history with its revocations", func(t *testing.T) {
reset(t)
user := fixture.identity.createUser(t, "restart@example.com", domain.ScenarioCandidate, start.Add(-72*time.Hour))
grantedAt := map[uuid.UUID]time.Time{}
c1 := fixture.registerC1(t, user)
grantedAt[c1.ID()] = fixture.now()
fixture.advance(time.Minute)
c2 := fixture.grant(t, fixture.grantInput(t, user.ID, domain.ConsentPurposePublication))
grantedAt[c2.ID()] = fixture.now()
fixture.advance(time.Minute)
c5 := fixture.grant(t, fixture.grantInput(t, user.ID, domain.ConsentPurposeFSPImport))
grantedAt[c5.ID()] = fixture.now()
fixture.advance(time.Minute)
revokedAt := fixture.now()
if revoked := fixture.revoke(t, fixture.store.ConsentLedger(), user.ID, domain.ConsentPurposePublication); len(revoked) != 1 {
t.Fatalf("Revoke(c2) = %v, want one record", revoked)
}
fixture.advance(time.Minute)
c2Again := fixture.grant(t, fixture.grantInput(t, user.ID, domain.ConsentPurposePublication))
grantedAt[c2Again.ID()] = fixture.now()
// Restart: close the pool and build every repository and the usecase
// again from a new one.
pool.Close()
restartedPool := env.newPool(ctx, t)
restartedStore := postgresadapter.NewConsentStore(restartedPool)
restartedService := consent.NewService(postgresadapter.NewTransactor(restartedPool), fixture.now)
page, err := restartedStore.ConsentLedger().List(ctx, outbound.ConsentListQuery{SubjectID: user.ID, Limit: consentPageSize})
if err != nil {
t.Fatalf("List() after restart error = %v", err)
}
assertConsentIDs(t, page.Records, c2Again.ID(), c5.ID(), c2.ID(), c1.ID())
assertConsentStates(t, page, domain.ConsentStateActive, domain.ConsentStateActive, domain.ConsentStateRevoked, domain.ConsentStateActive)
revokedRecord := page.Records[2]
if stamped, ok := revokedRecord.RevokedAt(); !ok || !stamped.Equal(revokedAt) ||
revokedRecord.RevocationSource() != domain.ConsentRevocationSourceConsentSettings {
t.Fatalf("revoked record = %v %v %q, want the stamp %v", stamped, ok, revokedRecord.RevocationSource(), revokedAt)
}
for _, record := range page.Records {
if want := grantedAt[record.ID()]; !record.GrantedAt().Equal(want) {
t.Fatalf("record %s granted at %v, want the application time %v", record, record.GrantedAt(), want)
}
}
// The restarted instance sees the same state: a repeat stores nothing.
current, err := restartedStore.ConsentTexts().Current(ctx, domain.ConsentPurposePublication, fixture.now())
if err != nil {
t.Fatalf("ConsentTexts().Current(c2) after restart error = %v", err)
}
repeat, err := restartedService.GrantInSettingsInNewTransaction(ctx, consent.SettingsGrantInput{
SubjectID: user.ID, Purpose: domain.ConsentPurposePublication, TextVersion: current.Version(),
})
if err != nil || repeat.Created || repeat.Record.ID() != c2Again.ID() {
t.Fatalf("GrantInSettingsInNewTransaction(repeat after restart) = %s, %v, %v, want the existing record", repeat.Record, repeat.Created, err)
}
assertRowCount(ctx, t, restartedPool, consentRecordsTable, 4)
})
}