44 lines
1.1 KiB
Go
44 lines
1.1 KiB
Go
package business
|
|
|
|
import (
|
|
"context"
|
|
"kra/internal/biz/system"
|
|
"testing"
|
|
)
|
|
|
|
type preferenceRepoFake struct {
|
|
value *UserPreference
|
|
userID uint
|
|
calls int
|
|
}
|
|
|
|
func (r *preferenceRepoFake) Find(_ context.Context, id uint) (*UserPreference, error) {
|
|
r.calls++
|
|
r.userID = id
|
|
return r.value, nil
|
|
}
|
|
func (r *preferenceRepoFake) Save(_ context.Context, id uint, pref UserPreference) error {
|
|
r.calls++
|
|
r.userID, r.value = id, &pref
|
|
return nil
|
|
}
|
|
|
|
func TestPreferenceDefaultsAndCurrentIdentity(t *testing.T) {
|
|
r := &preferenceRepoFake{}
|
|
uc := NewUserPreferenceUsecase(r)
|
|
if _, err := uc.Current(context.Background()); err != ErrUnauthenticated || r.calls != 0 {
|
|
t.Fatalf("匿名请求访问了偏好: %v", err)
|
|
}
|
|
ctx := system.NewActorContext(context.Background(), system.Actor{UserID: 7})
|
|
pref, err := uc.Current(ctx)
|
|
if err != nil || !pref.IncludeSubPromote || !pref.IDCardDedup {
|
|
t.Fatalf("缺省开关应均为开启: %+v %v", pref, err)
|
|
}
|
|
if err = uc.UpdateCurrent(ctx, UserPreference{}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if r.userID != 7 || r.value.IncludeSubPromote || r.value.IDCardDedup {
|
|
t.Fatalf("未保存当前用户的关闭值: %+v", r)
|
|
}
|
|
}
|