using MongoDB.Driver; using BCards.Web.Models; using BCards.Web.Repositories; using BCards.Web.ViewModels; namespace BCards.IntegrationTests.Fixtures; public class MongoDbTestFixture { public IMongoDatabase Database { get; } public IUserRepository UserRepository { get; } public IUserPageRepository UserPageRepository { get; } public ICategoryRepository CategoryRepository { get; } public MongoDbTestFixture(IMongoDatabase database) { Database = database; UserRepository = new UserRepository(database); UserPageRepository = new UserPageRepository(database); CategoryRepository = new CategoryRepository(database); } public async Task InitializeTestDataAsync() { // Initialize test categories var categories = new List { new() { Id = "tecnologia", Name = "Tecnologia", Description = "Empresas e profissionais de tecnologia" }, new() { Id = "negocios", Name = "Negócios", Description = "Empresas e empreendedores" }, new() { Id = "pessoal", Name = "Pessoal", Description = "Páginas pessoais e freelancers" }, new() { Id = "saude", Name = "Saúde", Description = "Profissionais da área da saúde" } }; var existingCategories = await CategoryRepository.GetAllActiveAsync(); if (!existingCategories.Any()) { foreach (var category in categories) { await CategoryRepository.CreateAsync(category); } } } public async Task CreateTestUserAsync(PlanType planType = PlanType.Basic, string? email = null, string? name = null) { var user = new User { Id = Guid.NewGuid().ToString(), Email = email ?? $"test-{Guid.NewGuid():N}@example.com", Name = name ?? "Test User", CurrentPlan = planType.ToString(), CreatedAt = DateTime.UtcNow, UpdatedAt = DateTime.UtcNow, IsActive = true }; await UserRepository.CreateAsync(user); return user; } public async Task CreateTestUserPageAsync( string userId, PageStatus status = PageStatus.Creating, string category = "tecnologia", int normalLinkCount = 3, int productLinkCount = 1, string? slug = null) { var pageSlug = slug ?? $"test-page-{Guid.NewGuid():N}"; var userPage = new UserPage { Id = Guid.NewGuid().ToString(), UserId = userId, DisplayName = "Test Page", Category = category, Slug = pageSlug, Bio = "Test page for integration testing", Status = status, BusinessType = "individual", Theme = new PageTheme { Name = "minimalist" }, Links = new List(), CreatedAt = DateTime.UtcNow, UpdatedAt = DateTime.UtcNow, ModerationAttempts = 0, ModerationHistory = new List() }; // Generate preview token for non-Active pages if (status != PageStatus.Active) { userPage.PreviewToken = Guid.NewGuid().ToString("N")[..16]; userPage.PreviewTokenExpiry = DateTime.UtcNow.AddHours(4); } // Add normal links for (int i = 0; i < normalLinkCount; i++) { userPage.Links.Add(new LinkItem { Title = $"Test Link {i + 1}", Url = $"https://example.com/link{i + 1}", Description = $"Description for test link {i + 1}", Icon = "fas fa-link", IsActive = true, Order = i, Type = LinkType.Normal }); } // Add product links for (int i = 0; i < productLinkCount; i++) { userPage.Links.Add(new LinkItem { Title = $"Test Product {i + 1}", Url = $"https://example.com/product{i + 1}", Description = $"Description for test product {i + 1}", Icon = "fas fa-shopping-cart", IsActive = true, Order = normalLinkCount + i, Type = LinkType.Product, ProductTitle = $"Amazing Product {i + 1}", ProductPrice = "R$ 99,90", ProductDescription = $"This is an amazing product for testing purposes {i + 1}", ProductImage = $"https://example.com/images/product{i + 1}.jpg" }); } await UserPageRepository.CreateAsync(userPage); return userPage; } public async Task CreateTestUserWithPageAsync( PlanType planType = PlanType.Basic, PageStatus pageStatus = PageStatus.Creating, int normalLinks = 3, int productLinks = 1) { var user = await CreateTestUserAsync(planType); var page = await CreateTestUserPageAsync(user.Id, pageStatus, "tecnologia", normalLinks, productLinks); return user; } public async Task CleanAllDataAsync() { var collections = new[] { "users", "userpages", "categories", "livepages", "subscriptions" }; foreach (var collectionName in collections) { try { await Database.DropCollectionAsync(collectionName); } catch (Exception) { // Ignore errors if collection doesn't exist } } await InitializeTestDataAsync(); } public async Task> GetUserPagesAsync(string userId) { var filter = Builders.Filter.Eq(p => p.UserId, userId); var pages = await UserPageRepository.GetManyAsync(filter); return pages.ToList(); } public async Task GetUserPageAsync(string category, string slug) { var filter = Builders.Filter.And( Builders.Filter.Eq(p => p.Category, category), Builders.Filter.Eq(p => p.Slug, slug) ); var pages = await UserPageRepository.GetManyAsync(filter); return pages.FirstOrDefault(); } }