Saml2JavaConfigurationITests.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * Copyright 2021 the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * https://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package example;
  17. import java.util.ArrayList;
  18. import java.util.List;
  19. import java.util.concurrent.TimeUnit;
  20. import org.htmlunit.ElementNotFoundException;
  21. import org.htmlunit.WebClient;
  22. import org.htmlunit.html.HtmlElement;
  23. import org.htmlunit.html.HtmlForm;
  24. import org.htmlunit.html.HtmlInput;
  25. import org.htmlunit.html.HtmlPage;
  26. import org.htmlunit.html.HtmlPasswordInput;
  27. import org.htmlunit.html.HtmlSubmitInput;
  28. import org.junit.jupiter.api.BeforeEach;
  29. import org.junit.jupiter.api.Test;
  30. import org.junit.jupiter.api.extension.ExtendWith;
  31. import org.springframework.beans.factory.annotation.Autowired;
  32. import org.springframework.core.env.Environment;
  33. import org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers;
  34. import org.springframework.test.context.ContextConfiguration;
  35. import org.springframework.test.context.junit.jupiter.SpringExtension;
  36. import org.springframework.test.context.web.WebAppConfiguration;
  37. import org.springframework.test.web.servlet.MockMvc;
  38. import org.springframework.test.web.servlet.htmlunit.MockMvcWebClientBuilder;
  39. import org.springframework.test.web.servlet.setup.MockMvcBuilders;
  40. import org.springframework.web.context.WebApplicationContext;
  41. import static org.assertj.core.api.Assertions.assertThat;
  42. import static org.awaitility.Awaitility.await;
  43. @ExtendWith(SpringExtension.class)
  44. @ContextConfiguration(classes = ApplicationConfiguration.class)
  45. @WebAppConfiguration
  46. public class Saml2JavaConfigurationITests {
  47. private MockMvc mvc;
  48. private WebClient webClient;
  49. @Autowired
  50. WebApplicationContext webApplicationContext;
  51. @Autowired
  52. Environment environment;
  53. @BeforeEach
  54. void setup() {
  55. this.mvc = MockMvcBuilders.webAppContextSetup(this.webApplicationContext)
  56. .apply(SecurityMockMvcConfigurers.springSecurity())
  57. .build();
  58. this.webClient = MockMvcWebClientBuilder.mockMvcSetup(this.mvc)
  59. .withDelegate(new LocalHostWebClient(this.environment))
  60. .build();
  61. this.webClient.getCookieManager().clearCookies();
  62. }
  63. @Test
  64. void authenticationAttemptWhenValidThenShowsUserEmailAddress() throws Exception {
  65. performLogin();
  66. HtmlPage home = (HtmlPage) this.webClient.getCurrentWindow().getEnclosedPage();
  67. assertThat(home.asNormalizedText()).contains("You're email address is testuser2@spring.security.saml");
  68. }
  69. @Test
  70. void logoutWhenRelyingPartyInitiatedLogoutThenLoginPageWithLogoutParam() throws Exception {
  71. performLogin();
  72. HtmlPage home = (HtmlPage) this.webClient.getCurrentWindow().getEnclosedPage();
  73. HtmlElement rpLogoutButton = home.getHtmlElementById("rp_logout_button");
  74. HtmlPage loginPage = rpLogoutButton.click();
  75. this.webClient.waitForBackgroundJavaScript(10000);
  76. List<String> urls = new ArrayList<>();
  77. urls.add(loginPage.getUrl().getFile());
  78. urls.add(((HtmlPage) this.webClient.getCurrentWindow().getEnclosedPage()).getUrl().getFile());
  79. assertThat(urls).withFailMessage(() -> {
  80. // @formatter:off
  81. String builder = loginPage.asXml()
  82. + "\n\n\n"
  83. + "Enclosing Page"
  84. + "\n\n\n"
  85. + ((HtmlPage) this.webClient.getCurrentWindow().getEnclosedPage()).asXml();
  86. // @formatter:on
  87. return builder;
  88. }).contains("/login?logout");
  89. }
  90. private void performLogin() throws Exception {
  91. HtmlPage login = this.webClient.getPage("/");
  92. this.webClient.waitForBackgroundJavaScript(10000);
  93. HtmlForm form = findForm(login);
  94. HtmlInput username = form.getInputByName("username");
  95. HtmlPasswordInput password = form.getInputByName("password");
  96. HtmlSubmitInput submit = login.getHtmlElementById("okta-signin-submit");
  97. username.type("testuser2@spring.security.saml");
  98. password.type("12345678");
  99. submit.click();
  100. this.webClient.waitForBackgroundJavaScript(10000);
  101. }
  102. private HtmlForm findForm(HtmlPage login) {
  103. await().atMost(10, TimeUnit.SECONDS)
  104. .until(() -> login.getForms().stream().map(HtmlForm::getId).anyMatch("form19"::equals));
  105. for (HtmlForm form : login.getForms()) {
  106. try {
  107. if (form.getId().equals("form19")) {
  108. return form;
  109. }
  110. }
  111. catch (ElementNotFoundException ex) {
  112. // Continue
  113. }
  114. }
  115. throw new IllegalStateException("Could not resolve login form");
  116. }
  117. }