Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Integration testing grails spring security plugin?

I have this:

@Secured(['ROLE_USER', 'ROLE_HELPDESK', 'ROLE_ADMIN'])
class MyController {
    def edit = {
    }

    @Secured(['ROLE_ADMIN'])
    def uploadForUser = {
        params.userId = params.id
        forward(controller: 'someController', action: 'someAction', params: params)
    }
}

and an integration test I think should fail:

public void test_uploadForUser_unauthenticated(){
    myController.params.id = "testUser"
    myController.uploadForUser()
}

And yet the tests pass. Is there any way to test controllers annotated with the spring security plugin?

like image 479
Stefan Kendall Avatar asked Jan 20 '23 00:01

Stefan Kendall


1 Answers

These annotations are analyzed by SpringSecurityFilter, so they don't work if you don't have an actual HTTP request performed.

Thus, you need either to switch to checking the roles by conditionals inside the actions, like being done here, or test it with WebDriver/Geb or some simpler framework -- very nice approach is presented in Grails Security Plugin itself.

like image 81
Artur Nowak Avatar answered Jan 21 '23 14:01

Artur Nowak