|
| 1 | +namespace MyTested.AspNetCore.Mvc.Test.BuildersTests.ActionResultsTests.SignInTests |
| 2 | +{ |
| 3 | + using Exceptions; |
| 4 | + using Setups; |
| 5 | + using Setups.Controllers; |
| 6 | + using Utilities; |
| 7 | + using Xunit; |
| 8 | + |
| 9 | + public class SignInTestBuilderTests |
| 10 | + { |
| 11 | + [Fact] |
| 12 | + public void ShouldReturnSignInShouldNotThrowExceptionIfResultIsSignInWithCorrectAuthenticationScheme() |
| 13 | + { |
| 14 | + MyController<MvcController> |
| 15 | + .Instance() |
| 16 | + .Calling(c => c.SignInWithEmptyAuthenticationPropertiesAndScheme()) |
| 17 | + .ShouldReturn() |
| 18 | + .SignIn(signIn => signIn |
| 19 | + .WithAuthenticationScheme(AuthenticationScheme.Basic)); |
| 20 | + } |
| 21 | + |
| 22 | + [Fact] |
| 23 | + public void ShouldReturnSignInShouldThrowExceptionIfResultIsSignInWithIncorrectAuthenticationScheme() |
| 24 | + { |
| 25 | + Test.AssertException<SignInResultAssertionException>( |
| 26 | + () => |
| 27 | + { |
| 28 | + MyController<MvcController> |
| 29 | + .Instance() |
| 30 | + .Calling(c => c.SignInWithEmptyAuthenticationPropertiesAndScheme()) |
| 31 | + .ShouldReturn() |
| 32 | + .SignIn(signIn => signIn |
| 33 | + .WithAuthenticationScheme(AuthenticationScheme.Digest)); |
| 34 | + }, |
| 35 | + $"When calling {nameof(MvcController.SignInWithEmptyAuthenticationPropertiesAndScheme)} " + |
| 36 | + $"action in MvcController expected sign in result authentication scheme to be 'Digest', but instead received 'Basic'."); |
| 37 | + } |
| 38 | + |
| 39 | + [Fact] |
| 40 | + public void ShouldReturnSignInShouldNotThrowExceptionIfResultIsSignInWithCorrectAuthenticationProperties() |
| 41 | + { |
| 42 | + MyController<MvcController> |
| 43 | + .Instance() |
| 44 | + .Calling(c => c.SignInWithAuthenticationPropertiesAndScheme()) |
| 45 | + .ShouldReturn() |
| 46 | + .SignIn(signIn => signIn |
| 47 | + .WithAuthenticationProperties(TestObjectFactory.GetAuthenticationProperties())); |
| 48 | + } |
| 49 | + |
| 50 | + [Fact] |
| 51 | + public void ShouldReturnSignInShouldThrowExceptionWithInvalidAuthenticationProperties() |
| 52 | + { |
| 53 | + Test.AssertException<SignInResultAssertionException>( |
| 54 | + () => |
| 55 | + { |
| 56 | + var authenticationProperties = TestObjectFactory.GetAuthenticationProperties(); |
| 57 | + |
| 58 | + authenticationProperties.AllowRefresh = false; |
| 59 | + |
| 60 | + MyController<MvcController> |
| 61 | + .Instance() |
| 62 | + .Calling(c => c.SignInWithAuthenticationPropertiesAndScheme()) |
| 63 | + .ShouldReturn() |
| 64 | + .SignIn(signIn => signIn |
| 65 | + .WithAuthenticationProperties(authenticationProperties)); |
| 66 | + }, |
| 67 | + $"When calling {nameof(MvcController.SignInWithAuthenticationPropertiesAndScheme)} " + |
| 68 | + $"action in MvcController expected sign in result authentication properties to be the same as the provided one, but instead received different result."); |
| 69 | + } |
| 70 | + |
| 71 | + [Fact] |
| 72 | + public void AndAlsoShouldWorkCorrectly() |
| 73 | + { |
| 74 | + MyController<MvcController> |
| 75 | + .Instance() |
| 76 | + .Calling(c => c.SignInWithAuthenticationPropertiesAndScheme()) |
| 77 | + .ShouldReturn() |
| 78 | + .SignIn(signIn => signIn |
| 79 | + .WithAuthenticationProperties(TestObjectFactory.GetAuthenticationProperties()) |
| 80 | + .AndAlso() |
| 81 | + .WithAuthenticationScheme(AuthenticationScheme.Basic)); |
| 82 | + } |
| 83 | + |
| 84 | + |
| 85 | + [Fact] |
| 86 | + public void PassingShouldCorrectlyRunItsAssertionFunction() |
| 87 | + { |
| 88 | + MyController<MvcController> |
| 89 | + .Instance() |
| 90 | + .Calling(c => c.SignInWithAuthenticationPropertiesAndScheme()) |
| 91 | + .ShouldReturn() |
| 92 | + .SignIn(signIn => signIn |
| 93 | + .Passing(si => si.AuthenticationScheme == AuthenticationScheme.Basic)); |
| 94 | + } |
| 95 | + |
| 96 | + [Fact] |
| 97 | + public void PassingShouldThrowAnExceptionOnAnIncorrectAssertion() |
| 98 | + { |
| 99 | + Test.AssertException<InvocationResultAssertionException>( |
| 100 | + () => |
| 101 | + { |
| 102 | + MyController<MvcController> |
| 103 | + .Instance() |
| 104 | + .Calling(c => c.SignInWithAuthenticationPropertiesAndScheme()) |
| 105 | + .ShouldReturn() |
| 106 | + .SignIn(signIn => signIn |
| 107 | + .Passing(si => si.AuthenticationScheme == AuthenticationScheme.Digest)); |
| 108 | + }, |
| 109 | + $"When calling {nameof(MvcController.SignInWithAuthenticationPropertiesAndScheme)} " + |
| 110 | + $"action in {nameof(MvcController)} expected the SignInResult to pass the given predicate, but it failed."); |
| 111 | + } |
| 112 | + |
| 113 | + [Fact] |
| 114 | + public void PassingShouldCorrectlyRunItsAssertionAction() |
| 115 | + { |
| 116 | + MyController<MvcController> |
| 117 | + .Instance() |
| 118 | + .Calling(c => c.SignInWithAuthenticationPropertiesAndScheme()) |
| 119 | + .ShouldReturn() |
| 120 | + .SignIn(signIn => signIn |
| 121 | + .Passing(si => |
| 122 | + { |
| 123 | + var expectedAuthScheme = AuthenticationScheme.Basic; |
| 124 | + var actualAuthScheme = si.AuthenticationScheme; |
| 125 | + if (actualAuthScheme != expectedAuthScheme) |
| 126 | + { |
| 127 | + throw new InvalidAssertionException( |
| 128 | + string.Format("Expected {0} to have {1} equal to {2}, but it has {3}.", |
| 129 | + si.GetType().ToFriendlyTypeName(), |
| 130 | + nameof(si.AuthenticationScheme), |
| 131 | + expectedAuthScheme, |
| 132 | + actualAuthScheme)); |
| 133 | + }; |
| 134 | + })); |
| 135 | + } |
| 136 | + } |
| 137 | +} |
0 commit comments