|
| 1 | +/* |
| 2 | + * Copyright 2002-2025 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.data.repository.query; |
| 18 | + |
| 19 | +import java.util.List; |
| 20 | + |
| 21 | +import org.junit.jupiter.api.Test; |
| 22 | + |
| 23 | +import org.springframework.data.domain.PageImpl; |
| 24 | +import org.springframework.data.domain.SliceImpl; |
| 25 | +import org.springframework.data.geo.Distance; |
| 26 | +import org.springframework.data.geo.GeoPage; |
| 27 | +import org.springframework.data.geo.GeoResult; |
| 28 | +import org.springframework.data.geo.GeoResults; |
| 29 | +import org.springframework.security.authorization.method.AuthorizationAdvisorProxyFactory; |
| 30 | +import org.springframework.security.authorization.method.AuthorizationProxy; |
| 31 | + |
| 32 | +import static org.assertj.core.api.Assertions.assertThat; |
| 33 | +import static org.assertj.core.api.InstanceOfAssertFactories.list; |
| 34 | +import static org.assertj.core.api.InstanceOfAssertFactories.type; |
| 35 | + |
| 36 | +/** |
| 37 | + * Tests for {@link DataSecurityContainerTypeVisitor}. |
| 38 | + * |
| 39 | + * @author Evgeniy Cheban |
| 40 | + */ |
| 41 | +class DataSecurityContainerTypeVisitorTests { |
| 42 | + |
| 43 | + private final DataSecurityContainerTypeVisitor visitor = new DataSecurityContainerTypeVisitor(); |
| 44 | + |
| 45 | + @Test |
| 46 | + void visitWhenTargetGeoResultsThenProxied() { |
| 47 | + AuthorizationAdvisorProxyFactory proxyFactory = AuthorizationAdvisorProxyFactory.withDefaults(); |
| 48 | + proxyFactory.setTargetVisitor(AuthorizationAdvisorProxyFactory.TargetVisitor.of(this.visitor, |
| 49 | + AuthorizationAdvisorProxyFactory.TargetVisitor.defaults())); |
| 50 | + GeoResults<AuthorizedObject> geoResults = getGeoResults(); |
| 51 | + Object result = this.visitor.visit(proxyFactory, geoResults); |
| 52 | + assertThat(result).asInstanceOf(type(GeoResults.class)) |
| 53 | + .extracting(GeoResults::getContent, list(GeoResult.class)) |
| 54 | + .extracting(GeoResult::getContent) |
| 55 | + .asInstanceOf(list(AuthorizedObject.class)) |
| 56 | + .allSatisfy((o) -> assertThat(o).isInstanceOf(AuthorizationProxy.class)) |
| 57 | + .extracting(AuthorizedObject::getValue) |
| 58 | + .containsExactly("test1", "test2", "test3"); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + void visitWhenTargetGeoPageThenProxied() { |
| 63 | + AuthorizationAdvisorProxyFactory proxyFactory = AuthorizationAdvisorProxyFactory.withDefaults(); |
| 64 | + proxyFactory.setTargetVisitor(AuthorizationAdvisorProxyFactory.TargetVisitor.of(this.visitor, |
| 65 | + AuthorizationAdvisorProxyFactory.TargetVisitor.defaults())); |
| 66 | + GeoPage<AuthorizedObject> geoPage = new GeoPage<>(getGeoResults()); |
| 67 | + Object result = this.visitor.visit(proxyFactory, geoPage); |
| 68 | + assertThat(result).asInstanceOf(type(GeoPage.class)) |
| 69 | + .extracting(GeoPage::getContent, list(GeoResult.class)) |
| 70 | + .extracting(GeoResult::getContent) |
| 71 | + .asInstanceOf(list(AuthorizedObject.class)) |
| 72 | + .allSatisfy((o) -> assertThat(o).isInstanceOf(AuthorizationProxy.class)) |
| 73 | + .extracting(AuthorizedObject::getValue) |
| 74 | + .containsExactly("test1", "test2", "test3"); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + void visitWhenTargetGeoResultThenProxied() { |
| 79 | + AuthorizationAdvisorProxyFactory proxyFactory = AuthorizationAdvisorProxyFactory.withDefaults(); |
| 80 | + proxyFactory.setTargetVisitor(AuthorizationAdvisorProxyFactory.TargetVisitor.of(this.visitor, |
| 81 | + AuthorizationAdvisorProxyFactory.TargetVisitor.defaults())); |
| 82 | + AuthorizedObject authorizedObject = new AuthorizedObject("test1"); |
| 83 | + GeoResult<AuthorizedObject> geoResult = new GeoResult<>(authorizedObject, new Distance(1.0)); |
| 84 | + Object result = this.visitor.visit(proxyFactory, geoResult); |
| 85 | + assertThat(result).asInstanceOf(type(GeoResult.class)) |
| 86 | + .extracting(GeoResult::getContent) |
| 87 | + .asInstanceOf(type(AuthorizedObject.class)) |
| 88 | + .isInstanceOf(AuthorizationProxy.class) |
| 89 | + .extracting(AuthorizedObject::getValue) |
| 90 | + .isEqualTo("test1"); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + void visitWhenTargetPageImplThenProxied() { |
| 95 | + AuthorizationAdvisorProxyFactory proxyFactory = AuthorizationAdvisorProxyFactory.withDefaults(); |
| 96 | + proxyFactory.setTargetVisitor(AuthorizationAdvisorProxyFactory.TargetVisitor.of(this.visitor, |
| 97 | + AuthorizationAdvisorProxyFactory.TargetVisitor.defaults())); |
| 98 | + PageImpl<AuthorizedObject> page = new PageImpl<>(getAuthorizedObjects()); |
| 99 | + Object result = this.visitor.visit(proxyFactory, page); |
| 100 | + assertThat(result).asInstanceOf(type(PageImpl.class)) |
| 101 | + .extracting(PageImpl::getContent, list(AuthorizedObject.class)) |
| 102 | + .allSatisfy((o) -> assertThat(o).isInstanceOf(AuthorizationProxy.class)) |
| 103 | + .extracting(AuthorizedObject::getValue) |
| 104 | + .containsExactly("test1", "test2", "test3"); |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + void visitWhenTargetSliceImplThenProxied() { |
| 109 | + AuthorizationAdvisorProxyFactory proxyFactory = AuthorizationAdvisorProxyFactory.withDefaults(); |
| 110 | + proxyFactory.setTargetVisitor(AuthorizationAdvisorProxyFactory.TargetVisitor.of(this.visitor, |
| 111 | + AuthorizationAdvisorProxyFactory.TargetVisitor.defaults())); |
| 112 | + SliceImpl<AuthorizedObject> page = new SliceImpl<>(getAuthorizedObjects()); |
| 113 | + Object result = this.visitor.visit(proxyFactory, page); |
| 114 | + assertThat(result).asInstanceOf(type(SliceImpl.class)) |
| 115 | + .extracting(SliceImpl::getContent, list(AuthorizedObject.class)) |
| 116 | + .allSatisfy((o) -> assertThat(o).isInstanceOf(AuthorizationProxy.class)) |
| 117 | + .extracting(AuthorizedObject::getValue) |
| 118 | + .containsExactly("test1", "test2", "test3"); |
| 119 | + } |
| 120 | + |
| 121 | + private GeoResults<AuthorizedObject> getGeoResults() { |
| 122 | + AuthorizedObject authorizedObject1 = new AuthorizedObject("test1"); |
| 123 | + AuthorizedObject authorizedObject2 = new AuthorizedObject("test2"); |
| 124 | + AuthorizedObject authorizedObject3 = new AuthorizedObject("test3"); |
| 125 | + GeoResult<AuthorizedObject> geoResult1 = new GeoResult<>(authorizedObject1, new Distance(1.0)); |
| 126 | + GeoResult<AuthorizedObject> geoResult2 = new GeoResult<>(authorizedObject2, new Distance(2.0)); |
| 127 | + GeoResult<AuthorizedObject> geoResult3 = new GeoResult<>(authorizedObject3, new Distance(3.0)); |
| 128 | + List<GeoResult<AuthorizedObject>> results = List.of(geoResult1, geoResult2, geoResult3); |
| 129 | + return new GeoResults<>(results); |
| 130 | + } |
| 131 | + |
| 132 | + private List<AuthorizedObject> getAuthorizedObjects() { |
| 133 | + AuthorizedObject authorizedObject1 = new AuthorizedObject("test1"); |
| 134 | + AuthorizedObject authorizedObject2 = new AuthorizedObject("test2"); |
| 135 | + AuthorizedObject authorizedObject3 = new AuthorizedObject("test3"); |
| 136 | + return List.of(authorizedObject1, authorizedObject2, authorizedObject3); |
| 137 | + } |
| 138 | + |
| 139 | + static class AuthorizedObject { |
| 140 | + |
| 141 | + private final String value; |
| 142 | + |
| 143 | + AuthorizedObject(String value) { |
| 144 | + this.value = value; |
| 145 | + } |
| 146 | + |
| 147 | + String getValue() { |
| 148 | + return this.value; |
| 149 | + } |
| 150 | + |
| 151 | + } |
| 152 | + |
| 153 | +} |
0 commit comments