BCards/tests/BCards.Tests/Fixtures/DatabaseFixture.cs
Ricardo Carneiro 3d2ce1f8cf
All checks were successful
BCards Deployment Pipeline / Run Tests (push) Successful in 4s
BCards Deployment Pipeline / PR Validation (push) Has been skipped
BCards Deployment Pipeline / Build and Push Image (push) Successful in 15m22s
BCards Deployment Pipeline / Deploy to Production (ARM - OCI) (push) Successful in 1m54s
BCards Deployment Pipeline / Deploy to Test (x86 - Local) (push) Has been skipped
BCards Deployment Pipeline / Cleanup Old Resources (push) Has been skipped
BCards Deployment Pipeline / Deployment Summary (push) Successful in 0s
fix: Increase session timeout to 7 days and set SameSite=None for Cloudflare compatibility
🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-09-18 12:32:42 -03:00

142 lines
4.6 KiB
C#

using MongoDB.Driver;
using BCards.Web.Models;
using BCards.Web.Repositories;
namespace BCards.Tests.Fixtures;
public class DatabaseFixture : IDisposable
{
public IMongoDatabase Database { get; }
public IUserRepository UserRepository { get; }
public IUserPageRepository UserPageRepository { get; }
public ICategoryRepository CategoryRepository { get; }
public DatabaseFixture(IMongoDatabase database)
{
Database = database;
UserRepository = new UserRepository(database);
UserPageRepository = new UserPageRepository(database);
CategoryRepository = new CategoryRepository(database);
InitializeTestData().Wait();
}
private async Task InitializeTestData()
{
// Clear any existing data
await Database.DropCollectionAsync("users");
await Database.DropCollectionAsync("userpages");
await Database.DropCollectionAsync("categories");
// Initialize test categories
var categories = new List<Category>
{
new Category { Id = "tech", Name = "Tecnologia", Description = "Tecnologia e inovação" },
new Category { Id = "business", Name = "Negócios", Description = "Empresas e negócios" },
new Category { Id = "personal", Name = "Pessoal", Description = "Páginas pessoais" }
};
await CategoryRepository.CreateManyAsync(categories);
}
public async Task<User> CreateTestUser(PlanType planType = PlanType.Trial, string? email = null)
{
var user = new User
{
Id = Guid.NewGuid().ToString(),
Email = email ?? $"test-{Guid.NewGuid():N}@example.com",
Name = "Test User",
CurrentPlan = planType.ToString(),
CreatedAt = DateTime.UtcNow,
UpdatedAt = DateTime.UtcNow,
IsActive = true
};
await UserRepository.CreateAsync(user);
return user;
}
public async Task<UserPage> CreateTestUserPage(string userId, string category = "tech", int linkCount = 1, int productLinkCount = 0)
{
var userPage = new UserPage
{
Id = Guid.NewGuid().ToString(),
UserId = userId,
DisplayName = "Test Page",
Category = category,
Slug = $"test-page-{Guid.NewGuid():N}",
Bio = "Test page description",
Status = BCards.Web.ViewModels.PageStatus.Active,
Links = new List<LinkItem>(),
CreatedAt = DateTime.UtcNow,
UpdatedAt = DateTime.UtcNow
};
// Add normal links
for (int i = 0; i < linkCount; i++)
{
userPage.Links.Add(new LinkItem
{
Title = $"Test Link {i + 1}",
Url = $"https://example.com/link{i + 1}",
Description = $"Test link {i + 1} description",
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 = $"Test product {i + 1} description",
Icon = "fas fa-shopping-cart",
IsActive = true,
Order = linkCount + i,
Type = LinkType.Product,
ProductTitle = $"Product {i + 1}",
ProductPrice = "R$ 99,90",
ProductDescription = $"Amazing product {i + 1}"
});
}
await UserPageRepository.CreateAsync(userPage);
return userPage;
}
public async Task CleanDatabase()
{
var collections = new[] { "users", "userpages", "categories", "livepages" };
foreach (var collection in collections)
{
try
{
await Database.DropCollectionAsync(collection);
}
catch (Exception)
{
// Ignore errors when collection doesn't exist
}
}
await InitializeTestData();
}
public void Dispose()
{
try
{
Database.Client.DropDatabase(Database.DatabaseNamespace.DatabaseName);
}
catch (Exception)
{
// Ignore cleanup errors
}
}
}