CheckSubscriptionPolicy extends AbstractPolicy { @Override protected void onRequest(Request request, Response response, PolicyChain policyChain, ExecutionContext executionContext) throws PolicyException { SubscriptionRepository subscriptionRepository = executionContext.getComponent(SubscriptionRepository.class); String api = (String) executionContext.getAttribute(ExecutionContext.ATTR_API); String clientId = (String) executionContext.getAttribute(CONTEXT_ATTRIBUTE_CLIENT_ID); request.metrics().setSecurityType(JWT); request.metrics().setSecurityToken(clientId); try { List<Subscription> subscriptions = subscriptionRepository.search( new SubscriptionCriteria.Builder() .apis(Collections.singleton(api)) .clientId(clientId) .status(Subscription.Status.ACCEPTED) .build()); if (subscriptions != null && !subscriptions.isEmpty()) { Subscription subscription = subscriptions.get(0); if (subscription.getClientId().equals(clientId) && ( subscription.getEndingAt() == null || subscription.getEndingAt().after(new Date(request.timestamp())))) { executionContext.setAttribute(ExecutionContext.ATTR_APPLICATION, subscription.getApplication()); executionContext.setAttribute(ExecutionContext.ATTR_SUBSCRIPTION_ID, subscription.getId()); executionContext.setAttribute(ExecutionContext.ATTR_PLAN, subscription.getPlan()); policyChain.doNext(request, response); return; } } sendUnauthorized(GATEWAY_OAUTH2_ACCESS_DENIED_KEY, policyChain, OAUTH2_ERROR_ACCESS_DENIED); } catch (TechnicalException te) { sendUnauthorized(GATEWAY_OAUTH2_SERVER_ERROR_KEY, policyChain, OAUTH2_ERROR_SERVER_ERROR); } }  @Override String id();  }
@Test public void shouldReturnUnauthorized_onException() throws PolicyException, TechnicalException { CheckSubscriptionPolicy policy = new CheckSubscriptionPolicy(); Response response = mock(Response.class); PolicyChain policyChain = mock(PolicyChain.class); ExecutionContext executionContext = mock(ExecutionContext.class); SubscriptionRepository subscriptionRepository = mock(SubscriptionRepository.class); when(executionContext.getComponent(SubscriptionRepository.class)).thenReturn(subscriptionRepository); when(subscriptionRepository.search(any(SubscriptionCriteria.class))) .thenThrow(TechnicalException.class); policy.onRequest(request, response, policyChain, executionContext); verify(policyChain, times(1)).failWith(argThat( result -> result.statusCode() == HttpStatusCode.UNAUTHORIZED_401 && CheckSubscriptionPolicy.GATEWAY_OAUTH2_SERVER_ERROR_KEY.equals(result.key()))); }
@Test public void shouldReturnUnauthorized_badClient() throws PolicyException, TechnicalException { CheckSubscriptionPolicy policy = new CheckSubscriptionPolicy(); Response response = mock(Response.class); PolicyChain policyChain = mock(PolicyChain.class); ExecutionContext executionContext = mock(ExecutionContext.class); when(executionContext.getAttribute(CheckSubscriptionPolicy.CONTEXT_ATTRIBUTE_CLIENT_ID)).thenReturn("my-client-id"); SubscriptionRepository subscriptionRepository = mock(SubscriptionRepository.class); when(executionContext.getComponent(SubscriptionRepository.class)).thenReturn(subscriptionRepository); Subscription subscription = mock(Subscription.class); when(subscription.getClientId()).thenReturn("my-bad-client-id"); when(subscriptionRepository.search(any(SubscriptionCriteria.class))) .thenReturn(Collections.singletonList(subscription)); policy.onRequest(request, response, policyChain, executionContext); verify(policyChain, times(1)).failWith(argThat( result -> result.statusCode() == HttpStatusCode.UNAUTHORIZED_401 && CheckSubscriptionPolicy.GATEWAY_OAUTH2_ACCESS_DENIED_KEY.equals(result.key()))); }
@Test public void shouldContinue() throws PolicyException, TechnicalException { CheckSubscriptionPolicy policy = new CheckSubscriptionPolicy(); Response response = mock(Response.class); PolicyChain policyChain = mock(PolicyChain.class); ExecutionContext executionContext = mock(ExecutionContext.class); when(executionContext.getAttribute(CheckSubscriptionPolicy.CONTEXT_ATTRIBUTE_CLIENT_ID)).thenReturn("my-client-id"); SubscriptionRepository subscriptionRepository = mock(SubscriptionRepository.class); when(executionContext.getComponent(SubscriptionRepository.class)).thenReturn(subscriptionRepository); Subscription subscription = mock(Subscription.class); when(subscription.getClientId()).thenReturn("my-client-id"); when(subscriptionRepository.search(any(SubscriptionCriteria.class))) .thenReturn(Collections.singletonList(subscription)); policy.onRequest(request, response, policyChain, executionContext); verify(policyChain, times(1)).doNext(request, response); }