I have the following code in my login controller and I have to test if the user is logged in. How would I go about writing the test case?
def index() {
if (springSecurityService.isLoggedIn()) {
redirect uri: '/'
}
else {
redirect action: 'auth', params: params
}
}
Something like this:
import grails.plugin.springsecurity.SpringSecurityService
import grails.test.mixin.TestFor
import spock.lang.Specification
@TestFor(YourController)
class YourControllerSpec extends Specification {
def springSecurityService = Mock( SpringSecurityService )
void setup() {
controller.springSecurityService = springSecurityService
}
void "test index"() {
when:
controller.index()
then:
1 * springSecurityService.isLoggedIn() >> true
response.redirectedUrl == '/'
when:
controller.response.reset()
controller.index()
then:
1 * springSecurityService.isLoggedIn() >> false
response.redirectedUrl == '/auth'
}
}