public void Index_ViewBagContainsUserEmail()
        {
            // arrange
            var tasksRepository = new Mock<ITasksRepository>();
            var usersRepository = new Mock<IUsersRepository>();
            var pathHelper = new Mock<IPathHelper>();
            var hashService = new HashService();
            var shareService = new ShareService(pathHelper.Object, hashService);
            var controller = new ShareController(shareService, tasksRepository.Object, usersRepository.Object);

            var tasks = new List<Task> 
            {
                new Task { Id = 0, UserId = 11,  Description = "Task 0" },
                new Task { Id = 1, UserId = 11, Description = "Task 1" },
                new Task { Id = 2, UserId = 11, Description = "Task 2" }
            };
            tasksRepository.Setup(r => r.Tasks).Returns(tasks.AsQueryable());

            var users = new List<User> 
            {
                new User { Email = "*****@*****.**", Id = 11 }
            };
            usersRepository.Setup(u => u.Users).Returns(users.AsQueryable());

            // act
            var email = "*****@*****.**";
            var correctHash = hashService.CreateMD5Hash(email + "shared_tasks");
            var result = controller.Index(email, correctHash) as ViewResult;

            // assert
            var emailResult = result.ViewBag.Email as string;
            emailResult.Should().Be(email);

        }
        public void GetApiToken_WrongPassword_ReturnedTokenWillBeNull()
        {
            using (var fixture = new FixtureInit("http://localhost"))
            {
                // arrange
                var userRepository = new UsersRepository(fixture.Setup.Context);
                var hashService = new HashService();

                var service = new ApiService(userRepository, hashService);

                var email = "*****@*****.**";
                var password = "******";
                var passwordHash = hashService.CreateMD5Hash(password);
                var apiToken = hashService.CreateApiToken(email, password);

                userRepository.Save(
                    new User
                    {
                        Email = email,
                        PasswordHash = passwordHash,
                        ApiToken = apiToken,
                        Temp = false
                    }
                );

                // act
                var result = service.GetApiToken(email, password + "xx");

                // assert
                Assert.That(result, Is.Null);
            }
        }
        public void Smoke()
        {
            // arrange
            var users = new Mock<IUsersRepository>();
            var forms = new Mock<IFormsAuthentication>();
            var hash = new HashService();
            var service = new AuthenticationService(users.Object, forms.Object, hash);

            // act / post
            Assert.That(service, Is.Not.Null);
        }
        public void Smoke()
        {
            using (var fixture = new FixtureInit("http://localhost"))
            {
                // arrange
                var userRepository = new UsersRepository(fixture.Setup.Context);
                var hashService = new HashService();

                // act / post
                var service = new ApiService(userRepository, hashService);
                Assert.That(service, Is.Not.Null);
            }
        }
        public void CreateShareLink_CreatedLinkAndToken()
        {
            // arrange
            var pathHelper = new Mock<IPathHelper>();
            var hashService = new HashService();
            var shareService = new ShareService(pathHelper.Object, hashService);

            pathHelper.SetReturnsDefault("/user/share");

            // act
            var link = shareService.CreateShareLink("*****@*****.**");

            // assert
            link.Should().Contain("/user/share/[email protected]?key=");
        }
        public void ValidateShareKey_WrongKey()
        {
            var pathHelper = new Mock<IPathHelper>();
            var hashService = new HashService();
            var shareService = new ShareService(pathHelper.Object, hashService);

            pathHelper.SetReturnsDefault("/user/share");

            var key = "dd12113dcb92ffeb0d7afaf0261fAAAA";

            // act
            var result = shareService.ValidateShareKey("*****@*****.**", key);

            // assert
            result.Should().Be.False();
        }
        public void Index_KeyHashIsWrong_RedirectedToError()
        {
            // arrange
            var tasksRepository = new Mock<ITasksRepository>();
            var usersRepository = new Mock<IUsersRepository>();
            var pathHelper = new Mock<IPathHelper>();
            var hashService = new HashService();
            var shareService = new ShareService(pathHelper.Object, hashService);
            var controller = new ShareController(shareService, tasksRepository.Object, usersRepository.Object);

            // act
            var result = controller.Index("email", "this_is_wrong_hash") as RedirectToRouteResult;

            // assert
            var action = result.RouteValues["action"] as string;
            action.Should().Be("error");
        }
        public void FailedAuthentication_WrongPassword()
        {
            // arrange
            var users = new Mock<IUsersRepository>();
            var forms = new Mock<IFormsAuthentication>();
            var hash = new HashService();
            var service = new AuthenticationService(users.Object, forms.Object, hash);

            users.Setup(u => u.Users).Returns(new List<User> { new User { Email = "*****@*****.**", PasswordHash = hash.CreateMD5Hash("111"), Id = 1, Temp = false } }.AsQueryable());
            forms.Setup(f => f.SetAuthCookie("", false)).Throws(new Exception("SetAuthCookie must not be called for failed authentication"));

            // act
            var auth = service.Authenticate("*****@*****.**", "1111");

            // post
            Assert.That(auth, Is.False);
        }
        public void SuccessAuthentication()
        {
            // arrange
            var users = new Mock<IUsersRepository>();
            var forms = new Mock<IFormsAuthentication>();
            var hash = new HashService();
            var service = new AuthenticationService(users.Object, forms.Object, hash);

            users.Setup(u => u.Users).Returns(new List<User> { new User { Email = "*****@*****.**", PasswordHash = hash.CreateMD5Hash("111"), Id = 1, Temp = false } }.AsQueryable());

            // act
            var auth = service.Authenticate("*****@*****.**", "111");

            // post
            Assert.That(auth, Is.True);
            forms.Verify(f => f.SetAuthCookie("*****@*****.**", false));
        }
        public void GetUserId()
        {
            // arrange
            var users = new Mock<IUsersRepository>();
            var forms = new Mock<IFormsAuthentication>();
            var hash = new HashService();
            var service = new AuthenticationService(users.Object, forms.Object, hash);

            users.Setup(u => u.Users).Returns(new List<User> { new User { Email = "*****@*****.**", PasswordHash = hash.CreateMD5Hash("111"), Id = 1, Temp = false } }.AsQueryable());

            // act
            var id = service.GetUserIdByEmail("*****@*****.**");

            // assert
            Assert.That(id, Is.EqualTo(1));
        }
        public void CreateNewUser_ApiToken_Created()
        {
            // arrange
            var usersRepository = new Mock<IUsersRepository>();
            var forms = new Mock<IFormsAuthentication>();
            var hash = new HashService();
            var service = new AuthenticationService(usersRepository.Object, forms.Object, hash);

            var users = new List<User>();
            usersRepository.Setup(u => u.Users).Returns(users.AsQueryable());
            usersRepository.Setup(u => u.Save(It.IsAny<User>())).Callback((User u) => users.Add(u));

            // act
            service.RegisterNewUser("*****@*****.**", "mypass");

            // assert
            Assert.That(users.Count, Is.GreaterThan(0));
            var found = users.Find((u) => u.Email == "*****@*****.**");
            Assert.That(found.ApiToken.Length, Is.EqualTo(32));

        }
        public void CreateNewUser_Returns_False_If_User_Exists()
        {
            // arrange
            var usersRepository = new Mock<IUsersRepository>();
            var forms = new Mock<IFormsAuthentication>();
            var hash = new HashService();
            var service = new AuthenticationService(usersRepository.Object, forms.Object, hash);

            var users = new List<User>();
            usersRepository.Setup(u => u.Users).Returns(users.AsQueryable());
            usersRepository.Setup(u => u.Save(It.IsAny<User>())).Callback((User u) => users.Add(u));

            // act
            service.RegisterNewUser("*****@*****.**", "mypass");
            var result = service.RegisterNewUser("*****@*****.**", "mypass");

            // assert
            Assert.That(result, Is.False);
        }
        public void CreateNewUser_User_Immediately_Auhtenticated()
        {
            //arrange
            var usersRepository = new Mock<IUsersRepository>();
            var forms = new Mock<IFormsAuthentication>();
            var hash = new HashService();
            var service = new AuthenticationService(usersRepository.Object, forms.Object, hash);

            var users = new List<User>();
            usersRepository.Setup(u => u.Users).Returns(users.AsQueryable());
            usersRepository.Setup(u => u.Save(It.IsAny<User>())).Callback((User u) => users.Add(u));

            // act
            service.RegisterNewUser("*****@*****.**", "mypass");

            // assert
            Assert.That(users.Count, Is.GreaterThan(0));
            forms.Verify(f => f.SetAuthCookie("*****@*****.**", false));
        }