OAuth2ResourceServerControllerTests.java 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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.security.core.authority.SimpleGrantedAuthority;
  22. import org.springframework.test.context.junit4.SpringRunner;
  23. import org.springframework.test.web.servlet.MockMvc;
  24. import static org.hamcrest.CoreMatchers.is;
  25. import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.opaqueToken;
  26. import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.jwt;
  27. import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
  28. import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
  29. import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
  30. import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
  31. /**
  32. * @author Josh Cummings
  33. * @since 5.3
  34. */
  35. @RunWith(SpringRunner.class)
  36. @WebMvcTest(OAuth2ResourceServerController.class)
  37. public class OAuth2ResourceServerControllerTests {
  38. @Autowired
  39. MockMvc mvc;
  40. @Test
  41. public void indexGreetsAuthenticatedUser() throws Exception {
  42. this.mvc.perform(get("/").with(opaqueToken().attribute("sub", "ch4mpy")))
  43. .andExpect(content().string(is("Hello, ch4mpy!")));
  44. }
  45. @Test
  46. public void messageCanBeReadWithScopeMessageReadAuthority() throws Exception {
  47. this.mvc.perform(get("/message").with(opaqueToken().scopes("message:read")))
  48. .andExpect(content().string(is("secret message")));
  49. this.mvc.perform(get("/message")
  50. .with(jwt().authorities(new SimpleGrantedAuthority(("SCOPE_message:read")))))
  51. .andExpect(content().string(is("secret message")));
  52. }
  53. @Test
  54. public void messageCanNotBeReadWithoutScopeMessageReadAuthority() throws Exception {
  55. this.mvc.perform(get("/message").with(opaqueToken()))
  56. .andExpect(status().isForbidden());
  57. }
  58. @Test
  59. public void messageCanNotBeCreatedWithoutAnyScope() throws Exception {
  60. this.mvc.perform(post("/message")
  61. .content("Hello message")
  62. .with(opaqueToken()))
  63. .andExpect(status().isForbidden());
  64. }
  65. @Test
  66. public void messageCanNotBeCreatedWithScopeMessageReadAuthority() throws Exception {
  67. this.mvc.perform(post("/message")
  68. .content("Hello message")
  69. .with(opaqueToken().scopes("message:read")))
  70. .andExpect(status().isForbidden());
  71. }
  72. @Test
  73. public void messageCanBeCreatedWithScopeMessageWriteAuthority() throws Exception {
  74. this.mvc.perform(post("/message")
  75. .content("Hello message")
  76. .with(opaqueToken().scopes("message:write")))
  77. .andExpect(status().isOk())
  78. .andExpect(content().string(is("Message was created. Content: Hello message")));
  79. }
  80. }