test(core): Skip SMTP tests if service unavailable (#3293)

This commit is contained in:
Iván Ovejero 2022-05-20 21:28:05 +02:00 committed by GitHub
parent 29ddac30d3
commit e7fa0ae883
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 0 deletions

View file

@ -21,6 +21,7 @@ let app: express.Application;
let testDbName = '';
let globalOwnerRole: Role;
let globalMemberRole: Role;
let isSmtpAvailable = false;
beforeAll(async () => {
app = utils.initTestServer({ endpointGroups: ['passwordReset'], applyAuth: true });
@ -32,6 +33,8 @@ beforeAll(async () => {
utils.initTestTelemetry();
utils.initTestLogger();
isSmtpAvailable = await utils.isTestSmtpServiceAvailable();
});
beforeEach(async () => {
@ -50,6 +53,8 @@ afterAll(async () => {
test(
'POST /forgot-password should send password reset email',
async () => {
if (!isSmtpAvailable) utils.skipSmtpTest(expect);
const owner = await testDb.createUser({ globalRole: globalOwnerRole });
const authlessAgent = utils.createAgent(app);

View file

@ -26,6 +26,7 @@ import { credentialsController } from '../../../src/api/credentials.api';
import type { User } from '../../../src/databases/entities/User';
import type { EndpointGroup, SmtpTestAccount } from './types';
import type { N8nApp } from '../../../src/UserManagement/Interfaces';
import * as UserManagementMailer from '../../../src/UserManagement/email/UserManagementMailer';
/**
* Initialize a test server.
@ -229,6 +230,21 @@ export async function configureSmtp() {
config.set('userManagement.emails.smtp.auth.pass', pass);
}
export async function isTestSmtpServiceAvailable() {
try {
await configureSmtp();
await UserManagementMailer.getInstance();
return true;
} catch (_) {
return false;
}
}
export function skipSmtpTest(expect: jest.Expect) {
console.warn(`SMTP service unavailable - Skipping test ${expect.getState().currentTestName}`);
return;
}
// ----------------------------------
// misc
// ----------------------------------

View file

@ -27,6 +27,7 @@ let globalMemberRole: Role;
let globalOwnerRole: Role;
let workflowOwnerRole: Role;
let credentialOwnerRole: Role;
let isSmtpAvailable = false;
beforeAll(async () => {
app = utils.initTestServer({ endpointGroups: ['users'], applyAuth: true });
@ -47,6 +48,8 @@ beforeAll(async () => {
utils.initTestTelemetry();
utils.initTestLogger();
isSmtpAvailable = await utils.isTestSmtpServiceAvailable();
});
beforeEach(async () => {
@ -482,6 +485,8 @@ test('POST /users should fail if user management is disabled', async () => {
test(
'POST /users should email invites and create user shells but ignore existing',
async () => {
if (!isSmtpAvailable) utils.skipSmtpTest(expect);
const owner = await testDb.createUser({ globalRole: globalOwnerRole });
const member = await testDb.createUser({ globalRole: globalMemberRole });
const memberShell = await testDb.createUserShell(globalMemberRole);
@ -534,6 +539,8 @@ test(
test(
'POST /users should fail with invalid inputs',
async () => {
if (!isSmtpAvailable) utils.skipSmtpTest(expect);
const owner = await testDb.createUser({ globalRole: globalOwnerRole });
const authOwnerAgent = utils.createAgent(app, { auth: true, user: owner });
@ -563,6 +570,8 @@ test(
test(
'POST /users should ignore an empty payload',
async () => {
if (!isSmtpAvailable) utils.skipSmtpTest(expect);
const owner = await testDb.createUser({ globalRole: globalOwnerRole });
const authOwnerAgent = utils.createAgent(app, { auth: true, user: owner });