Created
February 9, 2012 16:01
-
-
Save caspian311/1780807 to your computer and use it in GitHub Desktop.
nice little unit tests for ensuring the authorization filters and order of those filters in your .NET MVC web app
This file contains 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.Linq; | |
using System.Reflection; | |
using System.Web.Mvc; | |
using NUnit.Framework; | |
using WebApplication.Filters; | |
namespace WebApplicationTests.Filters | |
{ | |
[TestFixture] | |
public class FilterOrderTests | |
{ | |
[Test] | |
public void VerifyOrderPropertySetCorrectlyOnAllFilterAttributes() | |
{ | |
var types = Assembly.Load(new AssemblyName("WebApplication")).GetTypes().Where(typeof(AuthorizeAttribute).IsAssignableFrom); | |
var allFilters = types.Select(type => Activator.CreateInstance(type) as AuthorizeAttribute).ToList(); | |
Assert.AreEqual(4, allFilters.Count); | |
var sortedList = allFilters.OrderBy(filter => filter.Order).ToList(); | |
VerifyType(typeof(AuthenticateUserData), sortedList[0]); | |
VerifyType(typeof(AuthorizeUserDataRole), sortedList[1]); | |
VerifyType(typeof(ProfileCheck), sortedList[2]); | |
VerifyType(typeof(UserOwnsProject), sortedList[3]); | |
} | |
private static void VerifyType(Type type, AuthorizeAttribute element) | |
{ | |
Assert.IsTrue(element.GetType() == type, "Expected " + type.Name + " but was " + element.GetType().Name); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment