1
0
mirror of synced 2026-08-05 09:47:05 +00:00

Support nested suspend calls for Kotlin coroutines

Closes gh-13764
This commit is contained in:
Steve Riesenberg
2023-08-31 17:33:38 -05:00
committed by Steve Riesenberg
parent 1a45602dbb
commit 92256f0522
2 changed files with 43 additions and 28 deletions
@@ -18,14 +18,17 @@ package org.springframework.security.config.annotation.method.configuration
import io.mockk.Called
import io.mockk.clearAllMocks
import io.mockk.coEvery
import io.mockk.coVerify
import io.mockk.mockk
import io.mockk.verify
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.toList
import kotlinx.coroutines.runBlocking
import org.assertj.core.api.Assertions.assertThat
import org.assertj.core.api.Assertions.assertThatExceptionOfType
import org.junit.After
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
import org.springframework.beans.factory.annotation.Autowired
@@ -46,7 +49,7 @@ class KotlinEnableReactiveMethodSecurityNoAuthorizationManagerTests {
@Autowired
var messageService: KotlinReactiveMessageService? = null
@After
@AfterEach
fun cleanup() {
clearAllMocks()
}
@@ -125,6 +128,16 @@ class KotlinEnableReactiveMethodSecurityNoAuthorizationManagerTests {
verify { delegate wasNot Called }
}
@Test
@WithMockUser(authorities = ["ROLE_ADMIN"])
fun `suspendingPreAuthorizeDelegate when user has role then delegate called`() {
coEvery { delegate.suspendingPreAuthorizeHasRole() } returns "ok"
runBlocking {
messageService!!.suspendingPreAuthorizeDelegate()
}
coVerify(exactly = 1) { delegate.suspendingPreAuthorizeHasRole() }
}
@Test
@WithMockUser(authorities = ["ROLE_ADMIN"])
fun `suspendingFlowPreAuthorize when user has role then success`() {
@@ -168,6 +181,16 @@ class KotlinEnableReactiveMethodSecurityNoAuthorizationManagerTests {
verify { delegate wasNot Called }
}
@Test
@WithMockUser(authorities = ["ROLE_ADMIN"])
fun `suspendingFlowPreAuthorizeDelegate when user has role then delegate called`() {
coEvery { delegate.flowPreAuthorize() } returns flow { }
runBlocking {
messageService!!.suspendingFlowPreAuthorizeDelegate().collect()
}
coVerify(exactly = 1) { delegate.flowPreAuthorize() }
}
@Test
@WithMockUser(authorities = ["ROLE_ADMIN"])
fun `flowPreAuthorize when user has role then success`() {
@@ -211,6 +234,16 @@ class KotlinEnableReactiveMethodSecurityNoAuthorizationManagerTests {
verify { delegate wasNot Called }
}
@Test
@WithMockUser(authorities = ["ROLE_ADMIN"])
fun `flowPreAuthorizeDelegate when user has role then delegate called`() {
coEvery { delegate.flowPreAuthorize() } returns flow { }
runBlocking {
messageService!!.flowPreAuthorizeDelegate().collect()
}
coVerify(exactly = 1) { delegate.flowPreAuthorize() }
}
@Configuration
@EnableReactiveMethodSecurity(useAuthorizationManager = false)
open class Config {