unit-testing - 在 grails + acegi 中测试 securityConfig 映射

标签 unit-testing testing grails integration-testing spring-security

我正在为一个仍然使用 Acegi 插件(而不是较新的 Spring Core Security 插件)的项目编写测试用例,截至目前,我已经成功完成了本网站的工作(http://www.zorched.net/2008/09/01/grails-testing-acegi-security/)

建议检测当前登录的用户。但是,在我的 Controller 中,我有如下内容:

def list = {
     // code for an "admin account"
}

def list_others = {
     // code for other accounts
}

现在,我在 Controller 中检查登录用户。相反,我在 SecurityConfig.groovy 中定义了这些,例如:

security {
     ...
     requestMapString = """\
          /someController/list=ROLE_ADMIN
          /someController/list_others=ROLE_OTHERS
     """
     ...
}

因此,如果我有一个看起来像这样的测试:

void testTrial() {
     // define here that otherUser has a role of ROLE_OTHERS

     authenticate(otherUser, "other") // this calls the authenticate methode in the site I gave earlier
     controller.list()

     // I do an assertion here to check where this goes to
}

事情是,当我做断言时,当然列表会告诉我它转发到 list.gsp... 即使“登录”用户具有 ROLE_OTHERS 角色而不是管理员。

但是,我需要的是如何测试登录用户只能访问什么?在这种情况下,假设调用是对 *.list() 的调用并且登录用户具有 ROLE_OTHERS 角色,我应该被转发到“拒绝”页面。

帮助?谢谢!

最佳答案

您需要为此进行功能测试。单元测试只是 Groovy 或 Java 类加上一些模拟。没有 Spring 应用程序上下文、没有 Hibernate、没有数据库,对于这种情况最重要的是没有插件。

集成测试为您提供了更多功能,但即使有请求也大多是模拟的,并且由于没有容器,因此不会触发过滤器。 Spring Security(Acegi 插件使用)基于一系列 servlet 过滤器。因此,如果您想测试您的安全规则是否得到正确应用,您将需要一个正在运行的服务器,因此需要进行功能测试。

功能测试有多种选择,最流行的是 WebTest:http://grails.org/plugin/webtest ,功能测试插件:http://grails.org/plugin/functional-test ,以及 Selenium RC 插件:http://grails.org/plugin/selenium-rc .

最新的是 Geb:http://grails.org/plugin/geb .手册位于 http://geb.codehaus.org/最近有一篇关于它的博客文章:http://blog.springsource.com/2010/08/28/the-future-of-functional-web-testing/

关于unit-testing - 在 grails + acegi 中测试 securityConfig 映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3836473/

相关文章:

c++ - 在 C++ 中自动/模板化生成测试 vector

javascript - Test Cafe - 单击使用客户端功能不可见的链接时出错

c# - 单元测试 session 窗口在调试时关闭

Android Espresso 测试失败并出现 IncompleteClassChangeError

javascript - 如何使用 Jack 在 jQuery 中模拟 .each 和 .find 方法?

hibernate - 获取具有特定权限的人员-带有Grails的 Spring 安全

javascript - Grails - 在脚本方法之后调用正文加载

ios - 是否有一个集中维护的手机/平板电脑和操作系统组合列表,可以指导合理的设备列表进行测试

angular - 如何测试包含自定义表单控件的组件?

validation - 升级后 Grails 验证不起作用