static UsersController CreateController(
            GallerySetting settings = null,
            Mock<IFormsAuthenticationService> formsAuthSvc = null,
            Mock<IUserService> userSvc = null,
            Mock<IMessageService> messageSvc = null,
            Mock<IPrincipal> currentUser = null)
        {
            formsAuthSvc = formsAuthSvc ?? new Mock<IFormsAuthenticationService>();
            userSvc = userSvc ?? new Mock<IUserService>();
            var packageService = new Mock<IPackageService>();
            messageSvc = messageSvc ?? new Mock<IMessageService>();
            settings = settings ?? new GallerySetting();

            if (currentUser == null)
            {
                currentUser = new Mock<IPrincipal>();
                currentUser.Setup(u => u.Identity.Name).Returns((string)null);
            }

            var controller = new UsersController(
                userSvc.Object,
                packageService.Object,
                messageSvc.Object,
                settings,
                currentUser.Object);

            TestUtility.SetupHttpContextMockForUrlGeneration(new Mock<HttpContextBase>(), controller);
            return controller;
        }
        static UserService CreateUsersService(
            GallerySetting settings = null,
            Mock <ICryptographyService> cryptoSvc     = null,
            Mock <IEntityRepository <User> > userRepo = null,
            Action <Mock <UserService> > setup        = null)
        {
            if (settings == null)
            {
                settings = new GallerySetting {
                    ConfirmEmailAddresses = true
                };
            }
            cryptoSvc = cryptoSvc ?? new Mock <ICryptographyService>();
            userRepo  = userRepo ?? new Mock <IEntityRepository <User> >();

            var userSvc = new Mock <UserService>(
                settings,
                cryptoSvc.Object,
                userRepo.Object);

            userSvc.CallBase = true;

            if (setup != null)
            {
                setup(userSvc);
            }

            return(userSvc.Object);
        }
            public void WillSendEmailToAllOwners()
            {
                var from = new MailAddress("*****@*****.**", "flossy");
                var package = new PackageRegistration { Id = "smangit" };
                package.Owners = new[]
                    {
                        new User { EmailAddress = "*****@*****.**", EmailAllowed = true },
                        new User { EmailAddress = "*****@*****.**", EmailAllowed = true }
                    };
                var mailSender = new Mock<IMailSender>();
                var setting = new GallerySetting { GalleryOwnerName = "NuGet Gallery", GalleryOwnerEmail = "*****@*****.**" };
                var messageService = new MessageService(mailSender.Object, setting);
                MailMessage message = null;
                mailSender.Setup(m => m.Send(It.IsAny<MailMessage>())).Callback<MailMessage>(m => { message = m; });

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

                mailSender.Verify(m => m.Send(It.IsAny<MailMessage>()));
                Assert.Equal("*****@*****.**", message.To[0].Address);
                Assert.Equal("*****@*****.**", message.To[1].Address);
                Assert.Contains("[NuGet Gallery] Message for owners of the package 'smangit'", 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);
            }
            public void WillSaveTheNewUserAsConfirmedWhenConfigured()
            {
                var cryptoSvc = new Mock <ICryptographyService>();

                cryptoSvc
                .Setup(x => x.GenerateSaltedHash(It.IsAny <string>(), It.IsAny <string>()))
                .Returns("theHashedPassword");
                var userRepo = new Mock <IEntityRepository <User> >();
                var settings = new GallerySetting {
                    ConfirmEmailAddresses = false
                };
                var userSvc = CreateUsersService(
                    settings: settings,
                    cryptoSvc: cryptoSvc,
                    userRepo: userRepo);

                var user = userSvc.Create(
                    "theUsername",
                    "thePassword",
                    "theEmailAddress");

                userRepo.Verify(x => x.InsertOnCommit(It.Is <User>(u =>
                                                                   u.Username == "theUsername" &&
                                                                   u.HashedPassword == "theHashedPassword" &&
                                                                   u.Confirmed)));
                userRepo.Verify(x => x.CommitChanges());
            }
        private static UsersController CreateController(
            GallerySetting settings           = null,
            Mock <IUserService> userSvc       = null,
            Mock <IMessageService> messageSvc = null,
            Mock <IPrincipal> currentUser     = null)
        {
            userSvc = userSvc ?? new Mock <IUserService>();
            var packageService = new Mock <IPackageService>();

            messageSvc = messageSvc ?? new Mock <IMessageService>();
            settings   = settings ?? new GallerySetting();

            if (currentUser == null)
            {
                currentUser = new Mock <IPrincipal>();
                currentUser.Setup(u => u.Identity.Name).Returns((string)null);
            }

            var controller = new UsersController(
                userSvc.Object,
                packageService.Object,
                messageSvc.Object,
                settings,
                currentUser.Object);

            TestUtility.SetupHttpContextMockForUrlGeneration(new Mock <HttpContextBase>(), controller);
            return(controller);
        }
        static UserService CreateUsersService(
            GallerySetting settings = null,
            Mock<ICryptographyService> cryptoSvc = null,
            Mock<IEntityRepository<User>> userRepo = null,
            Action<Mock<UserService>> setup = null)
        {
            if (settings == null)
            {
                settings = new GallerySetting { ConfirmEmailAddresses = true };
            }
            cryptoSvc = cryptoSvc ?? new Mock<ICryptographyService>();
            userRepo = userRepo ?? new Mock<IEntityRepository<User>>();

            var userSvc = new Mock<UserService>(
                settings,
                cryptoSvc.Object,
                userRepo.Object);

            userSvc.CallBase = true;

            if (setup != null)
                setup(userSvc);

            return userSvc.Object;
        }
 public ApiController(IPackageService packageSvc, IPackageFileService packageFileSvc, IUserService userSvc, INuGetExeDownloaderService nugetExeDownloaderSvc, GallerySetting settings)
 {
     this.packageSvc = packageSvc;
     this.packageFileSvc = packageFileSvc;
     this.userSvc = userSvc;
     this.nugetExeDownloaderSvc = nugetExeDownloaderSvc;
     this.settings = settings;
 }
 public UsersController(IUserService userSvc, IPackageService packageService, IMessageService messageService, GallerySetting settings, IPrincipal currentUser, IUserSiteProfilesService profilesService)
 {
     userService          = userSvc;
     this.packageService  = packageService;
     this.messageService  = messageService;
     this.settings        = settings;
     this.currentUser     = currentUser;
     this.profilesService = profilesService;
 }
 public UsersController(IUserService userSvc, IPackageService packageService, IMessageService messageService, GallerySetting settings, IPrincipal currentUser, IUserSiteProfilesService profilesService)
 {
     userService = userSvc;
     this.packageService = packageService;
     this.messageService = messageService;
     this.settings = settings;
     this.currentUser = currentUser;
     this.profilesService = profilesService;
 }
Exemple #10
0
 public ApiController(IPackageService packageSvc, IScanService scanSvc, IPackageFileService packageFileSvc, IUserService userSvc, INuGetExeDownloaderService nugetExeDownloaderSvc, GallerySetting settings)
 {
     this.packageSvc            = packageSvc;
     this.scanSvc               = scanSvc;
     this.packageFileSvc        = packageFileSvc;
     this.userSvc               = userSvc;
     this.nugetExeDownloaderSvc = nugetExeDownloaderSvc;
     this.settings              = settings;
 }
 public UserService(
     GallerySetting settings,
     ICryptographyService cryptoService,
     IEntityRepository<User> userRepository)
 {
     _settings = settings;
     _cryptoService = cryptoService;
     _userRepository = userRepository;
 }
Exemple #12
0
 public UserService(
     GallerySetting settings,
     ICryptographyService cryptoSvc,
     IEntityRepository <User> userRepo)
 {
     this.settings  = settings;
     this.cryptoSvc = cryptoSvc;
     this.userRepo  = userRepo;
 }
 public UserService(
     GallerySetting settings,
     ICryptographyService cryptoSvc,
     IEntityRepository<User> userRepo)
 {
     _settings = settings;
     _cryptoSvc = cryptoSvc;
     _userRepo = userRepo;
 }
            public void ShowsDefaultThanksViewWhenConfirmingEmailAddressIsRequired()
            {
                var settings = new GallerySetting {
                    ConfirmEmailAddresses = true
                };
                var controller = CreateController(settings: settings);

                var result = controller.Thanks() as ViewResult;

                Assert.Empty(result.ViewName);
                Assert.Null(result.Model);
            }
 public UsersController(
     IUserService userSvc,
     IPackageService packageService,
     IMessageService messageService,
     GallerySetting settings,
     IPrincipal currentUser)
 {
     _userService    = userSvc;
     _packageService = packageService;
     _messageService = messageService;
     _settings       = settings;
     _currentUser    = currentUser;
 }
 public UsersController(
     IUserService userSvc,
     IPackageService packageService,
     IMessageService messageService,
     GallerySetting settings,
     IPrincipal currentUser)
 {
     this.userService    = userSvc;
     this.packageService = packageService;
     this.messageService = messageService;
     this.settings       = settings;
     this.currentUser    = currentUser;
 }
 public JsonApiController(
     IPackageService packageSvc,
     IUserService userSvc,
     IEntityRepository<PackageOwnerRequest> packageOwnerRequestRepository,
     IMessageService messageService,
     GallerySetting settings)
 {
     _packageSvc = packageSvc;
     _userSvc = userSvc;
     _packageOwnerRequestRepository = packageOwnerRequestRepository;
     _messageSvc = messageService;
     _settings = settings;
 }
 public UsersController(
     IUserService userSvc,
     IPackageService packageService,
     IMessageService messageService,
     GallerySetting settings,
     IPrincipal currentUser)
 {
     _userService = userSvc;
     _packageService = packageService;
     _messageService = messageService;
     _settings = settings;
     _currentUser = currentUser;
 }
 public UsersController(
     IUserService userSvc,
     IPackageService packageService,
     IMessageService messageService,
     GallerySetting settings,
     IPrincipal currentUser)
 {
     this.userService = userSvc;
     this.packageService = packageService;
     this.messageService = messageService;
     this.settings = settings;
     this.currentUser = currentUser;
 }
            public void ShowsConfirmViewWithModelWhenConfirmingEmailAddressIsNotRequired()
            {
                var settings = new GallerySetting {
                    ConfirmEmailAddresses = false
                };
                var controller = CreateController(settings: settings);

                var result = controller.Thanks() as ViewResult;

                Assert.Equal("Confirm", result.ViewName);
                var model = result.Model as EmailConfirmationModel;

                Assert.True(model.ConfirmingNewAccount);
                Assert.True(model.SuccessfulConfirmation);
            }
            public void SetsTheUserToConfirmedWhenEmailConfirmationIsNotEnabled()
            {
                var settings = new GallerySetting {
                    ConfirmEmailAddresses = false
                };
                var crypto = new Mock <ICryptographyService>();

                crypto.Setup(c => c.GenerateToken()).Returns("secret!");
                var userSvc = CreateUsersService(settings: settings, cryptoSvc: crypto);

                var user = userSvc.Create(
                    "theUsername",
                    "thePassword",
                    "theEmailAddress");

                Assert.Equal(true, user.Confirmed);
            }
            public void WillSendNewUserEmailIfConfirmationRequired()
            {
                var         messageSvc          = new Mock <IMessageService>();
                string      sentConfirmationUrl = null;
                MailAddress sentToAddress       = null;

                messageSvc.Setup(m => m.SendNewAccountEmail(It.IsAny <MailAddress>(), It.IsAny <string>()))
                .Callback <MailAddress, string>(
                    (to, url) =>
                {
                    sentToAddress       = to;
                    sentConfirmationUrl = url;
                });
                var userSvc = new Mock <IUserService>();

                userSvc
                .Setup(x => x.Create(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>()))
                .Returns(
                    new User
                {
                    Username               = "******",
                    EmailAddress           = "*****@*****.**",
                    EmailConfirmationToken = "confirmation"
                });
                var settings = new GallerySetting {
                    ConfirmEmailAddresses = true
                };
                var controller = CreateController(settings: settings, userSvc: userSvc, messageSvc: messageSvc);

                controller.Register(
                    new RegisterRequest
                {
                    Username     = "******",
                    Password     = "******",
                    EmailAddress = "*****@*****.**",
                });

                // We use a catch-all route for unit tests so we can see the parameters
                // are passed correctly.
                Assert.Equal("https://example.org/?Controller=Users&Action=Confirm&username=theUsername&token=confirmation", sentConfirmationUrl);
                Assert.Equal("*****@*****.**", sentToAddress.Address);
            }
            public void WillSendEmailToGalleryOwner()
            {
                var from = new MailAddress("*****@*****.**", "too");
                var package = new Package
                    {
                        PackageRegistration = new PackageRegistration { Id = "smangit" },
                        Version = "1.42.0.1"
                    };
                var mailSender = new Mock<IMailSender>();
                var setting = new GallerySetting { GalleryOwnerName = "NuGet Gallery", GalleryOwnerEmail = "*****@*****.**" };
                var messageService = new MessageService(mailSender.Object, setting);
                MailMessage message = null;
                mailSender.Setup(m => m.Send(It.IsAny<MailMessage>())).Callback<MailMessage>(m => { message = m; });

                messageService.ReportAbuse(from, package, "Abuse!");

                Assert.Equal("*****@*****.**", message.To[0].Address);
                Assert.Equal("[NuGet Gallery] Abuse Report for Package 'smangit' Version '1.42.0.1'", message.Subject);
                Assert.Contains("Abuse!", message.Body);
                Assert.Contains("User too ([email protected]) reports the package 'smangit' version '1.42.0.1' as abusive", message.Body);
            }
            public void WillNotSendEmailToOwnerThatOptsOut()
            {
                var from = new MailAddress("*****@*****.**", "flossy");
                var package = new PackageRegistration { Id = "smangit" };
                package.Owners = new[]
                    {
                        new User { EmailAddress = "*****@*****.**", EmailAllowed = true },
                        new User { EmailAddress = "*****@*****.**", EmailAllowed = false }
                    };
                var mailSender = new Mock<IMailSender>();
                var setting = new GallerySetting { GalleryOwnerName = "Joe Schmoe", GalleryOwnerEmail = "*****@*****.**" };
                var messageService = new MessageService(mailSender.Object, setting);
                MailMessage message = null;
                mailSender.Setup(m => m.Send(It.IsAny<MailMessage>())).Callback<MailMessage>(m => { message = m; });


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

                Assert.Equal("*****@*****.**", message.To[0].Address);
                Assert.Equal(1, message.To.Count);
            }
            public void WillSendEmailToNewUser()
            {
                var to = new MailAddress("*****@*****.**", "too");
                var mailSender = new Mock<IMailSender>();
                var setting = new GallerySetting { GalleryOwnerName = "NuGet Gallery", GalleryOwnerEmail = "*****@*****.**" };
                var messageService = new MessageService(mailSender.Object, setting);
                MailMessage message = null;
                mailSender.Setup(m => m.Send(It.IsAny<MailMessage>())).Callback<MailMessage>(m => { message = m; });

                messageService.SendNewAccountEmail(to, "http://example.com/confirmation-token-url");

                Assert.Equal("*****@*****.**", message.To[0].Address);
                Assert.Equal("[NuGet Gallery] Please verify your account.", message.Subject);
                Assert.Contains("http://example.com/confirmation-token-url", message.Body);
            }
            public void SendsPackageOwnerRequestConfirmationUrl()
            {
                var to = new User { Username = "******", EmailAddress = "*****@*****.**", EmailAllowed = true };
                var from = new User { Username = "******", EmailAddress = "*****@*****.**" };
                var mailSender = new Mock<IMailSender>();
                var setting = new GallerySetting { GalleryOwnerName = "NuGet Gallery", GalleryOwnerEmail = "*****@*****.**" };
                var messageService = new MessageService(mailSender.Object, setting);
                var package = new PackageRegistration { Id = "CoolStuff" };
                const string confirmationUrl = "http://example.com/confirmation-token-url";
                MailMessage message = null;
                mailSender.Setup(m => m.Send(It.IsAny<MailMessage>())).Callback<MailMessage>(m => { message = m; });

                messageService.SendPackageOwnerRequest(from, to, package, confirmationUrl);

                Assert.Equal("*****@*****.**", message.To[0].Address);
                Assert.Equal("*****@*****.**", message.From.Address);
                Assert.Equal("[NuGet Gallery] The user 'Existing' wants to add you as an owner of the package 'CoolStuff'.", message.Subject);
                Assert.Contains(confirmationUrl, message.Body);
                Assert.Contains("The user 'Existing' wants to add you as an owner of the package 'CoolStuff'.", message.Body);
            }
 public MessageService(IMailSender mailSender, GallerySetting settings)
 {
     _mailSender = mailSender;
     _settings = settings;
 }
Exemple #28
0
 public MessageService(IMailSender mailSender, GallerySetting settings)
 {
     _mailSender = mailSender;
     _settings   = settings;
 }
            public void DoesNotSendRequestIfUserDoesNotAllowEmails()
            {
                var to = new User { Username = "******", EmailAddress = "*****@*****.**", EmailAllowed = false };
                var from = new User { Username = "******", EmailAddress = "*****@*****.**" };
                var mailSender = new Mock<IMailSender>();
                mailSender.Setup(s => s.Send(It.IsAny<MailMessage>())).Throws(new InvalidOperationException("Should not be called"));
                var setting = new GallerySetting { GalleryOwnerName = "NuGet Gallery", GalleryOwnerEmail = "*****@*****.**" };
                var messageService = new MessageService(mailSender.Object, setting);
                var package = new PackageRegistration { Id = "CoolStuff" };
                const string confirmationUrl = "http://example.com/confirmation-token-url";
                MailMessage message = null;
                mailSender.Setup(m => m.Send(It.IsAny<MailMessage>())).Callback<MailMessage>(m => { message = m; });

                messageService.SendPackageOwnerRequest(from, to, package, confirmationUrl);

                Assert.Null(message);
            }
            public void WillSendInstructions()
            {
                var user = new User { EmailAddress = "*****@*****.**", Username = "******" };
                var mailSender = new Mock<IMailSender>();
                var setting = new GallerySetting { GalleryOwnerName = "NuGet Gallery", GalleryOwnerEmail = "*****@*****.**" };
                var messageService = new MessageService(mailSender.Object, setting);
                MailMessage message = null;
                mailSender.Setup(m => m.Send(It.IsAny<MailMessage>())).Callback<MailMessage>(m => { message = m; });

                messageService.SendPasswordResetInstructions(user, "http://example.com/pwd-reset-token-url");

                Assert.Equal("*****@*****.**", message.To[0].Address);
                Assert.Equal("[NuGet Gallery] Please reset your password.", message.Subject);
                Assert.Contains("Click the following link within the next", message.Body);
                Assert.Contains("http://example.com/pwd-reset-token-url", message.Body);
            }
            public void ShowsDefaultThanksViewWhenConfirmingEmailAddressIsRequired()
            {
                var settings = new GallerySetting { ConfirmEmailAddresses = true };
                var controller = CreateController(settings: settings);

                var result = controller.Thanks() as ViewResult;

                Assert.Empty(result.ViewName);
                Assert.Null(result.Model);
            }
            public void ShowsConfirmViewWithModelWhenConfirmingEmailAddressIsNotRequired()
            {
                var settings = new GallerySetting { ConfirmEmailAddresses = false };
                var controller = CreateController(settings: settings);

                var result = controller.Thanks() as ViewResult;

                Assert.Equal("Confirm", result.ViewName);
                var model = result.Model as EmailConfirmationModel;
                Assert.True(model.ConfirmingNewAccount);
                Assert.True(model.SuccessfulConfirmation);
            }
            public void WillSendNewUserEmailIfConfirmationRequired()
            {
                var messageSvc = new Mock<IMessageService>();
                string sentConfirmationUrl = null;
                MailAddress sentToAddress = null;
                messageSvc.Setup(m => m.SendNewAccountEmail(It.IsAny<MailAddress>(), It.IsAny<string>()))
                    .Callback<MailAddress, string>((to, url) =>
                    {
                        sentToAddress = to;
                        sentConfirmationUrl = url;
                    });
                var userSvc = new Mock<IUserService>();
                userSvc
                    .Setup(x => x.Create(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()))
                    .Returns(new User()
                    {
                        Username = "******",
                        EmailAddress = "*****@*****.**",
                        EmailConfirmationToken = "confirmation"
                    });
                var settings = new GallerySetting { ConfirmEmailAddresses = true };
                var controller = CreateController(settings: settings, userSvc: userSvc, messageSvc: messageSvc);

                controller.Register(new RegisterRequest()
                {
                    Username = "******",
                    Password = "******",
                    EmailAddress = "*****@*****.**",
                });

                // We use a catch-all route for unit tests so we can see the parameters
                // are passed correctly.
                Assert.Equal("https://example.org/?Controller=Users&Action=Confirm&username=theUsername&token=confirmation", sentConfirmationUrl);
                Assert.Equal("*****@*****.**", sentToAddress.Address);
            }
 public MessageService(IMailSender mailSender, GallerySetting settings)
 {
     this.mailSender = mailSender;
     this.settings = settings;
 }
            public void SetsTheUserToConfirmedWhenEmailConfirmationIsNotEnabled()
            {
                var settings = new GallerySetting { ConfirmEmailAddresses = false };
                var crypto = new Mock<ICryptographyService>();
                crypto.Setup(c => c.GenerateToken()).Returns("secret!");
                var userSvc = CreateUsersService(settings: settings, cryptoSvc: crypto);

                var user = userSvc.Create(
                    "theUsername",
                    "thePassword",
                    "theEmailAddress");

                Assert.Equal(true, user.Confirmed);
            }
 public MessageService(IMailSender mailSender, GallerySetting settings)
 {
     this.mailSender = mailSender;
     this.settings   = settings;
 }
            public void WillNotAttemptToSendIfNoOwnersAllow()
            {
                var from = new MailAddress("*****@*****.**", "flossy");
                var package = new PackageRegistration { Id = "smangit" };
                package.Owners = new[]
                    {
                        new User { EmailAddress = "*****@*****.**", EmailAllowed = false },
                        new User { EmailAddress = "*****@*****.**", EmailAllowed = false }
                    };
                var mailSender = new Mock<IMailSender>();
                mailSender.Setup(m => m.Send(It.IsAny<MailMessage>())).Throws(new InvalidOperationException());
                var setting = new GallerySetting { GalleryOwnerName = "Joe Schmoe", GalleryOwnerEmail = "*****@*****.**" };
                var messageService = new MessageService(mailSender.Object, setting);
                MailMessage message = null;
                mailSender.Setup(m => m.Send(It.IsAny<MailMessage>())).Callback<MailMessage>(m => { message = m; });

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

                mailSender.Verify(m => m.Send(It.IsAny<MailMessage>()), Times.Never());
                Assert.Null(message);
            }
            public void WillSaveTheNewUserAsConfirmedWhenConfigured()
            {
                var cryptoSvc = new Mock<ICryptographyService>();
                cryptoSvc
                    .Setup(x => x.GenerateSaltedHash(It.IsAny<string>(), It.IsAny<string>()))
                    .Returns("theHashedPassword");
                var userRepo = new Mock<IEntityRepository<User>>();
                var settings = new GallerySetting { ConfirmEmailAddresses = false };
                var userSvc = CreateUsersService(
                    settings: settings,
                    cryptoSvc: cryptoSvc,
                    userRepo: userRepo);

                var user = userSvc.Create(
                    "theUsername",
                    "thePassword",
                    "theEmailAddress");

                userRepo.Verify(x => x.InsertOnCommit(It.Is<User>(u =>
                    u.Username == "theUsername" &&
                    u.HashedPassword == "theHashedPassword" &&
                    u.Confirmed)));
                userRepo.Verify(x => x.CommitChanges());
            }