mirror of
https://github.com/bitwarden/server
synced 2025-12-10 13:23:27 +00:00
[PM-19290] Skip the notification step if no admin emails are available. (#5582)
This commit is contained in:
@@ -289,6 +289,12 @@ public class AuthRequestService : IAuthRequestService
|
|||||||
{
|
{
|
||||||
var adminEmails = await GetAdminAndAccountRecoveryEmailsAsync(organizationUser.OrganizationId);
|
var adminEmails = await GetAdminAndAccountRecoveryEmailsAsync(organizationUser.OrganizationId);
|
||||||
|
|
||||||
|
if (adminEmails.Count == 0)
|
||||||
|
{
|
||||||
|
_logger.LogWarning("There are no admin emails to send to.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
await _mailService.SendDeviceApprovalRequestedNotificationEmailAsync(
|
await _mailService.SendDeviceApprovalRequestedNotificationEmailAsync(
|
||||||
adminEmails,
|
adminEmails,
|
||||||
organizationUser.OrganizationId,
|
organizationUser.OrganizationId,
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ using Bit.Core.Utilities;
|
|||||||
using Bit.Test.Common.AutoFixture;
|
using Bit.Test.Common.AutoFixture;
|
||||||
using Bit.Test.Common.AutoFixture.Attributes;
|
using Bit.Test.Common.AutoFixture.Attributes;
|
||||||
using Bit.Test.Common.Helpers;
|
using Bit.Test.Common.Helpers;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
using NSubstitute;
|
using NSubstitute;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
using GlobalSettings = Bit.Core.Settings.GlobalSettings;
|
using GlobalSettings = Bit.Core.Settings.GlobalSettings;
|
||||||
@@ -395,6 +396,87 @@ public class AuthRequestServiceTests
|
|||||||
user.Name);
|
user.Name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
[Theory, BitAutoData]
|
||||||
|
public async Task CreateAuthRequestAsync_AdminApproval_WithAdminNotifications_AndNoAdminEmails_ShouldNotSendNotificationEmails(
|
||||||
|
SutProvider<AuthRequestService> sutProvider,
|
||||||
|
AuthRequestCreateRequestModel createModel,
|
||||||
|
User user,
|
||||||
|
OrganizationUser organizationUser1)
|
||||||
|
{
|
||||||
|
createModel.Type = AuthRequestType.AdminApproval;
|
||||||
|
user.Email = createModel.Email;
|
||||||
|
organizationUser1.UserId = user.Id;
|
||||||
|
|
||||||
|
sutProvider.GetDependency<IFeatureService>()
|
||||||
|
.IsEnabled(FeatureFlagKeys.DeviceApprovalRequestAdminNotifications)
|
||||||
|
.Returns(true);
|
||||||
|
|
||||||
|
sutProvider.GetDependency<IUserRepository>()
|
||||||
|
.GetByEmailAsync(user.Email)
|
||||||
|
.Returns(user);
|
||||||
|
|
||||||
|
sutProvider.GetDependency<ICurrentContext>()
|
||||||
|
.DeviceType
|
||||||
|
.Returns(DeviceType.ChromeExtension);
|
||||||
|
|
||||||
|
sutProvider.GetDependency<ICurrentContext>()
|
||||||
|
.UserId
|
||||||
|
.Returns(user.Id);
|
||||||
|
|
||||||
|
sutProvider.GetDependency<IGlobalSettings>()
|
||||||
|
.PasswordlessAuth.KnownDevicesOnly
|
||||||
|
.Returns(false);
|
||||||
|
|
||||||
|
sutProvider.GetDependency<IOrganizationUserRepository>()
|
||||||
|
.GetManyByUserAsync(user.Id)
|
||||||
|
.Returns(new List<OrganizationUser>
|
||||||
|
{
|
||||||
|
organizationUser1,
|
||||||
|
});
|
||||||
|
|
||||||
|
sutProvider.GetDependency<IOrganizationUserRepository>()
|
||||||
|
.GetManyByMinimumRoleAsync(organizationUser1.OrganizationId, OrganizationUserType.Admin)
|
||||||
|
.Returns([]);
|
||||||
|
|
||||||
|
sutProvider.GetDependency<IOrganizationUserRepository>()
|
||||||
|
.GetManyDetailsByRoleAsync(organizationUser1.OrganizationId, OrganizationUserType.Custom)
|
||||||
|
.Returns([]);
|
||||||
|
|
||||||
|
sutProvider.GetDependency<IAuthRequestRepository>()
|
||||||
|
.CreateAsync(Arg.Any<AuthRequest>())
|
||||||
|
.Returns(c => c.ArgAt<AuthRequest>(0));
|
||||||
|
|
||||||
|
var authRequest = await sutProvider.Sut.CreateAuthRequestAsync(createModel);
|
||||||
|
|
||||||
|
Assert.Equal(organizationUser1.OrganizationId, authRequest.OrganizationId);
|
||||||
|
|
||||||
|
await sutProvider.GetDependency<IAuthRequestRepository>()
|
||||||
|
.Received(1)
|
||||||
|
.CreateAsync(Arg.Is<AuthRequest>(o => o.OrganizationId == organizationUser1.OrganizationId));
|
||||||
|
|
||||||
|
await sutProvider.GetDependency<IAuthRequestRepository>()
|
||||||
|
.Received(1)
|
||||||
|
.CreateAsync(Arg.Any<AuthRequest>());
|
||||||
|
|
||||||
|
await sutProvider.GetDependency<IEventService>()
|
||||||
|
.Received(1)
|
||||||
|
.LogUserEventAsync(user.Id, EventType.User_RequestedDeviceApproval);
|
||||||
|
|
||||||
|
await sutProvider.GetDependency<IMailService>()
|
||||||
|
.Received(0)
|
||||||
|
.SendDeviceApprovalRequestedNotificationEmailAsync(
|
||||||
|
Arg.Any<IEnumerable<string>>(),
|
||||||
|
Arg.Any<Guid>(),
|
||||||
|
Arg.Any<string>(),
|
||||||
|
Arg.Any<string>());
|
||||||
|
|
||||||
|
var expectedLogMessage = "There are no admin emails to send to.";
|
||||||
|
sutProvider.GetDependency<ILogger<AuthRequestService>>()
|
||||||
|
.Received(1)
|
||||||
|
.LogWarning(expectedLogMessage);
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Story: When an <see cref="AuthRequest"> is approved we want to update it in the database so it cannot have
|
/// Story: When an <see cref="AuthRequest"> is approved we want to update it in the database so it cannot have
|
||||||
/// it's status changed again and we want to push a notification to let the user know of the approval.
|
/// it's status changed again and we want to push a notification to let the user know of the approval.
|
||||||
|
|||||||
Reference in New Issue
Block a user