Last active
February 18, 2020 04:25
-
-
Save jackmahoney/55c31fd313c6aced015cbd87872efec9 to your computer and use it in GitHub Desktop.
MailSlurp javascript client example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// import an apiKey and the official js client | |
const apiKey = require("./config.json").apiKey; | |
const MailSlurpClient = require("mailslurp-client"); | |
// instantiate a client for the inbox endpoints | |
const api = new MailSlurpClient.InboxcontrollerApi(); | |
// test the authentication flow on www.mailslurp.com | |
// using Cypress test runenr | |
describe("Emaile2e.com user signup", () => { | |
// we will set these with emaile2e | |
let username; | |
let emailAddress; | |
const password = "test-password"; | |
before("create a random email inbox to sign-up with", done => { | |
// create a new email inbox that returns an id and an address | |
api | |
.createRandomInboxUsingPOST(apiKey) | |
.then( | |
data => data.payload, | |
err => { | |
// log or inspect an error here if necessary | |
throw err; | |
} | |
) | |
.then(({ id, address }) => { | |
username = id; | |
emailAddress = address; | |
expect(username).to.eq(id); | |
done(); | |
}); | |
}); | |
it("allows signup with generated inbox and username", () => { | |
cy.visit("/sign-up"); | |
// use the username and email address we got | |
// from MailSlurp to sign a user up by filling out | |
// the sign-up form | |
cy.get("#username").type(username); | |
cy.get("#email").type(emailAddress); | |
cy.get("#password").type(password); | |
cy.get("#passwordConfirmation").type(password); | |
cy.get("button[type='submit']").click(); | |
}); | |
it( | |
"sends a verification code which can be " + | |
"received by user, extracted, and submitted", | |
() => { | |
let verificationCode; | |
cy.location("pathname").should("eq", `/verify/${username}`); | |
// use cypress's async api | |
cy.then(() => { | |
// check the users inbox and hold the | |
// connection until one email is present | |
return api | |
.getEmailsForInboxUsingGET(apiKey, username, { | |
minCount: 1, | |
maxWait: 90 | |
}) | |
.then( | |
data => data.payload, | |
err => { | |
throw err; | |
} | |
) | |
.then(([latestEmail]) => { | |
// regex match for the confirmation code | |
// within the email body | |
const r = /\s(\d{6})\./g; | |
// extract the verication code | |
verificationCode = r.exec(latestEmail.body)[1]; | |
}); | |
}); | |
// submit the code we extracted from the email | |
cy.get("#code").then(input => { | |
cy.wrap(input).type(verificationCode); | |
}); | |
cy.get("button[type='submit']").click(); | |
} | |
); | |
it("successfully verifies a user" + "and allows that user to login", () => { | |
cy.location("pathname").should("eq", "/login"); | |
cy.get("#username").type(username); | |
cy.get("#password").type(password); | |
cy.get("button[type='submit']").click(); | |
cy.location("pathname").should("eq", "/dashboard"); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment