e2e-mailbox
Version:
E2E test your email notification system using GuerrillaMail API.
150 lines (149 loc) • 6.74 kB
JavaScript
import axios from 'axios';
import MailboxService from './mailboxService';
import { simpleParser } from 'mailparser';
class DeveloperMailService extends MailboxService {
constructor() {
super(...arguments);
this.API_URL = 'https://www.developermail.com/api/v1';
this.PROVIDER = 'DEVELOPER';
this.token = '';
this.mailboxName = '';
}
/**
* Send request to DeveloperMail API.
* @param data
* @param method
* @param endpoint
* @returns AxiosResponse on success, undefined on failure.
*/
async sendRequest(data, method, endpoint) {
try {
// Set Authentication header for DeveloperMail
const headers = !!this.token ? {
'X-MailboxToken': this.token,
'Content-Type': 'application/json'
} : {};
const payloadType = method === 'GET' ? 'params' : 'data';
return axios(`${this.API_URL}${endpoint}`, {
[payloadType]: data, method, headers
});
}
catch (error) {
return;
}
}
/**
* Convert Mime-Type response from DeveloperMail to EmailResponse
* @param message
* @param mailId
* @returns generated EmailResponse
*/
static async convertMimeToEmailResponse(message, mailId) {
const parsedMessage = await simpleParser(message);
const messageFrom = !!parsedMessage.from ? parsedMessage.from.text : '';
const messageDate = parsedMessage.date ? `${parsedMessage.date.getTime()}` : '';
const messageSubject = parsedMessage.subject || '';
const messageBody = parsedMessage.html || '';
// v1.0 read model was tied to GuerrillaMail's response type, for
// compatibility-sake we will convert DeveloperMail to the same type.
return {
mail_id: mailId,
mail_from: messageFrom,
mail_timestamp: messageDate,
mail_subject: messageSubject,
mail_excerpt: '',
mail_body: messageBody
};
}
/**
* Initialize a session and set the client with an email address. If the session already exists,
* then it will return the email address details of the existing session. If a new session needs to be created, then it
* will first check for the SUBSCR cookie to create a session for a subscribed address, otherwise it will create new email
* address randomly.
* @returns email address
*/
async createEmailAddress() {
const response = await this.sendRequest({}, 'PUT', '/mailbox');
if (!response)
throw new Error("Could not create email address.");
const creationResponse = response.data;
// Ensure the DeveloperMail response is valid.
if (!(creationResponse === null || creationResponse === void 0 ? void 0 : creationResponse.result))
throw new Error("Could not create email address.");
const EMAIL_DOMAIN = 'developermail.com';
this.token = creationResponse.result.token;
this.mailboxName = creationResponse.result.name;
return `${creationResponse.result.name}@${EMAIL_DOMAIN}`;
}
/**
* Get the current list of emails from the email inbox.
* @returns Array of emails
*/
async fetchEmailList() {
const emailList = [];
// Fetch list of message IDs present in the mailbox
const getMessageIdsResponse = await this.sendRequest({}, 'GET', `/mailbox/${this.mailboxName}`);
if (!getMessageIdsResponse) {
return emailList;
}
const emailListResponse = getMessageIdsResponse.data;
// Fetch list of messages from the IDs gathered in getMessageIdsResponse
const getMessagesResponse = await this.sendRequest(emailListResponse.result, 'POST', `/mailbox/${this.mailboxName}/messages`);
if (!getMessagesResponse) {
return emailList;
}
const { result } = getMessagesResponse.data;
if (!result)
return emailList;
// Response value comes as Mime 1.0, we must parse this to conform with our
// read model.
for (const message of result) {
const email = await DeveloperMailService.convertMimeToEmailResponse(message.value, message.key);
emailList.push(email);
}
return emailList;
}
/**
* Forget the current email address. This will not stop the session, the existing session will be maintained.
* A subsequent call to get_email_address will fetch a new email address or the client can call set_email_user
* to set a new address. Typically, a user would want to set a new address manually after clicking the
* ‘forget me’ button.
* @returns True on success, false on failure
*/
async forgetEmailAddress() {
var _a;
const response = await this.sendRequest({}, 'DELETE', `/mailbox/${this.mailboxName}`);
return !!((_a = response === null || response === void 0 ? void 0 : response.data) === null || _a === void 0 ? void 0 : _a.result);
}
/**
* Delete a specific email by ID.
* @param emailId
* @returns true on success, false on failure
*/
async deleteEmailById(emailId) {
var _a, _b;
const response = await this.sendRequest({}, 'DELETE', `/mailbox/${this.mailboxName}/messages/${emailId}`);
return (_b = !!((_a = response === null || response === void 0 ? void 0 : response.data) === null || _a === void 0 ? void 0 : _a.result)) !== null && _b !== void 0 ? _b : false;
}
/**
* Get the contents of an email. All HTML in the body of the email is filtered.
* Eg, Javascript, applets, iframes, etc is removed. Subject and email excerpt are escaped using HTML Entities.
* Only emails owned by the current session id can be fetched.
* @param emailId
* @returns
*/
async fetchEmailById(emailId) {
const response = await this.sendRequest({}, 'GET', `/mailbox/${this.mailboxName}/messages/${emailId}`);
if (!response)
throw new Error("Could not find email with id: " + emailId);
const responseData = response.data.result;
return DeveloperMailService.convertMimeToEmailResponse(responseData, emailId);
}
async sendSelfMail(subject, body) {
var _a;
const payload = { subject, body, isHtml: true };
const response = await this.sendRequest(payload, 'PUT', `/mailbox/${this.mailboxName}/messages`);
return !!((_a = response === null || response === void 0 ? void 0 : response.data) === null || _a === void 0 ? void 0 : _a.result);
}
}
export default DeveloperMailService;