AbstractService { protected final <T extends AbstractAuthzEntity> T resolveFilterEntity( final Class<T> roleClass, final BigInteger entityId) { String ownerScope = getAccessScope(); String adminScope = getAdminScope(); T entity = resolveEntityInput(roleClass, entityId); if (getSecurityContext().isUserInRole(adminScope)) { return entity; } if (!getSecurityContext().isUserInRole(ownerScope)) { throw new InvalidScopeException(); } User currentUser = getCurrentUser(); if (currentUser == null) { throw new InvalidScopeException(); } if (entityId == null) { return null; } if (!currentUser.equals(entity.getOwner())) { throw new BadRequestException(); } return entity; }  final InjectionManager getInjector(); final void setInjector(final InjectionManager injector); final UriInfo getUriInfo(); final void setUriInfo(final UriInfo uriInfo); final Session getSession(); final void setSession(final Session session); final SearchFactory getSearchFactory(); final void setSearchFactory(final SearchFactory searchFactory); final FullTextSession getFullTextSession(); final void setFullTextSession(
            final FullTextSession fullTextSession); final Configuration getConfig(); final void setConfig(final Configuration config); final SecurityContext getSecurityContext(); final void setSecurityContext(
            final SecurityContext securityContext);  }
@Test public void testResolveFilterEntityNoEntityAdmin() { ApplicationContext adminContext = getAdminContext().getBuilder() .client(ClientType.Implicit) .authenticator(AuthenticatorType.Test) .bearerToken(Scope.APPLICATION) .build(); Mockito.doReturn(adminContext.getToken()) .when(mockContext).getUserPrincipal(); Mockito.doReturn(true).when(mockContext) .isUserInRole(Scope.APPLICATION_ADMIN); Application result = service.resolveFilterEntity(Application.class, null); assertNull(result); }
@Test(expected = BadRequestException.class) public void testResolveFilterEntityNonexistentEntityIdAdmin() { ApplicationContext adminContext = getAdminContext().getBuilder() .client(ClientType.Implicit) .authenticator(AuthenticatorType.Test) .bearerToken(Scope.APPLICATION) .build(); Mockito.doReturn(adminContext.getToken()) .when(mockContext).getUserPrincipal(); Mockito.doReturn(true).when(mockContext) .isUserInRole(Scope.APPLICATION_ADMIN); service.resolveFilterEntity(Application.class, IdUtil.next()); }
@Test public void testResolveFilterEntityValidEntityIdAdmin() { ApplicationContext adminContext = getAdminContext().getBuilder() .client(ClientType.Implicit) .authenticator(AuthenticatorType.Test) .bearerToken(Scope.APPLICATION) .build(); Mockito.doReturn(adminContext.getToken()) .when(mockContext).getUserPrincipal(); Mockito.doReturn(true).when(mockContext) .isUserInRole(Scope.APPLICATION_ADMIN); Application a = service.resolveFilterEntity(Application.class, userApp.getApplication().getId()); assertEquals(userApp.getApplication(), a); }
@Test(expected = InvalidScopeException.class) public void testResolveFilterEntityInvalidScope() { ApplicationContext adminContext = getAdminContext().getBuilder() .client(ClientType.Implicit) .authenticator(AuthenticatorType.Test) .bearerToken(Scope.APPLICATION) .build(); Mockito.doReturn(adminContext.getToken()) .when(mockContext).getUserPrincipal(); Mockito.doReturn(true).when(mockContext) .isUserInRole(Scope.USER); service.resolveFilterEntity(Application.class, userApp.getApplication().getId()); }
@Test public void testResolveFilterEntityNullEntityIdNonAdmin() { ApplicationContext adminContext = getAdminContext().getBuilder() .client(ClientType.Implicit) .authenticator(AuthenticatorType.Test) .bearerToken(Scope.APPLICATION) .build(); Mockito.doReturn(adminContext.getToken()) .when(mockContext).getUserPrincipal(); Mockito.doReturn(true).when(mockContext) .isUserInRole(Scope.APPLICATION); Application a = service.resolveFilterEntity(Application.class, null); assertNull(a); }
@Test(expected = BadRequestException.class) public void testResolveFilterEntityNonexistentEntityIdNonAdmin() { ApplicationContext adminContext = getAdminContext().getBuilder() .client(ClientType.Implicit) .authenticator(AuthenticatorType.Test) .bearerToken(Scope.APPLICATION) .build(); Mockito.doReturn(adminContext.getToken()) .when(mockContext).getUserPrincipal(); Mockito.doReturn(true).when(mockContext) .isUserInRole(Scope.APPLICATION); service.resolveFilterEntity(Application.class, IdUtil.next()); }
@Test(expected = InvalidScopeException.class) public void testResolveFilterEntityNoTokenIdentity() { ApplicationContext adminContext = getAdminContext().getBuilder() .client(ClientType.ClientCredentials) .bearerToken(Scope.APPLICATION) .build(); Mockito.doReturn(adminContext.getToken()) .when(mockContext).getUserPrincipal(); Mockito.doReturn(true).when(mockContext) .isUserInRole(Scope.APPLICATION); service.resolveFilterEntity(Application.class, adminContext.getApplication().getId()); }
@Test(expected = BadRequestException.class) public void testResolveFilterEntityNonAdminNonOwner() { ApplicationContext adminContext = getAdminContext().getBuilder() .user().identity() .build(); ApplicationContext userContext = userApp.getBuilder() .owner(adminContext.getUser()) .build(); adminContext = adminContext.getBuilder() .user() .identity() .bearerToken(Scope.APPLICATION) .build(); Mockito.doReturn(adminContext.getToken()) .when(mockContext).getUserPrincipal(); Mockito.doReturn(true).when(mockContext) .isUserInRole(Scope.APPLICATION); service.resolveFilterEntity(Application.class, userContext.getApplication().getId()); }
@Test public void testResolveFilterEntityNonAdminOwner() { ApplicationContext adminContext = getAdminContext().getBuilder() .user().identity() .bearerToken(Scope.APPLICATION) .build(); ApplicationContext userContext = userApp.getBuilder() .owner(adminContext.getUser()) .build(); Mockito.doReturn(adminContext.getToken()) .when(mockContext).getUserPrincipal(); Mockito.doReturn(true).when(mockContext) .isUserInRole(Scope.APPLICATION); Application a = service.resolveFilterEntity(Application.class, userContext.getApplication().getId()); assertEquals(adminContext.getUser(), a.getOwner()); }