Skip to content

Instantly share code, notes, and snippets.

@caspian311
Created February 9, 2012 16:01
Show Gist options
  • Save caspian311/1780807 to your computer and use it in GitHub Desktop.
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
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