Skip to content

Instantly share code, notes, and snippets.

@filiphric
Last active August 11, 2022 10:48
Show Gist options
  • Select an option

  • Save filiphric/e92d064df5cff422b4331dbb2741d402 to your computer and use it in GitHub Desktop.

Select an option

Save filiphric/e92d064df5cff422b4331dbb2741d402 to your computer and use it in GitHub Desktop.
Cypress request to mailosaur API
const userEmail = 'email.abcdefg@mailosaur.io'
describe('Reset password flow', () => {
it('Should receive an email and use reset password link', () => {
// trigger reset password for user
// this command is equal to clicking „I forgot my password“ link and entering user email
cy
.userPasswordReset(userEmail)
cy
.request({
method: 'POST',
url: 'https://mailosaur.com/api/messages/await?server=abcdefg', // abcdefg is server name
headers: {
authorization: Cypress.env('mailosaurAuth')
},
body: {
'sentTo': userEmail,
'subject': 'Password reset'
}
}).then( response => {
// continue with test, open link
cy
.visit(response.body.text.links[1])
// fill in and confirm new password
cy
.get('#password')
.type(newPassword);
cy
.get('#passwordConfirm')
.type(newPassword);
cy
.get('#reset-change-password-btn')
.should('be.visible')
.should('be.enabled')
.click();
// land on desired page after confirming password reset
cy
.location('href')
.should('contains', '/login');
// login with my new password
// ...
})
});
});
@muratkeremozcan

Copy link
Copy Markdown

Hey @filiphric

I am evaluating Mailosaur for Cypress integration at Siemens SW Hub. We want to use this for all our cloud products.

I began to create a sample repo with easy examples of setting Cypress from scratch and using it with Mailosaur.

I could not find Cypress-specific documentation on their site, so the goal is to get anyone at Siemens, or anyone in the open community using Cypress with Mailosaur started painlessly.

Any contribution from you is appreciated.

Thank you, Murat

@iranamiriyeva

Copy link
Copy Markdown

Hi,

Thank you very much for the amazing option. I have 2 questions:

  1. I just do not know where can i get mailosaurAuth value?
    As before i was using MAILOSAUR_API_KEY: and MAILOSAUR_SERVER: for the request via function.
  2. When i tried to test with Your script option, i received 405 error code due to origin.
    I use Cypress version 10.4. What should i do to prevent such issue?
    Can it be because i did not put mailosaurAuth?

Thank you in advance,
Irana

@iranamiriyeva

iranamiriyeva commented Aug 7, 2022

Copy link
Copy Markdown

Now i get 401 error code. It is better than 405 =D But still does not work:(

cy.request() failed on:

https://mailosaur.com/api/messages?server=*****
The response we received from your web server was:

401: Unauthorized
This was considered a failure because the status code was not 2xx or 3xx.
If you do not want status codes to cause failures pass the option: failOnStatusCode: false

The request we sent was:
Method: POST
URL: https://mailosaur.com/api/messages?server=*****
Headers: {
"Connection": "keep-alive",
"authorization": "",
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36",
"accept": "/",
"accept-encoding": "gzip, deflate",
"content-type": "application/json",
"content-length": 80
}
Body: {"sentTo":"@
****.mailosaur.net","subject":"Passwort zurücksetzen"}

The response we got was:
Status: 401 - Unauthorized
Headers: {
"content-length": "0",
"date": "Sun, 07 Aug 2022 21:23:12 GMT",
"server": "Kestrel",
"cache-control": "no-store, must-revalidate, no-cache",
"expires": "-1",
"www-authenticate": "Bearer",
"x-frame-options": "SAMEORIGIN",
"x-xss-protection": "1; mode=block",
"x-content-type-options": "nosniff",
"strict-transport-security": "max-age=31536000; includeSubdomains; preload",
"via": "1.1 google",
"alt-svc": "h3=":443"; ma=2592000,h3-29=":443"; ma=2592000"

@filiphric

Copy link
Copy Markdown
Author

Hey Irana, I’m pretty sure this way of checking the emails is deprecated. I suggest you take a look at the official mailosaur plugin: https://mailosaur.com/docs/frameworks-and-tools/cypress/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment