Skip to content

Commit

Permalink
[DIA-2886] Fix Gdpr applies (#729)
Browse files Browse the repository at this point in the history
  • Loading branch information
carmelo-iriti authored Oct 26, 2023
1 parent 86ab8c2 commit 8d6d7b2
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ 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.assertTrue
import org.junit.Test
import org.junit.runner.RunWith

Expand All @@ -17,16 +16,13 @@ class DataStorageGdprImplTest {
val storage = DataStorageGdpr.create(appContext).apply { clearAll() }

storage.saveAuthId("auth")
storage.gdprApplies = true
storage.saveGdpr("{\"type\":\"GDPR\"}")

storage.gdprApplies.assertTrue()
storage.getGdpr().assertEquals("{\"type\":\"GDPR\"}")

storage.clearInternalData()

/** clearInternalData DOES NOT delete these prefs */
storage.gdprApplies.assertTrue()
storage.getGdpr().assertEquals("{\"type\":\"GDPR\"}")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,6 @@ private class CampaignManagerImpl(
}
}

applies?.let { i -> dataStorage.gdprApplies = i }
childPmId?.let { i -> dataStorage.gdprChildPmId = i }
sampleRate?.let { i ->
if (i != dataStorage.gdprSamplingValue) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ private class ServiceImpl(
campaignManager.gdprConsentStatus = responseGdpr.copy(uuid = campaignManager.gdprUuid)
}
val consentHandler = ConsentManager.responseConsentHandler(
response.gdpr?.copy(uuid = campaignManager.gdprUuid),
response.gdpr?.copy(uuid = campaignManager.gdprUuid, applies = dataStorage.gdprApplies),
consentManagerUtils
)
sPConsentsSuccess?.invoke(consentHandler)
Expand Down Expand Up @@ -404,7 +404,7 @@ private class ServiceImpl(
// object.
if (actionType != ActionType.ACCEPT_ALL && actionType != ActionType.REJECT_ALL) {
campaignManager.gdprConsentStatus = postConsentResponse
val cr = ConsentManager.responseConsentHandler(postConsentResponse, consentManagerUtils)
val cr = ConsentManager.responseConsentHandler(postConsentResponse.copy(applies = dataStorage.gdprApplies), consentManagerUtils)
sPConsentsSuccess?.invoke(cr)
}
}
Expand Down Expand Up @@ -444,7 +444,7 @@ private class ServiceImpl(
.executeOnRight { ccpaResponse ->
campaignManager.ccpaConsentStatus = ccpaResponse.ccpa?.copy(uuid = campaignManager.ccpaConsentStatus?.uuid)
val consentHandler = ConsentManager.responseConsentHandler(
ccpaResponse.ccpa?.copy(uuid = campaignManager.ccpaConsentStatus?.uuid),
ccpaResponse.ccpa?.copy(uuid = campaignManager.ccpaConsentStatus?.uuid, applies = dataStorage.ccpaApplies),
consentManagerUtils
)
sPConsentsSuccess?.invoke(consentHandler)
Expand Down Expand Up @@ -489,7 +489,7 @@ private class ServiceImpl(
// because the response from those endpoints does not contain a full consent
// object.
if (at != ActionType.ACCEPT_ALL && at != ActionType.REJECT_ALL) {
val consentHandler = ConsentManager.responseConsentHandler(postConsentResponse, consentManagerUtils)
val consentHandler = ConsentManager.responseConsentHandler(postConsentResponse.copy(applies = dataStorage.ccpaApplies), consentManagerUtils)
sPConsentsSuccess?.invoke(consentHandler)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ internal interface DataStorage : DataStorageGdpr, DataStorageCcpa {

var consentStatusResponse: String?
var gdprConsentStatus: String?
val gdprApplies: Boolean
val ccpaApplies: Boolean
var ccpaConsentStatus: String?
var messagesOptimizedLocalState: String?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.sourcepoint.cmplibrary.data.local
import android.content.Context
import android.content.SharedPreferences
import android.preference.PreferenceManager
import com.sourcepoint.cmplibrary.core.Either
import com.sourcepoint.cmplibrary.data.local.DataStorageGdpr.Companion.AUTH_ID_KEY
import com.sourcepoint.cmplibrary.data.local.DataStorageGdpr.Companion.CMP_SDK_ID_KEY
import com.sourcepoint.cmplibrary.data.local.DataStorageGdpr.Companion.CMP_SDK_VERSION_KEY
Expand All @@ -29,9 +28,6 @@ import com.sourcepoint.cmplibrary.data.local.DataStorageGdpr.Companion.KEY_GDPR_
import com.sourcepoint.cmplibrary.data.local.DataStorageGdpr.Companion.KEY_GDPR_MESSAGE_SUBCATEGORY_OLD
import com.sourcepoint.cmplibrary.data.local.DataStorageGdpr.Companion.META_DATA_KEY
import com.sourcepoint.cmplibrary.data.local.DataStorageGdpr.Companion.USER_CONSENT_KEY
import com.sourcepoint.cmplibrary.data.network.converter.fail
import com.sourcepoint.cmplibrary.data.network.model.toGDPRUserConsent
import com.sourcepoint.cmplibrary.model.exposed.GDPRConsentInternal
import com.sourcepoint.cmplibrary.model.getMap
import com.sourcepoint.cmplibrary.model.toTreeMap
import com.sourcepoint.cmplibrary.util.check
Expand All @@ -44,7 +40,6 @@ internal interface DataStorageGdpr {

val preference: SharedPreferences

var gdprApplies: Boolean
var gdprChildPmId: String?
var gdprPostChoiceResp: String?
var gdprConsentUuid: String?
Expand Down Expand Up @@ -120,15 +115,6 @@ private class DataStorageGdprImpl(context: Context) : DataStorageGdpr {
PreferenceManager.getDefaultSharedPreferences(context)
}

override var gdprApplies: Boolean
get() = preference.getBoolean(KEY_GDPR_APPLIES, false)
set(value) {
preference
.edit()
.putBoolean(KEY_GDPR_APPLIES, value)
.apply()
}

override var gdprChildPmId: String?
get() = preference.getString(KEY_GDPR_CHILD_PM_ID, null)
set(value) {
Expand Down Expand Up @@ -335,11 +321,3 @@ private class DataStorageGdprImpl(context: Context) : DataStorageGdpr {

private fun fail(param: String): Nothing = throw RuntimeException("$param not fund in local storage.")
}

internal fun DataStorageGdpr.getGDPRConsent(): Either<GDPRConsentInternal> = check {
getGdprConsentResp()
.also { if (it == null || it.isBlank()) fail("GDPRConsent is not saved in the the storage!!") }
.let { JSONObject(it) }
.toTreeMap()
.toGDPRUserConsent(uuid = this.gdprConsentUuid, applies = this.gdprApplies)
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import com.sourcepoint.cmplibrary.data.local.DataStorage.Companion.PV_DATA_RESP
import com.sourcepoint.cmplibrary.data.local.DataStorage.Companion.SAVED_CONSENT
import com.sourcepoint.cmplibrary.data.network.converter.JsonConverter
import com.sourcepoint.cmplibrary.data.network.converter.converter
import com.sourcepoint.cmplibrary.data.network.model.optimized.CcpaCS
import com.sourcepoint.cmplibrary.data.network.model.optimized.MetaDataResp
import com.sourcepoint.cmplibrary.util.check
import kotlinx.serialization.decodeFromString

Expand Down Expand Up @@ -99,8 +99,15 @@ private class DataStorageImpl(
* By default - false (if there are no ccpaConsentStatus in data storage or applies value is null)
*/
override val ccpaApplies: Boolean
get() = ccpaConsentStatus?.let { verifiedCcpaConsentStatusString ->
check { JsonConverter.converter.decodeFromString<CcpaCS>(verifiedCcpaConsentStatusString) }
get() = metaDataResp?.let { metaData ->
check { JsonConverter.converter.decodeFromString<MetaDataResp>(metaData).ccpa }
.getOrNull()
?.applies ?: false
} ?: false

override val gdprApplies: Boolean
get() = metaDataResp?.let { metaData ->
check { JsonConverter.converter.decodeFromString<MetaDataResp>(metaData).gdpr }
.getOrNull()
?.applies ?: false
} ?: false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ fun campaignApplies(context: Context, campaign: CampaignType): Boolean {
dsCcpa = dataStorageCcpa,
)
return when (campaign) {
CampaignType.GDPR -> DataStorageGdpr.create(context).gdprApplies
CampaignType.GDPR -> dataStorage.gdprApplies
CampaignType.CCPA -> dataStorage.ccpaApplies
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,14 @@ class MainActivityKotlinTest {
wr(backup = { clickOnRefreshBtnActivity() }) { tapOptionWebView() }
wr { tapToEnableSomeOption() }
wr { tapSaveAndExitWebView() }
wr {
verify{
spClient.onConsentReady(withArg {
it.gdpr!!.consent.applies.assertTrue()
it.gdpr!!.consent.consentStatus!!.consentedAll.assertNotNull()
})
}
}
wr {
verify(exactly = 1) {
spClient.onSpFinished(withArg {
Expand Down

0 comments on commit 8d6d7b2

Please sign in to comment.