Skip to content
Open
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
72 changes: 42 additions & 30 deletions src/context/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,49 +25,61 @@ export function emailProviderDefaults(
const { name } = updated;

if (apiKeyProviders.includes(name)) {
updated.credentials = {
api_key: `##${name.toUpperCase()}_API_KEY##`,
...(updated.credentials || {}),
};
if (typeof updated.credentials !== 'string') {
updated.credentials = {
api_key: `##${name.toUpperCase()}_API_KEY##`,
...(updated.credentials || {}),
};
}
}

if (name === 'smtp') {
// This is to mask smtp_user to '##SMTP_USER##'
if (updated.credentials && 'smtp_user' in updated.credentials) {
delete updated.credentials.smtp_user;
// If credentials is a keyword placeholder string (e.g. @@SMTP_CREDENTIALS@@), preserve it as-is.
// The `in` operator requires an object and would throw a TypeError on a string.
if (typeof updated.credentials !== 'string') {
// This is to mask smtp_user to '##SMTP_USER##'
if (updated.credentials && 'smtp_user' in updated.credentials) {
delete updated.credentials.smtp_user;
}
updated.credentials = {
smtp_host: '##SMTP_HOSTNAME##',
smtp_port: '##SMTP_PORT##',
smtp_user: '##SMTP_USER##',
smtp_pass: '##SMTP_PASS##',
...(updated.credentials || {}),
};
}
updated.credentials = {
smtp_host: '##SMTP_HOSTNAME##',
smtp_port: '##SMTP_PORT##',
smtp_user: '##SMTP_USER##',
smtp_pass: '##SMTP_PASS##',
...(updated.credentials || {}),
};
}

if (name === 'ses') {
updated.credentials = {
accessKeyId: '##SES_ACCESS_KEY_ID##',
secretAccessKey: '##SES_ACCESS_SECRET_KEY##',
region: '##SES_AWS_REGION##',
...(updated.credentials || {}),
};
if (typeof updated.credentials !== 'string') {
updated.credentials = {
accessKeyId: '##SES_ACCESS_KEY_ID##',
secretAccessKey: '##SES_ACCESS_SECRET_KEY##',
region: '##SES_AWS_REGION##',
...(updated.credentials || {}),
};
}
}

if (name === 'azure_cs') {
updated.credentials = {
connectionString: '##AZURE_CS_CONNECTION_KEY##',
...(updated.credentials || {}),
};
if (typeof updated.credentials !== 'string') {
updated.credentials = {
connectionString: '##AZURE_CS_CONNECTION_KEY##',
...(updated.credentials || {}),
};
}
}

if (name === 'ms365') {
updated.credentials = {
tenantId: '##MS365_TENANT_ID##',
clientId: '##MS365_CLIENT_ID##',
clientSecret: '##MS365_CLIENT_SECRET##',
...(updated.credentials || {}),
};
if (typeof updated.credentials !== 'string') {
updated.credentials = {
tenantId: '##MS365_TENANT_ID##',
clientId: '##MS365_CLIENT_ID##',
clientSecret: '##MS365_CLIENT_SECRET##',
...(updated.credentials || {}),
};
}
}

return updated;
Expand Down
40 changes: 40 additions & 0 deletions test/context/defaults.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,46 @@ describe('#context defaults', () => {
});
});

it('should preserve smtp credentials when it is a keyword placeholder string', async () => {
const emailProvider = {
name: 'smtp',
credentials: '@@SMTP_CREDENTIALS@@',
};

const result = emailProviderDefaults(emailProvider);

expect(result).to.deep.equal({
name: 'smtp',
credentials: '@@SMTP_CREDENTIALS@@',
});
});

it('should preserve ses credentials when it is a keyword placeholder string', async () => {
const result = emailProviderDefaults({ name: 'ses', credentials: '@@SES_CREDENTIALS@@' });
expect(result).to.deep.equal({ name: 'ses', credentials: '@@SES_CREDENTIALS@@' });
});

it('should preserve mailgun credentials when it is a keyword placeholder string', async () => {
const result = emailProviderDefaults({
name: 'mailgun',
credentials: '@@MAILGUN_CREDENTIALS@@',
});
expect(result).to.deep.equal({ name: 'mailgun', credentials: '@@MAILGUN_CREDENTIALS@@' });
});

it('should preserve azure_cs credentials when it is a keyword placeholder string', async () => {
const result = emailProviderDefaults({
name: 'azure_cs',
credentials: '@@AZURE_CREDENTIALS@@',
});
expect(result).to.deep.equal({ name: 'azure_cs', credentials: '@@AZURE_CREDENTIALS@@' });
});

it('should preserve ms365 credentials when it is a keyword placeholder string', async () => {
const result = emailProviderDefaults({ name: 'ms365', credentials: '@@MS365_CREDENTIALS@@' });
expect(result).to.deep.equal({ name: 'ms365', credentials: '@@MS365_CREDENTIALS@@' });
});

it('should set emailProvider defaults for smtp and remove existing smtp_user', async () => {
const emailProvider = {
name: 'smtp',
Expand Down