I have a controller method that I'm annotating like so:
@Secured(['ROLE_ADMIN'])
def save() {
... // code ommitted
}
I'm trying to write a unit test to verify that only the admin user can hit the URL:
def "Only the admin user should be able to invoke save"() {
given:
def user = createNonAdminUser() // let's pretend this method exists
controller.springSecurityService = Mock(SpringSecurityService)
controller.springSecurityService.currentUser >> user
when:
controller.save()
then:
view ==~ 'accessdenied'
}
However, the view returned is the save
view and not the access denied view. It looks like it's bypassing the @Secured
annotation altogether. Is there a way to test @Secured
annotations from either a unit test or integration test?