public void LoginForm_GIVEN_Configuration_BlogConfigurationSection_Has_DefaultRedirectUrl_And_RedirectAcceptedHosts_Contains_localhost_WHEN_Redirect_Url_Is_For_A_Differnt_Web_Domain_THEN_Sets_RedirectUrl_To_DefaultRedirectUrl()
        {
            const string defaultRedirectUrl = "http://localhost/test.html";

            var blogConfiguration = new BlogConfigurationSection();

            var acceptedUrlConfig = new AcceptedRedirectUrlElement();
            acceptedUrlConfig.Url = "http://localhost/";

            blogConfiguration.AcceptedRedirectUrls = new AcceptedRedirectUrlCollection();
            blogConfiguration.AcceptedRedirectUrls.AuthenticationDefaultRedirectUrl = defaultRedirectUrl;
            blogConfiguration.AcceptedRedirectUrls.CallMethod("BaseAdd", acceptedUrlConfig);

            var mockedConfigurationReader = new Mock<IConfigurationReader>();
            mockedConfigurationReader.Setup(cr => cr.GetSection<BlogConfigurationSection>(It.IsAny<string>())).Returns(blogConfiguration);

            var mockedWebAuthenticator = new Mock<IWebAuthenticator>();
            mockedWebAuthenticator.Setup(c => c.GetUsernameFromCookie()).Returns(String.Empty);

            var controller = new AuthenticationController(mockedWebAuthenticator.Object, mockedConfigurationReader.Object);
            controller.SetPrincipal(GetPrincipal(false));

            var result = controller.LoginForm("Http://www.bbc.co.uk") as ViewResult;

            result.ShouldNotBeNull();
            result.Model.ShouldBeInstanceOfType(typeof(LoginViewModel));
            ((LoginViewModel)result.Model).RedirectUrl.ShouldEqual(defaultRedirectUrl);
        }
        private BlogConfigurationSection GetBlogConfigurationSection(string acceptedUrl = "http://localhost/", string defaultRedirectUrl = "http://localhost/test.html", 
            string defaultController = "Blog", string defaultAction = "Index")
        {
            var blogConfiguration = new BlogConfigurationSection();
            blogConfiguration.DefaultAction = defaultAction;
            blogConfiguration.DefaultController = defaultController;

            var acceptedUrlConfig = new AcceptedRedirectUrlElement();
            acceptedUrlConfig.Url = acceptedUrl;

            blogConfiguration.AcceptedRedirectUrls = new AcceptedRedirectUrlCollection();
            blogConfiguration.AcceptedRedirectUrls.AuthenticationDefaultRedirectUrl = defaultRedirectUrl;
            blogConfiguration.AcceptedRedirectUrls.CallMethod("BaseAdd", acceptedUrlConfig);

            return blogConfiguration;
        }
        public void LoginForm_GIVEN_No_redirectUrl_THEN_Sets_RedirectUrl_On_ViewModel_To_DefaultRedirectUrl_From_Config()
        {
            const string defaultRedirectUrl = "http://localhost/test.html";

            var blogConfiguration = new BlogConfigurationSection();

            blogConfiguration.AcceptedRedirectUrls = new AcceptedRedirectUrlCollection();
            blogConfiguration.AcceptedRedirectUrls.AuthenticationDefaultRedirectUrl = defaultRedirectUrl;

            var mockedConfigurationReader = new Mock<IConfigurationReader>();
            mockedConfigurationReader.Setup(cr => cr.GetSection<BlogConfigurationSection>(It.IsAny<string>())).Returns(blogConfiguration);

            var mockedWebAuthenticator = new Mock<IWebAuthenticator>();
            mockedWebAuthenticator.Setup(c => c.GetUsernameFromCookie()).Returns(String.Empty);

            var controller = new AuthenticationController(mockedWebAuthenticator.Object, mockedConfigurationReader.Object);
            controller.SetPrincipal(GetPrincipal(false));

            var result = controller.LoginForm() as ViewResult;

            result.ShouldNotBeNull();
            result.Model.ShouldBeInstanceOfType(typeof(LoginViewModel));
            ((LoginViewModel)result.Model).RedirectUrl.ShouldEqual(defaultRedirectUrl);
        }
        public void LoginForm_WHEN_RedirectUrl_PassedThrough_THEN_Sets_RedirectUrl_On_ViewModel()
        {
            const string url = "http://localhost/test.html";

            var blogConfiguration = new BlogConfigurationSection();

            var acceptedUrlConfig = new AcceptedRedirectUrlElement();
            acceptedUrlConfig.Url = "http://localhost/";

            blogConfiguration.AcceptedRedirectUrls = new AcceptedRedirectUrlCollection();
            blogConfiguration.AcceptedRedirectUrls.AuthenticationDefaultRedirectUrl = "http://jumbleblocks.com/";
            blogConfiguration.AcceptedRedirectUrls.CallMethod("BaseAdd", acceptedUrlConfig);

            var mockedWebAuthenticator = new Mock<IWebAuthenticator>();
            mockedWebAuthenticator.Setup(c => c.GetUsernameFromCookie()).Returns(String.Empty);

            var mockedConfigurationReader = new Mock<IConfigurationReader>();
            mockedConfigurationReader.Setup(cr => cr.GetSection<BlogConfigurationSection>(It.IsAny<string>()))
                .Returns(blogConfiguration);

            var controller = new AuthenticationController(mockedWebAuthenticator.Object, mockedConfigurationReader.Object);
            controller.SetPrincipal(GetPrincipal(false));

            var result = controller.LoginForm(url) as ViewResult;

            result.ShouldNotBeNull();
            result.Model.ShouldBeInstanceOfType(typeof(LoginViewModel));
            ((LoginViewModel)result.Model).RedirectUrl.ShouldEqual(url);
        }
        public static Mock<IConfigurationReader> CreateMockedConfigurationReader(string title = "Jumbleblocks", int postsPerPage = 10)
        {
            var configurationReader = new Mock<IConfigurationReader>();

            var blogSettings = new BlogConfigurationSection
            {
                Title = title,
                PagePostSummaryCount = postsPerPage
            };

            configurationReader.Setup(r => r.GetSection<BlogConfigurationSection>(It.IsAny<string>()))
                .Returns(blogSettings);

            return configurationReader;
        }