Skip to content

Instantly share code, notes, and snippets.

@jhaynie
Last active September 4, 2023 14:35
Show Gist options
  • Save jhaynie/1db3a38acdc7977fc66c483d4516d613 to your computer and use it in GitHub Desktop.
Save jhaynie/1db3a38acdc7977fc66c483d4516d613 to your computer and use it in GitHub Desktop.
Cypress.io login to github with node app using passport
import url from 'url';
describe('Login', function() {
it('cy.should - assert that <title> is correct', function() {
cy.visit('http://localhost:5001');
cy.get('#login').should('contain', 'Login with Github');
cy.request({
url: '/login',
followRedirect: false
})
.then((resp) => {
// should have status code 302
expect(resp.status).to.eq(302);
expect(resp.redirectedToUrl).to.eq('http://localhost:5001/auth/github');
})
.then(() => {
return cy.request({
url: '/auth/github',
followRedirect: false
})
})
.then((resp) => {
expect(resp.status).to.eq(302);
expect(resp.redirectedToUrl).to.match(/login\/oauth\/authorize/);
const u = url.parse(resp.redirectedToUrl, true);
expect(u.query.redirect_uri).to.match(/auth\/github\/callback/);
return cy.request({
url: resp.redirectedToUrl,
followRedirect: false
});
})
.then((resp) => {
expect(resp.status).to.eq(302);
expect(resp.redirectedToUrl).to.match(/\/login\?client_id=/);
return cy.request({
url: resp.redirectedToUrl,
followRedirect: false
})
.its('body')
.then(body => {
const html = Cypress.$(body);
console.log(body);
const authenticity_token = html.find('input[name=authenticity_token]').val();
const action = html.find('form[method=post]').attr('action');
expect(action).to.eq('/session');
expect(authenticity_token).to.match(/\w{15,}/);
return cy.request({
method: 'POST',
url: 'https://github.com/session',
form: true,
followRedirect: false,
body: {
authenticity_token: authenticity_token,
login: 'jhaynie+test@gmail.com',
password: 'xxx111xxx111xxx',
commit: 'Sign In',
utf8: '✓'
}
});
});
})
.then((resp) => {
console.log(resp);
expect(resp.status).to.eq(302);
expect(resp.redirectedToUrl).to.match(/\/oauth\/authorize\?client_id=/);
return cy.request({
url: resp.redirectedToUrl,
followRedirect: false
});
})
.then((resp) => {
console.log(resp);
expect(resp.status).to.eq(302);
expect(resp.redirectedToUrl).to.match(/\/auth\/github\/callback\?code=/);
return cy.request({
url: resp.redirectedToUrl,
followRedirect: true
});
})
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment