Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Basira.slnx
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,7 @@
<Folder Name="/tests/Architecture.Tests/">
<Project Path="tests/Architecture.Tests/Basira.Architecture.Tests.csproj" />
</Folder>
<Folder Name="/tests/Identity.UnitTests/">
<Project Path="tests/Identity.UnitTests/Basira.Modules.Identity.UnitTests.csproj" />
</Folder>
</Solution>
3 changes: 3 additions & 0 deletions src/BuildingBlocks/Domain/DomainException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace Basira.BuildingBlocks.Domain;

public class DomainException(string message) : Exception(message);
11 changes: 11 additions & 0 deletions src/BuildingBlocks/Domain/Entity.cs
Original file line number Diff line number Diff line change
@@ -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() { }
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<ItemGroup>
<ProjectReference Include="..\..\..\BuildingBlocks\Basira.BuildingBlocks.csproj" />
Expand Down
31 changes: 31 additions & 0 deletions src/Modules/Identity/Domain/Organizations/Organization.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
97 changes: 97 additions & 0 deletions src/Modules/Identity/Domain/Users/User.cs
Original file line number Diff line number Diff line change
@@ -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.");
}
}
8 changes: 8 additions & 0 deletions src/Modules/Identity/Domain/Users/UserRole.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Basira.Modules.Identity.Domain.Users;

public enum UserRole
{
Customer = 1,
Agent = 2,
Admin = 3
}
25 changes: 25 additions & 0 deletions tests/Identity.UnitTests/Basira.Modules.Identity.UnitTests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.4" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.14.1" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.4" />
</ItemGroup>

<ItemGroup>
<Using Include="Xunit" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Modules\Identity\Domain\Basira.Modules.Identity.Domain.csproj" />
</ItemGroup>

</Project>
35 changes: 35 additions & 0 deletions tests/Identity.UnitTests/OrganizationTests.cs
Original file line number Diff line number Diff line change
@@ -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<DomainException>(() => Organization.Create(name, Now));
}

[Fact]
public void Create_with_too_long_name_throws()
{
var name = new string('a', Organization.NameMaxLength + 1);

Assert.Throws<DomainException>(() => Organization.Create(name, Now));
}
}
79 changes: 79 additions & 0 deletions tests/Identity.UnitTests/UserTests.cs
Original file line number Diff line number Diff line change
@@ -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<DomainException>(() => CreateValidUser(email));
}

[Fact]
public void Create_with_empty_organization_throws()
{
Assert.Throws<DomainException>(() =>
User.Create(Guid.Empty, "a@b.com", "Ali", "hash", UserRole.Agent, Now));
}

[Fact]
public void Create_with_empty_password_hash_throws()
{
Assert.Throws<DomainException>(() =>
User.Create(OrgId, "a@b.com", "Ali", " ", UserRole.Agent, Now));
}

[Fact]
public void Create_with_undefined_role_throws()
{
Assert.Throws<DomainException>(() =>
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);
}
}