From f1e3f117fa59d6d34257522ec1b1fef271e8f444 Mon Sep 17 00:00:00 2001 From: Youssef11-dev Date: Thu, 3 Dec 2026 15:56:22 +0200 Subject: [PATCH] add user and user tests --- Basira.slnx | 3 + src/BuildingBlocks/Domain/DomainException.cs | 3 + src/BuildingBlocks/Domain/Entity.cs | 11 +++ .../Basira.Modules.Identity.Domain.csproj | 2 +- .../Domain/Organizations/Organization.cs | 31 ++++++ src/Modules/Identity/Domain/Users/User.cs | 97 +++++++++++++++++++ src/Modules/Identity/Domain/Users/UserRole.cs | 8 ++ .../Basira.Modules.Identity.UnitTests.csproj | 25 +++++ tests/Identity.UnitTests/OrganizationTests.cs | 35 +++++++ tests/Identity.UnitTests/UserTests.cs | 79 +++++++++++++++ 10 files changed, 293 insertions(+), 1 deletion(-) create mode 100644 src/BuildingBlocks/Domain/DomainException.cs create mode 100644 src/BuildingBlocks/Domain/Entity.cs create mode 100644 src/Modules/Identity/Domain/Organizations/Organization.cs create mode 100644 src/Modules/Identity/Domain/Users/User.cs create mode 100644 src/Modules/Identity/Domain/Users/UserRole.cs create mode 100644 tests/Identity.UnitTests/Basira.Modules.Identity.UnitTests.csproj create mode 100644 tests/Identity.UnitTests/OrganizationTests.cs create mode 100644 tests/Identity.UnitTests/UserTests.cs diff --git a/Basira.slnx b/Basira.slnx index f584218..5baece3 100644 --- a/Basira.slnx +++ b/Basira.slnx @@ -27,4 +27,7 @@ + + + diff --git a/src/BuildingBlocks/Domain/DomainException.cs b/src/BuildingBlocks/Domain/DomainException.cs new file mode 100644 index 0000000..daaf1cf --- /dev/null +++ b/src/BuildingBlocks/Domain/DomainException.cs @@ -0,0 +1,3 @@ +namespace Basira.BuildingBlocks.Domain; + +public class DomainException(string message) : Exception(message); \ No newline at end of file diff --git a/src/BuildingBlocks/Domain/Entity.cs b/src/BuildingBlocks/Domain/Entity.cs new file mode 100644 index 0000000..7b3169f --- /dev/null +++ b/src/BuildingBlocks/Domain/Entity.cs @@ -0,0 +1,11 @@ +namespace Basira.BuildingBlocks.Domain; + +public abstract class Entity +{ + public Guid Id { get; protected set; } + + protected Entity(Guid id) => Id = id; + + // Required by EF Core when it loads an entity from the database. + protected Entity() { } +} \ No newline at end of file diff --git a/src/Modules/Identity/Domain/Basira.Modules.Identity.Domain.csproj b/src/Modules/Identity/Domain/Basira.Modules.Identity.Domain.csproj index 3850da6..9516bbf 100644 --- a/src/Modules/Identity/Domain/Basira.Modules.Identity.Domain.csproj +++ b/src/Modules/Identity/Domain/Basira.Modules.Identity.Domain.csproj @@ -1,4 +1,4 @@ - + diff --git a/src/Modules/Identity/Domain/Organizations/Organization.cs b/src/Modules/Identity/Domain/Organizations/Organization.cs new file mode 100644 index 0000000..1541744 --- /dev/null +++ b/src/Modules/Identity/Domain/Organizations/Organization.cs @@ -0,0 +1,31 @@ +using Basira.BuildingBlocks.Domain; + +namespace Basira.Modules.Identity.Domain.Organizations; + +public sealed class Organization : Entity +{ + public const int NameMaxLength = 200; + + public string Name { get; private set; } = string.Empty; + public DateTime CreatedAtUtc { get; private set; } + + private Organization() { } + + private Organization(Guid id, string name, DateTime createdAtUtc) : base(id) + { + Name = name; + CreatedAtUtc = createdAtUtc; + } + + public static Organization Create(string name, DateTime utcNow) + { + if (string.IsNullOrWhiteSpace(name)) + throw new DomainException("Organization name is required."); + + var trimmed = name.Trim(); + if (trimmed.Length > NameMaxLength) + throw new DomainException($"Organization name cannot exceed {NameMaxLength} characters."); + + return new Organization(Guid.NewGuid(), trimmed, utcNow); + } +} \ No newline at end of file diff --git a/src/Modules/Identity/Domain/Users/User.cs b/src/Modules/Identity/Domain/Users/User.cs new file mode 100644 index 0000000..cd13747 --- /dev/null +++ b/src/Modules/Identity/Domain/Users/User.cs @@ -0,0 +1,97 @@ +using Basira.BuildingBlocks.Domain; + +namespace Basira.Modules.Identity.Domain.Users; + +public sealed class User : Entity +{ + public const int EmailMaxLength = 256; + public const int FullNameMaxLength = 200; + + public Guid OrganizationId { get; private set; } + public string Email { get; private set; } = string.Empty; + public string FullName { get; private set; } = string.Empty; + public string PasswordHash { get; private set; } = string.Empty; + public UserRole Role { get; private set; } + public bool IsActive { get; private set; } + public DateTime CreatedAtUtc { get; private set; } + + private User() { } + + private User( + Guid id, Guid organizationId, string email, string fullName, + string passwordHash, UserRole role, DateTime createdAtUtc) : base(id) + { + OrganizationId = organizationId; + Email = email; + FullName = fullName; + PasswordHash = passwordHash; + Role = role; + IsActive = true; + CreatedAtUtc = createdAtUtc; + } + + public static User Create( + Guid organizationId, string email, string fullName, + string passwordHash, UserRole role, DateTime utcNow) + { + if (organizationId == Guid.Empty) + throw new DomainException("A user must belong to an organization."); + + var normalizedEmail = NormalizeEmail(email); + + if (string.IsNullOrWhiteSpace(fullName)) + throw new DomainException("Full name is required."); + var trimmedName = fullName.Trim(); + if (trimmedName.Length > FullNameMaxLength) + throw new DomainException($"Full name cannot exceed {FullNameMaxLength} characters."); + + if (string.IsNullOrWhiteSpace(passwordHash)) + throw new DomainException("Password hash is required."); + + EnsureRoleIsDefined(role); + + return new User( + Guid.NewGuid(), organizationId, normalizedEmail, + trimmedName, passwordHash, role, utcNow); + } + + public void ChangeRole(UserRole newRole) + { + EnsureRoleIsDefined(newRole); + Role = newRole; + } + + public void Deactivate() => IsActive = false; + + public void Activate() => IsActive = true; + + public static string NormalizeEmail(string email) + { + if (string.IsNullOrWhiteSpace(email)) + throw new DomainException("Email is required."); + + var normalized = email.Trim().ToLowerInvariant(); + if (normalized.Length > EmailMaxLength || !LooksLikeEmail(normalized)) + throw new DomainException("Email is not valid."); + + return normalized; + } + + private static bool LooksLikeEmail(string value) + { + if (value.Any(char.IsWhiteSpace)) return false; + + var parts = value.Split('@'); + return parts.Length == 2 + && parts[0].Length > 0 + && parts[1].Contains('.') + && !parts[1].StartsWith('.') + && !parts[1].EndsWith('.'); + } + + private static void EnsureRoleIsDefined(UserRole role) + { + if (!Enum.IsDefined(role)) + throw new DomainException("Role is not valid."); + } +} \ No newline at end of file diff --git a/src/Modules/Identity/Domain/Users/UserRole.cs b/src/Modules/Identity/Domain/Users/UserRole.cs new file mode 100644 index 0000000..fe831c9 --- /dev/null +++ b/src/Modules/Identity/Domain/Users/UserRole.cs @@ -0,0 +1,8 @@ +namespace Basira.Modules.Identity.Domain.Users; + +public enum UserRole +{ + Customer = 1, + Agent = 2, + Admin = 3 +} \ No newline at end of file diff --git a/tests/Identity.UnitTests/Basira.Modules.Identity.UnitTests.csproj b/tests/Identity.UnitTests/Basira.Modules.Identity.UnitTests.csproj new file mode 100644 index 0000000..a7ccd7a --- /dev/null +++ b/tests/Identity.UnitTests/Basira.Modules.Identity.UnitTests.csproj @@ -0,0 +1,25 @@ + + + + net10.0 + enable + enable + false + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/tests/Identity.UnitTests/OrganizationTests.cs b/tests/Identity.UnitTests/OrganizationTests.cs new file mode 100644 index 0000000..0ad23d0 --- /dev/null +++ b/tests/Identity.UnitTests/OrganizationTests.cs @@ -0,0 +1,35 @@ +using Basira.BuildingBlocks.Domain; +using Basira.Modules.Identity.Domain.Organizations; + +namespace Basira.Modules.Identity.UnitTests; + +public class OrganizationTests +{ + private static readonly DateTime Now = new(2026, 11, 30, 9, 0, 0, DateTimeKind.Utc); + + [Fact] + public void Create_with_valid_name_trims_it_and_sets_fields() + { + var org = Organization.Create(" Acme ", Now); + + Assert.Equal("Acme", org.Name); + Assert.NotEqual(Guid.Empty, org.Id); + Assert.Equal(Now, org.CreatedAtUtc); + } + + [Theory] + [InlineData("")] + [InlineData(" ")] + public void Create_with_empty_name_throws(string name) + { + Assert.Throws(() => Organization.Create(name, Now)); + } + + [Fact] + public void Create_with_too_long_name_throws() + { + var name = new string('a', Organization.NameMaxLength + 1); + + Assert.Throws(() => Organization.Create(name, Now)); + } +} \ No newline at end of file diff --git a/tests/Identity.UnitTests/UserTests.cs b/tests/Identity.UnitTests/UserTests.cs new file mode 100644 index 0000000..4bfb80d --- /dev/null +++ b/tests/Identity.UnitTests/UserTests.cs @@ -0,0 +1,79 @@ +using Basira.BuildingBlocks.Domain; +using Basira.Modules.Identity.Domain.Users; + +namespace Basira.Modules.Identity.UnitTests; + +public class UserTests +{ + private static readonly DateTime Now = new(2026, 11, 30, 9, 0, 0, DateTimeKind.Utc); + private static readonly Guid OrgId = Guid.NewGuid(); + + private static User CreateValidUser(string email = "Ali@Example.com") => + User.Create(OrgId, email, " Ali Hassan ", "hash", UserRole.Agent, Now); + + [Fact] + public void Create_normalizes_email_trims_name_and_starts_active() + { + var user = CreateValidUser(); + + Assert.Equal("ali@example.com", user.Email); + Assert.Equal("Ali Hassan", user.FullName); + Assert.Equal(OrgId, user.OrganizationId); + Assert.True(user.IsActive); + } + + [Theory] + [InlineData("")] + [InlineData("not-an-email")] + [InlineData("a@b")] + [InlineData("@example.com")] + [InlineData("a b@example.com")] + [InlineData("a@@example.com")] + public void Create_with_invalid_email_throws(string email) + { + Assert.Throws(() => CreateValidUser(email)); + } + + [Fact] + public void Create_with_empty_organization_throws() + { + Assert.Throws(() => + User.Create(Guid.Empty, "a@b.com", "Ali", "hash", UserRole.Agent, Now)); + } + + [Fact] + public void Create_with_empty_password_hash_throws() + { + Assert.Throws(() => + User.Create(OrgId, "a@b.com", "Ali", " ", UserRole.Agent, Now)); + } + + [Fact] + public void Create_with_undefined_role_throws() + { + Assert.Throws(() => + User.Create(OrgId, "a@b.com", "Ali", "hash", (UserRole)0, Now)); + } + + [Fact] + public void Deactivate_then_Activate_toggles_IsActive() + { + var user = CreateValidUser(); + + user.Deactivate(); + Assert.False(user.IsActive); + + user.Activate(); + Assert.True(user.IsActive); + } + + [Fact] + public void ChangeRole_updates_role() + { + var user = CreateValidUser(); + + user.ChangeRole(UserRole.Admin); + + Assert.Equal(UserRole.Admin, user.Role); + } +} \ No newline at end of file