Skip to content

Commit e9e2684

Browse files
committed
Add tests for SignOutTestBuilder
1 parent 2359ddd commit e9e2684

File tree

1 file changed

+198
-0
lines changed
  • test/MyTested.AspNetCore.Mvc.Controllers.ActionResults.Test/BuildersTests/ActionResultsTests/SignOutTests

1 file changed

+198
-0
lines changed
Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
namespace MyTested.AspNetCore.Mvc.Test.BuildersTests.ActionResultsTests.SignOutTests
2+
{
3+
using Exceptions;
4+
using Setups;
5+
using Setups.Controllers;
6+
using System.Collections.Generic;
7+
using Utilities;
8+
using Xunit;
9+
10+
public class SignOutTestBuilderTests
11+
{
12+
[Fact]
13+
public void ShouldReturnSignOutShouldNotThrowExceptionIfResultIsSignOutWithCorrectAuthenticationScheme()
14+
{
15+
MyController<MvcController>
16+
.Instance()
17+
.Calling(c => c.SignOutWithAuthenticationSchemes())
18+
.ShouldReturn()
19+
.SignOut(signOut => signOut
20+
.ContainingAuthenticationScheme(AuthenticationScheme.Basic));
21+
}
22+
23+
[Fact]
24+
public void ShouldReturnSignOutShouldThrowExceptionIfResultIsSignOutWithIncorrectAuthenticationScheme()
25+
{
26+
Test.AssertException<SignOutResultAssertionException>(
27+
() =>
28+
{
29+
MyController<MvcController>
30+
.Instance()
31+
.Calling(c => c.SignOutWithAuthenticationSchemes())
32+
.ShouldReturn()
33+
.SignOut(signOut => signOut
34+
.ContainingAuthenticationScheme(AuthenticationScheme.Digest));
35+
},
36+
$"When calling {nameof(MvcController.SignOutWithAuthenticationSchemes)} " +
37+
$"action in MvcController expected sign out result authentication schemes to contain 'Digest', but none was found.");
38+
}
39+
40+
[Fact]
41+
public void ShouldReturnSignOutShouldNotThrowExceptionIfResultIsSignOutWithCorrectMultipleAuthenticationSchemes()
42+
{
43+
MyController<MvcController>
44+
.Instance()
45+
.Calling(c => c.SignOutWithAuthenticationSchemes())
46+
.ShouldReturn()
47+
.SignOut(signOut => signOut
48+
.ContainingAuthenticationSchemes(new List<string>
49+
{
50+
AuthenticationScheme.Basic,
51+
AuthenticationScheme.NTLM
52+
}));
53+
}
54+
55+
[Fact]
56+
public void ShouldReturnSignOutShouldNotThrowExceptionIfResultIsSignOutWithCorrectMultipleAuthenticationSchemesAsParams()
57+
{
58+
MyController<MvcController>
59+
.Instance()
60+
.Calling(c => c.SignOutWithAuthenticationSchemes())
61+
.ShouldReturn()
62+
.SignOut(signOut => signOut
63+
.ContainingAuthenticationSchemes(AuthenticationScheme.Basic, AuthenticationScheme.NTLM));
64+
}
65+
66+
[Fact]
67+
public void ShouldReturnSignOutShouldThrowExceptionIfResultIsNotSignOutWithIncorrectMultipleAuthenticationSchemes()
68+
{
69+
Test.AssertException<SignOutResultAssertionException>(
70+
() =>
71+
{
72+
MyController<MvcController>
73+
.Instance()
74+
.Calling(c => c.SignOutWithAuthenticationSchemes())
75+
.ShouldReturn()
76+
.SignOut(signOut => signOut
77+
.ContainingAuthenticationSchemes(AuthenticationScheme.Digest, AuthenticationScheme.Basic));
78+
},
79+
$"When calling {nameof(MvcController.SignOutWithAuthenticationSchemes)} " +
80+
$"action in MvcController expected sign out result authentication schemes to contain 'Digest', but none was found.");
81+
}
82+
83+
[Fact]
84+
public void ShouldReturnSignOutShouldThrowExceptionIfResultIsNotSignOutWithIncorrectMultipleAuthenticationSchemesCount()
85+
{
86+
Test.AssertException<SignOutResultAssertionException>(
87+
() =>
88+
{
89+
MyController<MvcController>
90+
.Instance()
91+
.Calling(c => c.SignOutWithAuthenticationSchemes())
92+
.ShouldReturn()
93+
.SignOut(signOut => signOut
94+
.ContainingAuthenticationSchemes(AuthenticationScheme.Digest));
95+
},
96+
$"When calling {nameof(MvcController.SignOutWithAuthenticationSchemes)} " +
97+
$"action in MvcController expected sign out result authentication schemes to be 1, but instead found 2.");
98+
}
99+
100+
[Fact]
101+
public void ShouldReturnSignOutShouldNotThrowExceptionIfResultIsSignOutWithCorrectAuthenticationProperties()
102+
{
103+
MyController<MvcController>
104+
.Instance()
105+
.Calling(c => c.SignOutWithAuthenticationProperties())
106+
.ShouldReturn()
107+
.SignOut(signOut => signOut
108+
.WithAuthenticationProperties(TestObjectFactory.GetAuthenticationProperties()));
109+
}
110+
111+
[Fact]
112+
public void ShouldReturnSignOutShouldThrowExceptionWithInvalidAuthenticationProperties()
113+
{
114+
Test.AssertException<SignOutResultAssertionException>(
115+
() =>
116+
{
117+
var authenticationProperties = TestObjectFactory.GetAuthenticationProperties();
118+
119+
authenticationProperties.AllowRefresh = false;
120+
121+
MyController<MvcController>
122+
.Instance()
123+
.Calling(c => c.SignOutWithAuthenticationProperties())
124+
.ShouldReturn()
125+
.SignOut(signOut => signOut
126+
.WithAuthenticationProperties(authenticationProperties));
127+
},
128+
$"When calling {nameof(MvcController.SignOutWithAuthenticationProperties)} " +
129+
$"action in MvcController expected sign out result authentication properties to be the same as the provided one, but instead received different result.");
130+
}
131+
132+
[Fact]
133+
public void AndAlsoShouldWorkCorrectly()
134+
{
135+
MyController<MvcController>
136+
.Instance()
137+
.Calling(c => c.SignOutWithAuthenticationSchemes())
138+
.ShouldReturn()
139+
.SignOut(signOut => signOut
140+
.ContainingAuthenticationScheme(AuthenticationScheme.Basic)
141+
.AndAlso()
142+
.ContainingAuthenticationScheme(AuthenticationScheme.NTLM));
143+
}
144+
145+
146+
[Fact]
147+
public void PassingShouldCorrectlyRunItsAssertionFunction()
148+
{
149+
MyController<MvcController>
150+
.Instance()
151+
.Calling(c => c.SignOutWithAuthenticationSchemes())
152+
.ShouldReturn()
153+
.SignOut(signOut => signOut
154+
.Passing(so => so.AuthenticationSchemes?.Count == 2));
155+
}
156+
157+
[Fact]
158+
public void PassingShouldThrowAnExceptionOnAnIncorrectAssertion()
159+
{
160+
Test.AssertException<InvocationResultAssertionException>(
161+
() =>
162+
{
163+
MyController<MvcController>
164+
.Instance()
165+
.Calling(c => c.SignOutWithAuthenticationSchemes())
166+
.ShouldReturn()
167+
.SignOut(signOut => signOut
168+
.Passing(so => so.AuthenticationSchemes?.Count == 0));
169+
},
170+
$"When calling {nameof(MvcController.SignOutWithAuthenticationSchemes)} " +
171+
$"action in {nameof(MvcController)} expected the SignOutResult to pass the given predicate, but it failed.");
172+
}
173+
174+
[Fact]
175+
public void PassingShouldCorrectlyRunItsAssertionAction()
176+
{
177+
MyController<MvcController>
178+
.Instance()
179+
.Calling(c => c.SignOutWithAuthenticationSchemes())
180+
.ShouldReturn()
181+
.SignOut(signOut => signOut
182+
.Passing(so =>
183+
{
184+
const int expectedAuthSchemes = 2;
185+
var actualAuthSchemes = so.AuthenticationSchemes?.Count;
186+
if (actualAuthSchemes != expectedAuthSchemes)
187+
{
188+
throw new InvalidAssertionException(
189+
string.Format("Expected {0} to have {1} {2}, but it has {3}.",
190+
so.GetType().ToFriendlyTypeName(),
191+
expectedAuthSchemes,
192+
nameof(so.AuthenticationSchemes),
193+
actualAuthSchemes));
194+
};
195+
}));
196+
}
197+
}
198+
}

0 commit comments

Comments
 (0)