We would like to Unit test authentication for portions of our MVC5 web-app but it's not clear how we can test this for controllers/controller methods decorated by the [Authorize] attribute. Instantiating the controller and invoking that method seems to ignore that [Authorize] attribute.
Similarly, some controller methods (eg: AccountController's SignInAsync() method) end up referencing HttpContext and the Authentication property within it's object hierarchy (snippet below, standard MVC5 template code). This makes it hard to unit test properly.
So does anyone have some ideas or approaches on how to effectively test the authentication aspect of the MVC5 app? Seems like another layer is needed to simulate the HTTP req/resp (even if those req/resp don't traverse the network stack) ...
1: private IAuthenticationManager AuthenticationManager
2: {
3: get { HttpContext.GetOwinContext().Authentication; }
4: }
5:
6: private async Task SignInAsync(ApplicationUser user, bool isPersistent)
7: {
8: AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
9: var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
10: AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, identity);
11: }