Polish gh-411
This commit is contained in:
@@ -16,8 +16,8 @@
|
||||
package sample;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import com.gargoylesoftware.htmlunit.WebClient;
|
||||
import com.gargoylesoftware.htmlunit.WebResponse;
|
||||
@@ -27,6 +27,7 @@ import com.gargoylesoftware.htmlunit.html.HtmlPage;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
@@ -42,7 +43,7 @@ import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* Consent screen integration tests for the sample Authorization Server
|
||||
* Consent screen integration tests for the sample Authorization Server.
|
||||
*
|
||||
* @author Dmitriy Dubson
|
||||
*/
|
||||
@@ -50,21 +51,22 @@ import static org.mockito.Mockito.when;
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||
@AutoConfigureMockMvc
|
||||
public class OAuth2AuthorizationServerConsentTests {
|
||||
|
||||
@Autowired
|
||||
private WebClient webClient;
|
||||
|
||||
@MockBean
|
||||
private OAuth2AuthorizationConsentService mockAuthorizationConsentService;
|
||||
private OAuth2AuthorizationConsentService authorizationConsentService;
|
||||
|
||||
private final String testConsentResultEndpoint = "http://127.0.0.1/login/oauth2/code/messaging-client-oidc";
|
||||
private final String redirectUri = "http://127.0.0.1/login/oauth2/code/messaging-client-oidc";
|
||||
|
||||
private final String authorizeEndpoint = UriComponentsBuilder
|
||||
private final String authorizationRequestUri = UriComponentsBuilder
|
||||
.fromPath("/oauth2/authorize")
|
||||
.queryParam("response_type", "code")
|
||||
.queryParam("client_id", "messaging-client")
|
||||
.queryParam("scope", "openid message.read message.write")
|
||||
.queryParam("state", "state")
|
||||
.queryParam("redirect_uri", testConsentResultEndpoint)
|
||||
.queryParam("redirect_uri", this.redirectUri)
|
||||
.toUriString();
|
||||
|
||||
@Before
|
||||
@@ -72,24 +74,28 @@ public class OAuth2AuthorizationServerConsentTests {
|
||||
this.webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
|
||||
this.webClient.getOptions().setRedirectEnabled(true);
|
||||
this.webClient.getCookieManager().clearCookies();
|
||||
when(mockAuthorizationConsentService.findById(any(), any())).thenReturn(null);
|
||||
when(this.authorizationConsentService.findById(any(), any())).thenReturn(null);
|
||||
}
|
||||
|
||||
@Test
|
||||
@WithMockUser("user1")
|
||||
public void whenUserConsentsToAllScopesThenReturnAuthorizationCode() throws IOException {
|
||||
final HtmlPage consentPage = webClient.getPage(authorizeEndpoint);
|
||||
final HtmlPage consentPage = this.webClient.getPage(this.authorizationRequestUri);
|
||||
assertThat(consentPage.getTitleText()).isEqualTo("Consent required");
|
||||
|
||||
List<HtmlCheckBoxInput> scopes = consentPage.querySelectorAll("input[name='scope']").stream()
|
||||
.map(scope -> (HtmlCheckBoxInput) scope).collect(Collectors.toList());
|
||||
List<HtmlCheckBoxInput> scopes = new ArrayList<>();
|
||||
consentPage.querySelectorAll("input[name='scope']").forEach(scope ->
|
||||
scopes.add((HtmlCheckBoxInput) scope));
|
||||
for (HtmlCheckBoxInput scope : scopes) {
|
||||
scope.click();
|
||||
}
|
||||
|
||||
assertThat(scopes.stream().map(DomElement::getId).collect(Collectors.toList()))
|
||||
.containsExactlyInAnyOrder("message.read", "message.write");
|
||||
assertThat(scopes.stream().allMatch(HtmlCheckBoxInput::isChecked)).isTrue();
|
||||
List<String> scopeIds = new ArrayList<>();
|
||||
scopes.forEach(scope -> {
|
||||
assertThat(scope.isChecked()).isTrue();
|
||||
scopeIds.add(scope.getId());
|
||||
});
|
||||
assertThat(scopeIds).containsExactlyInAnyOrder("message.read", "message.write");
|
||||
|
||||
DomElement submitConsentButton = consentPage.querySelector("button[id='submit-consent']");
|
||||
this.webClient.getOptions().setRedirectEnabled(false);
|
||||
@@ -97,14 +103,14 @@ public class OAuth2AuthorizationServerConsentTests {
|
||||
WebResponse approveConsentResponse = submitConsentButton.click().getWebResponse();
|
||||
assertThat(approveConsentResponse.getStatusCode()).isEqualTo(HttpStatus.MOVED_PERMANENTLY.value());
|
||||
String location = approveConsentResponse.getResponseHeaderValue("location");
|
||||
assertThat(location).startsWith(testConsentResultEndpoint);
|
||||
assertThat(location).startsWith(this.redirectUri);
|
||||
assertThat(location).contains("code=");
|
||||
}
|
||||
|
||||
@Test
|
||||
@WithMockUser("user1")
|
||||
public void whenUserCancelsApprovingConsentThenReturnAnAccessDeniedError() throws IOException {
|
||||
final HtmlPage consentPage = webClient.getPage(authorizeEndpoint);
|
||||
public void whenUserCancelsConsentThenReturnAccessDeniedError() throws IOException {
|
||||
final HtmlPage consentPage = this.webClient.getPage(this.authorizationRequestUri);
|
||||
assertThat(consentPage.getTitleText()).isEqualTo("Consent required");
|
||||
|
||||
DomElement cancelConsentButton = consentPage.querySelector("button[id='cancel-consent']");
|
||||
@@ -113,6 +119,7 @@ public class OAuth2AuthorizationServerConsentTests {
|
||||
WebResponse cancelConsentResponse = cancelConsentButton.click().getWebResponse();
|
||||
assertThat(cancelConsentResponse.getStatusCode()).isEqualTo(HttpStatus.MOVED_PERMANENTLY.value());
|
||||
String location = cancelConsentResponse.getResponseHeaderValue("location");
|
||||
assertThat(location).startsWith(this.redirectUri);
|
||||
assertThat(location).contains("error=access_denied");
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user