diff --git a/src/Nancy.Authentication.Forms.Tests/FormsAuthenticationFixture.cs b/src/Nancy.Authentication.Forms.Tests/FormsAuthenticationFixture.cs index 89a6edcc29..c423ac58ae 100644 --- a/src/Nancy.Authentication.Forms.Tests/FormsAuthenticationFixture.cs +++ b/src/Nancy.Authentication.Forms.Tests/FormsAuthenticationFixture.cs @@ -8,7 +8,6 @@ namespace Nancy.Authentication.Forms.Tests using FakeItEasy; using Fakes; using Helpers; - using Nancy.Security; using Nancy.Tests; using Nancy.Tests.Fakes; using Xunit; @@ -162,7 +161,71 @@ public void Should_return_ok_response_when_user_logs_in_without_redirect() result.StatusCode.ShouldEqual(HttpStatusCode.OK); } - [Fact] + #region Throw helpful exception when the configuration is not enabled + + [Fact] + public void Should_throw_helpful_exception_message_when_user_logs_in_without_redirect_and_forms_authentication_not_enabled() + { + // Given + const string expectedMessage = "The internal FormsAuthenticationConfiguration has not been set. Ensure that FormsAuthentication has been enabled in the bootstrapper"; + FormsAuthentication.Disable(); + + // When + var result = Record.Exception(() => FormsAuthentication.UserLoggedInResponse(userGuid)); + + // Then + result.ShouldBeOfType(typeof(InvalidOperationException)); + result.Message.ShouldBeSameAs(expectedMessage); + } + + [Fact] + public void Should_throw_helpful_exception_message_when_user_logs_in_with_redirect_and_forms_authentication_not_enabled() + { + // Given + const string expectedMessage = "The internal FormsAuthenticationConfiguration has not been set. Ensure that FormsAuthentication has been enabled in the bootstrapper"; + FormsAuthentication.Disable(); + + // When + var result = Record.Exception(() => FormsAuthentication.UserLoggedInRedirectResponse(context, userGuid)); + + // Then + result.ShouldBeOfType(typeof(InvalidOperationException)); + result.Message.ShouldBeSameAs(expectedMessage); + } + + [Fact] + public void Should_throw_helpful_exception_message_when_user_logs_out_with_redirect_and_forms_authentication_not_enabled() + { + // Given + const string expectedMessage = "The internal FormsAuthenticationConfiguration has not been set. Ensure that FormsAuthentication has been enabled in the bootstrapper"; + FormsAuthentication.Disable(); + + // When + var result = Record.Exception(() => FormsAuthentication.LogOutAndRedirectResponse(context, "/")); + + // Then + result.ShouldBeOfType(typeof(InvalidOperationException)); + result.Message.ShouldBeSameAs(expectedMessage); + } + + [Fact] + public void Should_throw_helpful_exception_message_when_user_logs_out_without_redirect_and_forms_authentication_not_enabled() + { + // Given + const string expectedMessage = "The internal FormsAuthenticationConfiguration has not been set. Ensure that FormsAuthentication has been enabled in the bootstrapper"; + FormsAuthentication.Disable(); + + // When + var result = Record.Exception(() => FormsAuthentication.LogOutResponse()); + + // Then + result.ShouldBeOfType(typeof(InvalidOperationException)); + result.Message.ShouldBeSameAs(expectedMessage); + } + + #endregion + + [Fact] public void Should_have_authentication_cookie_in_login_response_when_logging_in_with_redirect() { FormsAuthentication.Enable(A.Fake(), this.config); diff --git a/src/Nancy.Authentication.Forms/FormsAuthentication.cs b/src/Nancy.Authentication.Forms/FormsAuthentication.cs index 8652347292..894b751211 100644 --- a/src/Nancy.Authentication.Forms/FormsAuthentication.cs +++ b/src/Nancy.Authentication.Forms/FormsAuthentication.cs @@ -34,7 +34,15 @@ public static string FormsAuthenticationCookieName } } - /// + /// + /// To support testing, necessary as everying is static, but not ideal + /// + internal static void Disable() + { + currentConfiguration = null; + } + + /// /// Enables forms authentication for the application /// /// Pipelines to add handlers to (usually "this") @@ -110,7 +118,12 @@ public static void Enable(INancyModule module, FormsAuthenticationConfiguration /// Nancy response with redirect. public static Response UserLoggedInRedirectResponse(NancyContext context, Guid userIdentifier, DateTime? cookieExpiry = null, string fallbackRedirectUrl = null) { - var redirectUrl = fallbackRedirectUrl; + if (currentConfiguration == null) + { + throw new InvalidOperationException("The internal FormsAuthenticationConfiguration has not been set. Ensure that FormsAuthentication has been enabled in the bootstrapper"); + } + + var redirectUrl = fallbackRedirectUrl; if (string.IsNullOrEmpty(redirectUrl)) { @@ -149,11 +162,14 @@ public static Response UserLoggedInRedirectResponse(NancyContext context, Guid u /// Nancy response with status public static Response UserLoggedInResponse(Guid userIdentifier, DateTime? cookieExpiry = null) { - var response = - (Response)HttpStatusCode.OK; + if (currentConfiguration == null) + { + throw new InvalidOperationException("The internal FormsAuthenticationConfiguration has not been set. Ensure that FormsAuthentication has been enabled in the bootstrapper"); + } + + var response = (Response)HttpStatusCode.OK; - var authenticationCookie = - BuildCookie(userIdentifier, cookieExpiry, currentConfiguration); + var authenticationCookie = BuildCookie(userIdentifier, cookieExpiry, currentConfiguration); response.AddCookie(authenticationCookie); @@ -168,7 +184,12 @@ public static Response UserLoggedInResponse(Guid userIdentifier, DateTime? cooki /// Nancy response public static Response LogOutAndRedirectResponse(NancyContext context, string redirectUrl) { - var response = context.GetRedirect(redirectUrl); + if (currentConfiguration == null) + { + throw new InvalidOperationException("The internal FormsAuthenticationConfiguration has not been set. Ensure that FormsAuthentication has been enabled in the bootstrapper"); + } + + var response = context.GetRedirect(redirectUrl); var authenticationCookie = BuildLogoutCookie(currentConfiguration); response.AddCookie(authenticationCookie); @@ -181,11 +202,14 @@ public static Response LogOutAndRedirectResponse(NancyContext context, string re /// Nancy response public static Response LogOutResponse() { - var response = - (Response)HttpStatusCode.OK; + if (currentConfiguration == null) + { + throw new InvalidOperationException("The internal FormsAuthenticationConfiguration has not been set. Ensure that FormsAuthentication has been enabled in the bootstrapper"); + } + + var response = (Response)HttpStatusCode.OK; - var authenticationCookie = - BuildLogoutCookie(currentConfiguration); + var authenticationCookie = BuildLogoutCookie(currentConfiguration); response.AddCookie(authenticationCookie); diff --git a/src/Nancy.Testing/Nancy.Testing.csproj b/src/Nancy.Testing/Nancy.Testing.csproj index 12756859d2..22b9d7f6d6 100644 --- a/src/Nancy.Testing/Nancy.Testing.csproj +++ b/src/Nancy.Testing/Nancy.Testing.csproj @@ -85,9 +85,9 @@ bin\MonoRelease\Nancy.Testing.XML - + False - ..\packages\CsQuery.1.3.3\lib\net40\CsQuery.dll + ..\..\..\..\savetrees\code\references\packages\CsQuery.1.3.4\lib\net40\CsQuery.dll @@ -171,7 +171,6 @@ - @@ -180,6 +179,9 @@ Resources.Designer.cs + + +