public async void new_should_redirect_if_model_is_valid()
        {
            //arrange
            var validModel = new UserModel {
                Name = "test name", Email = "*****@*****.**", Password = "******"
            };

            var userService = new Mock <IUserService>();

            userService.Setup(x => x.Create(It.IsAny <UserModel>(), SetLocaleRole.Developer.Value)).Returns(() => Task.FromResult <int?>(1));

            var formsAuthenticationService = new Mock <IFormsAuthenticationService>();

            formsAuthenticationService.Setup(x => x.SignIn(string.Format("{0}|{1}|{2}", 1, validModel.Name, validModel.Email), true));

            //act
            var sut = new UserControllerBuilder().WithUserService(userService.Object)
                      .WithFormsAuthenticationService(formsAuthenticationService.Object)
                      .Build();
            var view = await sut.New(validModel);

            //assert
            Assert.NotNull(view);
            Assert.AreEqual(((RedirectResult)view).Url, "/user/apps");
            userService.Verify(x => x.Create(It.IsAny <UserModel>(), SetLocaleRole.Developer.Value), Times.Once);

            sut.AssertPostAndAntiForgeryTokenAttribute("New", new[] { typeof(UserModel) });
            sut.AssertAllowAnonymousAttribute("New", new[] { typeof(UserModel) });
        }
        public async void any_user_can_login()
        {
            //arrange
            var userService = new Mock <IUserService>();

            userService.Setup(x => x.Authenticate(ValidLoginModel.Email, ValidLoginModel.Password))
            .Returns(Task.FromResult(true));

            userService.Setup(x => x.GetByEmail(ValidLoginModel.Email))
            .Returns(Task.FromResult(ValidUserEntity));

            var authService = new Mock <IAuthService>();

            authService.Setup(x => x.SignIn(ValidUserEntity.Id, ValidUserEntity.Name, ValidUserEntity.Email, ConstHelper.User, true));

            ////act
            var sut = new UserControllerBuilder().WithUserService(userService.Object)
                      .WithAuthService(authService.Object)
                      .Build();

            var result = await sut.Login(ValidLoginModel);

            ////assert
            Assert.IsNotNull(result);
            Assert.IsAssignableFrom <RedirectResult>(result);

            userService.Verify(x => x.Authenticate(ValidLoginModel.Email, ValidLoginModel.Password), Times.Once);
            userService.Verify(x => x.GetByEmail(ValidLoginModel.Email), Times.Once);
            authService.Verify(x => x.SignIn(ValidUserEntity.Id, ValidUserEntity.Name, ValidUserEntity.Email, ConstHelper.User, true), Times.Once);

            sut.AssertPostAttribute(ACTION_LOGIN, new[] { typeof(LoginModel) });
            sut.AssertAllowAnonymousAttribute(ACTION_LOGIN, new[] { typeof(LoginModel) });
        }
        public async void words_should_return_with_word_model()
        {
            //arrange
            var list = new List <Word> {
                new Word {
                    Id = 1
                }, new Word {
                    Id = 2
                }
            };
            var wordService = new Mock <IWordService>();

            wordService.Setup(x => x.GetByUserId(1, 1))
            .Returns(Task.FromResult(new PagedList <Word>(1, 2, 3, list)));

            //act
            var sut = new UserControllerBuilder().WithWordService(wordService.Object)
                      .Build();

            var view = await sut.Words(1, 1);

            var model = view.Model as PageModel <WordModel>;

            //assert
            Assert.NotNull(view);
            Assert.NotNull(model);
            Assert.IsInstanceOf <BaseController>(sut);
            Assert.IsAssignableFrom <PageModel <WordModel> >(view.Model);
            Assert.AreEqual(model.Items.Count, list.Count);
            CollectionAssert.AllItemsAreUnique(model.Items);

            wordService.Verify(x => x.GetByUserId(1, 1), Times.Once);

            sut.AssertGetAttribute(ActionNameWords, new[] { typeof(int), typeof(int) });
        }
        public async void apps_should_return_with_app_model()
        {
            //arrange
            var list = new List <App> {
                new App {
                    Id = 1, Tokens = new List <Token>()
                }, new App {
                    Id = 2, Tokens = new List <Token>()
                }
            };
            var appService = new Mock <IAppService>();

            appService.Setup(x => x.GetByUserId(1, 1))
            .Returns(Task.FromResult(new PagedList <App>(1, 2, 3, list)));

            //act
            var sut = new UserControllerBuilder().WithAppService(appService.Object)
                      .Build();
            var view = await sut.Apps(1, 1) as ViewResult;

            var model = view.Model as PageModel <AppModel>;

            //assert
            Assert.NotNull(view);
            Assert.NotNull(model);
            Assert.IsInstanceOf <BaseController>(sut);
            Assert.IsAssignableFrom <PageModel <AppModel> >(view.Model);
            Assert.AreEqual(model.Items.Count, list.Count);
            CollectionAssert.AllItemsAreUnique(model.Items);

            appService.Verify(x => x.GetByUserId(1, 1), Times.Once);

            sut.AssertGetAttribute(ActionNameApps, new[] { typeof(int), typeof(int) });
        }
        public void Add_NewUser_Returns200OK(string name, string username, string password)
        {
            var expected = new UserTransferObject
            {
                name     = name,
                username = username,
                password = password
            };

            var builder     = new UserControllerBuilder();
            var serviceMock = builder.GetDefaultUserService();

            serviceMock.Setup(r => r.AddUser(It.IsAny <UserTransferObject>()))
            .Returns(ServiceResult <UserTransferObject> .SuccessResult(
                         new UserTransferObject
            {
                name     = name,
                username = username,
                password = password
            }));

            var controller = builder.WithUserService(serviceMock.Object).Build();

            var response = controller.addUsers(expected);

            Assert.IsType <OkObjectResult>(response);
        }
        public void GetAll_User_Returns200OK()
        {
            var builder     = new UserControllerBuilder();
            var serviceMock = builder.GetDefaultUserService();

            serviceMock.Setup(r => r.getAll())
            .Returns(ServiceResult <IEnumerable <UserTransferObject> > .SuccessResult(
                         Enumerable.Empty <UserTransferObject>()));

            var controller = builder.WithUserService(serviceMock.Object).Build();
            var response   = controller.getUsers();

            Assert.IsType <OkObjectResult>(response);
        }
        public void login_should_return_with_login_model()
        {
            //act
            var sut  = new UserControllerBuilder().Build();
            var view = sut.Login() as ViewResult;

            //assert
            Assert.NotNull(view);
            var model = view.Model;

            Assert.NotNull(model);
            Assert.IsAssignableFrom(typeof(LoginModel), model);

            sut.AssertGetAttribute("Login");
            sut.AssertAllowAnonymousAttribute("Login");
        }
        public void reset_should_return_with_reset_model()
        {
            //act
            var sut  = new UserControllerBuilder().BuildWithMockControllerContext();
            var view = sut.Reset() as ViewResult;

            //assert
            Assert.NotNull(view);
            var model = view.Model;

            Assert.NotNull(model);
            Assert.IsAssignableFrom(typeof(ResetModel), model);

            sut.AssertGetAttribute("Reset");
            sut.AssertAllowAnonymousAttribute("Reset");
        }
        public void any_user_can_logout()
        {
            //arrange
            var authService = new Mock <IAuthService>();

            authService.Setup(x => x.SignOut());

            //act
            var sut = new UserControllerBuilder().WithAuthService(authService.Object)
                      .Build();
            var result = sut.Logout();

            //assert
            Assert.IsNotNull(result);
            Assert.IsAssignableFrom <RedirectResult>(result);

            sut.AssertGetAttribute(ACTION_LOGOUT);
        }
        public async void logout_should_redirect()
        {
            //arrange
            var formsAuthenticationService = new Mock <IFormsAuthenticationService>();

            formsAuthenticationService.Setup(x => x.SignOut());

            //act
            var sut = new UserControllerBuilder().WithFormsAuthenticationService(formsAuthenticationService.Object)
                      .Build();

            var view = sut.Logout() as RedirectResult;

            //assert
            Assert.NotNull(view);
            Assert.AreEqual(view.Url, "/");
            sut.AssertGetAttribute("Logout");
        }
        public async void login_should_return_with_login_model_if_model_is_invalid()
        {
            //arrange
            var invalidModel = new LoginModel {
                Email = "*****@*****.**"
            };

            //act
            var sut  = new UserControllerBuilder().Build();;
            var view = await sut.Login(invalidModel) as ViewResult;

            //assert
            Assert.NotNull(view);
            var model = view.Model;

            Assert.NotNull(model);
            Assert.IsAssignableFrom(typeof(LoginModel), model);

            sut.AssertPostAndAntiForgeryTokenAttribute("Login", new[] { typeof(LoginModel) });
            sut.AssertAllowAnonymousAttribute("Login", new[] { typeof(LoginModel) });
        }
        public async void new_should_return_with_user_model_if_model_is_invalid()
        {
            //arrange
            var invalidModel = new UserModel {
                Name = "test name"
            };

            //act
            var sut  = new UserControllerBuilder().Build();
            var view = await sut.New(invalidModel) as ViewResult;

            //assert
            Assert.NotNull(view);
            var model = view.Model;

            Assert.NotNull(model);
            Assert.IsAssignableFrom(typeof(UserModel), model);

            sut.AssertPostAndAntiForgeryTokenAttribute("New", new[] { typeof(UserModel) });
            sut.AssertAllowAnonymousAttribute("New", new[] { typeof(UserModel) });
        }
        public async void any_user_can_change_password()
        {
            //arrange
            var userService = new Mock <IUserService>();

            userService.Setup(x => x.ChangePassword(ValidPasswordChangeModel.Email, ValidPasswordChangeModel.Token, ValidPasswordChangeModel.Password))
            .Returns(Task.FromResult(true));

            //act
            var sut = new UserControllerBuilder().WithUserService(userService.Object)
                      .Build();
            var result = await sut.PasswordChange(ValidPasswordChangeModel);

            //assert
            Assert.IsNotNull(result);
            Assert.IsAssignableFrom <RedirectResult>(result);

            userService.Verify(x => x.ChangePassword(ValidPasswordChangeModel.Email, ValidPasswordChangeModel.Token, ValidPasswordChangeModel.Password), Times.Once);

            sut.AssertPostAttributeWithOutAntiForgeryToken(ACTION_PASSWORD_CHANGE, new[] { typeof(PasswordChangeModel) });
            sut.AssertAllowAnonymousAttribute(ACTION_PASSWORD_CHANGE, new[] { typeof(PasswordChangeModel) });
        }
        public async void any_user_can_request_password_reset_link()
        {
            //arrange
            var userService = new Mock <IUserService>();

            userService.Setup(x => x.RequestPasswordReset(ValidPasswordResetModel.Email))
            .Returns(Task.FromResult(true));

            //act
            var sut = new UserControllerBuilder().WithUserService(userService.Object)
                      .Build();
            var result = await sut.PasswordReset(ValidPasswordResetModel);

            //assert
            Assert.IsNotNull(result);
            Assert.IsAssignableFrom <ViewResult>(result);

            userService.Verify(x => x.RequestPasswordReset(ValidPasswordResetModel.Email), Times.Once);

            sut.AssertPostAttribute(ACTION_PASSWORD_RESET, new[] { typeof(PasswordResetModel) });
            sut.AssertAllowAnonymousAttribute(ACTION_PASSWORD_RESET, new[] { typeof(PasswordResetModel) });
        }
        public async void login_should_redirect_if_model_is_valid()
        {
            //arrange
            const int    id         = 1;
            const string email      = "*****@*****.**";
            const string name       = "test";
            var          validModel = new LoginModel {
                Email = email, Password = "******"
            };

            var userService = new Mock <IUserService>();

            userService.Setup(x => x.Authenticate(validModel.Email, validModel.Password)).Returns(() => Task.FromResult(true));
            userService.Setup(x => x.GetByEmail(validModel.Email))
            .Returns(() => Task.FromResult(new User
            {
                Id    = id,
                Name  = name,
                Email = email
            }));

            var formsAuthenticationService = new Mock <IFormsAuthenticationService>();

            formsAuthenticationService.Setup(x => x.SignIn(string.Format("{0}|{1}|{2}", id, name, email), true));

            //act
            var sut = new UserControllerBuilder().WithUserService(userService.Object)
                      .WithFormsAuthenticationService(formsAuthenticationService.Object)
                      .Build();
            var view = await sut.Login(validModel) as RedirectResult;

            //assert
            Assert.NotNull(view);
            Assert.AreEqual(view.Url, "/user/apps");
            userService.Verify(x => x.Authenticate(validModel.Email, validModel.Password), Times.Once);

            sut.AssertPostAndAntiForgeryTokenAttribute("Login", new[] { typeof(LoginModel) });
            sut.AssertAllowAnonymousAttribute("Login", new[] { typeof(LoginModel) });
        }
        public async void change_status_should_return_with_response_model()
        {
            //arrange
            var userService = new Mock <IUserService>();

            userService.Setup(x => x.ChangeStatus(1, true)).Returns(() => Task.FromResult(new bool()));

            //act
            var sut = new UserControllerBuilder().WithUserService(userService.Object)
                      .Build();

            var view = await sut.ChangeStatus(1, true);

            //assert
            Assert.NotNull(view);

            var model = view.Data;

            Assert.NotNull(model);
            Assert.IsAssignableFrom(typeof(ResponseModel), model);

            sut.AssertPostAndAntiForgeryTokenAttribute("ChangeStatus", new[] { typeof(int), typeof(bool) });
            userService.Verify(x => x.ChangeStatus(1, true), Times.Once);
        }