OAuth2ResourceServerControllerTests.java 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. package sample;
  17. import org.junit.Test;
  18. import org.junit.runner.RunWith;
  19. import org.springframework.beans.factory.annotation.Autowired;
  20. import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
  21. import org.springframework.boot.test.mock.mockito.MockBean;
  22. import org.springframework.security.core.authority.SimpleGrantedAuthority;
  23. import org.springframework.security.oauth2.jwt.JwtDecoder;
  24. import org.springframework.test.context.junit4.SpringRunner;
  25. import org.springframework.test.web.servlet.MockMvc;
  26. import static org.hamcrest.CoreMatchers.is;
  27. import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.jwt;
  28. import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
  29. import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
  30. import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
  31. import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
  32. /**
  33. *
  34. * @author Jérôme Wacongne <ch4mp@c4-soft.com>
  35. * @author Josh Cummings
  36. * @since 5.2.0
  37. *
  38. */
  39. @RunWith(SpringRunner.class)
  40. @WebMvcTest(OAuth2ResourceServerController.class)
  41. public class OAuth2ResourceServerControllerTests {
  42. @Autowired
  43. MockMvc mockMvc;
  44. @MockBean
  45. JwtDecoder jwtDecoder;
  46. @Test
  47. public void indexGreetsAuthenticatedUser() throws Exception {
  48. mockMvc.perform(get("/").with(jwt(jwt -> jwt.subject("ch4mpy"))))
  49. .andExpect(content().string(is("Hello, ch4mpy!")));
  50. }
  51. @Test
  52. public void messageCanBeReadWithScopeMessageReadAuthority() throws Exception {
  53. mockMvc.perform(get("/message").with(jwt(jwt -> jwt.claim("scope", "message:read"))))
  54. .andExpect(content().string(is("secret message")));
  55. mockMvc.perform(get("/message")
  56. .with(jwt().authorities(new SimpleGrantedAuthority(("SCOPE_message:read")))))
  57. .andExpect(content().string(is("secret message")));
  58. }
  59. @Test
  60. public void messageCanNotBeReadWithoutScopeMessageReadAuthority() throws Exception {
  61. mockMvc.perform(get("/message").with(jwt()))
  62. .andExpect(status().isForbidden());
  63. }
  64. @Test
  65. public void messageCanNotBeCreatedWithoutAnyScope() throws Exception {
  66. mockMvc.perform(post("/message")
  67. .content("Hello message")
  68. .with(jwt()))
  69. .andExpect(status().isForbidden());
  70. }
  71. @Test
  72. public void messageCanNotBeCreatedWithScopeMessageReadAuthority() throws Exception {
  73. mockMvc.perform(post("/message")
  74. .content("Hello message")
  75. .with(jwt(jwt -> jwt.claim("scope", "message:read"))))
  76. .andExpect(status().isForbidden());
  77. }
  78. @Test
  79. public void messageCanBeCreatedWithScopeMessageWriteAuthority()
  80. throws Exception {
  81. mockMvc.perform(post("/message")
  82. .content("Hello message")
  83. .with(jwt(jwt -> jwt.claim("scope", "message:write"))))
  84. .andExpect(status().isOk())
  85. .andExpect(content().string(is("Message was created. Content: Hello message")));
  86. }
  87. }