Files
monorepo/LCU.API.Operations.IncentiveAPI/LCU.API.Internal.Operations.IncentiveApi.UnitTests/Controllers/EmployeeControllerTest.cs
T
2026-03-17 09:53:09 -05:00

704 lines
25 KiB
C#

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using LCU.Library.Internal.IncentiveApiModels.Entities;
using LCU.Library.Internal.IncentiveApiModels.Enums;
using Microsoft.AspNetCore.Mvc;
using Moq;
using NUnit.Framework;
using LCU.API.Internal.Operations.IncentiveApi.Controllers;
using LCU.API.Internal.Operations.IncentiveApiLib.Services;
using LCU.API.Internal.Operations.IncentiveApi.Services.Dayforce;
using LCU.API.Internal.Operations.IncentiveApiLib.Persistence;
using LCU.API.Internal.Operations.IncentiveApi.Models.DataTransfer;
using LCU.API.Internal.Operations.IncentiveApi.Exceptions;
using LCU.Library.Internal.IncentiveApiModels.Requests;
namespace Application.UnitTests.Controllers
{
public class EmployeeControllerTest
{
private EmployeeController _controller;
private Mock<IUserService> _mockUserService;
private Mock<IDayForceSyncService> _mockDayForceService;
private Mock<ICurrentUserService> _mockCurrentUserService;
[SetUp]
public void Setup()
{
_mockCurrentUserService = new Mock<ICurrentUserService>();
_mockDayForceService = new Mock<IDayForceSyncService>();
_mockUserService = new Mock<IUserService>();
_controller = new EmployeeController(
_mockCurrentUserService.Object,
_mockDayForceService.Object,
_mockUserService.Object
);
}
[Test]
public async Task Get_OK()
{
_mockUserService
.Setup(service => service.GetAllUsersAsync(null))
.ReturnsAsync(
new List<AppUser>()
{
new AppUser()
{
AdEmail = "[email protected]",
PersonId = 123,
EmployeeId = "ABC",
BaseRole = "Developer",
Role = Role.UNKNOWN_ROLE,
HomeDepartment = "Branch-44"
},
new AppUser()
{
AdEmail = "[email protected]",
PersonId = 456,
EmployeeId = "DEF",
BaseRole = "Developer",
Role = Role.Admin,
HomeDepartment = "Branch-44"
},
new AppUser()
{
AdEmail = "[email protected]",
PersonId = 789,
EmployeeId = "CONTRACTOR1",
BaseRole = "QA",
Role = Role.Teller,
HomeDepartment = "Branch-44"
},
new AppUser()
{
AdEmail = "[email protected]",
PersonId = 900,
EmployeeId = "CONTRACTOR2",
BaseRole = "QA",
Role = Role.BM,
HomeDepartment = "Branch-0"
}
});
var result = await _controller.GetAllUsers(null, null);
Assert.NotNull(result);
var okResult = result as OkObjectResult;
Assert.NotNull(okResult);
Assert.AreEqual(200, okResult.StatusCode);
}
[Test]
public async Task Get_Resolve_ok()
{
_mockUserService
.Setup(service => service.FindUserAsync("123", null))
.ReturnsAsync(
new AppUser()
{
AdEmail = "[email protected]",
PersonId = 123,
EmployeeId = "ABC",
BaseRole = "Developer",
Role = Role.UNKNOWN_ROLE,
HomeDepartment = "Branch-44"
}
);
var result = await _controller.ResolveUser("123");
Assert.NotNull(result);
var okResult = result as OkObjectResult;
Assert.NotNull(okResult);
Assert.AreEqual(200, okResult.StatusCode);
}
[Test]
public async Task Get_Resolve_BadRequest()
{
_mockUserService
.Setup(service => service.FindUserAsync("11771", null))
.ReturnsAsync(
new AppUser()
{
AdEmail = "[email protected]",
PersonId = 123,
EmployeeId = "ABC",
BaseRole = "Developer",
Role = Role.UNKNOWN_ROLE,
HomeDepartment = "Branch-44"
}
);
var result = await _controller.ResolveUser("11771", "[email protected]");
Assert.NotNull(result);
var statusRes = result as BadRequestObjectResult;
Assert.NotNull(statusRes);
Assert.AreEqual(400, statusRes.StatusCode);
}
[Test]
public async Task Get_Resolve_Throws()
{
_mockUserService
.Setup(service => service.FindUserAsync("123", null))
.Throws<Exception>();
var result = await _controller.ResolveUser("123");
Assert.NotNull(result);
var statusRes = result as StatusCodeResult;
Assert.NotNull(statusRes);
Assert.AreEqual(500, statusRes.StatusCode);
}
[Test]
public async Task Get_Self_ok()
{
_mockCurrentUserService.Setup(service => service.GetCurrentUser())
.Returns(
new ApplicationUser()
{
FirstName = "Test",
LastName = "McGee",
Roles = new[] { "QA" },
Email = "[email protected]",
});
_mockUserService
.Setup(service => service.GetUserByAdEmailAsync("[email protected]"))
.ReturnsAsync(
new AppUser()
{
AdEmail = "[email protected]",
PersonId = 123,
EmployeeId = "ABC",
BaseRole = "Developer",
Role = Role.UNKNOWN_ROLE,
HomeDepartment = "Branch-44"
}
);
var result = await _controller.GetCurrentEmployee();
Assert.NotNull(result);
var okResult = result as OkObjectResult;
Assert.NotNull(okResult);
Assert.AreEqual(200, okResult.StatusCode);
}
[Test]
public async Task Get_Self_Unknown()
{
_mockCurrentUserService.Setup(service => service.GetCurrentUser())
.Returns(
new ApplicationUser()
{
FirstName = "Test",
LastName = "McGee",
Roles = new[] { "QA" },
Email = "[email protected]",
});
_mockUserService
.Setup(service => service.GetUserByAdEmailAsync("[email protected]"))
.ReturnsAsync(
(AppUser)null
);
var result = await _controller.GetCurrentEmployee();
Assert.NotNull(result);
var okResult = result as NoContentResult;
Assert.NotNull(okResult);
Assert.AreEqual(204, okResult.StatusCode);
}
[Test]
public async Task Post_DepartmentAssignment_Ok()
{
var employee = new AppUser()
{
EmployeeId = "11771",
PersonId = 0,
AdEmail = "[email protected]"
};
var department = new Department()
{
Id = 555,
State = DepartmentState.Active,
DepartmentName = "Test Dept",
BusinessLineCode = BusinessLine.Operations.Code
};
var assignment = new DepartmentAssignmentRequest()
{
Note = "Test Assignment",
DepartmentId = new int [555],
StartDate = DateTime.Today
};
var userDepartments = new List<UserDepartment>
{
new UserDepartment()
{
Employee = employee,
Note = "Test Assignment",
Department = department,
StartDate = DateTime.Today,
IsHome = false
}
};
_mockCurrentUserService.Setup(service => service.GetCurrentUser())
.Returns(
new ApplicationUser()
{
FirstName = "Test",
LastName = "McGee",
Roles = new[] { "QA" },
Email = "[email protected]",
});
_mockUserService
.Setup(service => service.GetUserByAdEmailAsync("[email protected]"))
.ReturnsAsync(
new AppUser()
{
EmployeeId = "1",
PersonId = 0,
AdEmail = "[email protected]"
}
);
_mockUserService
.Setup(service => service.AssignDepartments(employee.EmployeeId, It.IsAny<DepartmentAssignmentRequest>()))
.ReturnsAsync(
userDepartments
);
var result = await _controller.AssignDepartment(employee.EmployeeId, assignment);
Assert.NotNull(result);
var okResult = result as OkResult;
Assert.NotNull(okResult);
Assert.AreEqual(200, okResult.StatusCode);
}
[Test]
public async Task Post_DepartmentAssignment_BadRequest()
{
var department = new Department()
{
Id = 555,
State = DepartmentState.Active,
DepartmentName = "Test Dept",
BusinessLineCode = BusinessLine.Operations.Code
};
var assignment = new DepartmentAssignmentRequest()
{
Note = "Test Assignment",
DepartmentId = new int [555],
StartDate = DateTime.Today
};
var employee = new AppUser()
{
EmployeeId = "11771",
PersonId = 0,
AdEmail = "[email protected]"
};
_mockCurrentUserService.Setup(service => service.GetCurrentUser())
.Returns(
new ApplicationUser()
{
FirstName = "Test",
LastName = "McGee",
Roles = new[] { "QA" },
Email = "[email protected]",
});
_mockUserService
.Setup(service => service.GetUserByAdEmailAsync("[email protected]"))
.ReturnsAsync(
new AppUser()
{
EmployeeId = "1",
PersonId = 0,
AdEmail = "[email protected]"
}
);
_mockUserService
.Setup(service => service.AssignDepartments(employee.EmployeeId, It.IsAny<DepartmentAssignmentRequest>()))
.ThrowsAsync(new BadDepartmentAssignmentException("test"));
var result = await _controller.AssignDepartment(employee.EmployeeId, assignment);
Assert.NotNull(result);
var okResult = result as BadRequestObjectResult;
Assert.NotNull(okResult);
Assert.AreEqual(400, okResult.StatusCode);
}
[Test]
public async Task Post_DepartmentAssignment_ADUserNotFound()
{
var assignment = new DepartmentAssignmentRequest()
{
Note = "Test Assignment",
DepartmentId = new int [555],
StartDate = DateTime.Today
};
_mockCurrentUserService
.Setup(service => service.GetCurrentUser())
.Returns<ApplicationUser>(null);
var result = await _controller.AssignDepartment("C1", assignment);
Assert.NotNull(result);
var unauthorizedResult = result as UnauthorizedObjectResult;
Assert.NotNull(unauthorizedResult);
Assert.AreEqual(401, unauthorizedResult.StatusCode);
}
[Test]
public async Task Post_DepartmentAssignment_ForbiddenSelfAssign()
{
var assignment = new DepartmentAssignmentRequest()
{
Note = "Test Assignment",
DepartmentId = new int [555],
StartDate = DateTime.Today
};
_mockCurrentUserService.Setup(service => service.GetCurrentUser())
.Returns(
new ApplicationUser()
{
FirstName = "Test",
LastName = "McGee",
Roles = new[] { "QA" },
Email = "[email protected]",
});
_mockUserService
.Setup(service => service.GetUserByAdEmailAsync("[email protected]"))
.ReturnsAsync(
new AppUser()
{
EmployeeId = "999",
PersonId = 0,
AdEmail = "[email protected]"
}
);
var result = await _controller.AssignDepartment("999", assignment);
Assert.NotNull(result);
var forbiddennResult = result as ObjectResult;
Assert.NotNull(forbiddennResult);
Assert.AreEqual(403, forbiddennResult.StatusCode);
Assert.AreEqual("Not allowed to assign yourself to a department.", forbiddennResult.Value);
}
[Test]
public async Task Post_DepartmentAssignmentUpdate_Ok()
{
var employee = new AppUser()
{
EmployeeId = "11771",
PersonId = 0,
AdEmail = "[email protected]"
};
var assignment = new UserDepartment()
{
Note = "Test Assignment",
DepartmentId = 555,
Employee = employee,
IsHome = false,
StartDate = DateTime.Today
};
var request = new DepartmentUpdateRequest()
{
EmployeeId = "11771",
AssignmentId = 5,
SlidingPayout = 100,
Role = "Teller"
};
_mockCurrentUserService.Setup(service => service.GetCurrentUser())
.Returns(
new ApplicationUser()
{
FirstName = "Test",
LastName = "McGee",
Roles = new[] { "QA" },
Email = "[email protected]",
});
_mockUserService
.Setup(service => service.GetUserByAdEmailAsync("[email protected]"))
.ReturnsAsync(
new AppUser()
{
EmployeeId = "1",
PersonId = 0,
AdEmail = "[email protected]"
}
);
_mockUserService
.Setup(service =>
service.UpdateDepartmentAssignment(request))
.ReturnsAsync(
assignment
);
var result =
await _controller.UpdateDepartmentAssignment(assignment.Employee.EmployeeId, assignment.DepartmentId, request);
Assert.NotNull(result);
var okResult = result as OkResult;
Assert.NotNull(okResult);
Assert.AreEqual(200, okResult.StatusCode);
}
[Test]
public async Task Post_DepartmentAssignmentTerm_BadRequest()
{
var employee = new AppUser()
{
EmployeeId = "11771",
PersonId = 0,
AdEmail = "[email protected]"
};
var assignment = new UserDepartment()
{
Note = "Test Assignment",
DepartmentId = 555,
Employee = employee,
IsHome = false,
StartDate = DateTime.Today
};
var request = new DepartmentUpdateRequest()
{
EmployeeId = "11771",
AssignmentId = 5,
SlidingPayout = 999,
Role = "Teller"
};
_mockCurrentUserService.Setup(service => service.GetCurrentUser())
.Returns(
new ApplicationUser()
{
FirstName = "Test",
LastName = "McGee",
Roles = new[] { "QA" },
Email = "[email protected]",
});
_mockUserService
.Setup(service => service.GetUserByAdEmailAsync("[email protected]"))
.ReturnsAsync(
new AppUser()
{
EmployeeId = "1",
PersonId = 0,
AdEmail = "[email protected]"
}
);
_mockUserService
.Setup(service =>
service.UpdateDepartmentAssignment(request))
.ThrowsAsync(new BadDepartmentAssignmentException("test"));
var result =
await _controller.UpdateDepartmentAssignment(assignment.Employee.EmployeeId, assignment.DepartmentId, request);
Assert.NotNull(result);
var okResult = result as BadRequestObjectResult;
Assert.NotNull(okResult);
Assert.AreEqual(400, okResult.StatusCode);
}
[Test]
public async Task Post_DepartmentAssignmentTerm_ADUserNotFound()
{
var assignment = new DepartmentAssignmentRequest()
{
Note = "Test Assignment",
DepartmentId = new int [555],
StartDate = DateTime.Today
};
_mockCurrentUserService
.Setup(service => service.GetCurrentUser())
.Returns<ApplicationUser>(null);
var result = await _controller.UpdateDepartmentAssignment("C1", 1, null);
Assert.NotNull(result);
var unauthorizedResult = result as UnauthorizedObjectResult;
Assert.NotNull(unauthorizedResult);
Assert.AreEqual(401, unauthorizedResult.StatusCode);
}
[Test]
public async Task Post_DepartmentAssignmentTerm_ForbiddenSelfTerm()
{
var assignment = new DepartmentAssignmentRequest()
{
Note = "Test Assignment",
DepartmentId = new int [555],
StartDate = DateTime.Today
};
var request = new DepartmentUpdateRequest()
{
EmployeeId = "11771",
AssignmentId = 5,
SlidingPayout = 999,
Role = "Teller"
};
_mockCurrentUserService.Setup(service => service.GetCurrentUser())
.Returns(
new ApplicationUser()
{
FirstName = "Test",
LastName = "McGee",
Roles = new[] { "QA" },
Email = "[email protected]",
});
_mockUserService
.Setup(service => service.GetUserByAdEmailAsync("[email protected]"))
.ReturnsAsync(
new AppUser()
{
EmployeeId = "999",
PersonId = 0,
AdEmail = "[email protected]"
}
);
var result = await _controller.UpdateDepartmentAssignment("999", 1, request);
Assert.NotNull(result);
var forbiddennResult = result as ObjectResult;
Assert.NotNull(forbiddennResult);
Assert.AreEqual(403, forbiddennResult.StatusCode);
Assert.AreEqual("Not allowed to terminate yourself from a department.", forbiddennResult.Value);
}
[Test]
public async Task Get_Employee_By_Id_ok()
{
_mockCurrentUserService.Setup(service => service.GetCurrentUser())
.Returns(
new ApplicationUser()
{
FirstName = "Test",
LastName = "McGee",
Roles = new[] { "QA" },
Email = "[email protected]",
});
_mockUserService
.Setup(service => service.GetUserByEmployeeIdAsync("ABC"))
.ReturnsAsync(
new AppUser()
{
AdEmail = "[email protected]",
PersonId = 123,
EmployeeId = "ABC",
BaseRole = "Developer",
Role = Role.UNKNOWN_ROLE,
HomeDepartment = "Branch-44"
}
);
var result = await _controller.GetEmployeeById("ABC");
Assert.NotNull(result);
var okResult = result as OkObjectResult;
Assert.NotNull(okResult);
Assert.AreEqual(200, okResult.StatusCode);
}
[Test]
public async Task Get_Employee_By_Id_Unknown()
{
_mockCurrentUserService.Setup(service => service.GetCurrentUser())
.Returns(
new ApplicationUser()
{
FirstName = "Test",
LastName = "McGee",
Roles = new[] { "QA" },
Email = "[email protected]",
});
_mockUserService
.Setup(service => service.GetUserByEmployeeIdAsync("DEF"))
.ReturnsAsync(
(AppUser)null
);
_mockUserService
.Setup(service => service.GetUserByEmployeeIdAsync("ABC"))
.ReturnsAsync(
new AppUser()
{
AdEmail = "[email protected]",
PersonId = 123,
EmployeeId = "ABC",
BaseRole = "Developer",
Role = Role.UNKNOWN_ROLE,
HomeDepartment = "Branch-44"
}
);
var result = await _controller.GetEmployeeById("DEF");
Assert.NotNull(result);
var okResult = result as NoContentResult;
Assert.NotNull(okResult);
Assert.AreEqual(204, okResult.StatusCode);
}
[Test]
public async Task Get_Employee_By_Id_Throw()
{
_mockCurrentUserService.Setup(service => service.GetCurrentUser())
.Returns(
new ApplicationUser()
{
FirstName = "Test",
LastName = "McGee",
Roles = new[] { "QA" },
Email = "[email protected]"
});
_mockUserService
.Setup(service => service.GetUserByEmployeeIdAsync("DEF"))
.Throws<Exception>();
var result = await _controller.GetEmployeeById("DEF");
Assert.NotNull(result);
var badResult = result as StatusCodeResult;
Assert.NotNull(badResult);
Assert.AreEqual(500, badResult.StatusCode);
}
[Test]
public async Task SyncDayForce_ForceRefresh()
{
_mockDayForceService.Setup(service => service.SyncDayForceUsers(new DayforceProcessRequest()));
var result = await _controller.SyncDayForce();
Assert.NotNull(result);
var successResult = result as StatusCodeResult;
Assert.NotNull(successResult);
Assert.AreEqual(200, successResult.StatusCode);
}
}
}