|
| 1 | +using System.Linq; |
| 2 | +using System.Net; |
| 3 | +using System.Threading; |
| 4 | +using System.Threading.Tasks; |
| 5 | +using Hikkaba.Data.Context; |
| 6 | +using Hikkaba.Infrastructure.Models.Error; |
| 7 | +using Hikkaba.Infrastructure.Models.Role; |
| 8 | +using Hikkaba.Infrastructure.Repositories.Contracts; |
| 9 | +using Hikkaba.Tests.Integration.Builders; |
| 10 | +using Hikkaba.Tests.Integration.Constants; |
| 11 | +using Microsoft.EntityFrameworkCore; |
| 12 | +using Microsoft.Extensions.DependencyInjection; |
| 13 | + |
| 14 | +namespace Hikkaba.Tests.Integration.Tests.Repositories.Role; |
| 15 | + |
| 16 | +internal sealed class CreateRoleTests : IntegrationTestBase |
| 17 | +{ |
| 18 | + #region Basic create tests |
| 19 | + |
| 20 | + [CancelAfter(TestDefaults.TestTimeout)] |
| 21 | + [Test] |
| 22 | + public async Task CreateRole_WhenValidRequest_CreatesRoleSuccessfully( |
| 23 | + CancellationToken cancellationToken) |
| 24 | + { |
| 25 | + // Arrange |
| 26 | + using var appScope = await CreateAppScopeAsync(cancellationToken); |
| 27 | + var repository = appScope.ServiceScope.ServiceProvider.GetRequiredService<IRoleRepository>(); |
| 28 | + |
| 29 | + var request = new RoleCreateRequestModel |
| 30 | + { |
| 31 | + RoleName = "NewRole", |
| 32 | + }; |
| 33 | + |
| 34 | + // Act |
| 35 | + var result = await repository.CreateRoleAsync(request, cancellationToken); |
| 36 | + |
| 37 | + // Assert |
| 38 | + Assert.That(result.Value, Is.TypeOf<RoleCreateResultSuccessModel>(), "Expected successful role creation"); |
| 39 | + var successResult = result.AsT0; |
| 40 | + Assert.That(successResult.RoleId, Is.GreaterThan(0)); |
| 41 | + |
| 42 | + // Verify role was created in DB |
| 43 | + var dbContext = appScope.ServiceScope.ServiceProvider.GetRequiredService<ApplicationDbContext>(); |
| 44 | + var createdRole = await dbContext.Roles.FirstOrDefaultAsync(r => r.Id == successResult.RoleId, cancellationToken); |
| 45 | + Assert.That(createdRole, Is.Not.Null); |
| 46 | + Assert.That(createdRole!.Name, Is.EqualTo("NewRole")); |
| 47 | + } |
| 48 | + |
| 49 | + [CancelAfter(TestDefaults.TestTimeout)] |
| 50 | + [Test] |
| 51 | + public async Task CreateRole_WhenCreated_SetsNormalizedName( |
| 52 | + CancellationToken cancellationToken) |
| 53 | + { |
| 54 | + // Arrange |
| 55 | + using var appScope = await CreateAppScopeAsync(cancellationToken); |
| 56 | + var repository = appScope.ServiceScope.ServiceProvider.GetRequiredService<IRoleRepository>(); |
| 57 | + |
| 58 | + var request = new RoleCreateRequestModel |
| 59 | + { |
| 60 | + RoleName = "TestRole", |
| 61 | + }; |
| 62 | + |
| 63 | + // Act |
| 64 | + var result = await repository.CreateRoleAsync(request, cancellationToken); |
| 65 | + |
| 66 | + // Assert |
| 67 | + Assert.That(result.Value, Is.TypeOf<RoleCreateResultSuccessModel>(), "Expected successful role creation"); |
| 68 | + var successResult = result.AsT0; |
| 69 | + |
| 70 | + var dbContext = appScope.ServiceScope.ServiceProvider.GetRequiredService<ApplicationDbContext>(); |
| 71 | + var createdRole = await dbContext.Roles.FirstAsync(r => r.Id == successResult.RoleId, cancellationToken); |
| 72 | + Assert.That(createdRole.NormalizedName, Is.EqualTo("TESTROLE")); |
| 73 | + } |
| 74 | + |
| 75 | + [CancelAfter(TestDefaults.TestTimeout)] |
| 76 | + [Test] |
| 77 | + public async Task CreateRole_WhenMixedCaseName_SetsCorrectNormalizedName( |
| 78 | + CancellationToken cancellationToken) |
| 79 | + { |
| 80 | + // Arrange |
| 81 | + using var appScope = await CreateAppScopeAsync(cancellationToken); |
| 82 | + var repository = appScope.ServiceScope.ServiceProvider.GetRequiredService<IRoleRepository>(); |
| 83 | + |
| 84 | + var request = new RoleCreateRequestModel |
| 85 | + { |
| 86 | + RoleName = "SuperAdminRole", |
| 87 | + }; |
| 88 | + |
| 89 | + // Act |
| 90 | + var result = await repository.CreateRoleAsync(request, cancellationToken); |
| 91 | + |
| 92 | + // Assert |
| 93 | + Assert.That(result.Value, Is.TypeOf<RoleCreateResultSuccessModel>(), "Expected successful role creation"); |
| 94 | + var successResult = result.AsT0; |
| 95 | + |
| 96 | + var dbContext = appScope.ServiceScope.ServiceProvider.GetRequiredService<ApplicationDbContext>(); |
| 97 | + var createdRole = await dbContext.Roles.FirstAsync(r => r.Id == successResult.RoleId, cancellationToken); |
| 98 | + Assert.That(createdRole.Name, Is.EqualTo("SuperAdminRole")); |
| 99 | + Assert.That(createdRole.NormalizedName, Is.EqualTo("SUPERADMINROLE")); |
| 100 | + } |
| 101 | + |
| 102 | + #endregion |
| 103 | + |
| 104 | + #region Duplicate role tests |
| 105 | + |
| 106 | + [CancelAfter(TestDefaults.TestTimeout)] |
| 107 | + [Test] |
| 108 | + public async Task CreateRole_WhenDuplicateName_ReturnsError( |
| 109 | + CancellationToken cancellationToken) |
| 110 | + { |
| 111 | + // Arrange |
| 112 | + using var appScope = await CreateAppScopeAsync(cancellationToken); |
| 113 | + |
| 114 | + await new TestDataBuilder(appScope.ServiceScope) |
| 115 | + .WithRole("ExistingRole") |
| 116 | + .SaveAsync(cancellationToken); |
| 117 | + |
| 118 | + var repository = appScope.ServiceScope.ServiceProvider.GetRequiredService<IRoleRepository>(); |
| 119 | + |
| 120 | + var request = new RoleCreateRequestModel |
| 121 | + { |
| 122 | + RoleName = "ExistingRole", |
| 123 | + }; |
| 124 | + |
| 125 | + // Act |
| 126 | + var result = await repository.CreateRoleAsync(request, cancellationToken); |
| 127 | + |
| 128 | + // Assert |
| 129 | + Assert.That(result.Value, Is.TypeOf<DomainError>(), "Expected error result for duplicate role"); |
| 130 | + var error = result.AsT1; |
| 131 | + Assert.That(error.StatusCode, Is.EqualTo((int)HttpStatusCode.InternalServerError)); |
| 132 | + Assert.That(error.ErrorMessage, Does.Contain("Role creation failed")); |
| 133 | + } |
| 134 | + |
| 135 | + [CancelAfter(TestDefaults.TestTimeout)] |
| 136 | + [Test] |
| 137 | + public async Task CreateRole_WhenDuplicateNameDifferentCase_ReturnsError( |
| 138 | + CancellationToken cancellationToken) |
| 139 | + { |
| 140 | + // Arrange |
| 141 | + using var appScope = await CreateAppScopeAsync(cancellationToken); |
| 142 | + |
| 143 | + await new TestDataBuilder(appScope.ServiceScope) |
| 144 | + .WithRole("TestRole") |
| 145 | + .SaveAsync(cancellationToken); |
| 146 | + |
| 147 | + var repository = appScope.ServiceScope.ServiceProvider.GetRequiredService<IRoleRepository>(); |
| 148 | + |
| 149 | + var request = new RoleCreateRequestModel |
| 150 | + { |
| 151 | + RoleName = "TESTROLE", // Same role name but different case |
| 152 | + }; |
| 153 | + |
| 154 | + // Act |
| 155 | + var result = await repository.CreateRoleAsync(request, cancellationToken); |
| 156 | + |
| 157 | + // Assert |
| 158 | + Assert.That(result.Value, Is.TypeOf<DomainError>(), "Expected error result for duplicate role (case-insensitive)"); |
| 159 | + var error = result.AsT1; |
| 160 | + Assert.That(error.StatusCode, Is.EqualTo((int)HttpStatusCode.InternalServerError)); |
| 161 | + } |
| 162 | + |
| 163 | + #endregion |
| 164 | + |
| 165 | + #region Multiple roles tests |
| 166 | + |
| 167 | + [CancelAfter(TestDefaults.TestTimeout)] |
| 168 | + [Test] |
| 169 | + public async Task CreateRole_WhenMultipleRolesCreated_AllHaveUniqueIds( |
| 170 | + CancellationToken cancellationToken) |
| 171 | + { |
| 172 | + // Arrange |
| 173 | + using var appScope = await CreateAppScopeAsync(cancellationToken); |
| 174 | + var repository = appScope.ServiceScope.ServiceProvider.GetRequiredService<IRoleRepository>(); |
| 175 | + |
| 176 | + // Act |
| 177 | + var result1 = await repository.CreateRoleAsync(new RoleCreateRequestModel { RoleName = "Role1" }, cancellationToken); |
| 178 | + var result2 = await repository.CreateRoleAsync(new RoleCreateRequestModel { RoleName = "Role2" }, cancellationToken); |
| 179 | + var result3 = await repository.CreateRoleAsync(new RoleCreateRequestModel { RoleName = "Role3" }, cancellationToken); |
| 180 | + |
| 181 | + // Assert |
| 182 | + Assert.That(result1.IsT0, Is.True); |
| 183 | + Assert.That(result2.IsT0, Is.True); |
| 184 | + Assert.That(result3.IsT0, Is.True); |
| 185 | + |
| 186 | + var roleId1 = result1.AsT0.RoleId; |
| 187 | + var roleId2 = result2.AsT0.RoleId; |
| 188 | + var roleId3 = result3.AsT0.RoleId; |
| 189 | + |
| 190 | + Assert.That(roleId1, Is.Not.EqualTo(roleId2)); |
| 191 | + Assert.That(roleId2, Is.Not.EqualTo(roleId3)); |
| 192 | + Assert.That(roleId1, Is.Not.EqualTo(roleId3)); |
| 193 | + } |
| 194 | + |
| 195 | + [CancelAfter(TestDefaults.TestTimeout)] |
| 196 | + [Test] |
| 197 | + public async Task CreateRole_WhenCreatedAfterExistingRoles_HasHigherId( |
| 198 | + CancellationToken cancellationToken) |
| 199 | + { |
| 200 | + // Arrange |
| 201 | + using var appScope = await CreateAppScopeAsync(cancellationToken); |
| 202 | + |
| 203 | + var builder = new TestDataBuilder(appScope.ServiceScope) |
| 204 | + .WithRole("ExistingRole1") |
| 205 | + .WithRole("ExistingRole2"); |
| 206 | + await builder.SaveAsync(cancellationToken); |
| 207 | + |
| 208 | + var existingRole1 = builder.GetRole("ExistingRole1"); |
| 209 | + var existingRole2 = builder.GetRole("ExistingRole2"); |
| 210 | + |
| 211 | + var repository = appScope.ServiceScope.ServiceProvider.GetRequiredService<IRoleRepository>(); |
| 212 | + |
| 213 | + // Act |
| 214 | + var result = await repository.CreateRoleAsync(new RoleCreateRequestModel { RoleName = "NewRole" }, cancellationToken); |
| 215 | + |
| 216 | + // Assert |
| 217 | + Assert.That(result.Value, Is.TypeOf<RoleCreateResultSuccessModel>(), "Expected successful role creation"); |
| 218 | + var newRoleId = result.AsT0.RoleId; |
| 219 | + Assert.That(newRoleId, Is.GreaterThan(existingRole1.Id)); |
| 220 | + Assert.That(newRoleId, Is.GreaterThan(existingRole2.Id)); |
| 221 | + } |
| 222 | + |
| 223 | + #endregion |
| 224 | + |
| 225 | + #region Special characters tests |
| 226 | + |
| 227 | + [CancelAfter(TestDefaults.TestTimeout)] |
| 228 | + [TestCase("Role With Spaces")] |
| 229 | + [TestCase("Role-With-Dashes")] |
| 230 | + [TestCase("Role_With_Underscores")] |
| 231 | + [TestCase("Role123")] |
| 232 | + public async Task CreateRole_WhenNameWithSpecialCharacters_CreatesSuccessfully( |
| 233 | + string roleName, |
| 234 | + CancellationToken cancellationToken) |
| 235 | + { |
| 236 | + // Arrange |
| 237 | + using var appScope = await CreateAppScopeAsync(cancellationToken); |
| 238 | + var repository = appScope.ServiceScope.ServiceProvider.GetRequiredService<IRoleRepository>(); |
| 239 | + |
| 240 | + var request = new RoleCreateRequestModel |
| 241 | + { |
| 242 | + RoleName = roleName, |
| 243 | + }; |
| 244 | + |
| 245 | + // Act |
| 246 | + var result = await repository.CreateRoleAsync(request, cancellationToken); |
| 247 | + |
| 248 | + // Assert |
| 249 | + Assert.That(result.Value, Is.TypeOf<RoleCreateResultSuccessModel>(), "Expected successful role creation"); |
| 250 | + |
| 251 | + var dbContext = appScope.ServiceScope.ServiceProvider.GetRequiredService<ApplicationDbContext>(); |
| 252 | + var createdRole = await dbContext.Roles.FirstAsync(r => r.Id == result.AsT0.RoleId, cancellationToken); |
| 253 | + Assert.That(createdRole.Name, Is.EqualTo(roleName)); |
| 254 | + } |
| 255 | + |
| 256 | + #endregion |
| 257 | +} |
0 commit comments