Skip to content

Commit

Permalink
Merge pull request #837 from SourcePointUSA/DIA-4613_replace_consent-…
Browse files Browse the repository at this point in the history
…status

DIA-4613 replace native `/consent-status` with core-module's
  • Loading branch information
andresilveirah authored Oct 20, 2024
2 parents 39a4b97 + 9417422 commit 20a71b4
Show file tree
Hide file tree
Showing 32 changed files with 347 additions and 1,051 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import com.sourcepoint.cmplibrary.core.getOrNull
import com.sourcepoint.cmplibrary.data.local.* //ktlint-disable
import com.sourcepoint.cmplibrary.data.network.converter.JsonConverter
import com.sourcepoint.cmplibrary.data.network.converter.converter
import com.sourcepoint.cmplibrary.data.network.model.optimized.ConsentStatusResp
import com.sourcepoint.cmplibrary.data.network.model.optimized.USNatConsentData
import com.sourcepoint.cmplibrary.data.network.util.CampaignsEnv
import com.sourcepoint.cmplibrary.exception.CampaignType
Expand Down Expand Up @@ -149,33 +148,6 @@ class CampaignManagerImplTest {
.assertEquals("44") // it is 44 because the feature is not yet implemented on ccpa
}

@Test
fun given_a_v7_consent_status_STORE_it_into_the_local_data_storage() {
val json = "v7/consent_status_with_auth_id.json".file2String()
val obj = JsonConverter.converter.decodeFromString<ConsentStatusResp>(json)

campaignManager.gdprConsentStatus = obj.consentStatusData!!.gdpr
campaignManager.ccpaConsentStatus = obj.consentStatusData!!.ccpa
campaignManager.messagesOptimizedLocalState = obj.localState

campaignManager.gdprConsentStatus!!.also {
it.uuid.assertEquals("69b29ebc-c358-4d7f-9220-38ca2f00125b_1_2_3_4_5_6_7_8_9_10")
it.dateCreated.toString().assertEquals("2022-08-25T20:56:38.551Z")
it.TCData!!.size.assertEquals(27)
}
campaignManager.ccpaConsentStatus!!.also {
it.uuid.assertEquals("e47e539d-41dd-442b-bb08-5cf52b1e33d4")
it.dateCreated.toString().assertEquals("2022-08-25T20:56:39.010Z")
}

campaignManager.gdprConsentStatus = null
campaignManager.ccpaConsentStatus = null

campaignManager.gdprConsentStatus.assertNull()
campaignManager.ccpaConsentStatus.assertNull()
campaignManager.messagesOptimizedLocalState.assertNotNull()
}

@Test
fun given_an_expired_GDPR_and_a_valid_CCPA_campaign_DELETE_only_the_GDPR_consent() {
val json = JSONObject("v7/expired_gdpr_valid_ccpa.json".file2String())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package com.sourcepoint.cmplibrary.data.local
import androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner
import androidx.test.platform.app.InstrumentationRegistry
import com.example.uitestutil.assertEquals
import com.example.uitestutil.assertFalse
import com.example.uitestutil.assertNull
import com.example.uitestutil.assertTrue
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
Expand All @@ -14,18 +17,52 @@ class DataStorageImplTest {
private val gdprStorage by lazy { DataStorageGdpr.create(appContext) }
private val ccpaStorage by lazy { DataStorageCcpa.create(appContext) }
private val usNatStorage by lazy { DataStorageUSNat.create(appContext) }
private val sut by lazy { DataStorage.create(appContext, gdprStorage, ccpaStorage, usNatStorage) }
private val dataStorage by lazy { DataStorage.create(appContext, gdprStorage, ccpaStorage, usNatStorage) }

@Before
fun setup() {
sut.clearAll()
dataStorage.clearAll()
}

@Test
fun check_used_key_for_save_get_LocalState() {
sut.run {
dataStorage.run {
saveLocalState("test_ls")
getLocalState().assertEquals("test_ls")
}
}

@Test
fun test_getBoolean() {
dataStorage.preference.getBoolean("foo").assertNull()
dataStorage.preference.edit().putBoolean("foo", true).apply()
dataStorage.preference.getBoolean("foo")!!.assertTrue()
}

@Test
fun test_putBoolean() {
dataStorage.preference.putBoolean("foo", false)
dataStorage.preference.getBoolean("foo", true).assertFalse()

dataStorage.preference.putBoolean("foo", null)
dataStorage.preference.getBoolean("foo").assertNull()
}

@Test
fun test_putFloat() {
dataStorage.preference.putFloat("foo", 1.0f)
dataStorage.preference.getFloat("foo", 0.0f).assertEquals(1.0f)

dataStorage.preference.putFloat("foo", null)
dataStorage.preference.contains("foo").assertFalse()
}

@Test
fun test_putString() {
dataStorage.preference.putString("foo", "bar")
dataStorage.preference.getString("foo", "").assertEquals("bar")

dataStorage.preference.putFloat("foo", null)
dataStorage.preference.contains("foo").assertFalse()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -853,7 +853,7 @@ internal class SpConsentLibImpl(
}
.executeOnLeft { spClient.onError(it) }
}
CampaignType.USNAT -> { /* TODO(Not implemented) */ }
CampaignType.USNAT -> { /* TODO(Not implemented, not in the roadmap) */ }
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ internal interface CampaignManager {
var gdprConsentStatus: GdprCS?
var ccpaConsentStatus: CcpaCS?
var usNatConsentData: USNatConsentData?
var messagesOptimizedLocalState: JsonElement?
var messagesOptimizedLocalState: String?
var nonKeyedLocalState: JsonElement?
var gdprUuid: String?
var ccpaUuid: String?
Expand Down Expand Up @@ -115,7 +115,6 @@ internal interface CampaignManager {
fun reConsentGdpr(additionsChangeDate: String?, legalBasisChangeDate: String?): ConsentStatus?
fun reConsentUsnat(additionsChangeDate: String?): USNatConsentStatus?
fun hasUsnatApplicableSectionsChanged(usnatMetaData: MetaDataResponse.MetaDataResponseUSNat?): Boolean
fun consentStatusLog(authId: String?)

companion object {
const val SIMPLE_DATE_FORMAT_PATTERN = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
Expand Down Expand Up @@ -472,7 +471,7 @@ private class CampaignManagerImpl(

val isNewUser: Boolean
get() {
val localStateSize = messagesOptimizedLocalState?.jsonObject?.size ?: 0
val localStateSize = messagesOptimizedLocalState?.length ?: 0
return messagesOptimizedLocalState == null ||
localStateSize == 0 ||
(gdprUuid == null && usNatConsentData?.uuid == null && (ccpaConsentStatus?.newUser == null || ccpaConsentStatus?.newUser == true))
Expand Down Expand Up @@ -507,7 +506,7 @@ private class CampaignManagerImpl(
val isGdprUuidPresent = dataStorage.gdprConsentUuid != null
val isCcpaUuidPresent = dataStorage.ccpaConsentUuid != null
val isUsNatUuidPresent = usNatConsentData?.uuid != null
val isLocalStateEmpty = messagesOptimizedLocalState?.jsonObject?.isEmpty() == true
val isLocalStateEmpty = messagesOptimizedLocalState == null
val isV630LocalStatePresent = dataStorage.preference.all.containsKey(LOCAL_STATE)
val isV690LocalStatePresent = dataStorage.preference.all.containsKey(LOCAL_STATE_OLD)
val ccpa2usnat = (
Expand All @@ -527,56 +526,6 @@ private class CampaignManagerImpl(
authId != null
}

override fun consentStatusLog(authId: String?) {
if (logger == null) return
val isGdprUuidPresent = dataStorage.gdprConsentUuid != null
val isCcpaUuidPresent = dataStorage.ccpaConsentUuid != null
val isUsNatUuidPresent = usNatConsentData?.uuid != null
val isLocalStateEmpty = messagesOptimizedLocalState?.jsonObject?.isEmpty() == true
val isV630LocalStatePresent = dataStorage.preference.all.containsKey(LOCAL_STATE)
val isV690LocalStatePresent = dataStorage.preference.all.containsKey(LOCAL_STATE_OLD)
val ccpa2usnat = (
ccpaConsentStatus != null &&
usNatConsentData == null &&
spConfig.isIncluded(USNAT)
)
val storedCcpaWithoutGPP = ccpaConsentStatus?.let { it.gppData.isNullOrEmpty() } ?: false

val shouldCallConsentStatus =
((isGdprUuidPresent || isCcpaUuidPresent || isUsNatUuidPresent) && isLocalStateEmpty) ||
isV630LocalStatePresent ||
isV690LocalStatePresent ||
usnatApplicableSectionChanged ||
ccpa2usnat ||
authId != null ||
storedCcpaWithoutGPP

logger.computation(
tag = "shouldCallConsentStatus",
msg = """ shouldCallConsentStatus[$shouldCallConsentStatus]
""".trimIndent(),
json = JSONObject().apply {
put(
"consentsStoredDetails",
JSONObject().also {
it.put("(GdprUuid ", isGdprUuidPresent)
it.put("OR CcpaUuid", isCcpaUuidPresent)
it.put("OR UsnatUuid)", isUsNatUuidPresent)
it.put("AND isLocalStateEmpty", isLocalStateEmpty)
}
)
put(" consentsStored", ((isGdprUuidPresent || isCcpaUuidPresent || isUsNatUuidPresent) && isLocalStateEmpty))
put(" OR isV630LocalStatePresent", isV630LocalStatePresent)
put(" OR isV690LocalStatePresent", isV690LocalStatePresent)
put(" OR usnatApplicableSectionChanged", usnatApplicableSectionChanged)
put(" OR storedCcpaWithoutGPP", storedCcpaWithoutGPP)
put(" OR transitionCcpa2Usnat", ccpa2usnat)
put(" OR authId", authId != null)
put("return shouldCallConsentStatus", shouldCallConsentStatus)
}
)
}

override var gdprMessageMetaData: MessageMetaData?
get() {
return dataStorage.gdprMessageMetaData?.let { JsonConverter.converter.decodeFromString<MessageMetaData>(it) }
Expand Down Expand Up @@ -640,17 +589,10 @@ private class CampaignManagerImpl(
}
}

override var messagesOptimizedLocalState: JsonElement?
get() {
return dataStorage.messagesOptimizedLocalState?.let {
JsonConverter.converter.decodeFromString<JsonElement>(
it
)
}
}
override var messagesOptimizedLocalState: String?
get() = dataStorage.messagesOptimizedLocalState
set(value) {
val serialised = value?.let { JsonConverter.converter.encodeToString(value) }
dataStorage.messagesOptimizedLocalState = serialised
dataStorage.messagesOptimizedLocalState = value
}

override var nonKeyedLocalState: JsonElement?
Expand Down
Loading

0 comments on commit 20a71b4

Please sign in to comment.