From e7fa0ae883fd8d762864b7245a9d2f969a4ca2fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iv=C3=A1n=20Ovejero?= Date: Fri, 20 May 2022 21:28:05 +0200 Subject: [PATCH] test(core): Skip SMTP tests if service unavailable (#3293) --- .../test/integration/passwordReset.api.test.ts | 5 +++++ packages/cli/test/integration/shared/utils.ts | 16 ++++++++++++++++ packages/cli/test/integration/users.api.test.ts | 9 +++++++++ 3 files changed, 30 insertions(+) diff --git a/packages/cli/test/integration/passwordReset.api.test.ts b/packages/cli/test/integration/passwordReset.api.test.ts index faa7e96a42..8501394463 100644 --- a/packages/cli/test/integration/passwordReset.api.test.ts +++ b/packages/cli/test/integration/passwordReset.api.test.ts @@ -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); diff --git a/packages/cli/test/integration/shared/utils.ts b/packages/cli/test/integration/shared/utils.ts index 3086b96349..3d756061d3 100644 --- a/packages/cli/test/integration/shared/utils.ts +++ b/packages/cli/test/integration/shared/utils.ts @@ -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 // ---------------------------------- diff --git a/packages/cli/test/integration/users.api.test.ts b/packages/cli/test/integration/users.api.test.ts index 62e4899b12..6ac6d6136c 100644 --- a/packages/cli/test/integration/users.api.test.ts +++ b/packages/cli/test/integration/users.api.test.ts @@ -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 });