-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Alerting] set correct parameter for unauthented email action (#63086) (
#63412) PR #60839 added support for unauthenticated emails, but didn't actually do enough to make it work. This PR completes that support, and adds some tests. You can do manual testing now with [maildev](http://maildev.github.io/maildev/).
- Loading branch information
Showing
4 changed files
with
299 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
175 changes: 175 additions & 0 deletions
175
x-pack/plugins/actions/server/builtin_action_types/lib/send_email.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
jest.mock('nodemailer', () => ({ | ||
createTransport: jest.fn(), | ||
})); | ||
|
||
import { Logger } from '../../../../../../src/core/server'; | ||
import { sendEmail } from './send_email'; | ||
import { loggingServiceMock } from '../../../../../../src/core/server/mocks'; | ||
import nodemailer from 'nodemailer'; | ||
|
||
const createTransportMock = nodemailer.createTransport as jest.Mock; | ||
const sendMailMockResult = { result: 'does not matter' }; | ||
const sendMailMock = jest.fn(); | ||
|
||
const mockLogger = loggingServiceMock.create().get() as jest.Mocked<Logger>; | ||
|
||
describe('send_email module', () => { | ||
beforeEach(() => { | ||
jest.resetAllMocks(); | ||
createTransportMock.mockReturnValue({ sendMail: sendMailMock }); | ||
sendMailMock.mockResolvedValue(sendMailMockResult); | ||
}); | ||
|
||
test('handles authenticated email using service', async () => { | ||
const sendEmailOptions = getSendEmailOptions(); | ||
const result = await sendEmail(mockLogger, sendEmailOptions); | ||
expect(result).toBe(sendMailMockResult); | ||
expect(createTransportMock.mock.calls[0]).toMatchInlineSnapshot(` | ||
Array [ | ||
Object { | ||
"auth": Object { | ||
"pass": "changeme", | ||
"user": "elastic", | ||
}, | ||
"service": "whatever", | ||
}, | ||
] | ||
`); | ||
expect(sendMailMock.mock.calls[0]).toMatchInlineSnapshot(` | ||
Array [ | ||
Object { | ||
"bcc": Array [], | ||
"cc": Array [ | ||
"bob@example.com", | ||
"robert@example.com", | ||
], | ||
"from": "fred@example.com", | ||
"html": "<p>a message</p> | ||
", | ||
"subject": "a subject", | ||
"text": "a message", | ||
"to": Array [ | ||
"jim@example.com", | ||
], | ||
}, | ||
] | ||
`); | ||
}); | ||
|
||
test('handles unauthenticated email using not secure host/port', async () => { | ||
const sendEmailOptions = getSendEmailOptions(); | ||
delete sendEmailOptions.transport.service; | ||
delete sendEmailOptions.transport.user; | ||
delete sendEmailOptions.transport.password; | ||
sendEmailOptions.transport.host = 'example.com'; | ||
sendEmailOptions.transport.port = 1025; | ||
const result = await sendEmail(mockLogger, sendEmailOptions); | ||
expect(result).toBe(sendMailMockResult); | ||
expect(createTransportMock.mock.calls[0]).toMatchInlineSnapshot(` | ||
Array [ | ||
Object { | ||
"host": "example.com", | ||
"port": 1025, | ||
"secure": false, | ||
"tls": Object { | ||
"rejectUnauthorized": false, | ||
}, | ||
}, | ||
] | ||
`); | ||
expect(sendMailMock.mock.calls[0]).toMatchInlineSnapshot(` | ||
Array [ | ||
Object { | ||
"bcc": Array [], | ||
"cc": Array [ | ||
"bob@example.com", | ||
"robert@example.com", | ||
], | ||
"from": "fred@example.com", | ||
"html": "<p>a message</p> | ||
", | ||
"subject": "a subject", | ||
"text": "a message", | ||
"to": Array [ | ||
"jim@example.com", | ||
], | ||
}, | ||
] | ||
`); | ||
}); | ||
|
||
test('handles unauthenticated email using secure host/port', async () => { | ||
const sendEmailOptions = getSendEmailOptions(); | ||
delete sendEmailOptions.transport.service; | ||
delete sendEmailOptions.transport.user; | ||
delete sendEmailOptions.transport.password; | ||
sendEmailOptions.transport.host = 'example.com'; | ||
sendEmailOptions.transport.port = 1025; | ||
sendEmailOptions.transport.secure = true; | ||
const result = await sendEmail(mockLogger, sendEmailOptions); | ||
expect(result).toBe(sendMailMockResult); | ||
expect(createTransportMock.mock.calls[0]).toMatchInlineSnapshot(` | ||
Array [ | ||
Object { | ||
"host": "example.com", | ||
"port": 1025, | ||
"secure": true, | ||
}, | ||
] | ||
`); | ||
expect(sendMailMock.mock.calls[0]).toMatchInlineSnapshot(` | ||
Array [ | ||
Object { | ||
"bcc": Array [], | ||
"cc": Array [ | ||
"bob@example.com", | ||
"robert@example.com", | ||
], | ||
"from": "fred@example.com", | ||
"html": "<p>a message</p> | ||
", | ||
"subject": "a subject", | ||
"text": "a message", | ||
"to": Array [ | ||
"jim@example.com", | ||
], | ||
}, | ||
] | ||
`); | ||
}); | ||
|
||
test('passes nodemailer exceptions to caller', async () => { | ||
const sendEmailOptions = getSendEmailOptions(); | ||
|
||
sendMailMock.mockReset(); | ||
sendMailMock.mockRejectedValue(new Error('wops')); | ||
|
||
await expect(sendEmail(mockLogger, sendEmailOptions)).rejects.toThrow('wops'); | ||
}); | ||
}); | ||
|
||
function getSendEmailOptions(): any { | ||
return { | ||
content: { | ||
message: 'a message', | ||
subject: 'a subject', | ||
}, | ||
routing: { | ||
from: 'fred@example.com', | ||
to: ['jim@example.com'], | ||
cc: ['bob@example.com', 'robert@example.com'], | ||
bcc: [], | ||
}, | ||
transport: { | ||
service: 'whatever', | ||
user: 'elastic', | ||
password: 'changeme', | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters