diff --git a/Server.UnitTest/Server.UnitTest.csproj b/Server.UnitTest/Server.UnitTest.csproj index 7b2879b..3d2fe42 100644 --- a/Server.UnitTest/Server.UnitTest.csproj +++ b/Server.UnitTest/Server.UnitTest.csproj @@ -13,7 +13,7 @@ - + diff --git a/Server.UnitTest/Services/TestStuffService.cs b/Server.UnitTest/Services/TestStuffService.cs index 7489fb5..98a9ab1 100644 --- a/Server.UnitTest/Services/TestStuffService.cs +++ b/Server.UnitTest/Services/TestStuffService.cs @@ -51,16 +51,15 @@ public class TestStuffService StfCreatedAt = DateTime.UtcNow.ToString("o") }; - private readonly SqliteConnection _connection; private readonly StuffDbContext _dbContext; private readonly IStuffService _stuffService; public TestStuffService() { - _connection = new SqliteConnection("DataSource=:memory:"); - _connection.Open(); + var connection = new SqliteConnection("DataSource=:memory:"); + connection.Open(); var options = new DbContextOptionsBuilder() - .UseSqlite(_connection) + .UseSqlite(connection) .Options; _dbContext = new StuffDbContext(options); _dbContext.Database.EnsureCreated(); @@ -71,13 +70,6 @@ public TestStuffService() _stuffService = new StuffService(_dbContext, mockHttpCtx); } - [Fact] - public void Dispose() - { - _dbContext.Dispose(); - _connection.Close(); - } - // ***** ***** ***** LIST [Fact] public async Task StuffService_GetListAsync_ShouldReturn_Ok() @@ -164,38 +156,38 @@ public async Task StuffService_SearchListAsync_ShouldThrow_ArgumentException() [Fact] public async Task StuffService_CreateAsync_ShouldReturn_Ok() { - // Arrange1 + // Arrange // Existing user _dbContext.Add(_dbUser); await _dbContext.SaveChangesAsync(); - // Act1 + // Act var serviceResult = await _stuffService.CreateAsync(DatumModelTest); - // Assert1 + // Assert int actual = serviceResult.Id.Count(x => x == '-'); int expected = 4; Assert.Equal(expected, actual); + } - // *** - // Arrange2 - // Creating user at the same time as stuff - DatumModelTest.User = null; - - // Act2 - serviceResult = await _stuffService.CreateAsync(DatumModelTest); + [Fact] + public async Task StuffService_CreateAsync_Without_User_ShouldThrow_KeyNotFoundException() + { + // Arrange + // No user in DB - // Assert2 - actual = serviceResult.Id.Count(x => x == '-'); - expected = 4; - Assert.Equal(expected, actual); + // Act + var serviceResult = _stuffService.CreateAsync(DatumModelTest); + var exception = await Record.ExceptionAsync(() => serviceResult); - // Restore - DatumModelTest.User = TestUserModel; + // Assert + Assert.NotNull(exception); + Assert.IsType(exception); + Assert.Equal("User not found.", exception.Message); } [Fact] - public async Task StuffService_CreateAsync_ShouldReturn_ArgumentException() + public async Task StuffService_CreateAsync_ShouldThrow_ArgumentException() { // Arrange DatumModelTest.Label = string.Empty; diff --git a/Server.UnitTest/Shared/TestErrorController.cs b/Server.UnitTest/Shared/TestErrorController.cs index 7f350f4..789a47f 100644 --- a/Server.UnitTest/Shared/TestErrorController.cs +++ b/Server.UnitTest/Shared/TestErrorController.cs @@ -2,6 +2,7 @@ using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; using Moq; using Server.Shared; using Xunit; @@ -15,6 +16,7 @@ public void ErrorController_NotFoundObjectResult() { // Arrange var mockEnv = Mock.Of(); + var mockLogger = Mock.Of>(); var mockException = Mock.Of(x => x.Error == new KeyNotFoundException("Not found")); var context = new DefaultHttpContext(); @@ -26,7 +28,7 @@ public void ErrorController_NotFoundObjectResult() }; // Act - IActionResult actionResult = controller.Error(mockEnv); + IActionResult actionResult = controller.Error(mockEnv, mockLogger); // Assert var notFoundResult = Assert.IsType(actionResult); @@ -40,6 +42,7 @@ public void ErrorHandlerFilter_BadRequestObjectResult_Development() { // Arrange var mockEnv = Mock.Of(x => x.EnvironmentName == "Development"); + var mockLogger = Mock.Of>(); var mockException = Mock.Of(x => x.Error == new ArgumentException("Should be displayed")); var context = new DefaultHttpContext(); @@ -51,7 +54,7 @@ public void ErrorHandlerFilter_BadRequestObjectResult_Development() }; // Act - IActionResult actionResult = controller.Error(mockEnv); + IActionResult actionResult = controller.Error(mockEnv, mockLogger); // Assert var notFoundResult = Assert.IsType(actionResult); @@ -65,6 +68,7 @@ public void ErrorHandlerFilter_BadRequestObjectResult_Production() { // Arrange var mockEnv = Mock.Of(x => x.EnvironmentName == "Production"); + var mockLogger = Mock.Of>(); var mockException = Mock.Of(x => x.Error == new ArgumentException("Should not be displayed")); var context = new DefaultHttpContext(); @@ -76,7 +80,7 @@ public void ErrorHandlerFilter_BadRequestObjectResult_Production() }; // Act - IActionResult actionResult = controller.Error(mockEnv); + IActionResult actionResult = controller.Error(mockEnv, mockLogger); // Assert var notFoundResult = Assert.IsType(actionResult); diff --git a/Server/Server.csproj b/Server/Server.csproj index 6325f58..dc13281 100644 --- a/Server/Server.csproj +++ b/Server/Server.csproj @@ -11,9 +11,9 @@ - - - + + + diff --git a/Server/Services/StuffService.cs b/Server/Services/StuffService.cs index 373c83e..69c5383 100644 --- a/Server/Services/StuffService.cs +++ b/Server/Services/StuffService.cs @@ -72,9 +72,8 @@ public async Task CreateAsync(DatumModel input) UserModel userAuth = _httpContext.HttpContext.GetCurrentUser(); TUser dbUser = await _dbContext.TUsers.FirstOrDefaultAsync(x => x.UsrId == userAuth.Id); if (dbUser == null) - { // Create and attach new user - dbUser.UsrCreatedAt = DateTime.UtcNow.ToStrDate(); - dbStuff.StfUser = dbUser; + { + throw new KeyNotFoundException("User not found."); } // Attach foreign key @@ -95,7 +94,6 @@ public async Task ReadAsync(string stuffId) .Where(x => x.StfId == stuffId) .Include(x => x.StfUser) .FirstOrDefaultAsync(); - if (dbStuff == null) { throw new KeyNotFoundException("Stuff not found."); diff --git a/Server/Shared/ErrorController.cs b/Server/Shared/ErrorController.cs index b724303..d2e6192 100644 --- a/Server/Shared/ErrorController.cs +++ b/Server/Shared/ErrorController.cs @@ -7,7 +7,7 @@ namespace Server.Shared; [ApiExplorerSettings(IgnoreApi = true)] public class ErrorController : ControllerBase { - public IActionResult Error([FromServices] IHostEnvironment env) + public IActionResult Error([FromServices] IHostEnvironment env, [FromServices] ILogger logger) { var context = HttpContext.Features.Get(); if (context == null) @@ -16,6 +16,7 @@ public IActionResult Error([FromServices] IHostEnvironment env) } var exception = context.Error; + logger.LogCritical(exception, "API_ERROR"); var msg = exception.InnerException == null ? exception.Message : exception.InnerException.Message; diff --git a/client/package-lock.json b/client/package-lock.json index 7f1894c..9716c63 100644 --- a/client/package-lock.json +++ b/client/package-lock.json @@ -8,7 +8,7 @@ "name": "svelte-netcore-identity", "version": "0.0.1", "devDependencies": { - "@axa-fr/vanilla-oidc": "^6.15.2", + "@axa-fr/vanilla-oidc": "^6.15.5", "@sveltejs/vite-plugin-svelte": "^2.0.2", "axios": "^1.2.2", "bootstrap": "^5.2.3", @@ -19,9 +19,9 @@ } }, "node_modules/@axa-fr/vanilla-oidc": { - "version": "6.15.3", - "resolved": "https://registry.npmjs.org/@axa-fr/vanilla-oidc/-/vanilla-oidc-6.15.3.tgz", - "integrity": "sha512-LYxH2trPakAhQZ/sv9XoLxCH8y31+taHpex0hzLFwsi5XgzoXLVBi1t0NPPHtoqnQ526sKBM30JuFzkbAEZs5g==", + "version": "6.15.5", + "resolved": "https://registry.npmjs.org/@axa-fr/vanilla-oidc/-/vanilla-oidc-6.15.5.tgz", + "integrity": "sha512-B5Jd0ErLzSzpnkwVMQn/UcsGE+sWi4yL4JjYCgCposw68n5wm8Gt8iuiUhfG9yzgYO37zm5pGdtS9iTsLx1Xag==", "dev": true, "hasInstallScript": true, "dependencies": { @@ -1156,9 +1156,9 @@ }, "dependencies": { "@axa-fr/vanilla-oidc": { - "version": "6.15.3", - "resolved": "https://registry.npmjs.org/@axa-fr/vanilla-oidc/-/vanilla-oidc-6.15.3.tgz", - "integrity": "sha512-LYxH2trPakAhQZ/sv9XoLxCH8y31+taHpex0hzLFwsi5XgzoXLVBi1t0NPPHtoqnQ526sKBM30JuFzkbAEZs5g==", + "version": "6.15.5", + "resolved": "https://registry.npmjs.org/@axa-fr/vanilla-oidc/-/vanilla-oidc-6.15.5.tgz", + "integrity": "sha512-B5Jd0ErLzSzpnkwVMQn/UcsGE+sWi4yL4JjYCgCposw68n5wm8Gt8iuiUhfG9yzgYO37zm5pGdtS9iTsLx1Xag==", "dev": true, "requires": { "base64-js": "1.5.1" diff --git a/client/package.json b/client/package.json index 28fdc6d..9d50244 100644 --- a/client/package.json +++ b/client/package.json @@ -9,7 +9,7 @@ "preview": "vite preview" }, "devDependencies": { - "@axa-fr/vanilla-oidc": "^6.15.2", + "@axa-fr/vanilla-oidc": "^6.15.5", "@sveltejs/vite-plugin-svelte": "^2.0.2", "axios": "^1.2.2", "bootstrap": "^5.2.3", diff --git a/client/src/lib/const.js b/client/src/lib/const.js index 2344597..40731d9 100644 --- a/client/src/lib/const.js +++ b/client/src/lib/const.js @@ -5,9 +5,9 @@ export const configuration = { silent_redirect_uri: window.location.origin + "/authentication/silent-callback", scope: "openid profile email api offline_access", authority: "https://demo.duendesoftware.com", - service_worker_relative_url: "/OidcServiceWorker.js", + // service_worker_relative_url: "/OidcServiceWorker.js", service_worker_only: false, - monitor_session: true + monitor_session: false }; export const apiErrMsg = {