我正在使用Grails Spring Security Core和Grails Spring Security REST 插件,我才刚刚开始设置。User
我用一个类和一个类(默认值)初始化插件,然后按照我在 Grails 网站上找到Authority
的指南编写集成测试。
它说将以下内容放入集成测试中:
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' }
}
我继续做了类似的事情,它与 bootstrapped 一起工作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
断言通过是因为测试方法中有一个User
inBootstrap.groovy
和一个 create。
为什么这可以工作并通过引导程序User
完全没有任何问题,但不是在方法中创建的问题?有没有办法可以编写此集成测试,以便我可以以这种方式测试插件中/api/login
包含的端点?grails-spring-security-rest