mengstack-api/tests/integration/auth_test.go
MengStack Dev df809f1045
Some checks failed
CI / Build & Test (push) Failing after 1m31s
feat: user CRUD API + dashboard stats + kernel infrastructure
- 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
2026-10-03 03:42:58 +08:00

88 lines
2.5 KiB
Go

package integration
import (
"testing"
"mengstack/internal/app/middleware"
"mengstack/internal/config"
"mengstack/internal/kernel/tenant"
"mengstack/internal/modules/auth/application"
authinfra "mengstack/internal/modules/auth/infrastructure"
authinterfaces "mengstack/internal/modules/auth/interfaces"
"mengstack/internal/testutil"
"github.com/gin-gonic/gin"
)
func setupAuthEngine(t *testing.T) (*gin.Engine, *testutil.APITest) {
t.Helper()
db := testutil.NewTestDB(t)
testutil.CleanDB(t, db)
cfg := testutil.TestConfig()
repo := authinfra.NewUserRepository(db)
jwtCfg := application.JWTConfig{
Secret: cfg.JWT.Secret,
AccessExpiryMinutes: cfg.JWT.AccessExpiryMinutes,
RefreshExpiryDays: cfg.JWT.RefreshExpiryDays,
Issuer: cfg.JWT.Issuer,
}
svc := application.NewService(repo, jwtCfg)
handler := authinterfaces.NewHandler(svc)
r := gin.New()
tenantResolver := tenant.NewResolver(config.TenantConfig{Mode: "multi"})
authinterfaces.SetupRoutes(r, handler, middleware.MultiTenant(tenantResolver), tenantResolver)
return r, testutil.NewAPITest(t, r)
}
func TestRegister(t *testing.T) {
db := testutil.NewTestDB(t)
testutil.CleanDB(t, db)
testutil.SeedTenant(t, db)
_, api := setupAuthEngine(t)
api.Post("/api/v1/auth/register", map[string]string{
"username": "testuser",
"email": "test@example.com",
"password": "Password123!",
}).WithTenant(testutil.DefaultTenantID).Do().AssertStatus(200).AssertCode(0)
}
func TestLogin(t *testing.T) {
db := testutil.NewTestDB(t)
testutil.CleanDB(t, db)
testutil.SeedTenant(t, db)
testutil.SeedUser(t, db, testutil.DefaultTenantID, "login@test.com", "Password123!")
_, api := setupAuthEngine(t)
resp := api.Post("/api/v1/auth/login", map[string]string{
"email": "login@test.com",
"password": "Password123!",
}).WithTenant(testutil.DefaultTenantID).Do().AssertStatus(200).AssertCode(0)
data := resp.Data()
if data["access_token"] == nil {
t.Error("expected access_token in response")
}
if data["refresh_token"] == nil {
t.Error("expected refresh_token in response")
}
}
func TestLoginWrongPassword(t *testing.T) {
db := testutil.NewTestDB(t)
testutil.CleanDB(t, db)
testutil.SeedTenant(t, db)
testutil.SeedUser(t, db, testutil.DefaultTenantID, "wrong@test.com", "Password123!")
_, api := setupAuthEngine(t)
api.Post("/api/v1/auth/login", map[string]string{
"email": "wrong@test.com",
"password": "WrongPassword!",
}).WithTenant(testutil.DefaultTenantID).Do().AssertStatus(401)
}