16
16
17
17
package org.springframework.security.config.annotation.method.configuration
18
18
19
+ import io.mockk.Called
20
+ import io.mockk.clearAllMocks
21
+ import io.mockk.mockk
22
+ import io.mockk.verify
19
23
import kotlinx.coroutines.flow.collect
20
24
import kotlinx.coroutines.flow.toList
21
25
import kotlinx.coroutines.runBlocking
22
26
import org.assertj.core.api.Assertions.assertThat
23
27
import org.assertj.core.api.Assertions.assertThatExceptionOfType
28
+ import org.junit.After
24
29
import org.junit.Test
25
30
import org.junit.runner.RunWith
26
31
import org.springframework.beans.factory.annotation.Autowired
@@ -35,26 +40,38 @@ import org.springframework.test.context.junit4.SpringRunner
35
40
@ContextConfiguration
36
41
class KotlinEnableReactiveMethodSecurityTests {
37
42
43
+ private lateinit var delegate: KotlinReactiveMessageService
44
+
38
45
@Autowired
39
46
var messageService: KotlinReactiveMessageService ? = null
40
47
48
+ @After
49
+ fun cleanup () {
50
+ clearAllMocks()
51
+ }
52
+
53
+ @Autowired
54
+ fun setConfig (config : Config ) {
55
+ this .delegate = config.delegate
56
+ }
57
+
41
58
@Test
42
- fun suspendingGetResultWhenPermitAllThenSuccess () {
59
+ fun `suspendingNoAuth always success` () {
43
60
runBlocking {
44
61
assertThat(messageService!! .suspendingNoAuth()).isEqualTo(" success" )
45
62
}
46
63
}
47
64
48
65
@Test
49
66
@WithMockUser(authorities = [" ROLE_ADMIN" ])
50
- fun suspendingPreAuthorizeHasRoleWhenGrantedThenSuccess () {
67
+ fun `suspendingPreAuthorizeHasRole when user has role then success` () {
51
68
runBlocking {
52
69
assertThat(messageService!! .suspendingPreAuthorizeHasRole()).isEqualTo(" admin" )
53
70
}
54
71
}
55
72
56
73
@Test
57
- fun suspendingPreAuthorizeHasRoleWhenNoAuthenticationThenDenied () {
74
+ fun `suspendingPreAuthorizeHasRole when user does not have role then denied` () {
58
75
assertThatExceptionOfType(AccessDeniedException ::class .java).isThrownBy {
59
76
runBlocking {
60
77
messageService!! .suspendingPreAuthorizeHasRole()
@@ -64,14 +81,14 @@ class KotlinEnableReactiveMethodSecurityTests {
64
81
65
82
@Test
66
83
@WithMockUser
67
- fun suspendingPreAuthorizeBeanWhenGrantedThenSuccess () {
84
+ fun `suspendingPreAuthorizeBean when authorized then success` () {
68
85
runBlocking {
69
86
assertThat(messageService!! .suspendingPreAuthorizeBean(true )).isEqualTo(" check" )
70
87
}
71
88
}
72
89
73
90
@Test
74
- fun suspendingPreAuthorizeBeanWhenNotAuthorizedThenDenied () {
91
+ fun `suspendingPreAuthorizeBean when not authorized then denied` () {
75
92
assertThatExceptionOfType(AccessDeniedException ::class .java).isThrownBy {
76
93
runBlocking {
77
94
messageService!! .suspendingPreAuthorizeBean(false )
@@ -81,32 +98,42 @@ class KotlinEnableReactiveMethodSecurityTests {
81
98
82
99
@Test
83
100
@WithMockUser(" user" )
84
- fun suspendingPostAuthorizeWhenAuthorizedThenSuccess () {
101
+ fun `suspendingPostAuthorize when authorized then success` () {
85
102
runBlocking {
86
103
assertThat(messageService!! .suspendingPostAuthorizeContainsName()).isEqualTo(" user" )
87
104
}
88
105
}
89
106
90
107
@Test
91
108
@WithMockUser(" other-user" )
92
- fun suspendingPostAuthorizeWhenNotAuthorizedThenDenied () {
109
+ fun `suspendingPostAuthorize when not authorized then denied` () {
93
110
assertThatExceptionOfType(AccessDeniedException ::class .java).isThrownBy {
94
111
runBlocking {
95
112
messageService!! .suspendingPostAuthorizeContainsName()
96
113
}
97
114
}
98
115
}
99
116
117
+ @Test
118
+ fun `suspendingPreAuthorizeDelegate when user does not have role then delegate not called` () {
119
+ assertThatExceptionOfType(AccessDeniedException ::class .java).isThrownBy {
120
+ runBlocking {
121
+ messageService!! .suspendingPreAuthorizeDelegate()
122
+ }
123
+ }
124
+ verify { delegate wasNot Called }
125
+ }
126
+
100
127
@Test
101
128
@WithMockUser(authorities = [" ROLE_ADMIN" ])
102
- fun suspendingFlowPreAuthorizeHasRoleWhenGrantedThenSuccess () {
129
+ fun `suspendingFlowPreAuthorize when user has role then success` () {
103
130
runBlocking {
104
131
assertThat(messageService!! .suspendingFlowPreAuthorize().toList()).containsExactly(1 , 2 , 3 )
105
132
}
106
133
}
107
134
108
135
@Test
109
- fun suspendingFlowPreAuthorizeHasRoleWhenNoAuthenticationThenDenied () {
136
+ fun `suspendingFlowPreAuthorize when user does not have role then denied` () {
110
137
assertThatExceptionOfType(AccessDeniedException ::class .java).isThrownBy {
111
138
runBlocking {
112
139
messageService!! .suspendingFlowPreAuthorize().collect()
@@ -115,31 +142,41 @@ class KotlinEnableReactiveMethodSecurityTests {
115
142
}
116
143
117
144
@Test
118
- fun suspendingFlowPostAuthorizeWhenAuthorizedThenSuccess () {
145
+ fun `suspendingFlowPostAuthorize when authorized then success` () {
119
146
runBlocking {
120
147
assertThat(messageService!! .suspendingFlowPostAuthorize(true ).toList()).containsExactly(1 , 2 , 3 )
121
148
}
122
149
}
123
150
124
151
@Test
125
- fun suspendingFlowPostAuthorizeWhenNotAuthorizedThenDenied () {
152
+ fun `suspendingFlowPostAuthorize when not authorized then denied` () {
126
153
assertThatExceptionOfType(AccessDeniedException ::class .java).isThrownBy {
127
154
runBlocking {
128
155
messageService!! .suspendingFlowPostAuthorize(false ).collect()
129
156
}
130
157
}
131
158
}
132
159
160
+ @Test
161
+ fun `suspendingFlowPreAuthorizeDelegate when not authorized then delegate not called` () {
162
+ assertThatExceptionOfType(AccessDeniedException ::class .java).isThrownBy {
163
+ runBlocking {
164
+ messageService!! .suspendingFlowPreAuthorizeDelegate().collect()
165
+ }
166
+ }
167
+ verify { delegate wasNot Called }
168
+ }
169
+
133
170
@Test
134
171
@WithMockUser(authorities = [" ROLE_ADMIN" ])
135
- fun flowPreAuthorizeHasRoleWhenGrantedThenSuccess () {
172
+ fun `flowPreAuthorize when user has role then success` () {
136
173
runBlocking {
137
174
assertThat(messageService!! .flowPreAuthorize().toList()).containsExactly(1 , 2 , 3 )
138
175
}
139
176
}
140
177
141
178
@Test
142
- fun flowPreAuthorizeHasRoleWhenNoAuthenticationThenDenied () {
179
+ fun `flowPreAuthorize when user does not have role then denied` () {
143
180
assertThatExceptionOfType(AccessDeniedException ::class .java).isThrownBy {
144
181
runBlocking {
145
182
messageService!! .flowPreAuthorize().collect()
@@ -148,28 +185,39 @@ class KotlinEnableReactiveMethodSecurityTests {
148
185
}
149
186
150
187
@Test
151
- fun flowPostAuthorizeWhenAuthorizedThenSuccess () {
188
+ fun `flowPostAuthorize when authorized then success` () {
152
189
runBlocking {
153
190
assertThat(messageService!! .flowPostAuthorize(true ).toList()).containsExactly(1 , 2 , 3 )
154
191
}
155
192
}
156
193
157
194
@Test
158
- fun flowPostAuthorizeWhenNotAuthorizedThenDenied () {
195
+ fun `flowPostAuthorize when not authorized then denied` () {
159
196
assertThatExceptionOfType(AccessDeniedException ::class .java).isThrownBy {
160
197
runBlocking {
161
198
messageService!! .flowPostAuthorize(false ).collect()
162
199
}
163
200
}
164
201
}
165
202
203
+ @Test
204
+ fun `flowPreAuthorizeDelegate when user does not have role then delegate not called` () {
205
+ assertThatExceptionOfType(AccessDeniedException ::class .java).isThrownBy {
206
+ runBlocking {
207
+ messageService!! .flowPreAuthorizeDelegate().collect()
208
+ }
209
+ }
210
+ verify { delegate wasNot Called }
211
+ }
212
+
166
213
@EnableReactiveMethodSecurity
167
214
@Configuration
168
215
open class Config {
216
+ var delegate = mockk<KotlinReactiveMessageService >()
169
217
170
218
@Bean
171
219
open fun messageService (): KotlinReactiveMessageServiceImpl {
172
- return KotlinReactiveMessageServiceImpl ()
220
+ return KotlinReactiveMessageServiceImpl (this .delegate )
173
221
}
174
222
175
223
@Bean
0 commit comments