external-services-automation
Version:
External services automation library for Playwright and Cucumber
120 lines (119 loc) • 4.14 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.GuerrillaMailClient = exports.email = void 0;
const axios_1 = __importDefault(require("axios"));
const axios_cookiejar_support_1 = require("axios-cookiejar-support");
const tough_cookie_1 = require("tough-cookie");
class GuerrillaMailClient {
constructor() {
this.API_URL = 'https://api.guerrillamail.com/ajax.php';
this.ip = '127.0.0.1';
this.agent = encodeURIComponent('qa-automation-utils/1.0');
this.seq = 0;
this.emailAddress = '';
const jar = new tough_cookie_1.CookieJar();
this.client = (0, axios_cookiejar_support_1.wrapper)(axios_1.default.create({
jar,
withCredentials: true,
headers: {
'User-Agent': 'qa-automation-utils/1.0',
},
}));
}
async createMail(alias) {
if (alias) {
const { data } = await this.client.get(this.API_URL, {
params: {
f: 'set_email_user',
email_user: alias,
ip: this.ip,
agent: this.agent,
},
});
this.emailAddress = data.email_addr;
}
else {
const { data } = await this.client.get(this.API_URL, {
params: {
f: 'get_email_address',
ip: this.ip,
agent: this.agent,
},
});
this.emailAddress = data.email_addr;
}
return this.emailAddress;
}
async readMailBySubject(subjectRegex, timeoutMs = 60000, pollMs = 5000) {
const start = Date.now();
while (Date.now() - start < timeoutMs) {
const { data } = await this.client.get(this.API_URL, {
params: {
f: 'check_email',
seq: this.seq,
ip: this.ip,
agent: this.agent,
},
});
this.seq = data.seq ?? this.seq;
const list = data.list ?? [];
const matches = list.filter(m => subjectRegex.test(m.mail_subject));
if (matches.length > 0) {
matches.sort((a, b) => b.mail_timestamp - a.mail_timestamp);
const mostRecent = matches[0];
const full = await this.fetchEmail(mostRecent.mail_id);
return full;
}
await new Promise(r => setTimeout(r, pollMs));
}
throw new Error(`Timeout (${timeoutMs} ms) waiting for an email with subject matching ${subjectRegex}`);
}
async deleteMail(mailId) {
await this.client.get(this.API_URL, {
params: {
f: 'del_email',
email_ids: `[${mailId}]`,
ip: this.ip,
agent: this.agent,
},
});
}
async fetchEmail(mailId) {
const { data } = await this.client.get(this.API_URL, {
params: {
f: 'fetch_email',
email_id: mailId,
ip: this.ip,
agent: this.agent,
},
});
return data;
}
static createIsolatedClient() {
return new GuerrillaMailClient();
}
async clearInbox() {
const { data } = await this.client.get(this.API_URL, {
params: {
f: 'check_email',
seq: this.seq,
ip: this.ip,
agent: this.agent,
},
});
const list = data.list ?? [];
for (const email of list) {
try {
await this.deleteMail(email.mail_id);
}
catch (error) {
console.warn(`Could not delete email ${email.mail_id}:`, error);
}
}
}
}
exports.GuerrillaMailClient = GuerrillaMailClient;
exports.email = new GuerrillaMailClient();