UserAccessExpressionEvaluator implements ExpressionEvaluator { @Override public boolean hasPermissionToApplyToProject(Authentication authentication, User user, String projectId) { if(projectDoesNotExistOrHasLobIndependentStatus(projectId)) { return true; } var project = projectService.getProjectById(projectId); if(applicationService.userHasAppliedForProject(user, project)) { return true; } if(userService.userIsManager(user)) { return true; } if(!userAccessService.userHasActiveAccessInterval(user)) { return false; } var projectLob = project.getLob(); var userLob = userService.getUserData(user).getLob(); return projectLob == null || projectLob.equalsIgnoreCase(userLob) || bookmarkService.userHasBookmark(user, project); } @Autowired  UserAccessExpressionEvaluator(UserService userService,
                                         UserAccessService userAccessService,
                                         ProjectService projectService,
                                         ApplicationService applicationService,
                                         BookmarkService bookmarkService,
                                         ProjectBoardConfigurationProperties properties); @Override boolean hasAccessToProjects(Authentication authentication, User user); @Override boolean hasAccessToProject(Authentication authentication, User user, String projectId); @Override boolean hasPermissionToApplyToProject(Authentication authentication, User user, String projectId); @Override boolean hasPermissionToAccessUser(Authentication authentication, User user, String userId); @Override boolean hasElevatedAccessToUser(Authentication authentication, User user, String userId);  }
@Test public void hasPersmissionToApplyToProjectReturnsTrueWhenProjectDoesNotExist() { given(projectServiceMock.projectExists(PROJECT_ID)).willReturn(false); boolean actualHasPermission = evaluator.hasPermissionToApplyToProject(authenticationMock, userMock, PROJECT_ID); assertThat(actualHasPermission).isTrue(); }
@Test public void hasPermissionToApplyToProjectReturnsTrueWhenStatusIsLobIndependent() { given(projectServiceMock.projectExists(PROJECT_ID)).willReturn(true); given(projectServiceMock.getProjectById(PROJECT_ID)).willReturn(projectMock); given(projectMock.getStatus()).willReturn(LOB_INDEPENDENT_STATUS); boolean actualHasPermission = evaluator.hasPermissionToApplyToProject(authenticationMock, userMock, PROJECT_ID); assertThat(actualHasPermission).isTrue(); }
@Test public void hasPermissionToApplyToProjectReturnsTrueWhenUserHasAppliedForProject() { given(projectServiceMock.projectExists(PROJECT_ID)).willReturn(true); given(projectServiceMock.getProjectById(PROJECT_ID)).willReturn(projectMock); given(projectMock.getStatus()).willReturn(LOB_DEPENDENT_STATUS); given(applicationServiceMock.userHasAppliedForProject(userMock, projectMock)).willReturn(true); boolean actualHasPermission = evaluator.hasPermissionToApplyToProject(authenticationMock, userMock, PROJECT_ID); assertThat(actualHasPermission).isTrue(); }
@Test public void hasPermissionToApplyToProjectReturnsTrueWhenStatusIsLobDependentAndUserIsManager() { given(projectServiceMock.projectExists(PROJECT_ID)).willReturn(true); given(projectServiceMock.getProjectById(PROJECT_ID)).willReturn(projectMock); given(projectMock.getStatus()).willReturn(LOB_DEPENDENT_STATUS); given(userServiceMock.userIsManager(userMock)).willReturn(true); boolean actualHasPermission = evaluator.hasPermissionToApplyToProject(authenticationMock, userMock, PROJECT_ID); assertThat(actualHasPermission).isTrue(); }
@Test public void hasPermissionToApplyToProjectReturnsFalseWhenUserHasNoActiveAccessInterval() { given(projectServiceMock.projectExists(PROJECT_ID)).willReturn(true); given(projectServiceMock.getProjectById(PROJECT_ID)).willReturn(projectMock); given(projectMock.getStatus()).willReturn(LOB_DEPENDENT_STATUS); given(userAccessServiceMock.userHasActiveAccessInterval(userMock)).willReturn(false); boolean actualHasPermission = evaluator.hasPermissionToApplyToProject(authenticationMock, userMock, PROJECT_ID); assertThat(actualHasPermission).isFalse(); }
@Test public void hasPermissionToApplyToProjectReturnsFalseWhenProjectLobNotEqualToUserLobAndUserHasNotBookmarkedProject() { var projectLob = "proj-lob"; var userLob = "user-lob"; given(projectServiceMock.projectExists(PROJECT_ID)).willReturn(true); given(projectServiceMock.getProjectById(PROJECT_ID)).willReturn(projectMock); given(projectMock.getStatus()).willReturn(LOB_DEPENDENT_STATUS); given(projectMock.getLob()).willReturn(projectLob); given(userAccessServiceMock.userHasActiveAccessInterval(userMock)).willReturn(true); given(userServiceMock.getUserData(userMock)).willReturn(userDataMock); given(userDataMock.getLob()).willReturn(userLob); boolean actualHasPermission = evaluator.hasPermissionToApplyToProject(authenticationMock, userMock, PROJECT_ID); assertThat(actualHasPermission).isFalse(); }
@Test public void hasPermissionToApplyToProjectReturnsTrueWhenProjectLobNull() { var userLob = "user-lob"; given(projectServiceMock.projectExists(PROJECT_ID)).willReturn(true); given(projectServiceMock.getProjectById(PROJECT_ID)).willReturn(projectMock); given(projectMock.getStatus()).willReturn(LOB_DEPENDENT_STATUS); given(projectMock.getLob()).willReturn(null); given(userAccessServiceMock.userHasActiveAccessInterval(userMock)).willReturn(true); given(userServiceMock.getUserData(userMock)).willReturn(userDataMock); given(userDataMock.getLob()).willReturn(userLob); boolean actualHasPermission = evaluator.hasPermissionToApplyToProject(authenticationMock, userMock, PROJECT_ID); assertThat(actualHasPermission).isTrue(); }
@Test public void hasPermissionToApplyToProjectReturnsTrueWhenProjectLobAndUserLobAreEqual() { var allLob = "user-lob"; given(projectServiceMock.projectExists(PROJECT_ID)).willReturn(true); given(projectServiceMock.getProjectById(PROJECT_ID)).willReturn(projectMock); given(projectMock.getStatus()).willReturn(LOB_DEPENDENT_STATUS); given(projectMock.getLob()).willReturn(allLob); given(userAccessServiceMock.userHasActiveAccessInterval(userMock)).willReturn(true); given(userServiceMock.getUserData(userMock)).willReturn(userDataMock); given(userDataMock.getLob()).willReturn(allLob); boolean actualHasPermission = evaluator.hasPermissionToApplyToProject(authenticationMock, userMock, PROJECT_ID); assertThat(actualHasPermission).isTrue(); }
@Test public void hasPermissionToApplyToProjectReturnsTrueWhenProjectLobAndUserLobDifferButUserBookmarkedProject() { var projectLob = "proj-lob"; var userLob = "user-lob"; given(projectServiceMock.projectExists(PROJECT_ID)).willReturn(true); given(projectServiceMock.getProjectById(PROJECT_ID)).willReturn(projectMock); given(projectMock.getStatus()).willReturn(LOB_DEPENDENT_STATUS); given(projectMock.getLob()).willReturn(projectLob); given(userAccessServiceMock.userHasActiveAccessInterval(userMock)).willReturn(true); given(userServiceMock.getUserData(userMock)).willReturn(userDataMock); given(userDataMock.getLob()).willReturn(userLob); given(bookmarkServiceMock.userHasBookmark(userMock, projectMock)).willReturn(true); boolean actualHasPermission = evaluator.hasPermissionToApplyToProject(authenticationMock, userMock, PROJECT_ID); assertThat(actualHasPermission).isTrue(); }