From d252b6d7743146a9d0dd6528d960cb55fddffd6f Mon Sep 17 00:00:00 2001 From: Mobin Barfi Date: Tue, 20 Aug 2024 19:19:06 +0330 Subject: [PATCH 1/4] test: test signup service --- .../DomainService/IdentityServiceTests.cs | 158 ++++++++++++++++++ .../Controllers/IdentityControllerTests.cs | 2 + 2 files changed, 160 insertions(+) create mode 100644 test/Application.UnitTests/Services/DomainService/IdentityServiceTests.cs diff --git a/test/Application.UnitTests/Services/DomainService/IdentityServiceTests.cs b/test/Application.UnitTests/Services/DomainService/IdentityServiceTests.cs new file mode 100644 index 0000000..ba4b577 --- /dev/null +++ b/test/Application.UnitTests/Services/DomainService/IdentityServiceTests.cs @@ -0,0 +1,158 @@ +using Application.DTOs.Identity.ChangeRole; +using Application.DTOs.Identity.CreateUser; +using Application.DTOs.Identity.GetUser; +using Application.DTOs.Identity.LoginUser; +using Application.Interfaces; +using Application.Interfaces.Repositories; +using Application.Mappers; +using Application.Services.DomainService; +using Domain.Entities; +using Microsoft.AspNetCore.Identity; +using NSubstitute; +using Web.DTOs.Identity; +using Web.Mappers; + +namespace test.Application.UnitTests.Services.DomainService; + +public class IdentityServiceTests +{ + private readonly IUserManagerRepository _userManagerRepository; + private readonly IRoleManagerRepository _roleManagerRepository; + private readonly IJwtGenerator _jwtGenerator; + private readonly IdentityService _identityService; + + public IdentityServiceTests() + { + _userManagerRepository = Substitute.For(); + _roleManagerRepository = Substitute.For(); + _jwtGenerator = Substitute.For(); + _identityService = new IdentityService(_userManagerRepository, _roleManagerRepository, _jwtGenerator); + } + + // Signup Tests + [Fact] + public async Task SignUpUser_WhenRoleDoesNotExist_ReturnsFailResult() + { + // Arrange + var createUserRequest = new CreateUserRequest + { + UserName = "MobinBarfi", + Password = "Abc@123", + Email = "mobinbr99@gmail.com", + Role = "NonExistentRole" + }; + + _roleManagerRepository.RoleExistsAsync(createUserRequest.Role).Returns(Task.FromResult(false)); + + // Act + var result = await _identityService.SignUpUser(createUserRequest); + + // Assert + Assert.False(result.Succeed); + Assert.Equal("role does not exist", result.Message); + } + + [Fact] + public async Task SignUpUser_WhenUserCreationFails_ReturnsFailResult() + { + // Arrange + var createUserRequest = new CreateUserRequest + { + UserName = "MobinBarfi", + Password = "Abc@123", + Email = "mobinbr99@gmail.com", + Role = "Admin" + }; + + _roleManagerRepository.RoleExistsAsync(createUserRequest.Role).Returns(Task.FromResult(true)); + + _userManagerRepository.CreateAsync(Arg.Any(), createUserRequest.Password) + .Returns(Task.FromResult(IdentityResult.Failed(new IdentityError { Description = "User creation failed" }))); + + // Act + var result = await _identityService.SignUpUser(createUserRequest); + + // Assert + Assert.False(result.Succeed); + Assert.Equal("User creation failed", result.Message); + } + + [Fact] + public async Task SignUpUser_WhenRoleAssignmentFails_ReturnsFailResult() + { + // Arrange + var createUserRequest = new CreateUserRequest + { + UserName = "MobinBarfi", + Password = "Abc@123", + Email = "mobinbr99@gmail.com", + Role = "Admin" + }; + + _roleManagerRepository.RoleExistsAsync(createUserRequest.Role).Returns(Task.FromResult(true)); + + _userManagerRepository.CreateAsync(Arg.Any(), createUserRequest.Password) + .Returns(Task.FromResult(IdentityResult.Success)); + + _userManagerRepository.SetRoleAsync(Arg.Any(), createUserRequest.Role) + .Returns(Task.FromResult(IdentityResult.Failed(new IdentityError { Description = "Role assignment failed" }))); + + // Act + var result = await _identityService.SignUpUser(createUserRequest); + + // Assert + Assert.False(result.Succeed); + Assert.Equal("Role assignment failed", result.Message); + } + + [Fact] + public async Task SignUpUser_WhenUserIsCreatedAndRoleAssignedSuccessfully_ReturnsSuccessResult() + { + // Arrange + var createUserRequest = new CreateUserRequest + { + UserName = "MobinBarfi", + Password = "Abc@123", + Email = "mobinbr99@gmail.com", + Role = "Admin" + }; + + var appUser = new AppUser { UserName = createUserRequest.UserName }; + var identityResultSuccess = IdentityResult.Success; + + _roleManagerRepository.RoleExistsAsync(createUserRequest.Role) + .Returns(Task.FromResult(true)); + + _userManagerRepository.CreateAsync(Arg.Is(u => u.UserName == createUserRequest.UserName), createUserRequest.Password) + .Returns(Task.FromResult(identityResultSuccess)); + + _userManagerRepository.SetRoleAsync(Arg.Is(u => u.UserName == createUserRequest.UserName), createUserRequest.Role) + .Returns(Task.FromResult(identityResultSuccess)); + + var expectedResponse = new CreateUserResponse + { + UserName = createUserRequest.UserName, + Email = createUserRequest.Email, + FirstName = "", + LastName = "", + Role = createUserRequest.Role + }; + + // Act + var result = await _identityService.SignUpUser(createUserRequest); + + // Assert + Assert.True(result.Succeed); + Assert.NotNull(result.Value); + + var actualResponse = result.Value; + + Assert.Equal(expectedResponse.UserName, actualResponse.UserName); + Assert.Equal(expectedResponse.Email, actualResponse.Email); + Assert.Equal(expectedResponse.FirstName, actualResponse.FirstName); + Assert.Equal(expectedResponse.LastName, actualResponse.LastName); + Assert.Equal(expectedResponse.Role, actualResponse.Role); + } + + +} \ No newline at end of file diff --git a/test/Web.UnitTests/Controllers/IdentityControllerTests.cs b/test/Web.UnitTests/Controllers/IdentityControllerTests.cs index d0d83c6..2a602b7 100644 --- a/test/Web.UnitTests/Controllers/IdentityControllerTests.cs +++ b/test/Web.UnitTests/Controllers/IdentityControllerTests.cs @@ -2,6 +2,8 @@ using Application.DTOs; using Application.DTOs.Identity; using Application.DTOs.Identity.ChangeRole; +using Application.DTOs.Identity.CreateUser; +using Application.DTOs.Identity.LoginUser; using Application.Interfaces.Services; using Domain.Constants; using Domain.Entities; From 746c8adbc5ee05679eb1e29431ac017c117b15c5 Mon Sep 17 00:00:00 2001 From: Mobin Barfi Date: Tue, 20 Aug 2024 19:20:31 +0330 Subject: [PATCH 2/4] test: add login service tests --- .../DomainService/IdentityServiceTests.cs | 90 +++++++++++++++++++ 1 file changed, 90 insertions(+) diff --git a/test/Application.UnitTests/Services/DomainService/IdentityServiceTests.cs b/test/Application.UnitTests/Services/DomainService/IdentityServiceTests.cs index ba4b577..d8e7157 100644 --- a/test/Application.UnitTests/Services/DomainService/IdentityServiceTests.cs +++ b/test/Application.UnitTests/Services/DomainService/IdentityServiceTests.cs @@ -154,5 +154,95 @@ public async Task SignUpUser_WhenUserIsCreatedAndRoleAssignedSuccessfully_Return Assert.Equal(expectedResponse.Role, actualResponse.Role); } + // Login Tests + [Fact] + public async Task Login_WhenUsernameNotFound_ReturnsFailResult() + { + // Arrange + var loginUserRequest = new LoginUserRequest + { + UserName = "NonExistentUser", + Password = "Abc@123" + }; + + _userManagerRepository.FindByNameAsync(loginUserRequest.UserName).Returns(Task.FromResult(null)); + + // Act + var result = await _identityService.Login(loginUserRequest); + + // Assert + Assert.False(result.Succeed); + Assert.Equal("Invalid username/email!", result.Message); + } + + [Fact] + public async Task Login_WhenPasswordIncorrect_ReturnsFailResult() + { + // Arrange + var loginUserRequest = new LoginUserRequest + { + UserName = "MobinBarfi", + Password = "WrongPassword" + }; + + var appUser = new AppUser(); + _userManagerRepository.FindByNameAsync(loginUserRequest.UserName).Returns(Task.FromResult(appUser)); + _userManagerRepository.CheckPasswordAsync(appUser, loginUserRequest.Password).Returns(Task.FromResult(false)); + + // Act + var result = await _identityService.Login(loginUserRequest); + // Assert + Assert.False(result.Succeed); + Assert.Equal("Username/Email not found and/or password incorrect", result.Message); + } + + [Fact] + public async Task Login_WhenLoginSucceeds_ReturnsSuccessResult() + { + // Arrange + var loginDto = new LoginDto + { + UserName = "MobinBarfi", + Password = "Abc@123" + }; + + var loginRequest = loginDto.ToLoginUserRequest(); + var appUser = new AppUser + { + UserName = loginDto.UserName, + Email = "mobinbr99@gmail.com" + }; + var role = "Admin"; + var token = "valid.jwt.token"; + + var loginUserResponse = new LoginUserResponse + { + UserName = appUser.UserName, + Email = appUser.Email, + Token = token + }; + + // Set up the mock behavior + _userManagerRepository.FindByNameAsync(loginRequest.UserName) + .Returns(Task.FromResult(appUser)); + _userManagerRepository.CheckPasswordAsync(appUser, loginRequest.Password) + .Returns(Task.FromResult(true)); + _userManagerRepository.GetRoleAsync(appUser) + .Returns(Task.FromResult(role)); + _jwtGenerator.GenerateToken(appUser, role) + .Returns(token); + + // Act + var result = await _identityService.Login(loginRequest); + + // Assert + Assert.True(result.Succeed); + var loginResponse = result.Value; + + Assert.NotNull(loginResponse); + Assert.Equal(loginUserResponse.UserName, loginResponse.UserName); + Assert.Equal(loginUserResponse.Email, loginResponse.Email); + Assert.Equal(loginUserResponse.Token, loginResponse.Token); + } } \ No newline at end of file From 845907fb5133424d1843ca71836ae68c1306bd1c Mon Sep 17 00:00:00 2001 From: Mobin Barfi Date: Tue, 20 Aug 2024 19:21:27 +0330 Subject: [PATCH 3/4] test: test change-role method --- .../DomainService/IdentityServiceTests.cs | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/test/Application.UnitTests/Services/DomainService/IdentityServiceTests.cs b/test/Application.UnitTests/Services/DomainService/IdentityServiceTests.cs index d8e7157..14209d0 100644 --- a/test/Application.UnitTests/Services/DomainService/IdentityServiceTests.cs +++ b/test/Application.UnitTests/Services/DomainService/IdentityServiceTests.cs @@ -245,4 +245,69 @@ public async Task Login_WhenLoginSucceeds_ReturnsSuccessResult() Assert.Equal(loginUserResponse.Email, loginResponse.Email); Assert.Equal(loginUserResponse.Token, loginResponse.Token); } + + // ChangeRole Tests + [Fact] + public async Task ChangeRole_WhenRoleDoesNotExist_ReturnsFailResult() + { + // Arrange + var changeRoleRequest = new ChangeRoleRequest + { + UserName = "MobinBarfi", + Role = "NonExistentRole" + }; + + _roleManagerRepository.RoleExistsAsync(changeRoleRequest.Role).Returns(Task.FromResult(false)); + + // Act + var result = await _identityService.ChangeRole(changeRoleRequest); + + // Assert + Assert.False(result.Succeed); + Assert.Equal("role does not exist", result.Message); + } + + [Fact] + public async Task ChangeRole_WhenUserDoesNotExist_ReturnsFailResult() + { + // Arrange + var changeRoleRequest = new ChangeRoleRequest + { + UserName = "NonExistentUser", + Role = "Admin" + }; + + _roleManagerRepository.RoleExistsAsync(changeRoleRequest.Role).Returns(Task.FromResult(true)); + _userManagerRepository.FindByNameAsync(changeRoleRequest.UserName).Returns(Task.FromResult(null)); + + // Act + var result = await _identityService.ChangeRole(changeRoleRequest); + + // Assert + Assert.False(result.Succeed); + Assert.Equal("Invalid username", result.Message); + } + + [Fact] + public async Task ChangeRole_WhenOperationSucceeds_ReturnsSuccessResult() + { + // Arrange + var changeRoleRequest = new ChangeRoleRequest + { + UserName = "MobinBarfi", + Role = "Admin" + }; + + var appUser = new AppUser(); + + _roleManagerRepository.RoleExistsAsync(changeRoleRequest.Role).Returns(Task.FromResult(true)); + _userManagerRepository.FindByNameAsync(changeRoleRequest.UserName).Returns(Task.FromResult(appUser)); + _userManagerRepository.ChangeRoleAsync(appUser, changeRoleRequest.Role).Returns(Task.FromResult(IdentityResult.Success)); + + // Act + var result = await _identityService.ChangeRole(changeRoleRequest); + + // Assert + Assert.True(result.Succeed); + } } \ No newline at end of file From 6c282e050f08363bf329d740d8500a9d5ee14b16 Mon Sep 17 00:00:00 2001 From: Mobin Barfi Date: Tue, 20 Aug 2024 19:22:11 +0330 Subject: [PATCH 4/4] test: test get-users in identity service --- .../DomainService/IdentityServiceTests.cs | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/test/Application.UnitTests/Services/DomainService/IdentityServiceTests.cs b/test/Application.UnitTests/Services/DomainService/IdentityServiceTests.cs index 14209d0..20fc129 100644 --- a/test/Application.UnitTests/Services/DomainService/IdentityServiceTests.cs +++ b/test/Application.UnitTests/Services/DomainService/IdentityServiceTests.cs @@ -310,4 +310,43 @@ public async Task ChangeRole_WhenOperationSucceeds_ReturnsSuccessResult() // Assert Assert.True(result.Succeed); } + + // GetUsers Tests + [Fact] + public async Task GetUsersAsync_ReturnsUserListWithRoles() + { + // Arrange + var users = new List + { + new AppUser { UserName = "User1", Email = "user1@example.com" }, + new AppUser { UserName = "User2", Email = "user2@example.com" } + }; + + var roles = new List { "Admin", "User" }; + + var userResponses = new List + { + new GetUserResponse { UserName = "User1", Email = "user1@example.com", Role = "Admin" }, + new GetUserResponse { UserName = "User2", Email = "user2@example.com", Role = "User" } + }; + + // Mock the repository methods + _userManagerRepository.GetUsersAsync() + .Returns(Task.FromResult(users)); + + _userManagerRepository.GetRoleAsync(users[0]) + .Returns(Task.FromResult(roles[0])); + + _userManagerRepository.GetRoleAsync(users[1]) + .Returns(Task.FromResult(roles[1])); + + // Act + var result = await _identityService.GetUsersAsync(); + + // Assert + Assert.NotNull(result); + Assert.Equal(2, result.Count); + Assert.Contains(result, r => r.UserName == "User1" && r.Role == "Admin"); + Assert.Contains(result, r => r.UserName == "User2" && r.Role == "User"); + } } \ No newline at end of file