I'm trying to run API level tests against a Spring Boot Application. We use DGS for GraphQL and our endpoints are secured with @PreAuthorize
annotations.
The tests look something like this:
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class ApiTest {
@LocalServerPort
lateinit var localServerPort: Number
@Autowired
private lateinit var webClientBuilder: WebClient.Builder
val graphQLClient by lazy {
val baseUrl = "http://localhost:$localServerPort/graphql"
MonoGraphQLClient.createWithWebClient(
builder.baseUrl(baseUrl).build(),
)
}
@Test
fun `should call an endpoint`() {
val request = ...
client.reactiveExecuteQuery(request.serialize()).block()
... do something with the result ...
}
}
When I run this, I get "Access is denied".
How can I enable this WebClient to make authorised requests, ideally with any role or authority I choose?