mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 07:11:10 +00:00
Adds authorization module tests
This commit is contained in:
1 parent
718e1c49cf
commit
0114ff13e8
125 files changed
+10411
-203
No files matched your search
+62
@@ -0,0 +1,62 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using AutoFixture;
|
||||
using Bogus;
|
||||
using Lumina.Application.Common.DataAccess.Entities.Authorization;
|
||||
using Lumina.Domain.Common.Enums.Authorization;
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.DataAccess.UnitTests.Core.Repositories.Authorization.Fixtures;
|
||||
|
||||
/// <summary>
|
||||
/// Fixture class for the <see cref="PermissionEntity"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class PermissionEntityFixture
|
||||
{
|
||||
private readonly Fixture _fixture;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="PermissionEntityFixture"/> class.
|
||||
/// </summary>
|
||||
public PermissionEntityFixture()
|
||||
{
|
||||
_fixture = new Fixture();
|
||||
ConfigureCustomTypes();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a random valid <see cref="PermissionEntity"/>.
|
||||
/// </summary>
|
||||
/// <returns>The created <see cref="PermissionEntity"/>.</returns>
|
||||
public PermissionEntity CreatePermissionModel()
|
||||
{
|
||||
return new Faker<PermissionEntity>()
|
||||
.RuleFor(x => x.Id, f => f.Random.Guid())
|
||||
.RuleFor(x => x.PermissionName, f => f.PickRandom<AuthorizationPermission>())
|
||||
.RuleFor(x => x.RolePermissions, [])
|
||||
.RuleFor(x => x.UserPermissions, [])
|
||||
.RuleFor(x => x.CreatedOnUtc, f => f.Date.Past())
|
||||
.RuleFor(x => x.CreatedBy, f => f.Random.Guid())
|
||||
.RuleFor(x => x.UpdatedOnUtc, f => f.Date.Recent())
|
||||
.RuleFor(x => x.UpdatedBy, f => f.Random.Guid())
|
||||
.Generate();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Configures custom type generation rules for the AutoFixture instance.
|
||||
/// </summary>
|
||||
private void ConfigureCustomTypes()
|
||||
{
|
||||
_fixture.Register(() => new PermissionEntity
|
||||
{
|
||||
Id = _fixture.Create<Guid>(),
|
||||
PermissionName = _fixture.Create<AuthorizationPermission>(),
|
||||
CreatedOnUtc = _fixture.Create<DateTime>(),
|
||||
CreatedBy = _fixture.Create<Guid>(),
|
||||
UpdatedOnUtc = _fixture.Create<DateTime>(),
|
||||
UpdatedBy = _fixture.Create<Guid>()
|
||||
});
|
||||
}
|
||||
}
|
||||
+111
@@ -0,0 +1,111 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using AutoFixture;
|
||||
using Bogus;
|
||||
using Lumina.Application.Common.DataAccess.Entities.Authorization;
|
||||
using Lumina.Domain.Common.Enums.Authorization;
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.DataAccess.UnitTests.Core.Repositories.Authorization.Fixtures;
|
||||
|
||||
/// <summary>
|
||||
/// Fixture class for the <see cref="RolePermissionEntity"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class RolePermissionEntityFixture
|
||||
{
|
||||
private readonly Fixture _fixture;
|
||||
private readonly Faker _faker;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="RolePermissionEntityFixture"/> class.
|
||||
/// </summary>
|
||||
public RolePermissionEntityFixture()
|
||||
{
|
||||
_fixture = new Fixture();
|
||||
_faker = new Faker();
|
||||
ConfigureCustomTypes();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a random valid <see cref="RolePermissionEntity"/>.
|
||||
/// </summary>
|
||||
/// <returns>The created <see cref="RolePermissionEntity"/>.</returns>
|
||||
public RolePermissionEntity CreateRolePermissionModel()
|
||||
{
|
||||
PermissionEntity permission = new()
|
||||
{
|
||||
Id = _faker.Random.Guid(),
|
||||
PermissionName = _faker.PickRandom<AuthorizationPermission>(),
|
||||
CreatedOnUtc = _faker.Date.Past(),
|
||||
CreatedBy = _faker.Random.Guid(),
|
||||
UpdatedOnUtc = _faker.Date.Recent(),
|
||||
UpdatedBy = _faker.Random.Guid()
|
||||
};
|
||||
|
||||
RoleEntity role = new()
|
||||
{
|
||||
Id = _faker.Random.Guid(),
|
||||
RoleName = _faker.Random.String2(10),
|
||||
CreatedOnUtc = _faker.Date.Past(),
|
||||
CreatedBy = _faker.Random.Guid(),
|
||||
UpdatedOnUtc = _faker.Date.Recent(),
|
||||
UpdatedBy = _faker.Random.Guid()
|
||||
};
|
||||
|
||||
return new Faker<RolePermissionEntity>()
|
||||
.CustomInstantiator(f => new RolePermissionEntity
|
||||
{
|
||||
Id = f.Random.Guid(),
|
||||
RoleId = role.Id,
|
||||
Role = role,
|
||||
PermissionId = permission.Id,
|
||||
Permission = permission,
|
||||
CreatedOnUtc = f.Date.Past(),
|
||||
CreatedBy = f.Random.Guid(),
|
||||
UpdatedOnUtc = f.Date.Recent(),
|
||||
UpdatedBy = f.Random.Guid()
|
||||
})
|
||||
.Generate();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Configures custom type generation rules for the AutoFixture instance.
|
||||
/// </summary>
|
||||
private void ConfigureCustomTypes()
|
||||
{
|
||||
_fixture.Register(() => new PermissionEntity
|
||||
{
|
||||
Id = _fixture.Create<Guid>(),
|
||||
PermissionName = _fixture.Create<AuthorizationPermission>(),
|
||||
CreatedOnUtc = _fixture.Create<DateTime>(),
|
||||
CreatedBy = _fixture.Create<Guid>(),
|
||||
UpdatedOnUtc = _fixture.Create<DateTime>(),
|
||||
UpdatedBy = _fixture.Create<Guid>()
|
||||
});
|
||||
|
||||
_fixture.Register(() => new RoleEntity
|
||||
{
|
||||
Id = _fixture.Create<Guid>(),
|
||||
RoleName = _fixture.Create<string>(),
|
||||
CreatedOnUtc = _fixture.Create<DateTime>(),
|
||||
CreatedBy = _fixture.Create<Guid>(),
|
||||
UpdatedOnUtc = _fixture.Create<DateTime>(),
|
||||
UpdatedBy = _fixture.Create<Guid>()
|
||||
});
|
||||
|
||||
_fixture.Register(() => new RolePermissionEntity
|
||||
{
|
||||
Id = _fixture.Create<Guid>(),
|
||||
RoleId = _fixture.Create<Guid>(),
|
||||
Role = _fixture.Create<RoleEntity>(),
|
||||
PermissionId = _fixture.Create<Guid>(),
|
||||
Permission = _fixture.Create<PermissionEntity>(),
|
||||
CreatedOnUtc = _fixture.Create<DateTime>(),
|
||||
CreatedBy = _fixture.Create<Guid>(),
|
||||
UpdatedOnUtc = _fixture.Create<DateTime>(),
|
||||
UpdatedBy = _fixture.Create<Guid>()
|
||||
});
|
||||
}
|
||||
}
|
||||
+71
@@ -0,0 +1,71 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using AutoFixture;
|
||||
using Bogus;
|
||||
using Lumina.Application.Common.DataAccess.Entities.Authorization;
|
||||
using Lumina.Application.Common.DataAccess.Entities.UsersManagement;
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.DataAccess.UnitTests.Core.Repositories.Authorization.Fixtures;
|
||||
|
||||
/// <summary>
|
||||
/// Fixture class for the <see cref="UserPermissionEntity"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class UserPermissionEntityFixture
|
||||
{
|
||||
private readonly Fixture _fixture;
|
||||
private readonly Faker _faker;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="UserPermissionEntityFixture"/> class.
|
||||
/// </summary>
|
||||
public UserPermissionEntityFixture()
|
||||
{
|
||||
_fixture = new Fixture();
|
||||
_faker = new Faker();
|
||||
ConfigureCustomTypes();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a random valid <see cref="UserPermissionEntity"/>.
|
||||
/// </summary>
|
||||
/// <returns>The created <see cref="UserPermissionEntity"/>.</returns>
|
||||
public UserPermissionEntity CreateUserPermissionModel(UserEntity user, PermissionEntity permission)
|
||||
{
|
||||
return new Faker<UserPermissionEntity>()
|
||||
.CustomInstantiator(f => new UserPermissionEntity
|
||||
{
|
||||
Id = f.Random.Guid(),
|
||||
UserId = user.Id,
|
||||
User = user,
|
||||
PermissionId = permission.Id,
|
||||
Permission = permission,
|
||||
CreatedOnUtc = f.Date.Past(),
|
||||
CreatedBy = user.Id,
|
||||
UpdatedOnUtc = f.Random.Bool() ? f.Date.Recent() : null,
|
||||
UpdatedBy = f.Random.Bool() ? user.Id : null
|
||||
})
|
||||
.Generate();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Configures custom type generation rules for the AutoFixture instance.
|
||||
/// </summary>
|
||||
private void ConfigureCustomTypes()
|
||||
{
|
||||
_fixture.Register(() => new UserPermissionEntity
|
||||
{
|
||||
Id = _fixture.Create<Guid>(),
|
||||
UserId = _fixture.Create<Guid>(),
|
||||
User = _fixture.Create<UserEntity>(),
|
||||
PermissionId = _fixture.Create<Guid>(),
|
||||
Permission = _fixture.Create<PermissionEntity>(),
|
||||
CreatedOnUtc = _fixture.Create<DateTime>(),
|
||||
CreatedBy = _fixture.Create<Guid>(),
|
||||
UpdatedOnUtc = _fixture.Create<DateTime>(),
|
||||
UpdatedBy = _fixture.Create<Guid>()
|
||||
});
|
||||
}
|
||||
}
|
||||
+91
@@ -0,0 +1,91 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using AutoFixture;
|
||||
using Bogus;
|
||||
using Lumina.Application.Common.DataAccess.Entities.Authorization;
|
||||
using Lumina.Application.Common.DataAccess.Entities.UsersManagement;
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.DataAccess.UnitTests.Core.Repositories.Authorization.Fixtures;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Fixture class for the <see cref="UserRoleEntity"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class UserRoleEntityFixture
|
||||
{
|
||||
private readonly Fixture _fixture;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="UserRoleEntityFixture"/> class.
|
||||
/// </summary>
|
||||
public UserRoleEntityFixture()
|
||||
{
|
||||
_fixture = new Fixture();
|
||||
ConfigureCustomTypes();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a random valid <see cref="UserRoleEntity"/>.
|
||||
/// </summary>
|
||||
/// <returns>The created <see cref="UserRoleEntity"/>.</returns>
|
||||
public UserRoleEntity CreateUserRoleModel()
|
||||
{
|
||||
return new Faker<UserRoleEntity>()
|
||||
.CustomInstantiator(f => new UserRoleEntity
|
||||
{
|
||||
Id = f.Random.Guid(),
|
||||
UserId = f.Random.Guid(),
|
||||
User = new()
|
||||
{
|
||||
Id = f.Random.Guid(),
|
||||
Username = f.Internet.UserName(),
|
||||
Password = f.Internet.Password(),
|
||||
Libraries = [],
|
||||
UserRole = null,
|
||||
UserPermissions = [],
|
||||
CreatedOnUtc = f.Date.Past(),
|
||||
CreatedBy = f.Random.Guid()
|
||||
},
|
||||
RoleId = f.Random.Guid(),
|
||||
Role = new()
|
||||
{
|
||||
Id = f.Random.Guid(),
|
||||
RoleName = f.Random.String2(f.Random.Number(1, 50)),
|
||||
CreatedOnUtc = f.Date.Past(),
|
||||
CreatedBy = f.Random.Guid()
|
||||
},
|
||||
CreatedOnUtc = f.Date.Past(),
|
||||
CreatedBy = f.Random.Guid()
|
||||
})
|
||||
.Generate();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Configures custom type generation rules for the AutoFixture instance.
|
||||
/// </summary>
|
||||
private void ConfigureCustomTypes()
|
||||
{
|
||||
_fixture.Register(() => new UserEntity
|
||||
{
|
||||
Id = _fixture.Create<Guid>(),
|
||||
Username = _fixture.Create<string>(),
|
||||
Password = _fixture.Create<string>(),
|
||||
Libraries = [],
|
||||
UserRole = null,
|
||||
UserPermissions = [],
|
||||
CreatedOnUtc = _fixture.Create<DateTime>(),
|
||||
CreatedBy = _fixture.Create<Guid>()
|
||||
});
|
||||
|
||||
_fixture.Register(() => new RoleEntity
|
||||
{
|
||||
Id = _fixture.Create<Guid>(),
|
||||
RoleName = _fixture.Create<string>(),
|
||||
CreatedOnUtc = _fixture.Create<DateTime>(),
|
||||
CreatedBy = _fixture.Create<Guid>()
|
||||
});
|
||||
}
|
||||
}
|
||||
+153
@@ -0,0 +1,153 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using EntityFrameworkCore.Testing.NSubstitute;
|
||||
using ErrorOr;
|
||||
using FluentAssertions;
|
||||
using Lumina.Application.Common.DataAccess.Entities.Authorization;
|
||||
using Lumina.Application.Common.Errors;
|
||||
using Lumina.DataAccess.Core.Repositories.Authorization;
|
||||
using Lumina.DataAccess.Core.UoW;
|
||||
using Lumina.DataAccess.UnitTests.Core.Repositories.Authorization.Fixtures;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.ChangeTracking;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.DataAccess.UnitTests.Core.Repositories.Authorization;
|
||||
|
||||
/// <summary>
|
||||
/// Contains unit tests for the <see cref="PermissionRepository"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class PermissionRepositoryTests
|
||||
{
|
||||
private readonly LuminaDbContext _mockContext;
|
||||
private readonly PermissionRepository _sut;
|
||||
private readonly PermissionEntityFixture _permissionEntityFixture;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="PermissionRepositoryTests"/> class.
|
||||
/// </summary>
|
||||
public PermissionRepositoryTests()
|
||||
{
|
||||
_mockContext = Create.MockedDbContextFor<LuminaDbContext>();
|
||||
_sut = new PermissionRepository(_mockContext);
|
||||
_permissionEntityFixture = new PermissionEntityFixture();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task InsertAsync_WhenPermissionDoesNotExist_ShouldAddPermissionToContextAndReturnCreated()
|
||||
{
|
||||
// Arrange
|
||||
PermissionEntity permissionModel = _permissionEntityFixture.CreatePermissionModel();
|
||||
|
||||
// Act
|
||||
ErrorOr<Created> result = await _sut.InsertAsync(permissionModel, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
result.IsError.Should().BeFalse();
|
||||
result.Value.Should().Be(Result.Created);
|
||||
|
||||
// Check if the permission was added to the context's ChangeTracker
|
||||
EntityEntry<PermissionEntity>? addedPermission = _mockContext.ChangeTracker.Entries<PermissionEntity>()
|
||||
.FirstOrDefault(e => e.State == EntityState.Added && e.Entity.Id == permissionModel.Id);
|
||||
addedPermission.Should().NotBeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task InsertAsync_WhenPermissionAlreadyExists_ShouldReturnError()
|
||||
{
|
||||
// Arrange
|
||||
PermissionEntity permissionModel = _permissionEntityFixture.CreatePermissionModel();
|
||||
|
||||
_mockContext.Permissions.Add(permissionModel);
|
||||
await _mockContext.SaveChangesAsync();
|
||||
|
||||
// Act
|
||||
ErrorOr<Created> result = await _sut.InsertAsync(permissionModel, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
result.IsError.Should().BeTrue();
|
||||
result.FirstError.Should().Be(Errors.Authorization.PermissionAlreadyExists);
|
||||
_mockContext.ChangeTracker.Entries<PermissionEntity>().Should().HaveCount(1);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetAllAsync_WhenPermissionsExist_ShouldReturnAllPermissions()
|
||||
{
|
||||
// Arrange
|
||||
List<PermissionEntity> permissions =
|
||||
[
|
||||
_permissionEntityFixture.CreatePermissionModel(),
|
||||
_permissionEntityFixture.CreatePermissionModel(),
|
||||
_permissionEntityFixture.CreatePermissionModel()
|
||||
];
|
||||
_mockContext.Permissions.AddRange(permissions);
|
||||
await _mockContext.SaveChangesAsync();
|
||||
|
||||
// Act
|
||||
ErrorOr<IEnumerable<PermissionEntity>> result = await _sut.GetAllAsync(CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
result.IsError.Should().BeFalse();
|
||||
result.Value.Should().NotBeNull();
|
||||
result.Value.Should().HaveCount(3);
|
||||
result.Value.Should().BeEquivalentTo(permissions);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetAllAsync_WhenNoPermissionsExist_ShouldReturnEmptyList()
|
||||
{
|
||||
// Act
|
||||
ErrorOr<IEnumerable<PermissionEntity>> result = await _sut.GetAllAsync(CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
result.IsError.Should().BeFalse();
|
||||
result.Value.Should().NotBeNull();
|
||||
result.Value.Should().BeEmpty();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetByIdsAsync_WhenPermissionsExist_ShouldReturnMatchingPermissions()
|
||||
{
|
||||
// Arrange
|
||||
List<PermissionEntity> permissions =
|
||||
[
|
||||
_permissionEntityFixture.CreatePermissionModel(),
|
||||
_permissionEntityFixture.CreatePermissionModel(),
|
||||
_permissionEntityFixture.CreatePermissionModel()
|
||||
];
|
||||
_mockContext.Permissions.AddRange(permissions);
|
||||
await _mockContext.SaveChangesAsync();
|
||||
|
||||
List<Guid> requestedIds = permissions.Take(2).Select(p => p.Id).ToList();
|
||||
|
||||
// Act
|
||||
ErrorOr<IEnumerable<PermissionEntity>> result = await _sut.GetByIdsAsync(requestedIds, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
result.IsError.Should().BeFalse();
|
||||
result.Value.Should().NotBeNull();
|
||||
result.Value.Should().HaveCount(2);
|
||||
result.Value.Select(p => p.Id).Should().BeEquivalentTo(requestedIds);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetByIdsAsync_WhenNoPermissionsMatch_ShouldReturnEmptyList()
|
||||
{
|
||||
// Arrange
|
||||
List<Guid> requestedIds = [Guid.NewGuid(), Guid.NewGuid()];
|
||||
|
||||
// Act
|
||||
ErrorOr<IEnumerable<PermissionEntity>> result = await _sut.GetByIdsAsync(requestedIds, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
result.IsError.Should().BeFalse();
|
||||
result.Value.Should().NotBeNull();
|
||||
result.Value.Should().BeEmpty();
|
||||
}
|
||||
}
|
||||
+57
@@ -0,0 +1,57 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using EntityFrameworkCore.Testing.NSubstitute;
|
||||
using ErrorOr;
|
||||
using FluentAssertions;
|
||||
using Lumina.Application.Common.DataAccess.Entities.Authorization;
|
||||
using Lumina.DataAccess.Core.Repositories.Authorization;
|
||||
using Lumina.DataAccess.Core.UoW;
|
||||
using Lumina.DataAccess.UnitTests.Core.Repositories.Authorization.Fixtures;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.ChangeTracking;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.DataAccess.UnitTests.Core.Repositories.Authorization;
|
||||
|
||||
/// <summary>
|
||||
/// Contains unit tests for the <see cref="RolePermissionRepository"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class RolePermissionRepositoryTests
|
||||
{
|
||||
private readonly LuminaDbContext _mockContext;
|
||||
private readonly RolePermissionRepository _sut;
|
||||
private readonly RolePermissionEntityFixture _rolePermissionEntityFixture;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="RolePermissionRepositoryTests"/> class.
|
||||
/// </summary>
|
||||
public RolePermissionRepositoryTests()
|
||||
{
|
||||
_mockContext = Create.MockedDbContextFor<LuminaDbContext>();
|
||||
_sut = new RolePermissionRepository(_mockContext);
|
||||
_rolePermissionEntityFixture = new RolePermissionEntityFixture();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task InsertAsync_WhenCalled_ShouldAddRolePermissionToContextAndReturnCreated()
|
||||
{
|
||||
// Arrange
|
||||
RolePermissionEntity rolePermissionModel = _rolePermissionEntityFixture.CreateRolePermissionModel();
|
||||
|
||||
// Act
|
||||
ErrorOr<Created> result = await _sut.InsertAsync(rolePermissionModel, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
result.IsError.Should().BeFalse();
|
||||
result.Value.Should().Be(Result.Created);
|
||||
|
||||
// check if the role permission was added to the context's ChangeTracker
|
||||
EntityEntry<RolePermissionEntity>? addedRolePermission = _mockContext.ChangeTracker.Entries<RolePermissionEntity>()
|
||||
.FirstOrDefault(e => e.State == EntityState.Added && e.Entity.Id == rolePermissionModel.Id);
|
||||
addedRolePermission.Should().NotBeNull();
|
||||
}
|
||||
}
|
||||
+412
@@ -0,0 +1,412 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using EntityFrameworkCore.Testing.NSubstitute;
|
||||
using ErrorOr;
|
||||
using FluentAssertions;
|
||||
using Lumina.Application.Common.DataAccess.Entities.Authorization;
|
||||
using Lumina.Application.Common.Errors;
|
||||
using Lumina.DataAccess.Core.Repositories.Authorization;
|
||||
using Lumina.DataAccess.Core.UoW;
|
||||
using Lumina.DataAccess.UnitTests.Core.Repositories.Authorization.Fixtures;
|
||||
using Lumina.Domain.Common.Enums.Authorization;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.ChangeTracking;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.DataAccess.UnitTests.Core.Repositories.Authorization;
|
||||
|
||||
/// <summary>
|
||||
/// Contains unit tests for the <see cref="RoleRepository"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class RoleRepositoryTests
|
||||
{
|
||||
private readonly LuminaDbContext _mockContext;
|
||||
private readonly RoleRepository _sut;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="RoleRepositoryTests"/> class.
|
||||
/// </summary>
|
||||
public RoleRepositoryTests()
|
||||
{
|
||||
_mockContext = Create.MockedDbContextFor<LuminaDbContext>();
|
||||
_sut = new RoleRepository(_mockContext);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task InsertAsync_WhenRoleDoesNotExist_ShouldAddRoleToContextAndReturnCreated()
|
||||
{
|
||||
// Arrange
|
||||
RoleEntity role = new()
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
RoleName = "Admin",
|
||||
CreatedOnUtc = DateTime.UtcNow,
|
||||
CreatedBy = Guid.NewGuid()
|
||||
};
|
||||
|
||||
// Act
|
||||
ErrorOr<Created> result = await _sut.InsertAsync(role, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
result.IsError.Should().BeFalse();
|
||||
result.Value.Should().Be(Result.Created);
|
||||
|
||||
// Check if the role was added to the context's ChangeTracker
|
||||
EntityEntry<RoleEntity>? addedRole = _mockContext.ChangeTracker.Entries<RoleEntity>()
|
||||
.FirstOrDefault(e => e.State == EntityState.Added && e.Entity.Id == role.Id);
|
||||
addedRole.Should().NotBeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task InsertAsync_WhenRoleWithSameIdExists_ShouldReturnError()
|
||||
{
|
||||
// Arrange
|
||||
RoleEntity existingRole = new()
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
RoleName = "Admin",
|
||||
CreatedOnUtc = DateTime.UtcNow,
|
||||
CreatedBy = Guid.NewGuid()
|
||||
};
|
||||
|
||||
_mockContext.Roles.Add(existingRole);
|
||||
await _mockContext.SaveChangesAsync();
|
||||
|
||||
RoleEntity newRole = new()
|
||||
{
|
||||
Id = existingRole.Id,
|
||||
RoleName = "SuperAdmin",
|
||||
CreatedOnUtc = DateTime.UtcNow,
|
||||
CreatedBy = Guid.NewGuid()
|
||||
};
|
||||
|
||||
// Act
|
||||
ErrorOr<Created> result = await _sut.InsertAsync(newRole, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
result.IsError.Should().BeTrue();
|
||||
result.FirstError.Should().Be(Errors.Authorization.RoleAlreadyExists);
|
||||
_mockContext.ChangeTracker.Entries<RoleEntity>().Should().HaveCount(1);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task InsertAsync_WhenRoleWithSameNameExists_ShouldReturnError()
|
||||
{
|
||||
// Arrange
|
||||
RoleEntity existingRole = new()
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
RoleName = "Admin",
|
||||
CreatedOnUtc = DateTime.UtcNow,
|
||||
CreatedBy = Guid.NewGuid()
|
||||
};
|
||||
|
||||
_mockContext.Roles.Add(existingRole);
|
||||
await _mockContext.SaveChangesAsync();
|
||||
|
||||
RoleEntity newRole = new()
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
RoleName = "Admin",
|
||||
CreatedOnUtc = DateTime.UtcNow,
|
||||
CreatedBy = Guid.NewGuid()
|
||||
};
|
||||
|
||||
// Act
|
||||
ErrorOr<Created> result = await _sut.InsertAsync(newRole, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
result.IsError.Should().BeTrue();
|
||||
result.FirstError.Should().Be(Errors.Authorization.RoleAlreadyExists);
|
||||
_mockContext.ChangeTracker.Entries<RoleEntity>().Should().HaveCount(1);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetAllAsync_WhenRolesExist_ShouldReturnAllRoles()
|
||||
{
|
||||
// Arrange
|
||||
List<RoleEntity> roles =
|
||||
[
|
||||
new()
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
RoleName = "Admin",
|
||||
CreatedOnUtc = DateTime.UtcNow,
|
||||
CreatedBy = Guid.NewGuid()
|
||||
},
|
||||
new()
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
RoleName = "User",
|
||||
CreatedOnUtc = DateTime.UtcNow,
|
||||
CreatedBy = Guid.NewGuid()
|
||||
}
|
||||
];
|
||||
|
||||
_mockContext.Roles.AddRange(roles);
|
||||
await _mockContext.SaveChangesAsync();
|
||||
|
||||
// Act
|
||||
ErrorOr<IEnumerable<RoleEntity>> result = await _sut.GetAllAsync(CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
result.IsError.Should().BeFalse();
|
||||
result.Value.Should().NotBeNull();
|
||||
result.Value.Should().HaveCount(2);
|
||||
result.Value.Should().BeEquivalentTo(roles);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetAllAsync_WhenNoRolesExist_ShouldReturnEmptyList()
|
||||
{
|
||||
// Act
|
||||
ErrorOr<IEnumerable<RoleEntity>> result = await _sut.GetAllAsync(CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
result.IsError.Should().BeFalse();
|
||||
result.Value.Should().NotBeNull();
|
||||
result.Value.Should().BeEmpty();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetByNameAsync_WhenRoleExists_ShouldReturnRoleWithPermissions()
|
||||
{
|
||||
// Arrange
|
||||
RoleEntity role = new()
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
RoleName = "Admin",
|
||||
CreatedOnUtc = DateTime.UtcNow,
|
||||
CreatedBy = Guid.NewGuid()
|
||||
};
|
||||
|
||||
_mockContext.Roles.Add(role);
|
||||
await _mockContext.SaveChangesAsync();
|
||||
|
||||
// Act
|
||||
ErrorOr<RoleEntity?> result = await _sut.GetByNameAsync("Admin", CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
result.IsError.Should().BeFalse();
|
||||
result.Value.Should().NotBeNull();
|
||||
result.Value.Should().BeEquivalentTo(role);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetByNameAsync_WhenRoleDoesNotExist_ShouldReturnNull()
|
||||
{
|
||||
// Act
|
||||
ErrorOr<RoleEntity?> result = await _sut.GetByNameAsync("NonExistentRole", CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
result.IsError.Should().BeFalse();
|
||||
result.Value.Should().BeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetByIdAsync_WhenRoleExists_ShouldReturnRoleWithPermissions()
|
||||
{
|
||||
// Arrange
|
||||
RolePermissionEntityFixture rolePermissionFixture = new();
|
||||
RolePermissionEntity rolePermission = rolePermissionFixture.CreateRolePermissionModel();
|
||||
RoleEntity role = rolePermission.Role;
|
||||
|
||||
_mockContext.Roles.Add(role);
|
||||
await _mockContext.SaveChangesAsync();
|
||||
|
||||
// Act
|
||||
ErrorOr<RoleEntity?> result = await _sut.GetByIdAsync(role.Id, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
result.IsError.Should().BeFalse();
|
||||
result.Value.Should().NotBeNull();
|
||||
result.Value.Should().BeEquivalentTo(role, options => options
|
||||
.Including(x => x.Id)
|
||||
.Including(x => x.RoleName)
|
||||
.Including(x => x.CreatedOnUtc)
|
||||
.Including(x => x.CreatedBy));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetByIdAsync_WhenRoleDoesNotExist_ShouldReturnNull()
|
||||
{
|
||||
// Arrange
|
||||
Guid nonExistentId = Guid.NewGuid();
|
||||
|
||||
// Act
|
||||
ErrorOr<RoleEntity?> result = await _sut.GetByIdAsync(nonExistentId, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
result.IsError.Should().BeFalse();
|
||||
result.Value.Should().BeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task UpdateAsync_WhenRoleExists_ShouldUpdateRoleAndReturnUpdated()
|
||||
{
|
||||
// Arrange
|
||||
RoleEntity existingRole = new()
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
RoleName = "Admin",
|
||||
CreatedOnUtc = DateTime.UtcNow,
|
||||
CreatedBy = Guid.NewGuid()
|
||||
};
|
||||
|
||||
_mockContext.Roles.Add(existingRole);
|
||||
await _mockContext.SaveChangesAsync();
|
||||
|
||||
RoleEntity updatedRole = new()
|
||||
{
|
||||
Id = existingRole.Id,
|
||||
RoleName = "SuperAdmin",
|
||||
CreatedOnUtc = existingRole.CreatedOnUtc,
|
||||
CreatedBy = existingRole.CreatedBy,
|
||||
UpdatedOnUtc = DateTime.UtcNow,
|
||||
UpdatedBy = Guid.NewGuid()
|
||||
};
|
||||
|
||||
// Act
|
||||
ErrorOr<Updated> result = await _sut.UpdateAsync(updatedRole, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
result.IsError.Should().BeFalse();
|
||||
result.Value.Should().Be(Result.Updated);
|
||||
|
||||
EntityEntry<RoleEntity>? updatedEntry = _mockContext.ChangeTracker.Entries<RoleEntity>()
|
||||
.FirstOrDefault(e => e.Entity.Id == existingRole.Id);
|
||||
updatedEntry.Should().NotBeNull();
|
||||
updatedEntry!.Entity.RoleName.Should().Be("SuperAdmin");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task UpdateAsync_WhenRoleDoesNotExist_ShouldReturnError()
|
||||
{
|
||||
// Arrange
|
||||
RoleEntity nonExistentRole = new()
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
RoleName = "Admin",
|
||||
CreatedOnUtc = DateTime.UtcNow,
|
||||
CreatedBy = Guid.NewGuid()
|
||||
};
|
||||
|
||||
// Act
|
||||
ErrorOr<Updated> result = await _sut.UpdateAsync(nonExistentRole, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
result.IsError.Should().BeTrue();
|
||||
result.FirstError.Should().Be(Errors.Authorization.RoleNotFound);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task UpdateAsync_WhenRoleHasPermissions_ShouldUpdatePermissionsAndReturnUpdated()
|
||||
{
|
||||
// Arrange
|
||||
RolePermissionEntityFixture rolePermissionFixture = new();
|
||||
RolePermissionEntity rolePermission = rolePermissionFixture.CreateRolePermissionModel();
|
||||
RoleEntity existingRole = rolePermission.Role;
|
||||
existingRole.RolePermissions = [rolePermission];
|
||||
|
||||
_mockContext.Roles.Add(existingRole);
|
||||
await _mockContext.SaveChangesAsync();
|
||||
|
||||
RolePermissionEntity newRolePermission = rolePermissionFixture.CreateRolePermissionModel();
|
||||
RoleEntity updatedRole = new()
|
||||
{
|
||||
Id = existingRole.Id,
|
||||
RoleName = existingRole.RoleName,
|
||||
RolePermissions = [newRolePermission],
|
||||
CreatedOnUtc = existingRole.CreatedOnUtc,
|
||||
CreatedBy = existingRole.CreatedBy,
|
||||
UpdatedOnUtc = DateTime.UtcNow,
|
||||
UpdatedBy = Guid.NewGuid()
|
||||
};
|
||||
|
||||
// Act
|
||||
ErrorOr<Updated> result = await _sut.UpdateAsync(updatedRole, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
result.IsError.Should().BeFalse();
|
||||
result.Value.Should().Be(Result.Updated);
|
||||
|
||||
EntityEntry<RoleEntity>? updatedEntry = _mockContext.ChangeTracker.Entries<RoleEntity>()
|
||||
.FirstOrDefault(e => e.Entity.Id == existingRole.Id);
|
||||
updatedEntry.Should().NotBeNull();
|
||||
updatedEntry!.Entity.RolePermissions.Should().HaveCount(1);
|
||||
updatedEntry.Entity.RolePermissions.First().Should().BeEquivalentTo(newRolePermission);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task DeleteByIdAsync_WhenRoleExists_ShouldDeleteRoleAndReturnDeleted()
|
||||
{
|
||||
// Arrange
|
||||
RoleEntity existingRole = new()
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
RoleName = "Admin",
|
||||
CreatedOnUtc = DateTime.UtcNow,
|
||||
CreatedBy = Guid.NewGuid()
|
||||
};
|
||||
|
||||
_mockContext.Roles.Add(existingRole);
|
||||
await _mockContext.SaveChangesAsync();
|
||||
|
||||
// Act
|
||||
ErrorOr<Deleted> result = await _sut.DeleteByIdAsync(existingRole.Id, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
result.IsError.Should().BeFalse();
|
||||
result.Value.Should().Be(Result.Deleted);
|
||||
|
||||
EntityEntry<RoleEntity>? deletedEntry = _mockContext.ChangeTracker.Entries<RoleEntity>()
|
||||
.FirstOrDefault(e => e.Entity.Id == existingRole.Id);
|
||||
deletedEntry.Should().NotBeNull();
|
||||
deletedEntry!.State.Should().Be(EntityState.Deleted);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task DeleteByIdAsync_WhenRoleDoesNotExist_ShouldReturnError()
|
||||
{
|
||||
// Arrange
|
||||
Guid nonExistentId = Guid.NewGuid();
|
||||
|
||||
// Act
|
||||
ErrorOr<Deleted> result = await _sut.DeleteByIdAsync(nonExistentId, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
result.IsError.Should().BeTrue();
|
||||
result.FirstError.Should().Be(Errors.Authorization.RoleNotFound);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task DeleteByIdAsync_WhenRoleHasPermissions_ShouldDeleteRoleWithPermissions()
|
||||
{
|
||||
// Arrange
|
||||
RolePermissionEntityFixture rolePermissionFixture = new();
|
||||
RolePermissionEntity rolePermission = rolePermissionFixture.CreateRolePermissionModel();
|
||||
RoleEntity existingRole = rolePermission.Role;
|
||||
existingRole.RolePermissions = [rolePermission];
|
||||
|
||||
_mockContext.Roles.Add(existingRole);
|
||||
await _mockContext.SaveChangesAsync();
|
||||
|
||||
// Act
|
||||
ErrorOr<Deleted> result = await _sut.DeleteByIdAsync(existingRole.Id, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
result.IsError.Should().BeFalse();
|
||||
result.Value.Should().Be(Result.Deleted);
|
||||
|
||||
EntityEntry<RoleEntity>? deletedEntry = _mockContext.ChangeTracker.Entries<RoleEntity>()
|
||||
.FirstOrDefault(e => e.Entity.Id == existingRole.Id);
|
||||
deletedEntry.Should().NotBeNull();
|
||||
deletedEntry!.State.Should().Be(EntityState.Deleted);
|
||||
}
|
||||
}
|
||||
+61
@@ -0,0 +1,61 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using EntityFrameworkCore.Testing.NSubstitute;
|
||||
using ErrorOr;
|
||||
using FluentAssertions;
|
||||
using Lumina.Application.Common.DataAccess.Entities.Authorization;
|
||||
using Lumina.Application.Common.Errors;
|
||||
using Lumina.DataAccess.Core.Repositories.Authorization;
|
||||
using Lumina.DataAccess.Core.UoW;
|
||||
using Lumina.DataAccess.UnitTests.Core.Repositories.Authorization.Fixtures;
|
||||
using Lumina.Domain.Common.Enums.Authorization;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.ChangeTracking;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.DataAccess.UnitTests.Core.Repositories.Authorization;
|
||||
|
||||
/// <summary>
|
||||
/// Contains unit tests for the <see cref="UserRoleRepository"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class UserRoleRepositoryTests
|
||||
{
|
||||
private readonly LuminaDbContext _mockContext;
|
||||
private readonly UserRoleRepository _sut;
|
||||
private readonly UserRoleEntityFixture _userRoleEntityFixture;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="UserRoleRepositoryTests"/> class.
|
||||
/// </summary>
|
||||
public UserRoleRepositoryTests()
|
||||
{
|
||||
_mockContext = Create.MockedDbContextFor<LuminaDbContext>();
|
||||
_sut = new UserRoleRepository(_mockContext);
|
||||
_userRoleEntityFixture = new UserRoleEntityFixture();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task InsertAsync_WhenUserRoleIsValid_ShouldAddToContextAndReturnCreated()
|
||||
{
|
||||
// Arrange
|
||||
UserRoleEntity userRole = _userRoleEntityFixture.CreateUserRoleModel();
|
||||
|
||||
// Act
|
||||
ErrorOr<Created> result = await _sut.InsertAsync(userRole, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
result.IsError.Should().BeFalse();
|
||||
result.Value.Should().Be(Result.Created);
|
||||
|
||||
EntityEntry<UserRoleEntity>? addedUserRole = _mockContext.ChangeTracker.Entries<UserRoleEntity>()
|
||||
.FirstOrDefault(e => e.State == EntityState.Added && e.Entity.Id == userRole.Id);
|
||||
addedUserRole.Should().NotBeNull();
|
||||
addedUserRole!.State.Should().Be(EntityState.Added);
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -81,8 +81,8 @@ public class BookEntityFixture
|
||||
.RuleFor(x => x.AppleBooksId, f => $"id{f.Random.Number(1, 999999)}")
|
||||
.RuleFor(x => x.ISBNs, f => CreateIsbns(f.Random.Number(1, 5)))
|
||||
.RuleFor(x => x.Ratings, f => CreateBookRatings(f.Random.Number(1, 5)))
|
||||
.RuleFor(x => x.Created, f => f.Date.Past())
|
||||
.RuleFor(x => x.Updated, f => f.Date.Recent())
|
||||
.RuleFor(x => x.CreatedOnUtc, f => f.Date.Past())
|
||||
.RuleFor(x => x.UpdatedOnUtc, f => f.Date.Recent())
|
||||
.Generate();
|
||||
}
|
||||
|
||||
|
||||
+8
-4
@@ -69,8 +69,10 @@ public class UserEntityFixture
|
||||
Title = f.Random.String2(f.Random.Number(1, 50)),
|
||||
LibraryType = f.PickRandom<LibraryType>(),
|
||||
ContentLocations = CreateContentLocations(f.Random.Number(1, 3)),
|
||||
Created = f.Date.Past(),
|
||||
Updated = f.Random.Bool() ? f.Date.Recent() : null
|
||||
CreatedOnUtc = f.Date.Past(),
|
||||
CreatedBy = userId,
|
||||
UpdatedOnUtc = f.Random.Bool() ? f.Date.Recent() : null,
|
||||
UpdatedBy = f.Random.Bool() ? userId : null,
|
||||
})
|
||||
.Generate(count);
|
||||
}
|
||||
@@ -102,8 +104,10 @@ public class UserEntityFixture
|
||||
Title = _fixture.Create<string>(),
|
||||
LibraryType = _fixture.Create<LibraryType>(),
|
||||
ContentLocations = [],
|
||||
Created = _fixture.Create<DateTime>(),
|
||||
Updated = _fixture.Create<DateTime>()
|
||||
CreatedOnUtc = _fixture.Create<DateTime>(),
|
||||
CreatedBy = _fixture.Create<Guid>(),
|
||||
UpdatedOnUtc = _fixture.Create<DateTime>(),
|
||||
UpdatedBy = _fixture.Create<Guid>()
|
||||
});
|
||||
|
||||
_fixture.Register(() => new LibraryContentLocationEntity
|
||||
|
||||
+326
@@ -2,9 +2,11 @@
|
||||
using EntityFrameworkCore.Testing.NSubstitute;
|
||||
using ErrorOr;
|
||||
using FluentAssertions;
|
||||
using Lumina.Application.Common.DataAccess.Entities.Authorization;
|
||||
using Lumina.Application.Common.DataAccess.Entities.UsersManagement;
|
||||
using Lumina.DataAccess.Core.Repositories.Users;
|
||||
using Lumina.DataAccess.Core.UoW;
|
||||
using Lumina.DataAccess.UnitTests.Core.Repositories.Authorization.Fixtures;
|
||||
using Lumina.DataAccess.UnitTests.Core.Repositories.Users.Fixtures;
|
||||
using Lumina.Domain.Common.Errors;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
@@ -28,6 +30,7 @@ public class UserRepositoryTests
|
||||
private readonly LuminaDbContext _mockContext;
|
||||
private readonly UserRepository _sut;
|
||||
private readonly UserEntityFixture _userEntityFixture;
|
||||
private readonly PermissionEntityFixture _permissionEntityFixture;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="UserRepositoryTests"/> class.
|
||||
@@ -37,6 +40,7 @@ public class UserRepositoryTests
|
||||
_mockContext = Create.MockedDbContextFor<LuminaDbContext>();
|
||||
_sut = new UserRepository(_mockContext);
|
||||
_userEntityFixture = new UserEntityFixture();
|
||||
_permissionEntityFixture = new PermissionEntityFixture();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -189,4 +193,326 @@ public class UserRepositoryTests
|
||||
result.IsError.Should().BeTrue();
|
||||
result.FirstError.Should().Be(Errors.Users.UserDoesNotExist);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetByIdAsync_WhenUserExists_ShouldReturnUserWithAllRelations()
|
||||
{
|
||||
// Arrange
|
||||
UserEntity userModel = _userEntityFixture.CreateUserModel();
|
||||
_mockContext.Users.Add(userModel);
|
||||
await _mockContext.SaveChangesAsync();
|
||||
|
||||
// Act
|
||||
ErrorOr<UserEntity?> result = await _sut.GetByIdAsync(userModel.Id, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
result.IsError.Should().BeFalse();
|
||||
result.Value.Should().NotBeNull();
|
||||
result.Value.Should().BeEquivalentTo(userModel, options => options
|
||||
.Including(x => x.Id)
|
||||
.Including(x => x.Username)
|
||||
.Including(x => x.Password)
|
||||
.Including(x => x.Libraries)
|
||||
.Including(x => x.UserPermissions)
|
||||
.Including(x => x.UserRole));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetByIdAsync_WhenUserDoesNotExist_ShouldReturnNull()
|
||||
{
|
||||
// Arrange
|
||||
Guid nonExistentId = Guid.NewGuid();
|
||||
|
||||
// Act
|
||||
ErrorOr<UserEntity?> result = await _sut.GetByIdAsync(nonExistentId, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
result.IsError.Should().BeFalse();
|
||||
result.Value.Should().BeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task UpdateAsync_WhenUserExistsWithPermissions_ShouldUpdatePermissionsAndReturnUpdated()
|
||||
{
|
||||
// Arrange
|
||||
UserEntity existingUser = _userEntityFixture.CreateUserModel();
|
||||
PermissionEntity oldPermission = _permissionEntityFixture.CreatePermissionModel();
|
||||
PermissionEntity newPermission = _permissionEntityFixture.CreatePermissionModel();
|
||||
|
||||
_mockContext.Users.Add(existingUser);
|
||||
_mockContext.Permissions.Add(oldPermission);
|
||||
_mockContext.Permissions.Add(newPermission);
|
||||
await _mockContext.SaveChangesAsync();
|
||||
|
||||
UserEntity updatedUser = new()
|
||||
{
|
||||
Id = existingUser.Id,
|
||||
Username = existingUser.Username,
|
||||
Password = "NewPassword",
|
||||
Libraries = [],
|
||||
UserRole = null,
|
||||
UserPermissions =
|
||||
[
|
||||
new()
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
UserId = existingUser.Id,
|
||||
User = existingUser,
|
||||
PermissionId = newPermission.Id,
|
||||
Permission = newPermission,
|
||||
CreatedOnUtc = DateTime.UtcNow,
|
||||
CreatedBy = existingUser.Id
|
||||
}
|
||||
],
|
||||
CreatedBy = existingUser.CreatedBy,
|
||||
CreatedOnUtc = existingUser.CreatedOnUtc,
|
||||
UpdatedOnUtc = DateTime.UtcNow
|
||||
};
|
||||
|
||||
// Act
|
||||
ErrorOr<Updated> result = await _sut.UpdateAsync(updatedUser, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
result.IsError.Should().BeFalse();
|
||||
result.Value.Should().Be(Result.Updated);
|
||||
|
||||
UserEntity? modifiedUser = await _mockContext.Users
|
||||
.Include(u => u.UserPermissions)
|
||||
.ThenInclude(up => up.Permission)
|
||||
.FirstOrDefaultAsync(u => u.Username == existingUser.Username);
|
||||
modifiedUser.Should().NotBeNull();
|
||||
modifiedUser!.UserPermissions.Should().HaveCount(1);
|
||||
modifiedUser.UserPermissions.First().Permission.PermissionName.Should().Be(newPermission.PermissionName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task UpdateAsync_WhenUserHasExistingPermissions_ShouldRemoveOldPermissionsAndAddNew()
|
||||
{
|
||||
// Arrange
|
||||
UserEntityFixture userFixture = new();
|
||||
PermissionEntityFixture permissionFixture = new();
|
||||
UserPermissionEntityFixture userPermissionFixture = new();
|
||||
|
||||
UserEntity existingUser = userFixture.CreateUserModel();
|
||||
PermissionEntity oldPermission = permissionFixture.CreatePermissionModel();
|
||||
PermissionEntity newPermission = permissionFixture.CreatePermissionModel();
|
||||
|
||||
UserEntity userWithPermissions = new()
|
||||
{
|
||||
Id = existingUser.Id,
|
||||
Username = existingUser.Username,
|
||||
Password = existingUser.Password,
|
||||
Libraries = existingUser.Libraries,
|
||||
UserRole = null,
|
||||
UserPermissions =
|
||||
[
|
||||
userPermissionFixture.CreateUserPermissionModel(existingUser, oldPermission)
|
||||
],
|
||||
CreatedOnUtc = existingUser.CreatedOnUtc,
|
||||
CreatedBy = existingUser.CreatedBy,
|
||||
UpdatedOnUtc = existingUser.UpdatedOnUtc,
|
||||
UpdatedBy = existingUser.UpdatedBy
|
||||
};
|
||||
|
||||
_mockContext.Users.Add(userWithPermissions);
|
||||
await _mockContext.SaveChangesAsync();
|
||||
|
||||
UserEntity updatedUser = new()
|
||||
{
|
||||
Id = existingUser.Id,
|
||||
Username = existingUser.Username,
|
||||
Password = existingUser.Password,
|
||||
Libraries = existingUser.Libraries,
|
||||
UserRole = null,
|
||||
UserPermissions =
|
||||
[
|
||||
userPermissionFixture.CreateUserPermissionModel(existingUser, newPermission)
|
||||
],
|
||||
CreatedBy = existingUser.CreatedBy,
|
||||
CreatedOnUtc = existingUser.CreatedOnUtc,
|
||||
UpdatedOnUtc = DateTime.UtcNow,
|
||||
UpdatedBy = existingUser.Id
|
||||
};
|
||||
|
||||
// Act
|
||||
ErrorOr<Updated> result = await _sut.UpdateAsync(updatedUser, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
result.IsError.Should().BeFalse();
|
||||
result.Value.Should().Be(Result.Updated);
|
||||
|
||||
EntityEntry<UserPermissionEntity>? removedPermission = _mockContext.ChangeTracker
|
||||
.Entries<UserPermissionEntity>()
|
||||
.FirstOrDefault(e => e.State == EntityState.Deleted);
|
||||
removedPermission.Should().NotBeNull();
|
||||
removedPermission!.Entity.PermissionId.Should().Be(oldPermission.Id);
|
||||
|
||||
EntityEntry<UserPermissionEntity>? addedPermission = _mockContext.ChangeTracker
|
||||
.Entries<UserPermissionEntity>()
|
||||
.FirstOrDefault(e => e.State == EntityState.Added);
|
||||
addedPermission.Should().NotBeNull();
|
||||
addedPermission!.Entity.PermissionId.Should().Be(newPermission.Id);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task UpdateAsync_WhenUserRoleChanges_ShouldRemoveOldRoleAndAddNew()
|
||||
{
|
||||
// Arrange
|
||||
RoleEntity oldRole = new()
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
RoleName = "OldRole",
|
||||
CreatedOnUtc = DateTime.UtcNow,
|
||||
CreatedBy = Guid.NewGuid()
|
||||
};
|
||||
|
||||
RoleEntity newRole = new()
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
RoleName = "NewRole",
|
||||
CreatedOnUtc = DateTime.UtcNow,
|
||||
CreatedBy = Guid.NewGuid()
|
||||
};
|
||||
|
||||
UserEntity existingUser = new()
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
Username = "TestUser",
|
||||
Password = "TestPass",
|
||||
Libraries = [],
|
||||
UserRole = null,
|
||||
UserPermissions = [],
|
||||
CreatedOnUtc = DateTime.UtcNow,
|
||||
CreatedBy = Guid.NewGuid()
|
||||
};
|
||||
|
||||
UserRoleEntity oldUserRole = new()
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
UserId = existingUser.Id,
|
||||
User = existingUser,
|
||||
RoleId = oldRole.Id,
|
||||
Role = oldRole,
|
||||
CreatedOnUtc = DateTime.UtcNow,
|
||||
CreatedBy = Guid.NewGuid()
|
||||
};
|
||||
|
||||
existingUser = new()
|
||||
{
|
||||
Id = existingUser.Id,
|
||||
Username = existingUser.Username,
|
||||
Password = existingUser.Password,
|
||||
Libraries = [],
|
||||
UserRole = oldUserRole,
|
||||
UserPermissions = [],
|
||||
CreatedOnUtc = existingUser.CreatedOnUtc,
|
||||
CreatedBy = existingUser.CreatedBy
|
||||
};
|
||||
|
||||
_mockContext.Users.Add(existingUser);
|
||||
await _mockContext.SaveChangesAsync();
|
||||
|
||||
UserRoleEntity newUserRole = new()
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
UserId = existingUser.Id,
|
||||
User = existingUser,
|
||||
RoleId = newRole.Id,
|
||||
Role = newRole,
|
||||
CreatedOnUtc = DateTime.UtcNow,
|
||||
CreatedBy = existingUser.Id
|
||||
};
|
||||
|
||||
UserEntity updatedUser = new()
|
||||
{
|
||||
Id = existingUser.Id,
|
||||
Username = existingUser.Username,
|
||||
Password = existingUser.Password,
|
||||
Libraries = [],
|
||||
UserRole = newUserRole,
|
||||
UserPermissions = [],
|
||||
CreatedBy = existingUser.CreatedBy,
|
||||
CreatedOnUtc = existingUser.CreatedOnUtc,
|
||||
UpdatedOnUtc = DateTime.UtcNow,
|
||||
UpdatedBy = existingUser.Id
|
||||
};
|
||||
|
||||
// Act
|
||||
ErrorOr<Updated> result = await _sut.UpdateAsync(updatedUser, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
result.IsError.Should().BeFalse();
|
||||
result.Value.Should().Be(Result.Updated);
|
||||
|
||||
EntityEntry<UserRoleEntity>? removedRole = _mockContext.ChangeTracker
|
||||
.Entries<UserRoleEntity>()
|
||||
.FirstOrDefault(e => e.State == EntityState.Deleted);
|
||||
removedRole.Should().NotBeNull();
|
||||
removedRole!.Entity.RoleId.Should().Be(oldRole.Id);
|
||||
|
||||
EntityEntry<UserRoleEntity>? addedRole = _mockContext.ChangeTracker
|
||||
.Entries<UserRoleEntity>()
|
||||
.FirstOrDefault(e => e.State == EntityState.Added);
|
||||
addedRole.Should().NotBeNull();
|
||||
addedRole!.Entity.UserId.Should().Be(existingUser.Id);
|
||||
addedRole.Entity.RoleId.Should().Be(newRole.Id);
|
||||
addedRole.Entity.Role.Should().Be(newRole);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task UpdateAsync_WhenRemovingUserRole_ShouldRemoveRoleAndNotAddNew()
|
||||
{
|
||||
// Arrange
|
||||
UserEntityFixture userFixture = new();
|
||||
UserRoleEntityFixture userRoleFixture = new();
|
||||
|
||||
UserRoleEntity oldUserRole = userRoleFixture.CreateUserRoleModel();
|
||||
|
||||
UserEntity existingUser = new()
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
Username = "TestUser",
|
||||
Password = "TestPass",
|
||||
Libraries = [],
|
||||
UserRole = oldUserRole,
|
||||
UserPermissions = [],
|
||||
CreatedOnUtc = DateTime.UtcNow,
|
||||
CreatedBy = Guid.NewGuid()
|
||||
};
|
||||
|
||||
_mockContext.Users.Add(existingUser);
|
||||
await _mockContext.SaveChangesAsync();
|
||||
|
||||
UserEntity updatedUser = new()
|
||||
{
|
||||
Id = existingUser.Id,
|
||||
Username = existingUser.Username,
|
||||
Password = existingUser.Password,
|
||||
Libraries = [],
|
||||
UserRole = null,
|
||||
UserPermissions = [],
|
||||
CreatedBy = existingUser.CreatedBy,
|
||||
CreatedOnUtc = existingUser.CreatedOnUtc,
|
||||
UpdatedOnUtc = DateTime.UtcNow,
|
||||
UpdatedBy = existingUser.Id
|
||||
};
|
||||
|
||||
// Act
|
||||
ErrorOr<Updated> result = await _sut.UpdateAsync(updatedUser, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
result.IsError.Should().BeFalse();
|
||||
result.Value.Should().Be(Result.Updated);
|
||||
|
||||
EntityEntry<UserRoleEntity>? removedRole = _mockContext.ChangeTracker
|
||||
.Entries<UserRoleEntity>()
|
||||
.FirstOrDefault(e => e.State == EntityState.Deleted);
|
||||
removedRole.Should().NotBeNull();
|
||||
removedRole!.Entity.Should().Be(oldUserRole);
|
||||
|
||||
EntityEntry<UserRoleEntity>? addedRole = _mockContext.ChangeTracker
|
||||
.Entries<UserRoleEntity>()
|
||||
.FirstOrDefault(e => e.State == EntityState.Added);
|
||||
addedRole.Should().BeNull();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user