diff --git a/DamageAssesmentApi/DamageAssesment.Api.UsersAccess.Test/DamageAssesment.Api.UsersAccess.Test.csproj b/DamageAssesmentApi/DamageAssesment.Api.UsersAccess.Test/DamageAssesment.Api.UsersAccess.Test.csproj new file mode 100644 index 0000000..e655693 --- /dev/null +++ b/DamageAssesmentApi/DamageAssesment.Api.UsersAccess.Test/DamageAssesment.Api.UsersAccess.Test.csproj @@ -0,0 +1,30 @@ + + + + net6.0 + enable + enable + + false + true + + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + + + + + diff --git a/DamageAssesmentApi/DamageAssesment.Api.UsersAccess.Test/MockData.cs b/DamageAssesmentApi/DamageAssesment.Api.UsersAccess.Test/MockData.cs new file mode 100644 index 0000000..b25ec9a --- /dev/null +++ b/DamageAssesmentApi/DamageAssesment.Api.UsersAccess.Test/MockData.cs @@ -0,0 +1,44 @@ +using DamageAssesment.Api.UsersAccess.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Xunit.Sdk; + +namespace DamageAssesment.Api.UsersAccess.Test +{ + public class MockData + { + public static async Task<(bool, Models.TokenResponse, string)> getTokenResponse(bool status, string message) + { + return (status, new Models.TokenResponse { jwttoken = "1234", refreshtoken = "12345" }, message); + } + + public static async Task<(bool, List, string)> getUsers(bool status, string message) + { + List users = new List(); + users.Add(new User { Id = 1, EmployeeCode = "Emp1", EmployeeId = 1, RoleId = 1, IsActive = true, CreateDate = DateTime.Now }); + users.Add(new User { Id = 2, EmployeeCode = "Emp2", EmployeeId = 2, RoleId = 1, IsActive = true, CreateDate = DateTime.Now }); + users.Add(new User { Id = 3, EmployeeCode = "Emp3", EmployeeId = 3, RoleId = 1, IsActive = true, CreateDate = DateTime.Now }); + return (status, users, message); + } + + public static async Task<(bool, User, string)> getUser(bool status, string message) + { + User user = getUsers(status, message).Result.Item2.FirstOrDefault(); + return (status, user, message); + } + + public static async Task<(bool, List, string)> getRoles(bool status, string message) + { + List roles = new List(); + roles.Add(new Role { Id = 1, Name = "Role 1" }); + roles.Add(new Role { Id = 2, Name = "Role 2" }); + roles.Add(new Role { Id = 3, Name = "Role 3" }); + + return (status, roles, message); + } + + } +} diff --git a/DamageAssesmentApi/DamageAssesment.Api.UsersAccess.Test/UsersAccessTest.cs b/DamageAssesmentApi/DamageAssesment.Api.UsersAccess.Test/UsersAccessTest.cs new file mode 100644 index 0000000..46165f3 --- /dev/null +++ b/DamageAssesmentApi/DamageAssesment.Api.UsersAccess.Test/UsersAccessTest.cs @@ -0,0 +1,194 @@ +using DamageAssesment.Api.UsersAccess.Controllers; +using DamageAssesment.Api.UsersAccess.Interfaces; +using Microsoft.AspNetCore.Mvc; +using Moq; +using Xunit; + +namespace DamageAssesment.Api.UsersAccess.Test +{ + public class UsersAccessTest + { + private Mock mockService; + + public UsersAccessTest() + { + mockService = new Mock(); + } + [Fact(DisplayName = "Get Token - Ok case")] + public async Task GetTokenAsync_ShouldReturnStatusCode200() + { + var response = await MockData.getTokenResponse(true,null); + mockService.Setup(service => service.AuthenticateAsync("Emp1")).ReturnsAsync(response); + var controller = new UsersAccessController(mockService.Object); + var result = (OkObjectResult)await controller.AuthenticateAsync("Emp1"); + Assert.Equal(200, result.StatusCode); + } + + [Fact(DisplayName = "Get Token - Unauthorized case")] + public async Task GetTokenAsync_ShouldReturnStatusCode401() + { + var response = await MockData.getTokenResponse(false, null); + mockService.Setup(service => service.AuthenticateAsync("Emp1")).ReturnsAsync(response); + var controller = new UsersAccessController(mockService.Object); + var result = (UnauthorizedObjectResult)await controller.AuthenticateAsync("Emp1"); + Assert.Equal(401, result.StatusCode); + } + + + [Fact(DisplayName = "RefreshToken - Ok case")] + public async Task RefreshTokenAsync_ShouldReturnStatusCode200() + { + var response = await MockData.getTokenResponse(true, null); + mockService.Setup(service => service.RefreshTokenAsync(null)).ReturnsAsync(response); + var controller = new UsersAccessController(mockService.Object); + var result = (OkObjectResult)await controller.RefreshTokenAsync(null); + Assert.Equal(200, result.StatusCode); + } + + [Fact(DisplayName = "RefreshToken - Unauthorized case")] + public async Task RefreshTokenAsync_ShouldReturnStatusCode401() + { + var response = await MockData.getTokenResponse(false, null); + mockService.Setup(service => service.RefreshTokenAsync(null)).ReturnsAsync(response); + var controller = new UsersAccessController(mockService.Object); + var result = (UnauthorizedObjectResult)await controller.RefreshTokenAsync(null); + Assert.Equal(401, result.StatusCode); + } + + [Fact(DisplayName = "GetUsers - Ok case")] + public async Task GetUsersAsync_ShouldReturnStatusCode200() + { + var response = await MockData.getUsers(true, null); + mockService.Setup(service => service.GetUsersAsync()).ReturnsAsync(response); + var controller = new UsersAccessController(mockService.Object); + var result = (OkObjectResult)await controller.GetUsersAsync(); + Assert.Equal(200, result.StatusCode); + } + + [Fact(DisplayName = "GetUsers - NoContent case")] + public async Task GetUsersAsync_ShouldReturnStatusCode204() + { + var response = await MockData.getUsers(false, null); + mockService.Setup(service => service.GetUsersAsync()).ReturnsAsync(response); + var controller = new UsersAccessController(mockService.Object); + var result = (NoContentResult)await controller.GetUsersAsync(); + Assert.Equal(204, result.StatusCode); + } + + [Fact(DisplayName = "GetUser - Ok case")] + public async Task GetUserAsync_ShouldReturnStatusCode200() + { + var response = await MockData.getUser(true, null); + mockService.Setup(service => service.GetUsersAsync(1)).ReturnsAsync(response); + var controller = new UsersAccessController(mockService.Object); + var result = (OkObjectResult)await controller.GetUsersAsync(1); + Assert.Equal(200, result.StatusCode); + } + + [Fact(DisplayName = "GetUser - NotFound case")] + public async Task GetUserAsync_ShouldReturnStatusCode204() + { + var response = await MockData.getUser(false, null); + mockService.Setup(service => service.GetUsersAsync(1)).ReturnsAsync(response); + var controller = new UsersAccessController(mockService.Object); + var result = (NotFoundResult)await controller.GetUsersAsync(1); + Assert.Equal(404, result.StatusCode); + } + + [Fact(DisplayName = "GetRoles - Ok case")] + public async Task GetRolesAsync_ShouldReturnStatusCode200() + { + var response = await MockData.getRoles(true, null); + mockService.Setup(service => service.GetRolesAsync()).ReturnsAsync(response); + var controller = new UsersAccessController(mockService.Object); + var result = (OkObjectResult)await controller.GetRolesAsync(); + Assert.Equal(200, result.StatusCode); + } + + [Fact(DisplayName = "GetRoles - NoContent case")] + public async Task GetRolesAsync_ShouldReturnStatusCode204() + { + var response = await MockData.getRoles(false, null); + mockService.Setup(service => service.GetRolesAsync()).ReturnsAsync(response); + var controller = new UsersAccessController(mockService.Object); + var result = (NoContentResult)await controller.GetRolesAsync(); + Assert.Equal(204, result.StatusCode); + } + + [Fact(DisplayName = "PostUser - Ok case")] + public async Task PostUserAsync_ShouldReturnStatusCode200() + { + var response = await MockData.getUser(true, null); + var user = new Models.User { Id = 1, EmployeeCode = "Emp1", EmployeeId = 1, RoleId = 1, IsActive = true, CreateDate = DateTime.Now }; + mockService.Setup(service => service.PostUserAsync(user)).ReturnsAsync(response); + var controller = new UsersAccessController(mockService.Object); + var result = (OkObjectResult)await controller.PostUserAsync(user); + Assert.Equal(200, result.StatusCode); + } + + [Fact(DisplayName = "PostUser - Bad Request case")] + public async Task PostUserAsync_ShouldReturnStatusCode400() + { + var response = await MockData.getUser(false, null); + var user = new Models.User { Id = 1, EmployeeCode = "Emp1", EmployeeId = 1, RoleId = 1, IsActive = true, CreateDate = DateTime.Now }; + mockService.Setup(service => service.PostUserAsync(user)).ReturnsAsync(response); + var controller = new UsersAccessController(mockService.Object); + var result = (BadRequestObjectResult)await controller.PostUserAsync(user); + Assert.Equal(400, result.StatusCode); + } + + [Fact(DisplayName = "PutUser - Ok case")] + public async Task PutUserAsync_ShouldReturnStatusCode200() + { + var response = await MockData.getUser(true, null); + var user = new Models.User { Id = 1, EmployeeCode = "Emp1", EmployeeId = 1, RoleId = 1, IsActive = true, CreateDate = DateTime.Now }; + mockService.Setup(service => service.PutUserAsync(1,user)).ReturnsAsync(response); + var controller = new UsersAccessController(mockService.Object); + var result = (OkObjectResult)await controller.PutUserAsync(1,user); + Assert.Equal(200, result.StatusCode); + } + + [Fact(DisplayName = "PutUser - BadRequest case")] + public async Task PutUserAsync_ShouldReturnStatusCode400() + { + var response = await MockData.getUser(false, null); + var user = new Models.User { Id = 1, EmployeeCode = "Emp1", EmployeeId = 1, RoleId = 1, IsActive = true, CreateDate = DateTime.Now }; + mockService.Setup(service => service.PutUserAsync(1,user)).ReturnsAsync(response); + var controller = new UsersAccessController(mockService.Object); + var result = (BadRequestObjectResult)await controller.PutUserAsync(1,user); + Assert.Equal(400, result.StatusCode); + } + + [Fact(DisplayName = "PutUser - Not Found case")] + public async Task PutUserAsync_ShouldReturnStatusCode404() + { + var response = await MockData.getUser(false, "Not Found"); + var user = new Models.User { Id = 1, EmployeeCode = "Emp1", EmployeeId = 1, RoleId = 1, IsActive = true, CreateDate = DateTime.Now }; + mockService.Setup(service => service.PutUserAsync(1, user)).ReturnsAsync(response); + var controller = new UsersAccessController(mockService.Object); + var result = (NotFoundObjectResult)await controller.PutUserAsync(1,user); + Assert.Equal(404, result.StatusCode); + } + + + [Fact(DisplayName = "DeleteUser - Ok case")] + public async Task DeleteUserAsync_ShouldReturnStatusCode200() + { + var response = await MockData.getUser(true, null); + mockService.Setup(service => service.DeleteUserAsync(1)).ReturnsAsync(response); + var controller = new UsersAccessController(mockService.Object); + var result = (OkObjectResult)await controller.DeleteUserAsync(1); + Assert.Equal(200, result.StatusCode); + } + + [Fact(DisplayName = "DeleteUser - Not Found case")] + public async Task DeleteUserAsync_ShouldReturnStatusCode404() + { + var response = await MockData.getUser(false, "Not Found"); + mockService.Setup(service => service.DeleteUserAsync(1)).ReturnsAsync(response); + var controller = new UsersAccessController(mockService.Object); + var result = (NotFoundResult)await controller.DeleteUserAsync(1); + Assert.Equal(404, result.StatusCode); + } + } +} \ No newline at end of file diff --git a/DamageAssesmentApi/DamageAssesment.Api.UsersAccess/Controllers/UsersAccessController.cs b/DamageAssesmentApi/DamageAssesment.Api.UsersAccess/Controllers/UsersAccessController.cs index d18eb40..b8b2760 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.UsersAccess/Controllers/UsersAccessController.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.UsersAccess/Controllers/UsersAccessController.cs @@ -112,7 +112,7 @@ namespace DamageAssesment.Api.UsersAccess.Controllers [Authorize(Policy = "DamageApp", Roles = "admin")] [HttpDelete("users/{Id}")] - public async Task DeleteSurveysAsync(int Id) + public async Task DeleteUserAsync(int Id) { var result = await userAccessProvider.DeleteUserAsync(Id); if (result.IsSuccess) diff --git a/DamageAssesmentApi/DamageAssesment.sln b/DamageAssesmentApi/DamageAssesment.sln index a9ec1fe..7efdefc 100644 --- a/DamageAssesmentApi/DamageAssesment.sln +++ b/DamageAssesmentApi/DamageAssesment.sln @@ -43,6 +43,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DamageAssesment.Api.DocuLin EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DamageAssesment.Api.UsersAccess", "DamageAssesment.Api.UsersAccess\DamageAssesment.Api.UsersAccess.csproj", "{40240AD6-90D2-4128-BCDF-12C77D1B1B55}" EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DamageAssesment.Api.UsersAccess.Test", "DamageAssesment.Api.UsersAccess.Test\DamageAssesment.Api.UsersAccess.Test.csproj", "{ADAF9385-262C-4A37-A603-A53B77EA515D}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -116,6 +118,10 @@ Global {40240AD6-90D2-4128-BCDF-12C77D1B1B55}.Debug|Any CPU.Build.0 = Debug|Any CPU {40240AD6-90D2-4128-BCDF-12C77D1B1B55}.Release|Any CPU.ActiveCfg = Release|Any CPU {40240AD6-90D2-4128-BCDF-12C77D1B1B55}.Release|Any CPU.Build.0 = Release|Any CPU + {ADAF9385-262C-4A37-A603-A53B77EA515D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {ADAF9385-262C-4A37-A603-A53B77EA515D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {ADAF9385-262C-4A37-A603-A53B77EA515D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {ADAF9385-262C-4A37-A603-A53B77EA515D}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE