|
| 1 | +/* |
| 2 | + * Copyright 2020-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 sample; |
| 17 | + |
| 18 | +import java.io.IOException; |
| 19 | + |
| 20 | +import com.gargoylesoftware.htmlunit.Page; |
| 21 | +import com.gargoylesoftware.htmlunit.WebClient; |
| 22 | +import com.gargoylesoftware.htmlunit.WebResponse; |
| 23 | +import com.gargoylesoftware.htmlunit.html.HtmlButton; |
| 24 | +import com.gargoylesoftware.htmlunit.html.HtmlElement; |
| 25 | +import com.gargoylesoftware.htmlunit.html.HtmlInput; |
| 26 | +import com.gargoylesoftware.htmlunit.html.HtmlPage; |
| 27 | +import org.junit.Before; |
| 28 | +import org.junit.Test; |
| 29 | +import org.junit.runner.RunWith; |
| 30 | + |
| 31 | +import org.springframework.beans.factory.annotation.Autowired; |
| 32 | +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; |
| 33 | +import org.springframework.boot.test.context.SpringBootTest; |
| 34 | +import org.springframework.http.HttpStatus; |
| 35 | +import org.springframework.test.context.junit4.SpringRunner; |
| 36 | +import org.springframework.web.util.UriComponentsBuilder; |
| 37 | + |
| 38 | +import static org.assertj.core.api.Assertions.assertThat; |
| 39 | + |
| 40 | +/** |
| 41 | + * Integration tests for the sample Authorization Server. |
| 42 | + * |
| 43 | + * @author Daniel Garnier-Moiroux |
| 44 | + */ |
| 45 | +@RunWith(SpringRunner.class) |
| 46 | +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) |
| 47 | +@AutoConfigureMockMvc |
| 48 | +public class OAuth2AuthorizationServerApplicationTests { |
| 49 | + private static final String REDIRECT_URI = "http://127.0.0.1:8080/login/oauth2/code/messaging-client-oidc"; |
| 50 | + |
| 51 | + private static final String AUTHORIZATION_REQUEST = UriComponentsBuilder |
| 52 | + .fromPath("/oauth2/authorize") |
| 53 | + .queryParam("response_type", "code") |
| 54 | + .queryParam("client_id", "messaging-client") |
| 55 | + .queryParam("scope", "openid") |
| 56 | + .queryParam("state", "some-state") |
| 57 | + .queryParam("redirect_uri", REDIRECT_URI) |
| 58 | + .toUriString(); |
| 59 | + |
| 60 | + @Autowired |
| 61 | + private WebClient webClient; |
| 62 | + |
| 63 | + @Before |
| 64 | + public void setUp() { |
| 65 | + this.webClient.getOptions().setThrowExceptionOnFailingStatusCode(true); |
| 66 | + this.webClient.getOptions().setRedirectEnabled(true); |
| 67 | + this.webClient.getCookieManager().clearCookies(); // log out |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + public void whenLoginSuccessfulThenDisplayNotFoundError() throws IOException { |
| 72 | + HtmlPage page = this.webClient.getPage("/"); |
| 73 | + |
| 74 | + assertLoginPage(page); |
| 75 | + |
| 76 | + this.webClient.getOptions().setThrowExceptionOnFailingStatusCode(false); |
| 77 | + WebResponse signInResponse = signIn(page, "user1", "password").getWebResponse(); |
| 78 | + assertThat(signInResponse.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND.value()); // there is no "default" index page |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + public void whenLoginFailsThenDisplayBadCredentials() throws IOException { |
| 83 | + HtmlPage page = this.webClient.getPage("/"); |
| 84 | + |
| 85 | + HtmlPage loginErrorPage = signIn(page, "user1", "wrong-password"); |
| 86 | + |
| 87 | + HtmlElement alert = loginErrorPage.querySelector("div[role=\"alert\"]"); |
| 88 | + assertThat(alert).isNotNull(); |
| 89 | + assertThat(alert.getTextContent()).isEqualTo("Bad credentials"); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void whenNotLoggedInAndRequestingTokenThenRedirectsToLogin() throws IOException { |
| 94 | + HtmlPage page = this.webClient.getPage(AUTHORIZATION_REQUEST); |
| 95 | + |
| 96 | + assertLoginPage(page); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + public void whenLoggingInAndRequestingTokenThenRedirectsToClientApplication() throws IOException { |
| 101 | + // Log in |
| 102 | + this.webClient.getOptions().setThrowExceptionOnFailingStatusCode(false); |
| 103 | + this.webClient.getOptions().setRedirectEnabled(false); |
| 104 | + signIn(this.webClient.getPage("/login"), "user1", "password"); |
| 105 | + |
| 106 | + // Request token |
| 107 | + WebResponse response = this.webClient.getPage(AUTHORIZATION_REQUEST).getWebResponse(); |
| 108 | + |
| 109 | + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.MOVED_PERMANENTLY.value()); |
| 110 | + String location = response.getResponseHeaderValue("location"); |
| 111 | + assertThat(location).startsWith(REDIRECT_URI); |
| 112 | + assertThat(location).contains("code="); |
| 113 | + } |
| 114 | + |
| 115 | + private static <P extends Page> P signIn(HtmlPage page, String username, String password) throws IOException { |
| 116 | + HtmlInput usernameInput = page.querySelector("input[name=\"username\"]"); |
| 117 | + HtmlInput passwordInput = page.querySelector("input[name=\"password\"]"); |
| 118 | + HtmlButton signInButton = page.querySelector("button"); |
| 119 | + |
| 120 | + usernameInput.type(username); |
| 121 | + passwordInput.type(password); |
| 122 | + return signInButton.click(); |
| 123 | + } |
| 124 | + |
| 125 | + private static void assertLoginPage(HtmlPage page) { |
| 126 | + assertThat(page.getUrl().toString()).endsWith("/login"); |
| 127 | + |
| 128 | + HtmlInput usernameInput = page.querySelector("input[name=\"username\"]"); |
| 129 | + HtmlInput passwordInput = page.querySelector("input[name=\"password\"]"); |
| 130 | + HtmlButton signInButton = page.querySelector("button"); |
| 131 | + |
| 132 | + assertThat(usernameInput).isNotNull(); |
| 133 | + assertThat(passwordInput).isNotNull(); |
| 134 | + assertThat(signInButton.getTextContent()).isEqualTo("Sign in"); |
| 135 | + } |
| 136 | + |
| 137 | +} |
0 commit comments