94 lines
3.4 KiB
Go
94 lines
3.4 KiB
Go
package directory
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"testing"
|
|
)
|
|
|
|
type fakeStore struct {
|
|
profile Profile
|
|
found bool
|
|
readErr error
|
|
writeErr error
|
|
writes int
|
|
}
|
|
|
|
func (s *fakeStore) LookupDirectoryProfile(context.Context, string, string) (Profile, bool, error) {
|
|
return s.profile, s.found, s.readErr
|
|
}
|
|
func (s *fakeStore) UpsertDirectoryProfiles(_ context.Context, _ string, profiles []Profile) error {
|
|
s.writes++
|
|
if len(profiles) > 0 {
|
|
s.profile = profiles[0]
|
|
}
|
|
return s.writeErr
|
|
}
|
|
|
|
type fakeLookup struct {
|
|
profile Profile
|
|
err error
|
|
calls int
|
|
}
|
|
func (l *fakeLookup) LookupUser(context.Context, string) (Profile, error) {
|
|
l.calls++
|
|
return l.profile, l.err
|
|
}
|
|
|
|
func TestResolverUsesLocalProfileWithoutRemoteLookup(t *testing.T) {
|
|
store := &fakeStore{profile: Profile{StaffID: "staff-1", Name: "张三", DepartmentName: "技术支持部", Region: "APAC"}, found: true}
|
|
lookup := &fakeLookup{}
|
|
resolver, err := NewResolver(store, lookup)
|
|
if err != nil { t.Fatal(err) }
|
|
profile, err := resolver.ResolveUser(context.Background(), "tenant-a", "staff-1", "昵称")
|
|
if err != nil { t.Fatalf("ResolveUser: %v", err) }
|
|
if profile.Name != "张三" || lookup.calls != 0 || store.writes != 0 {
|
|
t.Fatalf("profile=%+v lookup=%d writes=%d", profile, lookup.calls, store.writes)
|
|
}
|
|
}
|
|
|
|
func TestResolverLooksUpAndCachesMissingUser(t *testing.T) {
|
|
store := &fakeStore{}
|
|
lookup := &fakeLookup{profile: Profile{Name: "李四", DepartmentName: "销售部", Region: "EMEA"}}
|
|
resolver, err := NewResolver(store, lookup)
|
|
if err != nil { t.Fatal(err) }
|
|
profile, err := resolver.ResolveUser(context.Background(), "tenant-a", "staff-2", "昵称")
|
|
if err != nil { t.Fatalf("ResolveUser: %v", err) }
|
|
if profile.StaffID != "staff-2" || profile.Name != "李四" || profile.Source != SourceOnDemand || lookup.calls != 1 || store.writes != 1 {
|
|
t.Fatalf("profile=%+v lookup=%d writes=%d", profile, lookup.calls, store.writes)
|
|
}
|
|
}
|
|
|
|
func TestResolverFallsBackWithoutBlockingOnFailures(t *testing.T) {
|
|
tests := []struct{
|
|
name string
|
|
store *fakeStore
|
|
lookup *fakeLookup
|
|
}{
|
|
{name: "local read fails", store: &fakeStore{readErr: errors.New("db unavailable")}, lookup: &fakeLookup{}},
|
|
{name: "remote lookup fails", store: &fakeStore{}, lookup: &fakeLookup{err: errors.New("remote unavailable")}},
|
|
{name: "cache write fails but resolved profile survives", store: &fakeStore{writeErr: errors.New("db unavailable")}, lookup: &fakeLookup{profile: Profile{Name: "王五"}}},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
resolver, err := NewResolver(tt.store, tt.lookup)
|
|
if err != nil { t.Fatal(err) }
|
|
profile, resolveErr := resolver.ResolveUser(context.Background(), "tenant-a", "staff-3", " 回调昵称 ")
|
|
if profile.Name == "" { t.Fatalf("empty fallback profile: %+v", profile) }
|
|
if tt.name != "cache write fails but resolved profile survives" && profile.Name != "回调昵称" {
|
|
t.Fatalf("profile name=%q, want fallback", profile.Name)
|
|
}
|
|
if resolveErr == nil { t.Fatal("expected diagnostic error") }
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestResolverWithoutRemoteUsesFallback(t *testing.T) {
|
|
resolver, err := NewResolver(&fakeStore{}, nil)
|
|
if err != nil { t.Fatal(err) }
|
|
profile, err := resolver.ResolveUser(context.Background(), "tenant-a", "staff-4", "赵六")
|
|
if err != nil || profile.Name != "赵六" || profile.StaffID != "staff-4" {
|
|
t.Fatalf("profile=%+v err=%v", profile, err)
|
|
}
|
|
}
|