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);
        }
        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 void LoginForm_Returns_ViewResult()
        {
            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(new BlogConfigurationSection());

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

            var result = controller.LoginForm();

            result.ShouldBeInstanceOfType(typeof(ViewResult));
        }
        public void LoginForm_GIVEN_Username_ABC_Exists_In_Authorisation_Cookie_THEN_Returns_LoginView_With_Username_Filled_In()
        {
            const string username = "******";

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

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

            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).Username.ShouldEqual(username);
        }
        public void LoginForm_GIVEN_redirectUrl_Starts_With_Slash_THEN_Adds_WigglyLine_To_Front_Of_Url_And_Returns_It_As_RedirectUrl()
        {
            const string url = "/admin";

            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(new BlogConfigurationSection());

            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 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);
        }