1 parent
d92dfdacff
commit
f68c367042
34 files changed
+2121
-74
No files matched your search
@@ -3,6 +3,7 @@ package session
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.pcux.cn/xuans/Smail/models"
|
||||
"github.com/alexedwards/scs/v2"
|
||||
@@ -28,4 +29,30 @@ func TestUserIdentityRoundTrip(t *testing.T) {
|
||||
if identity.ID != user.ID || identity.Account != user.Account || identity.Name != user.Name || identity.IsAdmin != 1 || identity.SessionVersion != 4 || identity.SecurityVersion != 3 || identity.MustChangePassword != 1 {
|
||||
t.Fatalf("unexpected session identity: %+v", identity)
|
||||
}
|
||||
if !identity.HasRecentAuthentication(time.Now().UTC()) {
|
||||
t.Fatal("completed login did not record recent authentication")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRecentAuthenticationRejectsMissingFutureAndExpiredTimes(t *testing.T) {
|
||||
now := time.Date(2026, 9, 6, 0, 0, 0, 0, time.UTC)
|
||||
for _, test := range []struct {
|
||||
name string
|
||||
at int64
|
||||
want bool
|
||||
}{
|
||||
{"legacy session", 0, false},
|
||||
{"future time", now.Add(time.Second).Unix(), false},
|
||||
{"fresh login", now.Unix(), true},
|
||||
{"before expiry", now.Add(-recentAuthenticationLifetime + time.Second).Unix(), true},
|
||||
{"exact expiry", now.Add(-recentAuthenticationLifetime).Unix(), false},
|
||||
{"expired login", now.Add(-time.Hour).Unix(), false},
|
||||
} {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
identity := UserIdentity{ID: 1, AuthenticatedAt: test.at}
|
||||
if identity.HasRecentAuthentication(now) != test.want {
|
||||
t.Fatal("unexpected recent authentication state")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user