我使用的是Grails Spring Security Core和Grails Spring Security REST plugin,我剛剛開始設置事情。我使用User
類和Authority
類(默認值)對插件進行初始化,然後繼續編寫集成測試,然後輸入guide I found on the Grails website。在grails集成測試中創建的受保護的用戶是未經授權的,但是自舉的用戶是
它說,把下面的在集成測試:
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響應代碼失敗。該User.count == 2
斷言通過,因爲有一個User
在Bootstrap.groovy
和一個建立在試驗方法
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
沒有任何問題,但沒有在方法中創建一個嗎?有沒有一種方法可以編寫此集成測試,以便我可以用這種方式測試grails-spring-security-rest
插件中包含的/api/login
端點?
你可以檢查用戶是否已啓用,accountLocked,accountExpired設置爲true? – dynamo
@dynamo不幸的是'enabled = true','accountExpired = false','accountLocked = false','passwordExpired = false',就像引導用戶一樣。 –