diff --git a/InvoiceReminder.API.UnitTests/Endpoints/InvoiceEndpointsTests.cs b/InvoiceReminder.API.UnitTests/Endpoints/InvoiceEndpointsTests.cs index db6c69d..e67a969 100644 --- a/InvoiceReminder.API.UnitTests/Endpoints/InvoiceEndpointsTests.cs +++ b/InvoiceReminder.API.UnitTests/Endpoints/InvoiceEndpointsTests.cs @@ -183,7 +183,7 @@ public async Task GetInvoiceById_WhenUserIsNotAuthenticated_ShouldReturnUnauthor } [TestMethod] - public async Task GetInvoiceById_WhenUserIsAuthenticatedButServiceFails_ShouldReturnInternalServerError() + public async Task GetInvoiceById_WhenUserIsAuthenticatedButServiceFails_ShouldReturnBadRequest() { // Arrange var id = Guid.NewGuid(); @@ -200,6 +200,31 @@ public async Task GetInvoiceById_WhenUserIsAuthenticatedButServiceFails_ShouldRe var response = await _client.SendAsync(request); var result = await response.Content.ReadFromJsonAsync(); + // Assert + _ = _invoiceAppService.Received(1).GetByIdAsync(Arg.Any()); + response.StatusCode.ShouldBe(HttpStatusCode.BadRequest); + _ = result.ShouldNotBeNull(); + _ = result.ShouldBeOfType(); + } + + [TestMethod] + public async Task GetInvoiceById_WhenUserIsAuthenticatedButServiceFails_ShouldReturnInternalServerError() + { + // Arrange + var id = Guid.NewGuid(); + var request = new HttpRequestMessage(HttpMethod.Get, $"/api/invoice/{id}"); + request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", "test_token"); + + _ = _invoiceAppService.GetByIdAsync(Arg.Any()).ThrowsAsync(); + + _ = _authorizationService.AuthorizeAsync(Arg.Any(), Arg.Any(), + Arg.Any>()) + .Returns(Task.FromResult(AuthorizationResult.Success())); + + // Act + var response = await _client.SendAsync(request); + var result = await response.Content.ReadFromJsonAsync(); + // Assert _ = _invoiceAppService.Received(1).GetByIdAsync(Arg.Any()); response.StatusCode.ShouldBe(HttpStatusCode.InternalServerError); @@ -290,7 +315,7 @@ public async Task GetInvoiceByBarcode_WhenUserIsNotAuthenticated_ShouldReturnUna } [TestMethod] - public async Task GetInvoiceByBarcode_WhenUserIsAuthenticatedButServiceFails_ShouldReturnInternalServerError() + public async Task GetInvoiceByBarcode_WhenUserIsAuthenticatedButServiceFails_ShouldReturnBadRequest() { // Arrange var value = "12345678901234567890123456789012345678901234"; @@ -307,6 +332,31 @@ public async Task GetInvoiceByBarcode_WhenUserIsAuthenticatedButServiceFails_Sho var response = await _client.SendAsync(request); var result = await response.Content.ReadFromJsonAsync(); + // Assert + _ = _invoiceAppService.Received(1).GetByBarcodeAsync(Arg.Any()); + response.StatusCode.ShouldBe(HttpStatusCode.BadRequest); + _ = result.ShouldNotBeNull(); + _ = result.ShouldBeOfType(); + } + + [TestMethod] + public async Task GetInvoiceByBarcode_WhenUserIsAuthenticatedButServiceFails_ShouldReturnInternalServerError() + { + // Arrange + var value = "12345678901234567890123456789012345678901234"; + var request = new HttpRequestMessage(HttpMethod.Get, $"/api/invoice/get_by_barcode/{value}"); + request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", "test_token"); + + _ = _invoiceAppService.GetByBarcodeAsync(Arg.Any()).ThrowsAsync(); + + _ = _authorizationService.AuthorizeAsync(Arg.Any(), Arg.Any(), + Arg.Any>()) + .Returns(Task.FromResult(AuthorizationResult.Success())); + + // Act + var response = await _client.SendAsync(request); + var result = await response.Content.ReadFromJsonAsync(); + // Assert _ = _invoiceAppService.Received(1).GetByBarcodeAsync(Arg.Any()); response.StatusCode.ShouldBe(HttpStatusCode.InternalServerError); diff --git a/InvoiceReminder.API.UnitTests/Endpoints/JobScheduleEndPointsTests.cs b/InvoiceReminder.API.UnitTests/Endpoints/JobScheduleEndPointsTests.cs index 40f9106..bec30f5 100644 --- a/InvoiceReminder.API.UnitTests/Endpoints/JobScheduleEndPointsTests.cs +++ b/InvoiceReminder.API.UnitTests/Endpoints/JobScheduleEndPointsTests.cs @@ -174,7 +174,7 @@ public async Task GetJobScheduleById_WhenUserIsNotAuthenticated_ShouldReturnUnau } [TestMethod] - public async Task GetJobScheduleById_WhenUserIsAuthenticatedButServiceFails_ShouldReturnInternalServerError() + public async Task GetJobScheduleById_WhenUserIsAuthenticatedButServiceFails_ShouldReturnBadRequest() { // Arrange var id = Guid.NewGuid(); @@ -191,6 +191,31 @@ public async Task GetJobScheduleById_WhenUserIsAuthenticatedButServiceFails_Shou var response = await _client.SendAsync(request); var result = await response.Content.ReadFromJsonAsync(); + // Assert + _ = _jobScheduleAppService.Received(1).GetByIdAsync(Arg.Any()); + response.StatusCode.ShouldBe(HttpStatusCode.BadRequest); + _ = result.ShouldNotBeNull(); + _ = result.ShouldBeOfType(); + } + + [TestMethod] + public async Task GetJobScheduleById_WhenUserIsAuthenticatedButServiceFails_ShouldReturnInternalServerError() + { + // Arrange + var id = Guid.NewGuid(); + var request = new HttpRequestMessage(HttpMethod.Get, $"/api/job_schedule/{id}"); + request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", "test_token"); + + _ = _jobScheduleAppService.GetByIdAsync(Arg.Any()).ThrowsAsync(new ApplicationException("Service error")); + + _ = _authorizationService.AuthorizeAsync(Arg.Any(), Arg.Any(), + Arg.Any>()) + .Returns(Task.FromResult(AuthorizationResult.Success())); + + // Act + var response = await _client.SendAsync(request); + var result = await response.Content.ReadFromJsonAsync(); + // Assert _ = _jobScheduleAppService.Received(1).GetByIdAsync(Arg.Any()); response.StatusCode.ShouldBe(HttpStatusCode.InternalServerError); @@ -285,6 +310,31 @@ public async Task GetJobScheduleByUserId_WhenUserIsNotAuthenticated_ShouldReturn response.StatusCode.ShouldBe(HttpStatusCode.Unauthorized); } + [TestMethod] + public async Task GetJobScheduleByUserId_WhenUserIsAuthenticatedButServiceFails_ShouldReturnBadRequest() + { + // Arrange + var id = Guid.NewGuid(); + var request = new HttpRequestMessage(HttpMethod.Get, $"/api/job_schedule/get_by_user_id/{id}"); + request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", "test_token"); + + _ = _jobScheduleAppService.GetByUserIdAsync(Arg.Any()).ThrowsAsync(); + + _ = _authorizationService.AuthorizeAsync(Arg.Any(), Arg.Any(), + Arg.Any>()) + .Returns(Task.FromResult(AuthorizationResult.Success())); + + // Act + var response = await _client.SendAsync(request); + var result = await response.Content.ReadFromJsonAsync(); + + // Assert + _ = _jobScheduleAppService.Received(1).GetByUserIdAsync(Arg.Any()); + response.StatusCode.ShouldBe(HttpStatusCode.BadRequest); + _ = result.ShouldNotBeNull(); + _ = result.ShouldBeOfType(); + } + [TestMethod] public async Task GetJobScheduleByUserId_WhenUserIsAuthenticatedButServiceFails_ShouldReturnInternalServerError() { @@ -293,7 +343,7 @@ public async Task GetJobScheduleByUserId_WhenUserIsAuthenticatedButServiceFails_ var request = new HttpRequestMessage(HttpMethod.Get, $"/api/job_schedule/get_by_user_id/{id}"); request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", "test_token"); - _ = _jobScheduleAppService.GetByUserIdAsync(Arg.Any()).ThrowsAsync(new ArgumentException("Service error")); + _ = _jobScheduleAppService.GetByUserIdAsync(Arg.Any()).ThrowsAsync(); _ = _authorizationService.AuthorizeAsync(Arg.Any(), Arg.Any(), Arg.Any>()) diff --git a/InvoiceReminder.API.UnitTests/Endpoints/ScanEmailDefinitionEndpointsTests.cs b/InvoiceReminder.API.UnitTests/Endpoints/ScanEmailDefinitionEndpointsTests.cs index acec8a4..2af4ec3 100644 --- a/InvoiceReminder.API.UnitTests/Endpoints/ScanEmailDefinitionEndpointsTests.cs +++ b/InvoiceReminder.API.UnitTests/Endpoints/ScanEmailDefinitionEndpointsTests.cs @@ -182,6 +182,31 @@ public async Task GetScanEmailDefinitionById_WhenUserIsNotAuthenticated_ShouldRe response.StatusCode.ShouldBe(HttpStatusCode.Unauthorized); } + [TestMethod] + public async Task GetScanEmailDefinitionById_WhenUserIsAuthenticatedButServiceFails_ShouldReturnBadRequest() + { + // Arrange + var id = Guid.NewGuid(); + var request = new HttpRequestMessage(HttpMethod.Get, $"/api/scan_email/{id}"); + request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", "test_token"); + + _ = _scanEmailDefinitionAppService.GetByIdAsync(Arg.Any()).ThrowsAsync(); + + _ = _authorizationService.AuthorizeAsync(Arg.Any(), Arg.Any(), + Arg.Any>()) + .Returns(Task.FromResult(AuthorizationResult.Success())); + + // Act + var response = await _client.SendAsync(request); + var result = await response.Content.ReadFromJsonAsync(); + + // Assert + _ = _scanEmailDefinitionAppService.Received(1).GetByIdAsync(Arg.Any()); + response.StatusCode.ShouldBe(HttpStatusCode.BadRequest); + _ = result.ShouldNotBeNull(); + _ = result.ShouldBeOfType(); + } + [TestMethod] public async Task GetScanEmailDefinitionById_WhenUserIsAuthenticatedButServiceFails_ShouldReturnInternalServerError() { @@ -190,8 +215,7 @@ public async Task GetScanEmailDefinitionById_WhenUserIsAuthenticatedButServiceFa var request = new HttpRequestMessage(HttpMethod.Get, $"/api/scan_email/{id}"); request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", "test_token"); - _ = _scanEmailDefinitionAppService.GetByIdAsync(Arg.Any()) - .ThrowsAsync(new ArgumentException("Service error")); + _ = _scanEmailDefinitionAppService.GetByIdAsync(Arg.Any()).ThrowsAsync(); _ = _authorizationService.AuthorizeAsync(Arg.Any(), Arg.Any(), Arg.Any>()) @@ -297,6 +321,31 @@ public async Task GetScanEmailDefinitionByUserId_WhenUserIsNotAuthenticated_Shou response.StatusCode.ShouldBe(HttpStatusCode.Unauthorized); } + [TestMethod] + public async Task GetScanEmailDefinitionByUserId_WhenUserIsAuthenticatedButServiceFails_ShouldReturnBadRequest() + { + // Arrange + var id = Guid.NewGuid(); + var request = new HttpRequestMessage(HttpMethod.Get, $"/api/scan_email/get_by_user_id/{id}"); + request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", "test_token"); + + _ = _scanEmailDefinitionAppService.GetByUserIdAsync(Arg.Any()).ThrowsAsync(); + + _ = _authorizationService.AuthorizeAsync(Arg.Any(), Arg.Any(), + Arg.Any>()) + .Returns(Task.FromResult(AuthorizationResult.Success())); + + // Act + var response = await _client.SendAsync(request); + var result = await response.Content.ReadFromJsonAsync(); + + // Assert + _ = _scanEmailDefinitionAppService.Received(1).GetByUserIdAsync(Arg.Any()); + response.StatusCode.ShouldBe(HttpStatusCode.BadRequest); + _ = result.ShouldNotBeNull(); + _ = result.ShouldBeOfType(); + } + [TestMethod] public async Task GetScanEmailDefinitionByUserId_WhenUserIsAuthenticatedButServiceFails_ShouldReturnInternalServerError() { @@ -305,8 +354,7 @@ public async Task GetScanEmailDefinitionByUserId_WhenUserIsAuthenticatedButServi var request = new HttpRequestMessage(HttpMethod.Get, $"/api/scan_email/get_by_user_id/{id}"); request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", "test_token"); - _ = _scanEmailDefinitionAppService.GetByUserIdAsync(Arg.Any()) - .ThrowsAsync(new ArgumentException("Service error")); + _ = _scanEmailDefinitionAppService.GetByUserIdAsync(Arg.Any()).ThrowsAsync(); _ = _authorizationService.AuthorizeAsync(Arg.Any(), Arg.Any(), Arg.Any>()) diff --git a/InvoiceReminder.API.UnitTests/Endpoints/UserEndpointsTests.cs b/InvoiceReminder.API.UnitTests/Endpoints/UserEndpointsTests.cs index 6f0ba49..6291ff5 100644 --- a/InvoiceReminder.API.UnitTests/Endpoints/UserEndpointsTests.cs +++ b/InvoiceReminder.API.UnitTests/Endpoints/UserEndpointsTests.cs @@ -179,6 +179,31 @@ public async Task GetUserById_WhenUserIsNotAuthenticated_ShouldReturnUnauthorize response.StatusCode.ShouldBe(HttpStatusCode.Unauthorized); } + [TestMethod] + public async Task GetUserById_WhenUserIsAuthenticatedButServiceFails_ShouldReturnBadRequest() + { + // Arrange + var id = Guid.NewGuid(); + var request = new HttpRequestMessage(HttpMethod.Get, $"/api/user/{id}"); + request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", "test_token"); + + _ = _userAppService.GetByIdAsync(Arg.Any()).ThrowsAsync(); + + _ = _authorizationService.AuthorizeAsync(Arg.Any(), Arg.Any(), + Arg.Any>()) + .Returns(Task.FromResult(AuthorizationResult.Success())); + + // Act + var response = await _client.SendAsync(request); + var result = await response.Content.ReadFromJsonAsync(); + + // Assert + _ = _userAppService.Received(1).GetByIdAsync(Arg.Any()); + response.StatusCode.ShouldBe(HttpStatusCode.BadRequest); + _ = result.ShouldNotBeNull(); + _ = result.ShouldBeOfType(); + } + [TestMethod] public async Task GetUserById_WhenUserIsAuthenticatedButServiceFails_ShouldReturnInternalServerError() { @@ -187,7 +212,7 @@ public async Task GetUserById_WhenUserIsAuthenticatedButServiceFails_ShouldRetur var request = new HttpRequestMessage(HttpMethod.Get, $"/api/user/{id}"); request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", "test_token"); - _ = _userAppService.GetByIdAsync(Arg.Any()).ThrowsAsync(new ArgumentException("Service error")); + _ = _userAppService.GetByIdAsync(Arg.Any()).ThrowsAsync(); _ = _authorizationService.AuthorizeAsync(Arg.Any(), Arg.Any(), Arg.Any>()) @@ -287,6 +312,31 @@ public async Task GetUserByEmail_WhenUserIsNotAuthenticated_ShouldReturnUnauthor response.StatusCode.ShouldBe(HttpStatusCode.Unauthorized); } + [TestMethod] + public async Task GetUserByEmail_WhenUserIsAuthenticatedButServiceFails_ShouldReturnBadRequest() + { + // Arrange + var value = "test_user@mail_com"; + var request = new HttpRequestMessage(HttpMethod.Get, $"/api/user/get_by_email/{value}"); + request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", "test_token"); + + _ = _userAppService.GetByEmailAsync(Arg.Any()).ThrowsAsync(); + + _ = _authorizationService.AuthorizeAsync(Arg.Any(), Arg.Any(), + Arg.Any>()) + .Returns(Task.FromResult(AuthorizationResult.Success())); + + // Act + var response = await _client.SendAsync(request); + var result = await response.Content.ReadFromJsonAsync(); + + // Assert + _ = _userAppService.Received(1).GetByEmailAsync(Arg.Any()); + response.StatusCode.ShouldBe(HttpStatusCode.BadRequest); + _ = result.ShouldNotBeNull(); + _ = result.ShouldBeOfType(); + } + [TestMethod] public async Task GetUserByEmail_WhenUserIsAuthenticatedButServiceFails_ShouldReturnInternalServerError() { @@ -295,7 +345,7 @@ public async Task GetUserByEmail_WhenUserIsAuthenticatedButServiceFails_ShouldRe var request = new HttpRequestMessage(HttpMethod.Get, $"/api/user/get_by_email/{value}"); request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", "test_token"); - _ = _userAppService.GetByEmailAsync(Arg.Any()).ThrowsAsync(new ArgumentException("Service error")); + _ = _userAppService.GetByEmailAsync(Arg.Any()).ThrowsAsync(); _ = _authorizationService.AuthorizeAsync(Arg.Any(), Arg.Any(), Arg.Any>()) diff --git a/InvoiceReminder.API/Exceptions/GlobalExceptionHandler.cs b/InvoiceReminder.API/Exceptions/GlobalExceptionHandler.cs index ba27e18..e8b1f21 100644 --- a/InvoiceReminder.API/Exceptions/GlobalExceptionHandler.cs +++ b/InvoiceReminder.API/Exceptions/GlobalExceptionHandler.cs @@ -20,7 +20,9 @@ public async ValueTask TryHandleAsync(HttpContext httpContext, Exception e httpContext.Response.StatusCode = exception switch { - ApplicationException => StatusCodes.Status400BadRequest, + ArgumentException or + ArgumentNullException or + BadHttpRequestException => StatusCodes.Status400BadRequest, _ => StatusCodes.Status500InternalServerError };