Skip to content

Commit

Permalink
Add link to package in Contact Owners email (#5202)
Browse files Browse the repository at this point in the history
  • Loading branch information
Scott Bommarito authored Dec 19, 2017
1 parent f7917ff commit 8f74b0b
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 21 deletions.
1 change: 1 addition & 0 deletions src/NuGetGallery/Controllers/PackagesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -956,6 +956,7 @@ public virtual ActionResult ContactOwners(string id, ContactOwnersViewModel cont
_messageService.SendContactOwnersMessage(
fromAddress,
package,
Url.Package(package, false),
contactForm.Message,
Url.AccountSettings(relativeUrl: false),
contactForm.CopySender);
Expand Down
2 changes: 1 addition & 1 deletion src/NuGetGallery/Services/IMessageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace NuGetGallery
{
public interface IMessageService
{
void SendContactOwnersMessage(MailAddress fromAddress, PackageRegistration packageRegistration, string message, string emailSettingsUrl, bool copyFromAddress);
void SendContactOwnersMessage(MailAddress fromAddress, PackageRegistration packageRegistration, string packageUrl, string message, string emailSettingsUrl, bool copyFromAddress);
void ReportAbuse(ReportPackageRequest report);
void ReportMyPackage(ReportPackageRequest report);
void SendNewAccountEmail(MailAddress toAddress, string confirmationUrl);
Expand Down
11 changes: 6 additions & 5 deletions src/NuGetGallery/Services/MessageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,17 +129,17 @@ public void ReportMyPackage(ReportPackageRequest request)
}
}

public void SendContactOwnersMessage(MailAddress fromAddress, PackageRegistration packageRegistration, string message, string emailSettingsUrl, bool copySender)
public void SendContactOwnersMessage(MailAddress fromAddress, PackageRegistration packageRegistration, string packageUrl, string message, string emailSettingsUrl, bool copySender)
{
string subject = "[{0}] Message for owners of the package '{1}'";
string body = @"_User {0} <{1}> sends the following message to the owners of Package '{2}'._
string body = @"_User {0} <{1}> sends the following message to the owners of Package '[{2}]({3})'._
{3}
{4}
-----------------------------------------------
<em style=""font-size: 0.8em;"">
To stop receiving contact emails as an owner of this package, sign in to the {4} and
[change your email notification settings]({5}).
To stop receiving contact emails as an owner of this package, sign in to the {5} and
[change your email notification settings]({6}).
</em>";

body = String.Format(
Expand All @@ -148,6 +148,7 @@ [change your email notification settings]({5}).
fromAddress.DisplayName,
fromAddress.Address,
packageRegistration.Id,
packageUrl,
message,
Config.GalleryOwner.DisplayName,
emailSettingsUrl);
Expand Down
10 changes: 9 additions & 1 deletion tests/NuGetGallery.Facts/Controllers/PackagesControllerFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1227,6 +1227,7 @@ public void OnlyShowsOwnersWhoAllowReceivingEmails()
[Fact]
public void HtmlEncodesMessageContent()
{
var sentPackageUrl = string.Empty;
var messageService = new Mock<IMessageService>();
string sentMessage = null;
messageService.Setup(
Expand All @@ -1235,8 +1236,13 @@ public void HtmlEncodesMessageContent()
It.IsAny<PackageRegistration>(),
It.IsAny<string>(),
It.IsAny<string>(),
It.IsAny<string>(),
false))
.Callback<MailAddress, PackageRegistration, string, string, bool>((_, __, msg, ___, ____) => sentMessage = msg);
.Callback<MailAddress, PackageRegistration, string, string, string, bool>((_, __, packageUrl, msg, ____, _____) =>
{
sentPackageUrl = packageUrl;
sentMessage = msg;
});
var package = new PackageRegistration { Id = "factory" };

var packageService = new Mock<IPackageService>();
Expand All @@ -1255,6 +1261,7 @@ public void HtmlEncodesMessageContent()
var result = controller.ContactOwners("factory", model) as RedirectToRouteResult;

Assert.Equal("I like the cut of your jib. It&#39;s &lt;b&gt;bold&lt;/b&gt;.", sentMessage);
Assert.Equal(controller.Url.Package(package, false), sentPackageUrl);
}

[Fact]
Expand All @@ -1265,6 +1272,7 @@ public void CallsSendContactOwnersMessageWithUserInfo()
s => s.SendContactOwnersMessage(
It.IsAny<MailAddress>(),
It.IsAny<PackageRegistration>(),
It.IsAny<string>(),
"I like the cut of your jib",
It.IsAny<string>(), false));
var package = new PackageRegistration { Id = "factory" };
Expand Down
35 changes: 21 additions & 14 deletions tests/NuGetGallery.Facts/Services/MessageServiceFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ public void WillCopySenderIfAsked()
};
var messageService = TestableMessageService.Create(GetConfigurationService());

messageService.SendContactOwnersMessage(from, package, "Test message", "http://someurl/", true);
messageService.SendContactOwnersMessage(from, package, "http://someurl/", "Test message", "http://someotherurl/", true);

var messages = messageService.MockMailSender.Sent;
Assert.Equal(2, messages.Count);
Expand All @@ -226,30 +226,37 @@ public void WillCopySenderIfAsked()
[Fact]
public void WillSendEmailToAllOwners()
{
var from = new MailAddress("[email protected]", "flossy");
var id = "smangit";
var owner1Email = "[email protected]";
var owner2Email = "[email protected]";
var userUsername = "flossy";
var userEmail = "[email protected]";
var from = new MailAddress(userEmail, userUsername);
var package = new PackageRegistration
{
Id = "smangit",
Id = id,
Owners = new[]
{
new User { EmailAddress = "[email protected]", EmailAllowed = true },
new User { EmailAddress = "[email protected]", EmailAllowed = true }
new User { EmailAddress = owner1Email, EmailAllowed = true },
new User { EmailAddress = owner2Email, EmailAllowed = true }
}
};

var messageService = TestableMessageService.Create(GetConfigurationService());

messageService.SendContactOwnersMessage(from, package, "Test message", "http://someurl/", false);
var packageUrl = "http://packageUrl/";
messageService.SendContactOwnersMessage(from, package, packageUrl, "Test message", "http://emailSettingsUrl/", false);
var message = messageService.MockMailSender.Sent.Last();

Assert.Equal("[email protected]", message.To[0].Address);
Assert.Equal("[email protected]", message.To[1].Address);
Assert.Equal(owner1Email, message.To[0].Address);
Assert.Equal(owner2Email, message.To[1].Address);
Assert.Equal(TestGalleryOwner, message.From);
Assert.Equal("[email protected]", message.ReplyToList.Single().Address);
Assert.Contains("[Joe Shmoe] Message for owners of the package 'smangit'", message.Subject);
Assert.Equal(userEmail, message.ReplyToList.Single().Address);
Assert.Contains($"[Joe Shmoe] Message for owners of the package '{id}'", message.Subject);
Assert.Contains("Test message", message.Body);
Assert.Contains(
"User flossy &lt;[email protected]&gt; sends the following message to the owners of Package 'smangit'.", message.Body);
$"User {userUsername} &lt;{userEmail}&gt; sends the following message to the owners of Package '[{id}]({packageUrl})'.",
message.Body);
}

[Fact]
Expand All @@ -268,7 +275,7 @@ public void WillNotSendEmailToOwnerThatOptsOut()

var messageService = TestableMessageService.Create(GetConfigurationService());

messageService.SendContactOwnersMessage(from, package, "Test message", "http://someurl/", false);
messageService.SendContactOwnersMessage(from, package, "http://someurl/", "Test message", "http://someotherurl/", false);
var message = messageService.MockMailSender.Sent.Last();

Assert.Equal("[email protected]", message.To[0].Address);
Expand All @@ -290,7 +297,7 @@ public void WillNotAttemptToSendIfNoOwnersAllow()
};

var messageService = TestableMessageService.Create(GetConfigurationService());
messageService.SendContactOwnersMessage(from, package, "Test message", "http://someurl/", false);
messageService.SendContactOwnersMessage(from, package, "http://someurl/", "Test message", "http://someotherurl/", false);

Assert.Empty(messageService.MockMailSender.Sent);
}
Expand All @@ -307,7 +314,7 @@ public void WillNotCopySenderIfNoOwnersAllow()
};

var messageService = TestableMessageService.Create(GetConfigurationService());
messageService.SendContactOwnersMessage(from, package, "Test message", "http://someurl/", false);
messageService.SendContactOwnersMessage(from, package, "http://someurl/", "Test message", "http://someotherurl/", false);

Assert.Empty(messageService.MockMailSender.Sent);
}
Expand Down

0 comments on commit 8f74b0b

Please sign in to comment.