|
| 1 | +/* |
| 2 | + * Copyright 2002-2019 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 | + |
| 17 | +package sample.web; |
| 18 | + |
| 19 | +import java.util.Collections; |
| 20 | + |
| 21 | +import org.junit.Test; |
| 22 | +import org.junit.runner.RunWith; |
| 23 | + |
| 24 | +import org.springframework.beans.factory.annotation.Autowired; |
| 25 | +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; |
| 26 | +import org.springframework.context.annotation.Bean; |
| 27 | +import org.springframework.context.annotation.Configuration; |
| 28 | +import org.springframework.context.annotation.Import; |
| 29 | +import org.springframework.security.oauth2.client.registration.ClientRegistration; |
| 30 | +import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository; |
| 31 | +import org.springframework.security.oauth2.client.registration.InMemoryClientRegistrationRepository; |
| 32 | +import org.springframework.security.oauth2.client.web.HttpSessionOAuth2AuthorizedClientRepository; |
| 33 | +import org.springframework.security.oauth2.client.web.OAuth2AuthorizedClientRepository; |
| 34 | +import org.springframework.security.oauth2.core.AuthorizationGrantType; |
| 35 | +import org.springframework.test.context.junit4.SpringRunner; |
| 36 | +import org.springframework.test.web.servlet.MockMvc; |
| 37 | + |
| 38 | +import static org.springframework.security.oauth2.core.oidc.IdTokenClaimNames.SUB; |
| 39 | +import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.oidcLogin; |
| 40 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
| 41 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model; |
| 42 | + |
| 43 | +/** |
| 44 | + * Tests for {@link OAuth2LoginController} |
| 45 | + * |
| 46 | + * @author Josh Cummings |
| 47 | + */ |
| 48 | +@RunWith(SpringRunner.class) |
| 49 | +@WebMvcTest |
| 50 | +@Import({OAuth2LoginController.class, OAuth2LoginControllerTests.OAuth2ClientConfig.class}) |
| 51 | +public class OAuth2LoginControllerTests { |
| 52 | + |
| 53 | + static ClientRegistration clientRegistration = ClientRegistration.withRegistrationId("test") |
| 54 | + .authorizationGrantType(AuthorizationGrantType.PASSWORD) |
| 55 | + .clientId("my-client-id") |
| 56 | + .clientName("my-client-name") |
| 57 | + .tokenUri("https://token-uri.example.org") |
| 58 | + .build(); |
| 59 | + |
| 60 | + @Autowired |
| 61 | + MockMvc mvc; |
| 62 | + |
| 63 | + @Test |
| 64 | + public void rootWhenAuthenticatedReturnsUserAndClient() throws Exception { |
| 65 | + this.mvc.perform(get("/").with(oidcLogin())) |
| 66 | + .andExpect(model().attribute("userName", "test-subject")) |
| 67 | + .andExpect(model().attribute("clientName", "test")) |
| 68 | + .andExpect(model().attribute("userAttributes", Collections.singletonMap(SUB, "test-subject"))); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void rootWhenOverridingClientRegistrationReturnsAccordingly() throws Exception { |
| 73 | + this.mvc.perform(get("/").with(oidcLogin() |
| 74 | + .clientRegistration(clientRegistration) |
| 75 | + .idToken(i -> i.subject("spring-security")))) |
| 76 | + .andExpect(model().attribute("userName", "spring-security")) |
| 77 | + .andExpect(model().attribute("clientName", "my-client-name")) |
| 78 | + .andExpect(model().attribute("userAttributes", Collections.singletonMap(SUB, "spring-security"))); |
| 79 | + } |
| 80 | + |
| 81 | + @Configuration |
| 82 | + static class OAuth2ClientConfig { |
| 83 | + |
| 84 | + @Bean |
| 85 | + ClientRegistrationRepository clientRegistrationRepository() { |
| 86 | + return new InMemoryClientRegistrationRepository(clientRegistration); |
| 87 | + } |
| 88 | + |
| 89 | + @Bean |
| 90 | + OAuth2AuthorizedClientRepository authorizedClientRepository() { |
| 91 | + return new HttpSessionOAuth2AuthorizedClientRepository(); |
| 92 | + } |
| 93 | + } |
| 94 | +} |
0 commit comments