feat: game lobby service

This commit is contained in:
Ilia Denisov
2026-04-25 23:20:55 +02:00
committed by GitHub
parent 32dc29359a
commit 48b0056b49
336 changed files with 57074 additions and 1418 deletions
@@ -20,12 +20,14 @@ var knownSanctionCodes = []policy.SanctionCode{
policy.SanctionCodePrivateGameManageBlock,
policy.SanctionCodeGameJoinBlock,
policy.SanctionCodeProfileUpdateBlock,
policy.SanctionCodePermanentBlock,
}
var knownLimitCodes = []policy.LimitCode{
policy.LimitCodeMaxOwnedPrivateGames,
policy.LimitCodeMaxPendingPublicApplications,
policy.LimitCodeMaxActiveGameMemberships,
policy.LimitCodeMaxRegisteredRaceNames,
}
var knownEligibilityMarkers = []policy.EligibilityMarker{
@@ -189,6 +191,16 @@ func deriveEligibilityMarkerValues(
isPaid bool,
activeSanctionCodes map[policy.SanctionCode]struct{},
) map[policy.EligibilityMarker]bool {
if _, permanentBlocked := activeSanctionCodes[policy.SanctionCodePermanentBlock]; permanentBlocked {
return map[policy.EligibilityMarker]bool{
policy.EligibilityMarkerCanLogin: false,
policy.EligibilityMarkerCanCreatePrivateGame: false,
policy.EligibilityMarkerCanManagePrivateGame: false,
policy.EligibilityMarkerCanJoinGame: false,
policy.EligibilityMarkerCanUpdateProfile: false,
}
}
_, loginBlocked := activeSanctionCodes[policy.SanctionCodeLoginBlock]
_, createBlocked := activeSanctionCodes[policy.SanctionCodePrivateGameCreateBlock]
_, manageBlocked := activeSanctionCodes[policy.SanctionCodePrivateGameManageBlock]
@@ -0,0 +1,58 @@
package userstore
import (
"testing"
"galaxy/user/internal/domain/policy"
"github.com/stretchr/testify/require"
)
func TestDeriveEligibilityMarkerValuesCollapsesUnderPermanentBlock(t *testing.T) {
t.Parallel()
activeCodes := map[policy.SanctionCode]struct{}{
policy.SanctionCodePermanentBlock: {},
}
values := deriveEligibilityMarkerValues(true, activeCodes)
require.False(t, values[policy.EligibilityMarkerCanLogin])
require.False(t, values[policy.EligibilityMarkerCanCreatePrivateGame])
require.False(t, values[policy.EligibilityMarkerCanManagePrivateGame])
require.False(t, values[policy.EligibilityMarkerCanJoinGame])
require.False(t, values[policy.EligibilityMarkerCanUpdateProfile])
}
func TestDeriveEligibilityMarkerValuesPermanentBlockDominatesOtherSanctions(t *testing.T) {
t.Parallel()
activeCodes := map[policy.SanctionCode]struct{}{
policy.SanctionCodePermanentBlock: {},
policy.SanctionCodeLoginBlock: {},
policy.SanctionCodeGameJoinBlock: {},
}
values := deriveEligibilityMarkerValues(false, activeCodes)
for marker, value := range values {
require.Falsef(t, value, "marker %q must be false under permanent_block", marker)
}
}
func TestDeriveEligibilityMarkerValuesFreeUserWithoutPermanentBlock(t *testing.T) {
t.Parallel()
values := deriveEligibilityMarkerValues(false, map[policy.SanctionCode]struct{}{})
require.True(t, values[policy.EligibilityMarkerCanLogin])
require.False(t, values[policy.EligibilityMarkerCanCreatePrivateGame])
require.False(t, values[policy.EligibilityMarkerCanManagePrivateGame])
require.True(t, values[policy.EligibilityMarkerCanJoinGame])
require.True(t, values[policy.EligibilityMarkerCanUpdateProfile])
}
func TestKnownCatalogsIncludeStage22Codes(t *testing.T) {
t.Parallel()
require.Contains(t, knownSanctionCodes, policy.SanctionCodePermanentBlock)
require.Contains(t, knownLimitCodes, policy.LimitCodeMaxRegisteredRaceNames)
}
@@ -25,21 +25,21 @@ func TestListUserIDsCreatedAtPagination(t *testing.T) {
first := validAccountRecord()
first.UserID = common.UserID("user-100")
first.Email = common.Email("u100@example.com")
first.RaceName = common.RaceName("User 100")
first.UserName = common.UserName("player-user100aa")
first.CreatedAt = base.Add(-time.Hour)
first.UpdatedAt = first.CreatedAt
second := validAccountRecord()
second.UserID = common.UserID("user-200")
second.Email = common.Email("u200@example.com")
second.RaceName = common.RaceName("User 200")
second.UserName = common.UserName("player-user200aa")
second.CreatedAt = base
second.UpdatedAt = second.CreatedAt
third := validAccountRecord()
third.UserID = common.UserID("user-300")
third.Email = common.Email("u300@example.com")
third.RaceName = common.RaceName("User 300")
third.UserName = common.UserName("player-user300aa")
third.CreatedAt = base
third.UpdatedAt = third.CreatedAt
@@ -80,7 +80,6 @@ func TestEnsureByEmailInitialAdminIndexes(t *testing.T) {
Account: record,
Entitlement: validEntitlementSnapshot(record.UserID, now),
EntitlementRecord: validEntitlementRecord(record.UserID, now),
Reservation: raceNameReservation(record.UserID, record.RaceName, now),
})
require.NoError(t, err)
require.Equal(t, ports.EnsureByEmailOutcomeCreated, result.Outcome)
@@ -125,7 +124,6 @@ func TestEntitlementLifecycleSyncsAdminIndexes(t *testing.T) {
Account: record,
Entitlement: validEntitlementSnapshot(record.UserID, now),
EntitlementRecord: validEntitlementRecord(record.UserID, now),
Reservation: raceNameReservation(record.UserID, record.RaceName, now),
})
require.NoError(t, err)
@@ -248,7 +246,6 @@ func TestPolicyLifecycleSyncsAdminIndexes(t *testing.T) {
Account: record,
Entitlement: validEntitlementSnapshot(record.UserID, now),
EntitlementRecord: validEntitlementRecord(record.UserID, now),
Reservation: raceNameReservation(record.UserID, record.RaceName, now),
})
require.NoError(t, err)
@@ -325,7 +322,6 @@ func TestAdminListerReevaluatesExpiredPaidSnapshots(t *testing.T) {
Account: record,
Entitlement: validEntitlementSnapshot(userID, record.CreatedAt),
EntitlementRecord: validEntitlementRecord(userID, record.CreatedAt),
Reservation: raceNameReservation(userID, record.RaceName, record.CreatedAt),
})
require.NoError(t, err)
@@ -401,7 +397,7 @@ func (generator adminStoreIDGenerator) NewUserID() (common.UserID, error) {
return "", nil
}
func (generator adminStoreIDGenerator) NewInitialRaceName() (common.RaceName, error) {
func (generator adminStoreIDGenerator) NewUserName() (common.UserName, error) {
return "", nil
}
+101 -247
View File
@@ -61,19 +61,14 @@ type Store struct {
type accountRecord struct {
UserID string `json:"user_id"`
Email string `json:"email"`
RaceName string `json:"race_name"`
UserName string `json:"user_name"`
DisplayName string `json:"display_name,omitempty"`
PreferredLanguage string `json:"preferred_language"`
TimeZone string `json:"time_zone"`
DeclaredCountry *string `json:"declared_country,omitempty"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
}
type raceNameReservationRecord struct {
CanonicalKey string `json:"canonical_key"`
UserID string `json:"user_id"`
RaceName string `json:"race_name"`
ReservedAt string `json:"reserved_at"`
DeletedAt *string `json:"deleted_at,omitempty"`
}
type blockedEmailRecord struct {
@@ -190,8 +185,8 @@ func (store *Store) Ping(ctx context.Context) error {
return nil
}
// Create stores one new account record together with the exact and canonical
// race-name lookup state.
// Create stores one new account record together with the exact user-name
// lookup state.
func (store *Store) Create(ctx context.Context, input ports.CreateAccountInput) error {
if err := input.Validate(); err != nil {
return fmt.Errorf("create account in redis: %w", err)
@@ -201,15 +196,10 @@ func (store *Store) Create(ctx context.Context, input ports.CreateAccountInput)
if err != nil {
return fmt.Errorf("create account in redis: %w", err)
}
reservationPayload, err := marshalRaceNameReservationRecord(input.Reservation)
if err != nil {
return fmt.Errorf("create account in redis: %w", err)
}
accountKey := store.keyspace.Account(input.Account.UserID)
emailLookupKey := store.keyspace.EmailLookup(input.Account.Email)
raceNameLookupKey := store.keyspace.RaceNameLookup(input.Account.RaceName)
reservationKey := store.keyspace.RaceNameReservation(input.Reservation.CanonicalKey)
userNameLookupKey := store.keyspace.UserNameLookup(input.Account.UserName)
operationCtx, cancel, err := store.operationContext(ctx, "create account in redis")
if err != nil {
@@ -224,18 +214,17 @@ func (store *Store) Create(ctx context.Context, input ports.CreateAccountInput)
if err := ensureKeyAbsent(operationCtx, tx, emailLookupKey); err != nil {
return fmt.Errorf("create account %q in redis: %w", input.Account.UserID, err)
}
if err := ensureKeyAbsent(operationCtx, tx, raceNameLookupKey); err != nil {
return fmt.Errorf("create account %q in redis: %w", input.Account.UserID, err)
}
if err := ensureKeyAbsent(operationCtx, tx, reservationKey); err != nil {
if err := ensureKeyAbsent(operationCtx, tx, userNameLookupKey); err != nil {
if errors.Is(err, ports.ErrConflict) {
return fmt.Errorf("create account %q in redis: %w", input.Account.UserID, ports.ErrUserNameConflict)
}
return fmt.Errorf("create account %q in redis: %w", input.Account.UserID, err)
}
_, err := tx.TxPipelined(operationCtx, func(pipe redis.Pipeliner) error {
pipe.Set(operationCtx, accountKey, accountPayload, 0)
pipe.Set(operationCtx, emailLookupKey, input.Account.UserID.String(), 0)
pipe.Set(operationCtx, raceNameLookupKey, input.Account.UserID.String(), 0)
pipe.Set(operationCtx, reservationKey, reservationPayload, 0)
pipe.Set(operationCtx, userNameLookupKey, input.Account.UserID.String(), 0)
store.addCreatedAtIndex(pipe, operationCtx, input.Account)
store.syncDeclaredCountryIndex(pipe, operationCtx, account.UserAccount{}, input.Account)
return nil
@@ -245,7 +234,7 @@ func (store *Store) Create(ctx context.Context, input ports.CreateAccountInput)
}
return nil
}, accountKey, emailLookupKey, raceNameLookupKey, reservationKey)
}, accountKey, emailLookupKey, userNameLookupKey)
switch {
case errors.Is(watchErr, redis.TxFailedErr):
@@ -317,26 +306,26 @@ func (store *Store) GetByEmail(ctx context.Context, email common.Email) (account
return record, nil
}
// GetByRaceName returns the stored account identified by the exact stored race
// name.
func (store *Store) GetByRaceName(ctx context.Context, raceName common.RaceName) (account.UserAccount, error) {
if err := raceName.Validate(); err != nil {
return account.UserAccount{}, fmt.Errorf("get account by race name from redis: %w", err)
// GetByUserName returns the stored account identified by the exact stored
// user name.
func (store *Store) GetByUserName(ctx context.Context, userName common.UserName) (account.UserAccount, error) {
if err := userName.Validate(); err != nil {
return account.UserAccount{}, fmt.Errorf("get account by user name from redis: %w", err)
}
operationCtx, cancel, err := store.operationContext(ctx, "get account by race name from redis")
operationCtx, cancel, err := store.operationContext(ctx, "get account by user name from redis")
if err != nil {
return account.UserAccount{}, err
}
defer cancel()
userID, err := store.loadLookupUserID(operationCtx, store.client, store.keyspace.RaceNameLookup(raceName))
userID, err := store.loadLookupUserID(operationCtx, store.client, store.keyspace.UserNameLookup(userName))
if err != nil {
switch {
case errors.Is(err, ports.ErrNotFound):
return account.UserAccount{}, fmt.Errorf("get account by race name %q from redis: %w", raceName, ports.ErrNotFound)
return account.UserAccount{}, fmt.Errorf("get account by user name %q from redis: %w", userName, ports.ErrNotFound)
default:
return account.UserAccount{}, fmt.Errorf("get account by race name %q from redis: %w", raceName, err)
return account.UserAccount{}, fmt.Errorf("get account by user name %q from redis: %w", userName, err)
}
}
@@ -344,16 +333,18 @@ func (store *Store) GetByRaceName(ctx context.Context, raceName common.RaceName)
if err != nil {
switch {
case errors.Is(err, ports.ErrNotFound):
return account.UserAccount{}, fmt.Errorf("get account by race name %q from redis: lookup references missing user %q", raceName, userID)
return account.UserAccount{}, fmt.Errorf("get account by user name %q from redis: lookup references missing user %q", userName, userID)
default:
return account.UserAccount{}, fmt.Errorf("get account by race name %q from redis: %w", raceName, err)
return account.UserAccount{}, fmt.Errorf("get account by user name %q from redis: %w", userName, err)
}
}
return record, nil
}
// ExistsByUserID reports whether userID identifies a stored account.
// ExistsByUserID reports whether userID currently identifies a stored account
// that is not soft-deleted. Soft-deleted accounts are treated as non-existing
// for external callers per Stage 22.
func (store *Store) ExistsByUserID(ctx context.Context, userID common.UserID) (bool, error) {
if err := userID.Validate(); err != nil {
return false, fmt.Errorf("exists by user id from redis: %w", err)
@@ -365,114 +356,25 @@ func (store *Store) ExistsByUserID(ctx context.Context, userID common.UserID) (b
}
defer cancel()
exists, err := store.client.Exists(operationCtx, store.keyspace.Account(userID)).Result()
if err != nil {
record, err := store.loadAccount(operationCtx, store.client, userID)
switch {
case err == nil:
case errors.Is(err, ports.ErrNotFound):
return false, nil
default:
return false, fmt.Errorf("exists by user id %q from redis: %w", userID, err)
}
return exists == 1, nil
if record.IsDeleted() {
return false, nil
}
return true, nil
}
// RenameRaceName replaces the stored race name of userID and swaps the exact
// and canonical race-name lookup state atomically.
func (store *Store) RenameRaceName(ctx context.Context, input ports.RenameRaceNameInput) error {
if err := input.Validate(); err != nil {
return fmt.Errorf("rename account race name in redis: %w", err)
}
accountKey := store.keyspace.Account(input.UserID)
newRaceNameLookupKey := store.keyspace.RaceNameLookup(input.NewRaceName)
newReservationKey := store.keyspace.RaceNameReservation(input.NewReservation.CanonicalKey)
newReservationPayload, err := marshalRaceNameReservationRecord(input.NewReservation)
if err != nil {
return fmt.Errorf("rename account race name in redis: %w", err)
}
operationCtx, cancel, err := store.operationContext(ctx, "rename account race name in redis")
if err != nil {
return err
}
defer cancel()
watchErr := store.client.Watch(operationCtx, func(tx *redis.Tx) error {
record, err := store.loadAccount(operationCtx, tx, input.UserID)
if err != nil {
return fmt.Errorf("rename account race name %q in redis: %w", input.UserID, err)
}
if record.RaceName == input.NewRaceName {
return nil
}
currentRaceNameLookupKey := store.keyspace.RaceNameLookup(record.RaceName)
currentLookupUserID, err := store.loadLookupUserID(operationCtx, tx, currentRaceNameLookupKey)
if err != nil {
return fmt.Errorf("rename account race name %q in redis: %w", input.UserID, err)
}
if currentLookupUserID != input.UserID {
return fmt.Errorf("rename account race name %q in redis: %w", input.UserID, ports.ErrConflict)
}
currentReservation, err := store.loadRaceNameReservation(operationCtx, tx, input.CurrentCanonicalKey)
if err != nil {
return fmt.Errorf("rename account race name %q in redis: %w", input.UserID, err)
}
if currentReservation.UserID != input.UserID || currentReservation.RaceName != record.RaceName {
return fmt.Errorf("rename account race name %q in redis: %w", input.UserID, ports.ErrConflict)
}
if err := ensureLookupAvailableOrOwned(operationCtx, tx, newRaceNameLookupKey, input.UserID); err != nil {
if errors.Is(err, ports.ErrConflict) {
return fmt.Errorf("rename account race name %q in redis: %w", input.UserID, ports.ErrRaceNameConflict)
}
return fmt.Errorf("rename account race name %q in redis: %w", input.UserID, err)
}
if input.CurrentCanonicalKey != input.NewReservation.CanonicalKey {
if err := store.ensureReservationAvailableOrOwned(operationCtx, tx, input.NewReservation.CanonicalKey, input.UserID); err != nil {
if errors.Is(err, ports.ErrConflict) {
return fmt.Errorf("rename account race name %q in redis: %w", input.UserID, ports.ErrRaceNameConflict)
}
return fmt.Errorf("rename account race name %q in redis: %w", input.UserID, err)
}
}
record.RaceName = input.NewRaceName
record.UpdatedAt = input.UpdatedAt.UTC()
payload, err := marshalAccountRecord(record)
if err != nil {
return fmt.Errorf("rename account race name %q in redis: %w", input.UserID, err)
}
_, err = tx.TxPipelined(operationCtx, func(pipe redis.Pipeliner) error {
pipe.Set(operationCtx, accountKey, payload, 0)
pipe.Set(operationCtx, newRaceNameLookupKey, input.UserID.String(), 0)
pipe.Set(operationCtx, newReservationKey, newReservationPayload, 0)
pipe.Del(operationCtx, currentRaceNameLookupKey)
if input.CurrentCanonicalKey != input.NewReservation.CanonicalKey {
pipe.Del(operationCtx, store.keyspace.RaceNameReservation(input.CurrentCanonicalKey))
}
return nil
})
if err != nil {
return fmt.Errorf("rename account race name %q in redis: %w", input.UserID, err)
}
return nil
}, accountKey, newRaceNameLookupKey, newReservationKey)
switch {
case errors.Is(watchErr, redis.TxFailedErr):
return fmt.Errorf("rename account race name %q in redis: %w", input.UserID, ports.ErrConflict)
case watchErr != nil:
return watchErr
default:
return nil
}
}
// Update replaces the stored account state for record.UserID.
// Update replaces the stored account state for record.UserID. `email` and
// `user_name` are immutable; any attempt to mutate them returns
// ports.ErrConflict.
func (store *Store) Update(ctx context.Context, record account.UserAccount) error {
if err := record.Validate(); err != nil {
return fmt.Errorf("update account in redis: %w", err)
@@ -485,7 +387,7 @@ func (store *Store) Update(ctx context.Context, record account.UserAccount) erro
accountKey := store.keyspace.Account(record.UserID)
emailLookupKey := store.keyspace.EmailLookup(record.Email)
raceNameLookupKey := store.keyspace.RaceNameLookup(record.RaceName)
userNameLookupKey := store.keyspace.UserNameLookup(record.UserName)
operationCtx, cancel, err := store.operationContext(ctx, "update account in redis")
if err != nil {
@@ -498,7 +400,7 @@ func (store *Store) Update(ctx context.Context, record account.UserAccount) erro
if err != nil {
return fmt.Errorf("update account %q in redis: %w", record.UserID, err)
}
if current.Email != record.Email || current.RaceName != record.RaceName {
if current.Email != record.Email || current.UserName != record.UserName {
return fmt.Errorf("update account %q in redis: %w", record.UserID, ports.ErrConflict)
}
@@ -510,11 +412,11 @@ func (store *Store) Update(ctx context.Context, record account.UserAccount) erro
return fmt.Errorf("update account %q in redis: %w", record.UserID, ports.ErrConflict)
}
raceLookupUserID, err := store.loadLookupUserID(operationCtx, tx, raceNameLookupKey)
userNameLookupUserID, err := store.loadLookupUserID(operationCtx, tx, userNameLookupKey)
if err != nil {
return fmt.Errorf("update account %q in redis: %w", record.UserID, err)
}
if raceLookupUserID != record.UserID {
if userNameLookupUserID != record.UserID {
return fmt.Errorf("update account %q in redis: %w", record.UserID, ports.ErrConflict)
}
@@ -528,7 +430,7 @@ func (store *Store) Update(ctx context.Context, record account.UserAccount) erro
}
return nil
}, accountKey, emailLookupKey, raceNameLookupKey)
}, accountKey, emailLookupKey, userNameLookupKey)
switch {
case errors.Is(watchErr, redis.TxFailedErr):
@@ -965,17 +867,31 @@ func (store *Store) ResolveByEmail(ctx context.Context, email common.Email) (por
accountRecord, err := store.GetByEmailAccount(operationCtx, email)
switch {
case err == nil:
return ports.ResolveByEmailResult{
Kind: ports.AuthResolutionKindExisting,
UserID: accountRecord.UserID,
}, nil
case errors.Is(err, ports.ErrNotFound):
return ports.ResolveByEmailResult{Kind: ports.AuthResolutionKindCreatable}, nil
default:
return ports.ResolveByEmailResult{}, fmt.Errorf("resolve by email %q in redis: %w", email, err)
}
if accountRecord.IsDeleted() {
return ports.ResolveByEmailResult{
Kind: ports.AuthResolutionKindBlocked,
BlockReasonCode: deletedAccountBlockReasonCode,
}, nil
}
return ports.ResolveByEmailResult{
Kind: ports.AuthResolutionKindExisting,
UserID: accountRecord.UserID,
}, nil
}
// deletedAccountBlockReasonCode is the reason_code returned when an auth-facing
// lookup resolves to a soft-deleted account. It is not a real sanction; the
// auth/session service treats it as a blocked outcome and refuses to issue a
// session for the subject.
const deletedAccountBlockReasonCode common.ReasonCode = "account_deleted"
// EnsureByEmail atomically returns an existing user, creates a new one, or
// reports a blocked outcome for one e-mail subject.
func (store *Store) EnsureByEmail(ctx context.Context, input ports.EnsureByEmailInput) (ports.EnsureByEmailResult, error) {
@@ -995,11 +911,6 @@ func (store *Store) EnsureByEmail(ctx context.Context, input ports.EnsureByEmail
if err != nil {
return ports.EnsureByEmailResult{}, fmt.Errorf("ensure by email in redis: %w", err)
}
reservationPayload, err := marshalRaceNameReservationRecord(input.Reservation)
if err != nil {
return ports.EnsureByEmailResult{}, fmt.Errorf("ensure by email in redis: %w", err)
}
operationCtx, cancel, err := store.operationContext(ctx, "ensure by email in redis")
if err != nil {
return ports.EnsureByEmailResult{}, err
@@ -1011,8 +922,7 @@ func (store *Store) EnsureByEmail(ctx context.Context, input ports.EnsureByEmail
accountKey := store.keyspace.Account(input.Account.UserID)
emailLookupKey := store.keyspace.EmailLookup(input.Email)
raceNameLookupKey := store.keyspace.RaceNameLookup(input.Account.RaceName)
reservationKey := store.keyspace.RaceNameReservation(input.Reservation.CanonicalKey)
userNameLookupKey := store.keyspace.UserNameLookup(input.Account.UserName)
blockedEmailKey := store.keyspace.BlockedEmailSubject(input.Email)
entitlementKey := store.keyspace.EntitlementSnapshot(input.Account.UserID)
entitlementRecordKey := store.keyspace.EntitlementRecord(input.EntitlementRecord.RecordID)
@@ -1039,6 +949,14 @@ func (store *Store) EnsureByEmail(ctx context.Context, input ports.EnsureByEmail
if err != nil {
return fmt.Errorf("ensure by email %q in redis: %w", input.Email, err)
}
if record.IsDeleted() {
result = ports.EnsureByEmailResult{
Outcome: ports.EnsureByEmailOutcomeBlocked,
BlockReasonCode: deletedAccountBlockReasonCode,
}
handled = true
return nil
}
result = ports.EnsureByEmailResult{
Outcome: ports.EnsureByEmailOutcomeExisting,
UserID: record.UserID,
@@ -1052,15 +970,9 @@ func (store *Store) EnsureByEmail(ctx context.Context, input ports.EnsureByEmail
if err := ensureKeyAbsent(operationCtx, tx, accountKey); err != nil {
return fmt.Errorf("ensure by email %q in redis: %w", input.Email, err)
}
if err := ensureKeyAbsent(operationCtx, tx, raceNameLookupKey); err != nil {
if err := ensureKeyAbsent(operationCtx, tx, userNameLookupKey); err != nil {
if errors.Is(err, ports.ErrConflict) {
return fmt.Errorf("ensure by email %q in redis: %w", input.Email, ports.ErrRaceNameConflict)
}
return fmt.Errorf("ensure by email %q in redis: %w", input.Email, err)
}
if err := ensureKeyAbsent(operationCtx, tx, reservationKey); err != nil {
if errors.Is(err, ports.ErrConflict) {
return fmt.Errorf("ensure by email %q in redis: %w", input.Email, ports.ErrRaceNameConflict)
return fmt.Errorf("ensure by email %q in redis: %w", input.Email, ports.ErrUserNameConflict)
}
return fmt.Errorf("ensure by email %q in redis: %w", input.Email, err)
}
@@ -1074,8 +986,7 @@ func (store *Store) EnsureByEmail(ctx context.Context, input ports.EnsureByEmail
_, err = tx.TxPipelined(operationCtx, func(pipe redis.Pipeliner) error {
pipe.Set(operationCtx, accountKey, accountPayload, 0)
pipe.Set(operationCtx, emailLookupKey, input.Account.UserID.String(), 0)
pipe.Set(operationCtx, raceNameLookupKey, input.Account.UserID.String(), 0)
pipe.Set(operationCtx, reservationKey, reservationPayload, 0)
pipe.Set(operationCtx, userNameLookupKey, input.Account.UserID.String(), 0)
pipe.Set(operationCtx, entitlementKey, entitlementPayload, 0)
pipe.Set(operationCtx, entitlementRecordKey, entitlementRecordPayload, 0)
pipe.ZAdd(operationCtx, entitlementHistoryKey, redis.Z{
@@ -1100,7 +1011,7 @@ func (store *Store) EnsureByEmail(ctx context.Context, input ports.EnsureByEmail
}
handled = true
return nil
}, blockedEmailKey, emailLookupKey, accountKey, raceNameLookupKey, reservationKey, entitlementKey, entitlementRecordKey, entitlementHistoryKey)
}, blockedEmailKey, emailLookupKey, accountKey, userNameLookupKey, entitlementKey, entitlementRecordKey, entitlementHistoryKey)
switch {
case errors.Is(watchErr, redis.TxFailedErr):
@@ -1136,6 +1047,9 @@ func (store *Store) BlockByUserID(ctx context.Context, input ports.BlockByUserID
}
return ports.BlockResult{}, fmt.Errorf("block by user id %q in redis: %w", input.UserID, err)
}
if currentAccount.IsDeleted() {
return ports.BlockResult{}, fmt.Errorf("block by user id %q in redis: %w", input.UserID, ports.ErrNotFound)
}
accountKey := store.keyspace.Account(input.UserID)
blockedEmailKey := store.keyspace.BlockedEmailSubject(currentAccount.Email)
@@ -1145,6 +1059,9 @@ func (store *Store) BlockByUserID(ctx context.Context, input ports.BlockByUserID
if err != nil {
return fmt.Errorf("block by user id %q in redis: %w", input.UserID, err)
}
if accountRecord.IsDeleted() {
return fmt.Errorf("block by user id %q in redis: %w", input.UserID, ports.ErrNotFound)
}
blocked, err := store.loadBlockedEmail(operationCtx, tx, accountRecord.Email)
switch {
@@ -1327,22 +1244,6 @@ func (store *Store) loadLookupUserID(ctx context.Context, getter bytesGetter, ke
return userID, nil
}
func (store *Store) loadRaceNameReservation(
ctx context.Context,
getter bytesGetter,
key account.RaceNameCanonicalKey,
) (account.RaceNameReservation, error) {
payload, err := getter.Get(ctx, store.keyspace.RaceNameReservation(key)).Bytes()
switch {
case errors.Is(err, redis.Nil):
return account.RaceNameReservation{}, ports.ErrNotFound
case err != nil:
return account.RaceNameReservation{}, err
}
return decodeRaceNameReservationRecord(payload)
}
func (store *Store) loadBlockedEmail(ctx context.Context, getter bytesGetter, email common.Email) (authblock.BlockedEmailSubject, error) {
payload, err := getter.Get(ctx, store.keyspace.BlockedEmailSubject(email)).Bytes()
switch {
@@ -1436,32 +1337,12 @@ func ensureLookupAvailableOrOwned(
return nil
}
func (store *Store) ensureReservationAvailableOrOwned(
ctx context.Context,
getter bytesGetter,
key account.RaceNameCanonicalKey,
userID common.UserID,
) error {
record, err := store.loadRaceNameReservation(ctx, getter, key)
switch {
case errors.Is(err, ports.ErrNotFound):
return nil
case err != nil:
return err
}
if record.UserID != userID {
return ports.ErrConflict
}
return nil
}
func marshalAccountRecord(record account.UserAccount) ([]byte, error) {
encoded := accountRecord{
UserID: record.UserID.String(),
Email: record.Email.String(),
RaceName: record.RaceName.String(),
UserName: record.UserName.String(),
DisplayName: record.DisplayName.String(),
PreferredLanguage: record.PreferredLanguage.String(),
TimeZone: record.TimeZone.String(),
CreatedAt: record.CreatedAt.UTC().Format(time.RFC3339Nano),
@@ -1471,6 +1352,10 @@ func marshalAccountRecord(record account.UserAccount) ([]byte, error) {
value := record.DeclaredCountry.String()
encoded.DeclaredCountry = &value
}
if record.DeletedAt != nil {
value := record.DeletedAt.UTC().Format(time.RFC3339Nano)
encoded.DeletedAt = &value
}
return json.Marshal(encoded)
}
@@ -1493,7 +1378,8 @@ func decodeAccountRecord(payload []byte) (account.UserAccount, error) {
record := account.UserAccount{
UserID: common.UserID(encoded.UserID),
Email: common.Email(encoded.Email),
RaceName: common.RaceName(encoded.RaceName),
UserName: common.UserName(encoded.UserName),
DisplayName: common.DisplayName(encoded.DisplayName),
PreferredLanguage: common.LanguageTag(encoded.PreferredLanguage),
TimeZone: common.TimeZoneName(encoded.TimeZone),
CreatedAt: createdAt.UTC(),
@@ -1502,6 +1388,14 @@ func decodeAccountRecord(payload []byte) (account.UserAccount, error) {
if encoded.DeclaredCountry != nil {
record.DeclaredCountry = common.CountryCode(*encoded.DeclaredCountry)
}
if encoded.DeletedAt != nil {
deletedAt, err := time.Parse(time.RFC3339Nano, *encoded.DeletedAt)
if err != nil {
return account.UserAccount{}, fmt.Errorf("decode account record deleted_at: %w", err)
}
deletedAt = deletedAt.UTC()
record.DeletedAt = &deletedAt
}
if err := record.Validate(); err != nil {
return account.UserAccount{}, fmt.Errorf("decode account record: %w", err)
}
@@ -1509,41 +1403,6 @@ func decodeAccountRecord(payload []byte) (account.UserAccount, error) {
return record, nil
}
func marshalRaceNameReservationRecord(record account.RaceNameReservation) ([]byte, error) {
encoded := raceNameReservationRecord{
CanonicalKey: record.CanonicalKey.String(),
UserID: record.UserID.String(),
RaceName: record.RaceName.String(),
ReservedAt: record.ReservedAt.UTC().Format(time.RFC3339Nano),
}
return json.Marshal(encoded)
}
func decodeRaceNameReservationRecord(payload []byte) (account.RaceNameReservation, error) {
var encoded raceNameReservationRecord
if err := decodeJSONPayload(payload, &encoded); err != nil {
return account.RaceNameReservation{}, err
}
reservedAt, err := time.Parse(time.RFC3339Nano, encoded.ReservedAt)
if err != nil {
return account.RaceNameReservation{}, fmt.Errorf("decode race-name reservation reserved_at: %w", err)
}
record := account.RaceNameReservation{
CanonicalKey: account.RaceNameCanonicalKey(encoded.CanonicalKey),
UserID: common.UserID(encoded.UserID),
RaceName: common.RaceName(encoded.RaceName),
ReservedAt: reservedAt.UTC(),
}
if err := record.Validate(); err != nil {
return account.RaceNameReservation{}, fmt.Errorf("decode race-name reservation: %w", err)
}
return record, nil
}
func marshalBlockedEmailRecord(record authblock.BlockedEmailSubject) ([]byte, error) {
encoded := blockedEmailRecord{
Email: record.Email.String(),
@@ -1902,9 +1761,9 @@ func (adapter *AccountStore) GetByEmail(ctx context.Context, email common.Email)
return adapter.store.GetByEmail(ctx, email)
}
// GetByRaceName returns the stored account identified by raceName.
func (adapter *AccountStore) GetByRaceName(ctx context.Context, raceName common.RaceName) (account.UserAccount, error) {
return adapter.store.GetByRaceName(ctx, raceName)
// GetByUserName returns the stored account identified by userName.
func (adapter *AccountStore) GetByUserName(ctx context.Context, userName common.UserName) (account.UserAccount, error) {
return adapter.store.GetByUserName(ctx, userName)
}
// ExistsByUserID reports whether userID currently identifies a stored
@@ -1913,11 +1772,6 @@ func (adapter *AccountStore) ExistsByUserID(ctx context.Context, userID common.U
return adapter.store.ExistsByUserID(ctx, userID)
}
// RenameRaceName replaces the stored race name of userID atomically.
func (adapter *AccountStore) RenameRaceName(ctx context.Context, input ports.RenameRaceNameInput) error {
return adapter.store.RenameRaceName(ctx, input)
}
// Update replaces the stored account state for record.UserID.
func (adapter *AccountStore) Update(ctx context.Context, record account.UserAccount) error {
return adapter.store.Update(ctx, record)
@@ -2,7 +2,6 @@ package userstore
import (
"context"
"strings"
"testing"
"time"
@@ -34,18 +33,13 @@ func TestAccountStoreCreateAndLookups(t *testing.T) {
require.NoError(t, err)
require.Equal(t, record, byEmail)
byRaceName, err := accountStore.GetByRaceName(context.Background(), record.RaceName)
byUserName, err := accountStore.GetByUserName(context.Background(), record.UserName)
require.NoError(t, err)
require.Equal(t, record, byRaceName)
require.Equal(t, record, byUserName)
exists, err := accountStore.ExistsByUserID(context.Background(), record.UserID)
require.NoError(t, err)
require.True(t, exists)
reservation, err := store.loadRaceNameReservation(context.Background(), store.client, canonicalKey(record.RaceName))
require.NoError(t, err)
require.Equal(t, record.UserID, reservation.UserID)
require.Equal(t, record.RaceName, reservation.RaceName)
}
func TestBlockedEmailStoreUpsertAndGet(t *testing.T) {
@@ -80,14 +74,13 @@ func TestEnsureResolveAndBlockFlows(t *testing.T) {
Account: accountRecord,
Entitlement: entitlementSnapshot,
EntitlementRecord: validEntitlementRecord(accountRecord.UserID, now),
Reservation: raceNameReservation(accountRecord.UserID, accountRecord.RaceName, accountRecord.UpdatedAt),
})
require.NoError(t, err)
require.Equal(t, ports.EnsureByEmailOutcomeCreated, created.Outcome)
reservation, err := store.loadRaceNameReservation(context.Background(), store.client, canonicalKey(accountRecord.RaceName))
byUserName, err := store.GetByUserName(context.Background(), accountRecord.UserName)
require.NoError(t, err)
require.Equal(t, accountRecord.UserID, reservation.UserID)
require.Equal(t, accountRecord.UserID, byUserName.UserID)
entitlementHistory, err := store.ListEntitlementRecordsByUserID(context.Background(), accountRecord.UserID)
require.NoError(t, err)
@@ -124,7 +117,6 @@ func TestEnsureResolveAndBlockFlows(t *testing.T) {
Account: accountRecord,
Entitlement: entitlementSnapshot,
EntitlementRecord: validEntitlementRecord(accountRecord.UserID, now),
Reservation: raceNameReservation(accountRecord.UserID, accountRecord.RaceName, accountRecord.UpdatedAt),
})
require.NoError(t, err)
require.Equal(t, ports.EnsureByEmailOutcomeBlocked, ensureBlocked.Outcome)
@@ -156,7 +148,6 @@ func TestBlockedEmailWithoutUserPreventsEnsureCreate(t *testing.T) {
Account: accountRecord,
Entitlement: entitlementSnapshot,
EntitlementRecord: validEntitlementRecord(accountRecord.UserID, now),
Reservation: raceNameReservation(accountRecord.UserID, accountRecord.RaceName, accountRecord.UpdatedAt),
})
require.NoError(t, err)
require.Equal(t, ports.EnsureByEmailOutcomeBlocked, ensured.Outcome)
@@ -174,7 +165,7 @@ func TestEnsureByEmailExistingDoesNotOverwriteStoredSettings(t *testing.T) {
existingAccount := account.UserAccount{
UserID: common.UserID("user-existing"),
Email: common.Email("pilot@example.com"),
RaceName: common.RaceName("Pilot Nova"),
UserName: common.UserName("player-abcdefgh"),
PreferredLanguage: common.LanguageTag("en"),
TimeZone: common.TimeZoneName("Europe/Kaliningrad"),
CreatedAt: createdAt,
@@ -187,7 +178,7 @@ func TestEnsureByEmailExistingDoesNotOverwriteStoredSettings(t *testing.T) {
Account: account.UserAccount{
UserID: common.UserID("user-created"),
Email: existingAccount.Email,
RaceName: common.RaceName("player-new123"),
UserName: common.UserName("player-newabcde"),
PreferredLanguage: common.LanguageTag("fr-FR"),
TimeZone: common.TimeZoneName("UTC"),
CreatedAt: createdAt.Add(time.Minute),
@@ -195,7 +186,6 @@ func TestEnsureByEmailExistingDoesNotOverwriteStoredSettings(t *testing.T) {
},
Entitlement: validEntitlementSnapshot(common.UserID("user-created"), createdAt.Add(time.Minute)),
EntitlementRecord: validEntitlementRecord(common.UserID("user-created"), createdAt.Add(time.Minute)),
Reservation: raceNameReservation(common.UserID("user-created"), common.RaceName("player-new123"), createdAt.Add(time.Minute)),
})
require.NoError(t, err)
require.Equal(t, ports.EnsureByEmailOutcomeExisting, result.Outcome)
@@ -206,76 +196,49 @@ func TestEnsureByEmailExistingDoesNotOverwriteStoredSettings(t *testing.T) {
require.Equal(t, existingAccount, storedAccount)
}
func TestAccountStoreRenameRaceNameSwapsLookupAtomically(t *testing.T) {
t.Parallel()
store := newTestStore(t)
accountStore := store.Accounts()
record := validAccountRecord()
require.NoError(t, accountStore.Create(context.Background(), createAccountInput(record)))
updatedAt := record.UpdatedAt.Add(time.Minute)
require.NoError(t, accountStore.RenameRaceName(context.Background(), renameRaceNameInput(record, common.RaceName("Nova Prime"), updatedAt)))
stored, err := accountStore.GetByUserID(context.Background(), record.UserID)
require.NoError(t, err)
require.Equal(t, common.RaceName("Nova Prime"), stored.RaceName)
require.True(t, stored.UpdatedAt.Equal(updatedAt))
_, err = accountStore.GetByRaceName(context.Background(), record.RaceName)
require.ErrorIs(t, err, ports.ErrNotFound)
renamed, err := accountStore.GetByRaceName(context.Background(), common.RaceName("Nova Prime"))
require.NoError(t, err)
require.Equal(t, record.UserID, renamed.UserID)
_, err = store.loadRaceNameReservation(context.Background(), store.client, canonicalKey(record.RaceName))
require.ErrorIs(t, err, ports.ErrNotFound)
reservation, err := store.loadRaceNameReservation(context.Background(), store.client, canonicalKey(common.RaceName("Nova Prime")))
require.NoError(t, err)
require.Equal(t, common.RaceName("Nova Prime"), reservation.RaceName)
}
func TestAccountStoreRenameRaceNameAllowsSameOwnerCanonicalSlot(t *testing.T) {
func TestAccountStoreUpdateDisplayNamePreservesImmutableFields(t *testing.T) {
t.Parallel()
store := newTestStore(t)
accountStore := store.Accounts()
record := validAccountRecord()
record.RaceName = common.RaceName("Pilot Nova")
require.NoError(t, accountStore.Create(context.Background(), createAccountInput(record)))
updatedAt := record.UpdatedAt.Add(time.Minute)
require.NoError(t, accountStore.RenameRaceName(context.Background(), renameRaceNameInput(record, common.RaceName("P1lot Nova"), updatedAt)))
updated := record
updated.DisplayName = common.DisplayName("NovaPrime")
updated.UpdatedAt = record.UpdatedAt.Add(time.Minute)
reservation, err := store.loadRaceNameReservation(context.Background(), store.client, canonicalKey(common.RaceName("P1lot Nova")))
require.NoError(t, accountStore.Update(context.Background(), updated))
byUserID, err := accountStore.GetByUserID(context.Background(), record.UserID)
require.NoError(t, err)
require.Equal(t, common.RaceName("P1lot Nova"), reservation.RaceName)
require.Equal(t, updated, byUserID)
byEmail, err := accountStore.GetByEmail(context.Background(), record.Email)
require.NoError(t, err)
require.Equal(t, updated, byEmail)
byUserName, err := accountStore.GetByUserName(context.Background(), record.UserName)
require.NoError(t, err)
require.Equal(t, updated, byUserName)
}
func TestAccountStoreRenameRaceNameReturnsConflictWhenTargetExists(t *testing.T) {
func TestAccountStoreUpdateRejectsUserNameMutation(t *testing.T) {
t.Parallel()
store := newTestStore(t)
accountStore := store.Accounts()
first := validAccountRecord()
second := validAccountRecord()
second.UserID = common.UserID("user-456")
second.Email = common.Email("other@example.com")
second.RaceName = common.RaceName("Taken Name")
record := validAccountRecord()
require.NoError(t, accountStore.Create(context.Background(), createAccountInput(record)))
require.NoError(t, accountStore.Create(context.Background(), createAccountInput(first)))
require.NoError(t, accountStore.Create(context.Background(), createAccountInput(second)))
attempted := record
attempted.UserName = common.UserName("player-changed")
attempted.UpdatedAt = record.UpdatedAt.Add(time.Minute)
err := accountStore.RenameRaceName(context.Background(), renameRaceNameInput(first, second.RaceName, first.UpdatedAt.Add(time.Minute)))
err := accountStore.Update(context.Background(), attempted)
require.ErrorIs(t, err, ports.ErrConflict)
stored, err := accountStore.GetByUserID(context.Background(), first.UserID)
require.NoError(t, err)
require.Equal(t, first.RaceName, stored.RaceName)
}
func TestAccountStoreUpdateDeclaredCountryPreservesLookups(t *testing.T) {
@@ -301,12 +264,35 @@ func TestAccountStoreUpdateDeclaredCountryPreservesLookups(t *testing.T) {
require.NoError(t, err)
require.Equal(t, updated, byEmail)
byRaceName, err := accountStore.GetByRaceName(context.Background(), record.RaceName)
byUserName, err := accountStore.GetByUserName(context.Background(), record.UserName)
require.NoError(t, err)
require.Equal(t, updated, byRaceName)
require.Equal(t, updated, byUserName)
}
func TestAccountStoreCreateReturnsConflictWhenCanonicalReservationExists(t *testing.T) {
func TestAccountStorePersistsSoftDeleteMarker(t *testing.T) {
t.Parallel()
store := newTestStore(t)
accountStore := store.Accounts()
record := validAccountRecord()
require.NoError(t, accountStore.Create(context.Background(), createAccountInput(record)))
deletedAt := record.UpdatedAt.Add(time.Hour)
updated := record
updated.UpdatedAt = deletedAt
updated.DeletedAt = &deletedAt
require.NoError(t, accountStore.Update(context.Background(), updated))
byUserID, err := accountStore.GetByUserID(context.Background(), record.UserID)
require.NoError(t, err)
require.NotNil(t, byUserID.DeletedAt)
require.True(t, byUserID.DeletedAt.Equal(deletedAt))
require.True(t, byUserID.IsDeleted())
}
func TestAccountStoreCreateReturnsUserNameConflict(t *testing.T) {
t.Parallel()
store := newTestStore(t)
@@ -316,12 +302,11 @@ func TestAccountStoreCreateReturnsConflictWhenCanonicalReservationExists(t *test
second := validAccountRecord()
second.UserID = common.UserID("user-456")
second.Email = common.Email("other@example.com")
second.RaceName = common.RaceName("P1lot Nova")
require.NoError(t, accountStore.Create(context.Background(), createAccountInput(first)))
err := accountStore.Create(context.Background(), createAccountInput(second))
require.ErrorIs(t, err, ports.ErrConflict)
require.ErrorIs(t, err, ports.ErrUserNameConflict)
}
func TestBlockByUserIDRepeatedCallsStayIdempotent(t *testing.T) {
@@ -805,7 +790,7 @@ func validAccountRecord() account.UserAccount {
return account.UserAccount{
UserID: common.UserID("user-123"),
Email: common.Email("pilot@example.com"),
RaceName: common.RaceName("Pilot Nova"),
UserName: common.UserName("player-abcdefgh"),
PreferredLanguage: common.LanguageTag("en"),
TimeZone: common.TimeZoneName("Europe/Kaliningrad"),
CreatedAt: createdAt,
@@ -889,42 +874,6 @@ func timePointer(value time.Time) *time.Time {
func createAccountInput(record account.UserAccount) ports.CreateAccountInput {
return ports.CreateAccountInput{
Account: record,
Reservation: raceNameReservation(record.UserID, record.RaceName, record.UpdatedAt),
Account: record,
}
}
func renameRaceNameInput(
record account.UserAccount,
newRaceName common.RaceName,
updatedAt time.Time,
) ports.RenameRaceNameInput {
return ports.RenameRaceNameInput{
UserID: record.UserID,
CurrentCanonicalKey: canonicalKey(record.RaceName),
NewRaceName: newRaceName,
NewReservation: raceNameReservation(record.UserID, newRaceName, updatedAt),
UpdatedAt: updatedAt,
}
}
func raceNameReservation(
userID common.UserID,
raceName common.RaceName,
reservedAt time.Time,
) account.RaceNameReservation {
return account.RaceNameReservation{
CanonicalKey: canonicalKey(raceName),
UserID: userID,
RaceName: raceName,
ReservedAt: reservedAt.UTC(),
}
}
func canonicalKey(raceName common.RaceName) account.RaceNameCanonicalKey {
return account.RaceNameCanonicalKey(strings.NewReplacer(
"1", "i",
"0", "o",
"8", "b",
).Replace(strings.ToLower(raceName.String())))
}