public async Task AddPhoneNumberPostInvokesGenerateChangePhoneNumberTokenAsyncWithCorrectParametersWhenModelStateIsValid()
        {
            //Arrange
            var userId = "userId";
            var token  = "token";
            var user   = new ApplicationUser {
                Id = userId
            };
            var model = new AddPhoneNumberViewModel {
                PhoneNumber = "phone"
            };

            var userManager = UserManagerMockHelper.CreateUserManagerMock();

            userManager.Setup(x => x.GetUserId(It.IsAny <ClaimsPrincipal>())).Returns(userId);
            userManager.Setup(x => x.GenerateChangePhoneNumberTokenAsync(It.IsAny <ApplicationUser>(),
                                                                         It.IsAny <string>())).ReturnsAsync(token);

            var signInManagerMock = SignInManagerMockHelper.CreateSignInManagerMock(userManager);
            var mediator          = new Mock <IMediator>();

            mediator.Setup(x => x.SendAsync(It.IsAny <UserByUserIdQuery>())).ReturnsAsync(user);

            var controller = new ManageController(userManager.Object, signInManagerMock.Object, mediator.Object);

            controller.SetFakeUser(userId);

            //Act
            await controller.AddPhoneNumber(model);

            //Assert
            userManager.Verify(x => x.GenerateChangePhoneNumberTokenAsync(It.Is <ApplicationUser>(u => u.Id == userId),
                                                                          It.Is <string>(p => p == model.PhoneNumber)),
                               Times.Once);
        }
        public async Task RemovePhoneNumberInvokesRefreshSignInAsyncWithCorrectParametersWhenUserIsNotNullAndPhoneNumberWasSetSuccessfullyAndUsersProfileIsComplete()
        {
            //Arrange
            var userId      = "userId";
            var phoneNumber = "phone";
            var user        = UserWithCompleteProfile(userId, phoneNumber);

            var userManager = UserManagerMockHelper.CreateUserManagerMock();

            userManager.Setup(x => x.GetUserId(It.IsAny <ClaimsPrincipal>())).Returns(userId);
            userManager.Setup(x => x.SetPhoneNumberAsync(It.IsAny <ApplicationUser>(),
                                                         It.IsAny <string>())).ReturnsAsync(IdentityResult.Success);

            var signInManager = SignInManagerMockHelper.CreateSignInManagerMock(userManager);

            var mediator = new Mock <IMediator>();

            mediator.Setup(x => x.SendAsync(It.IsAny <UserByUserIdQuery>())).ReturnsAsync(user);

            var controller = new ManageController(userManager.Object, signInManager.Object, mediator.Object);

            controller.SetFakeUser(userId);

            //Act
            await controller.RemovePhoneNumber();

            //Assert
            signInManager.Verify(x => x.RefreshSignInAsync(It.IsAny <ApplicationUser>()), Times.Once);
        }
        public async Task RemovePhoneNumberInvokesSetPhoneNumberAsyncWithCorrectParametersWhenUserIsNotNull()
        {
            //Arrange
            var userId = "userId";
            var user   = new ApplicationUser {
                Id = userId
            };
            var userManager = UserManagerMockHelper.CreateUserManagerMock();

            userManager.Setup(x => x.GetUserId(It.IsAny <ClaimsPrincipal>())).Returns(userId);
            userManager.Setup(x => x.SetPhoneNumberAsync(It.IsAny <ApplicationUser>(),
                                                         It.IsAny <string>())).ReturnsAsync(new IdentityResult());

            var signInManagerMock = SignInManagerMockHelper.CreateSignInManagerMock(userManager);
            var mediator          = new Mock <IMediator>();

            mediator.Setup(x => x.SendAsync(It.IsAny <UserByUserIdQuery>())).ReturnsAsync(user);

            var controller = new ManageController(userManager.Object, signInManagerMock.Object, mediator.Object);

            controller.SetFakeUser(userId);

            //Act
            await controller.RemovePhoneNumber();

            //Assert
            userManager.Verify(x => x.SetPhoneNumberAsync(It.Is <ApplicationUser>(u => u == user),
                                                          It.Is <string>(p => p == null)),
                               Times.Once);
        }
        public async Task VerifyPhoneNumberPostSendsRemoveUserProfileIncompleteClaimCommandWhenUserIsNotNullAndPhoneNumberChangeWasSuccessfulAndUserProfileIsComplete()
        {
            //Arrange
            var userId      = "userId";
            var token       = "token";
            var phoneNumber = "phone";
            var user        = UserWithCompleteProfile(userId, phoneNumber);

            var model = new VerifyPhoneNumberViewModel {
                PhoneNumber = phoneNumber, Code = token
            };
            var userManager = UserManagerMockHelper.CreateUserManagerMock();

            userManager.Setup(x => x.GetUserId(It.IsAny <ClaimsPrincipal>())).Returns(userId);
            userManager.Setup(x => x.ChangePhoneNumberAsync(It.IsAny <ApplicationUser>(),
                                                            It.IsAny <string>(),
                                                            It.IsAny <string>())).ReturnsAsync(IdentityResult.Success);

            var signInManagerMock = SignInManagerMockHelper.CreateSignInManagerMock(userManager);
            var mediator          = new Mock <IMediator>();

            mediator.Setup(x => x.SendAsync(It.IsAny <UserByUserIdQuery>())).ReturnsAsync(user);

            var controller = new ManageController(userManager.Object, signInManagerMock.Object, mediator.Object);

            controller.SetFakeUser(userId);

            //Act
            await controller.VerifyPhoneNumber(model);

            //Assert
            mediator.Verify(x => x.SendAsync(It.IsAny <RemoveUserProfileIncompleteClaimCommand>()), Times.Once);
        }
        public async Task VerifyPhoneNumberPostReturnsTheSameViewAndModelWhenModelStateIsInvalid()
        {
            //Arrange
            var userManagerMock   = UserManagerMockHelper.CreateUserManagerMock();
            var signInManagerMock = SignInManagerMockHelper.CreateSignInManagerMock(userManagerMock);
            var mediator          = new Mock <IMediator>();
            var phoneNumber       = "number";

            mediator.Setup(m => m.SendAsync(It.IsAny <UserByUserIdQuery>())).ReturnsAsync(new ApplicationUser());

            var controller   = new ManageController(userManagerMock.Object, signInManagerMock.Object, mediator.Object);
            var invalidModel = new VerifyPhoneNumberViewModel {
                PhoneNumber = phoneNumber
            };

            controller.ModelState.AddModelError("PhoneNumber", "Must be a number");

            //Act
            var result = await controller.VerifyPhoneNumber(invalidModel);

            var resultViewModel = (ViewResult)result;
            var viewModel       = (VerifyPhoneNumberViewModel)resultViewModel.ViewData.Model;

            //Assert
            Assert.Equal(phoneNumber, viewModel.PhoneNumber);
        }
        public async Task RemovePhoneNumberRedirectsToCorrectActionWithCorrectRouteValuesWhenUserIsNotNullAndPhoneNumberWasSetSuccessfully()
        {
            //Arrange
            var userId      = "userId";
            var phoneNumber = "phone";
            var user        = UserWithCompleteProfile(userId, phoneNumber);

            var userManager = UserManagerMockHelper.CreateUserManagerMock();

            userManager.Setup(x => x.GetUserId(It.IsAny <ClaimsPrincipal>())).Returns(userId);
            userManager.Setup(x => x.SetPhoneNumberAsync(It.IsAny <ApplicationUser>(),
                                                         It.IsAny <string>())).ReturnsAsync(IdentityResult.Success);

            var signInManager = SignInManagerMockHelper.CreateSignInManagerMock(userManager);

            var mediator = new Mock <IMediator>();

            mediator.Setup(x => x.SendAsync(It.IsAny <UserByUserIdQuery>())).ReturnsAsync(user);

            var controller = new ManageController(userManager.Object, signInManager.Object, mediator.Object);

            controller.SetFakeUser(userId);

            //Act
            var result = await controller.RemovePhoneNumber() as RedirectToActionResult;

            //Assert
            Assert.Equal("Index", result.ActionName);
            Assert.Equal(ManageController.ManageMessageId.RemovePhoneSuccess, result.RouteValues["Message"]);
        }
Esempio n. 7
0
        public async Task IndexPostSendsUserByUserIdQueryWithCorrectUserId()
        {
            //Arrange
            var userId = "userId";

            var userManagerMock = UserManagerMockHelper.CreateUserManagerMock();

            userManagerMock.Setup(x => x.GetUserId(It.IsAny <ClaimsPrincipal>())).Returns(userId);

            var signInManagerMock = SignInManagerMockHelper.CreateSignInManagerMock(userManagerMock);

            var mediator = new Mock <IMediator>();

            mediator.Setup(x => x.SendAsync(It.IsAny <AllReady.Features.Manage.UserByUserIdQuery>())).ReturnsAsync(new ApplicationUser());

            var controller = new ManageController(userManagerMock.Object, signInManagerMock.Object, mediator.Object);

            controller.SetFakeUser(userId);

            var vm = new IndexViewModel();

            await controller.Index(vm);

            mediator.Verify(m => m.SendAsync(It.Is <AllReady.Features.Manage.UserByUserIdQuery>(u => u.UserId == userId)), Times.Once);
        }
        public async Task ResendPhoneNumberSendsSendAccountSecurityTokenSmsAsyncWithCorrectData()
        {
            //Arrange
            var userId = "userId";
            var token  = "token";
            var user   = new ApplicationUser {
                Id = userId
            };
            var phoneNumber = "phone";

            var userManager = UserManagerMockHelper.CreateUserManagerMock();

            userManager.Setup(x => x.GetUserId(It.IsAny <ClaimsPrincipal>())).Returns(userId);
            userManager.Setup(x => x.GenerateChangePhoneNumberTokenAsync(It.IsAny <ApplicationUser>(),
                                                                         It.IsAny <string>())).ReturnsAsync(token);

            var signInManagerMock = SignInManagerMockHelper.CreateSignInManagerMock(userManager);
            var mediator          = new Mock <IMediator>();

            mediator.Setup(x => x.SendAsync(It.IsAny <UserByUserIdQuery>())).ReturnsAsync(user);

            var controller = new ManageController(userManager.Object, signInManagerMock.Object, mediator.Object);

            controller.SetFakeUser(userId);

            //Act
            await controller.ResendPhoneNumberConfirmation(phoneNumber);

            //Assert
            mediator.Verify(m => m.SendAsync(It.Is <SendAccountSecurityTokenSms>(x => x.Token == token && x.PhoneNumber == phoneNumber)), Times.Once);
        }
        public async Task ResendPhoneNumberRedirectsToCorrectActionWithCorrectRouteValues()
        {
            //Arrange
            var userId = "userId";
            var token  = "token";
            var user   = new ApplicationUser {
                Id = userId
            };
            var phoneNumber = "phone";

            var userManager = UserManagerMockHelper.CreateUserManagerMock();

            userManager.Setup(x => x.GetUserId(It.IsAny <ClaimsPrincipal>())).Returns(userId);
            userManager.Setup(x => x.GenerateChangePhoneNumberTokenAsync(It.IsAny <ApplicationUser>(),
                                                                         It.IsAny <string>())).ReturnsAsync(token);

            var signInManagerMock = SignInManagerMockHelper.CreateSignInManagerMock(userManager);
            var mediator          = new Mock <IMediator>();

            mediator.Setup(x => x.SendAsync(It.IsAny <UserByUserIdQuery>())).ReturnsAsync(user);

            var controller = new ManageController(userManager.Object, signInManagerMock.Object, mediator.Object);

            controller.SetFakeUser(userId);

            //Act
            var result = await controller.ResendPhoneNumberConfirmation(phoneNumber) as RedirectToActionResult;

            //Assert
            Assert.Equal("VerifyPhoneNumber", result.ActionName);
        }
Esempio n. 10
0
        public async Task SendCodePostReturnsRedirectToActionResult()
        {
            var model = new SendCodeViewModel {
                SelectedProvider = string.Empty, ReturnUrl = "ReturnUrl", RememberMe = true
            };

            var routeValues = new Dictionary <string, object>
            {
                ["Provider"]   = model.SelectedProvider,
                ["ReturnUrl"]  = model.ReturnUrl,
                ["RememberMe"] = model.RememberMe
            };

            var userManager   = UserManagerMockHelper.CreateUserManagerMock();
            var signInManager = SignInManagerMockHelper.CreateSignInManagerMock(userManager);

            signInManager.Setup(x => x.GetTwoFactorAuthenticationUserAsync()).ReturnsAsync(new ApplicationUser());
            userManager.Setup(x => x.GenerateTwoFactorTokenAsync(It.IsAny <ApplicationUser>(), It.IsAny <string>())).ReturnsAsync("token");

            var sut    = new AdminController(userManager.Object, signInManager.Object, null, null, null);
            var result = await sut.SendCode(model) as RedirectToActionResult;

            Assert.Equal(result.ActionName, nameof(AdminController.VerifyCode));
            Assert.Equal(result.RouteValues, routeValues);
        }
Esempio n. 11
0
        public async Task LoginReturnsCorrectCookieString()
        {
            var model = new LoginViewModel()
            {
                Email    = "*****@*****.**",
                Password = "******"
            };

            var mediator      = new Mock <IMediator>();
            var userManager   = UserManagerMockHelper.CreateUserManagerMock();
            var signInManager = SignInManagerMockHelper.CreateSignInManagerMock(userManager);

            signInManager.Setup(
                x =>
                x.PasswordSignInAsync(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <bool>(),
                                      It.IsAny <bool>()))
            .ReturnsAsync(Microsoft.AspNetCore.Identity.SignInResult.Success);

            var sut = new MeApiController(userManager.Object, signInManager.Object, mediator.Object);
            var mockedHttpRequest = sut.GetMockHttpRequest();

            await sut.Login(model);

            mockedHttpRequest.Verify(x => x.Cookies[".AspNet.ApplicationCookie"], Times.Once());
        }
Esempio n. 12
0
        public async Task IndexPostInvokesAddClaimAsyncWithCorrectParametersWhenUsersTimeZoneDoesNotEqualModelsTimeZone()
        {
            //Arrange
            var userManagerMock = UserManagerMockHelper.CreateUserManagerMock();

            userManagerMock.Setup(x => x.AddClaimAsync(It.IsAny <ApplicationUser>(), It.IsAny <Claim>())).ReturnsAsync(IdentityResult.Success);

            var signInManagerMock = SignInManagerMockHelper.CreateSignInManagerMock(userManagerMock);

            var mediator = new Mock <IMediator>();
            var user     = new ApplicationUser {
                TimeZoneId = "timeZoneId"
            };

            mediator.Setup(m => m.SendAsync(It.IsAny <UserByUserIdQuery>())).ReturnsAsync(user);

            var controller = new ManageController(userManagerMock.Object, signInManagerMock.Object, mediator.Object);

            controller.SetFakeUser("userId");
            var vM = new IndexViewModel {
                TimeZoneId = "differentTimeZoneId"
            };

            //Act
            await controller.Index(vM);

            //Assert
            userManagerMock.Verify(x => x.AddClaimAsync(user, It.Is <Claim>(c => c.Type == AllReady.Security.ClaimTypes.TimeZoneId)), Times.Once);
        }
Esempio n. 13
0
        public async Task IndexPostReturnsCorrectViewModelWhenModelStateIsInvalid()
        {
            //Arrange
            var userManagerMock   = UserManagerMockHelper.CreateUserManagerMock();
            var signInManagerMock = SignInManagerMockHelper.CreateSignInManagerMock(userManagerMock);

            var mediator = new Mock <IMediator>();

            mediator.Setup(m => m.SendAsync(It.IsAny <UserByUserIdQuery>())).ReturnsAsync(new ApplicationUser());
            var controller = new ManageController(userManagerMock.Object, signInManagerMock.Object, mediator.Object);

            controller.SetFakeUser("userId");
            IndexViewModel invalidVm = new IndexViewModel();

            controller.ModelState.AddModelError("FirstName", "Can't be a number");

            //Act
            var result = await controller.Index(invalidVm);

            var resultViewModel = ((ViewResult)result);
            var vm = (IndexViewModel)resultViewModel.ViewData.Model;

            //Assert
            Assert.IsType <IndexViewModel>(vm);
        }
Esempio n. 14
0
        public async Task SendCodeGetReturnsSendCodeViewModelWithCorrectData()
        {
            const string returnUrl  = "returnUrl";
            const bool   rememberMe = true;

            var userFactors = new List <string> {
                "userFactor1", "userFactor2"
            };
            var expectedProviders = userFactors.Select(factor => new SelectListItem {
                Text = factor, Value = factor
            }).ToList();

            var userManager   = UserManagerMockHelper.CreateUserManagerMock();
            var signInManager = SignInManagerMockHelper.CreateSignInManagerMock(userManager);

            signInManager.Setup(x => x.GetTwoFactorAuthenticationUserAsync()).ReturnsAsync(new ApplicationUser());
            userManager.Setup(x => x.GetValidTwoFactorProvidersAsync(It.IsAny <ApplicationUser>())).ReturnsAsync(userFactors);

            var sut = new AdminController(userManager.Object, signInManager.Object, null, null, null);

            var result = await sut.SendCode(returnUrl, rememberMe) as ViewResult;

            var modelResult = result.ViewData.Model as SendCodeViewModel;

            Assert.Equal(modelResult.ReturnUrl, returnUrl);
            Assert.Equal(modelResult.RememberMe, rememberMe);
            Assert.Equal(expectedProviders, modelResult.Providers, new SelectListItemComparer());
        }
Esempio n. 15
0
        public async Task SendCodeGetReturnsErrorViewWhenCannotFindUser()
        {
            var signInManager = SignInManagerMockHelper.CreateSignInManagerMock();
            var sut           = new AdminController(null, signInManager.Object, null, null, null);
            var result        = await sut.SendCode(null, It.IsAny <bool>()) as ViewResult;

            Assert.Equal(result.ViewName, "Error");
        }
Esempio n. 16
0
        public async Task VerifyCodeGetInvokesGetTwoFactorAuthenticationUserAsync()
        {
            var signInManager = SignInManagerMockHelper.CreateSignInManagerMock();
            var sut           = new AdminController(null, signInManager.Object, null, null, null);
            await sut.VerifyCode(It.IsAny <string>(), It.IsAny <bool>(), It.IsAny <string>());

            signInManager.Verify(x => x.GetTwoFactorAuthenticationUserAsync(), Times.Once);
        }
Esempio n. 17
0
        public async Task VerifyCodeGetReturnsErrorViewWhenUserIsNull()
        {
            var signInManager = SignInManagerMockHelper.CreateSignInManagerMock();
            var sut           = new AdminController(null, signInManager.Object, null, null, null);
            var result        = await sut.VerifyCode(It.IsAny <string>(), It.IsAny <bool>(), It.IsAny <string>()) as ViewResult;

            Assert.Equal(result.ViewName, "Error");
        }
Esempio n. 18
0
        public async Task SendCodePosReturnsErrorViewWhenUserIsNotFound()
        {
            var signInManager = SignInManagerMockHelper.CreateSignInManagerMock();

            var sut    = new AdminController(null, signInManager.Object, null, null, null);
            var result = await sut.SendCode(It.IsAny <SendCodeViewModel>()) as ViewResult;

            Assert.Equal(result.ViewName, "Error");
        }
Esempio n. 19
0
        public async Task VerifyCodePostReturnsLockoutViewIfTwoFactorSignInAsyncFailsAndIsLockedOut()
        {
            var signInManager = SignInManagerMockHelper.CreateSignInManagerMock();

            signInManager.Setup(x => x.TwoFactorSignInAsync(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <bool>(), It.IsAny <bool>())).ReturnsAsync(Microsoft.AspNetCore.Identity.SignInResult.LockedOut);

            var sut    = new AdminController(null, signInManager.Object, null, null, null);
            var result = await sut.VerifyCode(new VerifyCodeViewModel()) as ViewResult;

            Assert.Equal(result.ViewName, "Lockout");
        }
Esempio n. 20
0
        public async Task SendCodePostReturnsErrorViewWhenAuthenticationTokenIsNull()
        {
            var userManager   = UserManagerMockHelper.CreateUserManagerMock();
            var signInManager = SignInManagerMockHelper.CreateSignInManagerMock(userManager);

            signInManager.Setup(x => x.GetTwoFactorAuthenticationUserAsync()).ReturnsAsync(new ApplicationUser());

            var sut    = new AdminController(userManager.Object, signInManager.Object, null, null, null);
            var result = await sut.SendCode(new SendCodeViewModel()) as ViewResult;

            Assert.Equal(result.ViewName, "Error");
        }
Esempio n. 21
0
        public void LoginMethodHasHttpPostAtttribute()
        {
            var mediator      = new Mock <IMediator>();
            var userManager   = UserManagerMockHelper.CreateUserManagerMock();
            var signInManager = SignInManagerMockHelper.CreateSignInManagerMock(userManager);
            var sut           = new MeApiController(userManager.Object, signInManager.Object, mediator.Object);

            var attribute =
                sut.GetAttributesOn(x => x.Login(new LoginViewModel())).OfType <HttpPostAttribute>().SingleOrDefault();

            Assert.NotNull(attribute);
        }
Esempio n. 22
0
        public void ControllerHasRouteAttributeWithRoute()
        {
            var mediator      = new Mock <IMediator>();
            var userManager   = UserManagerMockHelper.CreateUserManagerMock();
            var signInManager = SignInManagerMockHelper.CreateSignInManagerMock(userManager);
            var sut           = new MeApiController(userManager.Object, signInManager.Object, mediator.Object);

            var attribute = sut.GetAttributes().OfType <RouteAttribute>().SingleOrDefault();

            Assert.NotNull(attribute);
            Assert.Equal(attribute.Template, "api/me");
        }
Esempio n. 23
0
        public async Task VerifyCodePostAddsErrorMessageToModelStateErrorWhenTwoFactorSignInAsyncIsNotSuccessful()
        {
            var signInManager = SignInManagerMockHelper.CreateSignInManagerMock();

            signInManager.Setup(x => x.TwoFactorSignInAsync(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <bool>(), It.IsAny <bool>())).ReturnsAsync(new Microsoft.AspNetCore.Identity.SignInResult());

            var sut = new AdminController(null, signInManager.Object, null, null, null);
            await sut.VerifyCode(new VerifyCodeViewModel());

            var errorMessage = sut.ModelState.GetErrorMessages().Single();

            Assert.Equal(errorMessage, "Invalid code.");
        }
Esempio n. 24
0
        public void LoginHasRouteAttributeWithRoute()
        {
            var mediator      = new Mock <IMediator>();
            var userManager   = UserManagerMockHelper.CreateUserManagerMock();
            var signInManager = SignInManagerMockHelper.CreateSignInManagerMock(userManager);
            var sut           = new MeApiController(userManager.Object, signInManager.Object, mediator.Object);

            var attribute =
                sut.GetAttributesOn(x => x.Login(new LoginViewModel())).OfType <RouteAttribute>().SingleOrDefault();

            Assert.NotNull(attribute);
            Assert.Equal("login", attribute.Template);
        }
Esempio n. 25
0
        public async Task SendCodeGetInvokesGetValidTwoFactorProvidersAsyncWithCorrectUser()
        {
            var applicationUser = new ApplicationUser();

            var userManager   = UserManagerMockHelper.CreateUserManagerMock();
            var signInManager = SignInManagerMockHelper.CreateSignInManagerMock(userManager);

            signInManager.Setup(x => x.GetTwoFactorAuthenticationUserAsync()).ReturnsAsync(applicationUser);
            userManager.Setup(x => x.GetValidTwoFactorProvidersAsync(It.IsAny <ApplicationUser>())).ReturnsAsync(new List <string>());

            var sut = new AdminController(userManager.Object, signInManager.Object, null, null, null);

            await sut.SendCode(null, It.IsAny <bool>());

            userManager.Verify(x => x.GetValidTwoFactorProvidersAsync(applicationUser), Times.Once);
        }
        public async Task RemovePhoneNumberRedirectsToCorrectActionWithCorrectRouteValuesWhenUserIsNull()
        {
            //Arrange
            var userManager   = UserManagerMockHelper.CreateUserManagerMock();
            var signInManager = SignInManagerMockHelper.CreateSignInManagerMock(userManager);
            var mediator      = new Mock <IMediator>();

            mediator.Setup(x => x.SendAsync(It.IsAny <UserByUserIdQuery>())).ReturnsAsync(null);

            var controller = new ManageController(userManager.Object, signInManager.Object, mediator.Object);

            //Act
            var result = await controller.RemovePhoneNumber() as RedirectToActionResult;

            //Assert
            Assert.Equal("Index", result.ActionName);
            Assert.Equal(ManageController.ManageMessageId.Error, result.RouteValues["Message"]);
        }
Esempio n. 27
0
        public async Task SendCodePostInvokesGenerateTwoFactorTokenAsyncWithCorrectUserAndTokenProvider()
        {
            var applicationUser = new ApplicationUser();
            var model           = new SendCodeViewModel {
                SelectedProvider = "Email"
            };

            var userManager = UserManagerMockHelper.CreateUserManagerMock();

            var signInManager = SignInManagerMockHelper.CreateSignInManagerMock(userManager);

            signInManager.Setup(x => x.GetTwoFactorAuthenticationUserAsync()).ReturnsAsync(applicationUser);

            var sut = new AdminController(userManager.Object, signInManager.Object, null, null, null);
            await sut.SendCode(model);

            userManager.Verify(x => x.GenerateTwoFactorTokenAsync(applicationUser, model.SelectedProvider), Times.Once);
        }
Esempio n. 28
0
        public async Task VerifyCodePostInvokesTwoFactorSignInAsyncWithCorrectParameters()
        {
            var model = new VerifyCodeViewModel
            {
                Provider        = "provider",
                Code            = "code",
                RememberBrowser = true,
                RememberMe      = true
            };

            var signInManager = SignInManagerMockHelper.CreateSignInManagerMock();

            signInManager.Setup(x => x.TwoFactorSignInAsync(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <bool>(), It.IsAny <bool>())).ReturnsAsync(new Microsoft.AspNetCore.Identity.SignInResult());

            var sut = new AdminController(null, signInManager.Object, null, null, null);
            await sut.VerifyCode(model);

            signInManager.Verify(x => x.TwoFactorSignInAsync(model.Provider, model.Code, model.RememberMe, model.RememberBrowser));
        }
Esempio n. 29
0
        public async Task VerifyCodePostRedirectsToHomeControllerIndexWhenTwoFactorSignInAsyncSucceedsAndReturnUrlIsNotLocalUrl()
        {
            var signInManager = SignInManagerMockHelper.CreateSignInManagerMock();

            signInManager.Setup(x => x.TwoFactorSignInAsync(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <bool>(), It.IsAny <bool>()))
            .ReturnsAsync(Microsoft.AspNetCore.Identity.SignInResult.Success);

            var urlHelper = new Mock <IUrlHelper>();

            urlHelper.Setup(x => x.IsLocalUrl(It.IsAny <string>())).Returns(false);

            var sut = new AdminController(null, signInManager.Object, null, null, null)
            {
                Url = urlHelper.Object
            };
            var result = await sut.VerifyCode(new VerifyCodeViewModel()) as RedirectToPageResult;

            Assert.Equal(result.PageName, "/Index");
        }
Esempio n. 30
0
        public async Task VerifyCodeGetReturnsCorrectViewModel()
        {
            const string provider   = "provider";
            const bool   rememberMe = true;
            const string returnUrl  = "returnUrl";

            var signInManager = SignInManagerMockHelper.CreateSignInManagerMock();

            signInManager.Setup(x => x.GetTwoFactorAuthenticationUserAsync()).ReturnsAsync(new ApplicationUser());

            var sut = new AdminController(null, signInManager.Object, null, null, null);

            var result = await sut.VerifyCode(provider, rememberMe, returnUrl) as ViewResult;

            var modelResult = result.ViewData.Model as VerifyCodeViewModel;

            Assert.Equal(modelResult.Provider, provider);
            Assert.Equal(modelResult.ReturnUrl, returnUrl);
            Assert.Equal(modelResult.RememberMe, rememberMe);
        }