-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathindex.js
286 lines (244 loc) Β· 7.82 KB
/
index.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
const mhApiUrl = (path) => {
const envValue = Cypress.env("mailHogUrl");
const basePath = envValue ? envValue : Cypress.config("mailHogUrl");
return new URL(`api${path}`, basePath).href;
};
let mhAuth = Cypress.env("mailHogAuth") || "";
if (Cypress.env("mailHogUsername") && Cypress.env("mailHogPassword")) {
mhAuth = {
user: Cypress.env("mailHogUsername"),
pass: Cypress.env("mailHogPassword"),
};
}
/**
* Gets unfiltered emails from mailhog.
* @param {number} limit The maximum number of emails to get.
* @returns {Promise<any>} The emails.
*/
const getMessages = (limit) => {
return cy
.request({
method: "GET",
url: mhApiUrl(`/v2/messages?limit=${encodeURIComponent(limit)}`),
auth: mhAuth,
log: false,
})
.then((response) => {
if (typeof response.body === "string") {
return JSON.parse(response.body);
} else {
return response.body;
}
})
.then((parsed) => parsed.items);
};
/**
* Gets emails on mailhog that match the specified search query.
* @param {'from' | 'to' | 'containing'} kind The search kind.
* @param {string} query The search query.
* @param {number} limit The maximum number of emails to get.
* @returns {Promise<any>} The emails.
*/
const searchMessages = (kind, query, limit) => {
return cy
.request({
method: "GET",
url: mhApiUrl(`/v2/search?kind=${encodeURIComponent(kind)}&query=${encodeURIComponent(query)}&limit=${encodeURIComponent(limit)}`),
auth: mhAuth,
log: false,
})
.then((response) => {
if (typeof response.body === "string") {
return JSON.parse(response.body);
} else {
return response.body;
}
})
.then((parsed) => parsed.items);
};
/**
* Fetches messages from mailhog with retryability.
* @param {(limit: number) => Promise<any>} fetcher The function to fetch the emails.
* @param {(mails: any) => any} filter The filter to apply to the feteched emails.
* @param {number} limit The maximum number of emails to fetch.
* @param {{timeout?: number}} options The request options.
* @returns {Promise<any>} The emails.
*/
const retryFetchMessages = (fetcher, filter, limit, options = {}) => {
const timeout =
options.timeout || Cypress.config("defaultCommandTimeout") || 4000;
let timedout = false;
setTimeout(() => {
timedout = true;
}, timeout);
const filteredMessages = (limit) => fetcher(limit).then(filter);
const resolve = () => {
if (timedout) {
return filteredMessages(limit);
}
return filteredMessages(limit).then((messages) => {
return cy.verifyUpcomingAssertions(messages, options, {
onRetry: resolve,
});
});
};
return resolve();
};
Cypress.Commands.add("mhGetJimMode", () => {
return cy
.request({
method: "GET",
url: mhApiUrl("/v2/jim"),
failOnStatusCode: false,
auth: mhAuth,
})
.then((response) => {
return cy.wrap(response.status === 200);
});
});
Cypress.Commands.add("mhSetJimMode", (enabled) => {
return cy.request({
method: enabled ? "POST" : "DELETE",
url: mhApiUrl("/v2/jim"),
failOnStatusCode: false,
auth: mhAuth,
});
});
/** Mail Collection */
Cypress.Commands.add("mhDeleteAll", (options = {}) => {
return cy.request({
method: "DELETE",
url: mhApiUrl("/v1/messages"),
auth: mhAuth,
timeout: options.timeout || Cypress.config('responseTimeout') || 30000,
});
});
Cypress.Commands.add("mhGetAllMails", (limit = 50, options = {}) => {
const filter = (mails) => mails;
return retryFetchMessages(getMessages, filter, limit, options);
});
Cypress.Commands.add("mhFirst", { prevSubject: true }, (mails) => {
return Array.isArray(mails) && mails.length > 0 ? mails[0] : mails;
});
Cypress.Commands.add(
"mhGetMailsBySubject",
(subject, limit = 50, options = {}) => {
const filter = (mails) =>
mails.filter((mail) => mail.Content.Headers.Subject[0] === subject);
return retryFetchMessages(getMessages, filter, limit, options);
}
);
Cypress.Commands.add(
"mhGetMailsByRecipient",
(recipient, limit = 50, options = {}) => {
const filter = (mails) => {
return mails.filter((mail) =>
mail.To.map(
(recipientObj) => `${recipientObj.Mailbox}@${recipientObj.Domain}`
).includes(recipient)
);
};
return retryFetchMessages(getMessages, filter, limit, options);
}
);
Cypress.Commands.add("mhGetMailsBySender", (from, limit = 50, options = {}) => {
const filter = (mails) => mails.filter((mail) => mail.Raw.From === from);
return retryFetchMessages(getMessages, filter, limit, options);
});
Cypress.Commands.add("mhSearchMails", (kind, query, limit = 50, options = {}) => {
const filter = (mails) => mails;
const fetcher = limit => searchMessages(kind, query, limit);
return retryFetchMessages(fetcher, filter, limit, options);
});
/** Filters on Mail Collections */
Cypress.Commands.add(
"mhFilterBySubject",
{ prevSubject: true },
(messages, subject) => {
return messages.filter(
(mail) => mail.Content.Headers.Subject[0] === subject
);
}
);
Cypress.Commands.add(
"mhFilterByRecipient",
{ prevSubject: true },
(messages, recipient) => {
return messages.filter((mail) =>
mail.To.map(
(recipientObj) => `${recipientObj.Mailbox}@${recipientObj.Domain}`
).includes(recipient)
);
}
);
Cypress.Commands.add(
"mhFilterBySender",
{ prevSubject: true },
(messages, from) => {
return messages.filter((mail) => mail.Raw.From === from);
}
);
/** Single Mail Commands and Assertions */
Cypress.Commands.add("mhGetSubject", { prevSubject: true }, (mail) => {
return cy.wrap(mail.Content.Headers).then((headers) => headers.Subject[0]);
});
Cypress.Commands.add("mhGetBody", { prevSubject: true }, (mail) => {
return cy.wrap(mail.Content).its("Body");
});
Cypress.Commands.add("mhGetSender", { prevSubject: true }, (mail) => {
return cy.wrap(mail.Raw).its("From");
});
Cypress.Commands.add("mhGetRecipients", { prevSubject: true }, (mail) => {
return cy
.wrap(mail)
.then((mail) =>
mail.To.map(
(recipientObj) => `${recipientObj.Mailbox}@${recipientObj.Domain}`
)
);
});
/** Mail Collection Assertions */
Cypress.Commands.add("mhHasMailWithSubject", (subject) => {
cy.mhGetMailsBySubject(subject).should("not.have.length", 0);
});
Cypress.Commands.add("mhHasMailFrom", (from) => {
cy.mhGetMailsBySender(from).should("not.have.length", 0);
});
Cypress.Commands.add("mhHasMailTo", (recipient) => {
cy.mhGetMailsByRecipient(recipient).should("not.have.length", 0);
});
/** Helpers */
Cypress.Commands.add("mhWaitForMails", (moreMailsThen = 0) => {
cy.mhGetAllMails().should("to.have.length.greaterThan", moreMailsThen);
});
/** Attachments */
Cypress.Commands.add("mhGetAttachments", { prevSubject: true }, (mail) => {
const attachments = [];
// search through mime parts to find attachments
if (Array.isArray(mail.MIME?.Parts)) {
for (const mimePart of mail.MIME?.Parts) {
// content disposition tells us if this part represents an attachment
// sample: Content-Disposition: ["attachment; filename=sample.pdf"]
if (
mimePart.Headers &&
mimePart.Headers["Content-Disposition"] &&
mimePart.Headers["Content-Disposition"][0]
) {
const contentDisposition = mimePart.Headers["Content-Disposition"][0];
if (contentDisposition) {
const dispositionTokens = contentDisposition
.split(";")
.map((token) => token.trim());
if (dispositionTokens.includes("attachment")) {
const fileNameToken = dispositionTokens.find((token) =>
token.startsWith("filename=")
);
const fileName = fileNameToken.replace("filename=", "");
attachments.push(fileName);
}
}
}
}
}
return cy.wrap(attachments);
});