AbstractService { protected final User resolveOwnershipFilter(final BigInteger ownerId) { String adminScope = getAdminScope(); if (getSecurityContext().isUserInRole(adminScope)) { if (ownerId == null) { return null; } User owner = getSession().get(User.class, ownerId); if (owner == null) { throw new BadRequestException(); } return owner; } User currentUser = getCurrentUser(); if (currentUser == null) { throw new InvalidScopeException(); } if (ownerId != null && !currentUser.getId().equals(ownerId)) { throw new InvalidScopeException(); } return currentUser; }  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 testRequestUserFilterAdminNoFilter() { 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); User user = service.resolveOwnershipFilter(null); assertNull(user); }
@Test(expected = BadRequestException.class) public void testRequestInvalidUserFilterAdminFilter() { ApplicationContext adminContext = getAdminContext().getBuilder() .client(ClientType.Implicit) .authenticator(AuthenticatorType.Test) .bearerToken(Scope.APPLICATION) .user() .build(); Mockito.doReturn(adminContext.getToken()) .when(mockContext).getUserPrincipal(); Mockito.doReturn(true).when(mockContext) .isUserInRole(Scope.APPLICATION_ADMIN); service.resolveOwnershipFilter(IdUtil.next()); }
@Test public void testRequestUserFilterAdminFilter() { ApplicationContext adminContext = getAdminContext().getBuilder() .client(ClientType.Implicit) .authenticator(AuthenticatorType.Test) .bearerToken(Scope.APPLICATION) .user() .build(); Mockito.doReturn(adminContext.getToken()) .when(mockContext).getUserPrincipal(); Mockito.doReturn(true).when(mockContext) .isUserInRole(Scope.APPLICATION_ADMIN); User requestedUser = adminContext.getUser(); User user = service.resolveOwnershipFilter(requestedUser.getId()); assertEquals(requestedUser, user); }
@Test public void testRequestUserFilterNonAdminNoFilter() { 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); User requestedUser = adminContext.getUser(); User user = service.resolveOwnershipFilter(null); assertEquals(adminContext.getUser(), user); }
@Test(expected = InvalidScopeException.class) public void testRequestUserFilterNonAdminFilter() { ApplicationContext adminContext = getAdminContext().getBuilder() .client(ClientType.Implicit) .authenticator(AuthenticatorType.Test) .bearerToken(Scope.APPLICATION) .user() .build(); Mockito.doReturn(adminContext.getToken()) .when(mockContext).getUserPrincipal(); Mockito.doReturn(true).when(mockContext) .isUserInRole(Scope.APPLICATION); User requestedUser = adminContext.getUser(); service.resolveOwnershipFilter(requestedUser.getId()); }
@Test public void testRequestUserFilterNonAdminFilterSelf() { 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); User requestedUser = adminContext.getToken().getIdentity().getUser(); User user = service.resolveOwnershipFilter(requestedUser.getId()); assertEquals(requestedUser, user); }
@Test(expected = InvalidScopeException.class) public void testRequestUserFilterNonAdminClientCredentials() { ApplicationContext adminContext = getAdminContext().getBuilder() .client(ClientType.ClientCredentials) .authenticator(AuthenticatorType.Test) .bearerToken(Scope.APPLICATION) .build(); Mockito.doReturn(adminContext.getToken()) .when(mockContext).getUserPrincipal(); Mockito.doReturn(true).when(mockContext) .isUserInRole(Scope.APPLICATION); service.resolveOwnershipFilter(adminContext.getUser().getId()); }