Saml2LoginApplicationITests.java 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. @SpringBootTest
  33. @AutoConfigureMockMvc
  34. @Disabled("gh-127")
  35. public class Saml2LoginApplicationITests {
  36. @Autowired
  37. MockMvc mvc;
  38. @Autowired
  39. WebClient webClient;
  40. @BeforeEach
  41. void setup() {
  42. this.webClient.getOptions().setThrowExceptionOnScriptError(false);
  43. this.webClient.getCookieManager().clearCookies();
  44. this.webClient.getOptions().setThrowExceptionOnScriptError(false);
  45. }
  46. @Test
  47. void authenticationAttemptWhenValidThenShowsUserEmailAddress() throws Exception {
  48. performLogin();
  49. HtmlPage home = (HtmlPage) this.webClient.getCurrentWindow().getEnclosedPage();
  50. assertThat(home.asNormalizedText()).contains("You're email address is testuser2@spring.security.saml");
  51. }
  52. private void performLogin() throws Exception {
  53. HtmlPage login = this.webClient.getPage("/");
  54. this.webClient.waitForBackgroundJavaScript(10000);
  55. HtmlForm form = findForm(login);
  56. HtmlInput username = form.getInputByName("username");
  57. HtmlPasswordInput password = form.getInputByName("password");
  58. HtmlSubmitInput submit = login.getHtmlElementById("okta-signin-submit");
  59. username.type("testuser2@spring.security.saml");
  60. password.type("12345678");
  61. submit.click();
  62. this.webClient.waitForBackgroundJavaScript(10000);
  63. }
  64. private HtmlForm findForm(HtmlPage login) {
  65. for (HtmlForm form : login.getForms()) {
  66. try {
  67. if (form.getId().equals("form19")) {
  68. return form;
  69. }
  70. }
  71. catch (ElementNotFoundException ex) {
  72. // Continue
  73. }
  74. }
  75. throw new IllegalStateException("Could not resolve login form");
  76. }
  77. }