AbstractService { protected final void assertCanAccess(final AbstractAuthzEntity entity, final String requiredScope) { if (entity == null) { throw new NotFoundException(); } User u = getCurrentUser(); if (u != null && u.equals(entity.getOwner())) { return; } if (getSecurityContext().isUserInRole(requiredScope)) { return; } throw new NotFoundException(); }  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(expected = NotFoundException.class) public void testAssertCanAccessNull() { service.assertCanAccess(null, Scope.APPLICATION_ADMIN); }
@Test public void testAssertCanAccessAsOwner() { ApplicationContext adminContext = getAdminContext().getBuilder() .role("lol") .user() .identity("not_admin") .bearerToken() .build(); ApplicationContext userContext = userApp.getBuilder() .owner(adminContext.getUser()) .build(); Mockito.doReturn(adminContext.getToken()) .when(mockContext).getUserPrincipal(); service.assertCanAccess(userContext.getApplication(), Scope.APPLICATION_ADMIN); }
@Test public void testAssertCanAccessNotOwnerValidScope() { ApplicationContext adminContext = getAdminContext().getBuilder() .bearerToken(Scope.APPLICATION_ADMIN) .build(); Mockito.doReturn(adminContext.getToken()) .when(mockContext).getUserPrincipal(); Mockito.doReturn(true).when(mockContext) .isUserInRole(Scope.APPLICATION_ADMIN); service.assertCanAccess(userApp.getApplication(), Scope.APPLICATION_ADMIN); }
@Test(expected = NotFoundException.class) public void testAssertCanAccessNotOwnerInvalidScope() { ApplicationContext adminContext = getAdminContext().getBuilder() .bearerToken(Scope.APPLICATION) .build(); Mockito.doReturn(adminContext.getToken()) .when(mockContext).getUserPrincipal(); Mockito.doReturn(false).when(mockContext) .isUserInRole(Scope.APPLICATION_ADMIN); service.assertCanAccess(userApp.getApplication(), Scope.APPLICATION_ADMIN); }
@Test public void testAssertCanAccessClientCredentialsValidScope() { ApplicationContext adminContext = getAdminContext().getBuilder() .client(ClientType.ClientCredentials) .authenticator(AuthenticatorType.Test) .bearerToken(Scope.APPLICATION_ADMIN) .build(); Mockito.doReturn(adminContext.getToken()) .when(mockContext).getUserPrincipal(); Mockito.doReturn(true).when(mockContext) .isUserInRole(Scope.APPLICATION_ADMIN); service.assertCanAccess(userApp.getApplication(), Scope.APPLICATION_ADMIN); assertTrue(true); }
@Test(expected = NotFoundException.class) public void testAssertCanAccessClientCredentialsInvalidScope() { ApplicationContext adminContext = getAdminContext().getBuilder() .client(ClientType.ClientCredentials) .authenticator(AuthenticatorType.Test) .bearerToken(Scope.APPLICATION) .build(); Mockito.doReturn(adminContext.getToken()) .when(mockContext).getUserPrincipal(); Mockito.doReturn(false).when(mockContext) .isUserInRole(Scope.APPLICATION_ADMIN); service.assertCanAccess(userApp.getApplication(), Scope.APPLICATION_ADMIN); }