I've been trying to write an integration test for our Spring MVC application. We're using oAuth2 for authentication.
Spring in this case gives us a Principal
instance which we use to determine which entities we have to send back to the client. In our controller we have an endpoint:
@RequestMapping("/bookings")
public @ResponseBody ResponseEntity<List<ThirdPartyBooking>> getBookings(Principal principal) {
OAuth2Authentication auth = (OAuth2Authentication) principal;
OAuth2AuthenticationDetails authDetails = (OAuthAuthenticationDetails) auth.getDetails();
// Extract stuff from the details...
}
Now in our test I want to make sure that we only send bookings for the authenticated user. Below the code for the test can be found:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = {ThirdPartyBookingServiceConfiguration.class})
@WebAppConfiguration
@Component
public abstract class RepositoryTestBase {
@Resource
private WebApplicationContext context;
private MockMvc mockMvc;
@Before
public void setUp() {
mockMvc = MockMvcBuilders.webAppContextSetup(context).build();
}
@Test
public void shouldOnlyReturnUserBookings() throws Exception {
MockHttpServletResponse result = mockMvc.perform(MockMvcRequestBuilders.get("/bookings").principal(???)).andReturn().getResponse();
// Validate the response
}
}
How would I insert a OAuth2Authentication
at the ???
?