Add comprehensive test suite infrastructure

- Create backend xUnit test project with Moq and FluentAssertions
- Add test utilities: TestDataFactory, MockHttpContext, TestUserClaims
- Create AuthControllerTests with comprehensive auth scenarios
- Install Jest and React Testing Library for frontend
- Configure jest.config.ts and jest.setup.ts with Next.js support
- Add test scripts to package.json
This commit is contained in:
Denis Urs Rudolph
2026-04-05 22:16:44 +02:00
parent 32bfbcadb1
commit 571fe5bc7c
19 changed files with 11224 additions and 1 deletions
@@ -0,0 +1,38 @@
using System.Security.Claims;
using Microsoft.AspNetCore.Http;
namespace backend.Tests.Utilities;
public static class MockHttpContext
{
public static HttpContext Create(
string userId = "test-user-id",
string email = "test@example.com",
string role = "Participant",
bool isAuthenticated = true)
{
var claims = new List<Claim>();
if (isAuthenticated)
{
claims.Add(new Claim(ClaimTypes.NameIdentifier, userId));
claims.Add(new Claim(ClaimTypes.Email, email));
claims.Add(new Claim(ClaimTypes.Role, role));
}
var identity = new ClaimsIdentity(claims, isAuthenticated ? "TestAuthType" : null);
var principal = new ClaimsPrincipal(identity);
var httpContext = new DefaultHttpContext
{
User = principal
};
return httpContext;
}
public static HttpContext CreateAnonymous()
{
return Create(isAuthenticated: false);
}
}
@@ -0,0 +1,99 @@
using RacePlannerApi.Models;
namespace backend.Tests.Utilities;
public static class TestDataFactory
{
public static User CreateUser(
string email = "test@example.com",
string name = "Test User",
UserRole role = UserRole.Participant,
string password = "password123")
{
return new User
{
Id = Guid.NewGuid(),
Email = email,
Name = name,
Role = role,
PasswordHash = BCrypt.Net.BCrypt.HashPassword(password),
CreatedAt = DateTime.UtcNow
};
}
public static Event CreateEvent(
string name = "Test Event",
string description = "Test event description",
DateTime? eventDate = null,
string location = "Test Location",
Guid? organizerId = null,
EventStatus status = EventStatus.Draft)
{
return new Event
{
Id = Guid.NewGuid(),
Name = name,
Description = description,
EventDate = eventDate ?? DateTime.UtcNow.AddDays(7),
Location = location,
OrganizerId = organizerId ?? Guid.NewGuid(),
Status = status,
Category = "Running",
Tags = new List<string>(),
MaxParticipants = 100,
CreatedAt = DateTime.UtcNow,
UpdatedAt = DateTime.UtcNow
};
}
public static Registration CreateRegistration(
Guid eventId,
Guid participantId,
RegistrationStatus status = RegistrationStatus.Pending)
{
return new Registration
{
Id = Guid.NewGuid(),
EventId = eventId,
ParticipantId = participantId,
Status = status,
EmergencyContact = "Emergency Contact: 123-456-7890",
Category = "Open",
CreatedAt = DateTime.UtcNow,
Payments = new List<Payment>()
};
}
public static Payment CreatePayment(
Guid registrationId,
decimal amount = 50.00m,
PaymentMethod method = PaymentMethod.Cash)
{
return new Payment
{
Id = Guid.NewGuid(),
RegistrationId = registrationId,
Amount = amount,
Method = method,
TransactionId = method == PaymentMethod.Online ? Guid.NewGuid().ToString() : null,
PaymentDate = DateTime.UtcNow,
CreatedAt = DateTime.UtcNow
};
}
public static Announcement CreateAnnouncement(
Guid eventId,
string title = "Test Announcement",
string content = "Test announcement content")
{
return new Announcement
{
Id = Guid.NewGuid(),
EventId = eventId,
Title = title,
Content = content,
CreatedAt = DateTime.UtcNow,
UpdatedAt = DateTime.UtcNow
};
}
}
@@ -0,0 +1,37 @@
using System.Security.Claims;
namespace backend.Tests.Utilities;
public static class TestUserClaims
{
public static ClaimsPrincipal CreateOrganizer(Guid? userId = null, string email = "organizer@example.com")
{
var claims = new List<Claim>
{
new Claim(ClaimTypes.NameIdentifier, (userId ?? Guid.NewGuid()).ToString()),
new Claim(ClaimTypes.Email, email),
new Claim(ClaimTypes.Role, "Organizer")
};
var identity = new ClaimsIdentity(claims, "TestAuthType");
return new ClaimsPrincipal(identity);
}
public static ClaimsPrincipal CreateParticipant(Guid? userId = null, string email = "participant@example.com")
{
var claims = new List<Claim>
{
new Claim(ClaimTypes.NameIdentifier, (userId ?? Guid.NewGuid()).ToString()),
new Claim(ClaimTypes.Email, email),
new Claim(ClaimTypes.Role, "Participant")
};
var identity = new ClaimsIdentity(claims, "TestAuthType");
return new ClaimsPrincipal(identity);
}
public static ClaimsPrincipal CreateUnauthenticated()
{
return new ClaimsPrincipal(new ClaimsIdentity());
}
}