From f982cd31f53c5e01a0f4ece54b7dcb5452b49b65 Mon Sep 17 00:00:00 2001 From: Raj Shah Date: Tue, 12 Sep 2023 12:25:19 +0530 Subject: [PATCH 1/4] Added cache constant changes --- .../api/config/CoreConstants.java | 8 ++++ .../api/config/MemcachedConfig.java | 8 ++-- .../lib/globalConstants/CacheConstants.java | 41 ++++++++++++++----- .../SalesforceOauthTokenRepository.java | 12 ++++-- .../SalesforceUserRepository.java | 12 ++++-- 5 files changed, 58 insertions(+), 23 deletions(-) diff --git a/src/main/java/com/salessparrow/api/config/CoreConstants.java b/src/main/java/com/salessparrow/api/config/CoreConstants.java index 270174d2..9e9b3798 100644 --- a/src/main/java/com/salessparrow/api/config/CoreConstants.java +++ b/src/main/java/com/salessparrow/api/config/CoreConstants.java @@ -25,6 +25,14 @@ public static Boolean isLocalTestEnvironment() { return environment().equals("local-test"); } + public static Boolean isStagingEnvironment() { + return environment().equals("staging"); + } + + public static Boolean isProductionEnvironment() { + return environment().equals("production"); + } + /* End: Env variables required before spring application context is initialized */ public static String cookieDomain() { diff --git a/src/main/java/com/salessparrow/api/config/MemcachedConfig.java b/src/main/java/com/salessparrow/api/config/MemcachedConfig.java index 6d0e6ce1..b8c68e0b 100644 --- a/src/main/java/com/salessparrow/api/config/MemcachedConfig.java +++ b/src/main/java/com/salessparrow/api/config/MemcachedConfig.java @@ -59,10 +59,10 @@ public CacheManager cacheManager() { private Collection internalCaches(MemcachedClient cache) { final Collection caches = new ArrayList<>(); - caches.add(new Memcached(CacheConstants.SS_SALESFORCE_USER_CACHE, CacheConstants.SS_SALESFORCE_USER_CACHE_EXP, - cache)); - caches.add(new Memcached(CacheConstants.SS_SALESFORCE_OAUTH_TOKEN_CACHE, - CacheConstants.SS_SALESFORCE_OAUTH_TOKEN_CACHE_EXP, cache)); + caches + .add(new Memcached(CacheConstants.SALESFORCE_USER_CACHE, CacheConstants.SALESFORCE_USER_CACHE_EXP, cache)); + caches.add(new Memcached(CacheConstants.SALESFORCE_OAUTH_TOKEN_CACHE, + CacheConstants.SALESFORCE_OAUTH_TOKEN_CACHE_EXP, cache)); return caches; } diff --git a/src/main/java/com/salessparrow/api/lib/globalConstants/CacheConstants.java b/src/main/java/com/salessparrow/api/lib/globalConstants/CacheConstants.java index c2863d9e..bb4ae3e5 100644 --- a/src/main/java/com/salessparrow/api/lib/globalConstants/CacheConstants.java +++ b/src/main/java/com/salessparrow/api/lib/globalConstants/CacheConstants.java @@ -1,20 +1,39 @@ package com.salessparrow.api.lib.globalConstants; import org.springframework.stereotype.Component; +import com.salessparrow.api.config.CoreConstants; @Component public class CacheConstants { - public static final String SalesSparrowPrefix = "ss_"; - - public static final String SS_SALESFORCE_USER_CACHE = SalesSparrowPrefix + "sf_user"; - - public static final Integer SS_SALESFORCE_USER_CACHE_EXP = 30 * 24 * 60 * 60; // 30 - // days - - public static final String SS_SALESFORCE_OAUTH_TOKEN_CACHE = SalesSparrowPrefix + "sf_oauth_token"; - - public static final Integer SS_SALESFORCE_OAUTH_TOKEN_CACHE_EXP = 30 * 24 * 60 * 60; // 30 - // days + public static final String CACHE_PREFIX; + + static { + if (CoreConstants.isProductionEnvironment()) { + CACHE_PREFIX = "prod_"; + } + else if (CoreConstants.isStagingEnvironment()) { + CACHE_PREFIX = "stag_"; + } + else if (CoreConstants.isTestEnvironment()) { + CACHE_PREFIX = "test_"; + } + else if (CoreConstants.isLocalTestEnvironment()) { + CACHE_PREFIX = "ltest_"; + } + else { + CACHE_PREFIX = "dev_"; + } + } + + public static final String SALESFORCE_USER_CACHE = "sf_user"; + + public static final Integer SALESFORCE_USER_CACHE_EXP = 30 * 24 * 60 * 60; // 30 + // days + + public static final String SALESFORCE_OAUTH_TOKEN_CACHE = "sf_oauth_token"; + + public static final Integer SALESFORCE_OAUTH_TOKEN_CACHE_EXP = 30 * 24 * 60 * 60; // 30 + // days } diff --git a/src/main/java/com/salessparrow/api/repositories/SalesforceOauthTokenRepository.java b/src/main/java/com/salessparrow/api/repositories/SalesforceOauthTokenRepository.java index e5896a6b..6dab2d73 100644 --- a/src/main/java/com/salessparrow/api/repositories/SalesforceOauthTokenRepository.java +++ b/src/main/java/com/salessparrow/api/repositories/SalesforceOauthTokenRepository.java @@ -28,7 +28,8 @@ public SalesforceOauthTokenRepository(DynamoDBMapper dynamoDBMapper) { * @param salesforceOauthToken * @return SalesforceOauthToken */ - @CacheEvict(value = CacheConstants.SS_SALESFORCE_OAUTH_TOKEN_CACHE, key = "#salesforceOauthToken.externalUserId") + @CacheEvict(value = CacheConstants.SALESFORCE_OAUTH_TOKEN_CACHE, + key = "T(com.salessparrow.api.lib.globalConstants.CacheConstants).CACHE_PREFIX + #salesforceOauthToken.externalUserId") public SalesforceOauthToken createSalesforceOauthToken(SalesforceOauthToken salesforceOauthToken) { // Create a row with status active and created at as current time salesforceOauthToken.setStatus(SalesforceOauthToken.Status.ACTIVE); @@ -48,7 +49,8 @@ public SalesforceOauthToken createSalesforceOauthToken(SalesforceOauthToken sale * @param salesforceOauthToken * @return SalesforceOauthToken */ - @CacheEvict(value = CacheConstants.SS_SALESFORCE_OAUTH_TOKEN_CACHE, key = "#salesforceOauthToken.externalUserId") + @CacheEvict(value = CacheConstants.SALESFORCE_OAUTH_TOKEN_CACHE, + key = "T(com.salessparrow.api.lib.globalConstants.CacheConstants).CACHE_PREFIX + #salesforceOauthToken.externalUserId") public SalesforceOauthToken updateSalesforceOauthToken(SalesforceOauthToken salesforceOauthToken) { try { dynamoDBMapper.save(salesforceOauthToken); @@ -65,7 +67,8 @@ public SalesforceOauthToken updateSalesforceOauthToken(SalesforceOauthToken sale * @param externalUserId * @return SalesforceOauthToken */ - @Cacheable(value = CacheConstants.SS_SALESFORCE_OAUTH_TOKEN_CACHE, key = "#externalUserId") + @Cacheable(value = CacheConstants.SALESFORCE_OAUTH_TOKEN_CACHE, + key = "T(com.salessparrow.api.lib.globalConstants.CacheConstants).CACHE_PREFIX + #externalUserId") public SalesforceOauthToken getSalesforceOauthTokenByExternalUserId(String externalUserId) { try { return dynamoDBMapper.load(SalesforceOauthToken.class, externalUserId); @@ -81,7 +84,8 @@ public SalesforceOauthToken getSalesforceOauthTokenByExternalUserId(String exter * @param salesforceOauthToken * @return void */ - @CacheEvict(value = CacheConstants.SS_SALESFORCE_OAUTH_TOKEN_CACHE, key = "#salesforceOauthToken.externalUserId") + @CacheEvict(value = CacheConstants.SALESFORCE_OAUTH_TOKEN_CACHE, + key = "T(com.salessparrow.api.lib.globalConstants.CacheConstants).CACHE_PREFIX + #salesforceOauthToken.externalUserId") public void deleteSalesforceOauthTokenBySalesforceOauthToken(SalesforceOauthToken salesforceOauthToken) { try { dynamoDBMapper.delete(salesforceOauthToken); diff --git a/src/main/java/com/salessparrow/api/repositories/SalesforceUserRepository.java b/src/main/java/com/salessparrow/api/repositories/SalesforceUserRepository.java index 97043c58..7e74d38a 100644 --- a/src/main/java/com/salessparrow/api/repositories/SalesforceUserRepository.java +++ b/src/main/java/com/salessparrow/api/repositories/SalesforceUserRepository.java @@ -29,7 +29,8 @@ public SalesforceUserRepository(DynamoDBMapper dynamoDBMapper) { * @param salesforceUser * @return SalesforceUser */ - @CacheEvict(value = CacheConstants.SS_SALESFORCE_USER_CACHE, key = "#salesforceUser.externalUserId") + @CacheEvict(value = CacheConstants.SALESFORCE_USER_CACHE, + key = "T(com.salessparrow.api.lib.globalConstants.CacheConstants).CACHE_PREFIX + #salesforceUser.externalUserId") public SalesforceUser createSalesforceUser(SalesforceUser salesforceUser) { // Create a row with status active and created at as current time salesforceUser.setStatus(SalesforceUser.Status.ACTIVE); @@ -49,7 +50,8 @@ public SalesforceUser createSalesforceUser(SalesforceUser salesforceUser) { * @param salesforceUser * @return SalesforceUser */ - @CacheEvict(value = CacheConstants.SS_SALESFORCE_USER_CACHE, key = "#salesforceUser.externalUserId") + @CacheEvict(value = CacheConstants.SALESFORCE_USER_CACHE, + key = "T(com.salessparrow.api.lib.globalConstants.CacheConstants).CACHE_PREFIX + #salesforceUser.externalUserId") public SalesforceUser updateSalesforceUser(SalesforceUser salesforceUser) { try { dynamoDBMapper.save(salesforceUser); @@ -66,7 +68,8 @@ public SalesforceUser updateSalesforceUser(SalesforceUser salesforceUser) { * @param id * @return SalesforceUser */ - @Cacheable(value = CacheConstants.SS_SALESFORCE_USER_CACHE, key = "#externalUserId") + @Cacheable(value = CacheConstants.SALESFORCE_USER_CACHE, + key = "T(com.salessparrow.api.lib.globalConstants.CacheConstants).CACHE_PREFIX + #externalUserId") public SalesforceUser getSalesforceUserByExternalUserId(String externalUserId) { try { return dynamoDBMapper.load(SalesforceUser.class, externalUserId); @@ -76,7 +79,8 @@ public SalesforceUser getSalesforceUserByExternalUserId(String externalUserId) { } } - @CacheEvict(value = CacheConstants.SS_SALESFORCE_USER_CACHE, key = "#externalUserId") + @CacheEvict(value = CacheConstants.SALESFORCE_USER_CACHE, + key = "T(com.salessparrow.api.lib.globalConstants.CacheConstants).CACHE_PREFIX + #externalUserId") public void removeSalesforceUserData(String externalUserId) { SalesforceUser salesforceUser = getSalesforceUserByExternalUserId(externalUserId); salesforceUser.setIdentityUrl(null); From 846e9c4d27943f09c83023622cec23d3b73c8ceb Mon Sep 17 00:00:00 2001 From: Raj Shah Date: Wed, 13 Sep 2023 10:37:25 +0530 Subject: [PATCH 2/4] Added test user changes --- .../salessparrow/api/config/CoreConstants.java | 8 ++++++++ .../globalConstants/SalesforceConstants.java | 4 ++++ .../lib/globalConstants/SecretConstants.java | 8 ++++++++ .../salesforce/wrappers/SalesforceTokens.java | 18 ++++++++++++++---- .../api/services/salesforce/AuthService.java | 15 ++++++++++++--- .../authController/PostDisconnectTest.java | 5 +++-- .../PostSalesforceConnectTest.java | 9 +++++---- .../wrappers/SalesforceTokensTest.java | 4 ++-- 8 files changed, 56 insertions(+), 15 deletions(-) diff --git a/src/main/java/com/salessparrow/api/config/CoreConstants.java b/src/main/java/com/salessparrow/api/config/CoreConstants.java index 9e9b3798..24bebb94 100644 --- a/src/main/java/com/salessparrow/api/config/CoreConstants.java +++ b/src/main/java/com/salessparrow/api/config/CoreConstants.java @@ -71,6 +71,14 @@ public static String localKmsEndpoint() { return SecretConstants.localKmsEndpoint(); } + public static String defaultTestUser() { + return SecretConstants.defaultTestUser(); + } + + public static String defaultTestUserPassword() { + return SecretConstants.defaultTestUserPassword(); + } + /** * This method returns the memcached address that is going to be used for locals * @return String diff --git a/src/main/java/com/salessparrow/api/lib/globalConstants/SalesforceConstants.java b/src/main/java/com/salessparrow/api/lib/globalConstants/SalesforceConstants.java index 139d5639..e1718598 100644 --- a/src/main/java/com/salessparrow/api/lib/globalConstants/SalesforceConstants.java +++ b/src/main/java/com/salessparrow/api/lib/globalConstants/SalesforceConstants.java @@ -54,6 +54,10 @@ public String authorizationCodeGrantType() { return "authorization_code"; } + public String passwordGrantType() { + return "password"; + } + public String refreshTokenGrantType() { return "refresh_token"; } diff --git a/src/main/java/com/salessparrow/api/lib/globalConstants/SecretConstants.java b/src/main/java/com/salessparrow/api/lib/globalConstants/SecretConstants.java index 89d646b0..0e10eefc 100644 --- a/src/main/java/com/salessparrow/api/lib/globalConstants/SecretConstants.java +++ b/src/main/java/com/salessparrow/api/lib/globalConstants/SecretConstants.java @@ -140,6 +140,14 @@ public static String localKmsEndpoint() { return getSecret("LOCAL_KMS_ENDPOINT"); } + public static String defaultTestUser() { + return getSecret("DEFAULT_TEST_USER"); + } + + public static String defaultTestUserPassword() { + return getSecret("DEFAULT_TEST_USER_PASSWORD"); + } + /* Secrets end */ /** diff --git a/src/main/java/com/salessparrow/api/lib/salesforce/wrappers/SalesforceTokens.java b/src/main/java/com/salessparrow/api/lib/salesforce/wrappers/SalesforceTokens.java index 1d7c0764..a152e0f6 100644 --- a/src/main/java/com/salessparrow/api/lib/salesforce/wrappers/SalesforceTokens.java +++ b/src/main/java/com/salessparrow/api/lib/salesforce/wrappers/SalesforceTokens.java @@ -31,13 +31,23 @@ public class SalesforceTokens { * @param redirectUri * @return HttpResponse */ - public HttpResponse getTokens(String code, String redirectUri) { + public HttpResponse getTokens(String code, String redirectUri, Boolean isTestUser) { String salesforceOAuthEndpoint = salesforceConstants.oauth2Url(); - String requestBody = "grant_type=" + salesforceConstants.authorizationCodeGrantType() + "&client_id=" - + CoreConstants.salesforceClientId() + "&client_secret=" + CoreConstants.salesforceClientSecret() - + "&code=" + code + "&redirect_uri=" + redirectUri; + String requestBody; + + if (!isTestUser) { + requestBody = "grant_type=" + salesforceConstants.authorizationCodeGrantType() + "&client_id=" + + CoreConstants.salesforceClientId() + "&client_secret=" + CoreConstants.salesforceClientSecret() + + "&code=" + code + "&redirect_uri=" + redirectUri; + } + else { + requestBody = "grant_type=" + salesforceConstants.passwordGrantType() + "&client_id=" + + CoreConstants.salesforceClientId() + "&client_secret=" + CoreConstants.salesforceClientSecret() + + "&username=" + CoreConstants.defaultTestUser() + "&password=" + + CoreConstants.defaultTestUserPassword() + "&redirect_uri=" + redirectUri; + } Map headers = new HashMap<>(); headers.put("content-type", "application/x-www-form-urlencoded"); diff --git a/src/main/java/com/salessparrow/api/services/salesforce/AuthService.java b/src/main/java/com/salessparrow/api/services/salesforce/AuthService.java index 44839bab..92b32978 100644 --- a/src/main/java/com/salessparrow/api/services/salesforce/AuthService.java +++ b/src/main/java/com/salessparrow/api/services/salesforce/AuthService.java @@ -30,6 +30,7 @@ import com.salessparrow.api.repositories.SalesforceOrganizationRepository; import com.salessparrow.api.repositories.SalesforceUserRepository; import jakarta.servlet.http.HttpServletRequest; +import software.amazon.awssdk.services.secretsmanager.endpoints.internal.Value.Bool; @Service public class AuthService { @@ -93,10 +94,17 @@ public AuthServiceDto connectToSalesforce(SalesforceConnectDto params, HttpServl this.isNewUser = true; // setting default value true to this variable, this will // be updated based on conditions in further processing + String testUserCode = "test_12341234"; + Boolean isTestUser = false; + code = params.getCode(); + if (code.equals(testUserCode)) { + isTestUser = true; + } + redirectUri = params.getRedirect_uri(); - fetchOauthTokensFromSalesforce(); + fetchOauthTokensFromSalesforce(isTestUser); validateAndSaveSalesforceOrganization(); @@ -116,10 +124,11 @@ public AuthServiceDto connectToSalesforce(SalesforceConnectDto params, HttpServl * Call Salesforce oauth token endpoint and fetch tokens. * @return void */ - private void fetchOauthTokensFromSalesforce() { + private void fetchOauthTokensFromSalesforce(Boolean isTestUser) { logger.info("Fetching OAuth Tokens from Salesforce"); - HttpResponse response = salesforceTokens.getTokens(this.code, this.redirectUri); + // TODO - Raj (Call below with diff params as per screenshot shared) + HttpResponse response = salesforceTokens.getTokens(this.code, this.redirectUri, isTestUser); JsonNode jsonNode = util.getJsonNode(response.getResponseBody()); diff --git a/src/test/java/com/salessparrow/api/functional/controllers/authController/PostDisconnectTest.java b/src/test/java/com/salessparrow/api/functional/controllers/authController/PostDisconnectTest.java index 6f0499ee..b0b01d2f 100644 --- a/src/test/java/com/salessparrow/api/functional/controllers/authController/PostDisconnectTest.java +++ b/src/test/java/com/salessparrow/api/functional/controllers/authController/PostDisconnectTest.java @@ -1,6 +1,7 @@ package com.salessparrow.api.functional.controllers.authController; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.ArgumentMatchers.anyBoolean; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.when; @@ -92,7 +93,7 @@ public void testPostDisconnectSuccess() throws Exception { getTokensMockRes .setResponseBody(objectMapper.writeValueAsString(testDataItem.getMocks().get("revokeTokens"))); - when(mockGetTokens.getTokens(anyString(), anyString())).thenReturn(getTokensMockRes); + when(mockGetTokens.getTokens(anyString(), anyString(), anyBoolean())).thenReturn(getTokensMockRes); ResultActions resultActions = mockMvc.perform(MockMvcRequestBuilders.post("/api/v1/auth/disconnect") .cookie(new Cookie(CookieConstants.USER_LOGIN_COOKIE_NAME, cookieValue)) @@ -129,7 +130,7 @@ public void testPostDisconnectNoTokens() throws Exception { getTokensMockRes .setResponseBody(objectMapper.writeValueAsString(testDataItem.getMocks().get("revokeTokens"))); - when(mockGetTokens.getTokens(anyString(), anyString())).thenReturn(getTokensMockRes); + when(mockGetTokens.getTokens(anyString(), anyString(), anyBoolean())).thenReturn(getTokensMockRes); ResultActions resultActions = mockMvc.perform(MockMvcRequestBuilders.post("/api/v1/auth/disconnect") .cookie(new Cookie(CookieConstants.USER_LOGIN_COOKIE_NAME, cookieValue)) diff --git a/src/test/java/com/salessparrow/api/functional/controllers/authController/PostSalesforceConnectTest.java b/src/test/java/com/salessparrow/api/functional/controllers/authController/PostSalesforceConnectTest.java index df83fe06..b6ef528d 100644 --- a/src/test/java/com/salessparrow/api/functional/controllers/authController/PostSalesforceConnectTest.java +++ b/src/test/java/com/salessparrow/api/functional/controllers/authController/PostSalesforceConnectTest.java @@ -8,6 +8,7 @@ import org.slf4j.LoggerFactory; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.ArgumentMatchers.anyBoolean; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; @@ -108,7 +109,7 @@ public void testPostSalesforceConnectSignup() throws Exception { getIdentityMockRes .setResponseBody(objectMapper.writeValueAsString(testDataItem.getMocks().get("getIdentity"))); - when(mockGetTokens.getTokens(anyString(), anyString())).thenReturn(getTokensMockRes); + when(mockGetTokens.getTokens(anyString(), anyString(), anyBoolean())).thenReturn(getTokensMockRes); when(mockGetIdentity.getUserIdentity(anyString(), anyString())).thenReturn(getIdentityMockRes); ResultActions resultActions = mockMvc.perform(MockMvcRequestBuilders.post("/api/v1/auth/salesforce/connect") @@ -153,7 +154,7 @@ public void testPostSalesforceConnectLogin() throws Exception { HttpResponse getTokensMockRes = new HttpResponse(); getTokensMockRes.setResponseBody(objectMapper.writeValueAsString(testDataItem.getMocks().get("getTokens"))); - when(mockGetTokens.getTokens(anyString(), anyString())).thenReturn(getTokensMockRes); + when(mockGetTokens.getTokens(anyString(), anyString(), anyBoolean())).thenReturn(getTokensMockRes); ResultActions resultActions = mockMvc.perform(MockMvcRequestBuilders.post("/api/v1/auth/salesforce/connect") .content(objectMapper.writeValueAsString(testDataItem.getInput().get("body"))) @@ -165,7 +166,7 @@ public void testPostSalesforceConnectLogin() throws Exception { if (resultActions.andReturn().getResponse().getStatus() == 200) { assertEquals(objectMapper.writeValueAsString(testDataItem.getOutput()), actualOutput); - verify(mockGetTokens, times(1)).getTokens(anyString(), anyString()); + verify(mockGetTokens, times(1)).getTokens(anyString(), anyString(), anyBoolean()); verify(mockGetIdentity, times(0)).getUserIdentity(anyString(), anyString()); } else if (resultActions.andReturn().getResponse().getStatus() == 400) { @@ -202,7 +203,7 @@ public void testPostSalesforceConnectDisconnectedUserSignup() throws Exception { getIdentityMockRes .setResponseBody(objectMapper.writeValueAsString(testDataItem.getMocks().get("getIdentity"))); - when(mockGetTokens.getTokens(anyString(), anyString())).thenReturn(getTokensMockRes); + when(mockGetTokens.getTokens(anyString(), anyString(), anyBoolean())).thenReturn(getTokensMockRes); when(mockGetIdentity.getUserIdentity(anyString(), anyString())).thenReturn(getIdentityMockRes); ResultActions resultActions = mockMvc.perform(MockMvcRequestBuilders.post("/api/v1/auth/salesforce/connect") diff --git a/src/test/java/com/salessparrow/api/unit/lib/salesforce/wrappers/SalesforceTokensTest.java b/src/test/java/com/salessparrow/api/unit/lib/salesforce/wrappers/SalesforceTokensTest.java index f845ec17..cd6d90a5 100644 --- a/src/test/java/com/salessparrow/api/unit/lib/salesforce/wrappers/SalesforceTokensTest.java +++ b/src/test/java/com/salessparrow/api/unit/lib/salesforce/wrappers/SalesforceTokensTest.java @@ -88,7 +88,7 @@ public void testGetTokensSuccess() throws Exception { httpClientMockedStatic.when(() -> HttpClient.makePostRequest(anyString(), anyMap(), anyString(), anyInt())) .thenReturn(mockResponse); - HttpResponse actualResponse = salesforceTokens.getTokens(code, redirectUri); + HttpResponse actualResponse = salesforceTokens.getTokens(code, redirectUri, false); // Assertions assertEquals(mockResponse.getResponseBody(), actualResponse.getResponseBody()); @@ -116,7 +116,7 @@ public void testGetTokensException() throws Exception { .thenThrow(new RuntimeException("Some error occurred")); CustomException exception = assertThrows(CustomException.class, () -> { - salesforceTokens.getTokens(code, redirectUri); + salesforceTokens.getTokens(code, redirectUri, false); }); // Assertions From 7dbee79a0cd4759f843ef8bf388f84658e1c367f Mon Sep 17 00:00:00 2001 From: Raj Shah Date: Wed, 13 Sep 2023 11:19:46 +0530 Subject: [PATCH 3/4] Updated cache_prefix to cache_suffix --- sample.secrets.json | 4 +++- .../api/lib/globalConstants/CacheConstants.java | 12 ++++++------ .../repositories/SalesforceOauthTokenRepository.java | 8 ++++---- .../api/repositories/SalesforceUserRepository.java | 8 ++++---- 4 files changed, 17 insertions(+), 15 deletions(-) diff --git a/sample.secrets.json b/sample.secrets.json index f3be477d..f804c4e4 100644 --- a/sample.secrets.json +++ b/sample.secrets.json @@ -14,5 +14,7 @@ "ERROR_MAIL_FROM": "", "ERROR_MAIL_TO": "", "COOKIE_DOMAIN": "", - "OPENAI_API_KEY": "" + "OPENAI_API_KEY": "", + "DEFAULT_TEST_USER":"", + "DEFAULT_TEST_USER_PASSWORD":"" } \ No newline at end of file diff --git a/src/main/java/com/salessparrow/api/lib/globalConstants/CacheConstants.java b/src/main/java/com/salessparrow/api/lib/globalConstants/CacheConstants.java index bb4ae3e5..9827578f 100644 --- a/src/main/java/com/salessparrow/api/lib/globalConstants/CacheConstants.java +++ b/src/main/java/com/salessparrow/api/lib/globalConstants/CacheConstants.java @@ -6,23 +6,23 @@ @Component public class CacheConstants { - public static final String CACHE_PREFIX; + public static final String CACHE_SUFFIX; static { if (CoreConstants.isProductionEnvironment()) { - CACHE_PREFIX = "prod_"; + CACHE_SUFFIX = "_prod"; } else if (CoreConstants.isStagingEnvironment()) { - CACHE_PREFIX = "stag_"; + CACHE_SUFFIX = "_stag"; } else if (CoreConstants.isTestEnvironment()) { - CACHE_PREFIX = "test_"; + CACHE_SUFFIX = "_test"; } else if (CoreConstants.isLocalTestEnvironment()) { - CACHE_PREFIX = "ltest_"; + CACHE_SUFFIX = "_ltest"; } else { - CACHE_PREFIX = "dev_"; + CACHE_SUFFIX = "_dev"; } } diff --git a/src/main/java/com/salessparrow/api/repositories/SalesforceOauthTokenRepository.java b/src/main/java/com/salessparrow/api/repositories/SalesforceOauthTokenRepository.java index 6dab2d73..87b7495a 100644 --- a/src/main/java/com/salessparrow/api/repositories/SalesforceOauthTokenRepository.java +++ b/src/main/java/com/salessparrow/api/repositories/SalesforceOauthTokenRepository.java @@ -29,7 +29,7 @@ public SalesforceOauthTokenRepository(DynamoDBMapper dynamoDBMapper) { * @return SalesforceOauthToken */ @CacheEvict(value = CacheConstants.SALESFORCE_OAUTH_TOKEN_CACHE, - key = "T(com.salessparrow.api.lib.globalConstants.CacheConstants).CACHE_PREFIX + #salesforceOauthToken.externalUserId") + key = "#salesforceOauthToken.externalUserId + T(com.salessparrow.api.lib.globalConstants.CacheConstants).CACHE_SUFFIX") public SalesforceOauthToken createSalesforceOauthToken(SalesforceOauthToken salesforceOauthToken) { // Create a row with status active and created at as current time salesforceOauthToken.setStatus(SalesforceOauthToken.Status.ACTIVE); @@ -50,7 +50,7 @@ public SalesforceOauthToken createSalesforceOauthToken(SalesforceOauthToken sale * @return SalesforceOauthToken */ @CacheEvict(value = CacheConstants.SALESFORCE_OAUTH_TOKEN_CACHE, - key = "T(com.salessparrow.api.lib.globalConstants.CacheConstants).CACHE_PREFIX + #salesforceOauthToken.externalUserId") + key = "#salesforceOauthToken.externalUserId + T(com.salessparrow.api.lib.globalConstants.CacheConstants).CACHE_SUFFIX") public SalesforceOauthToken updateSalesforceOauthToken(SalesforceOauthToken salesforceOauthToken) { try { dynamoDBMapper.save(salesforceOauthToken); @@ -68,7 +68,7 @@ public SalesforceOauthToken updateSalesforceOauthToken(SalesforceOauthToken sale * @return SalesforceOauthToken */ @Cacheable(value = CacheConstants.SALESFORCE_OAUTH_TOKEN_CACHE, - key = "T(com.salessparrow.api.lib.globalConstants.CacheConstants).CACHE_PREFIX + #externalUserId") + key = "#externalUserId + T(com.salessparrow.api.lib.globalConstants.CacheConstants).CACHE_SUFFIX") public SalesforceOauthToken getSalesforceOauthTokenByExternalUserId(String externalUserId) { try { return dynamoDBMapper.load(SalesforceOauthToken.class, externalUserId); @@ -85,7 +85,7 @@ public SalesforceOauthToken getSalesforceOauthTokenByExternalUserId(String exter * @return void */ @CacheEvict(value = CacheConstants.SALESFORCE_OAUTH_TOKEN_CACHE, - key = "T(com.salessparrow.api.lib.globalConstants.CacheConstants).CACHE_PREFIX + #salesforceOauthToken.externalUserId") + key = "#salesforceOauthToken.externalUserId + T(com.salessparrow.api.lib.globalConstants.CacheConstants).CACHE_SUFFIX") public void deleteSalesforceOauthTokenBySalesforceOauthToken(SalesforceOauthToken salesforceOauthToken) { try { dynamoDBMapper.delete(salesforceOauthToken); diff --git a/src/main/java/com/salessparrow/api/repositories/SalesforceUserRepository.java b/src/main/java/com/salessparrow/api/repositories/SalesforceUserRepository.java index 7e74d38a..4f3600ef 100644 --- a/src/main/java/com/salessparrow/api/repositories/SalesforceUserRepository.java +++ b/src/main/java/com/salessparrow/api/repositories/SalesforceUserRepository.java @@ -30,7 +30,7 @@ public SalesforceUserRepository(DynamoDBMapper dynamoDBMapper) { * @return SalesforceUser */ @CacheEvict(value = CacheConstants.SALESFORCE_USER_CACHE, - key = "T(com.salessparrow.api.lib.globalConstants.CacheConstants).CACHE_PREFIX + #salesforceUser.externalUserId") + key = "#salesforceUser.externalUserId + T(com.salessparrow.api.lib.globalConstants.CacheConstants).CACHE_SUFFIX") public SalesforceUser createSalesforceUser(SalesforceUser salesforceUser) { // Create a row with status active and created at as current time salesforceUser.setStatus(SalesforceUser.Status.ACTIVE); @@ -51,7 +51,7 @@ public SalesforceUser createSalesforceUser(SalesforceUser salesforceUser) { * @return SalesforceUser */ @CacheEvict(value = CacheConstants.SALESFORCE_USER_CACHE, - key = "T(com.salessparrow.api.lib.globalConstants.CacheConstants).CACHE_PREFIX + #salesforceUser.externalUserId") + key = "#salesforceUser.externalUserId + T(com.salessparrow.api.lib.globalConstants.CacheConstants).CACHE_SUFFIX") public SalesforceUser updateSalesforceUser(SalesforceUser salesforceUser) { try { dynamoDBMapper.save(salesforceUser); @@ -69,7 +69,7 @@ public SalesforceUser updateSalesforceUser(SalesforceUser salesforceUser) { * @return SalesforceUser */ @Cacheable(value = CacheConstants.SALESFORCE_USER_CACHE, - key = "T(com.salessparrow.api.lib.globalConstants.CacheConstants).CACHE_PREFIX + #externalUserId") + key = "#externalUserId + T(com.salessparrow.api.lib.globalConstants.CacheConstants).CACHE_SUFFIX") public SalesforceUser getSalesforceUserByExternalUserId(String externalUserId) { try { return dynamoDBMapper.load(SalesforceUser.class, externalUserId); @@ -80,7 +80,7 @@ public SalesforceUser getSalesforceUserByExternalUserId(String externalUserId) { } @CacheEvict(value = CacheConstants.SALESFORCE_USER_CACHE, - key = "T(com.salessparrow.api.lib.globalConstants.CacheConstants).CACHE_PREFIX + #externalUserId") + key = "#externalUserId + T(com.salessparrow.api.lib.globalConstants.CacheConstants).CACHE_SUFFIX") public void removeSalesforceUserData(String externalUserId) { SalesforceUser salesforceUser = getSalesforceUserByExternalUserId(externalUserId); salesforceUser.setIdentityUrl(null); From fb5d0269f0c95a087174ac3799c8a65a73cb4fb0 Mon Sep 17 00:00:00 2001 From: Raj Shah Date: Wed, 13 Sep 2023 12:14:02 +0530 Subject: [PATCH 4/4] Refactored code --- .../api/config/CoreConstants.java | 4 +++ .../lib/globalConstants/CacheConstants.java | 34 +++++++++---------- .../salesforce/helper/SalesforceRequest.java | 1 - .../salesforce/wrappers/SalesforceTokens.java | 15 ++++---- .../api/services/salesforce/AuthService.java | 3 +- 5 files changed, 30 insertions(+), 27 deletions(-) diff --git a/src/main/java/com/salessparrow/api/config/CoreConstants.java b/src/main/java/com/salessparrow/api/config/CoreConstants.java index 24bebb94..eafb853d 100644 --- a/src/main/java/com/salessparrow/api/config/CoreConstants.java +++ b/src/main/java/com/salessparrow/api/config/CoreConstants.java @@ -79,6 +79,10 @@ public static String defaultTestUserPassword() { return SecretConstants.defaultTestUserPassword(); } + public static String defaultTestUserCode() { + return "test_12341234"; + } + /** * This method returns the memcached address that is going to be used for locals * @return String diff --git a/src/main/java/com/salessparrow/api/lib/globalConstants/CacheConstants.java b/src/main/java/com/salessparrow/api/lib/globalConstants/CacheConstants.java index 9827578f..3ea8be41 100644 --- a/src/main/java/com/salessparrow/api/lib/globalConstants/CacheConstants.java +++ b/src/main/java/com/salessparrow/api/lib/globalConstants/CacheConstants.java @@ -6,34 +6,34 @@ @Component public class CacheConstants { - public static final String CACHE_SUFFIX; + public static final String CACHE_SUFFIX = getCacheSuffix(); - static { + public static final String SALESFORCE_USER_CACHE = "sf_user"; + + public static final Integer SALESFORCE_USER_CACHE_EXP = 30 * 24 * 60 * 60; // 30 + // days + + public static final String SALESFORCE_OAUTH_TOKEN_CACHE = "sf_oauth_token"; + + public static final Integer SALESFORCE_OAUTH_TOKEN_CACHE_EXP = 30 * 24 * 60 * 60; // 30 + // days + + public static String getCacheSuffix() { if (CoreConstants.isProductionEnvironment()) { - CACHE_SUFFIX = "_prod"; + return "_prod"; } else if (CoreConstants.isStagingEnvironment()) { - CACHE_SUFFIX = "_stag"; + return "_stag"; } else if (CoreConstants.isTestEnvironment()) { - CACHE_SUFFIX = "_test"; + return "_test"; } else if (CoreConstants.isLocalTestEnvironment()) { - CACHE_SUFFIX = "_ltest"; + return "_ltest"; } else { - CACHE_SUFFIX = "_dev"; + return "_dev"; } } - public static final String SALESFORCE_USER_CACHE = "sf_user"; - - public static final Integer SALESFORCE_USER_CACHE_EXP = 30 * 24 * 60 * 60; // 30 - // days - - public static final String SALESFORCE_OAUTH_TOKEN_CACHE = "sf_oauth_token"; - - public static final Integer SALESFORCE_OAUTH_TOKEN_CACHE_EXP = 30 * 24 * 60 * 60; // 30 - // days - } diff --git a/src/main/java/com/salessparrow/api/lib/salesforce/helper/SalesforceRequest.java b/src/main/java/com/salessparrow/api/lib/salesforce/helper/SalesforceRequest.java index 3f892dbb..72d5dda2 100644 --- a/src/main/java/com/salessparrow/api/lib/salesforce/helper/SalesforceRequest.java +++ b/src/main/java/com/salessparrow/api/lib/salesforce/helper/SalesforceRequest.java @@ -7,7 +7,6 @@ import com.salessparrow.api.domain.SalesforceOauthToken; import com.salessparrow.api.exception.CustomException; import com.salessparrow.api.lib.errorLib.ErrorObject; -import com.salessparrow.api.lib.httpLib.HttpClient; import com.salessparrow.api.repositories.SalesforceOauthTokenRepository; import org.slf4j.Logger; diff --git a/src/main/java/com/salessparrow/api/lib/salesforce/wrappers/SalesforceTokens.java b/src/main/java/com/salessparrow/api/lib/salesforce/wrappers/SalesforceTokens.java index a152e0f6..a1030a19 100644 --- a/src/main/java/com/salessparrow/api/lib/salesforce/wrappers/SalesforceTokens.java +++ b/src/main/java/com/salessparrow/api/lib/salesforce/wrappers/SalesforceTokens.java @@ -38,15 +38,16 @@ public HttpResponse getTokens(String code, String redirectUri, Boolean isTestUse String requestBody; if (!isTestUser) { - requestBody = "grant_type=" + salesforceConstants.authorizationCodeGrantType() + "&client_id=" - + CoreConstants.salesforceClientId() + "&client_secret=" + CoreConstants.salesforceClientSecret() - + "&code=" + code + "&redirect_uri=" + redirectUri; + requestBody = String.format("grant_type=%s&client_id=%s&client_secret=%s&code=%s&redirect_uri=%s", + salesforceConstants.authorizationCodeGrantType(), CoreConstants.salesforceClientId(), + CoreConstants.salesforceClientSecret(), code, redirectUri); } else { - requestBody = "grant_type=" + salesforceConstants.passwordGrantType() + "&client_id=" - + CoreConstants.salesforceClientId() + "&client_secret=" + CoreConstants.salesforceClientSecret() - + "&username=" + CoreConstants.defaultTestUser() + "&password=" - + CoreConstants.defaultTestUserPassword() + "&redirect_uri=" + redirectUri; + requestBody = String.format( + "grant_type=%s&client_id=%s&client_secret=%s&username=%s&password=%s&redirect_uri=%s", + salesforceConstants.passwordGrantType(), CoreConstants.salesforceClientId(), + CoreConstants.salesforceClientSecret(), CoreConstants.defaultTestUser(), + CoreConstants.defaultTestUserPassword(), redirectUri); } Map headers = new HashMap<>(); diff --git a/src/main/java/com/salessparrow/api/services/salesforce/AuthService.java b/src/main/java/com/salessparrow/api/services/salesforce/AuthService.java index 92b32978..6a7d6a87 100644 --- a/src/main/java/com/salessparrow/api/services/salesforce/AuthService.java +++ b/src/main/java/com/salessparrow/api/services/salesforce/AuthService.java @@ -30,7 +30,6 @@ import com.salessparrow.api.repositories.SalesforceOrganizationRepository; import com.salessparrow.api.repositories.SalesforceUserRepository; import jakarta.servlet.http.HttpServletRequest; -import software.amazon.awssdk.services.secretsmanager.endpoints.internal.Value.Bool; @Service public class AuthService { @@ -94,7 +93,7 @@ public AuthServiceDto connectToSalesforce(SalesforceConnectDto params, HttpServl this.isNewUser = true; // setting default value true to this variable, this will // be updated based on conditions in further processing - String testUserCode = "test_12341234"; + String testUserCode = CoreConstants.defaultTestUserCode(); Boolean isTestUser = false; code = params.getCode();