Search in sources :

Example 6 with NamespaceAuthorization

use of org.finra.herd.model.api.xml.NamespaceAuthorization in project herd by FINRAOS.

the class CurrentUserRestControllerTest method testGetCurrentUser.

@Test
public void testGetCurrentUser() throws Exception {
    // Create a set of test namespace authorizations.
    Set<NamespaceAuthorization> namespaceAuthorizations = new LinkedHashSet<>();
    namespaceAuthorizations.add(new NamespaceAuthorization(NAMESPACE, SUPPORTED_NAMESPACE_PERMISSIONS));
    namespaceAuthorizations.add(new NamespaceAuthorization(NAMESPACE_2, SUPPORTED_NAMESPACE_PERMISSIONS));
    UserAuthorizations userAuthorizations = new UserAuthorizations();
    userAuthorizations.setNamespaceAuthorizations(new ArrayList(namespaceAuthorizations));
    when(currentUserService.getCurrentUser()).thenReturn(userAuthorizations);
    // Get the current user information.
    UserAuthorizations resultUserAuthorizations = currentUserRestController.getCurrentUser();
    // Verify the external calls.
    verify(currentUserService).getCurrentUser();
    verifyNoMoreInteractions(currentUserService);
    // Validate the returned object.
    assertEquals(userAuthorizations, resultUserAuthorizations);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ArrayList(java.util.ArrayList) NamespaceAuthorization(org.finra.herd.model.api.xml.NamespaceAuthorization) UserAuthorizations(org.finra.herd.model.api.xml.UserAuthorizations) Test(org.junit.Test)

Example 7 with NamespaceAuthorization

use of org.finra.herd.model.api.xml.NamespaceAuthorization in project herd by FINRAOS.

the class JobServiceTestHelper method setCurrentUserNamespaceAuthorizations.

/**
 * Sets specified namespace authorizations for the current user by updating the security context.
 *
 * @param namespace the namespace
 * @param namespacePermissions the list of namespace permissions
 */
public void setCurrentUserNamespaceAuthorizations(String namespace, List<NamespacePermissionEnum> namespacePermissions) {
    String username = AbstractServiceTest.USER_ID;
    ApplicationUser applicationUser = new ApplicationUser(getClass());
    applicationUser.setUserId(username);
    Set<NamespaceAuthorization> namespaceAuthorizations = new LinkedHashSet<>();
    namespaceAuthorizations.add(new NamespaceAuthorization(namespace, namespacePermissions));
    applicationUser.setNamespaceAuthorizations(namespaceAuthorizations);
    SecurityContextHolder.getContext().setAuthentication(new TestingAuthenticationToken(new SecurityUserWrapper(username, "password", false, false, false, false, Collections.emptyList(), applicationUser), null));
}
Also used : ApplicationUser(org.finra.herd.model.dto.ApplicationUser) LinkedHashSet(java.util.LinkedHashSet) SecurityUserWrapper(org.finra.herd.model.dto.SecurityUserWrapper) NamespaceAuthorization(org.finra.herd.model.api.xml.NamespaceAuthorization) TestingAuthenticationToken(org.springframework.security.authentication.TestingAuthenticationToken)

Example 8 with NamespaceAuthorization

use of org.finra.herd.model.api.xml.NamespaceAuthorization in project herd by FINRAOS.

the class JobServiceTest method testGetJobAssertNoErrorGivenJobRunningAndUserDoesHasPermissions.

@Test
public void testGetJobAssertNoErrorGivenJobRunningAndUserDoesHasPermissions() throws Exception {
    jobDefinitionServiceTestHelper.createJobDefinition(ACTIVITI_XML_TEST_USER_TASK_WITH_CLASSPATH);
    Job job = jobService.createAndStartJob(jobServiceTestHelper.createJobCreateRequest(TEST_ACTIVITI_NAMESPACE_CD, TEST_ACTIVITI_JOB_NAME));
    String username = "username";
    ApplicationUser applicationUser = new ApplicationUser(getClass());
    applicationUser.setUserId(username);
    applicationUser.setNamespaceAuthorizations(new HashSet<>());
    applicationUser.getNamespaceAuthorizations().add(new NamespaceAuthorization(TEST_ACTIVITI_NAMESPACE_CD, Arrays.asList(NamespacePermissionEnum.READ)));
    SecurityContextHolder.getContext().setAuthentication(new TestingAuthenticationToken(new SecurityUserWrapper(username, "password", false, false, false, false, Collections.emptyList(), applicationUser), null));
    try {
        jobService.getJob(job.getId(), false);
    } catch (AccessDeniedException e) {
        fail();
    }
}
Also used : ApplicationUser(org.finra.herd.model.dto.ApplicationUser) AccessDeniedException(org.springframework.security.access.AccessDeniedException) SecurityUserWrapper(org.finra.herd.model.dto.SecurityUserWrapper) NamespaceAuthorization(org.finra.herd.model.api.xml.NamespaceAuthorization) Job(org.finra.herd.model.api.xml.Job) TestingAuthenticationToken(org.springframework.security.authentication.TestingAuthenticationToken) Test(org.junit.Test)

Example 9 with NamespaceAuthorization

use of org.finra.herd.model.api.xml.NamespaceAuthorization in project herd by FINRAOS.

the class JobServiceTest method testGetJobAssertNoErrorGivenJobCompletedAndUserDoesHasPermissions.

@Test
public void testGetJobAssertNoErrorGivenJobCompletedAndUserDoesHasPermissions() throws Exception {
    jobDefinitionServiceTestHelper.createJobDefinition(null);
    Job job = jobService.createAndStartJob(jobServiceTestHelper.createJobCreateRequest(TEST_ACTIVITI_NAMESPACE_CD, TEST_ACTIVITI_JOB_NAME));
    String username = "username";
    ApplicationUser applicationUser = new ApplicationUser(getClass());
    applicationUser.setUserId(username);
    applicationUser.setNamespaceAuthorizations(new HashSet<>());
    applicationUser.getNamespaceAuthorizations().add(new NamespaceAuthorization(TEST_ACTIVITI_NAMESPACE_CD, Arrays.asList(NamespacePermissionEnum.READ)));
    SecurityContextHolder.getContext().setAuthentication(new TestingAuthenticationToken(new SecurityUserWrapper(username, "password", false, false, false, false, Collections.emptyList(), applicationUser), null));
    try {
        jobService.getJob(job.getId(), false);
    } catch (AccessDeniedException e) {
        fail();
    }
}
Also used : ApplicationUser(org.finra.herd.model.dto.ApplicationUser) AccessDeniedException(org.springframework.security.access.AccessDeniedException) SecurityUserWrapper(org.finra.herd.model.dto.SecurityUserWrapper) NamespaceAuthorization(org.finra.herd.model.api.xml.NamespaceAuthorization) Job(org.finra.herd.model.api.xml.Job) TestingAuthenticationToken(org.springframework.security.authentication.TestingAuthenticationToken) Test(org.junit.Test)

Example 10 with NamespaceAuthorization

use of org.finra.herd.model.api.xml.NamespaceAuthorization in project herd by FINRAOS.

the class CurrentUserServiceTest method testGetCurrentUser.

@Test
public void testGetCurrentUser() throws Exception {
    // Create a set of test namespace authorizations.
    Set<NamespaceAuthorization> namespaceAuthorizations = new LinkedHashSet<>();
    namespaceAuthorizations.add(new NamespaceAuthorization(NAMESPACE, SUPPORTED_NAMESPACE_PERMISSIONS));
    namespaceAuthorizations.add(new NamespaceAuthorization(NAMESPACE_2, SUPPORTED_NAMESPACE_PERMISSIONS));
    // Create test roles
    List<SecurityRoleEntity> securityRoleEntities = securityRoleDaoTestHelper.createTestSecurityRoles();
    // Fetch the security role codes to add to the application user.
    Set<String> roles = securityRoleEntities.stream().map(SecurityRoleEntity::getCode).collect(Collectors.toSet());
    // Override the security context to return an application user populated with test values.
    Authentication originalAuthentication = SecurityContextHolder.getContext().getAuthentication();
    try {
        SecurityContextHolder.getContext().setAuthentication(new Authentication() {

            @Override
            public String getName() {
                return null;
            }

            @Override
            public void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException {
            }

            @Override
            public boolean isAuthenticated() {
                return false;
            }

            @Override
            public Object getPrincipal() {
                List<SimpleGrantedAuthority> authorities = Arrays.asList(new SimpleGrantedAuthority(SECURITY_FUNCTION), new SimpleGrantedAuthority(SECURITY_FUNCTION_2));
                ApplicationUser applicationUser = new ApplicationUser(this.getClass());
                applicationUser.setUserId(USER_ID);
                applicationUser.setRoles(roles);
                applicationUser.setNamespaceAuthorizations(namespaceAuthorizations);
                return new SecurityUserWrapper(USER_ID, STRING_VALUE, true, true, true, true, authorities, applicationUser);
            }

            @Override
            public Object getDetails() {
                return null;
            }

            @Override
            public Object getCredentials() {
                return null;
            }

            @Override
            public Collection<? extends GrantedAuthority> getAuthorities() {
                return null;
            }
        });
        // Get the current user information.
        UserAuthorizations userAuthorizations = currentUserService.getCurrentUser();
        // Validate the response object.
        assertEquals(new UserAuthorizations(USER_ID, new ArrayList<>(namespaceAuthorizations), new ArrayList<>(roles), Arrays.asList(SECURITY_FUNCTION, SECURITY_FUNCTION_2)), userAuthorizations);
    } finally {
        // Restore the original authentication.
        SecurityContextHolder.getContext().setAuthentication(originalAuthentication);
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ApplicationUser(org.finra.herd.model.dto.ApplicationUser) SecurityUserWrapper(org.finra.herd.model.dto.SecurityUserWrapper) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) GrantedAuthority(org.springframework.security.core.GrantedAuthority) ArrayList(java.util.ArrayList) NamespaceAuthorization(org.finra.herd.model.api.xml.NamespaceAuthorization) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) Authentication(org.springframework.security.core.Authentication) Collection(java.util.Collection) SecurityRoleEntity(org.finra.herd.model.jpa.SecurityRoleEntity) ArrayList(java.util.ArrayList) List(java.util.List) UserAuthorizations(org.finra.herd.model.api.xml.UserAuthorizations) Test(org.junit.Test)

Aggregations

NamespaceAuthorization (org.finra.herd.model.api.xml.NamespaceAuthorization)33 Test (org.junit.Test)29 ApplicationUser (org.finra.herd.model.dto.ApplicationUser)25 SecurityUserWrapper (org.finra.herd.model.dto.SecurityUserWrapper)22 TestingAuthenticationToken (org.springframework.security.authentication.TestingAuthenticationToken)20 AccessDeniedException (org.springframework.security.access.AccessDeniedException)17 Method (java.lang.reflect.Method)14 JoinPoint (org.aspectj.lang.JoinPoint)14 MethodSignature (org.aspectj.lang.reflect.MethodSignature)14 AbstractServiceTest (org.finra.herd.service.AbstractServiceTest)14 LinkedHashSet (java.util.LinkedHashSet)7 ArrayList (java.util.ArrayList)5 HashSet (java.util.HashSet)5 AbstractAppTest (org.finra.herd.app.AbstractAppTest)5 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)5 MockFilterChain (org.springframework.mock.web.MockFilterChain)4 MockFilterConfig (org.springframework.mock.web.MockFilterConfig)4 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)4 Job (org.finra.herd.model.api.xml.Job)3 UserAuthorizations (org.finra.herd.model.api.xml.UserAuthorizations)3