|
|
|
@ -16,11 +16,16 @@ |
|
|
|
|
|
|
|
|
|
|
|
package org.springframework.security.config.annotation.method.configuration |
|
|
|
package org.springframework.security.config.annotation.method.configuration |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import io.mockk.Called |
|
|
|
|
|
|
|
import io.mockk.clearAllMocks |
|
|
|
|
|
|
|
import io.mockk.mockk |
|
|
|
|
|
|
|
import io.mockk.verify |
|
|
|
import kotlinx.coroutines.flow.collect |
|
|
|
import kotlinx.coroutines.flow.collect |
|
|
|
import kotlinx.coroutines.flow.toList |
|
|
|
import kotlinx.coroutines.flow.toList |
|
|
|
import kotlinx.coroutines.runBlocking |
|
|
|
import kotlinx.coroutines.runBlocking |
|
|
|
import org.assertj.core.api.Assertions.assertThat |
|
|
|
import org.assertj.core.api.Assertions.assertThat |
|
|
|
import org.assertj.core.api.Assertions.assertThatExceptionOfType |
|
|
|
import org.assertj.core.api.Assertions.assertThatExceptionOfType |
|
|
|
|
|
|
|
import org.junit.After |
|
|
|
import org.junit.Test |
|
|
|
import org.junit.Test |
|
|
|
import org.junit.runner.RunWith |
|
|
|
import org.junit.runner.RunWith |
|
|
|
import org.springframework.beans.factory.annotation.Autowired |
|
|
|
import org.springframework.beans.factory.annotation.Autowired |
|
|
|
@ -35,11 +40,23 @@ import org.springframework.test.context.junit4.SpringRunner |
|
|
|
@ContextConfiguration |
|
|
|
@ContextConfiguration |
|
|
|
class KotlinEnableReactiveMethodSecurityTests { |
|
|
|
class KotlinEnableReactiveMethodSecurityTests { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private lateinit var delegate: KotlinReactiveMessageService |
|
|
|
|
|
|
|
|
|
|
|
@Autowired |
|
|
|
@Autowired |
|
|
|
var messageService: KotlinReactiveMessageService? = null |
|
|
|
var messageService: KotlinReactiveMessageService? = null |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@After |
|
|
|
|
|
|
|
fun cleanup() { |
|
|
|
|
|
|
|
clearAllMocks() |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Autowired |
|
|
|
|
|
|
|
fun setConfig(config: Config) { |
|
|
|
|
|
|
|
this.delegate = config.delegate |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
fun suspendingGetResultWhenPermitAllThenSuccess() { |
|
|
|
fun `suspendingNoAuth always success`() { |
|
|
|
runBlocking { |
|
|
|
runBlocking { |
|
|
|
assertThat(messageService!!.suspendingNoAuth()).isEqualTo("success") |
|
|
|
assertThat(messageService!!.suspendingNoAuth()).isEqualTo("success") |
|
|
|
} |
|
|
|
} |
|
|
|
@ -47,14 +64,14 @@ class KotlinEnableReactiveMethodSecurityTests { |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
@WithMockUser(authorities = ["ROLE_ADMIN"]) |
|
|
|
@WithMockUser(authorities = ["ROLE_ADMIN"]) |
|
|
|
fun suspendingPreAuthorizeHasRoleWhenGrantedThenSuccess() { |
|
|
|
fun `suspendingPreAuthorizeHasRole when user has role then success`() { |
|
|
|
runBlocking { |
|
|
|
runBlocking { |
|
|
|
assertThat(messageService!!.suspendingPreAuthorizeHasRole()).isEqualTo("admin") |
|
|
|
assertThat(messageService!!.suspendingPreAuthorizeHasRole()).isEqualTo("admin") |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
fun suspendingPreAuthorizeHasRoleWhenNoAuthenticationThenDenied() { |
|
|
|
fun `suspendingPreAuthorizeHasRole when user does not have role then denied`() { |
|
|
|
assertThatExceptionOfType(AccessDeniedException::class.java).isThrownBy { |
|
|
|
assertThatExceptionOfType(AccessDeniedException::class.java).isThrownBy { |
|
|
|
runBlocking { |
|
|
|
runBlocking { |
|
|
|
messageService!!.suspendingPreAuthorizeHasRole() |
|
|
|
messageService!!.suspendingPreAuthorizeHasRole() |
|
|
|
@ -64,14 +81,14 @@ class KotlinEnableReactiveMethodSecurityTests { |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
@WithMockUser |
|
|
|
@WithMockUser |
|
|
|
fun suspendingPreAuthorizeBeanWhenGrantedThenSuccess() { |
|
|
|
fun `suspendingPreAuthorizeBean when authorized then success`() { |
|
|
|
runBlocking { |
|
|
|
runBlocking { |
|
|
|
assertThat(messageService!!.suspendingPreAuthorizeBean(true)).isEqualTo("check") |
|
|
|
assertThat(messageService!!.suspendingPreAuthorizeBean(true)).isEqualTo("check") |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
fun suspendingPreAuthorizeBeanWhenNotAuthorizedThenDenied() { |
|
|
|
fun `suspendingPreAuthorizeBean when not authorized then denied`() { |
|
|
|
assertThatExceptionOfType(AccessDeniedException::class.java).isThrownBy { |
|
|
|
assertThatExceptionOfType(AccessDeniedException::class.java).isThrownBy { |
|
|
|
runBlocking { |
|
|
|
runBlocking { |
|
|
|
messageService!!.suspendingPreAuthorizeBean(false) |
|
|
|
messageService!!.suspendingPreAuthorizeBean(false) |
|
|
|
@ -81,7 +98,7 @@ class KotlinEnableReactiveMethodSecurityTests { |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
@WithMockUser("user") |
|
|
|
@WithMockUser("user") |
|
|
|
fun suspendingPostAuthorizeWhenAuthorizedThenSuccess() { |
|
|
|
fun `suspendingPostAuthorize when authorized then success`() { |
|
|
|
runBlocking { |
|
|
|
runBlocking { |
|
|
|
assertThat(messageService!!.suspendingPostAuthorizeContainsName()).isEqualTo("user") |
|
|
|
assertThat(messageService!!.suspendingPostAuthorizeContainsName()).isEqualTo("user") |
|
|
|
} |
|
|
|
} |
|
|
|
@ -89,7 +106,7 @@ class KotlinEnableReactiveMethodSecurityTests { |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
@WithMockUser("other-user") |
|
|
|
@WithMockUser("other-user") |
|
|
|
fun suspendingPostAuthorizeWhenNotAuthorizedThenDenied() { |
|
|
|
fun `suspendingPostAuthorize when not authorized then denied`() { |
|
|
|
assertThatExceptionOfType(AccessDeniedException::class.java).isThrownBy { |
|
|
|
assertThatExceptionOfType(AccessDeniedException::class.java).isThrownBy { |
|
|
|
runBlocking { |
|
|
|
runBlocking { |
|
|
|
messageService!!.suspendingPostAuthorizeContainsName() |
|
|
|
messageService!!.suspendingPostAuthorizeContainsName() |
|
|
|
@ -97,16 +114,26 @@ class KotlinEnableReactiveMethodSecurityTests { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
|
|
fun `suspendingPreAuthorizeDelegate when user does not have role then delegate not called`() { |
|
|
|
|
|
|
|
assertThatExceptionOfType(AccessDeniedException::class.java).isThrownBy { |
|
|
|
|
|
|
|
runBlocking { |
|
|
|
|
|
|
|
messageService!!.suspendingPreAuthorizeDelegate() |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
verify { delegate wasNot Called } |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
@WithMockUser(authorities = ["ROLE_ADMIN"]) |
|
|
|
@WithMockUser(authorities = ["ROLE_ADMIN"]) |
|
|
|
fun suspendingFlowPreAuthorizeHasRoleWhenGrantedThenSuccess() { |
|
|
|
fun `suspendingFlowPreAuthorize when user has role then success`() { |
|
|
|
runBlocking { |
|
|
|
runBlocking { |
|
|
|
assertThat(messageService!!.suspendingFlowPreAuthorize().toList()).containsExactly(1, 2, 3) |
|
|
|
assertThat(messageService!!.suspendingFlowPreAuthorize().toList()).containsExactly(1, 2, 3) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
fun suspendingFlowPreAuthorizeHasRoleWhenNoAuthenticationThenDenied() { |
|
|
|
fun `suspendingFlowPreAuthorize when user does not have role then denied`() { |
|
|
|
assertThatExceptionOfType(AccessDeniedException::class.java).isThrownBy { |
|
|
|
assertThatExceptionOfType(AccessDeniedException::class.java).isThrownBy { |
|
|
|
runBlocking { |
|
|
|
runBlocking { |
|
|
|
messageService!!.suspendingFlowPreAuthorize().collect() |
|
|
|
messageService!!.suspendingFlowPreAuthorize().collect() |
|
|
|
@ -115,14 +142,14 @@ class KotlinEnableReactiveMethodSecurityTests { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
fun suspendingFlowPostAuthorizeWhenAuthorizedThenSuccess() { |
|
|
|
fun `suspendingFlowPostAuthorize when authorized then success`() { |
|
|
|
runBlocking { |
|
|
|
runBlocking { |
|
|
|
assertThat(messageService!!.suspendingFlowPostAuthorize(true).toList()).containsExactly(1, 2, 3) |
|
|
|
assertThat(messageService!!.suspendingFlowPostAuthorize(true).toList()).containsExactly(1, 2, 3) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
fun suspendingFlowPostAuthorizeWhenNotAuthorizedThenDenied() { |
|
|
|
fun `suspendingFlowPostAuthorize when not authorized then denied`() { |
|
|
|
assertThatExceptionOfType(AccessDeniedException::class.java).isThrownBy { |
|
|
|
assertThatExceptionOfType(AccessDeniedException::class.java).isThrownBy { |
|
|
|
runBlocking { |
|
|
|
runBlocking { |
|
|
|
messageService!!.suspendingFlowPostAuthorize(false).collect() |
|
|
|
messageService!!.suspendingFlowPostAuthorize(false).collect() |
|
|
|
@ -130,16 +157,26 @@ class KotlinEnableReactiveMethodSecurityTests { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
|
|
fun `suspendingFlowPreAuthorizeDelegate when not authorized then delegate not called`() { |
|
|
|
|
|
|
|
assertThatExceptionOfType(AccessDeniedException::class.java).isThrownBy { |
|
|
|
|
|
|
|
runBlocking { |
|
|
|
|
|
|
|
messageService!!.suspendingFlowPreAuthorizeDelegate().collect() |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
verify { delegate wasNot Called } |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
@WithMockUser(authorities = ["ROLE_ADMIN"]) |
|
|
|
@WithMockUser(authorities = ["ROLE_ADMIN"]) |
|
|
|
fun flowPreAuthorizeHasRoleWhenGrantedThenSuccess() { |
|
|
|
fun `flowPreAuthorize when user has role then success`() { |
|
|
|
runBlocking { |
|
|
|
runBlocking { |
|
|
|
assertThat(messageService!!.flowPreAuthorize().toList()).containsExactly(1, 2, 3) |
|
|
|
assertThat(messageService!!.flowPreAuthorize().toList()).containsExactly(1, 2, 3) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
fun flowPreAuthorizeHasRoleWhenNoAuthenticationThenDenied() { |
|
|
|
fun `flowPreAuthorize when user does not have role then denied`() { |
|
|
|
assertThatExceptionOfType(AccessDeniedException::class.java).isThrownBy { |
|
|
|
assertThatExceptionOfType(AccessDeniedException::class.java).isThrownBy { |
|
|
|
runBlocking { |
|
|
|
runBlocking { |
|
|
|
messageService!!.flowPreAuthorize().collect() |
|
|
|
messageService!!.flowPreAuthorize().collect() |
|
|
|
@ -148,14 +185,14 @@ class KotlinEnableReactiveMethodSecurityTests { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
fun flowPostAuthorizeWhenAuthorizedThenSuccess() { |
|
|
|
fun `flowPostAuthorize when authorized then success`() { |
|
|
|
runBlocking { |
|
|
|
runBlocking { |
|
|
|
assertThat(messageService!!.flowPostAuthorize(true).toList()).containsExactly(1, 2, 3) |
|
|
|
assertThat(messageService!!.flowPostAuthorize(true).toList()).containsExactly(1, 2, 3) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
fun flowPostAuthorizeWhenNotAuthorizedThenDenied() { |
|
|
|
fun `flowPostAuthorize when not authorized then denied`() { |
|
|
|
assertThatExceptionOfType(AccessDeniedException::class.java).isThrownBy { |
|
|
|
assertThatExceptionOfType(AccessDeniedException::class.java).isThrownBy { |
|
|
|
runBlocking { |
|
|
|
runBlocking { |
|
|
|
messageService!!.flowPostAuthorize(false).collect() |
|
|
|
messageService!!.flowPostAuthorize(false).collect() |
|
|
|
@ -163,13 +200,24 @@ class KotlinEnableReactiveMethodSecurityTests { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
|
|
fun `flowPreAuthorizeDelegate when user does not have role then delegate not called`() { |
|
|
|
|
|
|
|
assertThatExceptionOfType(AccessDeniedException::class.java).isThrownBy { |
|
|
|
|
|
|
|
runBlocking { |
|
|
|
|
|
|
|
messageService!!.flowPreAuthorizeDelegate().collect() |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
verify { delegate wasNot Called } |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@EnableReactiveMethodSecurity |
|
|
|
@EnableReactiveMethodSecurity |
|
|
|
@Configuration |
|
|
|
@Configuration |
|
|
|
open class Config { |
|
|
|
open class Config { |
|
|
|
|
|
|
|
var delegate = mockk<KotlinReactiveMessageService>() |
|
|
|
|
|
|
|
|
|
|
|
@Bean |
|
|
|
@Bean |
|
|
|
open fun messageService(): KotlinReactiveMessageServiceImpl { |
|
|
|
open fun messageService(): KotlinReactiveMessageServiceImpl { |
|
|
|
return KotlinReactiveMessageServiceImpl() |
|
|
|
return KotlinReactiveMessageServiceImpl(this.delegate) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Bean |
|
|
|
@Bean |
|
|
|
|