Permalink
Browse files

Merge pull request #949 from NuGet/tilovell-937-contactus_fixes_preview

Tilovell 937 contactus fixes preview
  • Loading branch information...
2 parents a390707 + 2a407c2 commit 5ea1bb2ca41c57193057fcec3f132319bd923ff1 @TimLovellSmith TimLovellSmith committed Mar 26, 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>()));
+ 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>()));
+ 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>()));
+ s => s.ReportAbuse(It.Is<ReportPackageRequest>(r => r.Message == "Mordor took my finger")));
var package = new Package
{
PackageRegistration = new PackageRegistration { Id = "mordor" },
@@ -460,15 +448,15 @@ public void SendsMessageToGalleryOwnerWithUserInfoWhenAuthenticated()
httpContext.Setup(h => h.Request.IsAuthenticated).Returns(true);
httpContext.Setup(h => h.User.Identity.Name).Returns("Frodo");
var userService = new Mock<IUserService>();
- userService.Setup(u => u.FindByUsername("Frodo")).Returns(new User { EmailAddress = "frodo@hobbiton.example.com", Username = "Frodo" });
+ userService.Setup(u => u.FindByUsername("Frodo")).Returns(new User { EmailAddress = "frodo@hobbiton.example.com", Username = "Frodo", Key = 1 });
var controller = CreateController(
packageService: packageService,
messageService: messageService,
userService: userService,
httpContext: httpContext);
var model = new ReportAbuseViewModel
{
- Message = "Mordor took my finger.",
+ Message = "Mordor took my finger",
Reason = "GollumWasThere",
};
@@ -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>()));
+ 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/");
+ 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,31 @@ 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.Url).Returns(new Uri("http://unittest.nuget.org/"));
+ 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);
@@ -108,7 +108,7 @@
@helper OwnerGravatar(User owner, int size, UrlHelper url, bool showName = true)
{
- <a class="owner" href="@url.Action(MVC.Users.Profiles(owner.Username))" title="@owner.Username">
+ <a class="owner" href="@url.User(owner)" title="@owner.Username">
@if (!String.IsNullOrEmpty(owner.EmailAddress))
{
@GravatarImage(owner.EmailAddress, owner.Username, size)
@@ -224,11 +224,11 @@ public virtual ActionResult ReportAbuse(string id, string version)
var model = new ReportAbuseViewModel
{
- AllowedReasons =
+ ReasonChoices =
{
- ReportPackageReason.ContainsMaliciousCode,
- ReportPackageReason.ViolatesALicenseIOwn,
ReportPackageReason.IsFraudulent,
+ ReportPackageReason.ViolatesALicenseIOwn,
+ ReportPackageReason.ContainsMaliciousCode,
ReportPackageReason.HasABug,
ReportPackageReason.Other
},
@@ -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,20 +268,20 @@ 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,
ReportPackageReason.PublishedWithWrongVersion,
ReportPackageReason.ReleasedInPublicByAccident,
+ ReportPackageReason.ContainsMaliciousCode,
ReportPackageReason.Other
},
ConfirmedUser = user.Confirmed,
@@ -307,19 +308,30 @@ 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));
+ var request = new ReportPackageRequest
+ {
+ AlreadyContactedOwners = reportForm.AlreadyContactedOwner,
+ FromAddress = from,
+ Message = reportForm.Message,
+ Package = package,
+ Reason = reportForm.Reason,
+ RequestingUser = user,
+ Url = Url
+ };
+ _messageService.ReportAbuse(request
+ );
TempData["Message"] = "Your abuse report has been sent to the gallery operators.";
return RedirectToAction(MVC.Packages.DisplayPackage(id, version));
@@ -345,7 +357,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,13 +1,12 @@
-using System;
-using System.Net.Mail;
+using System.Net.Mail;
namespace NuGetGallery
{
public interface IMessageService
{
void SendContactOwnersMessage(MailAddress fromAddress, PackageRegistration packageRegistration, string message, string emailSettingsUrl);
- void ReportAbuse(MailAddress fromAddress, Package package, string reason, string message, bool alreadyContactedOwners, string packageUrl);
- void ReportMyPackage(MailAddress fromAddress, Package package, string reason, string message, string packageUrl);
+ void ReportAbuse(ReportPackageRequest report);
+ void ReportMyPackage(ReportPackageRequest report);
void SendNewAccountEmail(MailAddress toAddress, string confirmationUrl);
void SendEmailChangeConfirmationNotice(MailAddress newEmailAddress, string confirmationUrl);
void SendPasswordResetInstructions(User user, string resetPasswordUrl);
Oops, something went wrong.

0 comments on commit 5ea1bb2

Please sign in to comment.