diff --git a/orcid-pub-web/src/main/java/org/orcid/api/filters/ApiRateLimitFilter.java b/orcid-pub-web/src/main/java/org/orcid/api/filters/ApiRateLimitFilter.java index e8826d165a..6e9c5bdd79 100644 --- a/orcid-pub-web/src/main/java/org/orcid/api/filters/ApiRateLimitFilter.java +++ b/orcid-pub-web/src/main/java/org/orcid/api/filters/ApiRateLimitFilter.java @@ -47,7 +47,7 @@ @Component public class ApiRateLimitFilter extends OncePerRequestFilter { - private static Logger LOG = LoggerFactory.getLogger(ApiRateLimitFilter.class); + private static final Logger LOG = LoggerFactory.getLogger(ApiRateLimitFilter.class); @Autowired private PublicApiDailyRateLimitDao papiRateLimitingDao; @@ -61,9 +61,6 @@ public class ApiRateLimitFilter extends OncePerRequestFilter { @Autowired private MailGunManager mailGunManager; - @Autowired - private ProfileDao profileDao; - @Autowired private OrcidUrlManager orcidUrlManager; @@ -242,21 +239,21 @@ private Map createTemplateParams(String clientId, String clientN private void sendEmail(String clientId, LocalDate requestDate) { ClientDetailsEntity clientDetailsEntity = clientDetailsEntityCacheManager.retrieve(clientId); - ProfileEntity profile = profileDao.find(clientDetailsEntity.getGroupProfileId()); - String emailName = recordNameManager.deriveEmailFriendlyName(profile.getId()); - Map templateParams = this.createTemplateParams(clientId, clientDetailsEntity.getClientName(), emailName, profile.getId()); + String memberId = clientDetailsEntity.getGroupProfileId(); + String emailName = recordNameManager.deriveEmailFriendlyName(memberId); + Map templateParams = this.createTemplateParams(clientId, clientDetailsEntity.getClientName(), emailName, memberId); // Generate body from template String body = templateManager.processTemplate("papi_rate_limit_email.ftl", templateParams); // Generate html from template String html = templateManager.processTemplate("papi_rate_limit_email_html.ftl", templateParams); - String email = emailManager.findPrimaryEmail(profile.getId()).getEmail(); + String email = emailManager.findPrimaryEmail(memberId).getEmail(); LOG.info("from address={}", FROM_ADDRESS); LOG.info("text email={}", body); LOG.info("html email={}", html); if (enablePanoplyPapiExceededRateInProduction) { PanoplyPapiDailyRateExceededItem item = new PanoplyPapiDailyRateExceededItem(); item.setClientId(clientId); - item.setOrcid(profile.getId()); + item.setOrcid(memberId); item.setEmail(email); item.setRequestDate(requestDate); setPapiRateExceededItemInPanoply(item); @@ -265,7 +262,7 @@ private void sendEmail(String clientId, LocalDate requestDate) { // Send the email 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); + LOG.error("Failed to send email for papi limits, orcid=" + memberId + " email: " + email); } } diff --git a/orcid-pub-web/src/test/java/org/orcid/api/filters/ApiRateLimitFilterTest.java b/orcid-pub-web/src/test/java/org/orcid/api/filters/ApiRateLimitFilterTest.java index d655284faf..7664f524e2 100644 --- a/orcid-pub-web/src/test/java/org/orcid/api/filters/ApiRateLimitFilterTest.java +++ b/orcid-pub-web/src/test/java/org/orcid/api/filters/ApiRateLimitFilterTest.java @@ -7,6 +7,7 @@ import org.mockito.MockitoAnnotations; import org.orcid.core.oauth.service.OrcidTokenStore; import org.orcid.persistence.dao.PublicApiDailyRateLimitDao; +import org.orcid.persistence.jpa.entities.PublicApiDailyRateLimitEntity; import org.orcid.test.OrcidJUnit4ClassRunner; import org.orcid.test.TargetProxyHelper; import org.springframework.beans.factory.annotation.Autowired; @@ -20,6 +21,7 @@ import java.io.IOException; import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.fail; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.*; @@ -39,19 +41,57 @@ public class ApiRateLimitFilterTest { @Mock private PublicApiDailyRateLimitDao papiRateLimitingDaoMock; - MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest(); + MockHttpServletRequest httpServletRequestMock = new MockHttpServletRequest(); - MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse(); + MockHttpServletResponse httpServletResponseMock = new MockHttpServletResponse(); @Test - public void enableRateLimitingDisabledTest() throws ServletException, IOException { + public void doFilterInternal_rateLimitingDisabledTest() 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)); + + apiRateLimitFilter.doFilterInternal(httpServletRequestMock, httpServletResponseMock, filterChainMock); + + verify(filterChainMock, times(1)).doFilter(eq(httpServletRequestMock), eq(httpServletResponseMock)); verify(orcidTokenStoreMock, never()).readClientId(anyString()); verify(papiRateLimitingDaoMock, never()).findByIpAddressAndRequestDate(anyString(), any()); verify(papiRateLimitingDaoMock, never()).persist(any()); } -} + + @Test + public void doFilterInternal_annonymousRequest_newEntryTest() throws ServletException, IOException { + String ip = "127.0.0.1"; + MockitoAnnotations.initMocks(this); + TargetProxyHelper.injectIntoProxy(apiRateLimitFilter, "enableRateLimiting", false); + TargetProxyHelper.injectIntoProxy(apiRateLimitFilter, "orcidTokenStore", orcidTokenStoreMock); + + PublicApiDailyRateLimitEntity e = new PublicApiDailyRateLimitEntity(); + e.setId(1000L); + e.setIpAddress(ip); + e.setRequestCount(100L); + + when(papiRateLimitingDaoMock.findByIpAddressAndRequestDate(eq(ip), any())).thenReturn(e); + httpServletRequestMock.setAttribute("X-FORWARDED-FOR", ip); + + apiRateLimitFilter.doFilterInternal(httpServletRequestMock, httpServletResponseMock, filterChainMock); + + verify(orcidTokenStoreMock, never()).readClientId(anyString()); + verify(papiRateLimitingDaoMock, times(1)).updatePublicApiDailyRateLimit(any(), eq(false)); + } + + @Test + public void doFilterInternal_annonymousRequest_existingEntryTest() throws ServletException, IOException { + fail(); + } + + @Test + public void doFilterInternal_clientRequest_newEntryTest() throws ServletException, IOException { + fail(); + } + + @Test + public void doFilterInternal_clientRequest_existingEntryTest() throws ServletException, IOException { + fail(); + } +} \ No newline at end of file