Код: Выделить всё
class PlanIntegrationTests : BaseIntegrationTests() {
@Autowired
private lateinit var userRepository: DatabaseUserRecordRepository
@Autowired
private lateinit var planRepository: DatabasePlanRepository
private lateinit var payPerTestPlan: Plan
private lateinit var payPerMonthPlan: Plan
@BeforeEach
fun setup() {
cleanDatabase()
// Setup initial data into my test database - this all works fine
val userSeeds = UserSeeds(userRepository)
val planSeeds = PlanSeeds(planRepository)
runBlocking {
val adminUser = userSeeds.insertAdmin()
payPerTestPlan = planSeeds.insertPayPerTestPlan()
payPerMonthPlan = planSeeds.insertPayPerMonthPlan()
}
}
// This test always fails, why?
@Test
@WithLocalUser(username = UserSeeds.ADMIN_EMAIL)
fun `should return pay per test plan by id`(@Autowired webClient: WebTestClient) {
webClient
.get().uri("/plans/$payPerTestPlan.id")
.exchange()
.expectStatus().isOk()
.expectBody()
.consumeWith {
assertEquals(it.responseBody?.id, payPerTestPlan.id)
assertEquals(it.responseBody?.name, payPerTestPlan.name)
}
}
}
< /code>
Базовый класс имеет аннотации, которые, я думаю, требуются: < /p>
@ContextConfiguration(initializers = [DockerComposeInitializer::class])
@SpringBootTest()
@AutoConfigureWebTestClient
class BaseIntegrationTests { }
@Retention(AnnotationRetention.RUNTIME)
@WithSecurityContext(factory = IntegrationTestsSecurityContextFactory::class, setupBefore = TestExecutionEvent.TEST_METHOD)
annotation class WithLocalUser(val username: String)
class IntegrationTestsSecurityContextFactory(
// LocalUserDetailsService is from the web application and does the user lookup
@Autowired private val localUserDetailsService: LocalUserDetailsService
) : WithSecurityContextFactory {
override fun createSecurityContext(withUser: WithLocalUser): SecurityContext {
val context = SecurityContextHolder.createEmptyContext()
// This code works fine - it locates my test user and sets up the principal fine (I think)
val principal = runBlocking {
val userDetails = localUserDetailsService.findByUsername(withUser.username)
userDetails.awaitSingleOrNull()
}
context.authentication = UsernamePasswordAuthenticationToken(principal, principal?.password, principal?.authorities)
return context
}
}
< /code>
Для меня все выглядит так, как будто оно настроено правильно, но явно это не потому, что это не работает. Я упускаю где-нибудь какие-то пружинные аннотации или автоматически разрабатывать неправильные услуги?>
Подробнее здесь: https://stackoverflow.com/questions/794 ... -forbidden