Преглед изворни кода

Log error for failed auth operations

Robert Marshall пре 3 година
родитељ
комит
b8751bf2b1

+ 9 - 4
src/Website.Tests/Data/AuthenticationProviderTests.cs

@@ -3,6 +3,7 @@ using System.Net.Http;
 using System.Threading.Tasks;
 using FluentAssertions;
 using Microsoft.Extensions.Caching.Memory;
+using Microsoft.Extensions.Logging;
 using NSubstitute;
 using Robware.Lib.ApiClient;
 using Website.Data;
@@ -22,6 +23,7 @@ namespace Website.Tests.Data {
 							 .Build();
 
 			var cache = Substitute.For<IMemoryCache>();
+			var logger = Substitute.For<ILogger<AuthenticationProvider>>();
 
 			var request = new LoginRequest {
 				Username = "username",
@@ -33,7 +35,7 @@ namespace Website.Tests.Data {
 				Password = "password"
 			};
 
-			var provider = new AuthenticationProvider(httpClient, cache, new CacheDurations());
+			var provider = new AuthenticationProvider(httpClient, cache, new CacheDurations(), logger);
 			(await provider.Authenticate(request)).Should().BeEquivalentTo(expectedUser);
 		}
 
@@ -49,13 +51,14 @@ namespace Website.Tests.Data {
 							 .Build();
 
 			var cache = Substitute.For<IMemoryCache>();
+			var logger = Substitute.For<ILogger<AuthenticationProvider>>();
 
 			var request = new LoginRequest {
 				Username = "username",
 				Password = "wrong"
 			};
 
-			var provider = new AuthenticationProvider(httpClient, cache, new CacheDurations());
+			var provider = new AuthenticationProvider(httpClient, cache, new CacheDurations(), logger);
 			(await provider.Authenticate(request)).Should().BeNull();
 		}
 
@@ -69,6 +72,7 @@ namespace Website.Tests.Data {
 							 .Build();
 
 			var cache = Substitute.For<IMemoryCache>();
+			var logger = Substitute.For<ILogger<AuthenticationProvider>>();
 
 			var request = new UpdatePasswordRequest {
 				Username = "username",
@@ -76,7 +80,7 @@ namespace Website.Tests.Data {
 				NewPassword = "new"
 			};
 
-			var provider = new AuthenticationProvider(httpClient, cache, new CacheDurations());
+			var provider = new AuthenticationProvider(httpClient, cache, new CacheDurations(), logger);
 			(await provider.UpdateUserPassword(request)).Should().BeTrue();
 		}
 
@@ -90,6 +94,7 @@ namespace Website.Tests.Data {
 							 .Build();
 
 			var cache = Substitute.For<IMemoryCache>();
+			var logger = Substitute.For<ILogger<AuthenticationProvider>>();
 
 			var request = new UpdatePasswordRequest {
 				Username = "username",
@@ -97,7 +102,7 @@ namespace Website.Tests.Data {
 				NewPassword = "new"
 			};
 
-			var provider = new AuthenticationProvider(httpClient, cache, new CacheDurations());
+			var provider = new AuthenticationProvider(httpClient, cache, new CacheDurations(), logger);
 			(await provider.UpdateUserPassword(request)).Should().BeTrue();
 		}
 	}

+ 9 - 3
src/Website/Data/AuthenticationProvider.cs

@@ -1,19 +1,24 @@
 using System.Net.Http;
 using System.Threading.Tasks;
 using Microsoft.Extensions.Caching.Memory;
+using Microsoft.Extensions.Logging;
 using Robware.Lib.ApiClient;
 using Website.Models.Auth;
 
 namespace Website.Data {
 	public class AuthenticationProvider : ApiClient, IAuthenticationProvider {
-		public AuthenticationProvider(HttpClient client, IMemoryCache cache, CacheDurations cacheDurations) : base(client, cache, cacheDurations) {
+		private readonly ILogger<AuthenticationProvider> _logger;
+
+		public AuthenticationProvider(HttpClient client, IMemoryCache cache, CacheDurations cacheDurations, ILogger<AuthenticationProvider> logger) : base(client, cache, cacheDurations) {
+			_logger = logger;
 		}
 
 		public async Task<User> Authenticate(LoginRequest request) {
 			try {
 				return await Post<User>("user/authenticate", request);
 			}
-			catch (ApiCallException) {
+			catch (ApiCallException exception) {
+				_logger.Log(LogLevel.Error, exception, "Error when attempting to authenticate user");
 				return null;
 			}
 		}
@@ -23,7 +28,8 @@ namespace Website.Data {
 				await Patch<object>("user/updatepassword", null, new {request.Username, request.OldPassword, request.NewPassword});
 				return true;
 			}
-			catch (ApiCallException) {
+			catch (ApiCallException exception) {
+				_logger.Log(LogLevel.Error, exception, "Error when attempting to update user password");
 				return false;
 			}
 		}