package.cypress-utils.utils.email.js Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of neeto-commons-frontend Show documentation
Show all versions of neeto-commons-frontend Show documentation
A package encapsulating common code across neeto projects including initializers, utility functions, common components and hooks and so on.
import _defineProperty from "@babel/runtime/helpers/defineProperty";
var hostname = "api.fastmail.com";
var username = Cypress.env("FASTMAIL_USERNAME") || "[email protected]";
var token = Cypress.env("FASTMAIL_TOKEN");
var authUrl = "https://".concat(hostname, "/.well-known/jmap");
var headers = {
"Content-Type": "application/json",
Authorization: "Bearer ".concat(token)
};
var getSession = function getSession() {
return cy.request({
url: authUrl,
method: "GET",
headers: headers
});
};
var getDraftId = function getDraftId(_ref) {
var apiUrl = _ref.apiUrl,
accountId = _ref.accountId;
cy.request({
url: apiUrl,
method: "POST",
headers: headers,
body: JSON.stringify({
using: ["urn:ietf:params:jmap:core", "urn:ietf:params:jmap:mail"],
methodCalls: [["Mailbox/query", {
accountId: accountId,
filter: {
name: "Drafts"
}
}, "a"]]
})
}).then(function (_ref2) {
var data = _ref2.body;
return cy.wrap(data["methodResponses"][0][1].ids[0]).as("draftId");
});
};
var getIdentityId = function getIdentityId(_ref3) {
var apiUrl = _ref3.apiUrl,
accountId = _ref3.accountId;
cy.request({
url: apiUrl,
method: "POST",
headers: headers,
body: JSON.stringify({
using: ["urn:ietf:params:jmap:core", "urn:ietf:params:jmap:mail", "urn:ietf:params:jmap:submission"],
methodCalls: [["Identity/get", {
accountId: accountId,
ids: null
}, "a"]]
})
}).then(function (_ref4) {
var data = _ref4.body;
var identities = data["methodResponses"][0][1].list;
var identityId = identities.find(function (_ref5) {
var email = _ref5.email;
return email === username;
}).id;
cy.wrap(identityId).as("identityId");
});
};
var composeEmail = function composeEmail(_ref6) {
var apiUrl = _ref6.apiUrl,
accountId = _ref6.accountId,
draftId = _ref6.draftId,
identityId = _ref6.identityId,
emailDetails = _ref6.emailDetails;
var subject = emailDetails.subject,
body = emailDetails.body,
to = emailDetails.to,
senderName = emailDetails.senderName;
var draftObject = {
from: [{
email: username,
name: senderName
}],
to: [{
email: to
}],
subject: subject,
keywords: {
$draft: true
},
mailboxIds: _defineProperty({}, draftId, true),
bodyValues: {
body: {
value: body,
charset: "utf-8"
}
},
textBody: [{
partId: "body",
type: "text/plain"
}]
};
cy.request({
url: apiUrl,
method: "POST",
headers: headers,
body: JSON.stringify({
using: ["urn:ietf:params:jmap:core", "urn:ietf:params:jmap:mail", "urn:ietf:params:jmap:submission"],
methodCalls: [["Email/set", {
accountId: accountId,
create: {
draft: draftObject
}
}, "a"], ["EmailSubmission/set", {
accountId: accountId,
onSuccessDestroyEmail: ["#sendIt"],
create: {
sendIt: {
emailId: "#draft",
identityId: identityId
}
}
}, "b"]]
})
});
};
var checkForEnvs = function checkForEnvs() {
if (!username || !token) {
throw new Error("Please provide CYPRESS_FASTMAIL_USERNAME and CYPRESS_FASTMAIL_TOKEN as environment variables");
}
};
export var sendEmail = function sendEmail(emailDetails) {
checkForEnvs();
getSession().then(function (_ref7) {
var session = _ref7.body;
var apiUrl = session.apiUrl,
primaryAccounts = session.primaryAccounts;
var accountId = primaryAccounts["urn:ietf:params:jmap:mail"];
getDraftId({
apiUrl: apiUrl,
accountId: accountId
});
getIdentityId({
apiUrl: apiUrl,
accountId: accountId
});
cy.get("@draftId").then(function (draftId) {
cy.get("@identityId").then(function (identityId) {
return composeEmail({
apiUrl: apiUrl,
accountId: accountId,
draftId: draftId,
identityId: identityId,
emailDetails: emailDetails
});
});
});
});
};
export var emailUtils = {
sendEmail: sendEmail
};
//# sourceMappingURL=email.js.map