Skip to content

Commit

Permalink
Add GET request for accept/reject actions for usnatPm (#829)
Browse files Browse the repository at this point in the history
* Add `GET` logic for `accept/reject` actions at `usnatPm`

* Add new test
  • Loading branch information
Nevazhnovu authored Aug 6, 2024
1 parent 3d3aaf2 commit c577f2b
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ internal class ServiceImpl(
.executeOnLeft { error ->
(error as? ConsentLibExceptionK)?.let { logger.error(error) }
val spConsents = ConsentManager.responseConsentHandler(
gdpr = campaignManager.gdprConsentStatus?.copy(applies = dataStorage.gdprApplies),
ccpa = campaignManager.ccpaConsentStatus?.copy(applies = dataStorage.ccpaApplies),
consentManagerUtils = consentManagerUtils,
)
onSpConsentsSuccess?.invoke(spConsents)
Expand Down Expand Up @@ -544,7 +544,7 @@ internal class ServiceImpl(
(error as? ConsentLibExceptionK)?.let { logger.error(error) }
}

// don't overwrite gdpr consents if the action is accept all or reject all
// don't overwrite ccpa consents if the action is accept all or reject all
// because the response from those endpoints does not contain a full consent
// object.
if (shouldWaitForPost) {
Expand All @@ -566,6 +566,45 @@ internal class ServiceImpl(
consentAction: ConsentActionImpl,
onSpConsentSuccess: ((SPConsents) -> Unit)?,
): Either<USNatConsentData> = check {
var getResp: ChoiceResp? = null
if (consentAction.actionType.isAcceptOrRejectAll()) {
getResp = networkClient.getChoice(
GetChoiceParamReq(
choiceType = consentAction.actionType.toChoiceTypeParam(),
accountId = spConfig.accountId.toLong(),
propertyId = spConfig.propertyId.toLong(),
env = env,
metadataArg = campaignManager.metaDataResp?.toMetaDataArg()?.copy(gdpr = null, ccpa = null),
includeData = buildIncludeData(gppDataValue = campaignManager.spConfig.getGppCustomOption())
)
)
.executeOnRight { response ->
response.usNat?.let { usnatResponse ->
campaignManager.usNatConsentData = usnatResponse.copy(uuid = campaignManager.usNatConsentData?.uuid)
onSpConsentSuccess?.invoke(
ConsentManager.responseConsentHandler(
usNat = usnatResponse.copy(
uuid = campaignManager.usNatConsentData?.uuid,
applies = dataStorage.usNatApplies,
),
consentManagerUtils = consentManagerUtils,
)
)
}
}
.executeOnLeft { error ->
(error as? ConsentLibExceptionK)?.let { logger.error(error) }
val spConsents = ConsentManager.responseConsentHandler(
usNat = campaignManager.usNatConsentData?.copy(applies = dataStorage.usNatApplies),
consentManagerUtils = consentManagerUtils,
)
onSpConsentSuccess?.invoke(spConsents)
}
.getOrNull()
}

val shouldWaitForPost = consentAction.actionType.isAcceptOrRejectAll().not() || getResp?.usNat == null

networkClient.storeUsNatChoice(
PostChoiceParamReq(
env = env,
Expand All @@ -592,12 +631,16 @@ internal class ServiceImpl(
(error as? ConsentLibExceptionK)?.let { logger.error(error) }
}

onSpConsentSuccess?.invoke(
ConsentManager.responseConsentHandler(
usNat = campaignManager.usNatConsentData?.copy(applies = dataStorage.usNatApplies),
consentManagerUtils = consentManagerUtils,
// don't overwrite usNat consents if the action is accept all or reject all
// because the response from those endpoints does not contain a full consent
// object.
if (shouldWaitForPost) {
val spConsents = ConsentManager.responseConsentHandler(
usNat = campaignManager.usNatConsentData?.copy(applies = dataStorage.usNatApplies),
consentManagerUtils = consentManagerUtils,
)
)
onSpConsentSuccess?.invoke(spConsents)
}

campaignManager.usNatConsentData ?: throw InvalidConsentResponse(
cause = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,15 @@ class ClientEventManagerTest {
}

@Test
fun `GIVEN 2 successfully sendConsent (GDPR, CCPA) calls, TRIGGER 1 onSpFinish`() {
fun `GIVEN 3 successfully sendConsent (GDPR, CCPA, USNAT) calls, TRIGGER 1 onSpFinish`() {
clientEventManager.run {
setCampaignsToProcess(2) // 2 campaigns GDPR and CCPA
setAction(ConsentActionImpl(actionType = ACCEPT_ALL, requestFromPm = false, campaignType = CampaignType.GDPR)) // accept the GDPR
setAction(ConsentActionImpl(actionType = ACCEPT_ALL, requestFromPm = false, campaignType = CampaignType.CCPA)) // accept the CCPA
setAction(ConsentActionImpl(actionType = ACCEPT_ALL, requestFromPm = false, campaignType = CampaignType.USNAT)) // accept the USNAT
registerConsentResponse() // first consent saved
registerConsentResponse() // second consent saved
registerConsentResponse() // third consent saved
checkIfAllCampaignsWereProcessed() // check the status
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -356,4 +356,35 @@ class NetworkClientImplTest {
method.assertEquals("POST")
}
}

@Test
fun `storeUsNatChoice - WHEN executed with pubData in request params THEN should have pubData in request for GDPR`() {
// GIVEN
val slot = slot<Request>()
val mockResponse = mockk<Response>()
val mockCall = mockk<Call>()
val mockBody = JsonObject(
mapOf(
"pb_key" to JsonPrimitive("pb_value")
)
)
val mockRequest = PostChoiceParamReq(
env = Env.PROD,
actionType = ActionType.ACCEPT_ALL,
body = mockBody
)

// WHEN
every { okHttp.newCall(any()) }.returns(mockCall)
every { mockCall.execute() }.returns(mockResponse)
sut.storeUsNatChoice(mockRequest)

// THEN
verify(exactly = 1) { responseManager.parsePostUsNatChoiceResp(mockResponse) }
verify(exactly = 1) { okHttp.newCall(capture(slot)) }
slot.captured.run {
readText().let { Json.parseToJsonElement(it).jsonObject }.assertEquals(mockBody)
method.assertEquals("POST")
}
}
}

0 comments on commit c577f2b

Please sign in to comment.