Created
March 17, 2014 11:51
-
-
Save rarous/9597953 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Security; | |
| using System.Web.Mvc; | |
| using NSubstitute; | |
| using Xunit; | |
| namespace MultiConnector.Web | |
| { | |
| using Auth; | |
| using Modules.Authentication; | |
| public class AuthenticationTests : IDisposable | |
| { | |
| readonly IAuthProvider auth; | |
| readonly IFormsAuthentication formsAuthentication; | |
| readonly AuthenticationController controller; | |
| public AuthenticationTests() | |
| { | |
| auth = Substitute.For<IAuthProvider>(); | |
| formsAuthentication = Substitute.For<IFormsAuthentication>(); | |
| var principalFactory = Substitute.For<IPrincipalFactory>(); | |
| var authStrategy = new MultiConnectorAuthenticationStrategy(auth, null, principalFactory, null); | |
| controller = new AuthenticationController(authStrategy, formsAuthentication); | |
| } | |
| public void Dispose() | |
| { | |
| controller.Dispose(); | |
| } | |
| public class WhenSignInPageIsRequested : AuthenticationTests | |
| { | |
| const string ReturnUrl = "test"; | |
| [Fact] | |
| public void ShouldRenderView() | |
| { | |
| Assert.IsType<ViewResult>(controller.SignIn()); | |
| } | |
| [Fact] | |
| public void ShouldSetEmptyViewModel() | |
| { | |
| var result = controller.SignIn() as ViewResult; | |
| Assert.NotNull(result.Model as SignInRequest); | |
| } | |
| [Fact] | |
| public void ShouldRememberReturnUrl() | |
| { | |
| var result = controller.SignIn(ReturnUrl) as ViewResult; | |
| Assert.Equal(ReturnUrl, (result.Model as SignInRequest).ReturnUrl); | |
| } | |
| } | |
| public class WhenSignInRequestIsPosted : AuthenticationTests | |
| { | |
| public class GivenValidCredential : WhenSignInRequestIsPosted | |
| { | |
| const string UserName = "user"; | |
| const string Passsword = "password"; | |
| readonly SignInRequest validCredential; | |
| public GivenValidCredential() | |
| { | |
| validCredential = new SignInRequest | |
| { | |
| Username = UserName, | |
| Password = Passsword, | |
| RememberMe = false, | |
| }; | |
| } | |
| [Fact] | |
| public void ShouldAuthenticateUser() | |
| { | |
| controller.SignIn(validCredential); | |
| auth.Received().Authenticate(UserName, Passsword); | |
| } | |
| [Fact] | |
| public void ShouldSetFormsAuthenticationCookie() | |
| { | |
| controller.SignIn(validCredential); | |
| formsAuthentication.Received(). | |
| SetAuthCookie(UserName, createPersistentCookie: false); | |
| } | |
| [Fact] | |
| public void ShouldRedirectAuthenticatedUserToTheRoot() | |
| { | |
| var result = controller.SignIn(validCredential) as RedirectResult; | |
| Assert.Equal("/", result.Url); | |
| } | |
| } | |
| public class GivenRememeberMe : WhenSignInRequestIsPosted | |
| { | |
| const string UserName = "user"; | |
| readonly SignInRequest validCredential; | |
| public GivenRememeberMe() | |
| { | |
| validCredential = new SignInRequest | |
| { | |
| Username = UserName, | |
| RememberMe = true, | |
| }; | |
| } | |
| [Fact] | |
| public void ShouldSetPermanentCookie() | |
| { | |
| controller.SignIn(validCredential); | |
| formsAuthentication.Received(). | |
| SetAuthCookie(UserName, createPersistentCookie: true); | |
| } | |
| } | |
| public class GivenReturnUrl : WhenSignInRequestIsPosted | |
| { | |
| const string ReturnUrl = "test"; | |
| [Fact] | |
| public void ShouldRedirectAuthenticatedUserToReturnUrl() | |
| { | |
| var request = new SignInRequest | |
| { | |
| ReturnUrl = ReturnUrl | |
| }; | |
| var result = controller.SignIn(request) as RedirectResult; | |
| Assert.Equal(ReturnUrl, result.Url); | |
| } | |
| } | |
| public class GivenInvalidCredential : WhenSignInRequestIsPosted | |
| { | |
| public GivenInvalidCredential() | |
| { | |
| auth.WhenForAnyArgs(x => x.Authenticate(null, null)). | |
| Do(_ => { throw new SecurityException("error"); }); | |
| } | |
| [Fact] | |
| public void ShouldSetValidationError() | |
| { | |
| controller.SignIn(new SignInRequest()); | |
| Assert.Equal("error", controller.ModelState["*"].Errors[0].ErrorMessage); | |
| } | |
| [Fact] | |
| public void ShouldReturnView() | |
| { | |
| Assert.IsType<ViewResult>(controller.SignIn(new SignInRequest())); | |
| } | |
| } | |
| public class GivenInvalidInput : WhenSignInRequestIsPosted | |
| { | |
| public GivenInvalidInput() | |
| { | |
| controller.ModelState.AddModelError("test", string.Empty); | |
| } | |
| [Fact] | |
| public void ShouldReturnView() | |
| { | |
| Assert.IsType<ViewResult>(controller.SignIn(new SignInRequest())); | |
| } | |
| } | |
| } | |
| public class WhenSignOutPageIsRequested : AuthenticationTests | |
| { | |
| [Fact] | |
| public void ShouldInvalidateUserCookie() | |
| { | |
| controller.SignOut(); | |
| formsAuthentication.Received().SignOut(); | |
| } | |
| [Fact] | |
| public void ShouldRedirectToGoodByePage() | |
| { | |
| var result = controller.SignOut() as RedirectToRouteResult; | |
| Assert.Equal("SignOutSuccess", result.RouteValues["action"]); | |
| } | |
| } | |
| public class WhenSignOutSuccessPageIsRequested : AuthenticationTests | |
| { | |
| [Fact] | |
| public void SignOutSuccessShouldRenderView() | |
| { | |
| Assert.IsType<ViewResult>(controller.SignOutSuccess()); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment