2017-07-28 42 views
0

我使用的是Grails Spring Security CoreGrails 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斷言通過,因爲有一個UserBootstrap.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端點?

+0

你可以檢查用戶是否已啓用,accountLocked,accountExpired設置爲true? – dynamo

+0

@dynamo不幸的是'enabled = true','accountExpired = false','accountLocked = false','passwordExpired = false',就像引導用戶一樣。 –

回答

0

您在給定部分中創建的用戶在未提交的事務中。當您進行REST調用時,api /登錄控制器將在新的事務中運行,該事務看不到您未提交的用戶。

有幾個選項(還有其他的)...

  1. BootStrap.groovy中創建用戶

    def init = { servletContext -> 
        environments { 
        test { 
         new User(username: "securitySpecTestUserName", password: "securitySpecTestPassword").save(flush: true) 
        } 
        } 
    } 
    
  2. 製作REST調用來創建用戶 - 假設你有這樣的功能

  3. 在設置中創建用戶

    @Integration 
    @Rollback 
    class UserIntSpec extends Specification { 
    
        def setup() { 
        new User(username: "securitySpecTestUserName", password: "securitySpecTestPassword").save(flush: true) 
        } 
    
        void "check get access token"() { 
        given: 
        RestBuilder rest = new RestBuilder() 
    
        when: 
        def response = rest.post("http://localhost:${serverPort}/api/login") { 
         accept('application/json') 
         contentType('application/json') 
         json { 
         username = "securitySpecTestUserName" 
         password = "securitySpecTestPassword" 
         } 
        } 
    
        then: 
        response.status == HttpServletResponse.SC_OK 
    
        when: 
        def token = response.json.access_token 
    
        then: 
        token 
        } 
    } 
    

注意:在Grails> = 3.0中,setup()在單獨的事務中運行並且保持不變(爲什麼它可以解決您的問題),而不是回滾。任何數據都需要手動清理。

我建議你閱讀測試Grails文檔:Integration Testing

+0

謝謝!我已經徹底閱讀了文檔,但是我沒有意識到事務沒有被提交意味着用戶將無法通過Spring Security進行身份驗證。我認爲它會在'.save()'上爲用戶設置,即使它沒有保存到數據庫。現在我已經將代碼移動到了'setup()'方法,現在我可以執行測試了。 –

相關問題