【发布时间】:2018-01-05 01:37:50
【问题描述】:
我正在使用Grails Spring Security Core 和Grails Spring Security REST plugin,我刚刚开始设置。我用User 类和Authority 类(默认值)初始化插件,然后在guide I found on the Grails website 之后编写集成测试。
据说将以下内容放入集成测试中:
def "test a user with the role ROLE_BOSS is able to access /api/announcements url"() {
when: 'login with the sherlock'
RestBuilder rest = new RestBuilder()
def resp = rest.post("http://localhost:${serverPort}/api/login") {
accept('application/json')
contentType('application/json')
json {
username = 'sherlock'
password = 'elementary'
}
}
then:
resp.status == 200
resp.json.roles.find { it == 'ROLE_BOSS' }
}
我继续做了类似的事情,它与自举的User 一起工作,但是当我尝试使用在测试方法本身中创建的User 进行完全相同的测试时,它会因401 而失败HTTP 响应代码。
我正在尝试运行的代码:
void "check get access token"() {
given:
RestBuilder rest = new RestBuilder()
new User(username: "securitySpecTestUserName", password: "securitySpecTestPassword").save(flush: true)
assert User.count == 2
when:
def resp = rest.post("http://localhost:${serverPort}/api/login") {
accept('application/json')
contentType('application/json')
json {
username = "securitySpecTestUserName"
password = "securitySpecTestPassword"
}
}
then:
resp.status == 200
}
请注意,User.count == 2 断言通过了,因为Bootstrap.groovy 中有一个User,而测试方法中有一个 create。
为什么这可以正常工作并与引导的User 一起通过而没有任何问题,但不是在方法中创建的问题?有没有办法可以编写这个集成测试,以便我可以通过这种方式测试 grails-spring-security-rest 插件中包含的 /api/login 端点?
【问题讨论】:
-
能否检查一下用户是否已启用、accountLocked、accountExpired 是否设置为 true?
-
@dynamo 不幸的是
enabled = true、accountExpired = false、accountLocked = false、passwordExpired = false,就像引导用户一样。
标签: spring grails spring-security grails-rest-api