Skip to content

Commit

Permalink
Merge pull request #26 from SourcePointUSA/refactor_get_all_choices
Browse files Browse the repository at this point in the history
Refactor GET choice all
  • Loading branch information
andresilveirah authored Dec 12, 2024
2 parents 09311bc + 9e72519 commit c2e30e4
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 104 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import com.sourcepoint.mobile_core.models.SPPropertyName
import com.sourcepoint.mobile_core.models.SPUnableToParseBodyError
import com.sourcepoint.mobile_core.models.consents.GDPRConsent
import com.sourcepoint.mobile_core.network.requests.CCPAChoiceRequest
import com.sourcepoint.mobile_core.network.requests.ChoiceAllMetaDataRequest
import com.sourcepoint.mobile_core.network.requests.ChoiceAllRequest
import com.sourcepoint.mobile_core.network.requests.ConsentStatusRequest
import com.sourcepoint.mobile_core.network.requests.CustomConsentRequest
import com.sourcepoint.mobile_core.network.requests.DefaultRequest
Expand Down Expand Up @@ -81,11 +81,7 @@ interface SPClient {

@Throws(Exception::class) suspend fun getChoiceAll(
actionType: SPActionType,
accountId: Int,
propertyId: Int,
idfaStatus: SPIDFAStatus,
metadata: ChoiceAllMetaDataRequest,
includeData: IncludeData
campaigns: ChoiceAllRequest.ChoiceAllCampaigns
): ChoiceAllResponse

@Throws(Exception::class) suspend fun getMessages(request: MessagesRequest): MessagesResponse
Expand Down Expand Up @@ -262,38 +258,16 @@ class SourcepointClient(

override suspend fun getChoiceAll(
actionType: SPActionType,
accountId: Int,
propertyId: Int,
idfaStatus: SPIDFAStatus,
metadata: ChoiceAllMetaDataRequest,
includeData: IncludeData
campaigns: ChoiceAllRequest.ChoiceAllCampaigns
): ChoiceAllResponse {
val choicePath = when (actionType) {
SPActionType.AcceptAll -> {
"consent-all"
}
SPActionType.RejectAll -> {
"reject-all"
}
SPActionType.AcceptAll -> { "consent-all" }
SPActionType.RejectAll -> { "reject-all" }
else -> throw InvalidChoiceAllParamsError()
}
return http.get(URLBuilder(baseWrapperUrl).apply {
path("wrapper", "v2", "choice", choicePath)
withParams(DefaultRequest())
withParams(mapOf(
"accountId" to accountId,
"propertyId" to propertyId
)
)
withParams(mapOf(
"hasCsp" to true,
"withSiteActions" to false,
"includeCustomVendorsRes" to false
)
)
withParams(mapOf("idfaStatus" to idfaStatus.name))
withParams(mapOf("metadata" to metadata))
withParams(mapOf("includeData" to includeData))
withParams(ChoiceAllRequest(accountId, propertyId, campaigns))
}.build()).bodyOr(::reportErrorAndThrow)
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.sourcepoint.mobile_core.network.requests

import com.sourcepoint.mobile_core.models.SPIDFAStatus
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
data class ChoiceAllRequest (
val accountId: Int,
val propertyId: Int,
@SerialName("metadata") val campaigns: ChoiceAllCampaigns,
val idfaStatus: SPIDFAStatus? = SPIDFAStatus.current(),
val includeData: IncludeData? = IncludeData(),
private val hasCsp: Boolean = true,
private val withSiteActions: Boolean = false,
private val includeCustomVendorsRes: Boolean = false
): DefaultRequest() {
@Serializable
data class ChoiceAllCampaigns (
val gdpr: Campaign?,
val ccpa: Campaign?,
val usnat: Campaign?
) {
@Serializable
data class Campaign (val applies: Boolean)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import com.sourcepoint.mobile_core.models.consents.ConsentStatus
import com.sourcepoint.mobile_core.models.consents.GDPRConsent
import com.sourcepoint.mobile_core.models.consents.USNatConsent
import com.sourcepoint.mobile_core.network.requests.CCPAChoiceRequest
import com.sourcepoint.mobile_core.network.requests.ChoiceAllMetaDataRequest
import com.sourcepoint.mobile_core.network.requests.ChoiceAllRequest
import com.sourcepoint.mobile_core.network.requests.DefaultRequest
import com.sourcepoint.mobile_core.network.requests.GDPRChoiceRequest
import com.sourcepoint.mobile_core.network.requests.IncludeData
Expand Down Expand Up @@ -287,59 +287,15 @@ class SourcepointClientTest {
assertFalse(responseDeleteCustomConsent.categories.contains(categoryId2))
}

@Test
fun choiceAllContainCorrectParams() = runTest {
val mockEngine = mock()
SourcepointClient(123, 321, "test", httpEngine = mockEngine).getChoiceAll(
actionType = SPActionType.AcceptAll,
accountId = 123,
propertyId = 321,
idfaStatus = SPIDFAStatus.Accepted,
metadata = ChoiceAllMetaDataRequest(ChoiceAllMetaDataRequest.Campaign(true),ChoiceAllMetaDataRequest.Campaign(false),ChoiceAllMetaDataRequest.Campaign(false)),
includeData = IncludeData()
)
val defaultRequest = DefaultRequest()
assertEquals(mockEngine.requestHistory.last().url, Url(
" https://cdn.privacy-mgmt.com/wrapper/v2/choice/consent-all?env="+defaultRequest.env+"&scriptType="+defaultRequest.scriptType+
"&scriptVersion="+defaultRequest.scriptVersion+"&accountId=123&propertyId=321&hasCsp=true&withSiteActions=false&" +
"includeCustomVendorsRes=false&idfaStatus=Accepted&metadata=%7B%22gdpr%22%3A%7B%22applies%22%3Atrue%7D%2C%22" +
"ccpa%22%3A%7B%22applies%22%3Afalse%7D%2C%22usnat%22%3A%7B%22applies%22%3Afalse%7D%7D&includeData=%7B%22" +
"TCData%22%3A%7B%22type%22%3A%22string%22%7D%2C%22webConsentPayload%22%3A%7B%22type%22%3A%22string%22%7D%2C%22" +
"localState%22%3A%7B%22type%22%3A%22string%22%7D%2C%22categories%22%3Atrue%2C%22GPPData%22%3A%7B%22uspString%22%3Atrue%7D%7D"
))
}

@Test
fun choiceAllReturnInvalidChoiceAllParamsError() = runTest {
assertFailsWith<InvalidChoiceAllParamsError> {
api.getChoiceAll(
actionType = SPActionType.Custom,
accountId = 123,
propertyId = 321,
idfaStatus = SPIDFAStatus.Accepted,
metadata = ChoiceAllMetaDataRequest(
ChoiceAllMetaDataRequest.Campaign(true),
ChoiceAllMetaDataRequest.Campaign(false),
ChoiceAllMetaDataRequest.Campaign(false)
),
includeData = IncludeData()
)
}
}

@Test
fun getGDPRChoiceAcceptAllContainCorrectResponse() = runTest {
val response = api.getChoiceAll(
actionType = SPActionType.AcceptAll,
accountId = accountId,
propertyId = propertyId,
idfaStatus = SPIDFAStatus.Accepted,
metadata = ChoiceAllMetaDataRequest(
ChoiceAllMetaDataRequest.Campaign(true),
ChoiceAllMetaDataRequest.Campaign(false),
ChoiceAllMetaDataRequest.Campaign(false)
campaigns = ChoiceAllRequest.ChoiceAllCampaigns(
ChoiceAllRequest.ChoiceAllCampaigns.Campaign(true),
ChoiceAllRequest.ChoiceAllCampaigns.Campaign(false),
ChoiceAllRequest.ChoiceAllCampaigns.Campaign(false)
),
includeData = IncludeData()
)
assertTrue(response.gdpr?.consentStatus?.consentedAll == true)
assertTrue(response.gdpr?.acceptedVendors?.isNotEmpty() == true)
Expand All @@ -350,15 +306,11 @@ class SourcepointClientTest {
fun getGDPRChoiceRejectAllContainCorrectResponse() = runTest {
val response = api.getChoiceAll(
actionType = SPActionType.RejectAll,
accountId = accountId,
propertyId = propertyId,
idfaStatus = SPIDFAStatus.Accepted,
metadata = ChoiceAllMetaDataRequest(
ChoiceAllMetaDataRequest.Campaign(true),
ChoiceAllMetaDataRequest.Campaign(false),
ChoiceAllMetaDataRequest.Campaign(false)
campaigns = ChoiceAllRequest.ChoiceAllCampaigns(
ChoiceAllRequest.ChoiceAllCampaigns.Campaign(true),
ChoiceAllRequest.ChoiceAllCampaigns.Campaign(false),
ChoiceAllRequest.ChoiceAllCampaigns.Campaign(false)
),
includeData = IncludeData()
)
assertTrue(response.gdpr?.consentStatus?.rejectedAny == true)
assertTrue(response.gdpr?.acceptedVendors?.isEmpty() == true)
Expand Down

0 comments on commit c2e30e4

Please sign in to comment.