Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GET request for accept/reject actions for usnatPm #829

Merged
merged 2 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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),
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that was a typo

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. Could've been a copy & paste mistake.

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")
}
}
}