Skip to content

Commit

Permalink
fix: Forward user attributes to kits properly when kit blocking is en…
Browse files Browse the repository at this point in the history
…abled and unplanned UAs are allowed (#947)
  • Loading branch information
mmustafa-tse authored Dec 17, 2024
1 parent 9109876 commit fdbc6ba
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 8 deletions.
24 changes: 16 additions & 8 deletions src/kitBlocking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -460,17 +460,25 @@ export default class KitBlocker {
if (!this.blockUserAttributes) {
return false
}
if (this.blockUserAttributes) {
const matchedAttributes = this.dataPlanMatchLookups['user_attributes'];
if (matchedAttributes === true) {
return false
}
if (!matchedAttributes[key]) {
return true
const matchedAttributes = this.dataPlanMatchLookups['user_attributes'];

// When additionalProperties is set to true, matchedAttributes
// will be a boolean, otherwise it will return an object
if (typeof matchedAttributes === 'boolean' && matchedAttributes) {
return false
}

if (typeof matchedAttributes === "object") {
if (matchedAttributes[key] === true) {
return false;
} else {
return true;
}
}

return false
// When "Block unplanned user attributes" is enabled and "Allow unplanned user
// attributes" is also enabled in the UI, allowing unplanned user attributes will be prioritized
return false;
}

isIdentityBlocked(key: string) {
Expand Down
21 changes: 21 additions & 0 deletions test/src/tests-kit-blocking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import KitBlocker from '../../src/kitBlocking';
import Types from '../../src/types';
import { DataPlanVersion } from '@mparticle/data-planning-models';
import fetchMock from 'fetch-mock/esm/client';
import { expect } from 'chai'
const { findBatch, waitForCondition, fetchMockSuccess, hasIdentifyReturned } = Utils;

let forwarderDefaultConfiguration = Utils.forwarderDefaultConfiguration,
Expand Down Expand Up @@ -616,6 +617,26 @@ describe('kit blocking', () => {
})
});

it('integration test - should not throw an error when unplanned user attributes are allowed and block.ua = true', function(done) {
window.mParticle.config.kitConfigs.push(forwarderDefaultConfiguration('MockForwarder'));
window.mParticle.init(apiKey, window.mParticle.config);

// save old data points for reset later
const oldDataPoints = dataPlan.dtpn.vers.version_document.data_points;
// when "Allow unplanned user attributes" is enabled, the data points returned is an empty array
dataPlan.dtpn.vers.version_document.data_points = [];
let kitBlocker = new KitBlocker(kitBlockerDataPlan, window.mParticle.getInstance());

expect(() => { kitBlocker.isAttributeKeyBlocked('unplannedAttr') }).to.not.throw(TypeError, /Cannot read properties of undefined \(reading 'unplannedAttr'\)/)
// "Allow unplanned user attributes" is prioritized when blocking unplanned attributes is also enabled, hence the expected value is false
expect(kitBlocker.isAttributeKeyBlocked('unplannedAttr')).to.equal(false)
// reset data points
dataPlan.dtpn.vers.version_document.data_points = oldDataPoints;

done();

});

it('integration test - should allow an unplanned attribute to be set on forwarder if additionalProperties = true and blok.ua = true', function(done) {
let userAttributeDataPoint = dataPlan.dtpn.vers.version_document.data_points.find(dataPoint => {
return dataPoint.match.type === 'user_attributes'
Expand Down

0 comments on commit fdbc6ba

Please sign in to comment.