gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 21:34:41 25 4
gpt4 key购买 nike

我正在为一个仍然使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com