47 lines
1.6 KiB
Go
47 lines
1.6 KiB
Go
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)
|
|
}
|
|
})
|
|
}
|
|
}
|