2026-08-21 00:41:07 +08:00
|
|
|
package services_test
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"slices"
|
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
|
|
"code.tczkiot.com/wlw/ai-agent/identity"
|
|
|
|
|
"code.tczkiot.com/wlw/ai-agent/internal/pkg/enums"
|
|
|
|
|
"code.tczkiot.com/wlw/ai-agent/internal/services"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var testExternalSubjects sync.Map
|
|
|
|
|
|
|
|
|
|
func registerTestExternalSubject(id int64, username, name string, status enums.Status) {
|
2026-08-28 22:23:13 +08:00
|
|
|
registerTestSubject(identity.Subject{
|
|
|
|
|
Type: identity.SubjectAdmin,
|
2026-08-21 00:41:07 +08:00
|
|
|
Category: identity.CategorySystem,
|
|
|
|
|
ID: id,
|
|
|
|
|
Username: username,
|
|
|
|
|
Name: name,
|
|
|
|
|
Identifier: username,
|
|
|
|
|
Enabled: status == enums.StatusOk,
|
|
|
|
|
})
|
2026-08-28 22:23:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func registerTestSubject(subject identity.Subject) {
|
|
|
|
|
testExternalSubjects.Store(string(subject.Type)+":"+subject.Identifier, subject)
|
2026-08-21 00:41:07 +08:00
|
|
|
services.SetQuerySubjects(func(_ context.Context, query identity.Query) ([]identity.Subject, error) {
|
|
|
|
|
results := make([]identity.Subject, 0)
|
|
|
|
|
testExternalSubjects.Range(func(_, value any) bool {
|
|
|
|
|
subject := value.(identity.Subject)
|
2026-08-28 22:23:13 +08:00
|
|
|
if len(query.Types) > 0 && !slices.Contains(query.Types, subject.Type) {
|
|
|
|
|
return true
|
|
|
|
|
}
|
2026-08-21 00:41:07 +08:00
|
|
|
if len(query.IDs) > 0 && !slices.Contains(query.IDs, subject.ID) {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
if query.EnabledOnly && !subject.Enabled {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
results = append(results, subject)
|
|
|
|
|
return true
|
|
|
|
|
})
|
|
|
|
|
return results, nil
|
|
|
|
|
})
|
|
|
|
|
services.SetAuthorize(func(_ context.Context, _ string) error { return nil })
|
|
|
|
|
}
|