DeutschLernen/GermanApp/Domain/Entities/User.cs
Lasse Rune Hansen 6bcf592918 feat(backend): complete unit tests for User Authentication feature
- Fix MSTest compatibility with .NET 9.0 by upgrading to MSTest.TestFramework 4.2.3
- Move Tests directory to solution level (Tests/) to prevent test files from being compiled with GermanApp
- Update test project references to point to GermanApp/GermanApp.csproj
- Add InternalsVisibleTo attributes for test assemblies
- Make RefreshToken properties internal for testability
- Fix User.ChangeEmail null handling
- Add 46 comprehensive unit tests for User and RefreshToken domain entities
- All tests passing successfully

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
2026-06-05 17:07:15 +02:00

78 lines
2 KiB
C#

namespace GermanApp.Domain.Entities;
/// <summary>
/// Represents a user in the DeutschLernen system.
/// </summary>
public class User
{
public int Id { get; private set; }
public string Username { get; private set; } = string.Empty;
public string Email { get; private set; } = string.Empty;
public string PasswordHash { get; private set; } = string.Empty;
public string CurrentLevel { get; private set; } = "A1";
public int Streak { get; private set; }
public int TotalPoints { get; private set; }
public DateTime CreatedAt { get; private set; }
/// <summary>
/// Constructor for EF Core deserialization.
/// </summary>
private User() { }
/// <summary>
/// Factory method to create a new user.
/// </summary>
public static User Create(string username, string email, string passwordHash)
{
return new User
{
Username = username,
Email = email.ToLowerInvariant(),
PasswordHash = passwordHash,
CurrentLevel = "A1",
Streak = 0,
TotalPoints = 0,
CreatedAt = DateTime.UtcNow
};
}
/// <summary>
/// Updates user's gamification stats.
/// </summary>
public void AddPoints(int points)
{
TotalPoints += points;
}
/// <summary>
/// Updates user's streak.
/// </summary>
public void UpdateStreak(int streak)
{
Streak = streak;
}
/// <summary>
/// Updates user's current level.
/// </summary>
public void UpdateLevel(string level)
{
CurrentLevel = level;
}
/// <summary>
/// Changes user's email.
/// </summary>
public void ChangeEmail(string newEmail)
{
Email = newEmail?.ToLowerInvariant() ?? string.Empty;
}
/// <summary>
/// Changes user's password hash.
/// </summary>
public void ChangePassword(string newPasswordHash)
{
PasswordHash = newPasswordHash;
}
}