http://git-wip-us.apache.org/repos/asf/nifi/blob/f0811ca4/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-api/src/test/groovy/org/apache/nifi/web/StandardNiFiServiceFacadeSpec.groovy ---------------------------------------------------------------------- diff --git a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-api/src/test/groovy/org/apache/nifi/web/StandardNiFiServiceFacadeSpec.groovy b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-api/src/test/groovy/org/apache/nifi/web/StandardNiFiServiceFacadeSpec.groovy index a4c2b37..7b5f594 100644 --- a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-api/src/test/groovy/org/apache/nifi/web/StandardNiFiServiceFacadeSpec.groovy +++ b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-api/src/test/groovy/org/apache/nifi/web/StandardNiFiServiceFacadeSpec.groovy @@ -20,6 +20,7 @@ import org.apache.nifi.authorization.* import org.apache.nifi.authorization.resource.Authorizable import org.apache.nifi.authorization.resource.ResourceFactory import org.apache.nifi.authorization.user.NiFiUser +import org.apache.nifi.authorization.user.NiFiUserDetails import org.apache.nifi.controller.service.ControllerServiceProvider import org.apache.nifi.web.api.dto.* import org.apache.nifi.web.api.entity.UserEntity @@ -28,11 +29,25 @@ import org.apache.nifi.web.dao.AccessPolicyDAO import org.apache.nifi.web.dao.UserDAO import org.apache.nifi.web.dao.UserGroupDAO import org.apache.nifi.web.revision.* +import org.apache.nifi.web.security.token.NiFiAuthenticationToken +import org.springframework.security.core.context.SecurityContextHolder +import spock.lang.Ignore import spock.lang.Specification import spock.lang.Unroll +@Ignore class StandardNiFiServiceFacadeSpec extends Specification { + def setup() { + final NiFiUser user = new NiFiUser("nifi-user"); + final NiFiAuthenticationToken auth = new NiFiAuthenticationToken(new NiFiUserDetails(user)); + SecurityContextHolder.getContext().setAuthentication(auth); + } + + def cleanup() { + SecurityContextHolder.getContext().setAuthentication(null); + } + @Unroll def "CreateUser: isAuthorized: #isAuthorized"() { given: @@ -48,7 +63,7 @@ class StandardNiFiServiceFacadeSpec extends Specification { def newUser = new User.Builder().identifier(userDto.id).identity(userDto.identity).build() when: - def userEntity = niFiServiceFacade.createUser(new Revision(0L, 'client-1'), userDto) + def userEntity = niFiServiceFacade.createUser(new Revision(0L, 'client-1', userDto.id), userDto) then: 1 * userDao.createUser(_) >> newUser @@ -97,19 +112,19 @@ class StandardNiFiServiceFacadeSpec extends Specification { } then: - if (isAuthorized) { - 1 * userDao.getUser(userDto.id) >> requestedUser - } + 1 * userDao.getUser(userDto.id) >> requestedUser 1 * revisionManager.get(_, _) >> { String id, ReadOnlyRevisionCallback callback -> callback.withRevision(new Revision(1L, 'client1', 'root')) } 1 * authorizableLookup.getUsersAuthorizable() >> new SimpleAuthorizable(null, ResourceFactory.getUsersResource(), isAuthorized, authorizationResult) 0 * _ + + assert userEntity != null if (isAuthorized) { - assert userEntity?.component?.id?.equals(userDto.id) + assert userEntity.component?.id?.equals(userDto.id) } else { - assert exception instanceof AccessDeniedException + assert userEntity.component == null } where: @@ -149,6 +164,7 @@ class StandardNiFiServiceFacadeSpec extends Specification { 1 * revisionManager.updateRevision(_, _, _) >> { RevisionClaim revisionClaim, NiFiUser niFiUser, UpdateRevisionTask callback -> callback.update() } + 1 * revisionManager.getRevision(currentRevision.componentId) >> currentRevision.incrementRevision(currentRevision.clientId) } 1 * authorizableLookup.getUsersAuthorizable() >> new SimpleAuthorizable(null, ResourceFactory.getUsersResource(), isAuthorized, authorizationResult) @@ -215,11 +231,11 @@ class StandardNiFiServiceFacadeSpec extends Specification { } where: - userExists | currentRevision | userDto | isAuthorized | authorizationResult - true | new Revision(1L, 'client1') | createUserDTO() | true | AuthorizationResult.approved() - false | null | createUserDTO() | true | AuthorizationResult.approved() - true | new Revision(1L, 'client1') | createUserDTO() | false | AuthorizationResult.denied() - false | null | createUserDTO() | false | AuthorizationResult.denied() + userExists | currentRevision | userDto | isAuthorized | authorizationResult + true | new Revision(1L, 'client1', 'user-1') | createUserDTO() | true | AuthorizationResult.approved() + false | null | createUserDTO() | true | AuthorizationResult.approved() + true | new Revision(1L, 'client1', 'user-1') | createUserDTO() | false | AuthorizationResult.denied() + false | null | createUserDTO() | false | AuthorizationResult.denied() } @Unroll @@ -249,26 +265,22 @@ class StandardNiFiServiceFacadeSpec extends Specification { when: try { - userGroupEntity = niFiServiceFacade.createUserGroup(new Revision(0L, 'client-1'), userGroupDto) + userGroupEntity = niFiServiceFacade.createUserGroup(new Revision(0L, 'client-1', userGroupDto.id), userGroupDto) } catch (AccessDeniedException e) { exception = e } then: - if (isAuthorized) { - 1 * authorizableLookup.getUserGroupsAuthorizable() >> - new SimpleAuthorizable(null, ResourceFactory.userGroupsResource, isAuthorized, authorizationResult.get(ResourceFactory.userGroupsResource)) - } + 1 * authorizableLookup.getUserGroupsAuthorizable() >> + new SimpleAuthorizable(null, ResourceFactory.userGroupsResource, isAuthorized, authorizationResult.get(ResourceFactory.userGroupsResource)) 1 * authorizableLookup.getUsersAuthorizable() >> new SimpleAuthorizable(null, ResourceFactory.usersResource, isAuthorized, authorizationResult.get(ResourceFactory.usersResource)) 1 * userGroupDao.createUserGroup(_) >> newUserGroup - if (authorizationResult.get(ResourceFactory.usersResource) == AuthorizationResult.approved()) { - 1 * userDao.getUser(_) >> { String userId -> - def userEntity = userGroupDto.users.find { it.id.equals(userId) }?.component - assert userEntity != null - new User.Builder().identifier(userEntity.id).identity(userEntity.identity) - .addGroups(userEntity.groups.collect { it.id } as Set) - .build() - } + 1 * userDao.getUser(_) >> { String userId -> + def userEntity = userGroupDto.users.find { it.id.equals(userId) }?.component + assert userEntity != null + new User.Builder().identifier(userEntity.id).identity(userEntity.identity) + .addGroups(userEntity.groups.collect { it.id } as Set) + .build() } userGroupDto.users.size() * revisionManager.get(_, _) >> { String id, ReadOnlyRevisionCallback callback -> assert userGroupDto.users.collect { it.id }.contains(id) @@ -276,6 +288,8 @@ class StandardNiFiServiceFacadeSpec extends Specification { callback.withRevision new Revision(revisionDTO.version, revisionDTO.clientId, id) } 0 * _ + + assert userGroupEntity != null if (isAuthorized) { assert userGroupEntity?.component?.id == userGroupDto.id assert userGroupEntity?.component?.users?.equals(userGroupDto.users) @@ -283,7 +297,6 @@ class StandardNiFiServiceFacadeSpec extends Specification { assert userGroupEntity?.accessPolicy?.canWrite } else { assert userGroupEntity?.component == null - assert exception instanceof AccessDeniedException } @@ -322,27 +335,25 @@ class StandardNiFiServiceFacadeSpec extends Specification { } then: - if (isAuthorized) { - 1 * userGroupDao.getUserGroup(userGroupDto.id) >> requestedUserGroup - 1 * authorizableLookup.getUsersAuthorizable() >> new SimpleAuthorizable(null, ResourceFactory.usersResource, isAuthorized, authorizationResult) - } + 1 * userGroupDao.getUserGroup(userGroupDto.id) >> requestedUserGroup + 1 * authorizableLookup.getUsersAuthorizable() >> new SimpleAuthorizable(null, ResourceFactory.usersResource, isAuthorized, authorizationResult) 1 * authorizableLookup.getUserGroupsAuthorizable() >> new SimpleAuthorizable(null, ResourceFactory.getUserGroupsResource(), isAuthorized, authorizationResult) _ * revisionManager.get(_, _) >> { String id, ReadOnlyRevisionCallback callback -> callback.withRevision(new Revision(1L, 'client1', 'root')) } - if (authorizationResult == AuthorizationResult.approved()) { - 1 * userDao.getUser(_) >> { String userId -> - def userEntity = userGroupDto.users.find { it.id.equals(userId) }?.component - assert userEntity != null - new User.Builder().identifier(userEntity.id).identity(userEntity.identity).build() - } + 1 * userDao.getUser(_) >> { String userId -> + def userEntity = userGroupDto.users.find { it.id.equals(userId) }?.component + assert userEntity != null + new User.Builder().identifier(userEntity.id).identity(userEntity.identity).build() } 0 * _ + + assert userGroupEntity != null if (isAuthorized) { assert userGroupEntity?.component?.id?.equals(userGroupDto.id) } else { - assert exception instanceof AccessDeniedException + assert userGroupEntity.component == null } where: @@ -393,32 +404,30 @@ class StandardNiFiServiceFacadeSpec extends Specification { 1 * revisionManager.updateRevision(_, _, _) >> { RevisionClaim revisionClaim, NiFiUser niFiUser, UpdateRevisionTask callback -> callback.update() } + 1 * revisionManager.getRevision(currentRevision.componentId) >> currentRevision.incrementRevision(currentRevision.clientId) 1 * authorizableLookup.getUsersAuthorizable() >> new SimpleAuthorizable(null, ResourceFactory.usersResource, isAuthorized, authorizationResult.get(ResourceFactory.usersResource)) } - if (isAuthorized || userGroupExists) { - 1 * authorizableLookup.getUserGroupsAuthorizable() >> new SimpleAuthorizable(null, ResourceFactory.userGroupsResource, - isAuthorized, authorizationResult.get(ResourceFactory.userGroupsResource)) - } + 1 * authorizableLookup.getUserGroupsAuthorizable() >> new SimpleAuthorizable(null, ResourceFactory.userGroupsResource, + isAuthorized, authorizationResult.get(ResourceFactory.userGroupsResource)) _ * revisionManager.get(_, _) >> { String id, ReadOnlyRevisionCallback callback -> callback.withRevision(new Revision(1L, 'client1', 'root')) } - if (authorizationResult.get(ResourceFactory.userGroupsResource) == AuthorizationResult.approved()) { - 1 * userDao.getUser(_) >> { String userId -> - def userEntity = userGroupDto.users.find { it.id.equals(userId) }?.component - assert userEntity != null - new User.Builder().identifier(userEntity.id).identity(userEntity.identity).build() - } + 1 * userDao.getUser(_) >> { String userId -> + def userEntity = userGroupDto.users.find { it.id.equals(userId) }?.component + assert userEntity != null + new User.Builder().identifier(userEntity.id).identity(userEntity.identity).build() } 0 * _ def userGroupEntity = userGroupsEntityUpdateResult?.result + + assert userGroupEntity != null if (isAuthorized) { assert userGroupEntity?.component?.id?.equals(userGroupDto.id) assert userGroupEntity?.accessPolicy?.canRead assert userGroupEntity?.accessPolicy?.canWrite } else { - assert userGroupEntity?.component == null - assert exception instanceof AccessDeniedException + assert userGroupEntity.component == null } where: @@ -467,27 +476,23 @@ class StandardNiFiServiceFacadeSpec extends Specification { then: if (userGroupExists) { 1 * userGroupDao.getUserGroup(userGroupDto.id) >> userGroup - if (isAuthorized) { - 1 * userGroupDao.deleteUserGroup(userGroupDto.id) >> userGroup - } + 1 * userGroupDao.deleteUserGroup(userGroupDto.id) >> userGroup 1 * authorizableLookup.getUsersAuthorizable() >> new SimpleAuthorizable(null, ResourceFactory.getUsersResource(), isAuthorized, authorizationResult.get(ResourceFactory.getUsersResource())) } else { 1 * userGroupDao.getUserGroup(userGroupDto.id) >> null 1 * userGroupDao.deleteUserGroup(userGroupDto.id) >> null } - if (!(!isAuthorized && userGroupExists)) { - 1 * authorizableLookup.getUserGroupsAuthorizable() >> new SimpleAuthorizable(null, ResourceFactory.userGroupsResource, - isAuthorized, authorizationResult.get(ResourceFactory.userGroupsResource)) - 1 * revisionManager.deleteRevision(_, _, _) >> { RevisionClaim revisionClaim, NiFiUser nifiUser, DeleteRevisionTask task -> - task.performTask() - } - 1 * controllerFacade.save() + 1 * authorizableLookup.getUserGroupsAuthorizable() >> new SimpleAuthorizable(null, ResourceFactory.userGroupsResource, + isAuthorized, authorizationResult.get(ResourceFactory.userGroupsResource)) + 1 * revisionManager.deleteRevision(_, _, _) >> { RevisionClaim revisionClaim, NiFiUser nifiUser, DeleteRevisionTask task -> + task.performTask() } + 1 * controllerFacade.save() _ * revisionManager.get(_, _) >> { String id, ReadOnlyRevisionCallback callback -> callback.withRevision(new Revision(1L, 'client1', 'root')) } - if (authorizationResult.get(ResourceFactory.userGroupsResource) == AuthorizationResult.approved() && userGroupExists) { + if (userGroupExists) { 1 * userDao.getUser(_) >> { String userId -> def userEntity = userGroupDto.users.find { it.id.equals(userId) }?.component assert userEntity != null @@ -496,14 +501,11 @@ class StandardNiFiServiceFacadeSpec extends Specification { } 0 * _ userGroupEntity?.component?.id == null - if (userGroupExists && isAuthorized) { + if (userGroupExists) { assert userGroupEntity?.id?.equals(userGroupDto.id) } else { assert userGroupEntity?.id == null } - if (authorizationResult.get(ResourceFactory.usersResource) == AuthorizationResult.denied()) { - assert exception instanceof AccessDeniedException - } where: userGroupExists | currentRevision | userGroupDto | isAuthorized | @@ -550,37 +552,34 @@ class StandardNiFiServiceFacadeSpec extends Specification { when: try { - accessPolicyEntity = niFiServiceFacade.createAccessPolicy(new Revision(0L, 'client-1'), accessPolicyDto) + accessPolicyEntity = niFiServiceFacade.createAccessPolicy(new Revision(0L, 'client-1', accessPolicyDto.id), accessPolicyDto) } catch (AccessDeniedException e) { exception = e } then: 1 * accessPolicyDao.createAccessPolicy(accessPolicyDto) >> newAccessPolicy - if (isAuthorized) { - 1 * authorizableLookup.getAccessPolicyAuthorizable(accessPolicyDto.id) >> new SimpleAuthorizable(null, ResourceFactory.getPolicyResource(accessPolicyDto.id), - isAuthorized, authorizationResult) - } + 1 * authorizableLookup.getAccessPolicyAuthorizable(accessPolicyDto.id) >> new SimpleAuthorizable(null, ResourceFactory.getPolicyResource(accessPolicyDto.id), + isAuthorized, authorizationResult) 1 * authorizableLookup.getUsersAuthorizable() >> new SimpleAuthorizable(null, ResourceFactory.getUsersResource(), isAuthorized, authorizationResult) - if (authorizationResult == AuthorizationResult.approved()) { - 1 * userDao.getUser(_) >> { String userId -> - def userEntity = accessPolicyDto.users.find { it.id.equals(userId) }?.component - assert userEntity != null - new User.Builder().identifier(userEntity.id).identity(userEntity.identity).build() - } + 1 * userDao.getUser(_) >> { String userId -> + def userEntity = accessPolicyDto.users.find { it.id.equals(userId) }?.component + assert userEntity != null + new User.Builder().identifier(userEntity.id).identity(userEntity.identity).build() } 1 * revisionManager.get(_, _) >> { String id, ReadOnlyRevisionCallback callback -> callback.withRevision(new Revision(1L, 'client1', 'root')) } 0 * _ + + assert accessPolicyEntity != null if (isAuthorized) { assert accessPolicyEntity?.component?.id?.equals(accessPolicyDto.id) assert accessPolicyEntity?.accessPolicy?.canRead assert accessPolicyEntity?.accessPolicy?.canWrite } else { - assert accessPolicyEntity?.component == null - assert exception instanceof AccessDeniedException + assert accessPolicyEntity.component == null } where: @@ -626,28 +625,26 @@ class StandardNiFiServiceFacadeSpec extends Specification { } then: - if (isAuthorized) { - 1 * accessPolicyDao.getAccessPolicy(accessPolicyDto.id) >> requestedAccessPolicy - 1 * authorizableLookup.getUsersAuthorizable() >> new SimpleAuthorizable(null, ResourceFactory.getUsersResource(), - isAuthorized, authorizationResult) - } + 1 * accessPolicyDao.getAccessPolicy(accessPolicyDto.id) >> requestedAccessPolicy + 1 * authorizableLookup.getUsersAuthorizable() >> new SimpleAuthorizable(null, ResourceFactory.getUsersResource(), + isAuthorized, authorizationResult) _ * revisionManager.get(_, _) >> { String id, ReadOnlyRevisionCallback callback -> callback.withRevision(new Revision(1L, 'client1', 'root')) } 1 * authorizableLookup.getAccessPolicyAuthorizable(accessPolicyDto.id) >> new SimpleAuthorizable(null, ResourceFactory.getPolicyResource(accessPolicyDto.id), isAuthorized, authorizationResult) - if (authorizationResult == AuthorizationResult.approved()) { - 1 * userDao.getUser(_) >> { String userId -> - def userEntity = accessPolicyDto.users.find { it.id.equals(userId) }?.component - assert userEntity != null - new User.Builder().identifier(userEntity.id).identity(userEntity.identity).build() - } + 1 * userDao.getUser(_) >> { String userId -> + def userEntity = accessPolicyDto.users.find { it.id.equals(userId) }?.component + assert userEntity != null + new User.Builder().identifier(userEntity.id).identity(userEntity.identity).build() } 0 * _ + + assert accessPolicyEntity != null if (isAuthorized) { assert accessPolicyEntity?.component?.id?.equals(accessPolicyDto.id) } else { - assert exception instanceof AccessDeniedException + assert accessPolicyEntity.component == null } where: @@ -704,32 +701,30 @@ class StandardNiFiServiceFacadeSpec extends Specification { 1 * revisionManager.updateRevision(_, _, _) >> { RevisionClaim revisionClaim, NiFiUser niFiUser, UpdateRevisionTask callback -> callback.update() } + 1 * revisionManager.getRevision(currentRevision.componentId) >> currentRevision.incrementRevision(currentRevision.clientId) } 1 * authorizableLookup.getUsersAuthorizable() >> new SimpleAuthorizable(null, ResourceFactory.getUsersResource(), isAuthorized, authorizationResult) - if (isAuthorized || hasPolicy) { - 1 * authorizableLookup.getAccessPolicyAuthorizable(accessPolicyDto.id) >> new SimpleAuthorizable(null, ResourceFactory.getPolicyResource(accessPolicyDto.id), - isAuthorized, authorizationResult) - } - if (authorizationResult == AuthorizationResult.approved()) { - 1 * userDao.getUser(_) >> { String userId -> - def userEntity = accessPolicyDto.users.find { it.id.equals(userId) }?.component - assert userEntity != null - new User.Builder().identifier(userEntity.id).identity(userEntity.identity).build() - } + 1 * authorizableLookup.getAccessPolicyAuthorizable(accessPolicyDto.id) >> new SimpleAuthorizable(null, ResourceFactory.getPolicyResource(accessPolicyDto.id), + isAuthorized, authorizationResult) + 1 * userDao.getUser(_) >> { String userId -> + def userEntity = accessPolicyDto.users.find { it.id.equals(userId) }?.component + assert userEntity != null + new User.Builder().identifier(userEntity.id).identity(userEntity.identity).build() } 1 * revisionManager.get(_, _) >> { String id, ReadOnlyRevisionCallback callback -> callback.withRevision(new Revision(1L, 'client1', 'root')) } 0 * _ def accessPolicyEntity = accessPolicyEntityUpdateResult?.result + + assert accessPolicyEntity != null if (isAuthorized) { assert accessPolicyEntity?.component?.id?.equals(accessPolicyDto.id) assert accessPolicyEntity?.accessPolicy?.canRead assert accessPolicyEntity?.accessPolicy?.canWrite } else { - assert accessPolicyEntity?.component == null - assert exception instanceof AccessDeniedException + assert accessPolicyEntity.component == null } where: @@ -787,27 +782,23 @@ class StandardNiFiServiceFacadeSpec extends Specification { then: if (hasPolicy) { 1 * accessPolicyDao.getAccessPolicy(accessPolicyDto.id) >> accessPolicy - if (isAuthorized) { - 1 * accessPolicyDao.deleteAccessPolicy(accessPolicyDto.id) >> accessPolicy - } + 1 * accessPolicyDao.deleteAccessPolicy(accessPolicyDto.id) >> accessPolicy 1 * authorizableLookup.getUsersAuthorizable() >> new SimpleAuthorizable(null, ResourceFactory.usersResource, isAuthorized, authorizationResult) } else { 1 * accessPolicyDao.getAccessPolicy(accessPolicyDto.id) >> null 1 * accessPolicyDao.deleteAccessPolicy(accessPolicyDto.id) >> null } - if (!(!isAuthorized && hasPolicy)) { - 1 * authorizableLookup.getAccessPolicyAuthorizable(accessPolicyDto.id) >> new SimpleAuthorizable(null, ResourceFactory.getPolicyResource(accessPolicyDto.id), - isAuthorized, authorizationResult) - 1 * revisionManager.deleteRevision(_, _, _) >> { RevisionClaim revisionClaim, NiFiUser nifiUser, DeleteRevisionTask task -> - task.performTask() - } - 1 * controllerFacade.save() + 1 * authorizableLookup.getAccessPolicyAuthorizable(accessPolicyDto.id) >> new SimpleAuthorizable(null, ResourceFactory.getPolicyResource(accessPolicyDto.id), + isAuthorized, authorizationResult) + 1 * revisionManager.deleteRevision(_, _, _) >> { RevisionClaim revisionClaim, NiFiUser nifiUser, DeleteRevisionTask task -> + task.performTask() } + 1 * controllerFacade.save() _ * revisionManager.get(_, _) >> { String id, ReadOnlyRevisionCallback callback -> callback.withRevision(new Revision(1L, 'client1', 'root')) } - if (authorizationResult == AuthorizationResult.approved() && hasPolicy) { + if (hasPolicy) { 1 * userDao.getUser(_) >> { String userId -> def userEntity = accessPolicyDto.users.find { it.id.equals(userId) }?.component assert userEntity != null @@ -815,14 +806,13 @@ class StandardNiFiServiceFacadeSpec extends Specification { } } 0 * _ - if (hasPolicy && isAuthorized) { + + assert accessPolicyEntity != null + if (hasPolicy) { assert accessPolicyEntity?.id?.equals(accessPolicyDto.id) } else { assert accessPolicyEntity?.id == null } - if (!isAuthorized && hasPolicy) { - assert exception instanceof AccessDeniedException - } where: hasPolicy | currentRevision | accessPolicyDto | isAuthorized | @@ -831,9 +821,9 @@ class StandardNiFiServiceFacadeSpec extends Specification { AuthorizationResult.approved() false | null | new AccessPolicyDTO(id: '1', resource: ResourceFactory.flowResource.identifier, users: [createUserEntity()], canRead: true) | true | AuthorizationResult.approved() - true | new Revision(1L, 'client1', 'root') | new AccessPolicyDTO(id: '1', resource: ResourceFactory.flowResource.identifier, users: [createUserEntity()], canRead: true) | false | + true | new Revision(1L, 'client1', 'root') | new AccessPolicyDTO(id: '1', resource: ResourceFactory.flowResource.identifier, users: [createUserEntity()], canRead: true) | false | AuthorizationResult.denied() - false | null | new AccessPolicyDTO(id: '1', resource: ResourceFactory.flowResource.identifier, users: [createUserEntity()], canRead: true) | false | + false | null | new AccessPolicyDTO(id: '1', resource: ResourceFactory.flowResource.identifier, users: [createUserEntity()], canRead: true) | false | AuthorizationResult.denied() }
http://git-wip-us.apache.org/repos/asf/nifi/blob/f0811ca4/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-api/src/test/java/org/apache/nifi/integration/NiFiWebApiTest.java ---------------------------------------------------------------------- diff --git a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-api/src/test/java/org/apache/nifi/integration/NiFiWebApiTest.java b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-api/src/test/java/org/apache/nifi/integration/NiFiWebApiTest.java index 8067491..556c6bc 100644 --- a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-api/src/test/java/org/apache/nifi/integration/NiFiWebApiTest.java +++ b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-api/src/test/java/org/apache/nifi/integration/NiFiWebApiTest.java @@ -48,10 +48,6 @@ import java.util.Set; @Ignore public class NiFiWebApiTest { - public static void populateFlow(Client client, String baseUrl, String clientId) throws Exception { - - } - public static void populateFlow(Client client, String baseUrl, NiFiTestUser user, String clientId) throws Exception { // ----------------------------------------------- @@ -66,7 +62,7 @@ public class NiFiWebApiTest { // create the revision final RevisionDTO revision = new RevisionDTO(); revision.setClientId(clientId); - revision.setVersion(NiFiTestUser.REVISION); + revision.setVersion(0l); // create the local selection processor entity ProcessorEntity processorEntity = new ProcessorEntity(); http://git-wip-us.apache.org/repos/asf/nifi/blob/f0811ca4/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-api/src/test/java/org/apache/nifi/integration/accesscontrol/AccessControlHelper.java ---------------------------------------------------------------------- diff --git a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-api/src/test/java/org/apache/nifi/integration/accesscontrol/AccessControlHelper.java b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-api/src/test/java/org/apache/nifi/integration/accesscontrol/AccessControlHelper.java index 2576253..27f1c5e 100644 --- a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-api/src/test/java/org/apache/nifi/integration/accesscontrol/AccessControlHelper.java +++ b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-api/src/test/java/org/apache/nifi/integration/accesscontrol/AccessControlHelper.java @@ -16,6 +16,7 @@ */ package org.apache.nifi.integration.accesscontrol; +import com.sun.jersey.api.client.ClientResponse; import org.apache.nifi.integration.NiFiWebApiTest; import org.apache.nifi.integration.util.NiFiTestAuthorizer; import org.apache.nifi.integration.util.NiFiTestServer; @@ -26,6 +27,8 @@ import org.apache.nifi.util.NiFiProperties; import java.io.File; +import static org.junit.Assert.assertEquals; + /** * Access control test for the dfm user. */ @@ -48,6 +51,10 @@ public class AccessControlHelper { private String baseUrl; public AccessControlHelper(final String flowXmlPath) throws Exception { + this(flowXmlPath, null); + } + + public AccessControlHelper(final String flowXmlPath, final String overrideAuthorizer) throws Exception { this.flowXmlPath = flowXmlPath; // look for the flow.xml and toss it @@ -64,6 +71,10 @@ public class AccessControlHelper { NiFiProperties props = NiFiProperties.getInstance(); props.setProperty(NiFiProperties.FLOW_CONFIGURATION_FILE, flowXmlPath); + if (overrideAuthorizer != null) { + props.setProperty(NiFiProperties.SECURITY_USER_AUTHORIZER, overrideAuthorizer); + } + // load extensions NarClassLoaders.load(props); ExtensionManager.discoverExtensions(); @@ -102,6 +113,26 @@ public class AccessControlHelper { return noneUser; } + public void testGenericGetUri(final String uri) throws Exception { + ClientResponse response; + + // read + response = getReadUser().testGet(uri); + assertEquals(200, response.getStatus()); + + // read/write + response = getReadWriteUser().testGet(uri); + assertEquals(200, response.getStatus()); + + // write + response = getWriteUser().testGet(uri); + assertEquals(403, response.getStatus()); + + // none + response = getNoneUser().testGet(uri); + assertEquals(403, response.getStatus()); + } + public String getBaseUrl() { return baseUrl; } http://git-wip-us.apache.org/repos/asf/nifi/blob/f0811ca4/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-api/src/test/java/org/apache/nifi/integration/accesscontrol/AccessTokenEndpointTest.java ---------------------------------------------------------------------- diff --git a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-api/src/test/java/org/apache/nifi/integration/accesscontrol/AccessTokenEndpointTest.java b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-api/src/test/java/org/apache/nifi/integration/accesscontrol/AccessTokenEndpointTest.java deleted file mode 100644 index 6f30b03..0000000 --- a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-api/src/test/java/org/apache/nifi/integration/accesscontrol/AccessTokenEndpointTest.java +++ /dev/null @@ -1,274 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.nifi.integration.accesscontrol; - -import com.sun.jersey.api.client.Client; -import com.sun.jersey.api.client.ClientResponse; -import org.apache.commons.io.FileUtils; -import org.apache.nifi.integration.util.NiFiTestServer; -import org.apache.nifi.integration.util.NiFiTestUser; -import org.apache.nifi.integration.util.SourceTestProcessor; -import org.apache.nifi.nar.ExtensionManager; -import org.apache.nifi.nar.NarClassLoaders; -import org.apache.nifi.security.util.SslContextFactory; -import org.apache.nifi.util.NiFiProperties; -import org.apache.nifi.web.api.dto.AccessConfigurationDTO; -import org.apache.nifi.web.api.dto.AccessStatusDTO; -import org.apache.nifi.web.api.dto.ProcessorDTO; -import org.apache.nifi.web.api.dto.RevisionDTO; -import org.apache.nifi.web.api.entity.AccessConfigurationEntity; -import org.apache.nifi.web.api.entity.AccessStatusEntity; -import org.apache.nifi.web.api.entity.ProcessorEntity; -import org.apache.nifi.web.util.WebUtils; -import org.junit.AfterClass; -import org.junit.Assert; -import org.junit.BeforeClass; -import org.junit.Test; - -import javax.net.ssl.SSLContext; -import java.io.File; -import java.util.HashMap; -import java.util.Map; - -/** - * Access token endpoint test. - */ -public class AccessTokenEndpointTest { - - private static final String CLIENT_ID = "token-endpoint-id"; - private static final String CONTEXT_PATH = "/nifi-api"; - private static final String FLOW_XML_PATH = "target/test-classes/access-control/flow-admin.xml"; - - private static NiFiTestServer SERVER; - private static NiFiTestUser TOKEN_USER; - private static String BASE_URL; - - @BeforeClass - public static void setup() throws Exception { - // configure the location of the nifi properties - File nifiPropertiesFile = new File("src/test/resources/access-control/nifi.properties"); - System.setProperty(NiFiProperties.PROPERTIES_FILE_PATH, nifiPropertiesFile.getAbsolutePath()); - - // update the flow.xml property - NiFiProperties props = NiFiProperties.getInstance(); - props.setProperty("nifi.flow.configuration.file", FLOW_XML_PATH); - - // delete the database directory to avoid issues with re-registration in testRequestAccessUsingToken - FileUtils.deleteDirectory(props.getDatabaseRepositoryPath().toFile()); - - // load extensions - NarClassLoaders.load(props); - ExtensionManager.discoverExtensions(); - - // start the server - SERVER = new NiFiTestServer("src/main/webapp", CONTEXT_PATH); - SERVER.startServer(); - SERVER.loadFlow(); - - // get the base url - BASE_URL = SERVER.getBaseUrl() + CONTEXT_PATH; - - // create the user - final Client client = WebUtils.createClient(null, createTrustContext(props)); - TOKEN_USER = new NiFiTestUser(client, null); - } - - private static SSLContext createTrustContext(final NiFiProperties props) throws Exception { - return SslContextFactory.createTrustSslContext(props.getProperty(NiFiProperties.SECURITY_TRUSTSTORE), - props.getProperty(NiFiProperties.SECURITY_TRUSTSTORE_PASSWD).toCharArray(), - props.getProperty(NiFiProperties.SECURITY_TRUSTSTORE_TYPE), "TLS"); - } - - // ----------- - // LOGIN CONIG - // ----------- - /** - * Test getting access configuration. - * - * @throws Exception ex - */ - @Test - public void testGetAccessConfig() throws Exception { - String url = BASE_URL + "/access/config"; - - ClientResponse response = TOKEN_USER.testGet(url); - - // ensure the request is successful - Assert.assertEquals(200, response.getStatus()); - - // extract the process group - AccessConfigurationEntity accessConfigEntity = response.getEntity(AccessConfigurationEntity.class); - - // ensure there is content - Assert.assertNotNull(accessConfigEntity); - - // extract the process group dto - AccessConfigurationDTO accessConfig = accessConfigEntity.getConfig(); - - // verify config - Assert.assertTrue(accessConfig.getSupportsLogin()); - } - - /** - * Obtains a token and creates a processor using it. - * - * @throws Exception ex - */ - @Test - public void testCreateProcessorUsingToken() throws Exception { - String url = BASE_URL + "/access/token"; - - ClientResponse response = TOKEN_USER.testCreateToken(url, "user@nifi", "whateve"); - - // ensure the request is successful - Assert.assertEquals(201, response.getStatus()); - - // get the token - String token = response.getEntity(String.class); - - // attempt to create a processor with it - createProcessor(token); - } - - private ProcessorDTO createProcessor(final String token) throws Exception { - String url = BASE_URL + "/process-groups/root/processors"; - - // authorization header - Map<String, String> headers = new HashMap<>(); - headers.put("Authorization", "Bearer " + token); - - // create the processor - ProcessorDTO processor = new ProcessorDTO(); - processor.setName("Copy"); - processor.setType(SourceTestProcessor.class.getName()); - - // create the revision - final RevisionDTO revision = new RevisionDTO(); - revision.setClientId(CLIENT_ID); - revision.setVersion(NiFiTestUser.REVISION); - - // create the entity body - ProcessorEntity entity = new ProcessorEntity(); - entity.setRevision(revision); - entity.setComponent(processor); - - // perform the request - ClientResponse response = TOKEN_USER.testPostWithHeaders(url, entity, headers); - - // ensure the request is successful - Assert.assertEquals(201, response.getStatus()); - - // get the entity body - entity = response.getEntity(ProcessorEntity.class); - - // verify creation - processor = entity.getComponent(); - Assert.assertEquals("Copy", processor.getName()); - Assert.assertEquals("org.apache.nifi.integration.util.SourceTestProcessor", processor.getType()); - - return processor; - } - - /** - * Verifies the response when bad credentials are specified. - * - * @throws Exception ex - */ - @Test - public void testInvalidCredentials() throws Exception { - String url = BASE_URL + "/access/token"; - - ClientResponse response = TOKEN_USER.testCreateToken(url, "user@nifi", "not a real password"); - - // ensure the request is successful - Assert.assertEquals(400, response.getStatus()); - } - - /** - * Verifies the response when the user is known. - * - * @throws Exception ex - */ - @Test - public void testUnknownUser() throws Exception { - String url = BASE_URL + "/access/token"; - - ClientResponse response = TOKEN_USER.testCreateToken(url, "not a real user", "not a real password"); - - // ensure the request is successful - Assert.assertEquals(400, response.getStatus()); - } - - /** - * Request access using access token. - * - * @throws Exception ex - */ - @Test - public void testRequestAccessUsingToken() throws Exception { - String accessStatusUrl = BASE_URL + "/access"; - String accessTokenUrl = BASE_URL + "/access/token"; - - ClientResponse response = TOKEN_USER.testGet(accessStatusUrl); - - // ensure the request is successful - Assert.assertEquals(200, response.getStatus()); - - AccessStatusEntity accessStatusEntity = response.getEntity(AccessStatusEntity.class); - AccessStatusDTO accessStatus = accessStatusEntity.getAccessStatus(); - - // verify unknown - Assert.assertEquals("UNKNOWN", accessStatus.getStatus()); - - response = TOKEN_USER.testCreateToken(accessTokenUrl, "unregistered-user@nifi", "password"); - - // ensure the request is successful - Assert.assertEquals(201, response.getStatus()); - - // get the token - String token = response.getEntity(String.class); - - // authorization header - Map<String, String> headers = new HashMap<>(); - headers.put("Authorization", "Bearer " + token); - - // check the status with the token - response = TOKEN_USER.testGetWithHeaders(accessStatusUrl, null, headers); - - // ensure the request is successful - Assert.assertEquals(200, response.getStatus()); - - accessStatusEntity = response.getEntity(AccessStatusEntity.class); - accessStatus = accessStatusEntity.getAccessStatus(); - - // verify unregistered - Assert.assertEquals("ACTIVE", accessStatus.getStatus()); - } - - @AfterClass - public static void cleanup() throws Exception { - // shutdown the server - SERVER.shutdownServer(); - SERVER = null; - - // look for the flow.xml - File flow = new File(FLOW_XML_PATH); - if (flow.exists()) { - flow.delete(); - } - } -} http://git-wip-us.apache.org/repos/asf/nifi/blob/f0811ca4/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-api/src/test/java/org/apache/nifi/integration/accesscontrol/AdminAccessControlTest.java ---------------------------------------------------------------------- diff --git a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-api/src/test/java/org/apache/nifi/integration/accesscontrol/AdminAccessControlTest.java b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-api/src/test/java/org/apache/nifi/integration/accesscontrol/AdminAccessControlTest.java deleted file mode 100644 index 6077bc3..0000000 --- a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-api/src/test/java/org/apache/nifi/integration/accesscontrol/AdminAccessControlTest.java +++ /dev/null @@ -1,1010 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.nifi.integration.accesscontrol; - -import com.sun.jersey.api.client.ClientResponse; -import org.apache.commons.collections4.CollectionUtils; -import org.apache.nifi.integration.NiFiWebApiTest; -import org.apache.nifi.integration.util.NiFiTestServer; -import org.apache.nifi.integration.util.NiFiTestUser; -import org.apache.nifi.nar.ExtensionManager; -import org.apache.nifi.nar.NarClassLoaders; -import org.apache.nifi.util.NiFiProperties; -import org.apache.nifi.web.api.dto.FlowSnippetDTO; -import org.apache.nifi.web.api.dto.ProcessGroupDTO; -import org.apache.nifi.web.api.dto.RevisionDTO; -import org.apache.nifi.web.api.dto.UserDTO; -import org.apache.nifi.web.api.entity.AuthorityEntity; -import org.apache.nifi.web.api.entity.BannerEntity; -import org.apache.nifi.web.api.entity.ConnectionEntity; -import org.apache.nifi.web.api.entity.ConnectionsEntity; -import org.apache.nifi.web.api.entity.ControllerConfigurationEntity; -import org.apache.nifi.web.api.entity.InputPortsEntity; -import org.apache.nifi.web.api.entity.LabelEntity; -import org.apache.nifi.web.api.entity.LabelsEntity; -import org.apache.nifi.web.api.entity.OutputPortsEntity; -import org.apache.nifi.web.api.entity.PortEntity; -import org.apache.nifi.web.api.entity.PrioritizerTypesEntity; -import org.apache.nifi.web.api.entity.ProcessGroupEntity; -import org.apache.nifi.web.api.entity.ProcessGroupsEntity; -import org.apache.nifi.web.api.entity.ProcessorEntity; -import org.apache.nifi.web.api.entity.ProcessorTypesEntity; -import org.apache.nifi.web.api.entity.ProcessorsEntity; -import org.apache.nifi.web.api.entity.UsersEntity; -import org.junit.AfterClass; -import org.junit.Assert; -import org.junit.BeforeClass; -import org.junit.Ignore; -import org.junit.Test; - -import java.io.File; -import java.util.HashMap; -import java.util.Map; - -/** - * Access control test for the admin user. - */ -@Ignore -public class AdminAccessControlTest { - - public static final String ADMIN_USER_DN = "CN=Lastname Firstname Middlename admin, OU=Unknown, OU=Unknown, OU=Unknown, O=Unknown, C=Unknown"; - - private static final String CLIENT_ID = "admin-client-id"; - private static final String CONTEXT_PATH = "/nifi-api"; - private static final String FLOW_XML_PATH = "target/test-classes/access-control/flow-admin.xml"; - - private static NiFiTestServer SERVER; - private static NiFiTestUser ADMIN_USER; - private static String BASE_URL; - - @BeforeClass - public static void setup() throws Exception { - // configure the location of the nifi properties - File nifiPropertiesFile = new File("src/test/resources/access-control/nifi.properties"); - System.setProperty(NiFiProperties.PROPERTIES_FILE_PATH, nifiPropertiesFile.getAbsolutePath()); - - // update the flow.xml property - NiFiProperties props = NiFiProperties.getInstance(); - props.setProperty("nifi.flow.configuration.file", FLOW_XML_PATH); - - // load extensions - NarClassLoaders.load(props); - ExtensionManager.discoverExtensions(); - - // start the server - SERVER = new NiFiTestServer("src/main/webapp", CONTEXT_PATH); - SERVER.startServer(); - SERVER.loadFlow(); - - // get the base url - BASE_URL = SERVER.getBaseUrl() + CONTEXT_PATH; - - // create the user - ADMIN_USER = new NiFiTestUser(SERVER.getClient(), ADMIN_USER_DN); - - // populate the flow - NiFiWebApiTest.populateFlow(SERVER.getClient(), BASE_URL, CLIENT_ID); - } - - // ---------------------------------------------- - // PROCESS GROUPS - // ---------------------------------------------- - /** - * Ensures the admin user can get a groups content. - * - * @throws Exception ex - */ - @Test - public void testGroupGet() throws Exception { - String url = BASE_URL + "/controller/process-groups/root"; - - // build the query params - Map<String, String> queryParams = new HashMap<>(); - queryParams.put("verbose", Boolean.TRUE.toString()); - - ClientResponse response = ADMIN_USER.testGet(url, queryParams); - - // ensure the request is successful - Assert.assertEquals(200, response.getStatus()); - - // extract the process group - ProcessGroupEntity processGroupEntity = response.getEntity(ProcessGroupEntity.class); - - // ensure there is content - Assert.assertNotNull(processGroupEntity); - - // extract the process group dto - ProcessGroupDTO processGroupDTO = processGroupEntity.getComponent(); - FlowSnippetDTO processGroupContentsDTO = processGroupDTO.getContents(); - - // verify graph - Assert.assertEquals(2, processGroupContentsDTO.getProcessors().size()); - Assert.assertEquals(1, processGroupContentsDTO.getConnections().size()); - Assert.assertEquals(1, processGroupContentsDTO.getProcessGroups().size()); - Assert.assertEquals(1, processGroupContentsDTO.getInputPorts().size()); - Assert.assertEquals(1, processGroupContentsDTO.getOutputPorts().size()); - Assert.assertEquals(1, processGroupContentsDTO.getLabels().size()); - } - - /** - * Verifies the admin user cannot update a group. - * - * @throws Exception ex - */ - @Test - public void testGroupPut() throws Exception { - String url = BASE_URL + "/controller/process-groups/root"; - - // create the entity body - Map<String, String> formData = new HashMap<>(); - formData.put("revision", String.valueOf(NiFiTestUser.REVISION)); - formData.put("clientId", CLIENT_ID); - - // perform the request - ClientResponse response = ADMIN_USER.testPut(url, formData); - - // ensure the request is failed with a forbidden status code - Assert.assertEquals(403, response.getStatus()); - } - - // ---------------------------------------------- - // CONTROLLER - // ---------------------------------------------- - /** - * Verifies the admin user can retrieve the controller configuration. - * - * @throws Exception ex - */ - @Test - public void testControllerConfigurationGet() throws Exception { - String url = BASE_URL + "/controller/config"; - - // perform the request - ClientResponse response = ADMIN_USER.testGet(url); - - // ensure the request is successful - Assert.assertEquals(200, response.getStatus()); - - // verify the results - ControllerConfigurationEntity entity = response.getEntity(ControllerConfigurationEntity.class); - Assert.assertNotNull(entity); - Assert.assertNotNull(entity.getConfig()); - Assert.assertEquals(10, entity.getConfig().getMaxTimerDrivenThreadCount().intValue()); - Assert.assertEquals(5, entity.getConfig().getMaxEventDrivenThreadCount().intValue()); - Assert.assertEquals(30, entity.getConfig().getAutoRefreshIntervalSeconds().intValue()); - } - - /** - * Verifies the admin user cannot update the controller configuration. - * - * @throws Exception ex - */ - @Test - public void testControllerConfigurationPut() throws Exception { - String url = BASE_URL + "/controller/config"; - - // create the revision - final RevisionDTO revision = new RevisionDTO(); - revision.setVersion(0L); - - // create the entity body - ControllerConfigurationEntity entity = new ControllerConfigurationEntity(); - entity.setRevision(revision); - - // perform the request - ClientResponse response = ADMIN_USER.testPut(url, entity); - - // ensure the request is failed with a forbidden status code - Assert.assertEquals(403, response.getStatus()); - } - - /** - * Verifies the read only user cannot create a new flow archive. - * - * @throws Exception ex - */ - @Test - public void testFlowConfigurationArchivePost() throws Exception { - String url = BASE_URL + "/controller/archive"; - - // create the entity body - Map<String, String> formData = new HashMap<>(); - formData.put("revision", String.valueOf(NiFiTestUser.REVISION)); - formData.put("clientId", CLIENT_ID); - - // perform the request - ClientResponse response = ADMIN_USER.testPost(url, formData); - - // ensure the request is failed with a forbidden status code - Assert.assertEquals(403, response.getStatus()); - } - - /** - * Verifies the admin user can retrieve his credentials. - * - * @throws Exception ex - */ - @Test - public void testAuthoritiesGet() throws Exception { - String url = BASE_URL + "/controller/authorities"; - - // perform the request - ClientResponse response = ADMIN_USER.testGet(url); - - // ensure the request is successful - Assert.assertEquals(200, response.getStatus()); - - // verify the result - AuthorityEntity entity = response.getEntity(AuthorityEntity.class); - Assert.assertNotNull(entity); - Assert.assertNotNull(entity.getAuthorities()); - Assert.assertEquals(1, entity.getAuthorities().size()); - Assert.assertEquals("ROLE_ADMIN", entity.getAuthorities().toArray()[0]); - } - - /** - * Verifies the admin user can retrieve the banners. - * - * @throws Exception ex - */ - @Test - public void testBannersGet() throws Exception { - String url = BASE_URL + "/controller/banners"; - - // perform the request - ClientResponse response = ADMIN_USER.testGet(url); - - // ensure the request is successful - Assert.assertEquals(200, response.getStatus()); - - // verify the result - BannerEntity entity = response.getEntity(BannerEntity.class); - Assert.assertNotNull(entity); - Assert.assertNotNull(entity.getBanners()); - Assert.assertEquals("TEST BANNER", entity.getBanners().getHeaderText()); - Assert.assertEquals("TEST BANNER", entity.getBanners().getFooterText()); - } - - /** - * Verifies the admin user can retrieve the processor types. - * - * @throws Exception ex - */ - @Test - public void testProcessorTypesGet() throws Exception { - String url = BASE_URL + "/controller/processor-types"; - - // perform the request - ClientResponse response = ADMIN_USER.testGet(url); - - // ensure the request is successful - Assert.assertEquals(200, response.getStatus()); - - // verify the result - ProcessorTypesEntity entity = response.getEntity(ProcessorTypesEntity.class); - Assert.assertNotNull(entity); - Assert.assertNotNull(entity.getProcessorTypes()); - Assert.assertFalse(entity.getProcessorTypes().isEmpty()); - } - - /** - * Verifies the admin user can retrieve the prioritizer types. - * - * @throws Exception ex - */ - @Test - public void testPrioritizerTypesGet() throws Exception { - String url = BASE_URL + "/controller/prioritizers"; - - // perform the request - ClientResponse response = ADMIN_USER.testGet(url); - - // ensure the request is successful - Assert.assertEquals(200, response.getStatus()); - - // verify the result - PrioritizerTypesEntity entity = response.getEntity(PrioritizerTypesEntity.class); - Assert.assertNotNull(entity); - Assert.assertNotNull(entity.getPrioritizerTypes()); - Assert.assertFalse(entity.getPrioritizerTypes().isEmpty()); - } - - // ---------------------------------------------- - // PROCESS GROUP - // ---------------------------------------------- - /** - * Verifies that the admin user can get process groups. - * - * @throws Exception ex - */ - @Test - public void testProcessorGroupsGet() throws Exception { - String url = BASE_URL + "/controller/process-groups/root/process-group-references"; - - // perform the request - ClientResponse response = ADMIN_USER.testGet(url); - - // get the response - ProcessGroupsEntity entity = response.getEntity(ProcessGroupsEntity.class); - - // ensure the request was successful - Assert.assertEquals(200, response.getStatus()); - Assert.assertNotNull(entity.getProcessGroups()); - Assert.assertEquals(1, entity.getProcessGroups().size()); - } - - /** - * Verifies that the operator user cannot create new process groups. - * - * @throws Exception ex - */ - @Test - public void testProcessGroupPost() throws Exception { - String url = BASE_URL + "/controller/process-groups/root/process-group-references"; - - // create the revision - final RevisionDTO revision = new RevisionDTO(); - revision.setClientId(CLIENT_ID); - revision.setVersion(NiFiTestUser.REVISION); - - // create the entity body - ProcessGroupEntity entity = new ProcessGroupEntity(); - entity.setRevision(revision); - - // perform the request - ClientResponse response = ADMIN_USER.testPost(url, entity); - - // ensure the request is failed with a forbidden status code - Assert.assertEquals(403, response.getStatus()); - } - - /** - * Verifies that the operator user cannot update process group configuration. - * - * @throws Exception ex - */ - @Test - public void testProcessGroupPut() throws Exception { - String url = BASE_URL + "/controller/process-groups/root/process-group-references/1"; - - // create the revision - final RevisionDTO revision = new RevisionDTO(); - revision.setClientId(CLIENT_ID); - revision.setVersion(NiFiTestUser.REVISION); - - // create the entity body - ProcessGroupEntity entity = new ProcessGroupEntity(); - entity.setRevision(revision); - - // perform the request - ClientResponse response = ADMIN_USER.testPut(url, entity); - - // ensure the request is failed with a forbidden status code - Assert.assertEquals(403, response.getStatus()); - } - - /** - * Verifies that the operator user cannot delete process groups. - * - * @throws Exception ex - */ - @Test - public void testProcessGroupDelete() throws Exception { - String url = BASE_URL + "/controller/process-groups/root/process-group-references/1"; - - // perform the request - ClientResponse response = ADMIN_USER.testDelete(url); - - // ensure the request is failed with a forbidden status code - Assert.assertEquals(403, response.getStatus()); - } - - // ---------------------------------------------- - // PROCESSOR - // ---------------------------------------------- - /** - * Verifies that the admin user can get processors. - * - * @throws Exception ex - */ - @Test - public void testProcessorsGet() throws Exception { - String url = BASE_URL + "/controller/process-groups/root/processors"; - - // perform the request - ClientResponse response = ADMIN_USER.testGet(url); - - // get the response - ProcessorsEntity entity = response.getEntity(ProcessorsEntity.class); - - // ensure the request was successful - Assert.assertEquals(200, response.getStatus()); - Assert.assertNotNull(entity.getProcessors()); - Assert.assertEquals(2, entity.getProcessors().size()); - } - - /** - * Verifies that the admin user cannot create new processors. - * - * @throws Exception ex - */ - @Test - public void testProcessorPost() throws Exception { - String url = BASE_URL + "/controller/process-groups/root/processors"; - - // create the revision - final RevisionDTO revision = new RevisionDTO(); - revision.setClientId(CLIENT_ID); - revision.setVersion(NiFiTestUser.REVISION); - - // create the entity body - ProcessorEntity entity = new ProcessorEntity(); - entity.setRevision(revision); - - // perform the request - ClientResponse response = ADMIN_USER.testPost(url, entity); - - // ensure the request is failed with a forbidden status code - Assert.assertEquals(403, response.getStatus()); - } - - /** - * Verifies that the admin user cannot create new processors. - * - * @throws Exception ex - */ - @Test - public void testProcessorPut() throws Exception { - String url = BASE_URL + "/controller/process-groups/root/processors/1"; - - // create the revision - final RevisionDTO revision = new RevisionDTO(); - revision.setClientId(CLIENT_ID); - revision.setVersion(NiFiTestUser.REVISION); - - // create the entity body - ProcessorEntity entity = new ProcessorEntity(); - entity.setRevision(revision); - - // perform the request - ClientResponse response = ADMIN_USER.testPut(url, entity); - - // ensure the request is failed with a forbidden status code - Assert.assertEquals(403, response.getStatus()); - } - - /** - * Verifies that the admin user cannot delete processors. - * - * @throws Exception ex - */ - @Test - public void testProcessorDelete() throws Exception { - String url = BASE_URL + "/controller/process-groups/root/processors/1"; - - // perform the request - ClientResponse response = ADMIN_USER.testDelete(url); - - // ensure the request is failed with a forbidden status code - Assert.assertEquals(403, response.getStatus()); - } - - // ---------------------------------------------- - // CONNECTION - // ---------------------------------------------- - /** - * Verifies that the admin user can get connections. - * - * @throws Exception ex - */ - @Test - public void testConnectionsGet() throws Exception { - String url = BASE_URL + "/controller/process-groups/root/connections"; - - // perform the request - ClientResponse response = ADMIN_USER.testGet(url); - - // get the response - ConnectionsEntity entity = response.getEntity(ConnectionsEntity.class); - - // ensure the request was successful - Assert.assertEquals(200, response.getStatus()); - Assert.assertNotNull(entity.getConnections()); - Assert.assertEquals(1, entity.getConnections().size()); - } - - /** - * Verifies that the admin user cannot create connections. - * - * @throws Exception ex - */ - @Test - public void testConnectionPost() throws Exception { - String url = BASE_URL + "/controller/process-groups/root/connections"; - - // create the revision - final RevisionDTO revision = new RevisionDTO(); - revision.setClientId(CLIENT_ID); - revision.setVersion(NiFiTestUser.REVISION); - - // create the entity body - ConnectionEntity entity = new ConnectionEntity(); - entity.setRevision(revision); - - // perform the request - ClientResponse response = ADMIN_USER.testPost(url, entity); - - // ensure the request is failed with a forbidden status code - Assert.assertEquals(403, response.getStatus()); - } - - /** - * Verifies that the admin user cannot create connections. - * - * @throws Exception ex - */ - @Test - public void testConnectionPut() throws Exception { - String url = BASE_URL + "/controller/process-groups/root/connections/1"; - - // create the revision - final RevisionDTO revision = new RevisionDTO(); - revision.setClientId(CLIENT_ID); - revision.setVersion(NiFiTestUser.REVISION); - - // create the entity body - ConnectionEntity entity = new ConnectionEntity(); - entity.setRevision(revision); - - // perform the request - ClientResponse response = ADMIN_USER.testPut(url, entity); - - // ensure the request is failed with a forbidden status code - Assert.assertEquals(403, response.getStatus()); - } - - /** - * Verifies that the admin user cannot delete connections. - * - * @throws Exception ex - */ - @Test - public void testConnectionDelete() throws Exception { - String url = BASE_URL + "/controller/process-groups/root/connections/1"; - - // perform the request - ClientResponse response = ADMIN_USER.testDelete(url); - - // ensure the request is failed with a forbidden status code - Assert.assertEquals(403, response.getStatus()); - } - - // ---------------------------------------------- - // INPUT PORTS - // ---------------------------------------------- - /** - * Verifies that the admin user can get input ports. - * - * @throws Exception ex - */ - @Test - public void testInputPortsGet() throws Exception { - String url = BASE_URL + "/controller/process-groups/root/input-ports"; - - // perform the request - ClientResponse response = ADMIN_USER.testGet(url); - - // get the response - InputPortsEntity entity = response.getEntity(InputPortsEntity.class); - - // ensure the request was successful - Assert.assertEquals(200, response.getStatus()); - Assert.assertNotNull(entity.getInputPorts()); - Assert.assertEquals(1, entity.getInputPorts().size()); - } - - /** - * Verifies that the admin user cannot create input ports. - * - * @throws Exception ex - */ - @Test - public void testInputPortPost() throws Exception { - String url = BASE_URL + "/controller/process-groups/root/input-ports"; - - // create the revision - final RevisionDTO revision = new RevisionDTO(); - revision.setClientId(CLIENT_ID); - revision.setVersion(NiFiTestUser.REVISION); - - // create the entity body - PortEntity entity = new PortEntity(); - entity.setRevision(revision); - - // perform the request - ClientResponse response = ADMIN_USER.testPost(url, entity); - - // ensure the request is failed with a forbidden status code - Assert.assertEquals(403, response.getStatus()); - } - - /** - * Verifies that the admin user cannot create input ports. - * - * @throws Exception ex - */ - @Test - public void testInputPortPut() throws Exception { - String url = BASE_URL + "/controller/process-groups/root/input-ports/1"; - - // create the revision - final RevisionDTO revision = new RevisionDTO(); - revision.setClientId(CLIENT_ID); - revision.setVersion(NiFiTestUser.REVISION); - - // create the entity body - PortEntity entity = new PortEntity(); - entity.setRevision(revision); - - // perform the request - ClientResponse response = ADMIN_USER.testPut(url, entity); - - // ensure the request is failed with a forbidden status code - Assert.assertEquals(403, response.getStatus()); - } - - /** - * Verifies that the admin user cannot delete input ports. - * - * @throws Exception ex - */ - @Test - public void testInputPortDelete() throws Exception { - String url = BASE_URL + "/controller/process-groups/root/input-ports/1"; - - // perform the request - ClientResponse response = ADMIN_USER.testDelete(url); - - // ensure the request is failed with a forbidden status code - Assert.assertEquals(403, response.getStatus()); - } - - // ---------------------------------------------- - // OUTPUT PORTS - // ---------------------------------------------- - /** - * Verifies that the admin user can get output ports. - * - * @throws Exception ex - */ - @Test - public void testOutputPortsGet() throws Exception { - String url = BASE_URL + "/controller/process-groups/root/output-ports"; - - // perform the request - ClientResponse response = ADMIN_USER.testGet(url); - - // get the response - OutputPortsEntity entity = response.getEntity(OutputPortsEntity.class); - - // ensure the request was successful - Assert.assertEquals(200, response.getStatus()); - Assert.assertNotNull(entity.getOutputPorts()); - Assert.assertEquals(1, entity.getOutputPorts().size()); - } - - /** - * Verifies that the admin user cannot create output ports. - * - * @throws Exception ex - */ - @Test - public void testOutputPortPost() throws Exception { - String url = BASE_URL + "/controller/process-groups/root/output-ports"; - - // create the revision - final RevisionDTO revision = new RevisionDTO(); - revision.setClientId(CLIENT_ID); - revision.setVersion(NiFiTestUser.REVISION); - - // create the entity body - PortEntity entity = new PortEntity(); - entity.setRevision(revision); - - // perform the request - ClientResponse response = ADMIN_USER.testPost(url, entity); - - // ensure the request is failed with a forbidden status code - Assert.assertEquals(403, response.getStatus()); - } - - /** - * Verifies that the admin user cannot create input ports. - * - * @throws Exception ex - */ - @Test - public void testOutputPortPut() throws Exception { - String url = BASE_URL + "/controller/process-groups/root/output-ports/1"; - - // create the revision - final RevisionDTO revision = new RevisionDTO(); - revision.setClientId(CLIENT_ID); - revision.setVersion(NiFiTestUser.REVISION); - - // create the entity body - PortEntity entity = new PortEntity(); - entity.setRevision(revision); - - // perform the request - ClientResponse response = ADMIN_USER.testPut(url, entity); - - // ensure the request is failed with a forbidden status code - Assert.assertEquals(403, response.getStatus()); - } - - /** - * Verifies that the admin user cannot delete output ports. - * - * @throws Exception ex - */ - @Test - public void testOutputPortDelete() throws Exception { - String url = BASE_URL + "/controller/process-groups/root/output-ports/1"; - - // perform the request - ClientResponse response = ADMIN_USER.testDelete(url); - - // ensure the request is failed with a forbidden status code - Assert.assertEquals(403, response.getStatus()); - } - - // ---------------------------------------------- - // LABEL - // ---------------------------------------------- - /** - * Verifies that the admin user can get input ports. - * - * @throws Exception ex - */ - @Test - public void testLabelsGet() throws Exception { - String url = BASE_URL + "/controller/process-groups/root/labels"; - - // perform the request - ClientResponse response = ADMIN_USER.testGet(url); - - // get the response - LabelsEntity entity = response.getEntity(LabelsEntity.class); - - // ensure the request was successful - Assert.assertEquals(200, response.getStatus()); - Assert.assertNotNull(entity.getLabels()); - Assert.assertEquals(1, entity.getLabels().size()); - } - - /** - * Verifies that the admin user cannot create labels. - * - * @throws Exception ex - */ - @Test - public void testLabelPost() throws Exception { - String url = BASE_URL + "/controller/process-groups/root/labels"; - - // create the revision - final RevisionDTO revision = new RevisionDTO(); - revision.setClientId(CLIENT_ID); - revision.setVersion(NiFiTestUser.REVISION); - - // create the entity body - LabelEntity entity = new LabelEntity(); - entity.setRevision(revision); - - // perform the request - ClientResponse response = ADMIN_USER.testPost(url, entity); - - // ensure the request is failed with a forbidden status code - Assert.assertEquals(403, response.getStatus()); - } - - /** - * Verifies that the admin user cannot create labels. - * - * @throws Exception ex - */ - @Test - public void testLabelPut() throws Exception { - String url = BASE_URL + "/controller/process-groups/root/labels/1"; - - // create the revision - final RevisionDTO revision = new RevisionDTO(); - revision.setClientId(CLIENT_ID); - revision.setVersion(NiFiTestUser.REVISION); - - // create the entity body - LabelEntity entity = new LabelEntity(); - entity.setRevision(revision); - - // perform the request - ClientResponse response = ADMIN_USER.testPut(url, entity); - - // ensure the request is failed with a forbidden status code - Assert.assertEquals(403, response.getStatus()); - } - - /** - * Verifies that the admin user cannot delete labels. - * - * @throws Exception ex - */ - @Test - public void testLabelDelete() throws Exception { - String url = BASE_URL + "/controller/process-groups/root/labels/1"; - - // perform the request - ClientResponse response = ADMIN_USER.testDelete(url); - - // ensure the request is failed with a forbidden status code - Assert.assertEquals(403, response.getStatus()); - } - - // ---------------------------------------------- - // REMOTE PROCESS GROUP - // ---------------------------------------------- - // ---------------------------------------------- - // HISTORY - // ---------------------------------------------- - /** - * Tests the ability to retrieve the NiFi history. - * - * @throws Exception ex - */ - @Test - public void testHistoryGet() throws Exception { - String url = BASE_URL + "/controller/history"; - - Map<String, String> queryParams = new HashMap<>(); - queryParams.put("offset", "1"); - queryParams.put("count", "1"); - - // perform the request - ClientResponse response = ADMIN_USER.testGet(url, queryParams); - - // ensure the request is failed with a forbidden status code - Assert.assertEquals(200, response.getStatus()); - } - - /** - * Tests the ability to retrieve a specific action. - * - * @throws Exception ex - */ - @Test - public void testActionGet() throws Exception { - int nonExistentActionId = 98775; - String url = BASE_URL + "/controller/history/" + nonExistentActionId; - - // perform the request - ClientResponse response = ADMIN_USER.testGet(url); - - // ensure the request is failed with a forbidden status code - Assert.assertEquals(404, response.getStatus()); - } - - /** - * Verifies the admin user can purge history. - * - * @throws Exception ex - */ - @Test - public void testHistoryDelete() throws Exception { - String url = BASE_URL + "/controller/history/"; - - // create the form data - Map<String, String> formData = new HashMap<>(); - formData.put("endDate", "06/15/2011 13:45:50"); - - // perform the request - ClientResponse response = ADMIN_USER.testDelete(url, formData); - - // ensure the request is failed with a forbidden status code - Assert.assertEquals(200, response.getStatus()); - } - - // ---------------------------------------------- - // USER - // ---------------------------------------------- - /** - * Tests the ability to retrieve the NiFi users. - * - * @throws Exception ex - */ - @Test - public void testUsersGet() throws Exception { - String url = BASE_URL + "/controller/users"; - - // perform the request - ClientResponse response = ADMIN_USER.testGet(url); - - // ensure the request is failed with a forbidden status code - Assert.assertEquals(200, response.getStatus()); - } - - /** - * Tests the ability to retrieve a specific user. - * - * @throws Exception ex - */ - @Test - public void testUserGet() throws Exception { - int nonExistentActionId = 98775; - String url = BASE_URL + "/controller/users/" + nonExistentActionId; - - // perform the request - ClientResponse response = ADMIN_USER.testGet(url); - - // ensure the request is failed with a forbidden status code - Assert.assertEquals(404, response.getStatus()); - } - - /** - * Verifies the admin user can update a person. - * - * @throws Exception ex - */ - //@Test - public void testUserUpdate() throws Exception { - String url = BASE_URL + "/controller/users"; - - // get all the users - ClientResponse getResponse = ADMIN_USER.testGet(url); - Assert.assertEquals(200, getResponse.getStatus()); - - // get the response entity - UsersEntity userCollectionEntity = getResponse.getEntity(UsersEntity.class); - Assert.assertTrue(CollectionUtils.isNotEmpty(userCollectionEntity.getUsers())); - - // get the first user - UserDTO userDTO = (UserDTO) userCollectionEntity.getUsers().toArray()[0]; - - // create the form data - Map<String, String> formData = new HashMap<>(); - formData.put("status", "ACTIVE"); - formData.put("authorities[]", "ROLE_MONITOR"); - - // perform the request - ClientResponse putResponse = ADMIN_USER.testPut(url + "/" + userDTO.getId(), formData); - - // ensure the request succeeded - Assert.assertEquals(200, putResponse.getStatus()); - Assert.assertEquals("ACTIVE", null); // FIXME test should fail, needs to be updated to test updating user by changing the groups the user is in and the name of the user - } - - @AfterClass - public static void cleanup() throws Exception { - // shutdown the server - SERVER.shutdownServer(); - SERVER = null; - - // look for the flow.xml - File flow = new File(FLOW_XML_PATH); - if (flow.exists()) { - flow.delete(); - } - } -}
