J'ai une application qui utilise la connexion CAS (Central Authentication Service). Dans la configuration (application.yml
), CAS est configuré comme suit:
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 ...
Lors de l'exécution de tests fonctionnels, un serveur Tomcat est lancé avec le numéro de port aléatoire, par exemple, http://localhost:45359
et il change toujours à chaque course.
Il semble que l'application soit redirigée vers le CAS serviceUrl
après la connexion, et normalement mon application fonctionne sur le port 8443, donc ce n'est jamais un problème. Mais comme le test fonctionnel affiche le numéro de port aléatoire du serveur Tomcat, la redirection échoue.
Aussi, je suis en mon application avec run-app -https
(à savoir https://localhost:8443
), mais le test fonctionne sur http
(par exemple http://localhost:12345
). Je ne sais pas si c'est un détail important ou non.
Le test
Mon test fonctionnel est la suivante:
- Accédez à l'URL principale de l'application, qui obtient redirigé vers la page de connexion
- Vérifiez le titre de la page de connexion
- Entre les informations de connexion
- Cliquez sur le bouton Envoyer
- Erreurs avec "Connexion refusée" lors de la tentative de connexion à
localhost:8443
.
Source (basé sur: 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"
}
Stacktrace:
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
Question
Quelle est la bonne façon de le faire pour que mes tests fonctionnels peuvent passer le login CAS?
Autres Informations
- Grails: 3.2.7
- Groovy: 2.4.12
- IntelliJ: 2017.1.5
MISE À JOUR
Je essayé de régler les numéros de port en utilisant un n variable d'environnement, en vain.Voir: this Grails isssue