293 lines
8.0 KiB
Go
293 lines
8.0 KiB
Go
//go:build e2e
|
|
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"crypto/rand"
|
|
"encoding/base64"
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"lincodex/backend/internal/adapter/secondary/argon2id"
|
|
"lincodex/backend/internal/adapter/secondary/postgres"
|
|
"lincodex/backend/internal/domain"
|
|
"lincodex/backend/internal/port/outbound"
|
|
"lincodex/backend/internal/usecase/consent"
|
|
)
|
|
|
|
const (
|
|
fixtureGuardName = "E2E_CONSENT_FIXTURE"
|
|
fixtureEmail = "consent-browser@example.test"
|
|
fixturePassword = "browser consent fixture 2026"
|
|
firstOwnerEmail = "consent-owner-one@example.test"
|
|
secondOwnerEmail = "consent-owner-two@example.test"
|
|
fixtureTimeout = 90 * time.Second
|
|
)
|
|
|
|
func main() {
|
|
if err := run(); err != nil {
|
|
_, _ = fmt.Fprintln(os.Stderr, err)
|
|
os.Exit(1)
|
|
}
|
|
fmt.Fprintln(os.Stdout, "Consent browser fixture seeded.")
|
|
}
|
|
|
|
func run() error {
|
|
if os.Getenv(fixtureGuardName) != "1" {
|
|
return errors.New("consent fixture requires the isolated e2e environment")
|
|
}
|
|
if len(os.Args) != 1 {
|
|
return errors.New("consent fixture accepts no arguments")
|
|
}
|
|
databaseURL := os.Getenv("DATABASE_URL")
|
|
if databaseURL == "" {
|
|
return errors.New("consent fixture database is not configured")
|
|
}
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), fixtureTimeout)
|
|
defer cancel()
|
|
|
|
pool, err := postgres.NewSizedPool(ctx, databaseURL, 4)
|
|
if err != nil {
|
|
return errors.New("consent fixture could not open its database")
|
|
}
|
|
defer pool.Close()
|
|
if err := pool.Ping(ctx); err != nil {
|
|
return errors.New("consent fixture database is unavailable")
|
|
}
|
|
|
|
hasher, err := argon2id.NewHasher(1)
|
|
if err != nil {
|
|
return errors.New("consent fixture password service is unavailable")
|
|
}
|
|
now := time.Now().UTC().Truncate(time.Microsecond)
|
|
users, err := newFixtureUsers(ctx, hasher, now)
|
|
if err != nil {
|
|
return errors.New("consent fixture accounts could not be prepared")
|
|
}
|
|
|
|
transactor := postgres.NewTransactor(pool)
|
|
service := consent.NewService(transactor, func() time.Time { return now })
|
|
if err := seedFixture(ctx, transactor, service, users, now); err != nil {
|
|
if errors.Is(err, outbound.ErrEmailTaken) {
|
|
return errors.New("consent fixture addresses already exist; the database must be fresh")
|
|
}
|
|
return errors.New("consent fixture could not be seeded")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
type fixtureUsers struct {
|
|
candidate domain.User
|
|
firstOwner domain.User
|
|
secondOwner domain.User
|
|
}
|
|
|
|
func newFixtureUsers(
|
|
ctx context.Context,
|
|
hasher *argon2id.Hasher,
|
|
registeredAt time.Time,
|
|
) (fixtureUsers, error) {
|
|
candidate, err := newFixtureUser(ctx, hasher, fixtureEmail, fixturePassword, domain.ScenarioCandidate, registeredAt)
|
|
if err != nil {
|
|
return fixtureUsers{}, err
|
|
}
|
|
firstOwner, err := newFixtureOwner(ctx, hasher, firstOwnerEmail, registeredAt)
|
|
if err != nil {
|
|
return fixtureUsers{}, err
|
|
}
|
|
secondOwner, err := newFixtureOwner(ctx, hasher, secondOwnerEmail, registeredAt)
|
|
if err != nil {
|
|
return fixtureUsers{}, err
|
|
}
|
|
return fixtureUsers{candidate: candidate, firstOwner: firstOwner, secondOwner: secondOwner}, nil
|
|
}
|
|
|
|
func newFixtureOwner(
|
|
ctx context.Context,
|
|
hasher *argon2id.Hasher,
|
|
email string,
|
|
registeredAt time.Time,
|
|
) (domain.User, error) {
|
|
passwordBytes := make([]byte, 24)
|
|
if _, err := rand.Read(passwordBytes); err != nil {
|
|
return domain.User{}, errors.New("could not generate an isolated owner password")
|
|
}
|
|
rawPassword := base64.RawURLEncoding.EncodeToString(passwordBytes)
|
|
return newFixtureUser(ctx, hasher, email, rawPassword, domain.ScenarioEmployer, registeredAt)
|
|
}
|
|
|
|
func newFixtureUser(
|
|
ctx context.Context,
|
|
hasher *argon2id.Hasher,
|
|
emailValue string,
|
|
passwordValue string,
|
|
scenario domain.Scenario,
|
|
registeredAt time.Time,
|
|
) (domain.User, error) {
|
|
email, err := domain.NewEmailAddress(emailValue)
|
|
if err != nil {
|
|
return domain.User{}, errors.New("fixture account email is invalid")
|
|
}
|
|
password, err := domain.NewPassword(passwordValue)
|
|
if err != nil {
|
|
return domain.User{}, errors.New("fixture account password is invalid")
|
|
}
|
|
passwordHash, err := hasher.Hash(ctx, password)
|
|
if err != nil {
|
|
return domain.User{}, errors.New("fixture account password could not be hashed")
|
|
}
|
|
user, err := domain.NewUser(domain.Registration{
|
|
Email: email,
|
|
PasswordHash: passwordHash,
|
|
Scenario: scenario,
|
|
RegisteredAt: registeredAt,
|
|
})
|
|
if err != nil {
|
|
return domain.User{}, errors.New("fixture account could not be built")
|
|
}
|
|
return user, nil
|
|
}
|
|
|
|
func seedFixture(
|
|
ctx context.Context,
|
|
transactor outbound.Transactor,
|
|
service *consent.Service,
|
|
users fixtureUsers,
|
|
now time.Time,
|
|
) error {
|
|
return transactor.WithinTransaction(ctx, func(ctx context.Context, unit outbound.UnitOfWork) error {
|
|
for _, user := range []domain.User{users.candidate, users.firstOwner, users.secondOwner} {
|
|
if err := unit.Users().Create(ctx, user); err != nil {
|
|
return err
|
|
}
|
|
if _, err := unit.Users().MarkEmailVerified(ctx, user.ID, user.RegistrationVersion, now); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
firstOrganization, err := createOrganization(
|
|
ctx,
|
|
unit,
|
|
users.firstOwner.ID,
|
|
"Первая тестовая компания",
|
|
now,
|
|
)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
secondOrganization, err := createOrganization(
|
|
ctx,
|
|
unit,
|
|
users.secondOwner.ID,
|
|
"Вторая тестовая компания",
|
|
now,
|
|
)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
c1Text, err := unit.ConsentTexts().Current(ctx, domain.ConsentPurposePersonalData, now)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if _, err := service.GrantAtRegistration(
|
|
ctx,
|
|
unit,
|
|
users.candidate.ID,
|
|
users.candidate.RegistrationVersion,
|
|
c1Text.Version(),
|
|
); err != nil {
|
|
return err
|
|
}
|
|
|
|
c2Text, err := unit.ConsentTexts().Current(ctx, domain.ConsentPurposePublication, now)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if _, err := service.GrantInSettings(ctx, unit, consent.SettingsGrantInput{
|
|
SubjectID: users.candidate.ID,
|
|
Purpose: domain.ConsentPurposePublication,
|
|
TextVersion: c2Text.Version(),
|
|
}); err != nil {
|
|
return err
|
|
}
|
|
|
|
c4Text, err := unit.ConsentTexts().Current(ctx, domain.ConsentPurposeContactDisclosure, now)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err := grantFixtureContactConsent(ctx, service, unit, users.candidate.ID, firstOrganization, c4Text.Version(), domain.ContactFieldEmail); err != nil {
|
|
return err
|
|
}
|
|
if err := grantFixtureContactConsent(ctx, service, unit, users.candidate.ID, secondOrganization, c4Text.Version(), domain.ContactFieldPhone); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
})
|
|
}
|
|
|
|
func createOrganization(
|
|
ctx context.Context,
|
|
unit outbound.UnitOfWork,
|
|
ownerID uuid.UUID,
|
|
name string,
|
|
now time.Time,
|
|
) (domain.Organization, error) {
|
|
profile, err := domain.NewOrganizationProfile(domain.OrganizationProfileInput{Name: name})
|
|
if err != nil {
|
|
return domain.Organization{}, err
|
|
}
|
|
organization, err := domain.NewOrganization(profile, now)
|
|
if err != nil {
|
|
return domain.Organization{}, err
|
|
}
|
|
owner, err := domain.NewOwnerMembership(organization.ID(), ownerID, now)
|
|
if err != nil {
|
|
return domain.Organization{}, err
|
|
}
|
|
if err := unit.Organizations().CreateWithOwner(ctx, organization, owner); err != nil {
|
|
return domain.Organization{}, err
|
|
}
|
|
return organization, nil
|
|
}
|
|
|
|
func grantFixtureContactConsent(
|
|
ctx context.Context,
|
|
service *consent.Service,
|
|
unit outbound.UnitOfWork,
|
|
subjectID uuid.UUID,
|
|
organization domain.Organization,
|
|
textVersion domain.ConsentTextVersion,
|
|
field domain.ContactField,
|
|
) error {
|
|
fields, err := domain.NewContactFieldSet(field)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
invitationID, err := uuid.NewV7()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
// These synthetic invitation IDs exercise consent history and scoped revocation only.
|
|
// They are not evidence of the real invitation acceptance flow.
|
|
_, err = service.GrantContact(ctx, unit, consent.ContactGrantInput{
|
|
SubjectID: subjectID,
|
|
OrganizationID: organization.ID(),
|
|
OrganizationName: organization.Profile().Name(),
|
|
Fields: fields,
|
|
InvitationID: invitationID,
|
|
InvitationTermsVersion: 1,
|
|
TextVersion: textVersion,
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|