Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to testing Authorize Attributes with Roles in .net MVC 3?

I'm have that Controller:

[Authorize(Roles = "Administrator")]
public class ApuradorController : Controller
{
    private readonly Questiona2011Context _context;
    private readonly AuthenticationService _authenticationService;

    public ApuradorController(Questiona2011Context context, AuthenticationService authenticationService)
    {
        this._context = context;
        this._authenticationService = authenticationService;
    }

    ...
}

I'm using RoleProvider. How I can testing Roles and Authorization in my Controller?

like image 243
Acaz Souza Avatar asked Feb 23 '23 09:02

Acaz Souza


2 Answers

Filters (for example, your authorizeattribute) are designed to be independent of the controllers and action methods. when you are writing unit tests for you action methods, filters are simply ignored, they are just attributes. But, you do not have to test them when you are unit testing your controllers and actions! If you want to test how your app works alltogether, you must write ui automation tests.

Update: Steven Sanderson have great explanation of that in his book pro asp.net mvc 2 framework. In fact, I repeated his words above

like image 200
objectbox Avatar answered Mar 08 '23 02:03

objectbox


I believe this is for integration test again. Watin ( http://watin.org/ ) is a framework for Web integration test.

like image 31
Tae-Sung Shin Avatar answered Mar 08 '23 00:03

Tae-Sung Shin