Пример #1
0
        public async Task testPostRegisterUserAlreadyExists()
        {
            DriverRegisterViewModel model = new DriverRegisterViewModel();

            model.Email     = Constants.DEFAULT_EMAIL;
            model.Password  = Constants.DEFAULT_PASSWORD;
            model.FirstName = "Matt";

            var           controller      = ControllerSupplier.getAccountController();
            var           mockUserService = new Mock <IUserService>();
            IdentityError error           = new IdentityError();

            error.Code        = "user exists";
            error.Description = "user exists";
            IdentityResult identityResult = IdentityResult.Failed(error);

            mockUserService.Setup(m => m.CreateDriverUserAsync(model)).Returns(Task.FromResult <IdentityResult>(identityResult));
            controller.userService = mockUserService.Object;
            var result = (ViewResult)await controller.Register(model);

            Assert.Equal(result.Model, model);
            Assert.True(controller.ViewData.ModelState.ErrorCount == 1);
            var allErrors = controller.ModelState.Values.SelectMany(v => v.Errors);

            Assert.True(allErrors.First().ErrorMessage.Contains(error.Description));
        }
Пример #2
0
        public async Task testPostLoginIncorrectPassword()
        {
            LoginViewModel model = new LoginViewModel();

            model.Email    = Constants.DEFAULT_EMAIL;
            model.Password = "******";
            var controller = ControllerSupplier.getAccountController();

            await populateDatabaseWithUser(controller);

            var userManager = controller.getUserManager();
            var user        = await userManager.FindByIdAsync(Constants.USER_ID);

            var code = await userManager.GenerateEmailConfirmationTokenAsync(user);

            await userManager.ConfirmEmailAsync(user, code);

            var result = (ViewResult)await controller.Login(model);

            Assert.NotEmpty(controller.ViewData.ModelState.Keys);
            var allErrors = controller.ModelState.Values.SelectMany(v => v.Errors);

            Assert.Equal(allErrors.First().ErrorMessage, "Invalid login attempt.");
            Assert.Equal(result.Model, model);
        }
Пример #3
0
        public async Task testPostLoginSuccessWhenLoginFromHomeDriverRole()
        {
            LoginViewModel model = new LoginViewModel();

            model.Email    = Constants.DEFAULT_EMAIL;
            model.Password = Constants.DEFAULT_PASSWORD;
            var controller = ControllerSupplier.getAccountController();

            await populateDatabaseWithUser(controller);

            var userManager = controller.getUserManager();
            var user        = await userManager.FindByIdAsync(Constants.USER_ID);

            var code = await userManager.GenerateEmailConfirmationTokenAsync(user);

            await userManager.ConfirmEmailAsync(user, code);

            await userManager.AddToRoleAsync(user, AppRole.DRIVER);

            var result = await controller.Login(model, "Login") as RedirectToActionResult;

            Assert.NotNull(result);
            Assert.Equal(result.ControllerName, "DriverDashboard");
            Assert.Equal(result.ActionName, "Index");
        }
Пример #4
0
        public void testGetResetPasswod()
        {
            var controller = ControllerSupplier.getAccountController();
            var result     = controller.ResetPassword("123");
            var viewResult = Assert.IsType <ViewResult>(result);

            Assert.Null(viewResult.Model);
            Assert.Equal(viewResult.ViewName, "ResetPassword");
        }
Пример #5
0
        // [Fact]
        public async Task testPostRegisterSuccess()
        {
            DriverRegisterViewModel model = new DriverRegisterViewModel();

            model.Email    = "*****@*****.**";
            model.Password = "******";
            var controller = ControllerSupplier.getAccountController();
            var result     = (ViewResult)await controller.Register(model);
        }
Пример #6
0
        public void testGetLogin()
        {
            var controller = ControllerSupplier.getAccountController();
            var result     = controller.Login("return/url");
            var viewResult = Assert.IsType <ViewResult>(result);

            Assert.Equal(viewResult.ViewData["ReturnUrl"], "return/url");
            Assert.Null(viewResult.Model);
            Assert.Equal(viewResult.ViewName, "Login");
        }
Пример #7
0
        public async Task testPostRegisterInvalidModel()
        {
            DriverRegisterViewModel model = new DriverRegisterViewModel();
            var controller = ControllerSupplier.getAccountController();

            controller.ViewData.ModelState.AddModelError("Key", "ErrorMessage");
            var result = (ViewResult)await controller.Register(model);

            Assert.Equal(result.Model, model);
        }
Пример #8
0
        public async Task testGetConfimEmailWithNullConfirmCode()
        {
            var controller = ControllerSupplier.getAccountController();
            var result     = await controller.ConfirmEmail(Constants.USER_ID, null);

            Assert.NotNull(result);
            var viewName = ((ViewResult)result).ViewName;

            Assert.Equal(viewName, "Error");
        }
Пример #9
0
        public async Task testResetPasswordInvalidModelState()
        {
            ForgotPasswordViewModel model = new ForgotPasswordViewModel();
            var controller = ControllerSupplier.getAccountController();

            controller.ViewData.ModelState.AddModelError("Key", "ErrorMessage");
            var result = (ViewResult)await controller.ForgotPassword(model);

            Assert.Equal(result.Model, model);
        }
Пример #10
0
        public async Task testPostResetPasswordUserIsNull()
        {
            ForgotPasswordViewModel model = new ForgotPasswordViewModel();

            model.Email = "*****@*****.**";
            var controller = ControllerSupplier.getAccountController();
            var result     = await controller.ForgotPassword(model) as ViewResult;

            Assert.Equal(result.ViewName, "ForgotPasswordConfirmation");
        }
Пример #11
0
        public async Task testPostLoginAttemptWithInvalidModel()
        {
            LoginViewModel model      = new LoginViewModel();
            var            controller = ControllerSupplier.getAccountController();

            controller.ViewData.ModelState.AddModelError("Key", "ErrorMessage");
            var result = (ViewResult)await controller.Login(model);

            Assert.Equal(result.Model, model);
        }
Пример #12
0
        public void testGetRegister()
        {
            var controller = ControllerSupplier.getAccountController();
            var result     = controller.Register("return/url");
            var viewResult = Assert.IsType <ViewResult>(result);

            Assert.Equal(viewResult.ViewData["ReturnUrl"], "return/url");
            Assert.Equal(viewResult.Model.GetType(), typeof(DriverRegisterViewModel));
            Assert.Equal(viewResult.ViewName, "Register");
        }
Пример #13
0
        public async Task testGetConfimEmailWithNullUserId()
        {
            var controller = ControllerSupplier.getAccountController();
            var result     = await controller.ConfirmEmail(null, Constants.CONFIRM_CODE);

            Assert.NotNull(result);
            var viewName = ((ViewResult)result).ViewName;

            Assert.Equal(viewName, "Error");
        }
Пример #14
0
        public async Task testGetConfirmEmailForExistingUserWithWrongCode()
        {
            var controller = ControllerSupplier.getAccountController();

            await populateDatabaseWithUser(controller);

            var result = await controller.ConfirmEmail(Constants.USER_ID, "123");

            Assert.NotNull(result);
            var viewName = ((ViewResult)result).ViewName;

            Assert.Equal(viewName, "Error");
        }
Пример #15
0
        public async Task testPostLoginUnknownUser()
        {
            LoginViewModel model = new LoginViewModel();

            model.Email    = Constants.DEFAULT_EMAIL;
            model.Password = Constants.DEFAULT_PASSWORD;
            var controller = ControllerSupplier.getAccountController();
            var result     = (ViewResult)await controller.Login(model);

            Assert.NotEmpty(controller.ViewData.ModelState.Values);
            var allErrors = controller.ModelState.Values.SelectMany(v => v.Errors);

            Assert.Equal(allErrors.First().ErrorMessage, "Invalid login attempt.");
            Assert.Equal(result.Model, model);
        }
Пример #16
0
        public async Task testPostLoginEmailNotConfirmed()
        {
            LoginViewModel model = new LoginViewModel();

            model.Email    = Constants.DEFAULT_EMAIL;
            model.Password = Constants.DEFAULT_PASSWORD;
            var controller = ControllerSupplier.getAccountController();

            await populateDatabaseWithUser(controller);

            var result = (ViewResult)await controller.Login(model);

            Assert.NotEmpty(controller.ViewData.ModelState.Keys);
            var allErrors = controller.ModelState.Values.SelectMany(v => v.Errors);

            Assert.Equal(allErrors.First().ErrorMessage, "You must have a confirmed email to log in.");
            Assert.Equal(result.Model, model);
        }
Пример #17
0
        public void testGetRegisterDriver()
        {
            var    controller = ControllerSupplier.getAccountController();
            string email      = "*****@*****.**";
            string firstName  = "Alex";
            var    result     = controller.RegisterDriver(1, email, firstName, "url");
            var    viewResult = Assert.IsType <ViewResult>(result);

            Assert.Equal(viewResult.ViewData["ReturnUrl"], "url");
            Assert.Equal(viewResult.Model.GetType(), typeof(DriverRegisterViewModel));
            Assert.Equal(viewResult.ViewName, "Register");

            var model = (DriverRegisterViewModel)viewResult.Model;

            Assert.Equal(model.DriverTeamId, "1");
            Assert.Equal(model.FirstName, firstName);
            Assert.Equal(model.Email, email);
        }
Пример #18
0
        public async Task testPostLogOff()
        {
            var controller = ControllerSupplier.getAccountController();

            await populateDatabaseWithUser(controller);

            //sign in a user
            var userManager = controller.getUserManager();
            var user        = await userManager.FindByEmailAsync(Constants.DEFAULT_EMAIL);

            var signInManager = controller.getSignInManager();
            await signInManager.PasswordSignInAsync(user.Email, Constants.DEFAULT_PASSWORD, false, lockoutOnFailure : false);

            var result = await controller.LogOff() as RedirectToActionResult;

            Assert.NotNull(result);
            Assert.Equal(result.ControllerName, "Home");
            Assert.Equal(result.ActionName, "Index");
        }
Пример #19
0
        public async Task testPostRegisterUserFailureInvalidPassword()
        {
            DriverRegisterViewModel model = new DriverRegisterViewModel();

            model.Email    = "*****@*****.**";
            model.Password = "******";
            var           controller      = ControllerSupplier.getAccountController();
            var           mockUserService = new Mock <IUserService>();
            IdentityError error           = new IdentityError();

            error.Code        = "invalid password";
            error.Description = "invalid password";
            IdentityResult identityResult = IdentityResult.Failed(error);

            mockUserService.Setup(m => m.CreateDriverUserAsync(model)).Returns(Task.FromResult <IdentityResult>(identityResult));
            controller.userService = mockUserService.Object;
            var result = (ViewResult)await controller.Register(model);

            Assert.Equal(result.Model, model);
            var allErrors = controller.ModelState.Values.SelectMany(v => v.Errors);

            Assert.True(allErrors.First().ErrorMessage.Contains(error.Description));
        }
Пример #20
0
        public async Task testGetConfirmEmailForExistingUserShipperRole()
        {
            var controller = ControllerSupplier.getAccountController();

            await populateDatabaseWithUser(controller);

            var userManager = controller.getUserManager();

            var user = await userManager.FindByIdAsync(Constants.USER_ID);

            await userManager.AddToRoleAsync(user, AppRole.SHIPPER);

            var code = await userManager.GenerateEmailConfirmationTokenAsync(user);

            var result = await controller.ConfirmEmail(Constants.USER_ID, code);

            Assert.NotNull(result);
            var viewName = ((ViewResult)result).ViewName;

            Assert.Equal(viewName, "ConfirmEmail");
            var dbContext = controller.getApplicationContext();

            Assert.Equal(dbContext.Drivers.ToList <Driver>().Count, 0);
        }