Skip to content

Commit

Permalink
fix: Added support for identify with JWT
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions committed Jan 22, 2024
1 parent 1399bad commit b44d038
Show file tree
Hide file tree
Showing 6 changed files with 193 additions and 11 deletions.
156 changes: 156 additions & 0 deletions app/src/androidTest/java/com/extole/blackbox/sdk/ExtoleSdkTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import org.apache.commons.lang3.RandomStringUtils
import org.assertj.core.api.Assertions.assertThat
import org.awaitility.Awaitility.await
import org.awaitility.Duration
import org.json.JSONObject
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
Expand Down Expand Up @@ -258,4 +259,159 @@ class ExtoleSdkTests {
assertThat(initialTime).isBefore(timeAfterIdentify)
}

@Test
fun testIdentifyJwtWillFlushCache() {
val jwt = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiIsImtpZCI6IjUzNmQwNWE2LTMzZWUtNDI2NC04ODI2LW" +
"JhZDRjOTAyMWZhZiJ9.eyJpc3MiOiJtb2JpbGUtc2RrLmV4dG9sZS5jb20iLCJhdWQiOlsiZXh0b2xlLmNvbSJ" +
"dLCJlbWFpbCI6InNka3BlcnNvbi1lbWFpbEBtYWlsb3NhdXIuY29tIiwiaWF0IjoxNzA1NTg0Mjg0LCJleHAiO" +
"jI0ODMxODQyODR9.XdB5-j58GcEeKqKkCLd5f_G78CLLJIHCmsfcOpH-n3o"
val extole =
runBlocking {
return@runBlocking Extole.init(
"mobile-monitor.extole.io",
context = context, appName = "mobile-monitor", labels = setOf("business"),
data = mapOf("version" to "1.0"),
)
}


val initialTimeStampValue = runBlocking {
val (ctaZone, _) = extole.fetchZone("mobile_cta_timestamp")
val initialTimestampValue = ctaZone?.get("timestamp")
initialTimestampValue as Long
}

val emailBeforeIdentify = runBlocking {
val (ctaZone, _) = extole.fetchZone("mobile_cta_timestamp")
ctaZone?.get("email").toString()
}

assertThat(emailBeforeIdentify).isEqualTo("null")

runBlocking {
extole.identifyJwt(jwt)
}

val timeStampValueAfterIdentify = runBlocking {
val (ctaZone, _) = extole.fetchZone("mobile_cta_timestamp")
val initialTimestampValue = ctaZone?.get("timestamp")
initialTimestampValue as Long
}

val initialTime = Instant.ofEpochSecond(initialTimeStampValue)
val timeAfterIdentify = Instant.ofEpochSecond(timeStampValueAfterIdentify)
assertThat(initialTime).isBefore(timeAfterIdentify)


val emailAfterIdentify = runBlocking {
val (ctaZone, _) = extole.fetchZone("mobile_cta_timestamp")
ctaZone?.get("email") as String
}

assertThat(emailAfterIdentify).isEqualTo("[email protected]")
}

@Test
fun testIdentifyJwtWithoutEmail() {
val jwt = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiIsImtpZCI6IjUzNmQwNWE2LTMzZWUtNDI2NC04ODI2" +
"LWJhZDRjOTAyMWZhZiJ9.eyJpc3MiOiJtb2JpbGUtc2RrLmV4dG9sZS5jb20iLCJhdWQiOlsiZXh0b2xlLmN" +
"vbSJdLCJpYXQiOjE3MDU5MjQwMDksImV4cCI6MjQ4MzUyNDAwOX0.X2GnR6OV9amojSLSzoXeecoujrnMzyY" +
"As5VWzR86U4M";

val extole =
runBlocking {
return@runBlocking Extole.init(
"mobile-monitor.extole.io",
context = context, appName = "mobile-monitor", labels = setOf("business"),
data = mapOf("version" to "1.0"),
)
}


val initialTimeStampValue = runBlocking {
val (ctaZone, _) = extole.fetchZone("mobile_cta_timestamp")
val initialTimestampValue = ctaZone?.get("timestamp")
initialTimestampValue as Long
}

val emailBeforeIdentify = runBlocking {
val (ctaZone, _) = extole.fetchZone("mobile_cta_timestamp")
ctaZone?.get("email").toString()
}

assertThat(emailBeforeIdentify).isEqualTo("null")

runBlocking {
extole.identifyJwt(jwt)
}

val timeStampValueAfterIdentify = runBlocking {
val (ctaZone, _) = extole.fetchZone("mobile_cta_timestamp")
val initialTimestampValue = ctaZone?.get("timestamp")
initialTimestampValue as Long
}

val initialTime = Instant.ofEpochSecond(initialTimeStampValue)
val timeAfterIdentify = Instant.ofEpochSecond(timeStampValueAfterIdentify)
assertThat(initialTime).isEqualTo(timeAfterIdentify)


val emailAfterIdentify = runBlocking {
val (ctaZone, _) = extole.fetchZone("mobile_cta_timestamp")
ctaZone?.get("email").toString()
}

assertThat(emailAfterIdentify).isEqualTo("null")
}

@Test
fun testIdentifyWithInvalidJwt() {
val jwt = "invalid_jwt";

val extole =
runBlocking {
return@runBlocking Extole.init(
"mobile-monitor.extole.io",
context = context, appName = "mobile-monitor", labels = setOf("business"),
data = mapOf("version" to "1.0"),
)
}


val initialTimeStampValue = runBlocking {
val (ctaZone, _) = extole.fetchZone("mobile_cta_timestamp")
val initialTimestampValue = ctaZone?.get("timestamp")
initialTimestampValue as Long
}

val emailBeforeIdentify = runBlocking {
val (ctaZone, _) = extole.fetchZone("mobile_cta_timestamp")
ctaZone?.get("email").toString()
}

assertThat(emailBeforeIdentify).isEqualTo("null")

runBlocking {
extole.identifyJwt(jwt)
}

val timeStampValueAfterIdentify = runBlocking {
val (ctaZone, _) = extole.fetchZone("mobile_cta_timestamp")
val initialTimestampValue = ctaZone?.get("timestamp")
initialTimestampValue as Long
}

val initialTime = Instant.ofEpochSecond(initialTimeStampValue)
val timeAfterIdentify = Instant.ofEpochSecond(timeStampValueAfterIdentify)
assertThat(initialTime).isEqualTo(timeAfterIdentify)


val emailAfterIdentify = runBlocking {
val (ctaZone, _) = extole.fetchZone("mobile_cta_timestamp")
ctaZone?.get("email").toString()
}

assertThat(emailAfterIdentify).isEqualTo("null")
}

}
4 changes: 2 additions & 2 deletions config/detekt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ complexity:
TooManyFunctions:
active: true
excludes: ['**/test/**', '**/androidTest/**', '**/commonTest/**']
thresholdInFiles: 30
thresholdInClasses: 30
thresholdInFiles: 35
thresholdInClasses: 35
thresholdInInterfaces: 15
thresholdInObjects: 15
thresholdInEnums: 15
Expand Down
8 changes: 4 additions & 4 deletions mobile-sdk/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ detekt {
}

android {
compileSdkVersion 33
compileSdkVersion 34
buildToolsVersion '31.0.0'

defaultConfig {
minSdkVersion 21
targetSdkVersion 33
versionCode 46
versionName "1.0.48"
targetSdkVersion 34
versionCode 47
versionName "1.0.49"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles "consumer-rules.pro"
Expand Down
6 changes: 5 additions & 1 deletion mobile-sdk/src/main/java/com/extole/android/sdk/Extole.kt
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ interface Extole {
@Throws(RestException::class)
suspend fun sendEvent(
eventName: String,
data: Map<String, Any?> = emptyMap()
data: Map<String, Any?> = emptyMap(),
jwt: String? = null
): Id<Event>

/**
Expand All @@ -70,6 +71,9 @@ interface Extole {
@Throws(RestException::class)
suspend fun identify(identifier: String, data: Map<String, String> = mapOf()): Id<Event>

@Throws(RestException::class)
suspend fun identifyJwt(jwt: String, data: Map<String, String> = mapOf()): Id<Event>

/**
* Used to clear cache and remove current access_token
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,11 @@ class CampaignImpl(
return requestData
}

override suspend fun sendEvent(eventName: String, data: Map<String, Any?>): Id<Event> {
override suspend fun sendEvent(
eventName: String,
data: Map<String, Any?>,
jwt: String?
): Id<Event> {
try {
val requestData = mutableMapOf<String, Any?>()
requestData.putAll(data)
Expand All @@ -83,6 +87,9 @@ class CampaignImpl(
val requestBody = mutableMapOf<String, Any?>()
requestBody["event_name"] = eventName
requestBody["data"] = requestData
jwt?.let {
requestBody["jwt"] = it
}
return Id(
extole.getServices().getEventsEndpoints().post(requestBody).entity.getString("id")
)
Expand All @@ -97,8 +104,12 @@ class CampaignImpl(
}
}

override suspend fun identify(email: String, data: Map<String, String>): Id<Event> {
return extole.identify(email, data)
override suspend fun identify(identifier: String, data: Map<String, String>): Id<Event> {
return extole.identify(identifier, data)
}

override suspend fun identifyJwt(jwt: String, data: Map<String, String>): Id<Event> {
return extole.identifyJwt(jwt, data)
}

override fun logout() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,11 @@ class ExtoleImpl(

override fun getContext(): ApplicationContext = context

override suspend fun sendEvent(eventName: String, data: Map<String, Any?>): Id<Event> {
override suspend fun sendEvent(
eventName: String,
data: Map<String, Any?>,
jwt: String?
): Id<Event> {
EventBus.getDefault().post(AppEvent(eventName, data))
try {
val requestData = mutableMapOf<String, Any?>()
Expand All @@ -119,6 +123,9 @@ class ExtoleImpl(
val requestBody = mutableMapOf<String, Any?>()
requestBody["event_name"] = eventName
requestBody["data"] = requestData
jwt?.let {
requestBody["jwt"] = it
}

val httpPostResult = extoleServices.getEventsEndpoints().post(requestBody)
val accessTokenHeader = httpPostResult.headers.entries
Expand Down Expand Up @@ -159,6 +166,10 @@ class ExtoleImpl(
return sendEvent(IDENTIFY_EVENT_NAME, identifyData)
}

override suspend fun identifyJwt(jwt: String, data: Map<String, String>): Id<Event> {
return sendEvent(IDENTIFY_EVENT_NAME, data, jwt)
}

override suspend fun clone(
programDomain: String?,
appName: String?,
Expand Down

0 comments on commit b44d038

Please sign in to comment.