Skip to content

Commit

Permalink
Refactoring add test
Browse files Browse the repository at this point in the history
  • Loading branch information
assadriaz committed Oct 8, 2024
1 parent 2db1415 commit bc9c1b2
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@ public interface AuthorizationService {
*/
<T extends EntityStructure> boolean canEditEntity(RoleAssignment roleAssignment, T entity);

Set<String> getAllowedStopPlaceTypes();
Set<String> getAllowedStopPlaceTypes(Object entity);

Set<String> getBannedStopPlaceTypes();
Set<String> getBannedStopPlaceTypes(Object entity);

Set<String> getAllowedSubmodes();
Set<String> getAllowedSubmodes(Object entity);

Set<String> getBannedSubmodes();
Set<String> getBannedSubmodes(Object entity);



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.rutebanken.helper.organisation.DataScopedAuthorizationService;
import org.rutebanken.helper.organisation.RoleAssignment;
import org.rutebanken.helper.organisation.RoleAssignmentExtractor;
import org.rutebanken.tiamat.auth.check.TopographicPlaceChecker;
import org.rutebanken.tiamat.model.EntityStructure;
import org.springframework.security.access.AccessDeniedException;

Expand All @@ -22,11 +23,15 @@ public class DefaultAuthorizationService implements AuthorizationService {
private final RoleAssignmentExtractor roleAssignmentExtractor;
private static final String STOP_PLACE_TYPE = "StopPlaceType";
private static final String SUBMODE = "Submode";
private final TopographicPlaceChecker topographicPlaceChecker;

public DefaultAuthorizationService(DataScopedAuthorizationService dataScopedAuthorizationService, RoleAssignmentExtractor roleAssignmentExtractor) {
public DefaultAuthorizationService(DataScopedAuthorizationService dataScopedAuthorizationService,
RoleAssignmentExtractor roleAssignmentExtractor,
TopographicPlaceChecker topographicPlaceChecker) {
this.dataScopedAuthorizationService = dataScopedAuthorizationService;
this.roleAssignmentExtractor = roleAssignmentExtractor;
}
this.topographicPlaceChecker = topographicPlaceChecker;
}

@Override
public void verifyCanEditAllEntities() {
Expand Down Expand Up @@ -83,29 +88,30 @@ public boolean canEditEntity(EntityStructure entity) {
}

@Override
public Set<String> getAllowedStopPlaceTypes(){
return getStopTypesOrSubmode(STOP_PLACE_TYPE, true);
public Set<String> getAllowedStopPlaceTypes(Object entity){
return getStopTypesOrSubmode(STOP_PLACE_TYPE, true, entity);
}

@Override
public Set<String> getBannedStopPlaceTypes() {
return getStopTypesOrSubmode(STOP_PLACE_TYPE, false);
public Set<String> getBannedStopPlaceTypes(Object entity) {
return getStopTypesOrSubmode(STOP_PLACE_TYPE, false, entity);
}

@Override
public Set<String> getAllowedSubmodes() {
return getStopTypesOrSubmode(SUBMODE, true);
public Set<String> getAllowedSubmodes(Object entity) {
return getStopTypesOrSubmode(SUBMODE, true, entity);
}

@Override
public Set<String> getBannedSubmodes() {
return getStopTypesOrSubmode(SUBMODE, false);
public Set<String> getBannedSubmodes(Object entity) {
return getStopTypesOrSubmode(SUBMODE, false, entity);
}


private Set<String> getStopTypesOrSubmode(String type, boolean isAllowed) {
private Set<String> getStopTypesOrSubmode(String type, boolean isAllowed, Object entity) {
return roleAssignmentExtractor.getRoleAssignmentsForUser().stream()
.filter(roleAssignment -> roleAssignment.getEntityClassifications() != null)
.filter(roleAssignment -> topographicPlaceChecker.entityMatchesAdministrativeZone(roleAssignment, entity))
.filter(roleAssignment -> roleAssignment.getEntityClassifications().get(type) != null)
.map(roleAssignment -> roleAssignment.getEntityClassifications().get(type))
.flatMap(List::stream)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
import org.rutebanken.helper.organisation.DataScopedAuthorizationService;
import org.rutebanken.helper.organisation.ReflectionAuthorizationService;
import org.rutebanken.helper.organisation.RoleAssignmentExtractor;
import org.rutebanken.tiamat.auth.AuthorizationService;
import org.rutebanken.tiamat.auth.DefaultAuthorizationService;
import org.rutebanken.tiamat.auth.TiamatEntityResolver;
import org.rutebanken.tiamat.auth.check.TiamatOriganisationChecker;
import org.rutebanken.tiamat.auth.check.TopographicPlaceChecker;
import org.rutebanken.tiamat.auth.AuthorizationService;
import org.rutebanken.tiamat.auth.DefaultAuthorizationService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand All @@ -39,8 +39,8 @@ public class AuthorizationServiceConfig {


@Bean
public AuthorizationService authorizationService(DataScopedAuthorizationService dataScopedAuthorizationService, RoleAssignmentExtractor roleAssignmentExtractor) {
return new DefaultAuthorizationService(dataScopedAuthorizationService, roleAssignmentExtractor);
public AuthorizationService authorizationService(DataScopedAuthorizationService dataScopedAuthorizationService, RoleAssignmentExtractor roleAssignmentExtractor, TopographicPlaceChecker topographicPlaceChecker) {
return new DefaultAuthorizationService(dataScopedAuthorizationService, roleAssignmentExtractor, topographicPlaceChecker);
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ public Object get(DataFetchingEnvironment environment) throws Exception {

final boolean canEditEntities = authorizationService.canEditEntity(entityInVersionStructure);
final boolean canDeleteEntity = authorizationService.canDeleteEntity(entityInVersionStructure);
final Set<String> allowedStopPlaceTypes = authorizationService.getAllowedStopPlaceTypes();
final Set<String> bannedStopPlaceTypes = authorizationService.getBannedStopPlaceTypes();
final Set<String> allowedSubmode = authorizationService.getAllowedSubmodes();
final Set<String> bannedSubmode = authorizationService.getBannedSubmodes();
final Set<String> allowedStopPlaceTypes = authorizationService.getAllowedStopPlaceTypes(entityInVersionStructure);
final Set<String> bannedStopPlaceTypes = authorizationService.getBannedStopPlaceTypes(entityInVersionStructure);
final Set<String> allowedSubmode = authorizationService.getAllowedSubmodes(entityInVersionStructure);
final Set<String> bannedSubmode = authorizationService.getBannedSubmodes(entityInVersionStructure);


return new EntityPermissions(canEditEntities, canDeleteEntity, allowedStopPlaceTypes, bannedStopPlaceTypes, allowedSubmode, bannedSubmode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ class DefaultAuthorizationServiceTest {
@Test
void verifyCanEditAllEntities() {
List<RoleAssignment> roleAssignments = RoleAssignmentListBuilder.builder().withAccessAllAreas().build();
DefaultAuthorizationService defaultAuthorizationService = new DefaultAuthorizationService(null, null);
DefaultAuthorizationService defaultAuthorizationService = new DefaultAuthorizationService(null, null, null);
Assertions.assertDoesNotThrow(() -> defaultAuthorizationService.verifyCanEditAllEntities(roleAssignments));
}

@Test
void verifyCanEditAllEntitiesMissingRoleAssignment() {
List<RoleAssignment> roleAssignments = RoleAssignmentListBuilder.builder().build();
DefaultAuthorizationService defaultAuthorizationService = new DefaultAuthorizationService(null, null);
DefaultAuthorizationService defaultAuthorizationService = new DefaultAuthorizationService(null, null, null);
Assertions.assertThrows(AccessDeniedException.class, () -> defaultAuthorizationService.verifyCanEditAllEntities(roleAssignments));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public void StopPlaceAuthorizationServiceTest() {
tiamatOriganisationChecker,
topographicPlaceChecker,
tiamatEntityResolver);
this.authorizationService = authorizationServiceConfig.authorizationService(dataScopedAuthorizationService, roleAssignmentExtractor);
this.authorizationService = authorizationServiceConfig.authorizationService(dataScopedAuthorizationService, roleAssignmentExtractor,topographicPlaceChecker);



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,19 @@
package org.rutebanken.tiamat.auth;

import org.junit.Test;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.LinearRing;
import org.locationtech.jts.geom.Point;
import org.locationtech.jts.geom.Polygon;
import org.locationtech.jts.geom.impl.CoordinateArraySequence;
import org.rutebanken.helper.organisation.RoleAssignment;
import org.rutebanken.tiamat.TiamatIntegrationTest;
import org.rutebanken.tiamat.model.BusSubmodeEnumeration;
import org.rutebanken.tiamat.model.Quay;
import org.rutebanken.tiamat.model.StopPlace;
import org.rutebanken.tiamat.model.StopTypeEnumeration;
import org.rutebanken.tiamat.model.TopographicPlace;
import org.rutebanken.tiamat.model.WaterSubmodeEnumeration;
import org.rutebanken.tiamat.repository.StopPlaceRepository;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -151,9 +158,13 @@ public void authorizedWithSubmodeAndType() {

@Test
public void authorizedGetAllowedStopPlaceTypesTest() {
roleAssignmentsForRailAndRailReplacementMocked(ROLE_EDIT_STOPS);
final List<RoleAssignment> roleAssignments = roleAssignmentsForRailAndRailReplacementMocked(ROLE_EDIT_STOPS);

StopPlace stopPlace = new StopPlace();
stopPlace.setStopPlaceType(StopTypeEnumeration.BUS_STATION);
stopPlace.setBusSubmode(BusSubmodeEnumeration.REGIONAL_BUS);

final Set<String> allowedStopPlaceTypes = authorizationService.getAllowedStopPlaceTypes();
final Set<String> allowedStopPlaceTypes = authorizationService.getAllowedStopPlaceTypes(stopPlace);
assertThat("Should contain allowed StopPlaceType", allowedStopPlaceTypes.contains("railStation"), is(true));
}

Expand All @@ -166,15 +177,43 @@ public void authorizedGetBannedStopPlaceTypesTest() {
RoleAssignment roleAssignment = RoleAssignment.builder()
.withRole(ROLE_EDIT_STOPS)
.withOrganisation("OST")
.withAdministrativeZone("KVE:TopographicalPlace:01")
.withEntityClassification(ENTITY_TYPE, "StopPlace")
.withEntityClassification("StopPlaceType", "!airport")
.withEntityClassification("Submode", "!railReplacementBus")
.build();

mockedRoleAssignmentExtractor.setNextReturnedRoleAssignment(roleAssignment);

final Set<String> bannedStopPlaceTypes = authorizationService.getBannedStopPlaceTypes();
assertThat("Should contain banned StopPlaceType", bannedStopPlaceTypes.contains("airport"), is(true));
Point point = geometryFactory.createPoint(new Coordinate(9.84, 59.26));
Point point2 = geometryFactory.createPoint(new Coordinate(0, 0));

TopographicPlace municipality = new TopographicPlace();
municipality.setNetexId("KVE:TopographicalPlace:01");
municipality.setVersion(1);
municipality.setPolygon(createPolygon(point));
topographicPlaceRepository.saveAndFlush(municipality);



StopPlace stopPlace = new StopPlace();
stopPlace.setStopPlaceType(StopTypeEnumeration.BUS_STATION);
stopPlace.setBusSubmode(BusSubmodeEnumeration.REGIONAL_BUS);
stopPlace.setTopographicPlace(municipality);
stopPlace.setCentroid(point2);
stopPlaceRepository.saveAndFlush(stopPlace);

final Set<String> bannedStopPlaceTypes = authorizationService.getBannedStopPlaceTypes(stopPlace);
assertThat("Should contain banned StopPlaceType", bannedStopPlaceTypes.contains("airport"), is(false));
boolean authorized = authorizationService.canEditEntity(roleAssignment, stopPlace);
assertThat("Should be authorized as both type and submode are allowed", authorized, is(true));

}

private Polygon createPolygon(Point point) {
Geometry bufferedPoint = point.buffer(20);
LinearRing linearRing = new LinearRing(new CoordinateArraySequence(bufferedPoint.getCoordinates()), geometryFactory);
return geometryFactory.createPolygon(linearRing, null);
}

/**
Expand Down

0 comments on commit bc9c1b2

Please sign in to comment.