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); } } }