Skip to content

Commit

Permalink
Update too many requests error message (#7157)
Browse files Browse the repository at this point in the history
  • Loading branch information
amontenegro authored Dec 17, 2024
1 parent 640b9db commit 733620b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@ public class ApiRateLimitFilter extends OncePerRequestFilter {
private List<String> papiIpWhiteList;
private List<String> papiClientIdWhiteList;

private static final String TOO_MANY_REQUESTS_MSG = "Too Many Requests - You have exceeded the daily allowance of API calls.\\n"
private static final String TOO_MANY_REQUESTS_MSG = "Too Many Requests. You have exceeded the daily quota for anonymous usage of this API. \\n"
+ "You can increase your daily quota by registering for and using Public API client credentials "
+ "(https://info.orcid.org/documentation/integration-guide/registering-a-public-api-client/ )";
+ "(https://info.orcid.org/documentation/integration-guide/registering-a-public-api-client/)";

private static final String SUBJECT = "[ORCID-API] WARNING! You have exceeded the daily Public API Usage Limit - ";

Expand Down Expand Up @@ -211,7 +211,6 @@ private void rateLimitClientRequest(String clientId, LocalDate today) {
// update the request count
rateLimitEntity.setRequestCount(rateLimitEntity.getRequestCount() + 1);
papiRateLimitingDao.updatePublicApiDailyRateLimit(rateLimitEntity, true);

} else {
// create
rateLimitEntity = new PublicApiDailyRateLimitEntity();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import java.io.IOException;

import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.*;

Expand Down Expand Up @@ -179,4 +181,28 @@ public void doFilterInternal_clientRequest_existingEntryTest() throws ServletExc
verify(papiRateLimitingDaoMock, times(1)).updatePublicApiDailyRateLimit(any(PublicApiDailyRateLimitEntity.class), eq(true));
verify(papiRateLimitingDaoMock, never()).persist(any(PublicApiDailyRateLimitEntity.class));
}

@Test
public void doFilterInternal_checkLimitReachedTest() throws ServletException, IOException {
MockitoAnnotations.initMocks(this);
String ip = "127.0.0.2";

PublicApiDailyRateLimitEntity e = new PublicApiDailyRateLimitEntity();
e.setId(1000L);
e.setIpAddress(ip);
e.setRequestCount(10001L);

TargetProxyHelper.injectIntoProxy(apiRateLimitFilter, "enableRateLimiting", true);
TargetProxyHelper.injectIntoProxy(apiRateLimitFilter, "orcidTokenStore", orcidTokenStoreMock);
TargetProxyHelper.injectIntoProxy(apiRateLimitFilter, "papiRateLimitingDao", papiRateLimitingDaoMock);

when(papiRateLimitingDaoMock.findByIpAddressAndRequestDate(eq(ip), any())).thenReturn(e);
httpServletRequestMock.addHeader("X-REAL-IP", ip);

apiRateLimitFilter.doFilterInternal(httpServletRequestMock, httpServletResponseMock, filterChainMock);

assertEquals(429, httpServletResponseMock.getStatus());
String content = httpServletResponseMock.getContentAsString();
assertEquals("Too Many Requests. You have exceeded the daily quota for anonymous usage of this API. \\nYou can increase your daily quota by registering for and using Public API client credentials (https://info.orcid.org/documentation/integration-guide/registering-a-public-api-client/)", content);
}
}

0 comments on commit 733620b

Please sign in to comment.