public async Task ConfirmationEmailStatusUpdateFailedTest()
        {
            string aspNetUserId  = "123";
            int    userId        = 5;
            string token         = "token";
            string email         = "*****@*****.**";
            string composedToken = $"{token}:{email}";
            string encodedToken  = EmailConfirmationHelper.TokenEncode(composedToken);
            string decodedToken  = EmailConfirmationHelper.TokenDecode(encodedToken);

            List <User> userData = new List <User>()
            {
                new User()
                {
                    Id = userId, StatusId = UserStatuses.PendingEmailValidation
                }
            };
            AspNetUser aspNetUser = new AspNetUser()
            {
                Id = aspNetUserId, Email = email, Users = new List <User>()
                {
                    new User()
                    {
                        Id = userId
                    }
                }
            };

            var entitiesMock = GetEntitiesMockForEmailValidationTests(userData);

            entitiesMock.Setup(e => e.SaveChangesAsync())
            .Callback(() => { userData.FirstOrDefault().StatusId = UserStatuses.PendingEmailValidation; })
            .Throws(new Exception("Saving is failed."));
            var userManagerMock   = GetApplicationUserManagerMock(new Mock <IUserStore <AspNetUser, string> >());
            var signInManagerMock = GetSignInManagerMock(userManagerMock, GetAuthenticationManagerMock());

            userManagerMock.Setup(m => m.FindByIdAsync(It.Is <string>((id) => id == aspNetUserId))).ReturnsAsync(aspNetUser);
            userManagerMock.Setup(m => m.ConfirmEmailAsync(It.Is <string>((id) => id == aspNetUserId), It.Is <string>((t) => t == token)))
            .ReturnsAsync(new IdentityResult());

            AccountController controller = new AccountController(entitiesMock.Object, userManagerMock.Object, signInManagerMock.Object, new Mock <IEmailSender>().Object);

            controller.ControllerContext = GetDefaultAccountControllerRequest(controller);

            //Act
            ActionResult result = await controller.ValidateEmail(aspNetUserId, encodedToken);

            //Assert
            ViewResult viewResult = (ViewResult)result;

            Assert.AreEqual(composedToken, decodedToken);
            Assert.AreEqual("", viewResult.ViewName);
            Assert.AreEqual(userData.FirstOrDefault()?.StatusId, UserStatuses.PendingEmailValidation);
        }
        public async Task ConfirmationEmailTest()
        {
            string aspNetUserId  = "123";
            int    userId        = 5;
            string token         = "token";
            string email         = "*****@*****.**";
            string composedToken = $"{token}:{email}";
            string encodedToken  = EmailConfirmationHelper.TokenEncode(composedToken);
            string decodedToken  = EmailConfirmationHelper.TokenDecode(encodedToken);

            List <User> usertData = new List <User>()
            {
                new User()
                {
                    Id = userId, StatusId = UserStatuses.PendingEmailValidation
                }
            };
            AspNetUser aspNetUser = new AspNetUser()
            {
                Id = aspNetUserId, Email = email, Users = new List <User>()
                {
                    new User()
                    {
                        Id = userId
                    }
                }
            };

            var entitiesMock      = GetEntitiesMock();
            var userManagerMock   = GetApplicationUserManagerMock(new Mock <IUserStore <AspNetUser, string> >());
            var signInManagerMock = GetSignInManagerMock(userManagerMock, GetAuthenticationManagerMock());
            var usersDbSetMock    = GetUsersDbSetMock(usertData);

            usersDbSetMock.Setup(u => u.FindAsync(It.Is <int>((id) => id == userId))).ReturnsAsync(usertData [0]);
            entitiesMock.Setup(e => e.Users).Returns(usersDbSetMock.Object);
            entitiesMock.Setup(e => e.SaveChangesAsync()).ReturnsAsync(0).Verifiable();
            userManagerMock.Setup(m => m.FindByIdAsync(It.Is <string>((id) => id == aspNetUserId))).ReturnsAsync(aspNetUser);
            userManagerMock.Setup(m => m.ConfirmEmailAsync(It.Is <string>((id) => id == aspNetUserId), It.Is <string>((t) => t == token)))
            .ReturnsAsync(IdentityResult.Success);

            //Act

            AccountController controller = new AccountController(entitiesMock.Object, userManagerMock.Object, signInManagerMock.Object, new Mock <IEmailSender>().Object);
            ActionResult      result     = await controller.ValidateEmail(aspNetUserId, encodedToken);

            //Assert
            ViewResult viewResult = (ViewResult)result;

            Assert.AreEqual(composedToken, decodedToken);
            Assert.AreEqual((( EmailConfirmationViewModel )viewResult.Model).isSuccess, true);
            Assert.AreEqual(usertData.FirstOrDefault()?.StatusId, UserStatuses.Valid);
            entitiesMock.Verify();
        }
        public async Task ConfirmationEmailWrongUserEmailFailedTest()
        {
            string aspNetUserId  = "123";
            int    userId        = 5;
            string token         = "token";
            string email         = "*****@*****.**";
            string newEmail      = "*****@*****.**";
            string composedToken = $"{token}:{email}";
            string encodedToken  = EmailConfirmationHelper.TokenEncode(composedToken);
            string decodedToken  = EmailConfirmationHelper.TokenDecode(encodedToken);

            List <User> userData = new List <User>()
            {
                new User()
                {
                    Id = userId, StatusId = UserStatuses.PendingEmailValidation
                }
            };
            AspNetUser aspNetUser = new AspNetUser()
            {
                Id = aspNetUserId, Email = newEmail, Users = new List <User>()
                {
                    new User()
                    {
                        Id = userId
                    }
                }
            };

            var entitiesMock      = GetEntitiesMockForEmailValidationTests(userData);
            var userManagerMock   = GetApplicationUserManagerMock(new Mock <IUserStore <AspNetUser, string> >());
            var signInManagerMock = GetSignInManagerMock(userManagerMock, GetAuthenticationManagerMock());

            userManagerMock.Setup(m => m.FindByIdAsync(It.Is <string>((id) => id == aspNetUserId))).ReturnsAsync(aspNetUser);
            userManagerMock.Setup(m => m.ConfirmEmailAsync(It.Is <string>((id) => id == aspNetUserId), It.Is <string>((t) => t == decodedToken)))
            .ReturnsAsync(new IdentityResult());

            AccountController controller = new AccountController(entitiesMock.Object, userManagerMock.Object, signInManagerMock.Object, new Mock <IEmailSender>().Object);

            controller.ControllerContext = GetDefaultAccountControllerRequest(controller);

            //Act
            ActionResult result = await controller.ValidateEmail(aspNetUserId, encodedToken);

            //Assert
            ViewResult viewResult = (ViewResult)result;

            Assert.AreEqual(composedToken, decodedToken);
            Assert.AreEqual((( EmailConfirmationViewModel )viewResult.Model).isSuccess, false);
            Assert.AreEqual(userData.FirstOrDefault()?.StatusId, UserStatuses.PendingEmailValidation);
            userManagerMock.Verify(m => m.ConfirmEmailAsync(It.Is <string>((id) => id == aspNetUserId), It.Is <string>((t) => t == token)), Times.Never);
        }
        private async Task <string> GetEmailConfirmUrl(string aspNetUserId, string email)
        {
            //Andriy: we can't use token as parameter safe without such encoding, please look here:
            //http://stackoverflow.com/questions/28750480/aspnet-identit-2-1-0-confirmemailasync-always-returns-invalid-token
            string emailTtoken = await UserManager.GenerateEmailConfirmationTokenAsync(aspNetUserId);

            string extendedToken   = $"{emailTtoken}:{email}";
            string encodedToken    = EmailConfirmationHelper.TokenEncode(extendedToken);
            string scheme          = Request.RequestUri.Scheme;
            string host            = Request.RequestUri.Host;
            string port            = Request.RequestUri.Port != 80 ? $":{Request.RequestUri.Port}":null;
            string emailConfirmUrl = $"{scheme}://{host}{port}/validate-email?id={aspNetUserId}&t={encodedToken}";

            return(emailConfirmUrl);
        }