-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopkiller.test.js
213 lines (162 loc) · 6.06 KB
/
popkiller.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/* eslint-env jest */
'use strict';
const config = require('./config');
const server = require('./popkiller');
const nodemailer = require('nodemailer');
const BJSON = require('buffer-json');
const rabbitMock = require('./mocks/rabbit');
const message = require('./mocks/messages').simpleMessage;
jest.mock('amqplib');
const transport = nodemailer.createTransport({
host: config.server.host,
port: config.server.port,
tls: {
rejectUnauthorized: false
}
}, {
from: '[email protected]'
});
describe('The SMTP server', () => {
beforeAll(() => server.start());
afterAll(done => {
server.stop(done);
});
beforeEach(() => {
config.routes = {
'example.org': 'somequeue'
};
jest.clearAllMocks();
});
it('should accept messages sent to it', () => {
return transport.sendMail(message);
});
it('should reject authentication attempts', () => {
const authenticatedMessage = Object.assign({}, message, {
auth: {
user: 'user',
pass: 'password1'
}
});
expect.assertions(1);
return transport.sendMail(authenticatedMessage).catch(e => expect(e.toString()).toMatch('No auth'));
});
it('should add messages to the queue', async () => {
await transport.sendMail(message);
expect(rabbitMock.channel.sendToQueue.mock.calls.length).toBe(1);
});
it('should use a buffer to send the message to rabbit', async () => {
await transport.sendMail(message);
const buffer = rabbitMock.channel.sendToQueue.mock.calls[0][1];
expect(Buffer.isBuffer(buffer)).toBe(true);
});
it('should serialize messages as a json dict', async () => {
await transport.sendMail(message);
const queueBuffer = rabbitMock.channel.sendToQueue.mock.calls[0][1];
const queuedMessage = JSON.parse(queueBuffer.toString());
expect(queuedMessage).toEqual(
expect.objectContaining({
subject: 'test message',
text: 'simple text message\n'
})
);
expect(queuedMessage.from.value[0].address).toEqual('[email protected]');
expect(queuedMessage.to.value[0].address).toEqual('[email protected]');
});
it('should include attachments in message', async () => {
const messageWithAttachment = Object.assign({}, message, {
attachments: [{
filename: 'hello.txt',
content: 'Hello world!'
}]
});
await transport.sendMail(messageWithAttachment);
const queueBuffer = rabbitMock.channel.sendToQueue.mock.calls[0][1];
const queuedMessage = BJSON.parse(queueBuffer.toString());
expect(queuedMessage.attachments.length).toEqual(1);
expect(queuedMessage.attachments[0].filename).toEqual('hello.txt');
expect(queuedMessage.attachments[0].content.toString()).toEqual('Hello world!');
});
it('should route messages based on destination domain', async () => {
config.routes = {
'example.org': 'example-org',
'example.com': null
};
const exampleOrgMessage = message;
await transport.sendMail(exampleOrgMessage);
let queue = rabbitMock.channel.sendToQueue.mock.calls[0][0];
expect(queue).toBe('example-org');
const exampleComMessage = Object.assign({}, message, {
to: '[email protected]'
});
await transport.sendMail(exampleComMessage);
queue = rabbitMock.channel.sendToQueue.mock.calls[1][0];
expect(queue).toBe('example.com');
});
it('should route messages based on destination address', async () => {
config.routes = {
'example.org': 'domain-queue',
'[email protected]': 'full-address-queue'
};
await transport.sendMail(message);
let queue = rabbitMock.channel.sendToQueue.mock.calls.pop()[0];
expect(queue).toBe('domain-queue');
const fullAddressQueueMessage = Object.assign({}, message, {
to: '[email protected]'
});
await transport.sendMail(fullAddressQueueMessage);
queue = rabbitMock.channel.sendToQueue.mock.calls.pop()[0];
expect(queue).toBe('full-address-queue');
});
it('should reject messages for unknown domains', async () => {
config.routes = {
'example.com': 'example-com'
};
expect.assertions(1);
try {
await transport.sendMail(message);
} catch (e) {
expect(e.toString()).toMatch('Not allowed');
}
});
it('should add a tags property to the message', async () => {
await transport.sendMail(message);
const queueBuffer = rabbitMock.channel.sendToQueue.mock.calls[0][1];
const queuedMessage = BJSON.parse(queueBuffer.toString());
expect(queuedMessage.tags).toEqual([]);
});
it('should extract tags from destinatary', async () => {
const singleTagMessage = Object.assign({}, message, {
to: '[email protected]'
});
await transport.sendMail(singleTagMessage);
let queueBuffer = rabbitMock.channel.sendToQueue.mock.calls[0][1];
let queuedMessage = BJSON.parse(queueBuffer.toString());
expect(queuedMessage.tags).toEqual(['tag1']);
const multiTagMessage = Object.assign({}, message, {
to: '[email protected]'
});
await transport.sendMail(multiTagMessage);
queueBuffer = rabbitMock.channel.sendToQueue.mock.calls[1][1];
queuedMessage = BJSON.parse(queueBuffer.toString());
expect(queuedMessage.tags).toEqual(['tag1', 'tag2', 'tag3']);
});
it('should not consider tags for routing', async () => {
config.routes['[email protected]'] = 'anotherqueue';
await transport.sendMail(message);
const noTagsQueue = rabbitMock.channel.sendToQueue.mock.calls.pop()[0];
const singleTagMessage = Object.assign({}, message, {
to: '[email protected]'
});
await transport.sendMail(singleTagMessage);
const singleTagQueue = rabbitMock.channel.sendToQueue.mock.calls.pop()[0];
expect(noTagsQueue).toEqual(singleTagQueue);
});
it('should close the channel and connection on shutdown', done => {
expect(rabbitMock.channel.close.mock.calls.length).toEqual(0);
server.stop(() => {
expect(rabbitMock.channel.close.mock.calls.length).toEqual(1);
expect(rabbitMock.connection.close.mock.calls.length).toEqual(1);
done();
});
});
});