refactor(core): Move backend config to a separate package (no-changelog) (#9325)

This commit is contained in:
कारतोफ्फेलस्क्रिप्ट™
2024-07-05 11:43:27 +02:00
committed by GitHub
parent 1d5b9836ca
commit c7d4b471c4
48 changed files with 941 additions and 556 deletions

View File

@@ -1,41 +1,88 @@
import config from '@/config';
import { NodeMailer } from '@/UserManagement/email/NodeMailer';
import { UserManagementMailer } from '@/UserManagement/email/UserManagementMailer';
import type { GlobalConfig } from '@n8n/config';
import { mock } from 'jest-mock-extended';
import type { InviteEmailData, PasswordResetData } from '@/UserManagement/email/Interfaces';
import { NodeMailer } from '@/UserManagement/email/NodeMailer';
import { UserManagementMailer } from '@/UserManagement/email/UserManagementMailer';
import { mockInstance } from '@test/mocking';
describe('UserManagementMailer', () => {
describe('expect NodeMailer.verifyConnection', () => {
let mockInit: jest.SpyInstance<Promise<void>, []>;
let mockVerifyConnection: jest.SpyInstance<Promise<void>, []>;
const email = 'test@user.com';
const nodeMailer = mockInstance(NodeMailer);
const inviteEmailData = mock<InviteEmailData>({
email,
inviteAcceptUrl: 'https://accept.url',
});
const passwordResetData = mock<PasswordResetData>({
email,
passwordResetUrl: 'https://reset.url',
});
beforeAll(() => {
mockVerifyConnection = jest
.spyOn(NodeMailer.prototype, 'verifyConnection')
.mockImplementation(async () => {});
mockInit = jest.spyOn(NodeMailer.prototype, 'init').mockImplementation(async () => {});
beforeEach(() => {
jest.clearAllMocks();
nodeMailer.sendMail.mockResolvedValue({ emailSent: true });
});
describe('when SMTP is not configured', () => {
const config = mock<GlobalConfig>({
userManagement: {
emails: {
mode: '',
},
},
});
const userManagementMailer = new UserManagementMailer(config, mock(), mock(), mock());
it('should not setup email transport', async () => {
expect(userManagementMailer.isEmailSetUp).toBe(false);
expect(userManagementMailer.mailer).toBeUndefined();
});
afterAll(() => {
mockVerifyConnection.mockRestore();
mockInit.mockRestore();
it('should not send emails', async () => {
const result = await userManagementMailer.invite(inviteEmailData);
expect(result.emailSent).toBe(false);
expect(nodeMailer.sendMail).not.toHaveBeenCalled();
});
});
describe('when SMTP is configured', () => {
const config = mock<GlobalConfig>({
userManagement: {
emails: {
mode: 'smtp',
smtp: {
host: 'email.host',
},
},
},
});
const userManagementMailer = new UserManagementMailer(config, mock(), mock(), mock());
it('should setup email transport', async () => {
expect(userManagementMailer.isEmailSetUp).toBe(true);
expect(userManagementMailer.mailer).toEqual(nodeMailer);
});
test('not be called when SMTP not set up', async () => {
const userManagementMailer = new UserManagementMailer(mock(), mock(), mock());
// NodeMailer.verifyConnection gets called only explicitly
await expect(async () => await userManagementMailer.verifyConnection()).rejects.toThrow();
expect(NodeMailer.prototype.verifyConnection).toHaveBeenCalledTimes(0);
it('should send invitation emails', async () => {
const result = await userManagementMailer.invite(inviteEmailData);
expect(result.emailSent).toBe(true);
expect(nodeMailer.sendMail).toHaveBeenCalledWith({
body: expect.stringContaining(
`<a href="${inviteEmailData.inviteAcceptUrl}" target="_blank">`,
),
emailRecipients: email,
subject: 'You have been invited to n8n',
});
});
test('to be called when SMTP set up', async () => {
// host needs to be set, otherwise smtp is skipped
config.set('userManagement.emails.smtp.host', 'host');
config.set('userManagement.emails.mode', 'smtp');
const userManagementMailer = new UserManagementMailer(mock(), mock(), mock());
// NodeMailer.verifyConnection gets called only explicitly
expect(async () => await userManagementMailer.verifyConnection()).not.toThrow();
it('should send password reset emails', async () => {
const result = await userManagementMailer.passwordReset(passwordResetData);
expect(result.emailSent).toBe(true);
expect(nodeMailer.sendMail).toHaveBeenCalledWith({
body: expect.stringContaining(`<a href="${passwordResetData.passwordResetUrl}">`),
emailRecipients: email,
subject: 'n8n password reset',
});
});
});
});