Files
mc-gateway/internal/adminsession/session_test.go
tursom f508ecc1b9
Some checks failed
Go / build (.exe, 386, windows, windows-386) (push) Has been cancelled
Go / build (.exe, amd64, windows, windows-amd64) (push) Has been cancelled
Go / build (.exe, arm64, windows, windows-arm64) (push) Has been cancelled
Go / build (386, freebsd, freebsd-386) (push) Has been cancelled
Go / build (386, linux, linux-386) (push) Has been cancelled
Go / build (386, netbsd, netbsd-386) (push) Has been cancelled
Go / build (386, openbsd, openbsd-386) (push) Has been cancelled
Go / build (386, plan9, plan9-386) (push) Has been cancelled
Go / build (amd64, darwin, darwin-amd64) (push) Has been cancelled
Go / build (amd64, dragonfly, dragonfly-amd64) (push) Has been cancelled
Go / build (amd64, freebsd, freebsd-amd64) (push) Has been cancelled
Go / build (amd64, illumos, illumos-amd64) (push) Has been cancelled
Go / build (amd64, linux, linux-amd64) (push) Has been cancelled
Go / build (amd64, netbsd, netbsd-amd64) (push) Has been cancelled
Go / build (amd64, openbsd, openbsd-amd64) (push) Has been cancelled
Go / build (amd64, plan9, plan9-amd64) (push) Has been cancelled
Go / build (amd64, solaris, solaris-amd64) (push) Has been cancelled
Go / build (arm, 6, linux, linux-armv6) (push) Has been cancelled
Go / build (arm, 7, linux, linux-armv7) (push) Has been cancelled
Go / build (arm, freebsd, freebsd-arm) (push) Has been cancelled
Go / build (arm, netbsd, netbsd-arm) (push) Has been cancelled
Go / build (arm, openbsd, openbsd-arm) (push) Has been cancelled
Go / build (arm, plan9, plan9-arm) (push) Has been cancelled
Go / build (arm64, darwin, darwin-arm64) (push) Has been cancelled
Go / build (arm64, freebsd, freebsd-arm64) (push) Has been cancelled
Go / build (arm64, linux, linux-arm64) (push) Has been cancelled
Go / build (arm64, netbsd, netbsd-arm64) (push) Has been cancelled
Go / build (arm64, openbsd, openbsd-arm64) (push) Has been cancelled
Go / build (loong64, linux, linux-loong64) (push) Has been cancelled
Go / build (mips, linux, linux-mips) (push) Has been cancelled
Go / build (mips64, linux, linux-mips64) (push) Has been cancelled
Go / build (mips64le, linux, linux-mips64le) (push) Has been cancelled
Go / build (mipsle, linux, linux-mipsle) (push) Has been cancelled
Go / build (ppc64, aix, aix-ppc64) (push) Has been cancelled
Go / build (ppc64, linux, linux-ppc64) (push) Has been cancelled
Go / build (ppc64, openbsd, openbsd-ppc64) (push) Has been cancelled
Go / build (ppc64le, linux, linux-ppc64le) (push) Has been cancelled
Go / build (riscv64, freebsd, freebsd-riscv64) (push) Has been cancelled
Go / build (riscv64, linux, linux-riscv64) (push) Has been cancelled
Go / build (riscv64, openbsd, openbsd-riscv64) (push) Has been cancelled
Go / build (s390x, linux, linux-s390x) (push) Has been cancelled
Docker Image / docker (push) Has been cancelled
Go / merge-artifacts (push) Has been cancelled
docs: 补充中文代码注释
2026-06-27 20:15:29 +08:00

89 lines
2.3 KiB
Go

// internal/adminsession/session_test.go 包含用于约束 session 行为的测试。
package adminsession
import (
"testing"
"time"
)
func TestManagerCreateAndGet(t *testing.T) {
now := time.Unix(100, 0)
manager := NewManagerWithClock(func() time.Time { return now })
session, err := manager.Create("admin", "admin", time.Hour)
if err != nil {
t.Fatalf("Create() error = %v", err)
}
if session.Token == "" {
t.Fatal("Create() token is empty")
}
if len(session.Token) != 43 {
t.Fatalf("Create() token len = %d, want 43", len(session.Token))
}
if !session.ExpiresAt.Equal(now.Add(time.Hour)) {
t.Fatalf("ExpiresAt = %v, want %v", session.ExpiresAt, now.Add(time.Hour))
}
got, ok := manager.Get(session.Token)
if !ok {
t.Fatal("Get() ok = false, want true")
}
if got.Username != "admin" || got.Role != "admin" {
t.Fatalf("Get() = %+v", got)
}
}
func TestManagerExpiresSessions(t *testing.T) {
now := time.Unix(100, 0)
manager := NewManagerWithClock(func() time.Time { return now })
session, err := manager.Create("admin", "admin", time.Hour)
if err != nil {
t.Fatalf("Create() error = %v", err)
}
now = now.Add(time.Hour + time.Second)
if _, ok := manager.Get(session.Token); ok {
t.Fatal("Get(expired) ok = true, want false")
}
if _, ok := manager.Get(session.Token); ok {
t.Fatal("Get(expired deleted) ok = true, want false")
}
}
func TestManagerDeleteAndRemoveUser(t *testing.T) {
manager := NewManager()
admin, err := manager.Create("admin", "admin", time.Hour)
if err != nil {
t.Fatalf("Create(admin) error = %v", err)
}
member, err := manager.Create("member", "member", time.Hour)
if err != nil {
t.Fatalf("Create(member) error = %v", err)
}
guest, err := manager.Create("guest", "guest", time.Hour)
if err != nil {
t.Fatalf("Create(guest) error = %v", err)
}
manager.Delete(guest.Token)
if _, ok := manager.Get(guest.Token); ok {
t.Fatal("guest token still exists after Delete")
}
manager.RemoveUser("admin")
if _, ok := manager.Get(admin.Token); ok {
t.Fatal("admin token still exists after RemoveUser")
}
if _, ok := manager.Get(member.Token); !ok {
t.Fatal("member token was removed by RemoveUser(admin)")
}
manager.Clear()
if _, ok := manager.Get(member.Token); ok {
t.Fatal("member token still exists after Clear")
}
}