-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Added guard to FormsAuthentication as per issue #1755 #1778
base: master
Are you sure you want to change the base?
Changes from 1 commit
394922c
a4b8796
e21534e
3a656fc
08cfc39
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think it's neccesairy to disable the FormsAuthentication if you don't enable it it's disabled. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But as I explained in the comment, I can't see how else to have unit tests for the feature(possibly creating a new AppDomain, but that's no fun). Everything for FormsAuthentication is static and the unit tests don't/shouldn't run in a any particular order, so once a test has enabled FormsAuthentication, it's enabled for the whole test-run. Hence the whether the test will pass depends on state created by other tests. It will fail nearly all of the time and it's not testable without some way of disabling FormsAuthentication. It still would fail if the tests can run in parallel, but the existing tests would as well. FormsAuthentication could be redesigned not to be static- I don't think it should be static, it maintains a global state- however, that's a non-trivial breaking-change, so I suppose it wouldn't be approved. TL;DR- Accept that it's not testable, or leave the hack in and sweep the design under the carpet. Let me know what you'd like to do and I'll fix it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I understand but when There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps I'm misunderstanding. xUnit can't create a new instance of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah yes sorry my bad :) Sent from my iPhone
|
||
|
||
// When | ||
var result = Record.Exception(() => FormsAuthentication.UserLoggedInResponse(userGuid)); | ||
|
||
// Then | ||
result.ShouldBeOfType(typeof(InvalidOperationException)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please change this to use the generic overload |
||
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)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please change this to use the generic overload |
||
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)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please change this to use the generic overload |
||
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)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please change this to use the generic overload |
||
result.Message.ShouldBeSameAs(expectedMessage); | ||
} | ||
|
||
#endregion | ||
|
||
[Fact] | ||
public void Should_have_authentication_cookie_in_login_response_when_logging_in_with_redirect() | ||
{ | ||
FormsAuthentication.Enable(A.Fake<IPipelines>(), this.config); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation"> | ||
<s:String x:Key="/Default/CodeStyle/CodeCleanup/Profiles/=NancyStandard/@EntryIndexedValue"><?xml version="1.0" encoding="utf-16"?><Profile name="NancyStandard"><CSUseVar><BehavourStyle>CAN_CHANGE_TO_IMPLICIT</BehavourStyle><LocalVariableStyle>ALWAYS_IMPLICIT</LocalVariableStyle><ForeachVariableStyle>ALWAYS_IMPLICIT</ForeachVariableStyle></CSUseVar><CSOptimizeUsings><OptimizeUsings>True</OptimizeUsings><EmbraceInRegion>False</EmbraceInRegion><RegionName></RegionName></CSOptimizeUsings><CSReformatCode>True</CSReformatCode><CSShortenReferences>True</CSShortenReferences><CSReorderTypeMembers>True</CSReorderTypeMembers><CSMakeFieldReadonly>True</CSMakeFieldReadonly></Profile></s:String> | ||
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpCodeStyle/ThisQualifier/INSTANCE_MEMBERS_QUALIFY_MEMBERS/@EntryValue">All</s:String> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should probably remove this file from your PR. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, sorry about that. |
||
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/ANONYMOUS_METHOD_DECLARATION_BRACES/@EntryValue">NEXT_LINE</s:String> | ||
<s:Int64 x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/BLANK_LINES_BETWEEN_USING_GROUPS/@EntryValue">1</s:Int64> | ||
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/INITIALIZER_BRACES/@EntryValue">NEXT_LINE</s:String> | ||
|
@@ -10,5 +11,6 @@ | |
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=MethodPropertyEvent/@EntryIndexedValue"><Policy Inspect="True" Prefix="" Suffix="" Style="AaBb"><ExtraRule Prefix="" Suffix="" Style="Aa_bb" /></Policy></s:String> | ||
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PrivateInstanceFields/@EntryIndexedValue"><Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /></s:String> | ||
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EMigrateBlankLinesAroundFieldToBlankLinesAroundProperty/@EntryIndexedValue">True</s:Boolean> | ||
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EMigrateThisQualifierSettings/@EntryIndexedValue">True</s:Boolean> | ||
<s:String x:Key="/Default/FilterSettingsManager/CoverageFilterXml/@EntryValue"><data><IncludeFilters /><ExcludeFilters /></data></s:String> | ||
<s:String x:Key="/Default/FilterSettingsManager/AttributeFilterXml/@EntryValue"><data /></s:String></wpf:ResourceDictionary> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please use 4 spaces instead of tabs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ug. Yuck. Spaces. Ok. There isn't a style-guide, is there? There seems to be a space for one, but no link.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://github.com/NancyFx/Nancy/blob/master/CONTRIBUTING.md#style-guidelines
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thx
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we please get rid of the region? Thanks 😋