adding Test project for userAccess module

This commit is contained in:
Reginald Cherenfant Jasmin 2023-09-26 01:13:19 -04:00
parent 4ebd40108d
commit f6387fc371
5 changed files with 275 additions and 1 deletions

View File

@ -0,0 +1,30 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
<PackageReference Include="Moq" Version="4.18.4" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.1.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\DamageAssesment.Api.UsersAccess\DamageAssesment.Api.UsersAccess.csproj" />
</ItemGroup>
</Project>

View File

@ -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<User>, string)> getUsers(bool status, string message)
{
List<User> users = new List<User>();
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<Role>, string)> getRoles(bool status, string message)
{
List<Role> roles = new List<Role>();
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);
}
}
}

View File

@ -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<IUsersAccessProvider> mockService;
public UsersAccessTest()
{
mockService = new Mock<IUsersAccessProvider>();
}
[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);
}
}
}

View File

@ -112,7 +112,7 @@ namespace DamageAssesment.Api.UsersAccess.Controllers
[Authorize(Policy = "DamageApp", Roles = "admin")] [Authorize(Policy = "DamageApp", Roles = "admin")]
[HttpDelete("users/{Id}")] [HttpDelete("users/{Id}")]
public async Task<ActionResult> DeleteSurveysAsync(int Id) public async Task<ActionResult> DeleteUserAsync(int Id)
{ {
var result = await userAccessProvider.DeleteUserAsync(Id); var result = await userAccessProvider.DeleteUserAsync(Id);
if (result.IsSuccess) if (result.IsSuccess)

View File

@ -43,6 +43,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DamageAssesment.Api.DocuLin
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DamageAssesment.Api.UsersAccess", "DamageAssesment.Api.UsersAccess\DamageAssesment.Api.UsersAccess.csproj", "{40240AD6-90D2-4128-BCDF-12C77D1B1B55}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DamageAssesment.Api.UsersAccess", "DamageAssesment.Api.UsersAccess\DamageAssesment.Api.UsersAccess.csproj", "{40240AD6-90D2-4128-BCDF-12C77D1B1B55}"
EndProject 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 Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU 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}.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.ActiveCfg = Release|Any CPU
{40240AD6-90D2-4128-BCDF-12C77D1B1B55}.Release|Any CPU.Build.0 = 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 EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE