如何测试由Shiro Grails插件生成的AuthController?

时间:2014-06-02 10:23:26

标签: grails plugins shiro

我对Grails的Shiro插件感到非常沮丧。首先,我正在尝试建立一个基于ini的SecurityManager,就像Shiro建议的初学者教程一样,文档中没有任何内容告诉我如何配置。因此,在努力弄清楚并最终失败之后,我只是承认我必须使用使用quickstart命令生成的DbRealm。现在,我试图通过为生成的AuthController创建单元测试来发现事情是如何工作的。这是我的测试代码:

void "registered user logs in with valid credentials"() {
    given: "user 'root' is registered"
    def username = "root"
    def password = "secret"
    new User(username: username, password: password).save();

    when: "user logs in with correct credentials"
    params.username = username
    params.password = password
    controller.signIn();

    then: "user should be authenticated"
    assertTrue SecurityUtils.subject.authenticated
}

我仍然不确定如何验证用户是否实际登录,因此我认为then块中的代码现在应该没问题。但是,每次我尝试使用grails test-app测试应用时,我总是得到:

org.apache.shiro.UnavailableSecurityManagerException: No SecurityManager accessible to the calling code, either bound to the org.apache.shiro.util.ThreadContext or as a vm static singleton.  This is an invalid application configuration.
    at org.apache.shiro.SecurityUtils.getSecurityManager(SecurityUtils.java:123)

我并不是Grails的新手,但是自从我上次使用它以来已经很长时间了,无论如何我从来没有那么多接触过这个框架。但是,我知道它应该让设置起来不那么复杂,但是Shiro插件让我很难过。

那么,我该如何配置我的应用和/或测试生成的AuthController

0 个答案:

没有答案