Skip to content

Commit

Permalink
feat(client-id): rename hardwareId to clientId
Browse files Browse the repository at this point in the history
SDK-89

Co-authored-by: LasOri <[email protected]>
Co-authored-by: LordAndras <[email protected]>
  • Loading branch information
3 people committed Nov 14, 2024
1 parent 33a305b commit 4e8b156
Show file tree
Hide file tree
Showing 66 changed files with 696 additions and 665 deletions.
30 changes: 15 additions & 15 deletions core/src/androidTest/java/com/emarsys/core/DeviceInfoTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import com.emarsys.core.api.notification.ChannelSettings
import com.emarsys.core.api.notification.NotificationSettings
import com.emarsys.core.device.DeviceInfo
import com.emarsys.core.device.LanguageProvider
import com.emarsys.core.provider.hardwareid.HardwareIdProvider
import com.emarsys.core.provider.clientid.ClientIdProvider
import com.emarsys.core.provider.version.VersionProvider
import com.emarsys.core.util.AndroidVersionUtils
import com.emarsys.testUtil.AnnotationSpec
Expand All @@ -32,7 +32,7 @@ import java.util.*
class DeviceInfoTest : AnnotationSpec() {

companion object {
private const val HARDWARE_ID = "hwid"
private const val CLIENT_ID = "hwid"
private const val SDK_VERSION = "sdkVersion"
private const val LANGUAGE = "en-US"
private const val APP_VERSION = "2.0"
Expand All @@ -41,7 +41,7 @@ class DeviceInfoTest : AnnotationSpec() {
private lateinit var deviceInfo: DeviceInfo
private lateinit var tz: TimeZone
private lateinit var context: Context
private lateinit var mockHardwareIdProvider: HardwareIdProvider
private lateinit var mockClientIdProvider: ClientIdProvider
private lateinit var mockLanguageProvider: LanguageProvider
private lateinit var mockVersionProvider: VersionProvider
private lateinit var mockNotificationManagerHelper: NotificationSettings
Expand All @@ -52,8 +52,8 @@ class DeviceInfoTest : AnnotationSpec() {
tz = TimeZone.getTimeZone("Asia/Tokyo")
TimeZone.setDefault(tz)
context = getTargetContext().applicationContext
mockHardwareIdProvider = mock {
on { provideHardwareId() } doReturn HARDWARE_ID
mockClientIdProvider = mock {
on { provideClientId() } doReturn CLIENT_ID
}
mockLanguageProvider = mock {
on { provideLanguage(any()) } doReturn LANGUAGE
Expand All @@ -64,7 +64,7 @@ class DeviceInfoTest : AnnotationSpec() {
mockNotificationManagerHelper = mock()

deviceInfo = DeviceInfo(
context, mockHardwareIdProvider, mockVersionProvider,
context, mockClientIdProvider, mockVersionProvider,
mockLanguageProvider, mockNotificationManagerHelper,
isAutomaticPushSendingEnabled = true, isGooglePlayAvailable = true
)
Expand All @@ -78,7 +78,7 @@ class DeviceInfoTest : AnnotationSpec() {
@Test
fun testConstructor_initializesFields() {
with(deviceInfo) {
hardwareId shouldNotBe null
clientId shouldNotBe null
platform shouldNotBe null
language shouldNotBe null
timezone shouldNotBe null
Expand Down Expand Up @@ -106,7 +106,7 @@ class DeviceInfoTest : AnnotationSpec() {
whenever(packageManager.getPackageInfo(packageName, 0)).thenReturn(packageInfo)
whenever(mockContext.applicationInfo).thenReturn(Mockito.mock(ApplicationInfo::class.java))
val info = DeviceInfo(
mockContext, mockHardwareIdProvider, mockVersionProvider,
mockContext, mockClientIdProvider, mockVersionProvider,
mockLanguageProvider, mockNotificationManagerHelper,
isAutomaticPushSendingEnabled = true, isGooglePlayAvailable = true
)
Expand Down Expand Up @@ -140,7 +140,7 @@ class DeviceInfoTest : AnnotationSpec() {
fun testIsDebugMode_withDebugApplication() {
val mockDebugContext = applicationDebug
val debugDeviceInfo = DeviceInfo(
mockDebugContext, mockHardwareIdProvider,
mockDebugContext, mockClientIdProvider,
mockVersionProvider, mockLanguageProvider, mockNotificationManagerHelper,
isAutomaticPushSendingEnabled = true, isGooglePlayAvailable = true
)
Expand All @@ -152,7 +152,7 @@ class DeviceInfoTest : AnnotationSpec() {
val mockReleaseContext = applicationRelease
val releaseDeviceInfo = DeviceInfo(
mockReleaseContext,
mockHardwareIdProvider,
mockClientIdProvider,
mockVersionProvider,
mockLanguageProvider,
mockNotificationManagerHelper,
Expand All @@ -163,9 +163,9 @@ class DeviceInfoTest : AnnotationSpec() {
}

@Test
fun testHardwareId_isAcquiredFromHardwareIdProvider() {
Mockito.verify(mockHardwareIdProvider).provideHardwareId()
HARDWARE_ID shouldBe deviceInfo.hardwareId
fun testClientId_isAcquiredFromClientIdProvider() {
Mockito.verify(mockClientIdProvider).provideClientId()
CLIENT_ID shouldBe deviceInfo.clientId
}

@Test
Expand All @@ -189,7 +189,7 @@ class DeviceInfoTest : AnnotationSpec() {
whenever(mockContext.applicationInfo).thenReturn(Mockito.mock(ApplicationInfo::class.java))

deviceInfo = DeviceInfo(
mockContext, mockHardwareIdProvider, mockVersionProvider,
mockContext, mockClientIdProvider, mockVersionProvider,
mockLanguageProvider, mockNotificationManagerHelper,
isAutomaticPushSendingEnabled = true, isGooglePlayAvailable = true
)
Expand Down Expand Up @@ -250,7 +250,7 @@ class DeviceInfoTest : AnnotationSpec() {
whenever(mockContext.applicationInfo).thenReturn(Mockito.mock(ApplicationInfo::class.java))

deviceInfo = DeviceInfo(
mockContext, mockHardwareIdProvider, mockVersionProvider,
mockContext, mockClientIdProvider, mockVersionProvider,
mockLanguageProvider, mockNotificationManagerHelper,
isAutomaticPushSendingEnabled = true, isGooglePlayAvailable = false
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
import android.database.Cursor
import android.net.Uri
import com.emarsys.core.contentresolver.EmarsysContentResolver
import com.emarsys.core.contentresolver.clientid.ClientIdContentResolver
import com.emarsys.core.crypto.ClientIdentificationCrypto
import com.emarsys.core.database.DatabaseContract
import com.emarsys.core.device.ClientIdentification
import com.emarsys.testUtil.AnnotationSpec
import io.kotest.matchers.shouldBe
import io.mockk.every
import io.mockk.mockk
import io.mockk.verify

class ClientIdContentResolverTest : AnnotationSpec() {

companion object {
private val SHARED_PACKAGE_NAMES =
listOf("emarsys.test", "com.emarsys.test", "com.android.test")
private const val ENCRYPTED_ClIENT_ID = "encrypted_shared_hardware_id"
private const val SALT = "testSalt"
private const val IV = "testIv"

}

private var mockEmarsysContentResolver: EmarsysContentResolver = mockk()
private lateinit var contentResolver: ClientIdContentResolver
private lateinit var mockClientIdentificationCrypto: ClientIdentificationCrypto
private lateinit var mockCursor: Cursor

@Before
fun setUp() {
mockCursor = mockk(relaxed = true) {
every { moveToFirst() } returns true
every { getColumnIndexOrThrow(DatabaseContract.CLIENT_IDENTIFICATION_COLUMN_NAME_ENCRYPTED_CLIENT_ID) } returns 0
every { getColumnIndexOrThrow(DatabaseContract.CLIENT_IDENTIFICATION_COLUMN_NAME_SALT) } returns 1
every { getColumnIndexOrThrow(DatabaseContract.CLIENT_IDENTIFICATION_COLUMN_NAME_IV) } returns 2
every { getString(0) } returns ENCRYPTED_ClIENT_ID
every { getString(1) } returns SALT
every { getString(2) } returns IV
}
every {
mockEmarsysContentResolver.query(
any<Uri>(),
any(),
any(),
any(),
any()
)
} returns mockCursor

mockClientIdentificationCrypto = mockk()
every {
mockClientIdentificationCrypto.decrypt(
any(),
any(),
any()
)
} returns "CLIENT_ID"
every { mockClientIdentificationCrypto.encrypt(any()) } returns ClientIdentification(
ENCRYPTED_ClIENT_ID,
SALT,
IV
)
contentResolver = ClientIdContentResolver(
mockEmarsysContentResolver,
mockClientIdentificationCrypto,
SHARED_PACKAGE_NAMES
)
}

@Test
fun testProvideClientId_shouldGetClientId_fromContentResolver() {
contentResolver.resolveClientId()

verify { mockClientIdentificationCrypto.decrypt(ENCRYPTED_ClIENT_ID, SALT, IV) }
}

@Test
fun testProvideClientId_shouldNotGetClientId_fromContentResolver() {
val mockCursor: Cursor = mockk {
every { moveToFirst() } returns false
every { getColumnIndexOrThrow(DatabaseContract.CLIENT_IDENTIFICATION_COLUMN_NAME_ENCRYPTED_CLIENT_ID) } returns 0
every { getColumnIndexOrThrow(DatabaseContract.CLIENT_IDENTIFICATION_COLUMN_NAME_SALT) } returns 1
every { getColumnIndexOrThrow(DatabaseContract.CLIENT_IDENTIFICATION_COLUMN_NAME_IV) } returns 2
every { getString(0) } returns ENCRYPTED_ClIENT_ID
every { getString(1) } returns SALT
every { getString(2) } returns IV
}
every {
mockEmarsysContentResolver.query(
any<Uri>(),
any(),
any(),
any(),
any()
)
} returns mockCursor
val contentResolver = ClientIdContentResolver(
mockEmarsysContentResolver,
mockClientIdentificationCrypto,
SHARED_PACKAGE_NAMES
)
contentResolver.resolveClientId()

verify(exactly = 0) { mockClientIdentificationCrypto.decrypt(any(), any(), any()) }
verify(exactly = 0) { mockClientIdentificationCrypto.encrypt(any()) }
}

@Test
fun testProvideClientId_shouldReturnFalse_whenSharedPackageNamesIsMissing() {
val contentResolver =
ClientIdContentResolver(
mockEmarsysContentResolver,
mockClientIdentificationCrypto,
null
)

val result = contentResolver.resolveClientId()

verify(exactly = 0) { mockClientIdentificationCrypto.decrypt(any(), any(), any()) }
verify(exactly = 0) { mockClientIdentificationCrypto.encrypt(any()) }
result shouldBe null
}
}

This file was deleted.

Loading

0 comments on commit 4e8b156

Please sign in to comment.