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) }