From 93d53074206c0fb8a129d45c927464904d16f21d Mon Sep 17 00:00:00 2001 From: Andrei Grigorev Date: Tue, 6 Feb 2018 12:58:06 -0800 Subject: [PATCH] SendValidationTakingTooLongNotice method with tests (#5402) * Added SendValidationTakingTooLongNotice method and tests. * Updated with the new message * Removed the signature from the validation taking too long message. Removed the unused argument from the `SendValidationTakingTooLongNotice` method --- .../Services/CoreMessageService.cs | 38 ++++++++ .../Services/ICoreMessageService.cs | 1 + .../Services/MessageServiceFacts.cs | 89 +++++++++++++++++++ 3 files changed, 128 insertions(+) diff --git a/src/NuGetGallery.Core/Services/CoreMessageService.cs b/src/NuGetGallery.Core/Services/CoreMessageService.cs index 632872d84a..420493f98e 100644 --- a/src/NuGetGallery.Core/Services/CoreMessageService.cs +++ b/src/NuGetGallery.Core/Services/CoreMessageService.cs @@ -125,6 +125,44 @@ public void SendSignedPackageNotAllowedNotice(Package package, string packageUrl } } + public void SendValidationTakingTooLongNotice(Package package, string packageUrl) + { + string subject = "[{0}] Package validation taking longer than expected - {1} {2}"; + string body = "It is taking longer than expected for your package [{1} {2}]({3}) to get published.\n\n" + + "We are looking into it and there is no action on you at this time. We’ll send you an email notification when your package has been published.\n\n" + + "Thank you for your patience."; + + body = string.Format( + CultureInfo.CurrentCulture, + body, + CoreConfiguration.GalleryOwner.DisplayName, + package.PackageRegistration.Id, + package.Version, + packageUrl); + + subject = string.Format( + CultureInfo.CurrentCulture, + subject, + CoreConfiguration.GalleryOwner.DisplayName, + package.PackageRegistration.Id, + package.Version); + + using (var mailMessage = new MailMessage()) + { + mailMessage.Subject = subject; + mailMessage.Body = body; + mailMessage.From = CoreConfiguration.GalleryNoReplyAddress; + + AddAllOwnersToMailMessage(package.PackageRegistration, mailMessage); + + if (mailMessage.To.Any()) + { + SendMessage(mailMessage, copySender: false); + } + } + } + + protected static void AddAllOwnersToMailMessage(PackageRegistration packageRegistration, MailMessage mailMessage) { foreach (var owner in packageRegistration.Owners) diff --git a/src/NuGetGallery.Core/Services/ICoreMessageService.cs b/src/NuGetGallery.Core/Services/ICoreMessageService.cs index b86dc2b679..d59a5eb1c8 100644 --- a/src/NuGetGallery.Core/Services/ICoreMessageService.cs +++ b/src/NuGetGallery.Core/Services/ICoreMessageService.cs @@ -8,5 +8,6 @@ public interface ICoreMessageService void SendPackageAddedNotice(Package package, string packageUrl, string packageSupportUrl, string emailSettingsUrl); void SendPackageValidationFailedNotice(Package package, string packageUrl, string packageSupportUrl); void SendSignedPackageNotAllowedNotice(Package package, string packageUrl, string announcementsUrl, string twitterUrl); + void SendValidationTakingTooLongNotice(Package package, string packageUrl); } } \ No newline at end of file diff --git a/tests/NuGetGallery.Facts/Services/MessageServiceFacts.cs b/tests/NuGetGallery.Facts/Services/MessageServiceFacts.cs index a5db5df746..832fd444cb 100644 --- a/tests/NuGetGallery.Facts/Services/MessageServiceFacts.cs +++ b/tests/NuGetGallery.Facts/Services/MessageServiceFacts.cs @@ -1069,6 +1069,95 @@ public void WillSendEmailToOwnersRegardlessOfSettings(bool user1PushAllowed, boo } } + public class TheSendValidationTakingTooLongNoticeMethod + : TestContainer + { + [Theory] + [InlineData("1.2.3")] + [InlineData("1.2.3-alpha")] + [InlineData("1.2.3-alpha.1")] + [InlineData("1.2.3+metadata")] + [InlineData("1.2.3-alpha+metadata")] + [InlineData("1.2.3-alpha.1+metadata")] + public void WillSendEmailToAllOwners(string version) + { + // Arrange + var nugetVersion = new NuGetVersion(version); + var packageRegistration = new PackageRegistration + { + Id = "smangit", + Owners = new[] + { + new User { EmailAddress = "yung@example.com", NotifyPackagePushed = true }, + new User { EmailAddress = "flynt@example.com", NotifyPackagePushed = true } + } + }; + var package = new Package + { + Version = version, + PackageRegistration = packageRegistration + }; + packageRegistration.Packages.Add(package); + + // Act + var messageService = TestableMessageService.Create(GetConfigurationService()); + var packageUrl = $"https://localhost/packages/{packageRegistration.Id}/{nugetVersion.ToNormalizedString()}"; + messageService.SendValidationTakingTooLongNotice(package, packageUrl); + + // Assert + var message = messageService.MockMailSender.Sent.Last(); + + Assert.Equal("yung@example.com", message.To[0].Address); + Assert.Equal("flynt@example.com", message.To[1].Address); + Assert.Equal(TestGalleryNoReplyAddress, message.From); + Assert.Contains($"[Joe Shmoe] Package validation taking longer than expected - {packageRegistration.Id} {nugetVersion.ToNormalizedString()}", message.Subject); + Assert.Contains( + $"It is taking longer than expected for your package [{packageRegistration.Id} {nugetVersion.ToFullString()}]({packageUrl}) to get published.\n\n" + + $"We are looking into it and there is no action on you at this time. We’ll send you an email notification when your package has been published.\n\n" + + $"Thank you for your patience.", message.Body); + } + + public static IEnumerable EmailSettingsCombinations + => from u1pa in new[] { true, false } + from u2pa in new[] { true, false } + from u1ea in new[] { true, false } + from u2ea in new[] { true, false } + select new object[] { u1pa, u2pa, u1ea, u2ea }; + + [Theory] + [MemberData(nameof(EmailSettingsCombinations))] + public void WillSendEmailToOwnersRegardlessOfSettings(bool user1PushAllowed, bool user2PushAllowed, bool user1EmailAllowed, bool user2EmailAllowed) + { + // Arrange + var packageRegistration = new PackageRegistration + { + Id = "smangit", + Owners = new[] + { + new User { EmailAddress = "yung@example.com", NotifyPackagePushed = user1PushAllowed, EmailAllowed = user1EmailAllowed }, + new User { EmailAddress = "flynt@example.com", NotifyPackagePushed = user2PushAllowed, EmailAllowed = user2EmailAllowed } + } + }; + var package = new Package + { + Version = "1.2.3", + PackageRegistration = packageRegistration + }; + packageRegistration.Packages.Add(package); + + // Act + var messageService = TestableMessageService.Create(GetConfigurationService()); + messageService.SendValidationTakingTooLongNotice(package, "http://dummy1"); + + // Assert + var message = messageService.MockMailSender.Sent.Last(); + + Assert.Equal("yung@example.com", message.To[0].Address); + Assert.Equal("flynt@example.com", message.To[1].Address); + Assert.Equal(2, message.To.Count); + } + } + public class TheSendPackageUploadedNoticeMethod : TestContainer {