diff --git a/SecurityService.BusinessLogic/RequestHandlers/UserRequestHandler.cs b/SecurityService.BusinessLogic/RequestHandlers/UserRequestHandler.cs index b2018d23..f0019291 100644 --- a/SecurityService.BusinessLogic/RequestHandlers/UserRequestHandler.cs +++ b/SecurityService.BusinessLogic/RequestHandlers/UserRequestHandler.cs @@ -113,7 +113,8 @@ public async Task Handle(SecurityServiceCommands.CreateUserCommand comma TokenResponse token = await this.GetToken(cancellationToken); SendEmailRequest emailRequest = this.BuildEmailConfirmationRequest(newIdentityUser, uri); sendEmailResult = await this.MessagingServiceClient.SendEmail(token.AccessToken, emailRequest, cancellationToken); - // TODO: not so fussed if this fails, maybe just some logging that can be alerted on??? + if (sendEmailResult.IsFailed) + Logger.LogWarning($"Error sending email to {newIdentityUser.Email} as part of user creation {sendEmailResult}"); } if (createResult.IsFailed || addRolesToUserResult.IsFailed || addClaimsToUserResult.IsFailed) { diff --git a/SecurityService.UnitTests/RequestHandler/UserRequestHandlerTests.cs b/SecurityService.UnitTests/RequestHandler/UserRequestHandlerTests.cs index 5360e012..db44a7c9 100644 --- a/SecurityService.UnitTests/RequestHandler/UserRequestHandlerTests.cs +++ b/SecurityService.UnitTests/RequestHandler/UserRequestHandlerTests.cs @@ -67,6 +67,40 @@ await this.AuthenticationDbContext.Roles.AddAsync(new IdentityRole{ await this.AuthenticationDbContext.SaveChangesAsync(); this.SetupRequestHandlers.UserValidator.Setup(s => s.ValidateAsync(It.IsAny>(), It.IsAny())).ReturnsAsync(IdentityResult.Success); + this.SetupRequestHandlers.MessagingServiceClient.Setup(m => m.SendEmail(It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success); + + Result result = await this.RequestHandler.Handle(command, CancellationToken.None); + result.IsSuccess.ShouldBeTrue(); + + Int32 userCount = await this.AuthenticationDbContext.Users.CountAsync(); + userCount.ShouldBe(1); + } + + [Fact] + public async Task UserRequestHandler_CreateUserCommand_EmailSendFailed_RequestIsHandled() + { + SecurityServiceCommands.CreateUserCommand command = new(Guid.Parse(TestData.UserId), + TestData.GivenName, + TestData.MiddleName, + TestData.FamilyName, + TestData.UserName, + "password", + TestData.EmailAddress, + TestData.PhoneNumber, + TestData.Claims, + TestData.Roles); + + + await this.AuthenticationDbContext.Roles.AddAsync(new IdentityRole + { + Id = Guid.NewGuid().ToString(), + Name = "TESTROLE1", + NormalizedName = "TESTROLE1" + }); + await this.AuthenticationDbContext.SaveChangesAsync(); + + this.SetupRequestHandlers.UserValidator.Setup(s => s.ValidateAsync(It.IsAny>(), It.IsAny())).ReturnsAsync(IdentityResult.Success); + this.SetupRequestHandlers.MessagingServiceClient.Setup(m => m.SendEmail(It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(Result.Failure); Result result = await this.RequestHandler.Handle(command, CancellationToken.None); result.IsSuccess.ShouldBeTrue(); @@ -231,6 +265,7 @@ public async Task UserRequestHandler_CreateUserCommand_NullRoles_RequestIsHandle command = command with { Roles = null }; this.SetupRequestHandlers.UserValidator.Setup(s => s.ValidateAsync(It.IsAny>(), It.IsAny())).ReturnsAsync(IdentityResult.Success); + this.SetupRequestHandlers.MessagingServiceClient.Setup(m => m.SendEmail(It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success); var result = await this.RequestHandler.Handle(command, CancellationToken.None); result.IsSuccess.ShouldBeTrue(); @@ -244,8 +279,9 @@ public async Task UserRequestHandler_CreateUserCommand_EmptyRoles_RequestIsHandl SecurityServiceCommands.CreateUserCommand command = TestData.CreateUserCommand; command = command with { Roles = new List() }; this.SetupRequestHandlers.UserValidator.Setup(s => s.ValidateAsync(It.IsAny>(), It.IsAny())).ReturnsAsync(IdentityResult.Success); - - var result = await this.RequestHandler.Handle(command, CancellationToken.None); + this.SetupRequestHandlers.MessagingServiceClient.Setup(m => m.SendEmail(It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success); + + Result result = await this.RequestHandler.Handle(command, CancellationToken.None); result.IsSuccess.ShouldBeTrue(); Int32 userCount = await this.AuthenticationDbContext.Users.CountAsync(); diff --git a/SecurityService/Controllers/ApiResourceController.cs b/SecurityService/Controllers/ApiResourceController.cs index ae7e8871..2a919b5e 100644 --- a/SecurityService/Controllers/ApiResourceController.cs +++ b/SecurityService/Controllers/ApiResourceController.cs @@ -120,7 +120,7 @@ public async Task GetApiResources(CancellationToken cancellationT /// /// The controller name /// - public const String ControllerName = "apiresources"; + private const String ControllerName = "apiresources"; /// /// The controller route diff --git a/SecurityService/Controllers/ApiScopeController.cs b/SecurityService/Controllers/ApiScopeController.cs index c69edc35..9929b78b 100644 --- a/SecurityService/Controllers/ApiScopeController.cs +++ b/SecurityService/Controllers/ApiScopeController.cs @@ -111,7 +111,7 @@ public async Task GetApiScopes(CancellationToken cancellationToke /// /// The controller name /// - public const String ControllerName = "apiscopes"; + private const String ControllerName = "apiscopes"; /// /// The controller route diff --git a/SecurityService/Controllers/ClientController.cs b/SecurityService/Controllers/ClientController.cs index c7c6439c..43e52546 100644 --- a/SecurityService/Controllers/ClientController.cs +++ b/SecurityService/Controllers/ClientController.cs @@ -154,7 +154,7 @@ public async Task GetClients(CancellationToken cancellationToken) /// /// The controller name /// - public const String ControllerName = "clients"; + private const String ControllerName = "clients"; /// /// The controller route diff --git a/SecurityService/Controllers/DeveloperController.cs b/SecurityService/Controllers/DeveloperController.cs index f305bda3..69e2a865 100644 --- a/SecurityService/Controllers/DeveloperController.cs +++ b/SecurityService/Controllers/DeveloperController.cs @@ -54,7 +54,7 @@ public async Task GetLastSMSMessage(CancellationToken cancellatio /// /// The controller name /// - public const String ControllerName = "developer"; + private const String ControllerName = "developer"; /// /// The controller route diff --git a/SecurityService/Controllers/IdentityResourceController.cs b/SecurityService/Controllers/IdentityResourceController.cs index e5d1997e..3f950f33 100644 --- a/SecurityService/Controllers/IdentityResourceController.cs +++ b/SecurityService/Controllers/IdentityResourceController.cs @@ -147,7 +147,7 @@ public async Task GetIdentityResources(CancellationToken cancella /// /// The controller name /// - public const String ControllerName = "identityresources"; + private const String ControllerName = "identityresources"; /// /// The controller route diff --git a/SecurityService/Controllers/RoleController.cs b/SecurityService/Controllers/RoleController.cs index 12782137..6f1d0949 100644 --- a/SecurityService/Controllers/RoleController.cs +++ b/SecurityService/Controllers/RoleController.cs @@ -126,7 +126,7 @@ public async Task GetRoles(CancellationToken cancellationToken) /// /// The controller name /// - public const String ControllerName = "roles"; + private const String ControllerName = "roles"; /// /// The controller route diff --git a/SecurityService/Controllers/UserController.cs b/SecurityService/Controllers/UserController.cs index 2966a0aa..eb02c7a5 100644 --- a/SecurityService/Controllers/UserController.cs +++ b/SecurityService/Controllers/UserController.cs @@ -147,7 +147,7 @@ public async Task GetUsers([FromQuery] String userName, /// /// The controller name /// - public const String ControllerName = "users"; + private const String ControllerName = "users"; /// /// The controller route