Skip to content
This repository was archived by the owner on Apr 22, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public class IterableExtension extends MessageProcessor {
public static final String MPARTICLE_RESERVED_PHONE_ATTR = "$Mobile";
public static final String ITERABLE_RESERVED_PHONE_ATTR = "phoneNumber";
public static final Set<Integer> RETRIABLE_HTTP_STATUS_SET = new HashSet<>(Arrays.asList(429, 502, 504));
public static final long API_KEY_LENGTH = 32;
static final ObjectMapper mapper = new ObjectMapper();
IterableService iterableService = IterableService.newInstance();
IterableExtensionLogger logger;
Expand Down Expand Up @@ -107,19 +108,24 @@ private void processPushOpens(EventProcessingRequest processingRequest) throws I
}
}

private static String getApiKey(Event event) {
Account account = event.getRequest().getAccount();
return account.getStringSetting(SETTING_API_KEY, true, null);
private static String getApiKey(Event event) throws IOException {
return getApiKeyOrThrow(event.getRequest().getAccount());
}

private static String getApiKey(EventProcessingRequest event) {
Account account = event.getAccount();
return account.getStringSetting(SETTING_API_KEY, true, null);
private static String getApiKey(EventProcessingRequest event) throws IOException {
return getApiKeyOrThrow(event.getAccount());
}

private static String getApiKey(AudienceMembershipChangeRequest event) {
Account account = event.getAccount();
return account.getStringSetting(SETTING_API_KEY, true, null);
private static String getApiKey(AudienceMembershipChangeRequest event) throws IOException {
return getApiKeyOrThrow(event.getAccount());
}

private static String getApiKeyOrThrow(Account account) throws IOException {
String apiKey = account.getStringSetting(SETTING_API_KEY, true, null);
if (apiKey.length() != API_KEY_LENGTH) {
throw new ProcessingError("Invalid API key, length is " + apiKey.length());
}
return apiKey;
}

/**
Expand Down Expand Up @@ -157,6 +163,9 @@ public void processPushSubscriptionEvent(PushSubscriptionEvent event) throws IOE
if (event.getRequest().getRuntimeEnvironment().getType().equals(RuntimeEnvironment.Type.IOS)) {
Boolean sandboxed = ((IosRuntimeEnvironment) event.getRequest().getRuntimeEnvironment()).getIsSandboxed();
if (sandboxed != null && sandboxed) {
// TODO: Replace isSandboxed with isDebug
String keyIdentifier = getApiKey(event).substring(getApiKey(event).length() - 8);
logger.logMessage("Key identifier for sandbox registrant: " + keyIdentifier);
request.device.platform = Device.PLATFORM_APNS_SANDBOX;
request.device.applicationName = event.getRequest().getAccount().getAccountSettings().get(SETTING_APNS_SANDBOX_KEY);
} else {
Expand Down Expand Up @@ -264,6 +273,9 @@ void updateUser(EventProcessingRequest request) throws IOException {
private Map<String, List<Object>> processUserAttributesList(Map<String, List<String>> userAttributesList, boolean shouldCoerceStrings) {
Map<String, List<Object>> convertedAttributeList = new HashMap<>();
userAttributesList.forEach((key, value)-> {
if (value == null) {
return;
}
List<Object> convertedAttributes = convertedAttributeList.get(key);
if (convertedAttributes == null) {
convertedAttributes = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import static org.mockito.Mockito.times;

public class IterableExtensionTest {
private static final String TEST_API_KEY = "foo api key";
private static final String TEST_API_KEY = "7982f56948624d8dc2f53ff007f51de3";
private IterableExtension testExtension;
private IterableExtensionLogger testLogger;
private IterableService iterableServiceMock;
Expand Down Expand Up @@ -91,7 +91,7 @@ public void setup() {

testAccount = new Account();
Map<String, String> accountSettings = new HashMap<>();
accountSettings.put(SETTING_API_KEY, "some api key");
accountSettings.put(SETTING_API_KEY, "7982f56948624d8dc2f53ff007f51de3");
testAccount.setAccountSettings(accountSettings);

testIterableApiSuccess = new IterableApiResponse();
Expand Down Expand Up @@ -552,7 +552,7 @@ public void testProcessAudienceMembershipChangeRequest() throws Exception {
AudienceMembershipChangeRequest request = new AudienceMembershipChangeRequest();
Account account = new Account();
Map<String, String> settings = new HashMap<>();
settings.put(SETTING_API_KEY, "some api key");
settings.put(SETTING_API_KEY, "7982f56948624d8dc2f53ff007f51de3");
account.setAccountSettings(settings);
request.setAccount(account);
request.setUserProfiles(profiles);
Expand All @@ -563,7 +563,7 @@ public void testProcessAudienceMembershipChangeRequest() throws Exception {
ArgumentCaptor<String> apiArgument = ArgumentCaptor.forClass(String.class);
Mockito.verify(service, Mockito.times(3)).listSubscribe(apiArgument.capture(), argument.capture());
String apiKey = apiArgument.getValue();
assertEquals("some api key", apiKey);
assertEquals("7982f56948624d8dc2f53ff007f51de3", apiKey);
List<SubscribeRequest> subscribeRequests = argument.getAllValues();
int i = 0;
for (SubscribeRequest subscribeRequest : subscribeRequests) {
Expand Down Expand Up @@ -721,6 +721,45 @@ public void testConvertToCommerceItem() throws Exception {

}

@Test
public void testProcessPushSubscriptionEvent() throws Exception {
EventProcessingRequest request = createEventProcessingRequest(true);
String fakeToken = "bdcbbb717ed20a9bad33e63dec3703210abf085dd73362589c92e61697ca1234";
PushSubscriptionEvent event = new PushSubscriptionEvent();
event.setToken(fakeToken);

Account account = new Account();
HashMap<String, String> settings = new HashMap<String, String>();
settings.put(SETTING_APNS_KEY, "iterable.app");
settings.put(SETTING_API_KEY, TEST_API_KEY);
account.setAccountSettings(settings);
request.setAccount(account);

List<UserIdentity> userIdentities = new LinkedList<>();
userIdentities.add(new UserIdentity(UserIdentity.Type.EMAIL, Identity.Encoding.RAW, "mptest@mparticle.com"));
userIdentities.add(new UserIdentity(UserIdentity.Type.CUSTOMER, Identity.Encoding.RAW, "123456"));
request.setUserIdentities(userIdentities);
request.setRuntimeEnvironment(new IosRuntimeEnvironment());
event.setRequest(request);

testExtension.iterableService = Mockito.mock(IterableService.class);
Call callMock = Mockito.mock(Call.class);
Mockito.when(testExtension.iterableService.registerToken(Mockito.any(), Mockito.any())).thenReturn(callMock);

IterableApiResponse apiResponse = new IterableApiResponse();
apiResponse.code = IterableApiResponse.SUCCESS_MESSAGE;
Response<IterableApiResponse> response = Response.success(apiResponse);
Mockito.when(callMock.execute()).thenReturn(response);

testExtension.processPushSubscriptionEvent(event);
ArgumentCaptor<RegisterDeviceTokenRequest> pushRegisterArgs = ArgumentCaptor.forClass(RegisterDeviceTokenRequest.class);
Mockito.verify(testExtension.iterableService, Mockito.times(1)).registerToken(Mockito.any(), pushRegisterArgs.capture());
RegisterDeviceTokenRequest registerTokenRequest = pushRegisterArgs.getValue();
assertEquals("iterable.app", registerTokenRequest.device.applicationName);
assertEquals(fakeToken, registerTokenRequest.device.token);
assertEquals("mptest@mparticle.com", registerTokenRequest.email);
}

@Test
public void testProcessProductActionEvent() throws Exception {
ProductActionEvent event = new ProductActionEvent();
Expand Down Expand Up @@ -945,7 +984,7 @@ public void testUpdateSubscriptionsEvent() throws Exception {
event.setName(IterableExtension.UPDATE_SUBSCRIPTIONS_CUSTOM_EVENT_NAME);
EventProcessingRequest request = createEventProcessingRequest();
Map<String, String> settings = request.getAccount().getAccountSettings();
settings.put(SETTING_API_KEY, "foo api key 2");
settings.put(SETTING_API_KEY, "7982f56948624d8dc2f53ff007f51de3");
List<UserIdentity> userIdentities = new LinkedList<>();
userIdentities.add(new UserIdentity(UserIdentity.Type.EMAIL, Identity.Encoding.RAW, "mptest@mparticle.com"));
request.setUserIdentities(userIdentities);
Expand All @@ -969,7 +1008,7 @@ public void testUpdateSubscriptionsEvent() throws Exception {
ArgumentCaptor<UpdateSubscriptionsRequest> argument = ArgumentCaptor.forClass(UpdateSubscriptionsRequest.class);
ArgumentCaptor<String> stringArgumentCaptor = ArgumentCaptor.forClass(String.class);
Mockito.verify(testExtension.iterableService).updateSubscriptions(stringArgumentCaptor.capture(), argument.capture());
assertEquals("foo api key 2", stringArgumentCaptor.getValue());
assertEquals("7982f56948624d8dc2f53ff007f51de3", stringArgumentCaptor.getValue());
assertEquals("mptest@mparticle.com", argument.getValue().email);
assertEquals(expectedEmailListIdList, argument.getValue().emailListIds);
assertEquals(expectedChannelIdList, argument.getValue().unsubscribedChannelIds);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

public class IngressExtensionTest {

private static final String TEST_API_KEY = "foo api key";
private static final String TEST_API_KEY = "7982f56948624d8dc2f53ff007f51de3";
private static MessageSerializer serializer;
private static IngressExtension ingressExtension;

Expand Down