实现三种数据库迁移并完善认证与迁移写入保护
Security Checks / security (push) Waiting to run

This commit is contained in:
xuans committed 2026-09-06 01:31:29 +08:00
1 parent d92dfdacff
commit f68c367042
34 files changed
+2121 -74

No files matched your search

+27
View File
@@ -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")
}
})
}
}