diff --git a/src/kitBlocking.ts b/src/kitBlocking.ts index a75f3fec..86bb779b 100644 --- a/src/kitBlocking.ts +++ b/src/kitBlocking.ts @@ -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) { diff --git a/test/src/tests-kit-blocking.ts b/test/src/tests-kit-blocking.ts index c0240ae1..0c964960 100644 --- a/test/src/tests-kit-blocking.ts +++ b/test/src/tests-kit-blocking.ts @@ -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, @@ -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'