-
Notifications
You must be signed in to change notification settings - Fork 0
🧪 [testing improvement] Add tests for ClientesController #44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -100,7 +100,19 @@ public async Task<ActionResult<ClienteResponseDto>> GetCliente(int id) | |
| .Include(c => c.CodigoPostalCpNavigation) | ||
| .Include(c => c.LoginIdloginNavigation) | ||
| .Where(c => c.Idcliente == id) | ||
| .Select(c => new ClienteResponseDto { /* Mapping */ }) | ||
| .Select(c => new ClienteResponseDto { | ||
| Idcliente = c.Idcliente, | ||
| NomeCliente = c.NomeCliente, | ||
| DataNascCliente = c.DataNascCliente, | ||
| NifCliente = c.Nifcliente, | ||
| RuaCliente = c.RuaCliente, | ||
| CodigoPostal = c.CodigoPostalCpNavigation != null ? c.CodigoPostalCpNavigation.Cp.ToString("0000000") : null, | ||
| Localidade = c.CodigoPostalCpNavigation != null ? c.CodigoPostalCpNavigation.Localidade : null, | ||
| Email = c.LoginIdloginNavigation != null ? c.LoginIdloginNavigation.Email : null, | ||
|
Comment on lines
+107
to
+111
|
||
| ContactoC1 = c.ContactoC1, | ||
| ContactoC2 = c.ContactoC2, | ||
| EstadoValCc = c.EstadoValCc | ||
| }) | ||
| .FirstOrDefaultAsync(); | ||
|
|
||
| if (cliente == null) return NotFound(); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,204 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Threading.Tasks; | ||
| using System.Security.Claims; | ||
| using Microsoft.AspNetCore.Http; | ||
| using Microsoft.AspNetCore.Mvc; | ||
| using Microsoft.EntityFrameworkCore; | ||
| using RESTful_API.Controllers; | ||
| using RESTful_API.Models; | ||
| using Xunit; | ||
|
|
||
| namespace Unit_Tests | ||
| { | ||
| public class ClientesControllerTests | ||
| { | ||
| private PdsContext GetDbContextWithData() | ||
| { | ||
| var options = new DbContextOptionsBuilder<PdsContext>() | ||
| .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()) | ||
| .Options; | ||
| var context = new PdsContext(options); | ||
|
Comment on lines
+19
to
+22
|
||
|
|
||
| var login1 = new Login { Idlogin = 1, Email = "test@test.com", HashPassword = "hashedpassword" }; | ||
| var login2 = new Login { Idlogin = 2, Email = "test2@test.com", HashPassword = "hashedpassword2" }; | ||
|
|
||
| var cp = new CodigoPostal { Cp = 1234567, Localidade = "Lisboa" }; | ||
|
|
||
| var cliente1 = new Cliente | ||
| { | ||
| Idcliente = 1, | ||
| NomeCliente = "Joao", | ||
| Nifcliente = 123456789, | ||
| DataNascCliente = new DateTime(1990, 1, 1), | ||
| RuaCliente = "Rua A", | ||
| CodigoPostalCp = 1234567, | ||
| CodigoPostalCpNavigation = cp, | ||
| LoginIdlogin = 1, | ||
| LoginIdloginNavigation = login1, | ||
| ContactoC1 = 910000000, | ||
| EstadoValCc = true | ||
| }; | ||
|
|
||
| var cliente2 = new Cliente | ||
| { | ||
| Idcliente = 2, | ||
| NomeCliente = "Maria", | ||
| Nifcliente = 987654321, | ||
| DataNascCliente = new DateTime(1995, 1, 1), | ||
| RuaCliente = "Rua B", | ||
| CodigoPostalCp = 1234567, | ||
| CodigoPostalCpNavigation = cp, | ||
| LoginIdlogin = 2, | ||
| LoginIdloginNavigation = login2, | ||
| ContactoC1 = 920000000, | ||
| EstadoValCc = false | ||
| }; | ||
|
|
||
| context.Logins.AddRange(login1, login2); | ||
| context.CodigoPostals.Add(cp); | ||
| context.Clientes.AddRange(cliente1, cliente2); | ||
| context.SaveChanges(); | ||
|
|
||
| return context; | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task GetCliente_ValidId_ReturnsCliente() | ||
| { | ||
| var context = GetDbContextWithData(); | ||
| var controller = new ClientesController(context); | ||
|
|
||
| var result = await controller.GetCliente(1); | ||
|
|
||
| var actionResult = Assert.IsType<ActionResult<ClienteResponseDto>>(result); | ||
| var clienteResponse = Assert.IsType<ClienteResponseDto>(actionResult.Value); | ||
| Assert.Equal("Joao", clienteResponse.NomeCliente); | ||
| Assert.Equal(123456789, clienteResponse.NifCliente); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task GetCliente_InvalidId_ReturnsNotFound() | ||
| { | ||
| var context = GetDbContextWithData(); | ||
| var controller = new ClientesController(context); | ||
|
|
||
| var result = await controller.GetCliente(999); | ||
|
|
||
| Assert.IsType<NotFoundResult>(result.Result); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task GetMe_ValidUser_ReturnsCliente() | ||
| { | ||
| var context = GetDbContextWithData(); | ||
| var controller = new ClientesController(context); | ||
|
|
||
| var user = new ClaimsPrincipal(new ClaimsIdentity(new[] | ||
| { | ||
| new Claim(ClaimTypes.NameIdentifier, "1") | ||
| }, "mock")); | ||
|
Comment on lines
+92
to
+101
|
||
|
|
||
| controller.ControllerContext = new ControllerContext | ||
| { | ||
| HttpContext = new DefaultHttpContext { User = user } | ||
| }; | ||
|
|
||
| var result = await controller.GetMe(); | ||
|
|
||
| var okResult = Assert.IsType<OkObjectResult>(result.Result); | ||
| var clienteResponse = Assert.IsType<ClienteResponseDto>(okResult.Value); | ||
| Assert.Equal("Joao", clienteResponse.NomeCliente); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task GetClientesAdmin_ReturnsAllClientes() | ||
| { | ||
| var context = GetDbContextWithData(); | ||
| var controller = new ClientesController(context); | ||
|
|
||
| var user = new ClaimsPrincipal(new ClaimsIdentity(new[] | ||
| { | ||
| new Claim(ClaimTypes.NameIdentifier, "1"), | ||
| new Claim("roleId", "3") // Admin role | ||
| }, "mock")); | ||
|
|
||
|
Comment on lines
+115
to
+126
|
||
| controller.ControllerContext = new ControllerContext | ||
| { | ||
| HttpContext = new DefaultHttpContext { User = user } | ||
| }; | ||
|
|
||
| var login1 = context.Logins.Find(1); | ||
| login1.TipoLoginIdtlogin = 3; | ||
| context.SaveChanges(); | ||
|
|
||
| var result = await controller.GetClientesAdmin(); | ||
|
|
||
| var actionResult = Assert.IsType<ActionResult<List<ClienteResponseDtoAdmin>>>(result); | ||
| var list = Assert.IsType<List<ClienteResponseDtoAdmin>>(actionResult.Value); | ||
| Assert.Equal(2, list.Count); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task PostCliente_ValidCliente_ReturnsCreated() | ||
| { | ||
| var context = GetDbContextWithData(); | ||
| var controller = new ClientesController(context); | ||
|
|
||
| var newLogin = new Login { Idlogin = 3, Email = "new@test.com", HashPassword = "pwd" }; | ||
| context.Logins.Add(newLogin); | ||
| context.SaveChanges(); | ||
|
|
||
| var clienteDto = new ClienteCreateDto | ||
| { | ||
| NomeCliente = "Carlos", | ||
| NifCliente = 111222333, | ||
| DataNascCliente = new DateTime(2000, 1, 1), | ||
| RuaCliente = "Rua C", | ||
| CodigoPostal = "1234567", | ||
| Localidade = "Lisboa", | ||
| LoginIdlogin = 3, | ||
| ContactoC1 = 930000000 | ||
| }; | ||
|
|
||
| var result = await controller.PostCliente(clienteDto); | ||
|
|
||
| var createdAtActionResult = Assert.IsType<CreatedAtActionResult>(result.Result); | ||
| var createdCliente = Assert.IsType<ClienteResponseDto>(createdAtActionResult.Value); | ||
|
|
||
| Assert.Equal("Carlos", createdCliente.NomeCliente); | ||
| Assert.Equal("1234567", createdCliente.CodigoPostal); | ||
|
|
||
| var dbCliente = context.Clientes.FirstOrDefault(c => c.NomeCliente == "Carlos"); | ||
| Assert.NotNull(dbCliente); | ||
| Assert.Equal(111222333, dbCliente.Nifcliente); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task DeleteClienteAndAnonymizeLogin_ValidId_AnonymizesLogin() | ||
| { | ||
| var context = GetDbContextWithData(); | ||
| var controller = new ClientesController(context); | ||
|
|
||
| var user = new ClaimsPrincipal(new ClaimsIdentity(new[] | ||
| { | ||
| new Claim(ClaimTypes.NameIdentifier, "1"), | ||
| new Claim(ClaimTypes.Role, "admin") | ||
| }, "mock")); | ||
|
|
||
|
Comment on lines
+178
to
+189
|
||
| controller.ControllerContext = new ControllerContext | ||
| { | ||
| HttpContext = new DefaultHttpContext { User = user } | ||
| }; | ||
|
|
||
| var result = await controller.DeleteClienteAndAnonymizeLogin(2); | ||
|
|
||
| Assert.IsType<NoContentResult>(result); | ||
|
|
||
| var anonymizedLogin = context.Logins.Find(2); | ||
| Assert.Null(anonymizedLogin.Email); | ||
| Assert.Null(anonymizedLogin.HashPassword); | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Include(...)calls are unnecessary when you immediately project into a DTO (Select(new ClienteResponseDto { ... })). They add noise and can confuse future readers; EF will generate the required joins from the projection alone. Consider removing theIncludecalls for this query.