Пример #1
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));
        }
Пример #2
0
        public void GivenAUserInContext_AndTheUserIsLoggedIn_WhenIListPosts_ThenIGetAllThePosts()
        {
            const string nickname = "nickname";


            _postService.Setup(p => p.GetOrderedBlogPosts(It.IsAny <int>())).Returns(new List <Post>
            {
                new Post {
                    Title = "empty"
                }
            });
            _blogService.Setup(b => b.GetBlog(nickname)).Returns(new Blog {
                Id = 1
            });
            var controller = new PostsController(_postService.Object, _blogService.Object, null);

            SetControllerContext(controller);

            MockHttpContext.SetupProperty(h => h.User);
            controller.HttpContext.User = new UserViewModel {
                IsLoggedIn = true, Id = 1
            };

            var result = (ViewResult)controller.Index(new AdminBlogViewModel {
                Nickname = nickname
            });
            var model = result.Model as PostsViewModel;

            Assert.That(model.Posts.Count, Is.EqualTo(1));
        }
Пример #3
0
        public void GivenAValidPost_WhenITryAndDeleteThePost_ThenIGetASuccessfulResult()
        {
            MockHttpContext.SetupProperty(h => h.User);
            var controller = new PostController(_postServiceMock.Object, _dashboardServiceMock, _blogService.Object, null);

            var result = (HttpStatusCodeResult) controller.Delete(_userName, 2);
            Assert.That(result.StatusCode, Is.EqualTo(200));
        }
Пример #4
0
        public void GivenAValidPost_WhenITryAndDeleteThePost_ThenItIsDeleted()
        {
            MockHttpContext.SetupProperty(h => h.User);
            var controller = new PostController(_postServiceMock.Object, _dashboardServiceMock, _blogService.Object, null);

            controller.Delete(_userName, 2);
            _postServiceMock.Verify(p => p.Delete(2), Times.Once());
        }
Пример #5
0
        public void GivenAPostController_WhenICallItsNewMethod_ThenItReturnsTheCorrectView()
        {
            MockHttpContext.SetupProperty(h => h.User);

            var controller = new PostController(_postServiceMock.Object, _dashboardServiceMock, _blogService.Object, null);
            var result = controller.New(_userName) as ViewResult;

            Assert.That(result, Is.TypeOf<ViewResult>());
        }
Пример #6
0
        public void GivenAnInvalidPost_WhenITryAndDeleteThePost_ThenIGetAnError()
        {
            MockHttpContext.SetupProperty(h => h.User);
            _postServiceMock.Setup(p => p.Delete(It.IsAny<int>())).Throws<Exception>();
            var controller = new PostController(_postServiceMock.Object, _dashboardServiceMock, _blogService.Object, null);

            var result = (HttpStatusCodeResult)controller.Delete(_userName, 2);
            Assert.That(result.StatusCode, Is.EqualTo(500));
        }
Пример #7
0
        public void GivenAValidPost_WhenITryAndEditAPost_ThenIGetTheCorrectView()
        {
            MockHttpContext.SetupProperty(h => h.User);
            var controller = new PostController(_postServiceMock.Object, _dashboardServiceMock, _blogService.Object, null);

            ActionResult result = controller.Edit(_userName, 1);

            Assert.That(result, Is.TypeOf<ViewResult>());
        }
Пример #8
0
        public void GivenAMediaController_WhenNewIsCalled_ThenTheNewViewIsReturned()
        {
            MockHttpContext.SetupProperty(h => h.User);

            var controller = new MediaController(_mediaService.Object, null);

            ActionResult result = controller.New(new NewMediaViewModel {
                Nickname = "nickname"
            });

            Assert.That(result, Is.TypeOf <ViewResult>());
        }
Пример #9
0
        public void GivenAUserInContext_AndTheUserIsLoggedIn_WhenIGoToTheAdminIndexPage_ThenIGetTheAdminPage()
        {
            SetControllerContext(_controller);

            MockHttpContext.SetupProperty(h => h.User);
            _controller.HttpContext.User = new UserViewModel {
                IsLoggedIn = true, Id = 1
            };

            var result = (ViewResult)_controller.Index();

            Assert.That(result, Is.Not.Null);
        }
Пример #10
0
        public void GivenALoggedInUser_WhenNewIsCalled_ThenTheNewViewIsReturned()
        {
            MockHttpContext.SetupProperty(h => h.User);

            SetControllerContext(_controller);
            _controller.HttpContext.User = new UserViewModel {
                IsLoggedIn = true
            };

            ActionResult result = _controller.New();

            Assert.That(result, Is.TypeOf <ViewResult>());
        }
Пример #11
0
        public void GivenNoLoggedInUser_WhenNewIsCalled_ThenTheRequestIsRedirected()
        {
            MockHttpContext.SetupProperty(h => h.User);

            SetControllerContext(_controller);
            _controller.HttpContext.User = new UserViewModel {
                IsLoggedIn = false
            };

            ActionResult result = _controller.New();

            Assert.That(result, Is.TypeOf <RedirectToRouteResult>());
        }
Пример #12
0
        public void GivenAUserInContext_AndTheUserIsLoggedIn_WhenIGoToTheAdminIndexPage_ThenIGetAllTheBlogs()
        {
            SetControllerContext(_controller);

            MockHttpContext.SetupProperty(h => h.User);
            _controller.HttpContext.User = new UserViewModel {
                IsLoggedIn = true, Id = 1
            };

            var result = (ViewResult)_controller.Index();
            var model  = (AdminUserViewModel)result.Model;

            Assert.That(model.Blogs.Count, Is.EqualTo(1));
        }
Пример #13
0
        public void GivenALoggedInUser_WhenCreateIsCalled_AndTheModelIsValid_ThenTheAdminPageIsReturned()
        {
            MockHttpContext.SetupProperty(h => h.User);

            SetControllerContext(_controller);
            _controller.HttpContext.User = new UserViewModel {
                IsLoggedIn = true
            };

            var result = _controller.Create(new CreateBlogViewModel()) 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);
        }
Пример #14
0
        public void GivenALoggedInUser_WhenCreateIsCalled_AndTheModelIsInvalid_ThenTheNewViewIsReturned()
        {
            MockHttpContext.SetupProperty(h => h.User);

            SetControllerContext(_controller);
            _controller.HttpContext.User = new UserViewModel {
                IsLoggedIn = true
            };
            _controller.ModelState.AddModelError("error", "message");

            var result = _controller.Create(new CreateBlogViewModel()) as ViewResult;

            Assert.That(result, Is.Not.Null);
            Assert.That(result.ViewName, Is.EqualTo("New").IgnoreCase);
        }
Пример #15
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);
        }
Пример #16
0
        public void GivenAMediaController_WhenIUploadAValidFile_ThenAValidUrlIsReturned()
        {
            var    fileBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
            string fileName  = "foo.jpg";
            var    fileBase  = new Mock <HttpPostedFileBase>();

            fileBase.Setup(f => f.ContentLength).Returns(fileBytes.Length);
            fileBase.Setup(s => s.InputStream).Returns(new MemoryStream(fileBytes));
            fileBase.Setup(s => s.FileName).Returns(fileName);

            var controller = new MediaController(_mediaService.Object, null);

            controller.Url = new UrlHelper(new RequestContext(MockHttpContext.Object, new RouteData()), Routes);
            DateTime now   = DateTime.Now;
            Media    media = new Media {
                Year = now.Year, Month = now.Month, Day = now.Day, LinkKey = fileName
            };

            _mediaService.Setup(
                m => m.WriteMedia(It.IsAny <string>(), It.IsAny <int>(), It.IsAny <string>(), It.IsAny <Stream>(),
                                  It.IsAny <int>())).Returns(media);
            SetControllerContext(controller);
            MockHttpContext.SetupProperty(h => h.User);
            controller.HttpContext.User = new UserViewModel {
                IsLoggedIn = true, Id = 1
            };


            JsonResult result =
                controller.Create(new NewMediaViewModel
            {
                Title       = "title",
                Caption     = "caption",
                Description = "description",
                Alternate   = "alternate",
                Alignment   = (int)Media.ValidAllignments.Left,
                Size        = (int)Media.ValidSizes.Medium,
                File        = fileBase.Object
            });

            Assert.That(result, Is.TypeOf <JsonResult>());
            var response = (MediaCreateJsonResponse)result.Data;

            Assert.That(response.url,
                        Is.EqualTo(string.Format("{0}/{1}/{2}/{3}", now.Year, now.Month, now.Day, fileName)));
        }
Пример #17
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);
        }
Пример #18
0
        public void GivenAUserInContext_AndTheUserIsLoggedIn_WhenIListAllThePosts_ThenIGetThePosts()
        {
            SetControllerContext(_controller);

            MockHttpContext.SetupProperty(h => h.User);
            _controller.HttpContext.User = new UserViewModel {
                IsLoggedIn = true, Id = 1
            };
            _blogService.Setup(b => b.GetBlog(It.IsAny <string>())).Returns(new Blog {
                Id = 1
            });
            _postService.Setup(p => p.GetOrderedBlogPosts(1)).Returns(new List <Post> {
                new Post()
            });
            var result = (ViewResult)_controller.ListPosts(new AdminBlogViewModel());
            var model  = (PostsViewModel)result.Model;

            Assert.That(model.Posts.Count, Is.EqualTo(1));
        }
Пример #19
0
        public void GivenALoggdInUser_WhenILogout_ThenTheUserIsRemovedFromTheContext()
        {
            var    cookies    = new HttpCookieCollection();
            string cookieName = "USER";

            // Add response cookies
            FakeResponse.Cookies.Add(new HttpCookie(cookieName));
            Assert.That(FakeResponse.Cookies.Count, Is.EqualTo(1));

            // Add request cookies
            cookies.Add(new HttpCookie(cookieName));
            MockRequest.Setup(r => r.Cookies).Returns(cookies);

            MockHttpContext.SetupProperty(h => h.User);
            _sessionController.HttpContext.User = new UserViewModel();

            Assert.That(_sessionController.HttpContext.User, Is.Not.Null);
            var result = _sessionController.Delete() as RedirectToRouteResult;

            Assert.That(result, Is.Not.Null);
            Assert.That(_sessionController.HttpContext.User, Is.Null);
        }
Пример #20
0
        public void WhenUpdateIsCalled_AndTheModelStateIsValid_ThenTheFileIsWrittenToTheDatabase()
        {
            var fileBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };

            const int userId   = 1001;
            var       fileBase = new Mock <HttpPostedFileBase>();

            fileBase.Setup(f => f.ContentLength).Returns(fileBytes.Length);
            fileBase.Setup(s => s.InputStream).Returns(new MemoryStream(fileBytes));
            fileBase.Setup(s => s.ContentType).Returns("contentType");
            const string fileName = "fileName";

            fileBase.Setup(s => s.FileName).Returns(fileName);
            _mediaService.Setup(m => m.UpdateMediaDetails(1, "title", "caption", "description", "alternate", 1001)).
            Returns(new Media());

            var controller = new MediaController(_mediaService.Object, null);

            controller.Url = new UrlHelper(new RequestContext(MockHttpContext.Object, new RouteData()), Routes);
            SetControllerContext(controller);
            MockHttpContext.SetupProperty(h => h.User);
            controller.HttpContext.User = new UserViewModel {
                IsLoggedIn = true, Id = userId
            };

            var model = new UpdateMediaViewModel
            {
                Id          = 1,
                Title       = "title",
                Caption     = "caption",
                Description = "description",
                Alternate   = "alternate",
            };

            controller.Update(model);

            _mediaService.Verify(i => i.UpdateMediaDetails(1, "title", "caption", "description", "alternate", 1001), Times.Once());
        }
Пример #21
0
        public void GivenAMediaController_WhenIUploadAValidFileUsingHttpContext_ThenTheFileIsWrittenToTheDatabase()
        {
            //Assert.Fail("Write the test");
            var fileBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };

            int userId   = 1001;
            var fileBase = new Mock <HttpPostedFileBase>();

            fileBase.Setup(f => f.ContentLength).Returns(fileBytes.Length);
            fileBase.Setup(s => s.InputStream).Returns(new MemoryStream(fileBytes));
            fileBase.Setup(s => s.ContentType).Returns("contentType");
            const string fileName = "fileName";

            fileBase.Setup(s => s.FileName).Returns(fileName);

            var controller = new MediaController(_mediaService.Object, null);

            SetControllerContext(controller);
            MockHttpContext.SetupProperty(h => h.User);
            controller.HttpContext.User = new UserViewModel {
                IsLoggedIn = true, Id = userId
            };

            controller.Create(new NewMediaViewModel
            {
                Title       = "title",
                Caption     = "caption",
                Description = "description",
                Alternate   = "alternate",
                Alignment   = (int)Media.ValidAllignments.Left,
                Size        = (int)Media.ValidSizes.Medium,
                File        = fileBase.Object
            });

            _mediaService.Verify(
                i => i.WriteMedia(fileName, userId, "contentType", It.IsAny <Stream>(), It.IsAny <int>()), Times.Once());
        }