Files
kra-new/internal/biz/business/other_settings_test.go
T

58 lines
1.8 KiB
Go

package business
import (
"context"
"testing"
)
type otherSettingsFake struct {
value *OtherSettings
calls int
}
func (r *otherSettingsFake) Find(context.Context) (*OtherSettings, error) {
r.calls++
return r.value, nil
}
func (r *otherSettingsFake) Save(_ context.Context, value OtherSettings) (*OtherSettings, error) {
r.calls++
r.value = &value
return r.value, nil
}
func TestOtherSettingsOnlySuperAdminAndDefaults(t *testing.T) {
r := &otherSettingsFake{}
uc := NewOtherSettingsUsecase(r, nil)
for _, ctx := range []context.Context{context.Background(), accountContext(7, 901), accountContext(8, 902), accountContext(9, 903)} {
if _, err := uc.Get(ctx); err == nil {
t.Fatal("非超管不能读取设置")
}
if _, err := uc.Save(ctx, DefaultOtherSettings()); err == nil {
t.Fatal("非超管不能保存设置")
}
}
if r.calls != 0 {
t.Fatal("无权限请求不得访问存储")
}
value, err := uc.Get(accountContext(1, 888))
if err != nil || *value != DefaultOtherSettings() {
t.Fatalf("首次读取应返回默认值: %+v %v", value, err)
}
if _, err := uc.Save(accountContext(1, 888), OtherSettings{Amount: 0, Days: 1}); err != nil {
t.Fatal(err)
}
value, err = uc.Get(accountContext(2, 888))
if err != nil || value.Amount != 0 || value.Days != 1 {
t.Fatal("参数应为全站共享,不能按用户各存一份")
}
}
func TestOtherSettingsRejectInvalidRange(t *testing.T) {
uc := NewOtherSettingsUsecase(&otherSettingsFake{}, nil)
for _, value := range []OtherSettings{{Amount: -1, Days: 30}, {Amount: 3000, Days: 0}, {Amount: MaxSettingsInteger + 1, Days: 30}, {Amount: 3000, Days: MaxSettingsInteger + 1}} {
if _, err := uc.Save(accountContext(1, 888), value); err != ErrOtherSettingsInput {
t.Fatalf("不应保存非法值: %+v %v", value, err)
}
}
}