Skip to content
28 changes: 26 additions & 2 deletions src/main/kotlin/com/mparticle/kits/RoktKit.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.content.pm.PackageManager
import android.graphics.Typeface
import android.os.Build
import com.mparticle.BuildConfig
import com.mparticle.MParticle
import com.mparticle.MParticle.IdentityType
import com.mparticle.MpRoktEventCallback
import com.mparticle.UnloadReasons
Expand Down Expand Up @@ -188,7 +189,7 @@ class RoktKit : KitIntegration(), CommerceListener, IdentityListener, RoktListen
}?.toMap()

this.mpRoktEventCallback = mpRoktEventCallback
val finalAttributes: HashMap<String, String> = HashMap<String, String>()
val finalAttributes = mutableMapOf<String, String>()
filterUser?.userAttributes?.let { userAttrs ->
for ((key, value) in userAttrs) {
finalAttributes[key] = value.toString()
Expand All @@ -206,6 +207,7 @@ class RoktKit : KitIntegration(), CommerceListener, IdentityListener, RoktListen
attributes?.get(SANDBOX_MODE_ROKT)?.let { value ->
finalAttributes.put(SANDBOX_MODE_ROKT, value)
}
verifyHashedEmail(finalAttributes)
val roktConfig = mpRoktConfig?.let { mapToRoktConfig(it) }
Rokt.execute(
viewName,
Expand Down Expand Up @@ -318,9 +320,31 @@ class RoktKit : KitIntegration(), CommerceListener, IdentityListener, RoktListen
}
}

private fun verifyHashedEmail(attributes: MutableMap<String, String>?) {
if (attributes == null) return

val emailKey = MParticle.IdentityType.Email.name.lowercase()
val emailShaKey = "emailsha256"

val emailShaValue = attributes.entries.find { it.key.equals(emailShaKey, ignoreCase = true) }?.value

when {
!emailShaValue.isNullOrEmpty() -> {
// If emailsha256 is already present, remove entries with email
val iterator = attributes.entries.iterator()
while (iterator.hasNext()) {
val entry = iterator.next()
if (entry.key.equals(emailKey, ignoreCase = true)) {
iterator.remove()
}
}
}
}
}

private fun getStringForIdentity(identityType: IdentityType): String {
return when (identityType) {
IdentityType.Other -> "other"
IdentityType.Other -> "emailsha256"
IdentityType.CustomerId -> "customerid"
IdentityType.Facebook -> "facebook"
IdentityType.Twitter -> "twitter"
Expand Down
67 changes: 67 additions & 0 deletions src/test/kotlin/com/mparticle/kits/RoktKitTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import kotlinx.coroutines.flow.toList
import kotlinx.coroutines.test.runTest
import org.json.JSONArray
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Test
Expand Down Expand Up @@ -238,6 +239,34 @@ class RoktKitTests {
assertTrue(result.containsKey("key3"))
}

@Test
fun test_addIdentityAttributes_When_userIdentities_Contain_other() {
val mockFilterUser = mock(FilteredMParticleUser::class.java)
val userIdentities = HashMap<IdentityType, String>()
userIdentities.put(IdentityType.Email, "TestEmail@gamil.com")
userIdentities.put(IdentityType.Other, "hashedEmail@123.com")
Mockito.`when`(mockFilterUser.userIdentities).thenReturn(userIdentities)
val attributes: Map<String, String> = mapOf(
"key1" to "value1",
"key2" to "value2",
"key3" to "value3"
)
val method: Method = RoktKit::class.java.getDeclaredMethod(
"addIdentityAttributes",
Map::class.java,
FilteredMParticleUser::class.java
)
method.isAccessible = true
val result = method.invoke(roktKit, attributes, mockFilterUser) as Map<String, String>
assertEquals(5, result.size)

assertTrue(result.containsKey("key1"))
assertTrue(result.containsKey("key2"))
assertTrue(result.containsKey("key3"))
assertTrue(result.containsKey("email"))
assertTrue(result.containsKey("emailsha256"))
}

@Test
fun testSetSdkWrapper_correctlySetsRoktFramework() {
mockkObject(Rokt)
Expand Down Expand Up @@ -530,6 +559,44 @@ class RoktKitTests {
unmockkObject(Rokt)
}

@Test
fun TestverifyHashedEmail_removes_when_emailsha256_is_present() {
val attributes = mutableMapOf(
Comment thread
Mansi-mParticle marked this conversation as resolved.
"email" to "user@example.com",
"emailsha256" to "hashed_email_value",
"other" to "Test"
)
val method: Method = RoktKit::class.java.getDeclaredMethod(
"verifyHashedEmail",
MutableMap::class.java
)
method.isAccessible = true
method.invoke(roktKit, attributes)


assertFalse(attributes.containsKey("email"))
assertEquals("hashed_email_value", attributes["emailsha256"])
assertEquals("Test", attributes["other"])
}


@Test
fun TestverifyHashedEmail_removes_when_neither_emailsha256_nor_other_is_present() {
val attributes = mutableMapOf(
"email" to "user@example.com"
)

val method: Method = RoktKit::class.java.getDeclaredMethod(
"verifyHashedEmail",
MutableMap::class.java
)
method.isAccessible = true
method.invoke(roktKit, attributes)

assertEquals("user@example.com", attributes["email"])
assertFalse(attributes.containsKey("emailsha256"))
}

internal inner class TestCoreCallbacks : CoreCallbacks {
override fun isBackgrounded(): Boolean = false
override fun getUserBucket(): Int = 0
Expand Down
Loading