Skip to content

Commit

Permalink
Refactor and increase testing
Browse files Browse the repository at this point in the history
  • Loading branch information
SbDove committed Oct 9, 2024
1 parent 07ddbb3 commit edb93c3
Show file tree
Hide file tree
Showing 4 changed files with 188 additions and 18 deletions.
51 changes: 43 additions & 8 deletions src/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,19 @@ Common.prototype.buildPartnerData = function (mParticleUser) {
var pdKeys = {};
var userIdentities = mParticleUser.getUserIdentities();

var email = userIdentities.userIdentities['email'];
var email = this.normalizeEmail(userIdentities.userIdentities['email']);
var phone = this.normalizePhone(userIdentities.userIdentities['mobile_number']);

if (!email && !phone) {
return null;
}

if (email) {
pdKeys[1] = SHA256(this.normalizeEmail(email));
pdKeys[1] = SHA256(email);
}

var phone = userIdentities.userIdentities['mobile_number'];
if (phone) {
pdKeys[2]= SHA256(this.normalizePhone(phone));
pdKeys[2]= SHA256(phone);
}

var pdRaw = Object.keys(pdKeys).map(function(key){
Expand All @@ -44,26 +49,39 @@ Common.prototype.buildPartnerData = function (mParticleUser) {
}

Common.prototype.normalizeEmail = function(email) {
if (!email || !this.validateEmail(email)) {
return null;
}
var parts = email.split("@")
var charactersToRemove = ['+', '.']

if (parts[1] != 'gmail.com') {
return email;
}

parts[0]= replace(parts[0], charactersToRemove);
parts[0]= this.replace(parts[0], charactersToRemove);

return parts.join('@');
}

Common.prototype.normalizePhone = function(phone) {
if (!phone) {
return null;
}
var charactersToRemove = [' ', '-', '(', ')']
var normalizedPhone = this.replace(phone, charactersToRemove);

if (normalizedPhone[0] !== '+') {
normalizedPhone = '+' + normalizedPhone
}

return replace(phone, charactersToRemove);
if (!this.validatePhone(normalizedPhone)) {
return null;
}
return normalizedPhone;
}

function replace(string, targets) {
debugger;
Common.prototype.replace = function(string, targets) {
var newString = '';
for(var i = 0; i < string.length; i++){
var char = string[i];
Expand All @@ -73,4 +91,21 @@ function replace(string, targets) {
}
return newString.toLowerCase();
}

Common.prototype.validateEmail = function(email){
if (!email) {
return false;
}
var emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return emailRegex.test(email);
}

Common.prototype.validatePhone = function(phone) {
if (!phone){
return false;
}
var e164Regex = /^\+?[1-9]\d{1,14}$/;

return e164Regex.test(phone);
}
module.exports = Common;
15 changes: 10 additions & 5 deletions src/identity-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,23 @@ function IdentityHandler(common) {
IdentityHandler.prototype.onUserIdentified = function() {};
IdentityHandler.prototype.onIdentifyComplete = function() {};

//Must re-initialize ID5 with partner identities(pd) in the config file on login
IdentityHandler.prototype.onLoginComplete = function(
mParticleUser
) {
var partnerData = this.common.buildPartnerData(mParticleUser);
var id5Instance = window.ID5.init({partnerId: this.common.partnerId, pd: partnerData})
var logId5Id = this.common.logId5Id;

id5Instance.onAvailable(function(status){
logId5Id(status.getUserId());
}.bind(logId5Id));
if (partnerData) {
var id5Instance = window.ID5.init({partnerId: this.common.partnerId, pd: partnerData})
var logId5Id = this.common.logId5Id;

id5Instance.onAvailable(function(status){
logId5Id(status.getUserId());
}.bind(logId5Id));
}
};

//Must re-initialize ID5 without partner identities (pd) in the config on logout complete
IdentityHandler.prototype.onLogoutComplete = function(
) {
var id5Instance = window.ID5.init({partnerId: this.common.partnerId})
Expand Down
4 changes: 2 additions & 2 deletions src/initialization.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ var initialization = {
/* Load your Web SDK here using a variant of your snippet from your readme that your customers would generally put into their <head> tags
Generally, our integrations create script tags and append them to the <head>. Please follow the following format as a guide:
*/

//ID5 docs on initialization can be found here: https://github.com/id5io/id5-api.js/blob/master/README.md
var id5Script = document.createElement('script');
id5Script.src = 'https://cdn.id5-sync.com/api/1.0/id5-api.js'; // <---- Update this to be your script
id5Script.src = 'https://cdn.id5-sync.com/api/1.0/id5-api.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(id5Script);

common.id5Id = null;
Expand Down
136 changes: 133 additions & 3 deletions test/src/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,22 @@ describe('ID5 Forwarder', function () {
done();
});

it('should call ID5.init only once on userLoginComplete with a user without email or phone', function(done){
var user = {
getUserIdentities: function () {
return {
userIdentities: {
customerId: 'testId1234',
},
};
},
};
mParticle.forwarder.onLoginComplete(user);

window.ID5.numberOfInitsCalled.should.equal(1);
done();
})

it('should call ID5.init twice on userLogoutComplete', function(done){
var user = {
getUserIdentities: function () {
Expand Down Expand Up @@ -157,7 +173,7 @@ describe('ID5 Forwarder', function () {
return {
userIdentities: {
email: '[email protected]',
phone: '123-456-7890',
mobile_number: '123-456-7890',
}
}
},
Expand All @@ -166,10 +182,82 @@ describe('ID5 Forwarder', function () {
var pd = mParticle.forwarder.common.buildPartnerData(user)

pd.should.exist;
pd.should.equal('MT03MzA2MmQ4NzI5MjZjMmE1NTZmMTdiMzZmNTBlMzI4ZGRmOWJmZjlkNDAzOTM5YmQxNGI2YzNiN2Y1YTMzZmMy')
done();
});

it ('should build null pd when build PartnerData is called with an empty users', function(done){
var user = {
getUserIdentities: function() {
return {
userIdentities: {}
}
},
};
var pd = mParticle.forwarder.common.buildPartnerData(user)
debugger;
expect(pd).to.be.null;
done();
});

it ('should return null pd when buildPartnerData is called with a user only having an invalid phone number', function(done) {
var user = {
getUserIdentities: function() {
return {
userIdentities: {
mobile_number: '1234567890112233'
}
}
},
}
var pd = mParticle.forwarder.common.buildPartnerData(user)

expect(pd).to.be.null;
done();
});

it ('should return null pd when buildPartnerData is called with a user only having an invalid email', function(done) {
var user = {
getUserIdentities: function() {
return {
userIdentities: {
email: 'test@[email protected]'
}
}
},
}
var pd = mParticle.forwarder.common.buildPartnerData(user)

expect(pd).to.be.null;
done();
});

it ('should omit invalid identities when building PD when buildPartnerData is called', function(done) {
var user1 = {
getUserIdentities: function() {
return {
userIdentities: {
email: 'test@[email protected]', //note: invalid email address
mobile_number: '123-456-7890',
}
}
},
}
var user2 = {
getUserIdentities: function() {
return {
userIdentities: {
mobile_number: '123-456-7890',
}
}
},
}
var pd1 = mParticle.forwarder.common.buildPartnerData(user1);
var pd2 = mParticle.forwarder.common.buildPartnerData(user2);

pd1.should.equal(pd2);
done();
})

it ('should normalize an gmail when normalizeEmail is called', function(done) {
var normalizedGmail = mParticle.forwarder.common.normalizeEmail('[email protected]');

Expand All @@ -186,11 +274,53 @@ describe('ID5 Forwarder', function () {
done();
});

it ('should return null when normalizeEmail is called with a null value', function(done){
var normalized = mParticle.forwarder.common.normalizeEmail()

expect(normalized).to.be.null;
done();
});

it ('should normalize phone numbers when normalizePhone is called', function(done) {
var normalizedPhone = mParticle.forwarder.common.normalizePhone('(123) 456-7890');

normalizedPhone.should.exist;
normalizedPhone.should.equal('1234567890');
normalizedPhone.should.equal('+1234567890');
done();
});

it ('should return null when normalizePhone is called with a null value', function(done){
var normalizedPhone = mParticle.forwarder.common.normalizePhone();

expect(normalizedPhone).to.be.null;
done();
});

it ('should return true when a valid e.164 phone number is passed to validatePhone', function(done) {
var validated = mParticle.forwarder.common.validatePhone('+1234567890');

validated.should.equal(true);
done();
});

it ('should return false when an invalid e.164 phone number is passed to validatePhone', function(done) {
var validated = mParticle.forwarder.common.validatePhone('1234567890112233');

validated.should.equal(false);
done();
});

it ('should return true when a valid email is passed to validateEmail', function(done) {
var validated = mParticle.forwarder.common.validateEmail('[email protected]');

validated.should.equal(true);
done();
})

it ('should return false when an invalid email is passed to validateEmail', function(done) {
var validated = mParticle.forwarder.common.validateEmail('test@[email protected]')

validated.should.equal(false);
done();
})
})
Expand Down

0 comments on commit edb93c3

Please sign in to comment.