예제 #1
0
 private void UpdateCookiesAndContext(User user)
 {
     byte[] cipherText = user.Id.ToString().Encrypt();
     string base64CipherText = Convert.ToBase64String(cipherText);
     Response.Cookies.Add(new HttpCookie(GetCookieUserFilterAttribute.UserCookieName, base64CipherText));
     HttpContext.User = new UserViewModel {Email = user.Email, Name = user.Name, IsLoggedIn = true};
 }
예제 #2
0
        public void GivenAUser_WhenIInitializeAnEmptyInstance_ThenItIsNotValid()
        {
            var user = new User();
            var ctx = new ValidationContext(user, null, null);
            var validationResults = new List<ValidationResult>();
            bool isValid = Validator.TryValidateObject(user, ctx, validationResults);

            Assert.That(isValid, Is.False);
        }
예제 #3
0
        public void GivenAUser_WhenIInitializeItsProperties_ThenItIsValid()
        {
            var user = new User("Name", "EMail", "hpass", false);
            var ctx = new ValidationContext(user, null, null);
            var validationResults = new List<ValidationResult>();
            bool isValid = Validator.TryValidateObject(user, ctx, validationResults);

            Assert.That(isValid, Is.True);
        }
예제 #4
0
        private void InitializeUserRepository()
        {
            var userA = new User();
            userA.Blogs.Add(new Blog());
            userA.Blogs.Add(new Blog());
            userA.Blogs.Add(new Blog());
            var userB = new User();
            userB.Blogs.Add(new Blog());
            userB.Blogs.Add(new Blog());

            _userDomain.Setup(u => u.GetUsersWithTheirBlogs()).Returns(new List<User> {userA, userB, new User()});
        }
예제 #5
0
        public void GivenABlogId_WhenIAskForItsOrderedPosts_ThenIGetThePostsInDateOrder()
        {
            var posts = new List<Post>
                            {
                                BuildMeA.Post("title 1", "entry 1", new DateTime(2000, 4, 19), DateTime.Today),
                                BuildMeA.Post("title 2", "entry 1", new DateTime(2011, 4, 19), DateTime.Today)
                            };

            Blog blog = BuildMeA
                .Blog("title", "description", _nickname, DateTime.Now)
                .WithPosts(posts);

            _user1 = BuildMeA.User("email", "name", "password")
                .WithBlog(blog);
            _userRepository.Create(_user1);

            var newPosts = _postRepository.GetOrderedBlogPosts(blog.Id) as List<Post>;

            Assert.That(newPosts[0].Title, Is.StringEnding("2"));
        }
예제 #6
0
        public void Setup()
        {
            _transactionScope = new TransactionScope();
            _nickname1 = "nickname1";

            _userRepository = new UserRepository(ConfigurationManager.ConnectionStrings["mblog"].ConnectionString);

            Blog blog1 = BuildMeA
                .Blog("title1", "description1", _nickname1, DateTime.Now);
            Blog blog2 = BuildMeA
                .Blog("title2", "description2", _nickname1, DateTime.Now);

            _user = BuildMeA.User("email1", "name1", "password1")
                .WithBlog(blog1)
                .WithBlog(blog2);

            _userRepository.Create(_user);

            _user2 = BuildMeA.User("email1", "name1", "password1");
            _userRepository.Create(_user2);
        }
        public void SetUp()
        {
            var blogs = new List<Blog> {new Blog {Nickname = Nickname}};

            var user = new User("Name", "EMail", "Password", false);
            user.Blogs = blogs;

            var mockRepo = new Mock<IUserService>();

            mockRepo.Setup(r => r.GetUser(1)).Returns(user);
            mockRepo.Setup(r => r.GetUserWithTheirBlogs(1)).Returns(user);

            var user2 = new User("Name", "EMail", "Password", false);

            mockRepo.Setup(r => r.GetUser(2)).Returns(user2);
            mockRepo.Setup(r => r.GetUserWithTheirBlogs(2)).Returns(user2);

            _userService = mockRepo.Object;
            _mockHttpContext = new Mock<HttpContextBase>();
            _mockHttpContext.SetupProperty(h => h.User);

            HttpContextBase httpContextBase = _mockHttpContext.Object;

            // _userRepository
            var controllerContext = new ControllerContext(httpContextBase,
                                                          new RouteData(),
                                                          new BaseController(null));

            var actionDescriptor = new Mock<ActionDescriptor>();
            actionDescriptor.SetupGet(x => x.ActionName).Returns("Action_With_SomeAttribute");

            _actionExecutingContext =
                new AuthorizationContext(controllerContext,
                                         actionDescriptor.Object);

            _attribute = new GetCookieUserFilterAttribute();
            _attribute.UserService = _userService;
        }
예제 #8
0
        public void GivenTheUserHasCreatedMultiplePosts_WhenIGetAllPosts_ThenIGetTheCorrectPosts()
        {
            var posts = new List<Post>
                            {
                                BuildMeA.Post("title 1", "entry 1", DateTime.Today, DateTime.Today),
                                BuildMeA.Post("title 1", "entry 1", DateTime.Today, DateTime.Today),
                            };

            Blog blog = BuildMeA
                .Blog("title", "description", _nickname, DateTime.Now)
                .WithPosts(posts);

            _user1 = BuildMeA.User("email", "name", "password")
                .WithBlog(blog);

            _userRepository.Create(_user1);

            IEnumerable<Post> newPosts = _postRepository.GetBlogPosts(_nickname);

            Assert.That(newPosts, Is.Not.Null);
            Assert.That(newPosts.Count(), Is.EqualTo(posts.Count));
        }
예제 #9
0
        public void WhenIUpdateAPost_ThenIPostIsUpdated()
        {
            var posts = new List<Post>
                            {
                                BuildMeA.Post("title 1", "entry 1", new DateTime(2010, 4, 19), DateTime.Today),
                            };

            Blog blog = BuildMeA
                .Blog("title", "description", _nickname, DateTime.Now)
                .WithPosts(posts);

            _user1 = BuildMeA.User("email", "name", "password")
                .WithBlog(blog);

            _userRepository.Create(_user1);

            Post post = _postRepository.Entities.Where(p => p.Title == "title 1").FirstOrDefault();
            _postRepository.Update(post.Id, "new title", post.BlogPost);
            post = _postRepository.Entities.Where(p => p.Title == "new title").FirstOrDefault();

            Assert.That(post, Is.Not.Null);
        }
예제 #10
0
        public void GivenAUserThatExists_WhenTheyLogin_ThenTheCorrectCookieIsSet()
        {
            string email = "email";
            var user = new User("", email, "", false) {Id = 1};

            _userDomain.Setup(u => u.GetUser(email)).Returns(user);

            MockHttpContext.SetupProperty(h => h.User);

            Assert.That(_sessionController.HttpContext.User, Is.Null);
            _sessionController.Create(new LoginUserViewModel {Email = email});

            byte[] cipherText = user.Id.ToString().Encrypt();
            string base64CipherText = Convert.ToBase64String(cipherText);


            Assert.That(FakeResponse.Cookies.Count, Is.EqualTo(1));
            HttpCookie cookie = FakeResponse.Cookies[0];
            Assert.That(cookie.Value, Is.EqualTo(base64CipherText));
        }
예제 #11
0
        public void GivenThereIsAComment_WhenIRetriveTheOwningPost_ThenCommentIsRetrived()
        {
            Comment comment = BuildMeA.Comment("This is a comment", DateTime.Now);

            var posts = new List<Post>
                            {
                                BuildMeA.Post("title 1", "entry 1", new DateTime(2011, 4, 19), DateTime.Today).
                                    WithComment(comment)
                            };

            Blog blog = BuildMeA
                .Blog("title", "description", _nickname, DateTime.Now)
                .WithPosts(posts);

            _user1 = BuildMeA.User("email", "name", "password")
                .WithBlog(blog);
            _userRepository.Create(_user1);
            Post newPost = _postRepository.GetBlogPost(posts[0].Id);
            Assert.That(newPost.Comments.Count, Is.EqualTo(1));
        }
예제 #12
0
        public void WhenIUpdateAPost_AndThePostDoesNotExist_ThenAnExceptionIsThrown()
        {
            var posts = new List<Post>
                            {
                                BuildMeA.Post("title 1", "entry 1", new DateTime(2010, 4, 19), DateTime.Today),
                            };

            Blog blog = BuildMeA
                .Blog("title", "description", _nickname, DateTime.Now)
                .WithPosts(posts);

            _user1 = BuildMeA.User("email", "name", "password")
                .WithBlog(blog);

            _userRepository.Create(_user1);

            Post post = _postRepository.Entities.Where(p => p.Title == "title 1").FirstOrDefault();
            Assert.Throws<MBlogException>(() => _postRepository.Update(post.Id + 1001, "new title", post.BlogPost));
        }
예제 #13
0
            GivenTheUserHasMultiplePostsForTheSameYearMonthDayAndTitle_WhenIGetAllPostsForThatYearMonthDayWithTheWrongTitleTitle_ThenIGetNoPosts
            ()
        {
            var posts = new List<Post>
                            {
                                BuildMeA.Post("title 1", "entry 1", new DateTime(2010, 4, 19), DateTime.Today),
                                BuildMeA.Post("title 2", "entry 1", new DateTime(2011, 4, 19), DateTime.Today),
                                BuildMeA.Post("title 3", "entry 1", new DateTime(2011, 4, 19), DateTime.Today),
                                BuildMeA.Post("title 4", "entry 1", new DateTime(2011, 6, 21), DateTime.Today),
                            };

            Blog blog = BuildMeA
                .Blog("title", "description", _nickname, DateTime.Now)
                .WithPosts(posts);

            _user1 = BuildMeA.User("email", "name", "password")
                .WithBlog(blog);

            var posts2 = new List<Post>
                             {
                                 BuildMeA.Post("title 2", "entry 1", new DateTime(2010, 4, 19), DateTime.Today),
                                 BuildMeA.Post("title 2", "entry 1", new DateTime(2011, 4, 19), DateTime.Today),
                                 BuildMeA.Post("title 2", "entry 1", new DateTime(2011, 4, 20), DateTime.Today),
                             };

            Blog blog2 = BuildMeA
                .Blog("title2", "description2", "nickname2", DateTime.Now)
                .WithPosts(posts2);

            _user2 = BuildMeA.User("email2", "name2", "password2")
                .WithBlog(blog2);

            _userRepository.Create(_user1);
            _userRepository.Create(_user2);

            IEnumerable<Post> newPosts = _postRepository.GetBlogPosts(2011, 4, 19, _nickname, "title-3");

            Assert.That(newPosts, Is.Not.Null);
            Assert.That(newPosts.Count(), Is.EqualTo(1));
        }
예제 #14
0
        public void GivenTheUserHasMultiplePosts_WhenIGetAllPostsForThatNickname_ThenIGetTheCorrectPosts()
        {
            var posts = new List<Post>
                            {
                                BuildMeA.Post("title 1", "entry 1", new DateTime(2010, 4, 19), DateTime.Today),
                                BuildMeA.Post("title 1", "entry 1", new DateTime(2011, 4, 19), DateTime.Today),
                                BuildMeA.Post("title 1", "entry 1", new DateTime(2011, 5, 20), DateTime.Today),
                                BuildMeA.Post("title 1", "entry 1", new DateTime(2011, 6, 21), DateTime.Today),
                            };

            Blog blog = BuildMeA
                .Blog("title", "description", _nickname, DateTime.Now)
                .WithPosts(posts);

            _user1 = BuildMeA.User("email", "name", "password")
                .WithBlog(blog);

            var posts2 = new List<Post>
                             {
                                 BuildMeA.Post("title 2", "entry 1", new DateTime(2010, 4, 18), DateTime.Today),
                                 BuildMeA.Post("title 2", "entry 1", new DateTime(2011, 3, 19), DateTime.Today),
                                 BuildMeA.Post("title 2", "entry 1", new DateTime(2011, 4, 20), DateTime.Today),
                             };

            Blog blog2 = BuildMeA
                .Blog("title2", "description2", "nickname2", DateTime.Now)
                .WithPosts(posts2);

            _user2 = BuildMeA.User("email2", "name2", "password2")
                .WithBlog(blog2);

            _userRepository.Create(_user1);
            _userRepository.Create(_user2);

            IEnumerable<Post> newPosts = _postRepository.GetBlogPosts(0, 0, 0, _nickname, null);

            Assert.That(newPosts, Is.Not.Null);
            Assert.That(newPosts.Count(), Is.EqualTo(4));
        }
예제 #15
0
        public void GivenTheUserHasCreatedNoPosts_WhenIGetAllPosts_ThenIGetTheCorrectPosts()
        {
            Blog blog = BuildMeA
                .Blog("title", "description", _nickname, DateTime.Now);

            _user1 = BuildMeA.User("email", "name", "password")
                .WithBlog(blog);

            _userRepository.Create(_user1);

            IEnumerable<Post> newPosts = _postRepository.GetBlogPosts(_nickname);

            Assert.That(newPosts, Is.Not.Null);
            Assert.That(newPosts.Count(), Is.EqualTo(0));
        }
예제 #16
0
        public void GivenAUserThatExists_WhenTheyLogin_ThenIGetRedirectedToTheAdminPage()
        {
            string email = "email";
            var user = new User("", email, "", false);
            _userDomain.Setup(u => u.GetUser(email)).Returns(user);

            var result = _sessionController.Create(new LoginUserViewModel {Email = email}) as RedirectToRouteResult;

            Assert.That(result, Is.Not.Null);
            Assert.That(result.RouteValues["Controller"], Is.EqualTo("Dashboard").IgnoreCase);
            Assert.That(result.RouteValues["Action"], Is.EqualTo("Index").IgnoreCase);
        }
예제 #17
0
        public void GivenAUserThatExists_WhenTheyLogin_ThenTheUserIsInTheContext()
        {
            string email = "email";
            var user = new User("", email, "", false);
            _userDomain.Setup(u => u.GetUser(email)).Returns(user);

            MockHttpContext.SetupProperty(h => h.User);

            Assert.That(_sessionController.HttpContext.User, Is.Null);
            _sessionController.Create(new LoginUserViewModel {Email = email});

            Assert.That(_sessionController.HttpContext.User, Is.Not.Null);
        }
예제 #18
0
        public void GivenAUserThatExists_WhenTheyLogin_ThenTheUserMarkedAsLoggedIn()
        {
            string email = "email";
            var user = new User("", email, "", false);
            _userDomain.Setup(u => u.GetUser(email)).Returns(user);

            MockHttpContext.SetupProperty(h => h.User);

            Assert.That(_sessionController.HttpContext.User, Is.Null);
            _sessionController.Create(new LoginUserViewModel {Email = email});

            var model = _sessionController.HttpContext.User as UserViewModel;
            Assert.That(model.IsAuthenticated, Is.True);
        }
예제 #19
0
        public void GivenAnInvalidUser_WhenTheyLogin_ThenIGetReturnedToTheLoginView()
        {
            string email = "email";
            var user = new User("", email, "", false);

            _userDomain.Setup(u => u.GetUser(email)).Returns(user);

            var result = _sessionController.Create(new LoginUserViewModel()) as ViewResult;
            Assert.That(result, Is.Not.Null);
            Assert.That(result.ViewName, Is.EqualTo("New"));
        }