2026-04-13 21:36:09 -03:00
|
|
|
using System.Net;
|
|
|
|
|
using System.Net.Http.Json;
|
|
|
|
|
using System.Text.Json;
|
|
|
|
|
using SIGCM2.TestSupport;
|
|
|
|
|
|
|
|
|
|
namespace SIGCM2.Api.Tests.Auth;
|
|
|
|
|
|
|
|
|
|
[Collection("ApiIntegration")]
|
2026-04-15 12:50:24 -03:00
|
|
|
public class AuthControllerTests
|
2026-04-13 21:36:09 -03:00
|
|
|
{
|
|
|
|
|
private readonly HttpClient _client;
|
|
|
|
|
|
|
|
|
|
public AuthControllerTests(TestWebAppFactory factory)
|
|
|
|
|
{
|
|
|
|
|
_client = factory.CreateClient();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Scenario: happy path — valid admin credentials return 200 with token shape + usuario
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task Login_ValidCredentials_Returns200WithTokenShape()
|
|
|
|
|
{
|
|
|
|
|
var response = await _client.PostAsJsonAsync("/api/v1/auth/login", new
|
|
|
|
|
{
|
|
|
|
|
username = "admin",
|
|
|
|
|
password = "@Diego550@"
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
|
|
|
|
|
|
|
|
|
var json = await response.Content.ReadFromJsonAsync<JsonElement>();
|
|
|
|
|
Assert.True(json.TryGetProperty("accessToken", out var token), "Response missing 'accessToken'");
|
|
|
|
|
Assert.True(json.TryGetProperty("refreshToken", out var refresh), "Response missing 'refreshToken'");
|
|
|
|
|
Assert.True(json.TryGetProperty("expiresIn", out var expires), "Response missing 'expiresIn'");
|
|
|
|
|
|
|
|
|
|
Assert.False(string.IsNullOrWhiteSpace(token.GetString()), "'accessToken' must not be empty");
|
|
|
|
|
Assert.False(string.IsNullOrWhiteSpace(refresh.GetString()), "'refreshToken' must not be empty");
|
|
|
|
|
Assert.Equal(3600, expires.GetInt32());
|
|
|
|
|
|
|
|
|
|
// Contract: response must include usuario object
|
|
|
|
|
Assert.True(json.TryGetProperty("usuario", out var usuario), "Response missing 'usuario'");
|
|
|
|
|
Assert.True(usuario.TryGetProperty("id", out var id), "usuario missing 'id'");
|
|
|
|
|
Assert.True(usuario.TryGetProperty("nombre", out var nombre), "usuario missing 'nombre'");
|
|
|
|
|
Assert.True(usuario.TryGetProperty("rol", out var rol), "usuario missing 'rol'");
|
|
|
|
|
Assert.True(usuario.TryGetProperty("permisos", out var permisos), "usuario missing 'permisos'");
|
|
|
|
|
|
|
|
|
|
Assert.True(id.GetInt32() > 0, "'usuario.id' must be positive");
|
|
|
|
|
Assert.False(string.IsNullOrWhiteSpace(nombre.GetString()), "'usuario.nombre' must not be empty");
|
|
|
|
|
Assert.False(string.IsNullOrWhiteSpace(rol.GetString()), "'usuario.rol' must not be empty");
|
|
|
|
|
Assert.Equal(JsonValueKind.Array, permisos.ValueKind);
|
fix(adm-008): correcciones del verify loop
Seis ajustes post-verify detectados durante la corrida full de tests:
1. PuntoDeVentaRepository: UQ_PuntoDeVenta_Medio_AFIP (no _MedioId_NumeroAFIP)
— el catch de unique violation no disparaba → 500 en race duplicado.
2. Application.DependencyInjection: registro de 8 handlers PuntosDeVenta
— sin esto, dispatcher arrojaba "No service registered" → 500.
3. ReservarNumeroCommandHandler: backoff ampliado a 5 retries
[25, 75, 200, 500, 1200]ms para soportar 50 threads concurrentes.
4. SecuenciaComprobante: SYSTEM_VERSIONING = OFF (AD8 revisitado).
Under UPDATE concurrente sobre misma fila, el engine arroja
"transaction time earlier than period start time" — limitación
conocida de Temporal Tables con alta contención de UPDATEs.
Decisión: secuencia es operacional, no configuración → sin history.
V013 y SqlTestFixture actualizados para ser idempotentes.
5. SqlTestFixture: EnsureV013SchemaAsync idempotente + PuntoDeVenta_History
en TablesToIgnore + permiso administracion:puntos_de_venta:gestionar
en seed canónico + asignación a rol admin.
6. Tests: conteos 22→23 permisos (V013 agrega uno); repository fixtures
ignoran PuntoDeVenta_History; test UpdatePdv_WhenPdvInactive eliminado
(over-specified — spec no bloquea update en PdV inactivo, solo en Medio
padre inactivo; alineado con frontend que permite editar PdV inactivo).
Resultado: 190/190 Api.Tests y tests específicos ADM-008 verdes
(Domain 13, Application 42, Api 21 = 76 tests nuevos). El único failure
residual (AuditEventRepositoryTests.QueryAsync_Limit_EmitsCursor) es
pre-existente y no relacionado a ADM-008.
Covers: verify report CRITICAL (UQ name mismatch) + WARNINGs descubiertos
durante la ejecución (DI registro, temporal tables concurrency, permiso
fixture, counts de tests pre-existentes).
2026-04-17 13:02:35 -03:00
|
|
|
// V011 (ADM-001) adds 'administracion:secciones:gestionar' → 22
|
2026-04-17 17:41:25 -03:00
|
|
|
// V013 (ADM-008) adds 'administracion:puntos_de_venta:gestionar' → 23
|
2026-04-19 07:49:18 -03:00
|
|
|
// V014 (ADM-009) adds 'administracion:fiscal:gestionar' → 24
|
2026-04-19 09:57:11 -03:00
|
|
|
// V016 (CAT-001) adds 'catalogo:rubros:gestionar' → 25
|
2026-04-19 13:17:31 -03:00
|
|
|
// V017 (PRD-001) adds 'catalogo:tipos:gestionar' → 26
|
|
|
|
|
// V018 (PRD-002) adds 'catalogo:productos:gestionar' → 27 total
|
|
|
|
|
Assert.Equal(27, permisos.GetArrayLength());
|
2026-04-13 21:36:09 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Scenario: invalid credentials return 401 with opaque error
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task Login_InvalidCredentials_Returns401()
|
|
|
|
|
{
|
|
|
|
|
var response = await _client.PostAsJsonAsync("/api/v1/auth/login", new
|
|
|
|
|
{
|
|
|
|
|
username = "admin",
|
|
|
|
|
password = "WrongPassword1!"
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode);
|
|
|
|
|
|
|
|
|
|
var json = await response.Content.ReadFromJsonAsync<JsonElement>();
|
|
|
|
|
Assert.True(json.TryGetProperty("error", out var error));
|
|
|
|
|
Assert.Equal("Credenciales inválidas", error.GetString());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Scenario: malformed body (missing password) returns 400
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task Login_MissingPassword_Returns400()
|
|
|
|
|
{
|
|
|
|
|
var response = await _client.PostAsJsonAsync("/api/v1/auth/login", new
|
|
|
|
|
{
|
|
|
|
|
username = "admin"
|
|
|
|
|
// password intentionally missing — JSON serializes as no field
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
|
|
|
|
|
|
|
|
|
|
var json = await response.Content.ReadFromJsonAsync<JsonElement>();
|
|
|
|
|
Assert.True(json.TryGetProperty("errors", out var errors), "Response missing 'errors'");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Triangulation: empty username returns 400
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task Login_EmptyUsername_Returns400()
|
|
|
|
|
{
|
|
|
|
|
var response = await _client.PostAsJsonAsync("/api/v1/auth/login", new
|
|
|
|
|
{
|
|
|
|
|
username = "",
|
|
|
|
|
password = "@Diego550@"
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
|
|
|
|
|
}
|
2026-04-14 13:28:44 -03:00
|
|
|
|
|
|
|
|
// T-050: Refresh endpoint tests
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task Refresh_WithInvalidRefreshToken_Returns401()
|
|
|
|
|
{
|
|
|
|
|
var response = await _client.PostAsJsonAsync("/api/v1/auth/refresh", new
|
|
|
|
|
{
|
|
|
|
|
accessToken = "any.token.here",
|
|
|
|
|
refreshToken = "nonexistent_refresh_token_value_that_is_at_least_20_chars"
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task Refresh_MissingBody_Returns400()
|
|
|
|
|
{
|
|
|
|
|
var response = await _client.PostAsJsonAsync("/api/v1/auth/refresh", new
|
|
|
|
|
{
|
|
|
|
|
accessToken = "",
|
|
|
|
|
refreshToken = ""
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task Logout_WithoutBearer_Returns401()
|
|
|
|
|
{
|
|
|
|
|
var response = await _client.PostAsJsonAsync("/api/v1/auth/logout", new { });
|
|
|
|
|
Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task Login_Refresh_Logout_FullFlow()
|
|
|
|
|
{
|
|
|
|
|
// Step 1: Login to get tokens
|
|
|
|
|
var loginResp = await _client.PostAsJsonAsync("/api/v1/auth/login", new
|
|
|
|
|
{
|
|
|
|
|
username = "admin",
|
|
|
|
|
password = "@Diego550@"
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (loginResp.StatusCode == HttpStatusCode.InternalServerError)
|
|
|
|
|
{
|
|
|
|
|
// DB not available in this environment — skip gracefully
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Assert.Equal(HttpStatusCode.OK, loginResp.StatusCode);
|
|
|
|
|
var loginJson = await loginResp.Content.ReadFromJsonAsync<JsonElement>();
|
|
|
|
|
var accessToken = loginJson.GetProperty("accessToken").GetString()!;
|
|
|
|
|
var refreshToken = loginJson.GetProperty("refreshToken").GetString()!;
|
|
|
|
|
|
|
|
|
|
// Step 2: Use access token to call logout
|
|
|
|
|
using var logoutRequest = new HttpRequestMessage(HttpMethod.Post, "/api/v1/auth/logout");
|
|
|
|
|
logoutRequest.Headers.Authorization =
|
|
|
|
|
new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", accessToken);
|
|
|
|
|
logoutRequest.Content = JsonContent.Create(new { });
|
|
|
|
|
|
|
|
|
|
var logoutResp = await _client.SendAsync(logoutRequest);
|
|
|
|
|
Assert.Equal(HttpStatusCode.OK, logoutResp.StatusCode);
|
|
|
|
|
|
|
|
|
|
var logoutJson = await logoutResp.Content.ReadFromJsonAsync<JsonElement>();
|
|
|
|
|
Assert.True(logoutJson.GetProperty("success").GetBoolean());
|
|
|
|
|
|
|
|
|
|
// Step 3: After logout, refresh should fail (token revoked)
|
|
|
|
|
var refreshResp = await _client.PostAsJsonAsync("/api/v1/auth/refresh", new
|
|
|
|
|
{
|
|
|
|
|
accessToken = accessToken,
|
|
|
|
|
refreshToken = refreshToken
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
Assert.Equal(HttpStatusCode.Unauthorized, refreshResp.StatusCode);
|
|
|
|
|
}
|
2026-04-13 21:36:09 -03:00
|
|
|
}
|