All Downloads are FREE. Search and download functionalities are using the official Maven repository.

package.cjs.cypress-utils.utils.email.js Maven / Gradle / Ivy

Go to download

A package encapsulating common code across neeto projects including initializers, utility functions, common components and hooks and so on.

There is a newer version: 4.12.3
Show newest version
"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.sendEmail = exports.emailUtils = void 0;
var _defineProperty2 = _interopRequireDefault(require("@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: (0, _defineProperty2["default"])({}, 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");
  }
};
var sendEmail = exports.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
        });
      });
    });
  });
};
var emailUtils = exports.emailUtils = {
  sendEmail: sendEmail
};
//# sourceMappingURL=email.js.map




© 2015 - 2024 Weber Informatics LLC | Privacy Policy