Skip to content

Commit

Permalink
fix: prevent empty identifier and device token (#240)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shahroz16 committed Jul 14, 2023
1 parent 10fb5d4 commit e9b5d0c
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ internal class DeviceRepositoryImpl(
) : DeviceRepository {

override fun registerDeviceToken(deviceToken: String, attributes: CustomAttributes) {
if (deviceToken.isBlank()) {
logger.debug("device token cannot be blank. ignoring request to register device token")
return
}

val attributes = createDeviceAttributes(attributes)

Check warning on line 34 in sdk/src/main/java/io/customer/sdk/repository/DeviceRepository.kt

View workflow job for this annotation

GitHub Actions / Android Lint (messagingpush)

Name shadowed: attributes

Check warning on line 34 in sdk/src/main/java/io/customer/sdk/repository/DeviceRepository.kt

View workflow job for this annotation

GitHub Actions / Unit tests (messagingpush)

Name shadowed: attributes

Check warning on line 34 in sdk/src/main/java/io/customer/sdk/repository/DeviceRepository.kt

View workflow job for this annotation

GitHub Actions / Android Lint (sdk)

Name shadowed: attributes

Check warning on line 34 in sdk/src/main/java/io/customer/sdk/repository/DeviceRepository.kt

View workflow job for this annotation

GitHub Actions / Android Lint (messaginginapp)

Name shadowed: attributes

Check warning on line 34 in sdk/src/main/java/io/customer/sdk/repository/DeviceRepository.kt

View workflow job for this annotation

GitHub Actions / Unit tests (messaginginapp)

Name shadowed: attributes

Check warning on line 34 in sdk/src/main/java/io/customer/sdk/repository/DeviceRepository.kt

View workflow job for this annotation

GitHub Actions / instrumentation-test (java_layout)

Name shadowed: attributes

Check warning on line 34 in sdk/src/main/java/io/customer/sdk/repository/DeviceRepository.kt

View workflow job for this annotation

GitHub Actions / Unit tests (sdk)

Name shadowed: attributes

logger.info("registering device token $deviceToken, attributes: $attributes")
Expand All @@ -36,7 +41,7 @@ internal class DeviceRepositoryImpl(
sitePreferenceRepository.saveDeviceToken(deviceToken)

val identifiedProfileId = sitePreferenceRepository.getIdentifier()
if (identifiedProfileId == null) {
if (identifiedProfileId.isNullOrBlank()) {
logger.info("no profile identified, so not registering device token to a profile")
return
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ internal class ProfileRepositoryImpl(
logger.info("identify profile $identifier")
logger.debug("identify profile $identifier, $attributes")

if (identifier.isBlank()) {
logger.debug("Profile cannot be identified: Identifier is blank. Please retry with a valid, non-empty identifier.")
return
}

val currentlyIdentifiedProfileIdentifier = sitePreferenceRepository.getIdentifier()
// The SDK calls identify() with the already identified profile for changing profile attributes.
val isChangingIdentifiedProfile =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,25 @@ class DeviceRepositoryTest : BaseTest() {
prefRepository.getDeviceToken() shouldBeEqualTo givenDeviceToken
}

@Test
fun registerDeviceToken_givenEmptyDeviceToken_expectDoNotAddTaskToBackgroundQueue() {
val givenDeviceToken = ""

repository.registerDeviceToken(givenDeviceToken, emptyMap())

verifyNoInteractions(backgroundQueueMock)
}

@Test
fun registerDeviceToken_givenEmptyIdentifier_expectDoNotAddTaskToBackgroundQueue() {
val givenDeviceToken = String.random
prefRepository.saveIdentifier("")

repository.registerDeviceToken(givenDeviceToken, emptyMap())

verifyNoInteractions(backgroundQueueMock)
}

@Test
fun registerDeviceToken_givenIdentifiedProfile_expectAddTaskToQueue_expectSaveToken() {
val givenIdentifier = String.random
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import io.customer.sdk.queue.type.QueueStatus
import io.customer.sdk.repository.preference.SitePreferenceRepository
import io.customer.sdk.util.Logger
import org.amshove.kluent.internal.assertEquals
import org.amshove.kluent.shouldBe
import org.amshove.kluent.shouldBeNull
import org.junit.Before
import org.junit.Test
Expand Down Expand Up @@ -67,6 +68,55 @@ class ProfileRepositoryTest : BaseTest() {
verifyNoMoreInteractions(backgroundQueueMock)
}

@Test
fun identify_givenEmptyIdentifier_expectNoIdentifyBackgroundQueue_expectIdentifierNotSaved() {
val newIdentifier = ""
val givenAttributes = mapOf("name" to String.random)
whenever(
backgroundQueueMock.queueIdentifyProfile(
anyOrNull(),
anyOrNull(),
anyOrNull()
)
).thenReturn(QueueModifyResult(true, QueueStatus(siteId, 1)))

repository.identify(newIdentifier, givenAttributes)

verifyNoInteractions(backgroundQueueMock)

verify(backgroundQueueMock, times(0)).queueIdentifyProfile(
newIdentifier = newIdentifier,
oldIdentifier = null,
attributes = givenAttributes
)

prefRepository.getIdentifier().shouldBeNull()
}

@Test
fun identify_givenAlreadyIdentifiedProfile_givenNewEmptyIdentifier_expectIdentifierNotSaved() {
val givenIdentifier = ""
val givenAttributes = mapOf("name" to String.random)

val existingIdentifier = String.random

prefRepository.saveIdentifier(existingIdentifier)

whenever(
backgroundQueueMock.queueIdentifyProfile(
anyOrNull(),
anyOrNull(),
anyOrNull()
)
).thenReturn(QueueModifyResult(true, QueueStatus(siteId, 1)))

repository.identify(givenIdentifier, givenAttributes)

verifyNoInteractions(backgroundQueueMock)

prefRepository.getIdentifier() shouldBe existingIdentifier
}

@Test
fun identify_givenFirstTimeIdentify_givenDeviceTokenExists_expectIdentifyBackgroundQueue_expectDoNotDeleteToken_expectProfileIdentifiedHookUpdateWithCorrectIdentifier_expectRegisterDeviceToken() {
val newIdentifier = String.random
Expand Down

0 comments on commit e9b5d0c

Please sign in to comment.