153 lines
5.0 KiB
Go
153 lines
5.0 KiB
Go
//go:build e2e
|
|
|
|
package postgres
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"regexp"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/jackc/pgx/v5"
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
|
|
"lincodex/backend/internal/adapter/secondary/postgres/sqlc"
|
|
"lincodex/backend/internal/domain"
|
|
"lincodex/backend/internal/port/outbound"
|
|
)
|
|
|
|
var (
|
|
ErrCompanyFixtureScope = errors.New("company fixture scope rejected")
|
|
companyFixtureEmail = regexp.MustCompile(`^company-[a-f0-9]{8}@example\.test$`)
|
|
)
|
|
|
|
func isCompanyFixtureEmployer(user domain.User) bool {
|
|
return user.Scenario == domain.ScenarioEmployer && user.EmailVerified() &&
|
|
companyFixtureEmail.MatchString(user.Email.Address())
|
|
}
|
|
|
|
// RevokeCompanyFixtureMembership removes the synthetic owner membership used
|
|
// by the isolated browser suite. It is available only in e2e-tagged builds.
|
|
func RevokeCompanyFixtureMembership(
|
|
ctx context.Context,
|
|
pool *pgxpool.Pool,
|
|
organizationID, userID, successorUserID uuid.UUID,
|
|
) error {
|
|
const location = "postgres.RevokeCompanyFixtureMembership"
|
|
if pool == nil || organizationID == uuid.Nil || userID == uuid.Nil || successorUserID == uuid.Nil ||
|
|
userID == successorUserID {
|
|
return fmt.Errorf("%s - invalid fixture identity: %w", location, ErrCompanyFixtureScope)
|
|
}
|
|
|
|
tx, err := pool.BeginTx(ctx, transactionOptions)
|
|
if err != nil {
|
|
return fmt.Errorf("%s - begin transaction: %w", location, sanitizeConnectError(err))
|
|
}
|
|
finished := false
|
|
defer func() {
|
|
if !finished {
|
|
_ = rollback(ctx, tx)
|
|
}
|
|
}()
|
|
|
|
unit := &UnitOfWork{tx: tx, queries: sqlc.New(tx)}
|
|
membership, err := validateCompanyFixtureAccounts(ctx, unit, location, organizationID, userID, successorUserID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
changedAt := time.Now().UTC().Truncate(time.Microsecond)
|
|
if changedAt.Before(membership.UpdatedAt()) {
|
|
changedAt = membership.UpdatedAt()
|
|
}
|
|
if err := addCompanyFixtureSuccessor(ctx, unit, location, organizationID, successorUserID, changedAt); err != nil {
|
|
return err
|
|
}
|
|
if err := removeCompanyFixtureMembership(ctx, tx, location, organizationID, userID, changedAt); err != nil {
|
|
return err
|
|
}
|
|
|
|
finished = true
|
|
if err := tx.Commit(ctx); err != nil {
|
|
return fmt.Errorf("%s - commit transaction: %w", location, markTransactionConflict(err))
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func validateCompanyFixtureAccounts(
|
|
ctx context.Context,
|
|
unit *UnitOfWork,
|
|
location string,
|
|
organizationID, userID, successorUserID uuid.UUID,
|
|
) (domain.Membership, error) {
|
|
user, err := unit.Users().GetByID(ctx, userID)
|
|
if err != nil {
|
|
return domain.Membership{}, fmt.Errorf("%s - read fixture account: %w", location, err)
|
|
}
|
|
if !isCompanyFixtureEmployer(user) {
|
|
return domain.Membership{}, fmt.Errorf("%s - account is outside the fixture scope: %w", location, ErrCompanyFixtureScope)
|
|
}
|
|
successor, err := unit.Users().GetByID(ctx, successorUserID)
|
|
if err != nil {
|
|
return domain.Membership{}, fmt.Errorf("%s - read successor account: %w", location, err)
|
|
}
|
|
if !isCompanyFixtureEmployer(successor) {
|
|
return domain.Membership{}, fmt.Errorf("%s - successor account is outside the fixture scope: %w", location, ErrCompanyFixtureScope)
|
|
}
|
|
membership, err := unit.Memberships().Get(ctx, organizationID, userID)
|
|
if err != nil {
|
|
return domain.Membership{}, fmt.Errorf("%s - read fixture membership: %w", location, err)
|
|
}
|
|
if membership.Role() != domain.OrganizationRoleOwner || membership.State() != domain.MembershipStateActive {
|
|
return domain.Membership{}, fmt.Errorf("%s - membership is outside the fixture scope: %w", location, ErrCompanyFixtureScope)
|
|
}
|
|
if _, err := unit.Memberships().Get(ctx, organizationID, successorUserID); err == nil {
|
|
return domain.Membership{}, fmt.Errorf("%s - successor already belongs to the organization: %w", location, ErrCompanyFixtureScope)
|
|
} else if !errors.Is(err, outbound.ErrMembershipNotFound) {
|
|
return domain.Membership{}, fmt.Errorf("%s - check successor membership: %w", location, err)
|
|
}
|
|
return membership, nil
|
|
}
|
|
|
|
func addCompanyFixtureSuccessor(
|
|
ctx context.Context,
|
|
unit *UnitOfWork,
|
|
location string,
|
|
organizationID, successorUserID uuid.UUID,
|
|
changedAt time.Time,
|
|
) error {
|
|
successorMembership, err := domain.NewOwnerMembership(organizationID, successorUserID, changedAt)
|
|
if err != nil {
|
|
return fmt.Errorf("%s - create successor owner membership: %w", location, err)
|
|
}
|
|
if err := unit.Memberships().Insert(ctx, successorMembership); err != nil {
|
|
return fmt.Errorf("%s - add successor owner membership: %w", location, err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func removeCompanyFixtureMembership(
|
|
ctx context.Context,
|
|
tx pgx.Tx,
|
|
location string,
|
|
organizationID, userID uuid.UUID,
|
|
changedAt time.Time,
|
|
) error {
|
|
tag, err := tx.Exec(ctx, `
|
|
UPDATE memberships
|
|
SET state = 'removed', updated_at = $3
|
|
WHERE organization_id = $1
|
|
AND user_id = $2
|
|
AND role = 'owner'
|
|
AND state = 'active'
|
|
`, organizationID, userID, changedAt)
|
|
if err != nil {
|
|
return fmt.Errorf("%s - remove fixture membership: %w", location, err)
|
|
}
|
|
if tag.RowsAffected() != 1 {
|
|
return fmt.Errorf("%s - membership changed concurrently: %w", location, outbound.ErrTransactionConflict)
|
|
}
|
|
return nil
|
|
}
|