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

Utiq: Update domain resolving logic and tests #10044

Merged
merged 1 commit into from
Jun 5, 2023
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
71 changes: 14 additions & 57 deletions modules/utiqSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,89 +11,46 @@ import { MODULE_TYPE_UID } from '../src/activities/modules.js';

const MODULE_NAME = 'utiq';
const LOG_PREFIX = 'Utiq module';
let mnoDomain = '';

export const storage = getStorageManager({
moduleType: MODULE_TYPE_UID,
moduleName: MODULE_NAME,
});

/**
* Handle an event for an iframe.
* Takes the body.url parameter from event and returns the string domain.
* i.e.: "fc.vodafone.de"
* @param event
*/
function messageHandler(event) {
try {
if (event && event.data && typeof event.data === 'string') {
const msg = JSON.parse(event.data);
if (msg.msgType === 'MNOSELECTOR' && msg.body && msg.body.url) {
let URL = msg.body.url.split('//');
let domainURL = URL[1].split('/');
mnoDomain = domainURL[0];
logInfo(`${LOG_PREFIX}: Message handler set domain to ${mnoDomain}`);
}
}
} catch (e) {
logInfo(
`${LOG_PREFIX}: Unsupported message caught. Origin: ${event.origin}, data: ${event.data}.`
);
}
}

// Set a listener to handle the iframe response message.
window.addEventListener('message', messageHandler, false);

/**
* Get the "atid" from html5 local storage to make it available to the UserId module.
* @param config
* @returns {{utiq: (*|string)}}
*/
function getUtiqFromStorage() {
// Get the domain either from localStorage or global
let domain =
JSON.parse(storage.getDataFromLocalStorage('fcIdConnectDomain')) ||
mnoDomain;
logInfo(`${LOG_PREFIX}: Local storage domain: ${domain}`);

if (!domain) {
logInfo(`${LOG_PREFIX}: Local storage domain not found, returning null`);
return {
utiq: null,
};
}

let fcIdConnectObject;
let fcIdConnectData = JSON.parse(
storage.getDataFromLocalStorage('fcIdConnectData')
let utiqPass;
let utiqPassStorage = JSON.parse(
storage.getDataFromLocalStorage('utiqPass')
);
logInfo(
`${LOG_PREFIX}: Local storage fcIdConnectData: ${JSON.stringify(
fcIdConnectData
`${LOG_PREFIX}: Local storage utiqPass: ${JSON.stringify(
utiqPassStorage
)}`
);

if (
fcIdConnectData &&
fcIdConnectData.connectId &&
Array.isArray(fcIdConnectData.connectId.idGraph) &&
fcIdConnectData.connectId.idGraph.length > 0
utiqPassStorage &&
utiqPassStorage.connectId &&
Array.isArray(utiqPassStorage.connectId.idGraph) &&
utiqPassStorage.connectId.idGraph.length > 0
) {
fcIdConnectObject = fcIdConnectData.connectId.idGraph.find((item) => {
return item.domain === domain;
});
utiqPass = utiqPassStorage.connectId.idGraph[0];
}
logInfo(
`${LOG_PREFIX}: Local storage fcIdConnectObject for domain: ${JSON.stringify(
fcIdConnectObject
`${LOG_PREFIX}: Graph of utiqPass: ${JSON.stringify(
utiqPass
)}`
);

return {
utiq:
fcIdConnectObject && fcIdConnectObject.atid
? fcIdConnectObject.atid
utiqPass && utiqPass.atid
? utiqPass.atid
: null,
};
}
Expand Down
71 changes: 11 additions & 60 deletions test/spec/modules/utiqSystem_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import { utiqSubmodule } from 'modules/utiqSystem.js';
import { storage } from 'modules/utiqSystem.js';

describe('utiqSystem', () => {
const connectDataKey = 'fcIdConnectData';
const connectDomainKey = 'fcIdConnectDomain';
const utiqPassKey = 'utiqPass';

const getStorageData = (idGraph) => {
if (!idGraph) {
Expand All @@ -23,20 +22,15 @@ describe('utiqSystem', () => {

describe('utiq getId()', () => {
afterEach(() => {
storage.removeDataFromLocalStorage(connectDataKey);
storage.removeDataFromLocalStorage(connectDomainKey);
storage.removeDataFromLocalStorage(utiqPassKey);
});

after(() => {
window.FC_CONF = {};
})

it('it should return object with key callback', () => {
expect(utiqSubmodule.getId()).to.have.property('callback');
});

it('should return object with key callback with value type - function', () => {
storage.setDataInLocalStorage(connectDataKey, JSON.stringify(getStorageData()));
storage.setDataInLocalStorage(utiqPassKey, JSON.stringify(getStorageData()));
expect(utiqSubmodule.getId()).to.have.property('callback');
expect(typeof utiqSubmodule.getId().callback).to.be.equal('function');
});
Expand All @@ -46,27 +40,16 @@ describe('utiqSystem', () => {
'domain': 'domainValue',
'atid': 'atidValue',
};
storage.setDataInLocalStorage(connectDataKey, JSON.stringify(getStorageData(idGraph)));
expect(JSON.parse(storage.getDataFromLocalStorage(connectDataKey))).to.have.property('connectId');
});

it('returns {callback: func} if domains don\'t match', () => {
const idGraph = {
'domain': 'domainValue',
'atid': 'atidValue',
};
storage.setDataInLocalStorage(connectDomainKey, JSON.stringify('differentDomainValue'));
storage.setDataInLocalStorage(connectDataKey, JSON.stringify(getStorageData(idGraph)));
expect(utiqSubmodule.getId()).to.have.property('callback');
storage.setDataInLocalStorage(utiqPassKey, JSON.stringify(getStorageData(idGraph)));
expect(JSON.parse(storage.getDataFromLocalStorage(utiqPassKey))).to.have.property('connectId');
});

it('returns {id: {utiq: data.utiq}} if we have the right data stored in the localstorage ', () => {
const idGraph = {
'domain': 'test.domain',
'atid': 'atidValue',
};
storage.setDataInLocalStorage(connectDomainKey, JSON.stringify('test.domain'));
storage.setDataInLocalStorage(connectDataKey, JSON.stringify(getStorageData(idGraph)));
storage.setDataInLocalStorage(utiqPassKey, JSON.stringify(getStorageData(idGraph)));
const response = utiqSubmodule.getId();
expect(response).to.have.property('id');
expect(response.id).to.have.property('utiq');
Expand All @@ -83,8 +66,7 @@ describe('utiqSystem', () => {
expect(response.callback.toString()).contain('result(callback)');

if (typeof response.callback === 'function') {
storage.setDataInLocalStorage(connectDomainKey, JSON.stringify('test.domain'));
storage.setDataInLocalStorage(connectDataKey, JSON.stringify(getStorageData(idGraph)));
storage.setDataInLocalStorage(utiqPassKey, JSON.stringify(getStorageData(idGraph)));
response.callback(function (result) {
expect(result).to.not.be.null;
expect(result).to.have.property('utiq');
Expand All @@ -94,29 +76,6 @@ describe('utiqSystem', () => {
}
});

it('returns null if domains don\'t match', (done) => {
const idGraph = {
'domain': 'test.domain',
'atid': 'atidValue',
};
storage.setDataInLocalStorage(connectDomainKey, JSON.stringify('differentDomainValue'));
storage.setDataInLocalStorage(connectDataKey, JSON.stringify(getStorageData(idGraph)));

const response = utiqSubmodule.getId();
expect(response).to.have.property('callback');
expect(response.callback.toString()).contain('result(callback)');

if (typeof response.callback === 'function') {
setTimeout(() => {
expect(JSON.parse(storage.getDataFromLocalStorage(connectDomainKey))).to.be.equal('differentDomainValue');
}, 100)
response.callback(function (result) {
expect(result).to.be.null;
done()
})
}
});

it('returns {utiq: data.utiq} if we have the right data stored in the localstorage right after 500ms delay', (done) => {
const idGraph = {
'domain': 'test.domain',
Expand All @@ -129,8 +88,7 @@ describe('utiqSystem', () => {

if (typeof response.callback === 'function') {
setTimeout(() => {
storage.setDataInLocalStorage(connectDomainKey, JSON.stringify('test.domain'));
storage.setDataInLocalStorage(connectDataKey, JSON.stringify(getStorageData(idGraph)));
storage.setDataInLocalStorage(utiqPassKey, JSON.stringify(getStorageData(idGraph)));
}, 500);
response.callback(function (result) {
expect(result).to.not.be.null;
Expand All @@ -153,8 +111,7 @@ describe('utiqSystem', () => {

if (typeof response.callback === 'function') {
setTimeout(() => {
storage.setDataInLocalStorage(connectDomainKey, JSON.stringify('test.domain'));
storage.setDataInLocalStorage(connectDataKey, JSON.stringify(getStorageData(idGraph)));
storage.setDataInLocalStorage(utiqPassKey, JSON.stringify(getStorageData(idGraph)));
}, 500);
response.callback(function (result) {
expect(result).to.be.null;
Expand Down Expand Up @@ -196,14 +153,9 @@ describe('utiqSystem', () => {

describe('utiq messageHandler', () => {
afterEach(() => {
storage.removeDataFromLocalStorage(connectDataKey);
storage.removeDataFromLocalStorage(connectDomainKey);
storage.removeDataFromLocalStorage(utiqPassKey);
});

after(() => {
window.FC_CONF = {};
})

const domains = [
'domain1',
'domain2',
Expand All @@ -217,8 +169,7 @@ describe('utiqSystem', () => {
'atid': 'atidValue',
};

storage.setDataInLocalStorage(connectDomainKey, JSON.stringify(domain));
storage.setDataInLocalStorage(connectDataKey, JSON.stringify(getStorageData(idGraph)));
storage.setDataInLocalStorage(utiqPassKey, JSON.stringify(getStorageData(idGraph)));

const eventData = {
data: `{\"msgType\":\"MNOSELECTOR\",\"body\":{\"url\":\"https://${domain}/some/path\"}}`
Expand Down