ApiKeyInterceptor extends AbstractInterceptor { int isAllowed(ActionInvocation invocation) { ActionContext context = invocation.getInvocationContext(); Map<String, Object> parameters = context.getParameters(); String[] keys = (String[]) parameters.get("key"); if (keys == null || keys.length == 0) { return HttpServletResponse.SC_UNAUTHORIZED; } Status status = _keyService.getPermission(keys[0], "api"); if (status == Status.AUTHORIZED) { return HttpServletResponse.SC_OK; } if (status == Status.RATE_EXCEEDED) { return RESPONSE_TOO_MANY_REQUESTS; } return HttpServletResponse.SC_FORBIDDEN; }  @Override String intercept(ActionInvocation invocation); @Autowired void setKeyService(ApiKeyPermissionService _keyService); ApiKeyPermissionService getKeyService(); String serializeResponse(HttpServletResponse servletResponse); void setType(String type);  }
@Test public void testIsAllowedNoUser() { assertNotNull(_params.remove("key")); assertEquals(401, _aki.isAllowed(_ai)); }
@Test public void testIsAllowedValidUser() { when(_ks.getPermission((String)anyObject(), (String)anyObject())).thenReturn(Status.AUTHORIZED); assertEquals(200, _aki.isAllowed(_ai)); }
@Test public void testIsAllowedRateLimit() { when(_ks.getPermission((String)anyObject(), (String)anyObject())).thenReturn(Status.RATE_EXCEEDED); assertEquals(429, _aki.isAllowed(_ai)); }
@Test public void testIsAllowedUnauthorized() { when(_ks.getPermission((String)anyObject(), (String)anyObject())).thenReturn(Status.UNAUTHORIZED); assertEquals(403, _aki.isAllowed(_ai)); }