CustomConsentAuthorizationServerTests.java 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Copyright 2020-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 sample;
  17. import java.io.IOException;
  18. import java.util.ArrayList;
  19. import java.util.List;
  20. import com.gargoylesoftware.htmlunit.WebClient;
  21. import com.gargoylesoftware.htmlunit.WebResponse;
  22. import com.gargoylesoftware.htmlunit.html.DomElement;
  23. import com.gargoylesoftware.htmlunit.html.HtmlCheckBoxInput;
  24. import com.gargoylesoftware.htmlunit.html.HtmlPage;
  25. import org.junit.jupiter.api.BeforeEach;
  26. import org.junit.jupiter.api.Test;
  27. import org.junit.jupiter.api.extension.ExtendWith;
  28. import org.springframework.beans.factory.annotation.Autowired;
  29. import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
  30. import org.springframework.boot.test.context.SpringBootTest;
  31. import org.springframework.boot.test.mock.mockito.MockBean;
  32. import org.springframework.http.HttpStatus;
  33. import org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationConsentService;
  34. import org.springframework.security.test.context.support.WithMockUser;
  35. import org.springframework.test.context.junit.jupiter.SpringExtension;
  36. import org.springframework.web.util.UriComponentsBuilder;
  37. import static org.assertj.core.api.Assertions.assertThat;
  38. import static org.mockito.ArgumentMatchers.any;
  39. import static org.mockito.Mockito.when;
  40. /**
  41. * Consent page integration tests for the sample Authorization Server serving a custom Consent page.
  42. *
  43. * @author Dmitriy Dubson
  44. */
  45. @ExtendWith(SpringExtension.class)
  46. @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
  47. @AutoConfigureMockMvc
  48. public class CustomConsentAuthorizationServerTests {
  49. @Autowired
  50. private WebClient webClient;
  51. @MockBean
  52. private OAuth2AuthorizationConsentService authorizationConsentService;
  53. private final String redirectUri = "http://127.0.0.1/login/oauth2/code/messaging-client-oidc";
  54. private final String authorizationRequestUri = UriComponentsBuilder
  55. .fromPath("/oauth2/authorize")
  56. .queryParam("response_type", "code")
  57. .queryParam("client_id", "messaging-client")
  58. .queryParam("scope", "openid message.read message.write")
  59. .queryParam("state", "state")
  60. .queryParam("redirect_uri", this.redirectUri)
  61. .toUriString();
  62. @BeforeEach
  63. public void setUp() {
  64. this.webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
  65. this.webClient.getOptions().setRedirectEnabled(true);
  66. this.webClient.getCookieManager().clearCookies();
  67. when(this.authorizationConsentService.findById(any(), any())).thenReturn(null);
  68. }
  69. @Test
  70. @WithMockUser("user1")
  71. public void whenUserConsentsToAllScopesThenReturnAuthorizationCode() throws IOException {
  72. final HtmlPage consentPage = this.webClient.getPage(this.authorizationRequestUri);
  73. assertThat(consentPage.getTitleText()).isEqualTo("Custom consent page - Consent required");
  74. List<HtmlCheckBoxInput> scopes = new ArrayList<>();
  75. consentPage.querySelectorAll("input[name='scope']").forEach(scope ->
  76. scopes.add((HtmlCheckBoxInput) scope));
  77. for (HtmlCheckBoxInput scope : scopes) {
  78. scope.click();
  79. }
  80. List<String> scopeIds = new ArrayList<>();
  81. scopes.forEach(scope -> {
  82. assertThat(scope.isChecked()).isTrue();
  83. scopeIds.add(scope.getId());
  84. });
  85. assertThat(scopeIds).containsExactlyInAnyOrder("message.read", "message.write");
  86. DomElement submitConsentButton = consentPage.querySelector("button[id='submit-consent']");
  87. this.webClient.getOptions().setRedirectEnabled(false);
  88. WebResponse approveConsentResponse = submitConsentButton.click().getWebResponse();
  89. assertThat(approveConsentResponse.getStatusCode()).isEqualTo(HttpStatus.MOVED_PERMANENTLY.value());
  90. String location = approveConsentResponse.getResponseHeaderValue("location");
  91. assertThat(location).startsWith(this.redirectUri);
  92. assertThat(location).contains("code=");
  93. }
  94. @Test
  95. @WithMockUser("user1")
  96. public void whenUserCancelsConsentThenReturnAccessDeniedError() throws IOException {
  97. final HtmlPage consentPage = this.webClient.getPage(this.authorizationRequestUri);
  98. assertThat(consentPage.getTitleText()).isEqualTo("Custom consent page - Consent required");
  99. DomElement cancelConsentButton = consentPage.querySelector("button[id='cancel-consent']");
  100. this.webClient.getOptions().setRedirectEnabled(false);
  101. WebResponse cancelConsentResponse = cancelConsentButton.click().getWebResponse();
  102. assertThat(cancelConsentResponse.getStatusCode()).isEqualTo(HttpStatus.MOVED_PERMANENTLY.value());
  103. String location = cancelConsentResponse.getResponseHeaderValue("location");
  104. assertThat(location).startsWith(this.redirectUri);
  105. assertThat(location).contains("error=access_denied");
  106. }
  107. }