API { public boolean hasPermission(Permission permission) { Optional<Session> session = getSession(); if (!session.isPresent()) { return false; } if (permission.equals(Permission.ANY_PERMISSION)) { return true; } if (authorizer == null) { return false; } try { return authorizer.hasPermission(session.get().getUser(), permission); } catch (AuthorizationException e) { throw new PluginExecutionException( "Cannot check permission for user '" + session.get().getUser().getId() + "' using authorizer '" + authorizer.getClass().getName() + "'.", e); } } API(SessionManager sessionManager, Authorizer authorizer, RequestLookup requestLookup); RequestLookup getRequestLookup(); static Object callOSGiService(String serviceClassName, String serviceMethodName, Object... args); static Map<String, Object> getOSGiServices(String serviceClassName); static void callMicroService(); static void sendError(int status, String message); static void sendRedirect(String redirectUrl); Session createSession(User user); Optional<Session> getSession(); boolean hasPermission(Permission permission); boolean destroySession();  }
@Test public void testIsAuthorizedWithAnyPermission() throws Exception { HttpRequest request = mock(HttpRequest.class); when(request.getContextPath()).thenReturn("/test"); SessionManager sessionManager = createSessionManager(); mockGetSession(sessionManager); RequestLookup requestLookup = new RequestLookup("/test", request, null); API api = new API(sessionManager, null, requestLookup); Assert.assertTrue(api.hasPermission(Permission.ANY_PERMISSION)); }
@Test public void testIsAuthorizedWithNullAuthorizer() throws Exception { HttpRequest request = mock(HttpRequest.class); when(request.getContextPath()).thenReturn("/test"); SessionManager sessionManager = createSessionManager(); mockGetSession(sessionManager); RequestLookup requestLookup = new RequestLookup("/test", request, null); API api = new API(sessionManager, null, requestLookup); Permission permission = mock(Permission.class); Assert.assertFalse(api.hasPermission(permission)); }
@Test public void testIsAuthorizedWithNoSession() { HttpRequest request = mock(HttpRequest.class); when(request.getContextPath()).thenReturn("/test"); SessionManager sessionManager = createSessionManager(); RequestLookup requestLookup = new RequestLookup("/test", request, null); API api = new API(sessionManager, null, requestLookup); Assert.assertFalse(api.hasPermission(null)); }