Question

Background : We are using MVC4 and using WIF for Claims/Authorization. We are using Moq/MvcContrib for Mockup Objects. I have looked here and created the MockIdentity and MockPrincipal Objects - do I need them?

Goal : I have a controller class that has a class level attribute that only allows users with 'Manager' claim to access the actions. I want to create mock users and test to see if anyone that doesn't have 'Manager' claim can access the actions or not.

I get the mock concept but I have only dealt with the data objects mocking and having a tough time figuring out what plugins/classes/methods/setups I need in place to do what I need to do.

Thanks in advance.

Was it helpful?

Solution

I want to create mock users and test to see if anyone that doesn't have 'Manager' claim can access the actions or not.

No, you don't. You just want to pass users to that attribute you wrote and test that sets the filterContext.Result correctly. That's it. You don't need to test that System.Web.Mvc works. Single unit under test!

Presumably your attribute is an AuthorizeAttribute, correct? So you need to test OnAuthorization(AuthorizationContext).

Disclaimer: I haven't used moq in a while, but your code would presumably look generally like this:

var user = new Mock<IPrincipal>();
user.Setup(/* whatever you need to look at */);

var authContext = new Mock<AuthorizationContext>();
authContext.Setup(ac => ac.HttpContext.User).Returns(user);

var myAttribute = new RequireManagerAttribute();
myAttribute.OnAuthorization(authContext);

authContext.VerifySet(ac => ac.Result = /* whatever you expect */);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top