Some checks failed
CI / Build & Test (push) Failing after 1m31s
- Add user management endpoints (list/create/get/update/delete) with pagination and search - Add dashboard stats endpoint with tenant/user/online counts and growth metrics - Add tenant resolver middleware for multi-tenant request scoping - Add i18n kernel with zh/en message files and AcceptLanguage middleware - Add WebSocket hub/handler for real-time communication - Add job scheduler kernel with cron support - Add plugin sandbox for isolated execution - Add storage kernel (local filesystem) - Add event bus kernel for pub/sub - Add cache kernel abstraction - Add database migration runner and version upgrade checker - Add rate limiting middleware with Redis backend - Add SQL migrations for rbac, audit_logs, settings, notifications, examples - Extend user repository with list/delete/count operations - Register all module routes with tenant resolver
74 lines
1.6 KiB
Go
74 lines
1.6 KiB
Go
package testutil
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
|
|
authdomain "mengstack/internal/modules/auth/domain"
|
|
orgdomain "mengstack/internal/modules/org/domain"
|
|
rbacdomain "mengstack/internal/modules/rbac/domain"
|
|
|
|
"golang.org/x/crypto/bcrypt"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
const DefaultTenantID = "00000000-0000-0000-0000-000000000001"
|
|
|
|
func SeedTenant(t *testing.T, db *gorm.DB) string {
|
|
t.Helper()
|
|
tenant := orgdomain.Tenant{
|
|
ID: DefaultTenantID,
|
|
Name: "Test Tenant",
|
|
Slug: "test",
|
|
}
|
|
if err := db.FirstOrCreate(&tenant, orgdomain.Tenant{ID: DefaultTenantID}).Error; err != nil {
|
|
t.Fatalf("seed tenant: %v", err)
|
|
}
|
|
return tenant.ID
|
|
}
|
|
|
|
func SeedUser(t *testing.T, db *gorm.DB, tenantID, email, password string) uint {
|
|
t.Helper()
|
|
hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
|
if err != nil {
|
|
t.Fatalf("hash password: %v", err)
|
|
}
|
|
user := authdomain.User{
|
|
TenantID: tenantID,
|
|
Username: email,
|
|
Email: email,
|
|
Password: string(hash),
|
|
Nickname: "Test User",
|
|
Status: 1,
|
|
}
|
|
if err := db.Create(&user).Error; err != nil {
|
|
t.Fatalf("seed_user: %v", err)
|
|
}
|
|
return user.ID
|
|
}
|
|
|
|
func SeedUserWithRole(t *testing.T, db *gorm.DB, tenantID, email, password string) uint {
|
|
t.Helper()
|
|
userID := SeedUser(t, db, tenantID, email, password)
|
|
|
|
role := rbacdomain.Role{
|
|
TenantID: tenantID,
|
|
Name: "admin",
|
|
IsSystem: true,
|
|
}
|
|
db.FirstOrCreate(&role, rbacdomain.Role{TenantID: tenantID, Name: "admin"})
|
|
|
|
userRole := rbacdomain.UserRole{
|
|
UserID: userID,
|
|
TenantID: tenantID,
|
|
RoleID: role.ID,
|
|
}
|
|
db.FirstOrCreate(&userRole, userRole)
|
|
|
|
return userID
|
|
}
|
|
|
|
func FutureTime(d time.Duration) time.Time {
|
|
return time.Now().Add(d)
|
|
}
|