From 4abf8992227059b1656395c51608bc2ca9c13876 Mon Sep 17 00:00:00 2001 From: Ramon Wenger Date: Wed, 24 Oct 2018 14:33:46 +0200 Subject: [PATCH] Refactor login test to command --- client/cypress/integration/login-page-spec.js | 59 +++++++++++++++---- client/cypress/support/commands.js | 8 +++ 2 files changed, 56 insertions(+), 11 deletions(-) diff --git a/client/cypress/integration/login-page-spec.js b/client/cypress/integration/login-page-spec.js index eba23a19..04533264 100644 --- a/client/cypress/integration/login-page-spec.js +++ b/client/cypress/integration/login-page-spec.js @@ -3,21 +3,58 @@ describe('The Login Page', () => { const username = 'test'; const password = 'test'; - cy.visit('/'); + cy.login(username, password); - cy.get('#id_username').type(username); - cy.get('#id_password').type(`${password}{enter}`); cy.getCookie('sessionid').should('exist'); cy.get('.start-page__title').should('contain', 'skillbox') }); - it('logs in programmatically without using the UI', () => { - const username = 'test'; - const password = 'test'; + // it('logs in programmatically without using the UI', () => { + // cy.visit('/accounts/login/'); // have to get a csrf token by getting the base page first + // + // const username = 'test'; + // const password = 'test'; + // + // cy.getCookie('csrftoken').then(token => { + // const options = { + // url: '/accounts/login/', + // method: 'POST', + // headers: { + // 'X-CSRFToken': token.value, + // 'content-type': 'multipart/form-data' + // }, + // from: true, + // body: { + // username: username, + // password: password, + // // csrfmiddlewaretoken: token.value + // } + // }; + // + // cy.request(options); + // cy.getCookie('sessionid').should('exist'); + // cy.visit('/'); + // + // cy.get('.start-page__title').should('contain', 'skillbox') + // + // + // // cy.visit('/'); + // // cy.getCookie('csrftoken') + // // .then((csrftoken) => { + // // const response = cy.request({ + // // method: 'POST', + // // url: '/login/', + // // form: true, + // // body: { + // // identification: username, + // // password: password, + // // csrfmiddlewaretoken: csrftoken.value + // // } + // // }); + // // }); + // + // }); - cy.request('POST', '/login', { - username, - password - }) - }) + + // }) }) diff --git a/client/cypress/support/commands.js b/client/cypress/support/commands.js index c1f5a772..c65e201b 100644 --- a/client/cypress/support/commands.js +++ b/client/cypress/support/commands.js @@ -23,3 +23,11 @@ // // -- This is will overwrite an existing command -- // Cypress.Commands.overwrite("visit", (originalFn, url, options) => { ... }) + +Cypress.Commands.add("login", (username, password) => { + cy.visit('/'); + + cy.get('#id_username').type(username); + cy.get('#id_password').type(`${password}{enter}`); + +});