Saml2LoginApplicationITests.java 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * Copyright 2002-2022 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 com.gargoylesoftware.htmlunit.ElementNotFoundException;
  18. import com.gargoylesoftware.htmlunit.WebClient;
  19. import com.gargoylesoftware.htmlunit.html.HtmlForm;
  20. import com.gargoylesoftware.htmlunit.html.HtmlInput;
  21. import com.gargoylesoftware.htmlunit.html.HtmlPage;
  22. import com.gargoylesoftware.htmlunit.html.HtmlPasswordInput;
  23. import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
  24. import org.junit.jupiter.api.BeforeEach;
  25. import org.junit.jupiter.api.Disabled;
  26. import org.junit.jupiter.api.Test;
  27. import org.springframework.beans.factory.annotation.Autowired;
  28. import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
  29. import org.springframework.boot.test.context.SpringBootTest;
  30. import org.springframework.test.web.servlet.MockMvc;
  31. import static org.assertj.core.api.Assertions.assertThat;
  32. @Disabled
  33. @SpringBootTest
  34. @AutoConfigureMockMvc
  35. public class Saml2LoginApplicationITests {
  36. @Autowired
  37. MockMvc mvc;
  38. @Autowired
  39. WebClient webClient;
  40. @BeforeEach
  41. void setup() {
  42. this.webClient.getCookieManager().clearCookies();
  43. }
  44. @Test
  45. void authenticationAttemptWhenValidThenShowsUserEmailAddress() throws Exception {
  46. performLogin();
  47. HtmlPage home = (HtmlPage) this.webClient.getCurrentWindow().getEnclosedPage();
  48. assertThat(home.asNormalizedText()).contains("You're email address is testuser@spring.security.saml");
  49. }
  50. private void performLogin() throws Exception {
  51. HtmlPage login = this.webClient.getPage("/");
  52. this.webClient.waitForBackgroundJavaScript(10000);
  53. HtmlForm form = findForm(login);
  54. HtmlInput username = form.getInputByName("username");
  55. HtmlPasswordInput password = form.getInputByName("password");
  56. HtmlSubmitInput submit = login.getHtmlElementById("okta-signin-submit");
  57. username.type("testuser@spring.security.saml");
  58. password.type("12345678");
  59. submit.click();
  60. this.webClient.waitForBackgroundJavaScript(10000);
  61. }
  62. private HtmlForm findForm(HtmlPage login) {
  63. for (HtmlForm form : login.getForms()) {
  64. try {
  65. if (form.getId().equals("form19")) {
  66. return form;
  67. }
  68. }
  69. catch (ElementNotFoundException ex) {
  70. // Continue
  71. }
  72. }
  73. throw new IllegalStateException("Could not resolve login form");
  74. }
  75. }