Permalink
Browse files

Contact Us fixes for Preview

  • Loading branch information...
1 parent a423496 commit 970ebdd24e6608bc7d1fb393834710e089d6f453 @TimLovellSmith TimLovellSmith committed Mar 25, 2013
@@ -395,13 +395,7 @@ public void SendsMessageToGalleryOwnerWithEmailOnlyWhenUnauthenticated()
{
var messageService = new Mock<IMessageService>();
messageService.Setup(
- s => s.ReportAbuse(
- It.IsAny<MailAddress>(),
- It.IsAny<Package>(),
- It.IsAny<string>(),
- "Mordor took my finger",
- It.IsAny<bool>(),
- It.IsAny<string>(), TODO));
+ s => s.ReportAbuse(It.Is<ReportPackageRequest>(r => r.Message == "Mordor took my finger")));
var package = new Package
{
PackageRegistration = new PackageRegistration { Id = "mordor" },
@@ -429,26 +423,20 @@ public void SendsMessageToGalleryOwnerWithEmailOnlyWhenUnauthenticated()
Assert.NotNull(result);
messageService.Verify(
s => s.ReportAbuse(
- It.Is<MailAddress>(m => m.Address == "frodo@hobbiton.example.com"),
- package,
- "GollumWasThere",
- "Mordor took my finger.",
- true,
- It.IsAny<string>(), TODO));
+ It.Is<ReportPackageRequest>(
+ r => r.FromAddress.Address == "frodo@hobbiton.example.com"
+ && r.Package == package
+ && r.Reason == "GollumWasThere"
+ && r.Message == "Mordor took my finger."
+ && r.AlreadyContactedOwners)));
}
[Fact]
public void SendsMessageToGalleryOwnerWithUserInfoWhenAuthenticated()
{
var messageService = new Mock<IMessageService>();
messageService.Setup(
- s => s.ReportAbuse(
- It.IsAny<MailAddress>(),
- It.IsAny<Package>(),
- It.IsAny<string>(),
- "Mordor took my finger",
- It.IsAny<bool>(),
- It.IsAny<string>(), TODO));
+ s => s.ReportAbuse(It.Is<ReportPackageRequest>(r => r.Message == "Mordor took my finger")));
var package = new Package
{
PackageRegistration = new PackageRegistration { Id = "mordor" },
@@ -479,14 +467,11 @@ public void SendsMessageToGalleryOwnerWithUserInfoWhenAuthenticated()
userService.VerifyAll();
messageService.Verify(
s => s.ReportAbuse(
- It.Is<MailAddress>(
- m => m.Address == "frodo@hobbiton.example.com"
- && m.DisplayName == "Frodo"),
- package,
- "GollumWasThere",
- It.IsAny<string>(),
- It.IsAny<bool>(),
- It.IsAny<string>(), TODO));
+ It.Is<ReportPackageRequest>(
+ r => r.Message == "Mordor took my finger"
+ && r.FromAddress.Address == "frodo@hobbiton.example.com"
+ && r.FromAddress.DisplayName == "Frodo"
+ && r.Reason == "GollumWasThere")));
}
[Fact]
@@ -1,4 +1,5 @@
using System;
+using System.Collections.ObjectModel;
using System.Net.Mail;
using AnglicanGeek.MarkdownMailer;
using Moq;
@@ -27,14 +28,75 @@ public void WillSendEmailToGalleryOwner()
MailMessage message = null;
mailSender.Setup(m => m.Send(It.IsAny<MailMessage>())).Callback<MailMessage>(m => { message = m; });
- messageService.ReportAbuse(@from, package, "Reason!", "Abuse!", true, "http://package.url/", TODO);
+ messageService.ReportAbuse(
+ new ReportPackageRequest
+ {
+ AlreadyContactedOwners = true,
+ FromAddress = from,
+ Message = "Abuse!",
+ Package = package,
+ Reason = "Reason!",
+ RequestingUser = null,
+ Url = TestUtility.MockUrlHelper(),
+ });
+
+ Assert.Equal("joe@example.com", message.To[0].Address);
+ Assert.Equal("[NuGet Gallery] Support Request for 'smangit' version 1.42.0.1 (Reason: Reason!)", message.Subject);
+ Assert.Contains("Reason!", message.Body);
+ Assert.Contains("Abuse!", message.Body);
+ Assert.Contains("too (legit@example.com)", message.Body);
+ Assert.Contains("smangit", message.Body);
+ Assert.Contains("1.42.0.1", message.Body);
+ Assert.Contains("Yes", message.Body);
+ }
+ }
+
+ public class TheReportMyPackageMethod
+ {
+ [Fact]
+ public void WillSendEmailToGalleryOwner()
+ {
+ var from = new MailAddress("legit@example.com", "too");
+ var owner = new User
+ {
+ Username = "too",
+ EmailAddress = "legit@example.com",
+ };
+ var package = new Package
+ {
+ PackageRegistration = new PackageRegistration
+ {
+ Id = "smangit",
+ Owners = new Collection<User> { owner }
+ },
+ Version = "1.42.0.1"
+ };
+ var mailSender = new Mock<IMailSender>();
+ var config = new Mock<IConfiguration>();
+ config.Setup(x => x.GalleryOwnerName).Returns("NuGet Gallery");
+ config.Setup(x => x.GalleryOwnerEmail).Returns("joe@example.com");
+ var messageService = new MessageService(mailSender.Object, config.Object);
+ MailMessage message = null;
+ mailSender.Setup(m => m.Send(It.IsAny<MailMessage>())).Callback<MailMessage>(m => { message = m; });
+
+ messageService.ReportMyPackage(
+ new ReportPackageRequest
+ {
+ FromAddress = from,
+ Message = "Abuse!",
+ Package = package,
+ Reason = "Reason!",
+ RequestingUser = owner,
+ Url = TestUtility.MockUrlHelper(),
+ });
Assert.Equal("joe@example.com", message.To[0].Address);
- Assert.Equal("[NuGet Gallery] Abuse Report for Package 'smangit' Version '1.42.0.1' (Reason: Reason!)", message.Subject);
+ Assert.Equal("[NuGet Gallery] Owner Support Request for 'smangit' version 1.42.0.1 (Reason: Reason!)", message.Subject);
Assert.Contains("Reason!", message.Body);
Assert.Contains("Abuse!", message.Body);
- Assert.Contains("User too (legit@example.com) reports", message.Body);
- Assert.Contains("package 'smangit' version '1.42.0.1'", message.Body);
+ Assert.Contains("too (legit@example.com)", message.Body);
+ Assert.Contains("smangit", message.Body);
+ Assert.Contains("1.42.0.1", message.Body);
}
}
View
@@ -1,4 +1,5 @@
using System;
+using System.Collections.Specialized;
using System.Reflection;
using System.Threading.Tasks;
using System.Web;
@@ -33,6 +34,30 @@ public static void SetupUrlHelper(Controller controller, Mock<HttpContextBase> m
controller.Url = new UrlHelper(new RequestContext(mockHttpContext.Object, new RouteData()), routes);
}
+ public static UrlHelper MockUrlHelper()
+ {
+ var mockHttpContext = new Mock<HttpContextBase>(MockBehavior.Strict);
+ var mockHttpRequest = new Mock<HttpRequestBase>(MockBehavior.Strict);
+ var mockHttpResponse = new Mock<HttpResponseBase>(MockBehavior.Strict);
+ mockHttpContext.Setup(httpContext => httpContext.Request).Returns(mockHttpRequest.Object);
+ mockHttpContext.Setup(httpContext => httpContext.Response).Returns(mockHttpResponse.Object);
+ mockHttpRequest.Setup(httpRequest => httpRequest.ApplicationPath).Returns("http://unittest.nuget.org/");
+ mockHttpRequest.Setup(httpRequest => httpRequest.ServerVariables).Returns(new NameValueCollection());
+
+ string value = null;
+ Action<string> saveValue = x =>
+ {
+ value = x;
+ };
+ Func<String> restoreValue = () => value;
+ mockHttpResponse.Setup(httpResponse => httpResponse.ApplyAppPathModifier(It.IsAny<string>()))
+ .Callback(saveValue).Returns(restoreValue);
+ var requestContext = new RequestContext(mockHttpContext.Object, new RouteData());
+ var routes = new RouteCollection();
+ Routes.RegisterRoutes(routes);
+ return new UrlHelper(requestContext, routes);
+ }
+
public static T GetAnonymousPropertyValue<T>(Object source, string propertyName)
{
var property = source.GetType().GetProperty(propertyName, BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public);
@@ -224,7 +224,7 @@ public virtual ActionResult ReportAbuse(string id, string version)
var model = new ReportAbuseViewModel
{
- AllowedReasons =
+ ReasonChoices =
{
ReportPackageReason.ContainsMaliciousCode,
ReportPackageReason.ViolatesALicenseIOwn,
@@ -252,6 +252,7 @@ public virtual ActionResult ReportAbuse(string id, string version)
}
}
+ ViewData[Constants.ReturnUrlViewDataKey] = Url.Action(ActionNames.ReportMyPackage, new {id, version});
return View(model);
}
@@ -267,15 +268,15 @@ public virtual ActionResult ReportMyPackage(string id, string version)
return HttpNotFound();
}
- // If user hit this url by constructing it manually but is not the owner, redirect them to ReportAbuse
+ // If user hit this url by e.g. constructing it manually but is not the owner, redirect them to ReportAbuse
if (!package.IsOwner(user))
{
return RedirectToAction(ActionNames.ReportAbuse, new { id, version });
}
var model = new ReportAbuseViewModel
{
- AllowedReasons =
+ ReasonChoices =
{
ReportPackageReason.ContainsMaliciousCode,
ReportPackageReason.ContainsPrivateAndConfidentialData,
@@ -307,19 +308,29 @@ public virtual ActionResult ReportAbuse(string id, string version, ReportAbuseVi
return HttpNotFound();
}
+ User user = null;
MailAddress from;
if (Request.IsAuthenticated)
{
- var user = _userService.FindByUsername(HttpContext.User.Identity.Name);
+ user = _userService.FindByUsername(HttpContext.User.Identity.Name);
from = user.ToMailAddress();
}
else
{
from = new MailAddress(reportForm.Email);
}
- _messageService.ReportAbuse(@from, package, reportForm.Reason, reportForm.Message, reportForm.AlreadyContactedOwner,
- _config.GetSiteRoot(false) + Url.Package(id, version), TODO);
+ _messageService.ReportAbuse(
+ new ReportPackageRequest
+ {
+ AlreadyContactedOwners = reportForm.AlreadyContactedOwner,
+ FromAddress = from,
+ Message = reportForm.Message,
+ Package = package,
+ Reason = reportForm.Reason,
+ RequestingUser = user,
+ Url = Url
+ });
TempData["Message"] = "Your abuse report has been sent to the gallery operators.";
return RedirectToAction(MVC.Packages.DisplayPackage(id, version));
@@ -345,7 +356,16 @@ public virtual ActionResult ReportMyPackage(string id, string version, ReportAbu
var user = _userService.FindByUsername(HttpContext.User.Identity.Name);
MailAddress from = user.ToMailAddress();
- _messageService.ReportMyPackage(from, package, reportForm.Reason, reportForm.Message, Url.Package(id, version));
+ _messageService.ReportMyPackage(
+ new ReportPackageRequest
+ {
+ FromAddress = from,
+ Message = reportForm.Message,
+ Package = package,
+ Reason = reportForm.Reason,
+ RequestingUser = user,
+ Url = Url
+ });
TempData["Message"] = "Your support request has been sent to the gallery operators.";
return RedirectToAction(MVC.Packages.DisplayPackage(id, version));
@@ -1,5 +1,6 @@
using System;
using System.Net.Mail;
+using System.Text;
using System.Web.Mvc;
namespace NuGetGallery
@@ -25,5 +26,37 @@ public class ReportPackageRequest
public string Message { get; set; }
public bool AlreadyContactedOwners { get; set; }
public UrlHelper Url { get; set; }
+
+ internal string FillIn(string subject, IConfiguration config)
+ {
+ // note, format blocks {xxx} are matched by ordinal-case-sensitive comparison
+ var ret = new StringBuilder(subject);
+ Action<string, string> substitute = (target, value) => ret.Replace(target, Escape(value));
+
+ substitute("{GalleryOwnerName}", config.GalleryOwnerName);
+ substitute("{Id}", Package.PackageRegistration.Id);
+ substitute("{Version}", Package.Version);
+ substitute("{Reason}", Reason);
+ if (RequestingUser != null)
+ {
+ substitute("{Username}", RequestingUser.Username);
+ substitute("{UserUrl}", Url.User(RequestingUser));
+ }
+ substitute("{Name}", FromAddress.DisplayName);
+ substitute("{Address}", FromAddress.Address);
+ substitute("{AlreadyContactedOwners}", AlreadyContactedOwners ? "Yes" : "No" );
+ substitute("{PackageUrl}", Url.Package(Package.PackageRegistration));
+ substitute("{VersionUrl}", Url.Package(Package));
+ substitute("{Reason}", Reason);
+ substitute("{Message}", Message);
+
+ ret.Replace(@"\{\", "{");
+ return ret.ToString();
+ }
+
+ private string Escape(string s)
+ {
+ return s.Replace("{", @"\{\");
+ }
}
}
Oops, something went wrong. Retry.

0 comments on commit 970ebdd

Please sign in to comment.