|
| 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 | + * http://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 org.junit.Test; |
| 19 | +import org.junit.runner.RunWith; |
| 20 | + |
| 21 | +import org.springframework.beans.factory.annotation.Autowired; |
| 22 | +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; |
| 23 | +import org.springframework.boot.test.context.SpringBootTest; |
| 24 | +import org.springframework.http.HttpHeaders; |
| 25 | +import org.springframework.mock.web.MockHttpServletRequest; |
| 26 | +import org.springframework.test.context.ActiveProfiles; |
| 27 | +import org.springframework.test.context.junit4.SpringRunner; |
| 28 | +import org.springframework.test.web.servlet.MockMvc; |
| 29 | +import org.springframework.test.web.servlet.request.RequestPostProcessor; |
| 30 | + |
| 31 | +import static org.hamcrest.Matchers.containsString; |
| 32 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
| 33 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; |
| 34 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header; |
| 35 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
| 36 | + |
| 37 | +/** |
| 38 | + * Integration tests for {@link OAuth2ResourceServerApplication} |
| 39 | + * |
| 40 | + * @author Josh Cummings |
| 41 | + */ |
| 42 | +@RunWith(SpringRunner.class) |
| 43 | +@SpringBootTest |
| 44 | +@AutoConfigureMockMvc |
| 45 | +@ActiveProfiles("test") |
| 46 | +public class OAuth2ResourceServerApplicationITests { |
| 47 | + |
| 48 | + String noScopesToken = "00ed5855-1869-47a0-b0c9-0f3ce520aee7"; |
| 49 | + String messageReadToken = "b43d1500-c405-4dc9-b9c9-6cfd966c34c9"; |
| 50 | + |
| 51 | + @Autowired |
| 52 | + MockMvc mvc; |
| 53 | + |
| 54 | + @Test |
| 55 | + public void performWhenValidBearerTokenThenAllows() |
| 56 | + throws Exception { |
| 57 | + |
| 58 | + this.mvc.perform(get("/").with(bearerToken(this.noScopesToken))) |
| 59 | + .andExpect(status().isOk()) |
| 60 | + .andExpect(content().string(containsString("Hello, subject!"))); |
| 61 | + } |
| 62 | + |
| 63 | + // -- tests with scopes |
| 64 | + |
| 65 | + @Test |
| 66 | + public void performWhenValidBearerTokenThenScopedRequestsAlsoWork() |
| 67 | + throws Exception { |
| 68 | + |
| 69 | + this.mvc.perform(get("/message").with(bearerToken(this.messageReadToken))) |
| 70 | + .andExpect(status().isOk()) |
| 71 | + .andExpect(content().string(containsString("secret message"))); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void performWhenInsufficientlyScopedBearerTokenThenDeniesScopedMethodAccess() |
| 76 | + throws Exception { |
| 77 | + |
| 78 | + this.mvc.perform(get("/message").with(bearerToken(this.noScopesToken))) |
| 79 | + .andExpect(status().isForbidden()) |
| 80 | + .andExpect(header().string(HttpHeaders.WWW_AUTHENTICATE, |
| 81 | + containsString("Bearer error=\"insufficient_scope\""))); |
| 82 | + } |
| 83 | + |
| 84 | + private static class BearerTokenRequestPostProcessor implements RequestPostProcessor { |
| 85 | + private String token; |
| 86 | + |
| 87 | + public BearerTokenRequestPostProcessor(String token) { |
| 88 | + this.token = token; |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public MockHttpServletRequest postProcessRequest(MockHttpServletRequest request) { |
| 93 | + request.addHeader("Authorization", "Bearer " + this.token); |
| 94 | + return request; |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + private static BearerTokenRequestPostProcessor bearerToken(String token) { |
| 99 | + return new BearerTokenRequestPostProcessor(token); |
| 100 | + } |
| 101 | +} |
0 commit comments