【发布时间】:2011-04-19 16:27:00
【问题描述】:
我正在为一个仍然使用 Acegi 插件(不是较新的 Spring Core Security 插件)的项目编写测试用例,到目前为止,我已经成功地完成了这个站点 (http://www.zorched.net/2008/09/01/grails-testing-acegi-security/)
已建议检测当前登录的用户。但是,在我的控制器中,我的内容如下所示:
def list = {
// code for an "admin account"
}
def list_others = {
// code for other accounts
}
现在,我不将登录用户签入控制器。相反,我在 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 会告诉我它转发到 list.gsp ......即使“登录”用户的角色是 ROLE_OTHERS 而不是管理员。
但是,我需要的是如何测试登录用户只能访问的内容?在这种情况下,假设调用的是 *.list() 并且登录用户具有 ROLE_OTHERS 角色,我应该被转发到“拒绝”页面。
帮助?谢谢!
【问题讨论】:
标签: unit-testing testing grails integration-testing spring-security