-
Notifications
You must be signed in to change notification settings - Fork 153
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Coding done, working on some unit tests
- Loading branch information
1 parent
653cb2f
commit 6bbe5d8
Showing
5 changed files
with
85 additions
and
19 deletions.
There are no files selected for viewing
11 changes: 6 additions & 5 deletions
11
orcid-core/src/main/resources/org/orcid/core/template/papi_rate_limit_email.ftl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -110,11 +110,14 @@ public class ApiRateLimitFilter extends OncePerRequestFilter { | |
+ "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/ )"; | ||
|
||
private static final String SUBJECT = "[ORCID] You have exceeded the daily Public API Usage Limit - "; | ||
private static final String SUBJECT = "[ORCID-API] WARNING! You have exceeded the daily Public API Usage Limit - "; | ||
|
||
@Value("${org.orcid.papi.rate.limit.fromEmail:notify@notify.orcid.org}") | ||
@Value("${org.orcid.papi.rate.limit.fromEmail:apiusage@orcid.org}") | ||
private String FROM_ADDRESS; | ||
|
||
@Value("${org.orcid.papi.rate.limit.ccAddress:[email protected]}") | ||
private String CC_ADDRESS; | ||
|
||
@Override | ||
public void afterPropertiesSet() throws ServletException { | ||
super.afterPropertiesSet(); | ||
|
@@ -123,7 +126,7 @@ public void afterPropertiesSet() throws ServletException { | |
} | ||
|
||
@Override | ||
protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) | ||
public void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) | ||
throws ServletException, IOException { | ||
LOG.trace("ApiRateLimitFilter starts, rate limit is : " + enableRateLimiting); | ||
if (enableRateLimiting) { | ||
|
@@ -197,7 +200,6 @@ private void rateLimitAnonymousRequest(String ipAddress, LocalDate today, HttpSe | |
|
||
} | ||
return; | ||
|
||
} | ||
|
||
private void rateLimitClientRequest(String clientId, LocalDate today) { | ||
|
@@ -261,7 +263,7 @@ private void sendEmail(String clientId, LocalDate requestDate) { | |
} | ||
|
||
// Send the email | ||
boolean mailSent = mailGunManager.sendEmail(FROM_ADDRESS, email, SUBJECT, body, html); | ||
boolean mailSent = mailGunManager.sendEmailWithCC(FROM_ADDRESS, email, CC_ADDRESS, SUBJECT, body, html); | ||
if (!mailSent) { | ||
LOG.error("Failed to send email for papi limits, orcid=" + profile.getId() + " email: " + email); | ||
} | ||
|
@@ -281,7 +283,6 @@ private void setPapiRateExceededItemInPanoply(PanoplyPapiDailyRateExceededItem i | |
if (!result) { | ||
LOG.error("Async call to panoply for : " + item.toString() + " Stored: " + result); | ||
} | ||
|
||
}); | ||
} | ||
|
||
|
@@ -307,5 +308,4 @@ private boolean isWhiteListed(String ipAddress) { | |
private boolean isClientIdWhiteListed(String clientId) { | ||
return (papiClientIdWhiteList != null)?papiClientIdWhiteList.contains(clientId):false; | ||
} | ||
|
||
} |
57 changes: 57 additions & 0 deletions
57
orcid-pub-web/src/test/java/org/orcid/api/filters/ApiRateLimitFilterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package org.orcid.api.filters; | ||
|
||
import org.apache.commons.math3.stat.inference.TestUtils; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.Mock; | ||
import org.mockito.MockitoAnnotations; | ||
import org.orcid.core.oauth.service.OrcidTokenStore; | ||
import org.orcid.persistence.dao.PublicApiDailyRateLimitDao; | ||
import org.orcid.test.OrcidJUnit4ClassRunner; | ||
import org.orcid.test.TargetProxyHelper; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.mock.web.MockHttpServletRequest; | ||
import org.springframework.mock.web.MockHttpServletResponse; | ||
import org.springframework.test.context.ContextConfiguration; | ||
|
||
import javax.annotation.Resource; | ||
import javax.servlet.FilterChain; | ||
import javax.servlet.ServletException; | ||
import java.io.IOException; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.mockito.ArgumentMatchers.eq; | ||
import static org.mockito.Mockito.*; | ||
|
||
@RunWith(OrcidJUnit4ClassRunner.class) | ||
@ContextConfiguration(locations = { "classpath:test-orcid-t1-web-context.xml" }) | ||
public class ApiRateLimitFilterTest { | ||
|
||
@Resource | ||
public ApiRateLimitFilter apiRateLimitFilter; | ||
|
||
@Mock | ||
private FilterChain filterChainMock; | ||
|
||
@Mock | ||
private OrcidTokenStore orcidTokenStoreMock; | ||
|
||
@Mock | ||
private PublicApiDailyRateLimitDao papiRateLimitingDaoMock; | ||
|
||
MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest(); | ||
|
||
MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse(); | ||
|
||
@Test | ||
public void enableRateLimitingDisabledTest() throws ServletException, IOException { | ||
MockitoAnnotations.initMocks(this); | ||
TargetProxyHelper.injectIntoProxy(apiRateLimitFilter, "enableRateLimiting", false); | ||
TargetProxyHelper.injectIntoProxy(apiRateLimitFilter, "orcidTokenStore", orcidTokenStoreMock); | ||
apiRateLimitFilter.doFilterInternal(mockHttpServletRequest, mockHttpServletResponse, filterChainMock); | ||
verify(filterChainMock, times(1)).doFilter(eq(mockHttpServletRequest), eq(mockHttpServletResponse)); | ||
verify(orcidTokenStoreMock, never()).readClientId(anyString()); | ||
verify(papiRateLimitingDaoMock, never()).findByIpAddressAndRequestDate(anyString(), any()); | ||
verify(papiRateLimitingDaoMock, never()).persist(any()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters