feat(identity): store users, email tokens, and sessions

This commit is contained in:
shira committed 2026-10-06 00:15:22 +03:00
1 parent 15e2e50561
commit e7d0d92ef9
30 files changed
+4792 -32

No files matched your search

+46
View File
@@ -0,0 +1,46 @@
package domain
import (
"errors"
"testing"
"time"
"github.com/google/uuid"
)
func TestNewSessionSetsDeadlines(t *testing.T) {
input := NewSessionInput{
UserID: uuid.New(),
TokenHash: testTokenHash(t),
StartedAt: testRegisteredAt,
AbsoluteLifetime: 7 * 24 * time.Hour,
}
session, err := NewSession(input)
if err != nil {
t.Fatalf("NewSession() error = %v", err)
}
if session.ID.Version() != 7 || session.UserID != input.UserID || session.TokenHash != input.TokenHash ||
!session.CreatedAt.Equal(testRegisteredAt) || !session.LastSeenAt.Equal(testRegisteredAt) ||
!session.ExpiresAt.Equal(testRegisteredAt.Add(7*24*time.Hour)) || session.RevokedAt != nil {
t.Fatalf("NewSession() = %+v", session)
}
}
func TestNewSessionRejectsInvalidInput(t *testing.T) {
valid := NewSessionInput{UserID: uuid.New(), TokenHash: testTokenHash(t), StartedAt: testRegisteredAt, AbsoluteLifetime: time.Hour}
tests := map[string]func(*NewSessionInput){
"nil user": func(input *NewSessionInput) { input.UserID = uuid.Nil },
"zero hash": func(input *NewSessionInput) { input.TokenHash = SecretTokenHash{} },
"zero start": func(input *NewSessionInput) { input.StartedAt = time.Time{} },
"non-positive lifetime": func(input *NewSessionInput) { input.AbsoluteLifetime = -time.Second },
}
for name, mutate := range tests {
t.Run(name, func(t *testing.T) {
input := valid
mutate(&input)
if _, err := NewSession(input); !errors.Is(err, ErrInvalidSession) {
t.Fatalf("NewSession() error = %v, want ErrInvalidSession", err)
}
})
}
}