Grails Spring Security get assigned roles to a method -
i have test goes through methods available in controller , retrieves roles associated these methods. understand should functional test (as opposed unit test), still not know how request list of roles associated method. let's have controller:
@secured("hasanyrole('role_1')" class mycontroller { def methoda() {} @secured("hasanyrole('role_2')" def methodb() {} }
in test have this:
assertequals(['role_1'],getroles(mycontroller.class, "methoda")) assertequals(['role_1', 'role_2'],getroles(mycontroller.class, "methodb"))
any suggestions? thanks.
you can reflection api. like:
method m = mycontroller.class.getmethod("methodb"); annotation[] annos = m.getannotations();
don't think that's validation method, since ensure write role name correctly. think it's better try call action , check if process redirect denied.
@testfor(mycontroller) class mycontrollertests { @test void shouldredirecttodenied() { springsecurityutils.dowithauth('username') { controller.methodb() assert controller.response.redirectedurl == '/login/denied' } } }
the dowithauth
closure mock authentication username, it's same say: "do code if username logged in successfully".
it seems need use functional tests indeed. see burt's comment. i'm still think that's not valid effort create test validate if method have annotation.
Comments
Post a Comment