From d2e8c0867f96621fe3c1db74660e0c3c8ce623a5 Mon Sep 17 00:00:00 2001 From: Xavier Decoster Date: Sun, 7 May 2017 11:01:58 +0200 Subject: [PATCH] Use NormalizedVersion in URLs contained in PackageAddedNotice (#3886) --- src/NuGetGallery/Controllers/ApiController.cs | 4 ++-- .../Controllers/PackagesController.cs | 4 ++-- .../Services/MessageServiceFacts.cs | 23 ++++++++++++++----- 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/src/NuGetGallery/Controllers/ApiController.cs b/src/NuGetGallery/Controllers/ApiController.cs index bfa6384feb..d66dbebc1a 100644 --- a/src/NuGetGallery/Controllers/ApiController.cs +++ b/src/NuGetGallery/Controllers/ApiController.cs @@ -500,8 +500,8 @@ await AuditingService.SaveAuditRecordAsync( // Notify user of push MessageService.SendPackageAddedNotice(package, - Url.Action("DisplayPackage", "Packages", routeValues: new { id = package.PackageRegistration.Id, version = package.Version }, protocol: Request.Url.Scheme), - Url.Action("ReportMyPackage", "Packages", routeValues: new { id = package.PackageRegistration.Id, version = package.Version }, protocol: Request.Url.Scheme), + Url.Action("DisplayPackage", "Packages", routeValues: new { id = package.PackageRegistration.Id, version = package.NormalizedVersion }, protocol: Request.Url.Scheme), + Url.Action("ReportMyPackage", "Packages", routeValues: new { id = package.PackageRegistration.Id, version = package.NormalizedVersion }, protocol: Request.Url.Scheme), Url.Action("Account", "Users", routeValues: null, protocol: Request.Url.Scheme)); TelemetryService.TrackPackagePushEvent(package, user, User.Identity); diff --git a/src/NuGetGallery/Controllers/PackagesController.cs b/src/NuGetGallery/Controllers/PackagesController.cs index 3cdb65743d..92749faa74 100644 --- a/src/NuGetGallery/Controllers/PackagesController.cs +++ b/src/NuGetGallery/Controllers/PackagesController.cs @@ -1235,8 +1235,8 @@ await _auditingService.SaveAuditRecordAsync( // notify user _messageService.SendPackageAddedNotice(package, - Url.Action("DisplayPackage", "Packages", routeValues: new { id = package.PackageRegistration.Id, version = package.Version }, protocol: Request.Url.Scheme), - Url.Action("ReportMyPackage", "Packages", routeValues: new { id = package.PackageRegistration.Id, version = package.Version }, protocol: Request.Url.Scheme), + Url.Action("DisplayPackage", "Packages", routeValues: new { id = package.PackageRegistration.Id, version = package.NormalizedVersion }, protocol: Request.Url.Scheme), + Url.Action("ReportMyPackage", "Packages", routeValues: new { id = package.PackageRegistration.Id, version = package.NormalizedVersion }, protocol: Request.Url.Scheme), Url.Action("Account", "Users", routeValues: null, protocol: Request.Url.Scheme)); } diff --git a/tests/NuGetGallery.Facts/Services/MessageServiceFacts.cs b/tests/NuGetGallery.Facts/Services/MessageServiceFacts.cs index aef8174bde..171fec1a76 100644 --- a/tests/NuGetGallery.Facts/Services/MessageServiceFacts.cs +++ b/tests/NuGetGallery.Facts/Services/MessageServiceFacts.cs @@ -14,6 +14,7 @@ using NuGetGallery.Framework; using NuGetGallery.Infrastructure.Authentication; using Xunit; +using NuGet.Versioning; namespace NuGetGallery { @@ -576,10 +577,17 @@ public void ApiKeyAddedMessageIsCorrect() public class TheSendPackageAddedNoticeMethod { - [Fact] - public void WillSendEmailToAllOwners() + [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", @@ -591,14 +599,17 @@ public void WillSendEmailToAllOwners() }; var package = new Package { - Version = "1.2.3", + Version = version, PackageRegistration = packageRegistration }; packageRegistration.Packages.Add(package); // Act var messageService = new TestableMessageService(); - messageService.SendPackageAddedNotice(package, "http://dummy1", "http://dummy2", "http://dummy3"); + var packageUrl = $"https://localhost/packages/{packageRegistration.Id}/{nugetVersion.ToNormalizedString()}"; + var supportUrl = $"https://localhost/packages/{packageRegistration.Id}/{nugetVersion.ToNormalizedString()}/ReportMyPackage"; + var emailSettingsUrl = "https://localhost/account"; + messageService.SendPackageAddedNotice(package, packageUrl, supportUrl, emailSettingsUrl); // Assert var message = messageService.MockMailSender.Sent.Last(); @@ -606,9 +617,9 @@ public void WillSendEmailToAllOwners() 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 published - smangit 1.2.3", message.Subject); + Assert.Contains($"[Joe Shmoe] Package published - {packageRegistration.Id} {nugetVersion.ToNormalizedString()}", message.Subject); Assert.Contains( - "The package [smangit 1.2.3](http://dummy1) was just published on Joe Shmoe. If this was not intended, please [contact support](http://dummy2).", message.Body); + $"The package [{packageRegistration.Id} {nugetVersion.ToFullString()}]({packageUrl}) was just published on Joe Shmoe. If this was not intended, please [contact support]({supportUrl}).", message.Body); } [Fact]