Skip to content

Commit

Permalink
Add Test to cover usecase user with multiple roles
Browse files Browse the repository at this point in the history
  • Loading branch information
assadriaz committed Oct 18, 2024
1 parent 932a0a9 commit 0ff27b4
Showing 1 changed file with 90 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
import static org.rutebanken.helper.organisation.AuthorizationConstants.ENTITY_TYPE;
import static org.rutebanken.helper.organisation.AuthorizationConstants.ROLE_DELETE_STOPS;
import static org.rutebanken.helper.organisation.AuthorizationConstants.ROLE_EDIT_STOPS;

@Transactional // Because of the authorization service logs entities which could read lazy loaded fields
Expand Down Expand Up @@ -256,12 +257,6 @@ public void authorizedGetBannedStopPlaceTypesTest() {
assertThat("Should not contain banned stop place type", bannedStopPlaceTypesOutside.isEmpty(), is(true));
}

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

/**
* Test real life example from ninkasi
*/
Expand Down Expand Up @@ -311,6 +306,56 @@ public void testNSBEditStopsRoleAssignmentsBusStation() {
assertThat("bus station not allowed when sub mode not set", authorized, is(false));
}

/**
* Test if user has multiple roles
* A User has roles for editing stops TopographicPlace 1 and 2
*/
@Test
public void testUserWithMultipleRoles() {
final List<RoleAssignment> roleAssignments = roleAssignmentsMultipleRoles();

Point point = geometryFactory.createPoint(new Coordinate(9.536819, 61.772281));
Point point2 = geometryFactory.createPoint(new Coordinate(23.682516, 70.664175));

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

TopographicPlace municipality2 = new TopographicPlace();
municipality2.setNetexId("KVE:TopographicalPlace:02");
municipality2.setVersion(1);
municipality2.setPolygon(createPolygon(point2));
topographicPlaceRepository.saveAndFlush(municipality2);

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

StopPlace stopPlace2 = new StopPlace();
stopPlace2.setStopPlaceType(StopTypeEnumeration.BUS_STATION);
stopPlace2.setBusSubmode(BusSubmodeEnumeration.REGIONAL_BUS);
stopPlace2.setTopographicPlace(municipality2);
stopPlace2.setCentroid(point2);

stopPlaceRepository.saveAndFlush(stopPlace2);

mockedRoleAssignmentExtractor.setNextReturnedRoleAssignment(roleAssignments);
assertThat("Should be authorized as both type and submode are allowed", authorizationService.canEditEntity(stopPlace), is(true));
mockedRoleAssignmentExtractor.setNextReturnedRoleAssignment(roleAssignments);
assertThat("Should be authorized as both type and submode are allowed", authorizationService.canDeleteEntity(stopPlace), is(true));

mockedRoleAssignmentExtractor.setNextReturnedRoleAssignment(roleAssignments);
assertThat("Should be authorized as both type and submode are allowed", authorizationService.canEditEntity(stopPlace2), is(true));
mockedRoleAssignmentExtractor.setNextReturnedRoleAssignment(roleAssignments);
assertThat("Should be authorized as both type and submode are allowed", authorizationService.canDeleteEntity(stopPlace2), is(false));

}

private List<RoleAssignment> roleAssignmentsForRailAndRailReplacementMocked(String role) {
List<RoleAssignment> roleAssignments = Arrays.asList(RoleAssignment.builder().withRole(role)
.withOrganisation("NSB")
Expand All @@ -326,4 +371,43 @@ private List<RoleAssignment> roleAssignmentsForRailAndRailReplacementMocked(Stri
return roleAssignments;
}

private List<RoleAssignment> roleAssignmentsMultipleRoles() {
List<RoleAssignment> roleAssignments = Arrays.asList(RoleAssignment.builder()
.withRole(ROLE_EDIT_STOPS)
.withOrganisation("OST")
.withAdministrativeZone("KVE:TopographicalPlace:01")
.withEntityClassification(ENTITY_TYPE, "StopPlace")
.withEntityClassification("StopPlaceType", "!airport")
.withEntityClassification("StopPlaceType", "!railStation")
.withEntityClassification("Submode", "!railReplacementBus")
.build(),
RoleAssignment.builder()
.withRole(ROLE_DELETE_STOPS)
.withOrganisation("OST")
.withAdministrativeZone("KVE:TopographicalPlace:01")
.withEntityClassification(ENTITY_TYPE, "StopPlace")
.withEntityClassification("StopPlaceType", "!airport")
.withEntityClassification("StopPlaceType", "!railStation")
.withEntityClassification("Submode", "!railReplacementBus")
.build(),
RoleAssignment.builder()
.withRole(ROLE_EDIT_STOPS)
.withOrganisation("OST")
.withAdministrativeZone("KVE:TopographicalPlace:02")
.withEntityClassification(ENTITY_TYPE, "StopPlace")
.withEntityClassification("StopPlaceType", "!airport")
.withEntityClassification("StopPlaceType", "!railStation")
.withEntityClassification("Submode", "!railReplacementBus")
.build()
);

return roleAssignments;
}

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

}

0 comments on commit 0ff27b4

Please sign in to comment.