16
16
17
17
package example ;
18
18
19
+ import java .util .ArrayList ;
20
+ import java .util .List ;
21
+
19
22
import org .htmlunit .ElementNotFoundException ;
20
23
import org .htmlunit .WebClient ;
24
+ import org .htmlunit .html .HtmlButton ;
25
+ import org .htmlunit .html .HtmlElement ;
21
26
import org .htmlunit .html .HtmlForm ;
22
27
import org .htmlunit .html .HtmlInput ;
23
28
import org .htmlunit .html .HtmlPage ;
24
29
import org .htmlunit .html .HtmlPasswordInput ;
25
- import org .htmlunit .html .HtmlSubmitInput ;
26
30
import org .junit .jupiter .api .BeforeEach ;
27
31
import org .junit .jupiter .api .Test ;
28
32
29
33
import org .springframework .beans .factory .annotation .Autowired ;
30
34
import org .springframework .boot .test .autoconfigure .web .servlet .AutoConfigureMockMvc ;
31
35
import org .springframework .boot .test .context .SpringBootTest ;
32
- import org .springframework .test .web .servlet . MockMvc ;
36
+ import org .springframework .boot . test .web .server . LocalServerPort ;
33
37
34
38
import static org .assertj .core .api .Assertions .assertThat ;
35
39
36
- @ SpringBootTest
40
+ @ SpringBootTest ( webEnvironment = SpringBootTest . WebEnvironment . DEFINED_PORT )
37
41
@ AutoConfigureMockMvc
38
42
public class Saml2LoginApplicationITests {
39
43
40
- @ Autowired
41
- MockMvc mvc ;
44
+
45
+ @ LocalServerPort
46
+ int port ;
42
47
43
48
@ Autowired
44
49
WebClient webClient ;
@@ -52,26 +57,48 @@ void setup() {
52
57
void authenticationAttemptWhenValidThenShowsUserEmailAddress () throws Exception {
53
58
performLogin ();
54
59
HtmlPage home = (HtmlPage ) this .webClient .getCurrentWindow ().getEnclosedPage ();
55
- assertThat (home .asNormalizedText ()).contains ("You're email address is testuser2@spring.security.saml" );
60
+ assertThat (home .asNormalizedText ()).contains ("You're email address is user1@example.org" );
61
+ }
62
+
63
+ @ Test
64
+ void logoutWhenRelyingPartyInitiatedLogoutThenLoginPageWithLogoutParam () throws Exception {
65
+ performLogin ();
66
+ HtmlPage home = (HtmlPage ) this .webClient .getCurrentWindow ().getEnclosedPage ();
67
+ HtmlElement rpLogoutButton = home .getHtmlElementById ("rp_logout_button" );
68
+ HtmlPage loginPage = rpLogoutButton .click ();
69
+ this .webClient .waitForBackgroundJavaScript (10000 );
70
+ List <String > urls = new ArrayList <>();
71
+ urls .add (loginPage .getUrl ().getFile ());
72
+ urls .add (((HtmlPage ) this .webClient .getCurrentWindow ().getEnclosedPage ()).getUrl ().getFile ());
73
+ assertThat (urls ).withFailMessage (() -> {
74
+ // @formatter:off
75
+ String builder = loginPage .asXml ()
76
+ + "\n \n \n "
77
+ + "Enclosing Page"
78
+ + "\n \n \n "
79
+ + ((HtmlPage ) this .webClient .getCurrentWindow ().getEnclosedPage ()).asXml ();
80
+ // @formatter:on
81
+ return builder ;
82
+ }).contains ("/login?logout" );
56
83
}
57
84
58
85
private void performLogin () throws Exception {
59
- HtmlPage login = this .webClient .getPage ("/ " );
86
+ HtmlPage login = this .webClient .getPage ("http://localhost:" + this . port + "/saml2/authenticate/one " );
60
87
this .webClient .waitForBackgroundJavaScript (10000 );
61
88
HtmlForm form = findForm (login );
62
89
HtmlInput username = form .getInputByName ("username" );
63
90
HtmlPasswordInput password = form .getInputByName ("password" );
64
- HtmlSubmitInput submit = login . getHtmlElementById ( "okta-signin-submit" );
65
- username .type ("testuser2@spring.security.saml " );
66
- password .type ("12345678 " );
91
+ HtmlButton submit = ( HtmlButton ) form . getElementsByTagName ( "button" ). iterator (). next ( );
92
+ username .type ("user1 " );
93
+ password .type ("user1pass " );
67
94
submit .click ();
68
95
this .webClient .waitForBackgroundJavaScript (10000 );
69
96
}
70
97
71
98
private HtmlForm findForm (HtmlPage login ) {
72
99
for (HtmlForm form : login .getForms ()) {
73
100
try {
74
- if (form .getId ().equals ("form19 " )) {
101
+ if (form .getNameAttribute ().equals ("f " )) {
75
102
return form ;
76
103
}
77
104
}
0 commit comments