unit-testing - Grails如何对异常执行单元测试

标签 unit-testing grails spring-security

我正在尝试测试帐户过期异常。

def authfail() {
    String msg = ''
    def exception = session[WebAttributes.AUTHENTICATION_EXCEPTION]

//        println("print exception: ${exception} | ${session} | ${springSecurityService.getCurrentUser()}")
    if (exception) {
        if (exception instanceof AccountExpiredException) {
            msg = message(code: 'springSecurity.errors.login.expired')
        }
        else if (exception instanceof CredentialsExpiredException) {
            msg = message(code: 'springSecurity.errors.login.passwordExpired')
        }
        else if (exception instanceof DisabledException) {
            msg = message(code: 'springSecurity.errors.login.disabled')
        }
        else {
            msg = message(code: 'springSecurity.errors.login.fail')
        }
    }

    if (springSecurityService.isAjax(request)) {
        render([error: msg] as JSON)
    }
    else {
        flash.message = msg
        redirect action: 'auth', params: params
    }
}

我试图在意识到自己被卡住之前编写了上面的测试用例,因为我不知道如何触发过期的登录,以便满足引发AccountExceptionExpired异常的单元测试条件。
void "test authFail"() {

when:
    session."${WebAttributes.AUTHENTICATION_EXCEPTION}" = new AccountExpiredException( 'This account has expired' )
    def logexp = controller.authfail()
then:
    logexp == 'springSecurity.errors.login.expired'
when:
    session."${WebAttributes.AUTHENTICATION_EXCEPTION}" = new CredentialsExpiredException( 'This credentials have expired' )
    def passexp = controller.authfail()
then:
    passexp == 'springSecurity.errors.login.passwordExpired'
when:
    session."${WebAttributes.AUTHENTICATION_EXCEPTION}" = new DisabledException( 'The account is disabled' )
    def logdis = controller.authfail()
then:
    logdis == 'springSecurity.errors.login.disabled'
when:
    session."${WebAttributes.AUTHENTICATION_EXCEPTION}" = new UnsupportedOperationException( 'Sorry, we were not able to find a user with that username and password.' )
    def logfail = controller.authfail()
then:
    logfail == 'springSecurity.errors.login.fail'
when:
    controller.authfail()
then:
    1 * springSecurityService.isAjax( _ ) >> true
    controller.response.json == [error :'springSecurity.errors.login.fail']

    }
}

最佳答案

以下将测试您的大多数方法:

import grails.plugin.springsecurity.SpringSecurityService
import grails.test.mixin.TestFor
import org.springframework.security.authentication.AccountExpiredException
import org.springframework.security.authentication.CredentialsExpiredException
import org.springframework.security.authentication.DisabledException
import org.springframework.security.web.WebAttributes
import spock.lang.Specification

@TestFor(YourController)
class YourControllerSpec extends Specification {

def springSecurityService = Mock( SpringSecurityService )

void setup() {
    controller.springSecurityService = springSecurityService
}

void "test authFail"() {
    given:
        session."${WebAttributes.AUTHENTICATION_EXCEPTION}" = new AccountExpiredException( 'This account has expired' )
    when:
        controller.authfail()
    then:
        flash.message == 'springSecurity.errors.login.expired'
    when:
        session."${WebAttributes.AUTHENTICATION_EXCEPTION}" = new CredentialsExpiredException( 'This credentials have expired' )
        controller.authfail()
    then:
        flash.message == 'springSecurity.errors.login.passwordExpired'
    when:
        session."${WebAttributes.AUTHENTICATION_EXCEPTION}" = new DisabledException( 'The account is disabled' )
        controller.authfail()
    then:
        flash.message == 'springSecurity.errors.login.disabled'
    when:
        session."${WebAttributes.AUTHENTICATION_EXCEPTION}" = new UnsupportedOperationException( 'Bad stuff' )
        controller.authfail()
    then:
        flash.message == 'springSecurity.errors.login.fail'
    when:
        controller.authfail()
    then:
        1 * springSecurityService.isAjax( _ ) >> true
        response.json == [error :'springSecurity.errors.login.fail']
}
}

session 只是一个映射,我们在其中添加了字符串常量的键和异常的值。
对于所有测试,除最后一个测试外,我们都进入最后一个else块,在最终测试中,我们为“isAjax”返回true。

关于unit-testing - Grails如何对异常执行单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54086557/

相关文章:

visual-studio-2010 - 调试单元测试时,Visual Studio不会调试吗?

ios - Xcode 测试单独通过,与其他测试一起运行时失败

c# - 这个 Ambient Context 怎么会变成 null 呢?

grails - 在Grails函数中按值调用?

c# - 在观察者设计模式单元测试中我们需要测试什么?

mysql - 在 grails3 中使用 mongodb 和 hibernate

json - Grails:具有命名间隔约定的JSON View ?

java - Spring 4.1.5.RELEASE 中的 ResourceUrlEncodingFilter 启用了 springSecurityFilterChain

java - 使用 Spring Security 的多个用户

spring - 如何在 Spring OAuth2 上更改 response_type