Open arashbi opened 7 years ago
You can setup your own instance of MockMvc (https://docs.spring.io/spring-security/site/docs/current/reference/html/test-mockmvc.html) and pass it to the MockMvcTarget. This is how we setup the Pact Provider tests with secured endpoints:
public MockMvcTarget mockMvcTarget(@Value("${server.servlet.context-path}") String contextPath,
WebApplicationContext webApplicationContext) {
var requestBuilder = MockMvcRequestBuilders.get("/")
.servletPath(contextPath)
.with(user("admin")
.password("pass")
.roles("supervisor", "operator"));
MockMvc mockMvc = MockMvcBuilders
.webAppContextSetup(webApplicationContext)
.defaultRequest(requestBuilder)
.apply(springSecurity())
.build();
MockMvcTarget mockMvcTarget = new MockMvcTarget();
mockMvcTarget.setMockMvc(mockMvc);
return mockMvcTarget;
}
@arashbi Hey, have you figured out how to set up the Authentication object for the controllers? I am currently stuck on this :(
I want to test my secure end-points using spring module, but I cannot find a way to bootstrap spring security. Ideally, I would like to have a Principal object injected to my controller method. I can do it easily if I was writing the test, simply add it to the mockMvc method call. The question is : how to bootstrap Spring in Pact easily.