Specification Test for the Authorized Attribute in MVC

I wanted to Unit Test (in BDD I’d call it a specification) that the controller had the authorize attribute so I found this approach in a GoodSearch a while back and forgotten who to give credit for it, but I thought I’d post it, so I won’t have to search for it next time. I put this in a base class and it’s been very useful.

EDIT: March 21st, 2013 I added a way to also verify the correct roles are in the attribute. This is especially nice, sine the attribute takes strings.

[Authorize(Roles = "Super Admin, User Admin")] public void MyController2{}

[Authorize] public void MyController{}

///

It should require authorization for Controller or ApiController. /// The controller. /// The Authorize Attribute from the controller . protected AuthorizeAttribute It_Should_Require_Authorization(object controller) { var type = controller.GetType(); var attributes = type.GetCustomAttributes(typeof(AuthorizeAttribute), true); Assert.IsTrue(attributes.Any(), "No AuthorizeAttribute found"); return attributes.Any() ? attributes[0] as AuthorizeAttribute : null; }

///

It should require authorization for Controller or ApiController. /// The controller. /// The roles. protected void It_Should_Require_Authorization(object controller, string[] roles) { var authorizeAttribute = this.It_Should_Require_Authorization(controller); if (!roles.Any()) { return; }

if (authorizeAttribute == null)
{
    return;
}

bool all = authorizeAttribute.Roles.Split(',').All(r => roles.Contains(r.Trim()));
Assert.IsTrue(all);

}

And to call it:

[TestMethod] [TestCategory("MyController")] public void It_Should_Require_Authorization() { // where this.Controller is the controller you are testing

this.It_Should_Require_Authorization(this.Controller); }

[TestMethod] [TestCategory("MyController2")] public void It_Should_Require_Authorization() { var roles = new[] { “Super Admin”, “User Admin” }; this.It_Should_Require_Authorization(this.Controller, roles); }

This article is part of the GWB Archives. Original Author: Aligned

New on Geeks with Blogs

  • We Won The One Award I Actually Care About

    Full Scale made the Inc. 5000 for the fifth year straight, the 12th listing across my three companies. Here is why the one award you cannot buy is worth stopping for.

  • Your Customers Build the Features Now

    I let a tool I liked sit dead for a year rather than build the features I wanted. An MCP server meant I never had to, and your customers can do the same to your product.

  • Get the Size of a Directory in Linux the Easy Way

    du -sh for the quick answer, ncdu for the cleanup, df for the disk itself: every command for checking directory size in Linux, plus why du and df never agree.

  • Vim Search and Replace: The Ultimate Guide

    One :%s command replaces every match in a file before a find dialog would even open. The Vim substitute patterns worth the muscle memory: flags, ranges, capture groups, and multi-file edits.