推进P1聊天通知与凭证应用端口迁移
This commit is contained in:
1 parent
25531c05cb
commit
550aee8aae
17 files changed
+1677
-289
No files matched your search
@@ -0,0 +1,330 @@
|
||||
package notifications
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
// ErrChannelNotFound 表示通知渠道或账号绑定目标不存在。
|
||||
ErrChannelNotFound = errors.New("通知渠道不存在")
|
||||
// ErrChannelForbidden 表示当前用户无权操作通知渠道或账号绑定。
|
||||
ErrChannelForbidden = errors.New("无权操作通知渠道")
|
||||
// ErrChannelInvalidInput 表示渠道字段、用户归属或绑定参数无效。
|
||||
ErrChannelInvalidInput = errors.New("通知渠道参数无效")
|
||||
// ErrAccountForbidden 表示账号不存在或不属于当前用户。
|
||||
ErrAccountForbidden = errors.New("无权操作账号通知绑定")
|
||||
// ErrNotifierUnavailable 表示测试发送依赖尚未装配。
|
||||
ErrNotifierUnavailable = errors.New("通知器未启用")
|
||||
)
|
||||
|
||||
// ChannelSummary 是通知渠道的非敏感展示模型;它刻意不包含配置 JSON。
|
||||
type ChannelSummary struct {
|
||||
// ID 是渠道稳定标识。
|
||||
ID int64
|
||||
// Name 是用户可识别的渠道名称。
|
||||
Name string
|
||||
// Type 是通知渠道协议类型。
|
||||
Type string
|
||||
// EventTypes 是渠道订阅的事件类型编码。
|
||||
EventTypes string
|
||||
// Enabled 表示渠道是否参与通知投递。
|
||||
Enabled bool
|
||||
// UserID 是渠道所属用户,仅供应用层归属校验和管理员展示。
|
||||
UserID int64
|
||||
}
|
||||
|
||||
// ChannelRecord 是通知渠道更新时由持久化端口返回的内部记录,Config 仅允许留在应用编排与存储边界。
|
||||
type ChannelRecord struct {
|
||||
// ID 是渠道稳定标识。
|
||||
ID int64
|
||||
// Name 是渠道名称。
|
||||
Name string
|
||||
// Type 是渠道协议类型。
|
||||
Type string
|
||||
// Config 是加密存储解密后的配置 JSON,禁止进入 HTTP 或应用响应。
|
||||
Config string
|
||||
// EventTypes 是渠道订阅事件类型编码。
|
||||
EventTypes string
|
||||
// Enabled 表示渠道是否参与通知投递。
|
||||
Enabled bool
|
||||
// UserID 是渠道所属用户。
|
||||
UserID int64
|
||||
}
|
||||
|
||||
// ChannelInput 是创建通知渠道时的业务输入;Config 只用于写入敏感配置。
|
||||
type ChannelInput struct {
|
||||
// Name 是渠道名称。
|
||||
Name string
|
||||
// Type 是渠道协议类型。
|
||||
Type string
|
||||
// Config 是待加密保存的渠道配置 JSON,禁止记录日志或返回响应。
|
||||
Config string
|
||||
// EventTypes 是渠道订阅事件类型编码。
|
||||
EventTypes string
|
||||
// Enabled 表示创建后是否启用渠道。
|
||||
Enabled bool
|
||||
}
|
||||
|
||||
// ChannelPatch 是通知渠道的部分更新输入;nil 字段表示保留现值。
|
||||
type ChannelPatch struct {
|
||||
// Name 是可选的新渠道名称。
|
||||
Name *string
|
||||
// Type 是可选的新渠道协议类型。
|
||||
Type *string
|
||||
// Config 是可选的新敏感配置 JSON,禁止记录日志或返回响应。
|
||||
Config *string
|
||||
// EventTypes 是可选的新订阅事件类型编码。
|
||||
EventTypes *string
|
||||
// Enabled 是可选的新启用状态。
|
||||
Enabled *bool
|
||||
}
|
||||
|
||||
// BindingSummary 是账号与通知渠道绑定的非敏感展示模型。
|
||||
type BindingSummary struct {
|
||||
// ID 是绑定记录稳定标识。
|
||||
ID int64
|
||||
// CookieID 是绑定账号标识。
|
||||
CookieID string
|
||||
// ChannelID 是绑定渠道标识。
|
||||
ChannelID int64
|
||||
// ChannelName 是绑定渠道名称。
|
||||
ChannelName string
|
||||
// Enabled 表示绑定是否启用。
|
||||
Enabled bool
|
||||
}
|
||||
|
||||
// ChannelRepository 定义通知渠道和账号绑定用例所需的最小持久化端口。
|
||||
type ChannelRepository interface {
|
||||
// ListChannels 查询指定用户的非敏感渠道摘要。
|
||||
ListChannels(ctx context.Context, userID int64) ([]ChannelSummary, error)
|
||||
// GetChannelForUpdate 查询指定用户拥有的渠道完整记录,仅供更新合并使用。
|
||||
GetChannelForUpdate(ctx context.Context, channelID, userID int64) (*ChannelRecord, error)
|
||||
// CreateChannel 保存一条渠道并返回稳定标识。
|
||||
CreateChannel(ctx context.Context, userID int64, input ChannelInput) (int64, error)
|
||||
// UpdateChannel 保存已通过归属校验的完整渠道记录。
|
||||
UpdateChannel(ctx context.Context, userID int64, record ChannelRecord) error
|
||||
// DeleteChannel 删除指定用户拥有的渠道。
|
||||
DeleteChannel(ctx context.Context, channelID, userID int64) error
|
||||
// OwnsChannel 判断渠道是否属于指定用户。
|
||||
OwnsChannel(ctx context.Context, channelID, userID int64) (bool, error)
|
||||
// OwnsAccount 判断账号是否属于指定用户,不读取账号凭证。
|
||||
OwnsAccount(ctx context.Context, userID int64, cookieID string) (bool, error)
|
||||
// ListBindings 查询指定用户的非敏感绑定摘要。
|
||||
ListBindings(ctx context.Context, userID int64) ([]BindingSummary, error)
|
||||
// GetBindingIDs 查询账号当前启用的渠道标识。
|
||||
GetBindingIDs(ctx context.Context, cookieID string) ([]int64, error)
|
||||
// SetBindings 覆盖保存账号的渠道绑定。
|
||||
SetBindings(ctx context.Context, cookieID string, channelIDs []int64) error
|
||||
// SetSingleBinding 更新单个账号渠道绑定状态。
|
||||
SetSingleBinding(ctx context.Context, cookieID string, channelID int64, enabled bool) error
|
||||
// DeleteBinding 删除指定用户的一条绑定。
|
||||
DeleteBinding(ctx context.Context, userID, bindingID int64) error
|
||||
// DeleteAccountBindings 删除指定用户账号的全部绑定。
|
||||
DeleteAccountBindings(ctx context.Context, userID int64, cookieID string) error
|
||||
}
|
||||
|
||||
// ChannelSender 定义测试通知发送能力,避免应用层依赖具体通知器。
|
||||
type ChannelSender interface {
|
||||
// SendToChannel 向指定渠道发送测试正文。
|
||||
SendToChannel(channelID int64, body string) error
|
||||
}
|
||||
|
||||
// ChannelService 编排通知渠道 CRUD、账号绑定和测试发送,不依赖 HTTP 或数据库模型。
|
||||
type ChannelService struct {
|
||||
// repository 保存渠道与绑定的最小持久化端口。
|
||||
repository ChannelRepository
|
||||
// sender 保存测试通知发送端口;普通 CRUD 不依赖该端口。
|
||||
sender ChannelSender
|
||||
}
|
||||
|
||||
// NewChannelService 构造通知渠道应用服务。
|
||||
func NewChannelService(repository ChannelRepository, sender ChannelSender) *ChannelService {
|
||||
return &ChannelService{repository: repository, sender: sender}
|
||||
}
|
||||
|
||||
// ListChannels 查询用户拥有的非敏感通知渠道摘要。
|
||||
func (s *ChannelService) ListChannels(ctx context.Context, userID int64) ([]ChannelSummary, error) {
|
||||
if s == nil || s.repository == nil || userID <= 0 {
|
||||
return nil, ErrChannelInvalidInput
|
||||
}
|
||||
return s.repository.ListChannels(ctx, userID)
|
||||
}
|
||||
|
||||
// CreateChannel 校验并创建通知渠道;敏感配置只向持久化端口传递。
|
||||
func (s *ChannelService) CreateChannel(ctx context.Context, userID int64, input ChannelInput) (int64, error) {
|
||||
if s == nil || s.repository == nil || userID <= 0 {
|
||||
return 0, ErrChannelInvalidInput
|
||||
}
|
||||
// validationErr 表示创建输入的名称和协议类型校验结果。
|
||||
if validationErr := validateChannelFields(input.Name, input.Type); validationErr != nil {
|
||||
return 0, validationErr
|
||||
}
|
||||
return s.repository.CreateChannel(ctx, userID, input)
|
||||
}
|
||||
|
||||
// UpdateChannel 合并部分更新、校验最终字段并保存用户拥有的渠道。
|
||||
func (s *ChannelService) UpdateChannel(ctx context.Context, userID, channelID int64, patch ChannelPatch) error {
|
||||
if s == nil || s.repository == nil || userID <= 0 || channelID <= 0 {
|
||||
return ErrChannelInvalidInput
|
||||
}
|
||||
// record 保存已通过归属查询的渠道记录;lookupErr 保存读取失败原因。
|
||||
record, lookupErr := s.repository.GetChannelForUpdate(ctx, channelID, userID)
|
||||
if lookupErr != nil {
|
||||
return lookupErr
|
||||
}
|
||||
if record == nil {
|
||||
return ErrChannelForbidden
|
||||
}
|
||||
if patch.Name != nil {
|
||||
record.Name = *patch.Name
|
||||
}
|
||||
if patch.Type != nil {
|
||||
record.Type = *patch.Type
|
||||
}
|
||||
if patch.Config != nil {
|
||||
record.Config = *patch.Config
|
||||
}
|
||||
if patch.EventTypes != nil {
|
||||
record.EventTypes = *patch.EventTypes
|
||||
}
|
||||
if patch.Enabled != nil {
|
||||
record.Enabled = *patch.Enabled
|
||||
}
|
||||
// validationErr 表示合并后的渠道名称和协议类型校验结果。
|
||||
if validationErr := validateChannelFields(record.Name, record.Type); validationErr != nil {
|
||||
return validationErr
|
||||
}
|
||||
record.UserID = userID
|
||||
return s.repository.UpdateChannel(ctx, userID, *record)
|
||||
}
|
||||
|
||||
// DeleteChannel 删除用户拥有的通知渠道。
|
||||
func (s *ChannelService) DeleteChannel(ctx context.Context, userID, channelID int64) error {
|
||||
if s == nil || s.repository == nil || userID <= 0 || channelID <= 0 {
|
||||
return ErrChannelInvalidInput
|
||||
}
|
||||
return s.repository.DeleteChannel(ctx, channelID, userID)
|
||||
}
|
||||
|
||||
// TestChannel 校验渠道归属并发送固定格式测试通知;body 不包含渠道配置或凭证。
|
||||
func (s *ChannelService) TestChannel(ctx context.Context, userID, channelID int64, now time.Time) error {
|
||||
if s == nil || s.repository == nil || userID <= 0 || channelID <= 0 {
|
||||
return ErrChannelInvalidInput
|
||||
}
|
||||
if s.sender == nil {
|
||||
return ErrNotifierUnavailable
|
||||
}
|
||||
// owned 表示渠道归属结果;ownershipErr 保存归属查询失败原因。
|
||||
owned, ownershipErr := s.repository.OwnsChannel(ctx, channelID, userID)
|
||||
if ownershipErr != nil {
|
||||
return ownershipErr
|
||||
}
|
||||
if !owned {
|
||||
return ErrChannelForbidden
|
||||
}
|
||||
if now.IsZero() {
|
||||
now = time.Now()
|
||||
}
|
||||
// body 是固定测试通知正文,不包含渠道敏感配置。
|
||||
body := fmt.Sprintf("🧪 通知渠道测试\n\n这是一条来自Ydisks闲鱼助手的测试通知,收到说明渠道配置正常。\n时间: %s", now.Format("2006-01-02 15:04:05"))
|
||||
return s.sender.SendToChannel(channelID, body)
|
||||
}
|
||||
|
||||
// ListBindings 查询用户所有账号的非敏感通知绑定。
|
||||
func (s *ChannelService) ListBindings(ctx context.Context, userID int64) ([]BindingSummary, error) {
|
||||
if s == nil || s.repository == nil || userID <= 0 {
|
||||
return nil, ErrChannelInvalidInput
|
||||
}
|
||||
return s.repository.ListBindings(ctx, userID)
|
||||
}
|
||||
|
||||
// GetBindingIDs 校验账号归属后查询其启用渠道 ID。
|
||||
func (s *ChannelService) GetBindingIDs(ctx context.Context, userID int64, cookieID string) ([]int64, error) {
|
||||
// validationErr 表示账号归属校验结果。
|
||||
if validationErr := s.validateAccount(ctx, userID, cookieID); validationErr != nil {
|
||||
return nil, validationErr
|
||||
}
|
||||
return s.repository.GetBindingIDs(ctx, cookieID)
|
||||
}
|
||||
|
||||
// SetBindings 校验账号和全部渠道归属后覆盖保存绑定。
|
||||
func (s *ChannelService) SetBindings(ctx context.Context, userID int64, cookieID string, channelIDs []int64) error {
|
||||
// validationErr 表示账号归属校验结果。
|
||||
if validationErr := s.validateAccount(ctx, userID, cookieID); validationErr != nil {
|
||||
return validationErr
|
||||
}
|
||||
// channelID 表示当前待校验归属的渠道。
|
||||
for _, channelID := range channelIDs {
|
||||
// owned 表示渠道归属结果;ownershipErr 保存归属查询失败原因。
|
||||
owned, ownershipErr := s.repository.OwnsChannel(ctx, channelID, userID)
|
||||
if ownershipErr != nil {
|
||||
return ownershipErr
|
||||
}
|
||||
if !owned {
|
||||
return ErrChannelForbidden
|
||||
}
|
||||
}
|
||||
return s.repository.SetBindings(ctx, cookieID, channelIDs)
|
||||
}
|
||||
|
||||
// SetSingleBinding 校验账号和渠道归属后更新单条绑定状态。
|
||||
func (s *ChannelService) SetSingleBinding(ctx context.Context, userID int64, cookieID string, channelID int64, enabled bool) error {
|
||||
// validationErr 表示账号归属校验结果。
|
||||
if validationErr := s.validateAccount(ctx, userID, cookieID); validationErr != nil {
|
||||
return validationErr
|
||||
}
|
||||
// owned 表示渠道归属结果;ownershipErr 保存归属查询失败原因。
|
||||
owned, ownershipErr := s.repository.OwnsChannel(ctx, channelID, userID)
|
||||
if ownershipErr != nil {
|
||||
return ownershipErr
|
||||
}
|
||||
if !owned {
|
||||
return ErrChannelForbidden
|
||||
}
|
||||
return s.repository.SetSingleBinding(ctx, cookieID, channelID, enabled)
|
||||
}
|
||||
|
||||
// DeleteBinding 删除用户的一条绑定,归属由持久化端口再次约束。
|
||||
func (s *ChannelService) DeleteBinding(ctx context.Context, userID, bindingID int64) error {
|
||||
if s == nil || s.repository == nil || userID <= 0 || bindingID <= 0 {
|
||||
return ErrChannelInvalidInput
|
||||
}
|
||||
return s.repository.DeleteBinding(ctx, userID, bindingID)
|
||||
}
|
||||
|
||||
// DeleteAccountBindings 校验账号归属后删除其全部绑定。
|
||||
func (s *ChannelService) DeleteAccountBindings(ctx context.Context, userID int64, cookieID string) error {
|
||||
// validationErr 表示账号归属校验结果。
|
||||
if validationErr := s.validateAccount(ctx, userID, cookieID); validationErr != nil {
|
||||
return validationErr
|
||||
}
|
||||
return s.repository.DeleteAccountBindings(ctx, userID, cookieID)
|
||||
}
|
||||
|
||||
// validateAccount 校验用户和账号标识,并确保端口返回的结果属于当前用户。
|
||||
func (s *ChannelService) validateAccount(ctx context.Context, userID int64, cookieID string) error {
|
||||
if s == nil || s.repository == nil || userID <= 0 || strings.TrimSpace(cookieID) == "" {
|
||||
return ErrChannelInvalidInput
|
||||
}
|
||||
// owned 表示账号归属结果;ownershipErr 保存归属查询失败原因。
|
||||
owned, ownershipErr := s.repository.OwnsAccount(ctx, userID, cookieID)
|
||||
if ownershipErr != nil {
|
||||
return ownershipErr
|
||||
}
|
||||
if !owned {
|
||||
return ErrAccountForbidden
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// validateChannelFields 校验业务必填字段,避免将无名或无类型渠道写入存储。
|
||||
func validateChannelFields(name, channelType string) error {
|
||||
if strings.TrimSpace(name) == "" || strings.TrimSpace(channelType) == "" {
|
||||
return ErrChannelInvalidInput
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,183 @@
|
||||
package notifications
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// channelRepositoryStub 是通知渠道应用服务测试使用的可控端口替身。
|
||||
type channelRepositoryStub struct {
|
||||
// summaries 保存列表查询返回的非敏感渠道摘要。
|
||||
summaries []ChannelSummary
|
||||
// record 保存更新查询返回的渠道记录。
|
||||
record *ChannelRecord
|
||||
// bindings 保存绑定列表查询结果。
|
||||
bindings []BindingSummary
|
||||
// bindingIDs 保存账号启用的渠道 ID。
|
||||
bindingIDs []int64
|
||||
// ownedChannel 表示渠道归属校验结果。
|
||||
ownedChannel bool
|
||||
// ownedAccount 表示账号归属校验结果。
|
||||
ownedAccount bool
|
||||
// err 保存端口操作失败原因。
|
||||
err error
|
||||
// createdInput 保存最近一次创建输入,便于验证敏感值只进入端口。
|
||||
createdInput ChannelInput
|
||||
// updatedRecord 保存最近一次更新记录,便于验证部分更新合并。
|
||||
updatedRecord *ChannelRecord
|
||||
// sentBindings 保存最近一次覆盖绑定请求。
|
||||
sentBindings []int64
|
||||
}
|
||||
|
||||
// ListChannels 返回预置渠道摘要。
|
||||
func (r *channelRepositoryStub) ListChannels(context.Context, int64) ([]ChannelSummary, error) {
|
||||
return r.summaries, r.err
|
||||
}
|
||||
|
||||
// GetChannelForUpdate 返回预置渠道完整记录。
|
||||
func (r *channelRepositoryStub) GetChannelForUpdate(context.Context, int64, int64) (*ChannelRecord, error) {
|
||||
return r.record, r.err
|
||||
}
|
||||
|
||||
// CreateChannel 记录创建输入并返回固定渠道 ID。
|
||||
func (r *channelRepositoryStub) CreateChannel(_ context.Context, _ int64, input ChannelInput) (int64, error) {
|
||||
r.createdInput = input
|
||||
if r.err != nil {
|
||||
return 0, r.err
|
||||
}
|
||||
return 41, nil
|
||||
}
|
||||
|
||||
// UpdateChannel 记录更新后的完整渠道记录。
|
||||
func (r *channelRepositoryStub) UpdateChannel(_ context.Context, _ int64, record ChannelRecord) error {
|
||||
r.updatedRecord = &record
|
||||
return r.err
|
||||
}
|
||||
|
||||
// DeleteChannel 返回预置端口错误。
|
||||
func (r *channelRepositoryStub) DeleteChannel(context.Context, int64, int64) error { return r.err }
|
||||
|
||||
// OwnsChannel 返回预置渠道归属结果。
|
||||
func (r *channelRepositoryStub) OwnsChannel(context.Context, int64, int64) (bool, error) {
|
||||
return r.ownedChannel, r.err
|
||||
}
|
||||
|
||||
// OwnsAccount 返回预置账号归属结果。
|
||||
func (r *channelRepositoryStub) OwnsAccount(context.Context, int64, string) (bool, error) {
|
||||
return r.ownedAccount, r.err
|
||||
}
|
||||
|
||||
// ListBindings 返回预置绑定摘要。
|
||||
func (r *channelRepositoryStub) ListBindings(context.Context, int64) ([]BindingSummary, error) {
|
||||
return r.bindings, r.err
|
||||
}
|
||||
|
||||
// GetBindingIDs 返回预置账号绑定 ID。
|
||||
func (r *channelRepositoryStub) GetBindingIDs(context.Context, string) ([]int64, error) {
|
||||
return r.bindingIDs, r.err
|
||||
}
|
||||
|
||||
// SetBindings 记录覆盖绑定请求。
|
||||
func (r *channelRepositoryStub) SetBindings(_ context.Context, _ string, channelIDs []int64) error {
|
||||
r.sentBindings = append([]int64(nil), channelIDs...)
|
||||
return r.err
|
||||
}
|
||||
|
||||
// SetSingleBinding 返回预置端口错误。
|
||||
func (r *channelRepositoryStub) SetSingleBinding(context.Context, string, int64, bool) error {
|
||||
return r.err
|
||||
}
|
||||
|
||||
// DeleteBinding 返回预置端口错误。
|
||||
func (r *channelRepositoryStub) DeleteBinding(context.Context, int64, int64) error { return r.err }
|
||||
|
||||
// DeleteAccountBindings 返回预置端口错误。
|
||||
func (r *channelRepositoryStub) DeleteAccountBindings(context.Context, int64, string) error {
|
||||
return r.err
|
||||
}
|
||||
|
||||
// channelSenderStub 是测试发送端口的替身。
|
||||
type channelSenderStub struct {
|
||||
// body 保存最近一次发送的正文。
|
||||
body string
|
||||
// err 保存发送失败原因。
|
||||
err error
|
||||
}
|
||||
|
||||
// SendToChannel 记录发送正文并返回预置错误。
|
||||
func (s *channelSenderStub) SendToChannel(_ int64, body string) error {
|
||||
s.body = body
|
||||
return s.err
|
||||
}
|
||||
|
||||
// TestChannelServiceCreatesAndUpdatesWithoutReturningConfig 验证渠道创建更新成功且配置不进入展示模型。
|
||||
func TestChannelServiceCreatesAndUpdatesWithoutReturningConfig(t *testing.T) {
|
||||
// repository 是保存敏感配置但不把它放入摘要的端口替身。
|
||||
repository := &channelRepositoryStub{summaries: []ChannelSummary{{ID: 1, Name: "邮件", Type: "email"}}, record: &ChannelRecord{ID: 1, Name: "旧名", Type: "email", Config: `{"password":"secret"}`, Enabled: true}}
|
||||
// service 是待验证的通知渠道应用服务。
|
||||
service := NewChannelService(repository, nil)
|
||||
// channelID、createErr 保存创建结果。
|
||||
channelID, createErr := service.CreateChannel(context.Background(), 7, ChannelInput{Name: "邮件", Type: "email", Config: `{"password":"secret"}`})
|
||||
if createErr != nil || channelID != 41 || repository.createdInput.Config == "" {
|
||||
t.Fatalf("创建结果异常: id=%d err=%v", channelID, createErr)
|
||||
}
|
||||
// name 保存部分更新后的渠道名称。
|
||||
name := "新名"
|
||||
// updateErr 保存更新结果。
|
||||
updateErr := service.UpdateChannel(context.Background(), 7, 1, ChannelPatch{Name: &name})
|
||||
if updateErr != nil || repository.updatedRecord == nil || repository.updatedRecord.Name != "新名" || repository.updatedRecord.Config != `{"password":"secret"}` {
|
||||
t.Fatalf("更新结果异常: record=%+v err=%v", repository.updatedRecord, updateErr)
|
||||
}
|
||||
// summaries、listErr 保存非敏感渠道列表结果。
|
||||
summaries, listErr := service.ListChannels(context.Background(), 7)
|
||||
if listErr != nil || len(summaries) != 1 || summaries[0].Name != "邮件" {
|
||||
t.Fatalf("列表结果异常: summaries=%+v err=%v", summaries, listErr)
|
||||
}
|
||||
}
|
||||
|
||||
// TestChannelServiceRejectsOwnershipAndStorageFailures 验证归属失败和存储错误不会继续写入。
|
||||
func TestChannelServiceRejectsOwnershipAndStorageFailures(t *testing.T) {
|
||||
// forbiddenRepository 是拒绝渠道归属的端口替身。
|
||||
forbiddenRepository := &channelRepositoryStub{ownedChannel: false, ownedAccount: false}
|
||||
// forbiddenService 是使用拒绝归属端口的应用服务。
|
||||
forbiddenService := NewChannelService(forbiddenRepository, &channelSenderStub{})
|
||||
// forbiddenErr 保存测试发送的归属错误。
|
||||
forbiddenErr := forbiddenService.TestChannel(context.Background(), 7, 1, time.Unix(0, 0))
|
||||
if !errors.Is(forbiddenErr, ErrChannelForbidden) {
|
||||
t.Fatalf("渠道归属失败错误异常: %v", forbiddenErr)
|
||||
}
|
||||
// bindingErr 保存账号绑定的归属错误。
|
||||
bindingErr := forbiddenService.SetBindings(context.Background(), 7, "acc", []int64{1})
|
||||
if !errors.Is(bindingErr, ErrAccountForbidden) {
|
||||
t.Fatalf("账号归属失败错误异常: %v", bindingErr)
|
||||
}
|
||||
// storageError 是持久化端口失败原因。
|
||||
storageError := errors.New("storage failed")
|
||||
// storageRepository 是返回存储错误的端口替身。
|
||||
storageRepository := &channelRepositoryStub{ownedChannel: true, ownedAccount: true, err: storageError, record: &ChannelRecord{Name: "x", Type: "webhook"}}
|
||||
// storageService 是使用存储错误端口的应用服务。
|
||||
storageService := NewChannelService(storageRepository, &channelSenderStub{})
|
||||
// updateErr 保存存储错误传播结果。
|
||||
updateErr := storageService.UpdateChannel(context.Background(), 7, 1, ChannelPatch{})
|
||||
if !errors.Is(updateErr, storageError) {
|
||||
t.Fatalf("存储错误未透传: %v", updateErr)
|
||||
}
|
||||
}
|
||||
|
||||
// TestChannelServiceTestSendUsesSafeBody 验证测试发送只生成固定正文,不携带渠道配置。
|
||||
func TestChannelServiceTestSendUsesSafeBody(t *testing.T) {
|
||||
// repository 是允许渠道归属且不返回敏感配置的端口替身。
|
||||
repository := &channelRepositoryStub{ownedChannel: true}
|
||||
// sender 是记录测试正文的通知发送替身。
|
||||
sender := &channelSenderStub{}
|
||||
// service 是待验证的测试发送应用服务。
|
||||
service := NewChannelService(repository, sender)
|
||||
// sendErr 保存测试发送结果。
|
||||
sendErr := service.TestChannel(context.Background(), 7, 1, time.Unix(0, 0).UTC())
|
||||
if sendErr != nil || !strings.Contains(sender.body, "通知渠道测试") || strings.Contains(sender.body, "password") {
|
||||
t.Fatalf("测试正文异常: body=%q err=%v", sender.body, sendErr)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user