|
| 1 | +/* |
| 2 | + * Copyright 2002-2024 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.security.access.expression.method |
| 18 | + |
| 19 | +import io.mockk.every |
| 20 | +import io.mockk.mockk |
| 21 | +import org.aopalliance.intercept.MethodInvocation |
| 22 | +import org.assertj.core.api.Assertions.assertThat |
| 23 | +import org.junit.jupiter.api.BeforeEach |
| 24 | +import org.junit.jupiter.api.Test |
| 25 | +import org.springframework.expression.EvaluationContext |
| 26 | +import org.springframework.expression.Expression |
| 27 | +import org.springframework.security.core.Authentication |
| 28 | +import java.util.stream.Stream |
| 29 | +import kotlin.reflect.jvm.internal.impl.load.kotlin.JvmType |
| 30 | +import kotlin.reflect.jvm.javaMethod |
| 31 | + |
| 32 | +/** |
| 33 | + * @author Blagoja Stamatovski |
| 34 | + */ |
| 35 | +class DefaultMethodSecurityExpressionHandlerKotlinTests { |
| 36 | + private object Foo { |
| 37 | + fun bar() { |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + private lateinit var authentication: Authentication |
| 42 | + private lateinit var methodInvocation: MethodInvocation |
| 43 | + |
| 44 | + private val handler: MethodSecurityExpressionHandler = DefaultMethodSecurityExpressionHandler() |
| 45 | + |
| 46 | + @BeforeEach |
| 47 | + fun setUp() { |
| 48 | + authentication = mockk() |
| 49 | + methodInvocation = mockk() |
| 50 | + |
| 51 | + every { methodInvocation.`this` } returns { Foo } |
| 52 | + every { methodInvocation.method } answers { Foo::bar.javaMethod!! } |
| 53 | + every { methodInvocation.arguments } answers { arrayOf<JvmType.Object>() } |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + fun `filters non-empty maps`() { |
| 58 | + val expression: Expression = handler.expressionParser.parseExpression("filterObject.key eq 'key2'") |
| 59 | + val context: EvaluationContext = handler.createEvaluationContext( |
| 60 | + /* authentication = */ authentication, |
| 61 | + /* invocation = */ methodInvocation, |
| 62 | + ) |
| 63 | + val nonEmptyMap: Map<String, String> = mapOf( |
| 64 | + "key1" to "value1", |
| 65 | + "key2" to "value2", |
| 66 | + "key3" to "value3", |
| 67 | + ) |
| 68 | + |
| 69 | + val filtered: Any = handler.filter( |
| 70 | + /* filterTarget = */ nonEmptyMap, |
| 71 | + /* filterExpression = */ expression, |
| 72 | + /* ctx = */ context, |
| 73 | + ) |
| 74 | + |
| 75 | + assertThat(filtered).isInstanceOf(Map::class.java) |
| 76 | + val result = (filtered as Map<String, String>) |
| 77 | + assertThat(result).hasSize(1) |
| 78 | + assertThat(result).containsKey("key2") |
| 79 | + assertThat(result).containsValue("value2") |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + fun `filters empty maps`() { |
| 84 | + val expression: Expression = handler.expressionParser.parseExpression("filterObject.key eq 'key2'") |
| 85 | + val context: EvaluationContext = handler.createEvaluationContext( |
| 86 | + /* authentication = */ authentication, |
| 87 | + /* invocation = */ methodInvocation, |
| 88 | + ) |
| 89 | + val emptyMap: Map<String, String> = emptyMap() |
| 90 | + |
| 91 | + val filtered: Any = handler.filter( |
| 92 | + /* filterTarget = */ emptyMap, |
| 93 | + /* filterExpression = */ expression, |
| 94 | + /* ctx = */ context, |
| 95 | + ) |
| 96 | + |
| 97 | + assertThat(filtered).isInstanceOf(Map::class.java) |
| 98 | + val result = (filtered as Map<String, String>) |
| 99 | + assertThat(result).hasSize(0) |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + fun `filters non-empty collections`() { |
| 104 | + val expression: Expression = handler.expressionParser.parseExpression("filterObject eq 'string2'") |
| 105 | + val context: EvaluationContext = handler.createEvaluationContext( |
| 106 | + /* authentication = */ authentication, |
| 107 | + /* invocation = */ methodInvocation, |
| 108 | + ) |
| 109 | + val nonEmptyCollection: Collection<String> = listOf( |
| 110 | + "string1", |
| 111 | + "string2", |
| 112 | + "string1", |
| 113 | + ) |
| 114 | + |
| 115 | + val filtered: Any = handler.filter( |
| 116 | + /* filterTarget = */ nonEmptyCollection, |
| 117 | + /* filterExpression = */ expression, |
| 118 | + /* ctx = */ context, |
| 119 | + ) |
| 120 | + |
| 121 | + assertThat(filtered).isInstanceOf(Collection::class.java) |
| 122 | + val result = (filtered as Collection<String>) |
| 123 | + assertThat(result).hasSize(1) |
| 124 | + assertThat(result).contains("string2") |
| 125 | + } |
| 126 | + |
| 127 | + @Test |
| 128 | + fun `filters empty collections`() { |
| 129 | + val expression: Expression = handler.expressionParser.parseExpression("filterObject eq 'string2'") |
| 130 | + val context: EvaluationContext = handler.createEvaluationContext( |
| 131 | + /* authentication = */ authentication, |
| 132 | + /* invocation = */ methodInvocation, |
| 133 | + ) |
| 134 | + val emptyCollection: Collection<String> = emptyList() |
| 135 | + |
| 136 | + val filtered: Any = handler.filter( |
| 137 | + /* filterTarget = */ emptyCollection, |
| 138 | + /* filterExpression = */ expression, |
| 139 | + /* ctx = */ context, |
| 140 | + ) |
| 141 | + |
| 142 | + assertThat(filtered).isInstanceOf(Collection::class.java) |
| 143 | + val result = (filtered as Collection<String>) |
| 144 | + assertThat(result).hasSize(0) |
| 145 | + } |
| 146 | + |
| 147 | + @Test |
| 148 | + fun `filters non-empty arrays`() { |
| 149 | + val expression: Expression = handler.expressionParser.parseExpression("filterObject eq 'string2'") |
| 150 | + val context: EvaluationContext = handler.createEvaluationContext( |
| 151 | + /* authentication = */ authentication, |
| 152 | + /* invocation = */ methodInvocation, |
| 153 | + ) |
| 154 | + val nonEmptyArray: Array<String> = arrayOf( |
| 155 | + "string1", |
| 156 | + "string2", |
| 157 | + "string1", |
| 158 | + ) |
| 159 | + |
| 160 | + val filtered: Any = handler.filter( |
| 161 | + /* filterTarget = */ nonEmptyArray, |
| 162 | + /* filterExpression = */ expression, |
| 163 | + /* ctx = */ context, |
| 164 | + ) |
| 165 | + |
| 166 | + assertThat(filtered).isInstanceOf(Array<String>::class.java) |
| 167 | + val result = (filtered as Array<String>) |
| 168 | + assertThat(result).hasSize(1) |
| 169 | + assertThat(result).contains("string2") |
| 170 | + } |
| 171 | + |
| 172 | + @Test |
| 173 | + fun `filters empty arrays`() { |
| 174 | + val expression: Expression = handler.expressionParser.parseExpression("filterObject eq 'string2'") |
| 175 | + val context: EvaluationContext = handler.createEvaluationContext( |
| 176 | + /* authentication = */ authentication, |
| 177 | + /* invocation = */ methodInvocation, |
| 178 | + ) |
| 179 | + val emptyArray: Array<String> = emptyArray() |
| 180 | + |
| 181 | + val filtered: Any = handler.filter( |
| 182 | + /* filterTarget = */ emptyArray, |
| 183 | + /* filterExpression = */ expression, |
| 184 | + /* ctx = */ context, |
| 185 | + ) |
| 186 | + |
| 187 | + assertThat(filtered).isInstanceOf(Array<String>::class.java) |
| 188 | + val result = (filtered as Array<String>) |
| 189 | + assertThat(result).hasSize(0) |
| 190 | + } |
| 191 | + |
| 192 | + @Test |
| 193 | + fun `filters non-empty streams`() { |
| 194 | + val expression: Expression = handler.expressionParser.parseExpression("filterObject eq 'string2'") |
| 195 | + val context: EvaluationContext = handler.createEvaluationContext( |
| 196 | + /* authentication = */ authentication, |
| 197 | + /* invocation = */ methodInvocation, |
| 198 | + ) |
| 199 | + val nonEmptyStream: Stream<String> = listOf( |
| 200 | + "string1", |
| 201 | + "string2", |
| 202 | + "string1", |
| 203 | + ).stream() |
| 204 | + |
| 205 | + val filtered: Any = handler.filter( |
| 206 | + /* filterTarget = */ nonEmptyStream, |
| 207 | + /* filterExpression = */ expression, |
| 208 | + /* ctx = */ context, |
| 209 | + ) |
| 210 | + |
| 211 | + assertThat(filtered).isInstanceOf(Stream::class.java) |
| 212 | + val result = (filtered as Stream<String>).toList() |
| 213 | + assertThat(result).hasSize(1) |
| 214 | + assertThat(result).contains("string2") |
| 215 | + } |
| 216 | + |
| 217 | + @Test |
| 218 | + fun `filters empty streams`() { |
| 219 | + val expression: Expression = handler.expressionParser.parseExpression("filterObject eq 'string2'") |
| 220 | + val context: EvaluationContext = handler.createEvaluationContext( |
| 221 | + /* authentication = */ authentication, |
| 222 | + /* invocation = */ methodInvocation, |
| 223 | + ) |
| 224 | + val emptyStream: Stream<String> = emptyList<String>().stream() |
| 225 | + |
| 226 | + val filtered: Any = handler.filter( |
| 227 | + /* filterTarget = */ emptyStream, |
| 228 | + /* filterExpression = */ expression, |
| 229 | + /* ctx = */ context, |
| 230 | + ) |
| 231 | + |
| 232 | + assertThat(filtered).isInstanceOf(Stream::class.java) |
| 233 | + val result = (filtered as Stream<String>).toList() |
| 234 | + assertThat(result).hasSize(0) |
| 235 | + } |
| 236 | +} |
0 commit comments