How do I test a controller to ensure that the authorize filter is applied to it with the correct role?
-
10-06-2021 - |
Question
I have a controller in the admin section of my site and it is decorated with the Authorize filter with the role set to admin.
[Authorize(Roles = "Admin")]
public class SubscriberController : Controller
This works great but I'd like to create a unit test to ensure that the filter is not removed. I've got this far, to validate that the Authorize filter is present.
typeof(SubscriberController).Should()
.BeDecoratedWith<AuthorizeAttribute>(
"Subscriber controller users must be admins");
How can I validate the Roles argument? I am using Fluent Assertion 1.7.1.1.
It is possible now in Fluent Assertion v2:
typeof(SubscriberController).Should()
.BeDecoratedWith<AuthorizeAttribute>(a => a.Roles.Contains("Admin"),
"Subscriber controller users must be admins");
Solution
Looks like that isn't possible now - Testing Arguments of Attributes
You can up this thread and ask about progress.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow