From eb3d74dde42a9519ea3149fa9f8187c90e742634 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Herculano?= Date: Thu, 5 Dec 2024 14:14:40 -0500 Subject: [PATCH 1/3] move query params to its own data class --- .../mobile_core/network/SourcepointClient.kt | 38 +++---------------- .../requests/ChoiceAllMetaDataRequest.kt | 15 -------- .../network/requests/ChoiceAllRequest.kt | 27 +++++++++++++ 3 files changed, 33 insertions(+), 47 deletions(-) delete mode 100644 core/src/commonMain/kotlin/com/sourcepoint/mobile_core/network/requests/ChoiceAllMetaDataRequest.kt create mode 100644 core/src/commonMain/kotlin/com/sourcepoint/mobile_core/network/requests/ChoiceAllRequest.kt diff --git a/core/src/commonMain/kotlin/com/sourcepoint/mobile_core/network/SourcepointClient.kt b/core/src/commonMain/kotlin/com/sourcepoint/mobile_core/network/SourcepointClient.kt index 34a4eba..33e0e84 100644 --- a/core/src/commonMain/kotlin/com/sourcepoint/mobile_core/network/SourcepointClient.kt +++ b/core/src/commonMain/kotlin/com/sourcepoint/mobile_core/network/SourcepointClient.kt @@ -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 @@ -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 @@ -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) } diff --git a/core/src/commonMain/kotlin/com/sourcepoint/mobile_core/network/requests/ChoiceAllMetaDataRequest.kt b/core/src/commonMain/kotlin/com/sourcepoint/mobile_core/network/requests/ChoiceAllMetaDataRequest.kt deleted file mode 100644 index e8b43ea..0000000 --- a/core/src/commonMain/kotlin/com/sourcepoint/mobile_core/network/requests/ChoiceAllMetaDataRequest.kt +++ /dev/null @@ -1,15 +0,0 @@ -package com.sourcepoint.mobile_core.network.requests - -import kotlinx.serialization.Serializable - -@Serializable -data class ChoiceAllMetaDataRequest ( - val gdpr: Campaign?, - val ccpa: Campaign?, - val usnat: Campaign? -) { - @Serializable - data class Campaign ( - val applies: Boolean - ) -} diff --git a/core/src/commonMain/kotlin/com/sourcepoint/mobile_core/network/requests/ChoiceAllRequest.kt b/core/src/commonMain/kotlin/com/sourcepoint/mobile_core/network/requests/ChoiceAllRequest.kt new file mode 100644 index 0000000..9f80a62 --- /dev/null +++ b/core/src/commonMain/kotlin/com/sourcepoint/mobile_core/network/requests/ChoiceAllRequest.kt @@ -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) + } +} From ad9920392e3da578ab22cde49a120a2cedc039f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Herculano?= Date: Thu, 5 Dec 2024 14:15:10 -0500 Subject: [PATCH 2/3] remove tests for choice all url and invalid action type --- .../network/SourcepointClientTest.kt | 43 ------------------- 1 file changed, 43 deletions(-) diff --git a/core/src/commonTest/kotlin/com/sourcepoint/mobile_core/network/SourcepointClientTest.kt b/core/src/commonTest/kotlin/com/sourcepoint/mobile_core/network/SourcepointClientTest.kt index 9240a48..bc472e4 100644 --- a/core/src/commonTest/kotlin/com/sourcepoint/mobile_core/network/SourcepointClientTest.kt +++ b/core/src/commonTest/kotlin/com/sourcepoint/mobile_core/network/SourcepointClientTest.kt @@ -32,9 +32,6 @@ import io.ktor.http.headersOf import io.ktor.utils.io.ByteReadChannel import kotlinx.coroutines.delay import kotlinx.coroutines.test.runTest -import kotlinx.serialization.SerializationStrategy -import kotlinx.serialization.json.Json -import kotlinx.serialization.json.JsonObject import kotlin.test.Test import kotlin.test.assertContains import kotlin.test.assertEquals @@ -291,46 +288,6 @@ 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 { - 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( From 9e7251972c874bbb465a53b6de6e4db215f8a7ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Herculano?= Date: Thu, 5 Dec 2024 14:15:27 -0500 Subject: [PATCH 3/3] refactor choice all test to use new request class --- .../network/SourcepointClientTest.kt | 28 +++++++------------ 1 file changed, 10 insertions(+), 18 deletions(-) diff --git a/core/src/commonTest/kotlin/com/sourcepoint/mobile_core/network/SourcepointClientTest.kt b/core/src/commonTest/kotlin/com/sourcepoint/mobile_core/network/SourcepointClientTest.kt index bc472e4..8010f49 100644 --- a/core/src/commonTest/kotlin/com/sourcepoint/mobile_core/network/SourcepointClientTest.kt +++ b/core/src/commonTest/kotlin/com/sourcepoint/mobile_core/network/SourcepointClientTest.kt @@ -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 @@ -292,15 +292,11 @@ class SourcepointClientTest { 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) @@ -311,15 +307,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) @@ -521,7 +513,7 @@ class SourcepointClientTest { assertTrue(response.rejectedAll == false) assertTrue(response.rejectedCategories?.contains("608bae685461ff11a2c2865d") == true) } - + @Test fun postUSNatChoiceActionAcceptContainCorrectResponse() = runTest { val response = api.postChoiceUSNatAction(