public async Task UsersShouldNotBeAbleToRegisterIfNotAllowed()
        {
            var mockUserManager = UsersMockHelper.MockUserManager <IUser>().Object;
            var settings        = new RegistrationSettings {
                UsersCanRegister = UserRegistrationType.NoRegistration
            };
            var mockSiteService = Mock.Of <ISiteService>(ss =>
                                                         ss.GetSiteSettingsAsync() == Task.FromResult(
                                                             Mock.Of <ISite>(s => s.Properties == JObject.FromObject(new { RegistrationSettings = settings }))
                                                             )
                                                         );
            var mockSmtpService = Mock.Of <ISmtpService>();

            var controller = new RegistrationController(
                mockUserManager,
                Mock.Of <IAuthorizationService>(),
                mockSiteService,
                Mock.Of <INotifier>(),
                Mock.Of <IEmailAddressValidator>(),
                Mock.Of <ILogger <RegistrationController> >(),
                Mock.Of <IHtmlLocalizer <RegistrationController> >(),
                Mock.Of <IStringLocalizer <RegistrationController> >());

            var result = await controller.Register();

            Assert.IsType <NotFoundResult>(result);

            // Post
            result = await controller.Register(new RegisterViewModel());

            Assert.IsType <NotFoundResult>(result);
        }
Ejemplo n.º 2
0
        public async Task ShouldRequireUniqueEmail()
        {
            // Arrange
            var describer    = new IdentityErrorDescriber();
            var userManager  = UsersMockHelper.MockUserManager <IUser>();
            var existingUser = new User {
                UserId = Guid.NewGuid().ToString("n"), UserName = "******", Email = "*****@*****.**"
            };

            userManager.Setup(m => m.GetUserIdAsync(existingUser)).ReturnsAsync(existingUser.UserId);
            userManager.Setup(m => m.GetUserNameAsync(existingUser)).ReturnsAsync(existingUser.UserName);
            userManager.Setup(m => m.GetEmailAsync(existingUser)).ReturnsAsync(existingUser.Email);
            userManager.Setup(m => m.FindByEmailAsync(existingUser.Email)).ReturnsAsync(existingUser);
            userManager.Setup(m => m.FindByNameAsync(existingUser.UserName)).ReturnsAsync(existingUser);

            var user = new User {
                UserId = Guid.NewGuid().ToString("n"), UserName = "******", Email = "*****@*****.**"
            };

            userManager.Setup(m => m.GetUserIdAsync(user)).ReturnsAsync(user.UserId);
            userManager.Setup(m => m.GetUserNameAsync(user)).ReturnsAsync(user.UserName);
            userManager.Setup(m => m.GetEmailAsync(user)).ReturnsAsync(user.Email);

            // Act
            var validator = userManager.Object.UserValidators.FirstOrDefault();
            var result    = await validator.ValidateAsync(userManager.Object, user);

            // Test
            Assert.False(result.Succeeded);
            Assert.Equal(describer.DuplicateEmail(user.Email).Code, result.Errors.First().Code);
        }
        private static Mock <SignInManager <TUser> > MockSignInManager <TUser>(UserManager <TUser> userManager = null) where TUser : class
        {
            var context = new Mock <HttpContext>();
            var manager = userManager ?? UsersMockHelper.MockUserManager <TUser>().Object;

            return(new Mock <SignInManager <TUser> >(
                       manager,
                       new HttpContextAccessor {
                HttpContext = context.Object
            },
                       Mock.Of <IUserClaimsPrincipalFactory <TUser> >(),
                       null,
                       null,
                       null,
                       null)
            {
                CallBase = true
            });
        }
Ejemplo n.º 4
0
        public async Task CanValidateUser()
        {
            // Arrange
            var userManager = UsersMockHelper.MockUserManager <IUser>();
            var user        = new User {
                UserId = Guid.NewGuid().ToString("n"), UserName = "******", Email = "*****@*****.**"
            };

            userManager.Setup(m => m.GetUserIdAsync(user)).ReturnsAsync(user.UserId);
            userManager.Setup(m => m.GetUserNameAsync(user)).ReturnsAsync(user.UserName);
            userManager.Setup(m => m.GetEmailAsync(user)).ReturnsAsync(user.Email);

            // Act
            var validator = userManager.Object.UserValidators.FirstOrDefault();
            var result    = await validator.ValidateAsync(userManager.Object, user);

            // Test
            Assert.True(result.Succeeded);
        }
        private static Mock <SignInManager <TUser> > MockSignInManager <TUser>(UserManager <TUser> userManager = null) where TUser : class, IUser
        {
            var context = new Mock <HttpContext>();
            var manager = userManager ?? UsersMockHelper.MockUserManager <TUser>().Object;

            var signInManager = new Mock <SignInManager <TUser> >(
                manager,
                new HttpContextAccessor {
                HttpContext = context.Object
            },
                Mock.Of <IUserClaimsPrincipalFactory <TUser> >(),
                null,
                null,
                null,
                null)
            {
                CallBase = true
            };

            signInManager.Setup(x => x.SignInAsync(It.IsAny <TUser>(), It.IsAny <bool>(), It.IsAny <string>()))
            .Returns(Task.CompletedTask);

            return(signInManager);
        }
        private static RegistrationController SetupRegistrationController(RegistrationSettings registrationSettings)
        {
            var users           = new List <IUser>();
            var mockUserManager = UsersMockHelper.MockUserManager <IUser>();

            mockUserManager.Setup(um => um.FindByEmailAsync(It.IsAny <string>()))
            .Returns <string>(e => {
                var user = users.SingleOrDefault(u => (u as User).Email == e);

                return(Task.FromResult(user));
            });

            var mockSiteService = Mock.Of <ISiteService>(ss =>
                                                         ss.GetSiteSettingsAsync() == Task.FromResult(
                                                             Mock.Of <ISite>(s => s.Properties == JObject.FromObject(new { RegistrationSettings = registrationSettings }))
                                                             )
                                                         );
            var mockSmtpService     = Mock.Of <ISmtpService>();
            var mockStringLocalizer = new Mock <IStringLocalizer <RegistrationController> >();

            mockStringLocalizer.Setup(l => l[It.IsAny <string>()])
            .Returns <string>(s => new LocalizedString(s, s));

            var userService = new Mock <IUserService>();

            userService.Setup(u => u.CreateUserAsync(It.IsAny <IUser>(), It.IsAny <string>(), It.IsAny <Action <string, string> >()))
            .Callback <IUser, string, Action <string, string> >((u, p, e) => users.Add(u));

            var controller = new RegistrationController(
                mockUserManager.Object,
                Mock.Of <IAuthorizationService>(),
                mockSiteService,
                Mock.Of <INotifier>(),
                new EmailAddressValidator(),
                Mock.Of <ILogger <RegistrationController> >(),
                Mock.Of <IHtmlLocalizer <RegistrationController> >(),
                mockStringLocalizer.Object);
            var mockServiceProvider = new Mock <IServiceProvider>();

            mockServiceProvider
            .Setup(x => x.GetService(typeof(ISmtpService)))
            .Returns(mockSmtpService);
            mockServiceProvider
            .Setup(x => x.GetService(typeof(UserManager <IUser>)))
            .Returns(mockUserManager.Object);
            mockServiceProvider
            .Setup(x => x.GetService(typeof(ISiteService)))
            .Returns(mockSiteService);
            mockServiceProvider
            .Setup(x => x.GetService(typeof(IEnumerable <IRegistrationFormEvents>)))
            .Returns(Enumerable.Empty <IRegistrationFormEvents>());
            mockServiceProvider
            .Setup(x => x.GetService(typeof(IUserService)))
            .Returns(userService.Object);
            mockServiceProvider
            .Setup(x => x.GetService(typeof(SignInManager <IUser>)))
            .Returns(MockSignInManager(mockUserManager.Object).Object);
            mockServiceProvider
            .Setup(x => x.GetService(typeof(ITempDataDictionaryFactory)))
            .Returns(Mock.Of <ITempDataDictionaryFactory>());
            mockServiceProvider
            .Setup(x => x.GetService(typeof(IObjectModelValidator)))
            .Returns(Mock.Of <IObjectModelValidator>());

            var mockHttpContext = new Mock <HttpContext>();

            mockHttpContext
            .Setup(x => x.RequestServices)
            .Returns(mockServiceProvider.Object);

            controller.ControllerContext.HttpContext = mockHttpContext.Object;

            return(controller);
        }
        public async Task UsersShouldBeAbleToRegisterIfAllowed()
        {
            var mockUserManager = UsersMockHelper.MockUserManager <IUser>().Object;
            var settings        = new RegistrationSettings {
                UsersCanRegister = UserRegistrationType.AllowRegistration
            };
            var mockSiteService = Mock.Of <ISiteService>(ss =>
                                                         ss.GetSiteSettingsAsync() == Task.FromResult(
                                                             Mock.Of <ISite>(s => s.Properties == JObject.FromObject(new { RegistrationSettings = settings }))
                                                             )
                                                         );
            var mockSmtpService = Mock.Of <ISmtpService>();

            var controller = new RegistrationController(
                mockUserManager,
                Mock.Of <IAuthorizationService>(),
                mockSiteService,
                Mock.Of <INotifier>(),
                Mock.Of <IEmailAddressValidator>(),
                Mock.Of <ILogger <RegistrationController> >(),
                Mock.Of <IHtmlLocalizer <RegistrationController> >(),
                Mock.Of <IStringLocalizer <RegistrationController> >());

            var mockServiceProvider = new Mock <IServiceProvider>();

            mockServiceProvider
            .Setup(x => x.GetService(typeof(ISmtpService)))
            .Returns(mockSmtpService);
            mockServiceProvider
            .Setup(x => x.GetService(typeof(UserManager <IUser>)))
            .Returns(mockUserManager);
            mockServiceProvider
            .Setup(x => x.GetService(typeof(ISiteService)))
            .Returns(mockSiteService);
            mockServiceProvider
            .Setup(x => x.GetService(typeof(IEnumerable <IRegistrationFormEvents>)))
            .Returns(Enumerable.Empty <IRegistrationFormEvents>());
            mockServiceProvider
            .Setup(x => x.GetService(typeof(IUserService)))
            .Returns(Mock.Of <IUserService>());
            mockServiceProvider
            .Setup(x => x.GetService(typeof(SignInManager <IUser>)))
            .Returns(MockSignInManager(mockUserManager).Object);
            mockServiceProvider
            .Setup(x => x.GetService(typeof(ITempDataDictionaryFactory)))
            .Returns(Mock.Of <ITempDataDictionaryFactory>());
            mockServiceProvider
            .Setup(x => x.GetService(typeof(IObjectModelValidator)))
            .Returns(Mock.Of <IObjectModelValidator>());

            var mockHttpContext = new Mock <HttpContext>();

            mockHttpContext
            .Setup(x => x.RequestServices)
            .Returns(mockServiceProvider.Object);

            controller.ControllerContext.HttpContext = mockHttpContext.Object;

            var result = await controller.Register();

            Assert.IsType <ViewResult>(result);

            // Post
            result = await controller.Register(new RegisterViewModel());

            Assert.IsType <ViewResult>(result);
        }
Ejemplo n.º 8
0
        private static RegistrationController SetupRegistrationController(RegistrationSettings registrationSettings)
        {
            var users           = new List <IUser>();
            var mockUserManager = UsersMockHelper.MockUserManager <IUser>();

            mockUserManager.Setup(um => um.FindByEmailAsync(It.IsAny <string>()))
            .Returns <string>(e =>
            {
                var user = users.SingleOrDefault(u => (u as User).Email == e);

                return(Task.FromResult(user));
            });

            var mockSiteService = Mock.Of <ISiteService>(ss =>
                                                         ss.GetSiteSettingsAsync() == Task.FromResult(
                                                             Mock.Of <ISite>(s => s.Properties == JObject.FromObject(new { RegistrationSettings = registrationSettings }))
                                                             )
                                                         );
            var mockSmtpService     = Mock.Of <ISmtpService>(x => x.SendAsync(It.IsAny <MailMessage>()) == Task.FromResult(SmtpResult.Success));
            var mockStringLocalizer = new Mock <IStringLocalizer <RegistrationController> >();

            mockStringLocalizer.Setup(l => l[It.IsAny <string>()])
            .Returns <string>(s => new LocalizedString(s, s));

            var userService = new Mock <IUserService>();

            userService.Setup(u => u.CreateUserAsync(It.IsAny <IUser>(), It.IsAny <string>(), It.IsAny <Action <string, string> >()))
            .Callback <IUser, string, Action <string, string> >((u, p, e) => users.Add(u))
            .ReturnsAsync <IUser, string, Action <string, string>, IUserService, IUser>((u, p, e) => u);

            var urlHelperMock = new Mock <IUrlHelper>();

            urlHelperMock.Setup(urlHelper => urlHelper.Action(It.IsAny <UrlActionContext>()));

            var mockUrlHelperFactory = new Mock <IUrlHelperFactory>();

            mockUrlHelperFactory.Setup(f => f.GetUrlHelper(It.IsAny <ActionContext>()))
            .Returns(urlHelperMock.Object);

            var mockDisplayHelper = new Mock <IDisplayHelper>();

            mockDisplayHelper.Setup(x => x.ShapeExecuteAsync(It.IsAny <IShape>()))
            .ReturnsAsync(HtmlString.Empty);

            var controller = new RegistrationController(
                mockUserManager.Object,
                Mock.Of <IAuthorizationService>(),
                mockSiteService,
                Mock.Of <INotifier>(),
                Mock.Of <ILogger <RegistrationController> >(),
                Mock.Of <IHtmlLocalizer <RegistrationController> >(),
                mockStringLocalizer.Object);

            controller.Url = urlHelperMock.Object;

            var mockServiceProvider = new Mock <IServiceProvider>();

            mockServiceProvider
            .Setup(x => x.GetService(typeof(ISmtpService)))
            .Returns(mockSmtpService);
            mockServiceProvider
            .Setup(x => x.GetService(typeof(UserManager <IUser>)))
            .Returns(mockUserManager.Object);
            mockServiceProvider
            .Setup(x => x.GetService(typeof(ISiteService)))
            .Returns(mockSiteService);
            mockServiceProvider
            .Setup(x => x.GetService(typeof(IEnumerable <IRegistrationFormEvents>)))
            .Returns(Enumerable.Empty <IRegistrationFormEvents>());
            mockServiceProvider
            .Setup(x => x.GetService(typeof(IUserService)))
            .Returns(userService.Object);
            mockServiceProvider
            .Setup(x => x.GetService(typeof(SignInManager <IUser>)))
            .Returns(MockSignInManager(mockUserManager.Object).Object);
            mockServiceProvider
            .Setup(x => x.GetService(typeof(ITempDataDictionaryFactory)))
            .Returns(Mock.Of <ITempDataDictionaryFactory>());
            mockServiceProvider
            .Setup(x => x.GetService(typeof(IObjectModelValidator)))
            .Returns(Mock.Of <IObjectModelValidator>());
            mockServiceProvider
            .Setup(x => x.GetService(typeof(IDisplayHelper)))
            .Returns(mockDisplayHelper.Object);
            mockServiceProvider
            .Setup(x => x.GetService(typeof(IUrlHelperFactory)))
            .Returns(mockUrlHelperFactory.Object);
            mockServiceProvider
            .Setup(x => x.GetService(typeof(HtmlEncoder)))
            .Returns(HtmlEncoder.Default);

            // var mockRequest = new Mock<HttpRequest>();
            // mockRequest.Setup(x => x.Scheme)
            //     .Returns("http");

            var mockHttpContext = new Mock <HttpContext>();

            mockHttpContext
            .Setup(x => x.RequestServices)
            .Returns(mockServiceProvider.Object);
            mockHttpContext
            .Setup(x => x.Request)
            .Returns(Mock.Of <HttpRequest>(x => x.Scheme == "http"));

            controller.ControllerContext.HttpContext = mockHttpContext.Object;

            return(controller);
        }