package api import ( "context" "droboticflow/internal/domain" "droboticflow/internal/store" "fmt" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/options" "os" "regexp" "testing" "time" ) // Real HTTP dictionary-admin validation uses a separate test organization, // never a new administrator in the customer's organization. func TestFiveRulesAdminLiveFixture(t *testing.T) { if os.Getenv("FANYU_FIVE_RULES_FIXTURE") != "1" { t.Skip("explicit isolated tenant fixture only") } nonce := os.Getenv("FANYU_DISCARD_NONCE") if !regexp.MustCompile(`^2026[0-9a-z-]{6,32}$`).MatchString(nonce) { t.Fatal("invalid test nonce") } ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) defer cancel() c, e := mongo.Connect(ctx, options.Client().ApplyURI(os.Getenv("MONGO_URI"))) if e != nil { t.Fatal("Mongo connection") } defer c.Disconnect(ctx) s := store.NewMongoStore(c.Database(os.Getenv("MONGO_DB"))) orgID, userID := "org-five-rules-test-"+nonce, "user-five-rules-test-"+nonce if os.Getenv("FANYU_FIVE_RULES_MODE") == "disable" { u, e := s.GetUser(ctx, userID) if e != nil || u == nil || u.OrganizationID != orgID { t.Fatal("test user scope") } u.Status = domain.UserStatusDisabled if e = s.UpsertUser(ctx, *u); e != nil { t.Fatal(e) } fmt.Println("Disabled isolated dictionary test admin") return } if existing, e := s.GetOrganization(ctx, orgID); e != nil || existing != nil { t.Fatal("test organization already exists") } password := os.Getenv("FANYU_DISCARD_PASSWORD") if len(password) < 20 { t.Fatal("ephemeral password required") } hash, e := hashPassword(password) if e != nil { t.Fatal(e) } if e = s.UpsertOrganization(ctx, domain.OrganizationRecord{ID: orgID, Name: "五项规则隔离验收", Slug: "five-rules-test-" + nonce, Status: domain.OrganizationStatusActive, CreatedAt: nowISO()}); e != nil { t.Fatal(e) } if e = s.UpsertUser(ctx, domain.UserRecord{ID: userID, OrganizationID: orgID, Name: "隔离字典验收管理员", Phone: testPhoneFromSeed("five-rules-admin-" + nonce), PasswordHash: hash, Role: domain.UserRoleAdmin, Status: domain.UserStatusActive, CreatedAt: nowISO()}); e != nil { t.Fatal(e) } if e = s.UpsertProject(ctx, domain.ProjectRecord{ID: "project-five-rules-test-" + nonce, OrganizationID: orgID, Name: "字典隔离验收", Status: domain.ProjectStatusActive, CreatedAt: nowISO()}); e != nil { t.Fatal(e) } fmt.Println("Created only isolated dictionary test tenant and admin") }