-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathmailpit.ts
More file actions
121 lines (96 loc) · 3.33 KB
/
Copy pathmailpit.ts
File metadata and controls
121 lines (96 loc) · 3.33 KB
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
import { URL_LIST } from "@data/url";
import { type APIRequestContext, expect } from "@playwright/test";
const mailpitUrl = process.env.MAILPITURL || "no mailpit url provided";
export class MailpitService {
readonly request: APIRequestContext;
constructor(request: APIRequestContext) {
this.request = request;
}
async getLastEmails(getEmailsLimit = 50) {
let latestEmails: any;
await expect
.poll(
async () => {
latestEmails = await this.request.get(
`${mailpitUrl}/api/v1/messages?limit=${getEmailsLimit}`,
);
return latestEmails;
},
{
message: `Could not found last ${getEmailsLimit}`,
intervals: [2000, 3000, 5000, 5000],
timeout: 15000,
},
)
.not.toBeUndefined();
const latestEmailsJson = await latestEmails!.json();
return latestEmailsJson;
}
async getEmailsFromTime(fromTime = 10000) {
const latestEmails = await this.getLastEmails();
latestEmails.messages = latestEmails.messages.filter((message: { Created: string }) => {
const timeMinusLastMs = Date.now() - fromTime;
const mailCreated = new Date(message.Created);
return mailCreated.getTime() > timeMinusLastMs;
});
return latestEmails;
}
async getEmailDetails(mailId: string) {
const emailDetails = await this.request.get(`${mailpitUrl}/api/v1/message/${mailId}`);
const emailDetailsJson = await emailDetails.json();
return emailDetailsJson;
}
async getEmailsForUser(userEmail: string) {
let userEmails: any[] = [];
await expect
.poll(
async () => {
const emails = await this.getEmailsFromTime();
userEmails = await emails.messages.filter((mails: { To: any[] }) =>
mails.To.map((recipientObj: { Address: any }) => `${recipientObj.Address}`).includes(
userEmail,
),
);
return userEmails.length;
},
{
message: `User: ${userEmail} messages were not found.`,
intervals: [2000, 3000, 5000, 5000],
timeout: 15000,
},
)
.toBeGreaterThanOrEqual(1);
return userEmails;
}
async checkDoesUserReceivedExportedData(userEmail: string, mailSubject: string) {
let confirmationMessageReceived = false;
await expect
.poll(
async () => {
const userEmails: any[] = await this.getEmailsForUser(userEmail);
for (const email of userEmails) {
if (email.Subject === mailSubject) {
confirmationMessageReceived = true;
break;
}
}
return confirmationMessageReceived;
},
{
message: `Message with subject: ${mailSubject} was not found`,
intervals: [2000, 3000, 5000, 5000],
timeout: 30000,
},
)
.toBe(true);
}
async generateResetPasswordUrl(userEmail: string) {
const tokenRegex = /token=([A-Za-z0-9]+(-[A-Za-z0-9]+)+)/;
const userEmails = await this.getEmailsForUser(userEmail);
const emailDetails = await this.getEmailDetails(userEmails[0].ID);
const emailHtmlFormat = tokenRegex.exec(emailDetails.HTML.toString());
const token = "&" + emailHtmlFormat![0];
const resetPasswordUrl = URL_LIST.resetPassword + userEmail + token;
return resetPasswordUrl;
}
}