Skip to content

Commit 0bb28fa

Browse files
committed
fix: Pass cleaned identities to identity callbacks
1 parent 3cb1319 commit 0bb28fa

File tree

2 files changed

+289
-4
lines changed

2 files changed

+289
-4
lines changed

src/identity.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ export default function Identity(mpInstance) {
292292
identityApiRequest,
293293
Identify,
294294
callback,
295-
identityApiData,
295+
preProcessResult.cleanedIdentities,
296296
self.parseIdentityResponse,
297297
mpid,
298298
identityApiRequest.known_identities
@@ -368,7 +368,7 @@ export default function Identity(mpInstance) {
368368
identityApiRequest,
369369
Logout,
370370
callback,
371-
identityApiData,
371+
preProcessResult.cleanedIdentities,
372372
self.parseIdentityResponse,
373373
mpid
374374
);
@@ -478,7 +478,7 @@ export default function Identity(mpInstance) {
478478
identityApiRequest,
479479
Login,
480480
callback,
481-
identityApiData,
481+
preProcessResult.cleanedIdentities,
482482
self.parseIdentityResponse,
483483
mpid,
484484
identityApiRequest.known_identities
@@ -557,7 +557,7 @@ export default function Identity(mpInstance) {
557557
identityApiRequest,
558558
Modify,
559559
callback,
560-
identityApiData,
560+
preProcessResult.cleanedIdentities,
561561
self.parseIdentityResponse,
562562
mpid,
563563
identityApiRequest.known_identities

test/src/tests-identity.ts

Lines changed: 285 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4718,4 +4718,289 @@ describe('identity', function() {
47184718
);
47194719
});
47204720
});
4721+
4722+
describe('cleaned identities in callbacks', () => {
4723+
let mockForwarder;
4724+
let identityCallbackResult;
4725+
let onUserIdentifiedResult;
4726+
4727+
beforeEach(async () => {
4728+
await waitForCondition(hasBeforeEachCallbackReturned);
4729+
mParticle._resetForTests(MPConfig);
4730+
fetchMock.resetHistory();
4731+
4732+
// Set up mock forwarder to capture onUserIdentified calls
4733+
mockForwarder = new MockForwarder();
4734+
mockForwarder.register(window.mParticle.config);
4735+
4736+
const config1 = forwarderDefaultConfiguration('MockForwarder', 1);
4737+
window.mParticle.config.kitConfigs.push(config1);
4738+
4739+
// Set up identity callback to capture results
4740+
mParticle.config.identityCallback = function(result) {
4741+
identityCallbackResult = result;
4742+
};
4743+
});
4744+
4745+
afterEach(() => {
4746+
identityCallbackResult = null;
4747+
onUserIdentifiedResult = null;
4748+
});
4749+
4750+
it('should pass cleaned identities to onUserIdentified callback, removing falsy values', async () => {
4751+
fetchMockSuccess(urls.identify, {
4752+
mpid: 'test-mpid-cleaned',
4753+
is_logged_in: false,
4754+
matched_identities: {
4755+
4756+
customerid: 'customer123'
4757+
}
4758+
});
4759+
4760+
mParticle.init(apiKey, window.mParticle.config);
4761+
await waitForCondition(hasIdentifyReturned);
4762+
4763+
// Make identify call with falsy values that should be cleaned
4764+
const identityRequest = {
4765+
userIdentities: {
4766+
4767+
customerid: 'customer123',
4768+
other: null, // Should be removed
4769+
other2: undefined, // Should be removed
4770+
other3: '', // Should be removed
4771+
other4: false, // Should be removed
4772+
other5: 0, // Should be removed
4773+
facebook: 'valid-fb-id' // Should be kept
4774+
}
4775+
};
4776+
4777+
fetchMockSuccess(urls.identify, {
4778+
mpid: 'cleaned-identity-mpid',
4779+
is_logged_in: false,
4780+
matched_identities: {
4781+
4782+
customerid: 'customer123',
4783+
facebook: 'valid-fb-id'
4784+
}
4785+
});
4786+
4787+
mParticle.Identity.identify(identityRequest);
4788+
4789+
await waitForCondition(() => {
4790+
return mParticle.getInstance()._Store.identityCallInFlight === false;
4791+
});
4792+
4793+
// Verify onUserIdentified received cleaned identities
4794+
const onUserIdentifiedUser = window.MockForwarder1.instance.onUserIdentifiedUser;
4795+
expect(onUserIdentifiedUser).to.be.ok;
4796+
4797+
const userIdentities = onUserIdentifiedUser.getUserIdentities().userIdentities;
4798+
4799+
// Should have valid identities
4800+
expect(userIdentities).to.have.property('email', '[email protected]');
4801+
expect(userIdentities).to.have.property('customerid', 'customer123');
4802+
expect(userIdentities).to.have.property('facebook', 'valid-fb-id');
4803+
4804+
// Should NOT have falsy identities
4805+
expect(userIdentities).to.not.have.property('other');
4806+
expect(userIdentities).to.not.have.property('other2');
4807+
expect(userIdentities).to.not.have.property('other3');
4808+
expect(userIdentities).to.not.have.property('other4');
4809+
expect(userIdentities).to.not.have.property('other5');
4810+
});
4811+
4812+
it('should pass cleaned identities to identity callback, removing falsy values', async () => {
4813+
fetchMockSuccess(urls.identify, {
4814+
mpid: 'callback-test-mpid',
4815+
is_logged_in: false,
4816+
matched_identities: {
4817+
4818+
customerid: 'callback123'
4819+
}
4820+
});
4821+
4822+
mParticle.init(apiKey, window.mParticle.config);
4823+
await waitForCondition(hasIdentifyReturned);
4824+
4825+
// Make identify call with falsy values
4826+
const identityRequest = {
4827+
userIdentities: {
4828+
4829+
customerid: 'callback123',
4830+
other: null,
4831+
other2: '',
4832+
facebook: 'fb-callback-id'
4833+
}
4834+
};
4835+
4836+
fetchMockSuccess(urls.identify, {
4837+
mpid: 'callback-cleaned-mpid',
4838+
is_logged_in: false,
4839+
matched_identities: {
4840+
4841+
customerid: 'callback123',
4842+
facebook: 'fb-callback-id'
4843+
}
4844+
});
4845+
4846+
mParticle.Identity.identify(identityRequest);
4847+
4848+
await waitForCondition(() => {
4849+
return identityCallbackResult && identityCallbackResult.body;
4850+
});
4851+
4852+
// Verify identity callback received cleaned identities
4853+
expect(identityCallbackResult).to.be.ok;
4854+
expect(identityCallbackResult.body).to.be.ok;
4855+
4856+
// The callback should receive the response from the server with matched_identities
4857+
// which should only contain the cleaned identities that were sent
4858+
const matchedIdentities = identityCallbackResult.body.matched_identities;
4859+
expect(matchedIdentities).to.have.property('email', '[email protected]');
4860+
expect(matchedIdentities).to.have.property('customerid', 'callback123');
4861+
expect(matchedIdentities).to.have.property('facebook', 'fb-callback-id');
4862+
4863+
// Should not contain falsy values
4864+
expect(matchedIdentities).to.not.have.property('other');
4865+
expect(matchedIdentities).to.not.have.property('other2');
4866+
});
4867+
4868+
it('should verify that actual network request contains only cleaned identities', async () => {
4869+
mParticle.init(apiKey, window.mParticle.config);
4870+
await waitForCondition(hasIdentifyReturned);
4871+
4872+
fetchMock.resetHistory();
4873+
4874+
// Make identify call with falsy values
4875+
const identityRequest = {
4876+
userIdentities: {
4877+
4878+
customerid: 'network123',
4879+
other: null,
4880+
other2: undefined,
4881+
other3: '',
4882+
other4: false,
4883+
google: 'google-id'
4884+
}
4885+
};
4886+
4887+
fetchMockSuccess(urls.identify, {
4888+
mpid: 'network-test-mpid',
4889+
is_logged_in: false
4890+
});
4891+
4892+
mParticle.Identity.identify(identityRequest);
4893+
4894+
await waitForCondition(() => {
4895+
return mParticle.getInstance()._Store.identityCallInFlight === false;
4896+
});
4897+
4898+
// Verify the actual network request was made with cleaned identities
4899+
const identifyCalls = fetchMock.calls().filter(call =>
4900+
call[0].includes('/identify')
4901+
);
4902+
expect(identifyCalls.length).to.be.greaterThan(0);
4903+
4904+
const lastIdentifyCall = identifyCalls[identifyCalls.length - 1];
4905+
const requestBody = JSON.parse(lastIdentifyCall[1].body as string);
4906+
4907+
expect(requestBody).to.have.property('known_identities');
4908+
const sentIdentities = requestBody.known_identities;
4909+
4910+
// Should contain valid identities
4911+
expect(sentIdentities).to.have.property('email', '[email protected]');
4912+
expect(sentIdentities).to.have.property('customerid', 'network123');
4913+
expect(sentIdentities).to.have.property('google', 'google-id');
4914+
4915+
// Should NOT contain falsy identities in the network request
4916+
expect(sentIdentities).to.not.have.property('other');
4917+
expect(sentIdentities).to.not.have.property('other2');
4918+
expect(sentIdentities).to.not.have.property('other3');
4919+
expect(sentIdentities).to.not.have.property('other4');
4920+
});
4921+
4922+
it('should handle login calls with cleaned identities', async () => {
4923+
mParticle.init(apiKey, window.mParticle.config);
4924+
await waitForCondition(hasIdentifyReturned);
4925+
4926+
const loginRequest = {
4927+
userIdentities: {
4928+
4929+
customerid: 'login123',
4930+
other: null,
4931+
other2: '',
4932+
facebook: 'login-fb-id'
4933+
}
4934+
};
4935+
4936+
fetchMockSuccess(urls.login, {
4937+
mpid: 'login-cleaned-mpid',
4938+
is_logged_in: true,
4939+
matched_identities: {
4940+
4941+
customerid: 'login123',
4942+
facebook: 'login-fb-id'
4943+
}
4944+
});
4945+
4946+
mParticle.Identity.login(loginRequest);
4947+
4948+
await waitForCondition(() => {
4949+
return mParticle.getInstance()._Store.identityCallInFlight === false;
4950+
});
4951+
4952+
// Verify onLoginComplete received cleaned identities
4953+
const onLoginCompleteUser = window.MockForwarder1.instance.onLoginCompleteUser;
4954+
expect(onLoginCompleteUser).to.be.ok;
4955+
4956+
const userIdentities = onLoginCompleteUser.getUserIdentities().userIdentities;
4957+
expect(userIdentities).to.have.property('email', '[email protected]');
4958+
expect(userIdentities).to.have.property('customerid', 'login123');
4959+
expect(userIdentities).to.have.property('facebook', 'login-fb-id');
4960+
expect(userIdentities).to.not.have.property('other');
4961+
expect(userIdentities).to.not.have.property('other2');
4962+
});
4963+
4964+
it('should handle modify calls with cleaned identities', async () => {
4965+
mParticle.init(apiKey, window.mParticle.config);
4966+
await waitForCondition(hasIdentifyReturned);
4967+
4968+
const modifyRequest = {
4969+
userIdentities: {
4970+
4971+
customerid: 'modify123',
4972+
other: null,
4973+
other2: false,
4974+
twitter: 'twitter-handle'
4975+
}
4976+
};
4977+
4978+
fetchMockSuccess(urls.modify, {
4979+
mpid: testMPID,
4980+
is_logged_in: false,
4981+
matched_identities: {
4982+
4983+
customerid: 'modify123',
4984+
twitter: 'twitter-handle'
4985+
}
4986+
});
4987+
4988+
mParticle.Identity.modify(modifyRequest);
4989+
4990+
await waitForCondition(() => {
4991+
return mParticle.getInstance()._Store.identityCallInFlight === false;
4992+
});
4993+
4994+
// Verify onModifyComplete received cleaned identities
4995+
const onModifyCompleteUser = window.MockForwarder1.instance.onModifyCompleteUser;
4996+
expect(onModifyCompleteUser).to.be.ok;
4997+
4998+
const userIdentities = onModifyCompleteUser.getUserIdentities().userIdentities;
4999+
expect(userIdentities).to.have.property('email', '[email protected]');
5000+
expect(userIdentities).to.have.property('customerid', 'modify123');
5001+
expect(userIdentities).to.have.property('twitter', 'twitter-handle');
5002+
expect(userIdentities).to.not.have.property('other');
5003+
expect(userIdentities).to.not.have.property('other2');
5004+
});
5005+
});
47215006
});

0 commit comments

Comments
 (0)