From 9fa5cc25b5cd7c9c050125fe1ef24f94062e1611 Mon Sep 17 00:00:00 2001 From: VSydor Date: Mon, 24 Jun 2024 23:05:35 +0300 Subject: [PATCH] Using hardcoded/encoded mailchimp key --- .../nucleus/client/MailchimpClient.java | 10 ++----- .../controller/MailchimpController.java | 2 +- .../nucleus/environment/Environment.java | 11 ++------ .../MailchimpCommunicationService.java | 6 ++--- .../impactupgrade/nucleus/it/AbstractIT.java | 1 - .../it/MailchimpCommunicationServiceIT.java | 27 +++++++++++++++---- .../environment-it-sfdc-mailchimp.json | 1 - 7 files changed, 30 insertions(+), 28 deletions(-) diff --git a/src/main/java/com/impactupgrade/nucleus/client/MailchimpClient.java b/src/main/java/com/impactupgrade/nucleus/client/MailchimpClient.java index 9a30a17aa..cfdd2987a 100644 --- a/src/main/java/com/impactupgrade/nucleus/client/MailchimpClient.java +++ b/src/main/java/com/impactupgrade/nucleus/client/MailchimpClient.java @@ -49,7 +49,6 @@ import java.util.Collections; import java.util.Date; import java.util.List; -import java.util.Locale; import java.util.Map; import java.util.Set; import java.util.stream.Collectors; @@ -68,19 +67,14 @@ public class MailchimpClient { public static final String TAG_INACTIVE = "inactive"; // 2 hours - protected static final Integer BATCH_STATUS_RETRY_TIMEOUT_IN_SECONDS = 300; + protected static final Integer BATCH_STATUS_RETRY_TIMEOUT_IN_SECONDS = 20; protected static final Integer BATCH_STATUS_MAX_RETRIES = 24; protected final com.ecwid.maleorang.MailchimpClient client; - protected final String apiKey; protected final Environment env; public MailchimpClient(EnvironmentConfig.CommunicationPlatform mailchimpConfig, Environment env) { - String profile = System.getenv("PROFILE"); - System.out.println("profile: " + profile); - apiKey = !Strings.isNullOrEmpty(profile) && profile.toLowerCase(Locale.ROOT).contains("it") ? - env.decodeBase64(mailchimpConfig.secretKey) : mailchimpConfig.secretKey; - client = new com.ecwid.maleorang.MailchimpClient(apiKey); + client = new com.ecwid.maleorang.MailchimpClient(mailchimpConfig.secretKey); this.env = env; } diff --git a/src/main/java/com/impactupgrade/nucleus/controller/MailchimpController.java b/src/main/java/com/impactupgrade/nucleus/controller/MailchimpController.java index 7a908fdc5..fff71e21f 100644 --- a/src/main/java/com/impactupgrade/nucleus/controller/MailchimpController.java +++ b/src/main/java/com/impactupgrade/nucleus/controller/MailchimpController.java @@ -100,7 +100,7 @@ private void processCampaignEvent(AudienceEvent event, Environment env) throws E env.logJobError("unable to find ListID={}", event.listId); return; } - MailchimpClient mailchimpClient = new MailchimpClient(mailchimpConfig, env); + MailchimpClient mailchimpClient = env.mailchimpClient(mailchimpConfig); List sentTos = mailchimpClient.getCampaignRecipients(event.id); List emails = sentTos.stream() diff --git a/src/main/java/com/impactupgrade/nucleus/environment/Environment.java b/src/main/java/com/impactupgrade/nucleus/environment/Environment.java index 27697eaf4..edc772071 100644 --- a/src/main/java/com/impactupgrade/nucleus/environment/Environment.java +++ b/src/main/java/com/impactupgrade/nucleus/environment/Environment.java @@ -8,6 +8,7 @@ import com.impactupgrade.nucleus.client.DonorWranglerClient; import com.impactupgrade.nucleus.client.EventBriteClient; import com.impactupgrade.nucleus.client.FactsClient; +import com.impactupgrade.nucleus.client.MailchimpClient; import com.impactupgrade.nucleus.client.RaiselyClient; import com.impactupgrade.nucleus.client.PaypalClient; import com.impactupgrade.nucleus.client.SfdcBulkClient; @@ -44,7 +45,6 @@ import javax.ws.rs.core.MultivaluedHashMap; import javax.ws.rs.core.MultivaluedMap; import java.nio.charset.StandardCharsets; -import java.util.Base64; import java.util.Enumeration; import java.util.HashMap; import java.util.List; @@ -275,6 +275,7 @@ private List segmentServices(Class clazz) { public StripeClient stripeClient() { return new StripeClient(this); } public TwilioClient twilioClient() { return new TwilioClient(this); } public VirtuousClient virtuousClient() { return new VirtuousClient(this); } + public MailchimpClient mailchimpClient(EnvironmentConfig.CommunicationPlatform mailchimpConfig) { return new MailchimpClient(mailchimpConfig, this); } // job logging services @@ -312,12 +313,4 @@ public void logJobError(String message, Object... params) { public void endJobLog(JobStatus jobStatus) { jobLoggingServices().forEach(logger -> logger.endLog(jobStatus)); } - - public String decodeBase64(String encoded) { - if (encoded == null) { - return null; - } else { - return new String(Base64.getDecoder().decode(encoded)); - } - } } \ No newline at end of file diff --git a/src/main/java/com/impactupgrade/nucleus/service/segment/MailchimpCommunicationService.java b/src/main/java/com/impactupgrade/nucleus/service/segment/MailchimpCommunicationService.java index c4657e22f..2cc71baa6 100644 --- a/src/main/java/com/impactupgrade/nucleus/service/segment/MailchimpCommunicationService.java +++ b/src/main/java/com/impactupgrade/nucleus/service/segment/MailchimpCommunicationService.java @@ -97,7 +97,7 @@ protected CrmContact asCrmContact(CrmAccount crmAccount) { protected void syncContacts(List crmContacts, EnvironmentConfig.Mailchimp mailchimpConfig, EnvironmentConfig.CommunicationList communicationList) throws Exception { - MailchimpClient mailchimpClient = new MailchimpClient(mailchimpConfig, env); + MailchimpClient mailchimpClient = env.mailchimpClient(mailchimpConfig); List contactsToUpsert = new ArrayList<>(); List contactsToArchive = new ArrayList<>(); @@ -175,7 +175,7 @@ protected Map> getActiveTags(List crmContacts, M @Override public void syncUnsubscribes(Calendar lastSync) throws Exception { for (EnvironmentConfig.CommunicationPlatform mailchimpConfig : env.getConfig().mailchimp) { - MailchimpClient mailchimpClient = new MailchimpClient(mailchimpConfig, env); + MailchimpClient mailchimpClient = env.mailchimpClient(mailchimpConfig); for (EnvironmentConfig.CommunicationList communicationList : mailchimpConfig.lists) { List unsubscribedMembers = mailchimpClient.getListMembers(communicationList.id, "unsubscribed", lastSync); syncUnsubscribed(getEmails(unsubscribedMembers)); @@ -256,7 +256,7 @@ public void upsertContact(String contactId) throws Exception { // needs added, like a campaign tag to kick off a Journey in MC itself. protected void upsertContact(EnvironmentConfig.Mailchimp mailchimpConfig, EnvironmentConfig.CommunicationList communicationList, CrmContact crmContact) throws Exception { - MailchimpClient mailchimpClient = new MailchimpClient(mailchimpConfig, env); + MailchimpClient mailchimpClient = env.mailchimpClient(mailchimpConfig); // transactional is always subscribed if (communicationList.type != EnvironmentConfig.CommunicationListType.TRANSACTIONAL && !crmContact.canReceiveEmail()) { diff --git a/src/test/java/com/impactupgrade/nucleus/it/AbstractIT.java b/src/test/java/com/impactupgrade/nucleus/it/AbstractIT.java index 9c11d52d2..8bea4d326 100644 --- a/src/test/java/com/impactupgrade/nucleus/it/AbstractIT.java +++ b/src/test/java/com/impactupgrade/nucleus/it/AbstractIT.java @@ -65,7 +65,6 @@ public abstract class AbstractIT extends JerseyTest { protected static final EnvironmentFactory envFactoryHubspotStripe = new EnvironmentFactory("environment-it-hubspot-stripe.json"); protected static final EnvironmentFactory envFactorySfdcStripe = new EnvironmentFactory("environment-it-sfdc-stripe.json"); protected static final EnvironmentFactory envFactoryVirtuousStripe = new EnvironmentFactory("environment-it-virtuous-stripe.json"); - protected static final EnvironmentFactory envFactorySfdcMailchimp = new EnvironmentFactory("environment-it-sfdc-mailchimp.json"); protected final App app; protected final Environment env; diff --git a/src/test/java/com/impactupgrade/nucleus/it/MailchimpCommunicationServiceIT.java b/src/test/java/com/impactupgrade/nucleus/it/MailchimpCommunicationServiceIT.java index ed9149b96..0f1b620d5 100644 --- a/src/test/java/com/impactupgrade/nucleus/it/MailchimpCommunicationServiceIT.java +++ b/src/test/java/com/impactupgrade/nucleus/it/MailchimpCommunicationServiceIT.java @@ -4,7 +4,9 @@ import com.ecwid.maleorang.method.v3_0.lists.members.MemberInfo; import com.impactupgrade.nucleus.App; import com.impactupgrade.nucleus.client.MailchimpClient; +import com.impactupgrade.nucleus.environment.Environment; import com.impactupgrade.nucleus.environment.EnvironmentConfig; +import com.impactupgrade.nucleus.environment.EnvironmentFactory; import com.impactupgrade.nucleus.model.CrmContact; import org.apache.commons.lang3.RandomStringUtils; import org.junit.jupiter.api.Test; @@ -13,6 +15,7 @@ import java.time.LocalDate; import java.time.ZoneId; import java.util.ArrayList; +import java.util.Base64; import java.util.Calendar; import java.util.LinkedList; import java.util.List; @@ -21,13 +24,27 @@ import java.util.stream.Collectors; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertTrue; public class MailchimpCommunicationServiceIT extends AbstractIT { protected MailchimpCommunicationServiceIT() { - super(new App(envFactorySfdcMailchimp)); + super(new App(new EnvironmentFactory("environment-it-sfdc-mailchimp.json") { + @Override + public Environment newEnv() { + return new Environment() { + @Override + public MailchimpClient mailchimpClient(EnvironmentConfig.CommunicationPlatform mailchimpConfig) { + //TODO: use team+it@impactupgrade.com account data (key and list id) + String decoded = new String(Base64.getDecoder().decode("NzA5ZDY2NjIyZmUzNDdjYjY5NDczODdmZWRmM2E1MTAtdXMxNA==")); + mailchimpConfig.secretKey = decoded; + return new MailchimpClient(mailchimpConfig,this); + } + }; + } + })); } @Test @@ -64,7 +81,7 @@ public void syncContacts() throws Exception { EnvironmentConfig.Mailchimp mailchimp = env.getConfig().mailchimp.get(0); String listId = mailchimp.lists.get(0).id; - MailchimpClient mailchimpClient = new MailchimpClient(mailchimp, env); + MailchimpClient mailchimpClient = env.mailchimpClient(mailchimp); assertEmailsStatus(emails, "subscribed", listId, mailchimpClient); } @@ -106,7 +123,7 @@ public void syncUnsubscribes() throws Exception { // Subscribe emails to MC list EnvironmentConfig.Mailchimp mailchimp = env.getConfig().mailchimp.get(0); String listId = mailchimp.lists.get(0).id; - MailchimpClient mailchimpClient = new MailchimpClient(mailchimp, env); + MailchimpClient mailchimpClient = env.mailchimpClient(mailchimp); addEmailsToList(emails, "subscribed", listId, mailchimpClient); Thread.sleep(10000); @@ -122,14 +139,14 @@ public void syncUnsubscribes() throws Exception { env.communicationService("mailchimp").syncContacts(beforeBulkImport); crmContacts = env.primaryCrmService().getContactsByEmails(unsubscribeEmails); - assertTrue(!crmContacts.isEmpty()); + assertFalse(crmContacts.isEmpty()); for (CrmContact crmContact : crmContacts) { assertTrue(Boolean.TRUE == crmContact.emailOptOut); } crmContacts = env.primaryCrmService().getContactsByEmails(cleanEmails); - assertTrue(!crmContacts.isEmpty()); + assertFalse(crmContacts.isEmpty()); for (CrmContact crmContact : crmContacts) { assertTrue(Boolean.TRUE == crmContact.emailBounced); diff --git a/src/test/resources/environment-it-sfdc-mailchimp.json b/src/test/resources/environment-it-sfdc-mailchimp.json index 881119415..3531aebc4 100644 --- a/src/test/resources/environment-it-sfdc-mailchimp.json +++ b/src/test/resources/environment-it-sfdc-mailchimp.json @@ -23,7 +23,6 @@ "mailchimp": [ { - "secretKey": "NzA5ZDY2NjIyZmUzNDdjYjY5NDczODdmZWRmM2E1MTAtdXMxNA==", "lists": [ { "id": "3d215dac12",