推进P1登录管理器与通知不确定态收口

This commit is contained in:
Christ committed 2026-08-15 23:33:14 +08:00
1 parent 417723d2c0
commit 0a756c408e
17 files changed
+589 -31

No files matched your search

@@ -0,0 +1,79 @@
package account
import (
"context"
"errors"
"strings"
)
// CreateCookieInput 是手动录入账号 Cookie 用例的非敏感输入;明文 Cookie 由调用方专用端口封装。
type CreateCookieInput struct {
// AccountID 是待创建的平台账号稳定标识。
AccountID string
// UserID 是当前用户的本地身份标识。
UserID int64
// LoginMethod 是触发本次登录的方式;空值按手动 Cookie 录入处理。
LoginMethod string
}
// CookieWriter 定义一次性写入账号凭证的端口;实现方负责在最小作用域内持有明文 Cookie。
type CookieWriter interface {
// CreateOwnedCookie 原子校验账号归属并写入凭证;明文不应进入应用层 DTO 或日志。
CreateOwnedCookie(context.Context, string, int64) error
}
// LoginLifecyclePort 定义凭证成功写入后的审计、启用、资料刷新和运行时同步边界。
type LoginLifecyclePort interface {
// AfterSuccessfulLogin 在凭证写入并释放凭证锁后完成后续登录编排。
AfterSuccessfulLogin(context.Context, int64, string, string)
}
// LoginService 编排手动 Cookie 登录用例,不保存任何敏感凭证状态。
type LoginService struct {
// lifecycle 负责凭证写入后的审计、资料和运行时适配。
lifecycle LoginLifecyclePort
}
// NewLoginService 构造账号登录应用服务并校验必需的生命周期端口。
func NewLoginService(lifecycle LoginLifecyclePort) (*LoginService, error) {
if lifecycle == nil {
return nil, errors.New("账号登录生命周期端口未初始化")
}
return &LoginService{lifecycle: lifecycle}, nil
}
// CreateCookie 执行一次手动 Cookie 登录;凭证仅通过调用方提供的专用写入端口传递。
func (s *LoginService) CreateCookie(ctx context.Context, input CreateCookieInput, writer CookieWriter) error {
if s == nil || s.lifecycle == nil {
return errors.New("账号登录服务未初始化")
}
if writer == nil {
return errors.New("账号 Cookie 写入端口未初始化")
}
if strings.TrimSpace(input.AccountID) == "" {
return errors.New("缺少账号 ID")
}
// writeErr 保存基础设施端口写入凭证时返回的错误。
writeErr := writer.CreateOwnedCookie(ctx, input.AccountID, input.UserID)
if writeErr != nil {
return writeErr
}
// method 是归一化后的登录方式,供审计和运行时适配器保持统一语义。
method := normalizeLoginMethod(input.LoginMethod)
s.lifecycle.AfterSuccessfulLogin(ctx, input.UserID, input.AccountID, method)
return nil
}
// normalizeLoginMethod 将兼容的登录方式别名归一化为审计使用的稳定值。
func normalizeLoginMethod(method string) string {
switch strings.ToLower(strings.TrimSpace(method)) {
case "", "manual", "cookie", "manual_cookie":
return "manual"
case "password", "password_login":
return "password"
case "qr", "qr_login", "qr_scan":
return "qr_scan"
default:
return strings.ToLower(strings.TrimSpace(method))
}
}
@@ -0,0 +1,134 @@
package account
import (
"context"
"errors"
"testing"
)
// fakeCookieWriter 是登录应用服务测试用的凭证写入端口替身。
type fakeCookieWriter struct {
// err 是写入凭证时需要模拟的失败。
err error
// accountID 和 userID 保存最近一次写入请求,验证敏感值未进入应用输入。
accountID string
userID int64
called bool
}
// CreateOwnedCookie 记录写入请求但不接收明文 Cookie。
func (w *fakeCookieWriter) CreateOwnedCookie(_ context.Context, accountID string, userID int64) error {
w.called = true
w.accountID = accountID
w.userID = userID
return w.err
}
// fakeLoginLifecycle 是登录后续编排测试用的生命周期端口替身。
type fakeLoginLifecycle struct {
// calls 保存登录后续编排的调用次数。
calls int
// userID、accountID 和 method 保存最近一次登录后续输入。
userID int64
accountID string
method string
}
// AfterSuccessfulLogin 记录登录成功后的编排输入。
func (l *fakeLoginLifecycle) AfterSuccessfulLogin(_ context.Context, userID int64, accountID, method string) {
l.calls++
l.userID = userID
l.accountID = accountID
l.method = method
}
// TestLoginServiceRequiresLifecycleAndWriter 验证必需端口缺失时快速失败。
func TestLoginServiceRequiresLifecycleAndWriter(t *testing.T) {
// lifecycle 是有效的生命周期端口替身。
lifecycle := &fakeLoginLifecycle{}
// missingLifecycleErr 保存缺少生命周期端口时的构造错误。
_, missingLifecycleErr := NewLoginService(nil)
if missingLifecycleErr == nil {
t.Fatal("缺少生命周期端口时应构造失败")
}
// service 保存使用有效生命周期端口构造的登录服务;serviceErr 保存构造错误。
service, serviceErr := NewLoginService(lifecycle)
if serviceErr != nil {
t.Fatalf("构造登录服务失败: %v", serviceErr)
}
// missingWriterErr 保存缺少凭证写入端口时的执行错误。
missingWriterErr := service.CreateCookie(context.Background(), CreateCookieInput{AccountID: "acc1"}, nil)
if missingWriterErr == nil {
t.Fatal("缺少 Cookie 写入端口时应失败")
}
}
// TestLoginServiceCreateCookieKeepsCredentialOutOfInput 验证成功登录只通过凭证端口写入并触发后续编排。
func TestLoginServiceCreateCookieKeepsCredentialOutOfInput(t *testing.T) {
// lifecycle 是记录后续审计和运行时编排的测试替身。
lifecycle := &fakeLoginLifecycle{}
// writer 是不暴露 Cookie 参数的凭证写入测试替身。
writer := &fakeCookieWriter{}
// service 保存使用有效生命周期端口构造的登录服务;serviceErr 保存构造错误。
service, serviceErr := NewLoginService(lifecycle)
if serviceErr != nil {
t.Fatalf("构造登录服务失败: %v", serviceErr)
}
// createErr 保存成功用例执行时的错误。
createErr := service.CreateCookie(context.Background(), CreateCookieInput{AccountID: "acc1", UserID: 7, LoginMethod: "cookie"}, writer)
if createErr != nil {
t.Fatalf("创建 Cookie 登录失败: %v", createErr)
}
if !writer.called || writer.accountID != "acc1" || writer.userID != 7 {
t.Fatalf("凭证写入端口输入异常: %+v", writer)
}
if lifecycle.calls != 1 || lifecycle.accountID != "acc1" || lifecycle.userID != 7 || lifecycle.method != "manual" {
t.Fatalf("登录后续编排输入异常: %+v", lifecycle)
}
}
// TestLoginServiceCreateCookieStopsAfterWriterFailure 验证凭证写入失败时不会执行成功后续编排。
func TestLoginServiceCreateCookieStopsAfterWriterFailure(t *testing.T) {
// expectedErr 是凭证写入端口需要返回的故障。
expectedErr := errors.New("凭证写入失败")
// lifecycle 是用于确认未被调用的后续编排替身。
lifecycle := &fakeLoginLifecycle{}
// writer 是返回预期错误的凭证写入替身。
writer := &fakeCookieWriter{err: expectedErr}
// service 保存使用有效生命周期端口构造的登录服务;serviceErr 保存构造错误。
service, serviceErr := NewLoginService(lifecycle)
if serviceErr != nil {
t.Fatalf("构造登录服务失败: %v", serviceErr)
}
// callErr 保存凭证写入失败后由应用服务传出的错误。
callErr := service.CreateCookie(context.Background(), CreateCookieInput{AccountID: "acc1"}, writer)
if !errors.Is(callErr, expectedErr) {
t.Fatalf("应保留凭证写入错误,got %v", callErr)
}
if lifecycle.calls != 0 {
t.Fatal("凭证写入失败时不应触发登录成功后续编排")
}
}
// TestLoginServiceCreateCookiePropagatesOwnershipFailure 验证凭证端口拒绝跨用户账号时不会伪造登录成功。
func TestLoginServiceCreateCookiePropagatesOwnershipFailure(t *testing.T) {
// ownershipErr 是持久化端口完成账号归属校验后返回的越权错误。
ownershipErr := errors.New("账号不属于当前用户")
// lifecycle 是用于确认越权请求未进入成功后续编排的测试替身。
lifecycle := &fakeLoginLifecycle{}
// writer 是模拟归属校验失败的凭证写入端口。
writer := &fakeCookieWriter{err: ownershipErr}
// service 保存使用有效生命周期端口构造的登录服务;serviceErr 保存构造错误。
service, serviceErr := NewLoginService(lifecycle)
if serviceErr != nil {
t.Fatalf("构造登录服务失败: %v", serviceErr)
}
// callErr 保存归属校验失败后由应用服务传出的错误。
callErr := service.CreateCookie(context.Background(), CreateCookieInput{AccountID: "other", UserID: 7}, writer)
if !errors.Is(callErr, ownershipErr) {
t.Fatalf("应保留归属校验错误,got %v", callErr)
}
if lifecycle.calls != 0 {
t.Fatal("归属校验失败时不应触发登录成功后续编排")
}
}