From de4ca3b0cc63b7a568810424fb3deeb34aa4b676 Mon Sep 17 00:00:00 2001 From: Michael Kret <88898367+michael-radency@users.noreply.github.com> Date: Mon, 12 Sep 2022 18:07:56 +0300 Subject: [PATCH] test(UM tests): add missing UM tests n8n-3648 (#4077) * :zap: added test for users reinvite --- .../cli/test/integration/users.api.test.ts | 51 +++++++++++++++---- .../nodes/Function/Function.node.ts | 2 +- .../nodes/FunctionItem/FunctionItem.node.ts | 2 +- 3 files changed, 42 insertions(+), 13 deletions(-) diff --git a/packages/cli/test/integration/users.api.test.ts b/packages/cli/test/integration/users.api.test.ts index 6207264618..2d1143a7b8 100644 --- a/packages/cli/test/integration/users.api.test.ts +++ b/packages/cli/test/integration/users.api.test.ts @@ -20,6 +20,9 @@ import * as utils from './shared/utils'; import * as testDb from './shared/testDb'; import { compareHash } from '../../src/UserManagement/UserManagementHelper'; +import * as UserManagementMailer from '../../src/UserManagement/email/UserManagementMailer'; +import { NodeMailer } from '../../src/UserManagement/email/NodeMailer'; + jest.mock('../../src/telemetry'); jest.mock('../../src/UserManagement/email/NodeMailer'); @@ -542,19 +545,45 @@ test('POST /users should ignore an empty payload', async () => { expect(users.length).toBe(1); }); -// TODO: /users/:id/reinvite route tests missing +test('POST /users/:id/reinvite should send reinvite, but fail if user already accepted invite', async () => { + const owner = await testDb.createUser({ globalRole: globalOwnerRole }); + const authOwnerAgent = utils.createAgent(app, { auth: true, user: owner }); -// TODO: UserManagementMailer is a singleton - cannot reinstantiate with wrong creds -// test('POST /users should error for wrong SMTP config', async () => { -// const owner = await Db.collections.User.findOneOrFail(); -// const authOwnerAgent = utils.createAgent(app, { auth: true, user: owner }); + config.set('userManagement.emails.mode', 'smtp'); -// config.set('userManagement.emails.mode', 'smtp'); -// config.set('userManagement.emails.smtp.host', 'XYZ'); // break SMTP config + // those configs are needed to make sure the reinvite email is sent,because of this check isEmailSetUp() + config.set('userManagement.emails.smtp.host', 'host'); + config.set('userManagement.emails.smtp.auth.user', 'user'); + config.set('userManagement.emails.smtp.auth.pass', 'pass'); -// const payload = TEST_EMAILS_TO_CREATE_USER_SHELLS.map((e) => ({ email: e })); + const email = randomEmail(); + const payload = [{ email }]; + const response = await authOwnerAgent.post('/users').send(payload); -// const response = await authOwnerAgent.post('/users').send(payload); + expect(response.statusCode).toBe(200); -// expect(response.statusCode).toBe(500); -// }); + const { data } = response.body; + const invitedUserId = data[0].user.id; + const reinviteResponse = await authOwnerAgent.post(`/users/${invitedUserId}/reinvite`); + + expect(reinviteResponse.statusCode).toBe(200); + + const member = await testDb.createUser({ globalRole: globalMemberRole }); + const reinviteMemberResponse = await authOwnerAgent.post(`/users/${member.id}/reinvite`); + + expect(reinviteMemberResponse.statusCode).toBe(400); +}); + +test('UserManagementMailer expect NodeMailer.verifyConnection have been called', async () => { + jest.spyOn(NodeMailer.prototype, 'verifyConnection').mockImplementation(async () => {}); + + // NodeMailer.verifyConnection called 1 time + const userManagementMailer = UserManagementMailer.getInstance(); + // NodeMailer.verifyConnection called 2 time + (await userManagementMailer).verifyConnection(); + + expect(NodeMailer.prototype.verifyConnection).toHaveBeenCalledTimes(2); + + // @ts-ignore + NodeMailer.prototype.verifyConnection.mockRestore(); +}); diff --git a/packages/nodes-base/nodes/Function/Function.node.ts b/packages/nodes-base/nodes/Function/Function.node.ts index ea2895a1e1..2b595fe363 100644 --- a/packages/nodes-base/nodes/Function/Function.node.ts +++ b/packages/nodes-base/nodes/Function/Function.node.ts @@ -95,7 +95,7 @@ return items;`, if (item?.binary && item?.index !== undefined && item?.index !== null) { for (const binaryPropertyName of Object.keys(item.binary)) { item.binary[binaryPropertyName].data = ( - await this.helpers.getBinaryDataBuffer(item.index, binaryPropertyName) + await this.helpers.getBinaryDataBuffer(item.index as number, binaryPropertyName) )?.toString('base64'); } } diff --git a/packages/nodes-base/nodes/FunctionItem/FunctionItem.node.ts b/packages/nodes-base/nodes/FunctionItem/FunctionItem.node.ts index 1d5b7f942d..184378870a 100644 --- a/packages/nodes-base/nodes/FunctionItem/FunctionItem.node.ts +++ b/packages/nodes-base/nodes/FunctionItem/FunctionItem.node.ts @@ -113,7 +113,7 @@ return item;`, if (item?.binary && item?.index !== undefined && item?.index !== null) { for (const binaryPropertyName of Object.keys(item.binary)) { item.binary[binaryPropertyName].data = ( - await this.helpers.getBinaryDataBuffer(item.index, binaryPropertyName) + await this.helpers.getBinaryDataBuffer(item.index as number, binaryPropertyName) )?.toString('base64'); } }