The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
import { applyReplacementTags } from 'src/engine/core-modules/emailing-domain/utils/apply-replacement-tags.util';
import { buildCampaignBatchReplacements } from 'src/modules/emailing/utils/build-campaign-batch-replacements.util';
describe('buildCampaignBatchReplacements', () => {
it('escapes a value for the html body and leaves it raw for the text body', () => {
const replacements = buildCampaignBatchReplacements({
campaignVariables: [{ name: 'name.firstName' }],
variables: { 'name.firstName': '<img onerror="x">' },
});
expect(applyReplacementTags('Hello {{v_h_0}}', replacements)).toBe(
'Hello <img onerror="x">',
);
expect(applyReplacementTags('Hello {{v_t_0}}', replacements)).toBe(
'Hello <img onerror="x">',
);
});
it('percent-encodes a value destined for a url so it cannot leave its attribute', () => {
const replacements = buildCampaignBatchReplacements({
campaignVariables: [{ name: 'name.firstName' }],
variables: { 'name.firstName': '" onmouseover="alert(1)' },
});
expect(
applyReplacementTags(
'<a href="https://x.com/{{v_u_0}}">go</a>',
replacements,
),
).toBe('<a href="https://x.com/%22%20onmouseover%3D%22alert(1)">go</a>');
});
it('strips the scheme separator from a url value so it cannot become executable', () => {
const replacements = buildCampaignBatchReplacements({
campaignVariables: [{ name: 'website' }],
variables: { website: 'javascript:alert(1)' },
});
expect(applyReplacementTags('{{v_u_0}}', replacements)).toBe(
'javascript%3Aalert(1)',
);
});
it('substitutes an empty string for a variable the recipient has no value for', () => {
const replacements = buildCampaignBatchReplacements({
campaignVariables: [{ name: 'jobTitle' }],
variables: {},
});
expect(applyReplacementTags('Role: {{v_t_0}}.', replacements)).toBe(
'Role: .',
);
});
it('keeps each variable on its own pair of tags', () => {
const replacements = buildCampaignBatchReplacements({
campaignVariables: [{ name: 'first' }, { name: 'second' }],
variables: { first: 'Ada', second: 'Lovelace' },
});
expect(applyReplacementTags('{{v_t_0}} {{v_t_1}}', replacements)).toBe(
'Ada Lovelace',
);
});
it('leaves a tag the recipient has no replacement for untouched rather than blanking it', () => {
const replacements = buildCampaignBatchReplacements({
campaignVariables: [{ name: 'known' }],
variables: { known: 'yes' },
});
expect(applyReplacementTags('{{v_t_0}} {{v_t_9}}', replacements)).toBe(
'yes {{v_t_9}}',
);
});
});
describe('fallback variables', () => {
it('uses the fallback for a recipient whose value is only whitespace', () => {
const replacements = buildCampaignBatchReplacements({
campaignVariables: [{ name: 'jobTitle', fallback: 'there' }],
variables: { jobTitle: ' \n ' },
});
expect(applyReplacementTags('Hi {{v_t_0}}', replacements)).toBe('Hi there');
});
it('keeps the recipient value when there is one', () => {
const replacements = buildCampaignBatchReplacements({
campaignVariables: [{ name: 'jobTitle', fallback: 'there' }],
variables: { jobTitle: 'CTO' },
});
expect(applyReplacementTags('Hi {{v_t_0}}', replacements)).toBe('Hi CTO');
});
it('escapes the fallback for html and percent-encodes it for urls like a value', () => {
const replacements = buildCampaignBatchReplacements({
campaignVariables: [{ name: 'jobTitle', fallback: "<b>R&D</b> 'lead'" }],
variables: {},
});
expect(applyReplacementTags('{{v_h_0}}', replacements)).toBe(
'<b>R&D</b> 'lead'',
);
expect(applyReplacementTags('{{v_t_0}}', replacements)).toBe(
"<b>R&D</b> 'lead'",
);
expect(applyReplacementTags('{{v_u_0}}', replacements)).toBe(
encodeURIComponent("<b>R&D</b> 'lead'"),
);
});
});
describe('inherited object properties', () => {
it('treats a variable named after an Object prototype member as absent rather than its function', () => {
const replacements = buildCampaignBatchReplacements({
campaignVariables: [{ name: 'toString' }],
variables: {},
});
expect(applyReplacementTags('Hi {{v_t_0}}.', replacements)).toBe('Hi .');
});
it('leaves a prototype-named tag untouched instead of resolving it off Object.prototype', () => {
const replacements = buildCampaignBatchReplacements({
campaignVariables: [{ name: 'known' }],
variables: { known: 'yes' },
});
expect(
applyReplacementTags('{{constructor}} {{v_t_0}}', replacements),
).toBe('{{constructor}} yes');
});
});