159 lines
5.1 KiB
Go
159 lines
5.1 KiB
Go
package identity
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"lincodex/backend/internal/domain"
|
|
"lincodex/backend/internal/port/outbound"
|
|
)
|
|
|
|
const confirmationTestPasswordHash = "$argon2id$v=19$m=65536,t=3,p=4$c2FsdHNhbHRzYWx0c2FsdA$aGFzaGhhc2hoYXNoaGFzaGhhc2hoYXNoaGFzaGhhc2g"
|
|
|
|
type confirmationTokenReader struct {
|
|
outbound.EmailTokenRepository
|
|
|
|
token domain.EmailToken
|
|
err error
|
|
calls int
|
|
}
|
|
|
|
func (repository *confirmationTokenReader) FindByHash(context.Context, domain.SecretTokenHash, domain.EmailTokenPurpose) (domain.EmailToken, error) {
|
|
repository.calls++
|
|
return repository.token, repository.err
|
|
}
|
|
|
|
type confirmationTestLimiter struct {
|
|
keys []string
|
|
first outbound.RateLimitDecision
|
|
firstError error
|
|
}
|
|
|
|
func (limiter *confirmationTestLimiter) Take(_ context.Context, key string, _ ...outbound.RateLimitRule) (outbound.RateLimitDecision, error) {
|
|
limiter.keys = append(limiter.keys, key)
|
|
if len(limiter.keys) == 1 {
|
|
return limiter.first, limiter.firstError
|
|
}
|
|
return outbound.RateLimitDecision{Allowed: true}, nil
|
|
}
|
|
|
|
type confirmationTestTransactor struct {
|
|
calls int
|
|
}
|
|
|
|
func (transactor *confirmationTestTransactor) WithinTransaction(context.Context, func(context.Context, outbound.UnitOfWork) error) error {
|
|
transactor.calls++
|
|
return errors.New("unexpected transaction")
|
|
}
|
|
|
|
func TestEmailConfirmationKeepsInvalidTokenPrecedenceOverExpiry(t *testing.T) {
|
|
now := time.Date(2026, time.October, 7, 12, 0, 0, 0, time.UTC)
|
|
raw, err := domain.NewSecretToken()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
expired := now.Add(-time.Second)
|
|
passwordHash, err := domain.NewPasswordHash(confirmationTestPasswordHash)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
pending, err := domain.NewPendingRegistration(passwordHash, domain.ScenarioCandidate)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
consumedAt := now.Add(-time.Minute)
|
|
invalidatedAt := now.Add(-time.Minute)
|
|
for _, test := range []struct {
|
|
name string
|
|
token domain.EmailToken
|
|
readError error
|
|
want error
|
|
}{
|
|
{
|
|
name: "consumed",
|
|
token: domain.EmailToken{ExpiresAt: expired, ConsumedAt: &consumedAt},
|
|
want: ErrVerificationTokenInvalid,
|
|
},
|
|
{
|
|
name: "invalidated",
|
|
token: domain.EmailToken{ExpiresAt: expired, InvalidatedAt: &invalidatedAt},
|
|
want: ErrVerificationTokenInvalid,
|
|
},
|
|
{
|
|
name: "superseded registration",
|
|
readError: outbound.ErrEmailTokenInvalidated,
|
|
want: ErrVerificationTokenInvalid,
|
|
},
|
|
{
|
|
name: "current expired token",
|
|
token: domain.EmailToken{
|
|
UserID: uuid.New(), Purpose: domain.EmailTokenPurposeEmailVerification,
|
|
TokenHash: raw.Hash(), ExpiresAt: expired, Pending: &pending,
|
|
},
|
|
want: ErrVerificationTokenExpired,
|
|
},
|
|
} {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
tokens := &confirmationTokenReader{token: test.token, err: test.readError}
|
|
transactor := &confirmationTestTransactor{}
|
|
limiter := &confirmationTestLimiter{first: outbound.RateLimitDecision{Allowed: true}}
|
|
keyring, err := NewKeyring([]byte("confirmation key with enough entropy"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
confirmation, err := NewEmailConfirmation(ConfirmationDependencies{
|
|
Transactor: transactor, Tokens: tokens, Credentials: NewCredentials(nil),
|
|
Limiter: limiter, Keyring: keyring, Clock: func() time.Time { return now },
|
|
}, ConfirmationSettings{TokenHourlyLimit: 5, ClientHourlyLimit: 20})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
_, err = confirmation.Confirm(context.Background(), ConfirmCommand{Token: raw.Reveal(), Password: "correct horse battery staple"})
|
|
if !errors.Is(err, test.want) {
|
|
t.Fatalf("Confirm() error = %v, want %v", err, test.want)
|
|
}
|
|
if transactor.calls != 0 {
|
|
t.Fatalf("transaction calls = %d, want 0", transactor.calls)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestEmailConfirmationClientLimitRunsBeforeTokenLookup(t *testing.T) {
|
|
raw, err := domain.NewSecretToken()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
keyring, err := NewKeyring([]byte("confirmation key with enough entropy"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
tokens := &confirmationTokenReader{}
|
|
transactor := &confirmationTestTransactor{}
|
|
limiter := &confirmationTestLimiter{first: outbound.RateLimitDecision{Allowed: false, RetryAfter: time.Minute}}
|
|
confirmation, err := NewEmailConfirmation(ConfirmationDependencies{
|
|
Transactor: transactor, Tokens: tokens, Credentials: NewCredentials(nil),
|
|
Limiter: limiter, Keyring: keyring, Clock: time.Now,
|
|
}, ConfirmationSettings{TokenHourlyLimit: 5, ClientHourlyLimit: 20})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
_, err = confirmation.Confirm(context.Background(), ConfirmCommand{
|
|
Token: raw.Reveal(), Password: "correct horse battery staple", ClientKey: "client-key",
|
|
})
|
|
var limited *RateLimitedError
|
|
if !errors.As(err, &limited) || limited.RetryAfter != time.Minute {
|
|
t.Fatalf("Confirm() error = %v, want one-minute rate limit", err)
|
|
}
|
|
if len(limiter.keys) != 1 || limiter.keys[0] != keyring.ClientBucket("client-key") {
|
|
t.Fatalf("limiter keys = %v, want only the client bucket first", limiter.keys)
|
|
}
|
|
if tokens.calls != 0 || transactor.calls != 0 {
|
|
t.Fatalf("storage calls = lookup %d, transaction %d, want none", tokens.calls, transactor.calls)
|
|
}
|
|
}
|