feat: add admin settings control flow
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
package settings
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
type memoryRepo struct {
|
||||
admins map[string]bool
|
||||
prefs map[string]string
|
||||
bindings map[string]ResourceBinding
|
||||
candidate BootstrapCandidate
|
||||
}
|
||||
|
||||
func newMemoryRepo() *memoryRepo {
|
||||
return &memoryRepo{admins: map[string]bool{}, prefs: map[string]string{}, bindings: map[string]ResourceBinding{}}
|
||||
}
|
||||
func (r *memoryRepo) IsAdministrator(_ context.Context, tenant, staff string) (bool, error) {
|
||||
return r.admins[tenant+"/"+staff], nil
|
||||
}
|
||||
func (r *memoryRepo) SetPreference(_ context.Context, tenant, channel, staff, locale string) error {
|
||||
r.prefs[tenant+"/"+staff] = locale
|
||||
return nil
|
||||
}
|
||||
func (r *memoryRepo) SaveBinding(_ context.Context, b ResourceBinding) error {
|
||||
r.bindings[b.Kind] = b
|
||||
return nil
|
||||
}
|
||||
func (r *memoryRepo) DetectBootstrapCandidate(_ context.Context, c BootstrapCandidate) error {
|
||||
r.candidate = c
|
||||
return nil
|
||||
}
|
||||
func TestControllerLanguageForOrdinaryUser(t *testing.T) {
|
||||
r := newMemoryRepo()
|
||||
c := NewController(r)
|
||||
out, err := c.Handle(context.Background(), SettingInput{TenantKey: "t", Channel: "dingtalk", StaffID: "u", Text: "/setting language ja", ConversationType: "direct"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if r.prefs["t/u"] != "ja" || out == "" {
|
||||
t.Fatalf("prefs=%v out=%q", r.prefs, out)
|
||||
}
|
||||
}
|
||||
func TestControllerRejectsOrdinaryUserSystemBinding(t *testing.T) {
|
||||
r := newMemoryRepo()
|
||||
c := NewController(r)
|
||||
out, err := c.Handle(context.Background(), SettingInput{TenantKey: "t", StaffID: "u", Text: "/setting support", ConversationType: "group", ConversationID: "g"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(r.bindings) != 0 || out != "你没有系统管理权限。" {
|
||||
t.Fatalf("bindings=%v out=%q", r.bindings, out)
|
||||
}
|
||||
}
|
||||
func TestControllerBindsAdminSupportGroup(t *testing.T) {
|
||||
r := newMemoryRepo()
|
||||
r.admins["t/a"] = true
|
||||
c := NewController(r)
|
||||
_, err := c.Handle(context.Background(), SettingInput{TenantKey: "t", StaffID: "a", Text: "/setting support", ConversationType: "group", ConversationID: "g", ConversationTitle: "技术支持"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if r.bindings["support_group"].ConversationID != "g" {
|
||||
t.Fatalf("binding=%+v", r.bindings)
|
||||
}
|
||||
}
|
||||
func TestControllerParsesAdminQuestionTable(t *testing.T) {
|
||||
r := newMemoryRepo()
|
||||
r.admins["t/a"] = true
|
||||
c := NewController(r)
|
||||
_, err := c.Handle(context.Background(), SettingInput{TenantKey: "t", StaffID: "a", Text: "/setting question https://alidocs.dingtalk.com/i/nodes/base?sheetId=sheet&viewId=view", ConversationType: "direct"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if r.bindings["question_table"].BaseID != "base" {
|
||||
t.Fatalf("binding=%+v", r.bindings)
|
||||
}
|
||||
}
|
||||
func TestControllerDetectsBootstrapCandidate(t *testing.T) {
|
||||
r := newMemoryRepo()
|
||||
c := NewController(r)
|
||||
c.now = func() time.Time { return time.Unix(100, 0) }
|
||||
_, err := c.Handle(context.Background(), SettingInput{TenantKey: "t", StaffID: "a", UnionID: "union", DisplayName: "Alice", Text: "/setting 123456", ConversationType: "direct"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if r.candidate.Code != "123456" || r.candidate.StaffID != "a" {
|
||||
t.Fatalf("candidate=%+v", r.candidate)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user