2017-07-11 34 views
0

問題如何在CAS登錄後對重定向的Grails 3應用程序進行功能測試?

我有一個使用CAS(中央認證服務)登錄的應用程序。在上述結構(application.yml),CAS被配置如下:

grails: 
    plugin: 
    springsecurity: 
     # ...other configurations omitted ... 
     cas: 
     serverUrlPrefix: "https://<some endpoint for the actual CAS>" 
     serviceUrl: https://localhost:8443/myapp/login/cas 
     # ...other configurations omitted ... 

當運行功能測試,Tomcat服務器與隨機的端口號發起的,例如http://localhost:45359並且每次運行都會隨時更改。

似乎應用程序在登錄後重定向到CAS serviceUrl,通常我的應用程序在端口8443上運行,所以它從來都不是問題。但是,由於功能測試在隨機端口號下啓動Tomcat服務器,重定向失敗。

此外,我使用run-app -https(即https://localhost:8443)運行我的應用程序,但測試在http(例如http://localhost:12345)上運行。我不知道這是否是一個重要的細節。

測試

我的功能測試去如下:

  1. 導航到應用程序的主網址,這被重定向到登錄頁面
  2. 檢查標題爲
  3. 登錄頁面
  4. 輸入登錄憑證
  5. 點擊提交按鈕
  6. 當連接到localhost:8443時出現「連接被拒絕」錯誤。

源(基於:https://github.com/grails-guides/grails-test-security/blob/master/complete/src/integration-test/groovy/grails/test/security/AnnouncementControllerSpec.groovy):

@Integration 
class MyAppControllerFunctionalSpec extends GebSpec { 

    def setup(){ 
    } 

    def cleanup() { 
    } 

    void "test something"() { 
     when:"The home page is visited" 
      go '/myapp' // 1 

     then:"The title is correct" 
      title == "Login Page" // 2 
      at LoginPage // a helper class - subclass of geb.Page. 
         // Contains implementation of login method 

     when: "Sign in" 
      login('username', 'password') // 3 & 4 --> 5 

     then: "Title" 
      title == "Welcome MyApp" 
} 

堆棧跟蹤:

java.lang.RuntimeException: org.apache.http.conn.HttpHostConnectException: Connect to localhost:8443 [localhost/127.0.0.1] failed: Connection refused (Connection refused) 

    at com.gargoylesoftware.htmlunit.WebClient.download(WebClient.java:2067) 
    at com.gargoylesoftware.htmlunit.html.HtmlForm.submit(HtmlForm.java:138) 
    at com.gargoylesoftware.htmlunit.html.HtmlButton.doClickStateUpdate(HtmlButton.java:97) 
    at com.gargoylesoftware.htmlunit.html.DomElement.click(DomElement.java:786) 
    at com.gargoylesoftware.htmlunit.html.DomElement.click(DomElement.java:733) 
    at org.openqa.selenium.htmlunit.HtmlUnitMouse.click(HtmlUnitMouse.java:74) 
    at org.openqa.selenium.htmlunit.HtmlUnitWebElement.click(HtmlUnitWebElement.java:151) 
    at geb.navigator.NonEmptyNavigator.click(NonEmptyNavigator.groovy:437) 
    at geb.content.TemplateDerivedPageContent.click(TemplateDerivedPageContent.groovy:115) 
    at myapp.LoginPage.login(LoginPage.groovy:22) 
    at geb.Browser.methodMissing(Browser.groovy:207) 
    at geb.spock.GebSpec.methodMissing(GebSpec.groovy:56) 
    at myapp.MyAppControllerFunctionalSpec.test something(MyAppControllerFunctionalSpec.groovy:39) 
Caused by: org.apache.http.conn.HttpHostConnectException: Connect to localhost:8443 [localhost/127.0.0.1] failed: Connection refused (Connection refused) 
    at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:158) 
    at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:353) 
    at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:380) 
    at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:236) 
    at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184) 
    at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:88) 
    at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110) 
    at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184) 
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:71) 
    at com.gargoylesoftware.htmlunit.HttpWebConnection.getResponse(HttpWebConnection.java:179) 
    at com.gargoylesoftware.htmlunit.WebClient.loadWebResponseFromWebConnection(WebClient.java:1321) 
    at com.gargoylesoftware.htmlunit.WebClient.loadWebResponseFromWebConnection(WebClient.java:1379) 
    at com.gargoylesoftware.htmlunit.WebClient.loadWebResponse(WebClient.java:1238) 
    at com.gargoylesoftware.htmlunit.WebClient.download(WebClient.java:2063) 
    ... 12 more 
Caused by: java.net.ConnectException: Connection refused (Connection refused) 
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) 
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) 
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) 
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) 
    at java.net.Socket.connect(Socket.java:589) 
    at org.apache.http.conn.ssl.SSLConnectionSocketFactory.connectSocket(SSLConnectionSocketFactory.java:337) 
    at com.gargoylesoftware.htmlunit.httpclient.HtmlUnitSSLConnectionSocketFactory.connectSocket(HtmlUnitSSLConnectionSocketFactory.java:189) 
    at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:141) 
    ... 25 more 

問題

什麼是正確的方法,使之所以我的功能測試能通過CAS登錄?

其他信息

  • 的Grails:3.2.7
  • Groovy:在2.4.12
  • 的IntelliJ:2017年1月5日

UPDATE

我有嘗試使用a設置端口號n環境變量,無濟於事。請參閱:this Grails isssue

回答

0

我目前的解決方案,使功能測試與CAS登錄從IntelliJ IDEA的運行:

  1. 確保測試類是src/integration-test/groovy/<package>/目錄下使用
  2. 運行/啓動應用程序運行命令或控制按鈕。不要從IDE終端運行應用程序。
  3. 運行從IDE的終端的測試:

    grails test-app package.test_class --integration

例如,對於包:foo和測試類MyAppControllerFunctionalSpec

grails test-app foo.MyAppControllerFunctionalSpec --integration 
相關問題