From c468a13720e2270d0ccd7d4de11c852351130165 Mon Sep 17 00:00:00 2001 From: Ramon Wenger Date: Thu, 24 Jan 2019 15:15:14 +0100 Subject: [PATCH] Add some more base for cypress testing --- .../integration/home-page-logged-in-spec.js | 8 +++++ client/cypress/integration/login-csrf.spec.js | 13 ++++++++ client/cypress/integration/login-page-spec.js | 4 ++- client/cypress/integration/room-page.spec.js | 1 + client/cypress/support/commands.js | 30 +++++++++++++++++-- 5 files changed, 52 insertions(+), 4 deletions(-) create mode 100644 client/cypress/integration/home-page-logged-in-spec.js create mode 100644 client/cypress/integration/login-csrf.spec.js diff --git a/client/cypress/integration/home-page-logged-in-spec.js b/client/cypress/integration/home-page-logged-in-spec.js new file mode 100644 index 00000000..ea7c5aaa --- /dev/null +++ b/client/cypress/integration/home-page-logged-in-spec.js @@ -0,0 +1,8 @@ +describe('The Logged In Home Page', () => { + it('successfully loads', () => { + cy.login('test', 'test'); + cy.visit('/'); + + cy.get('.block-title__title').should('contain', 'Inhalte') + }) +}) diff --git a/client/cypress/integration/login-csrf.spec.js b/client/cypress/integration/login-csrf.spec.js new file mode 100644 index 00000000..4250711e --- /dev/null +++ b/client/cypress/integration/login-csrf.spec.js @@ -0,0 +1,13 @@ +describe('The Login CSRF Token', () => { + + it('403 status without token', () => { + cy.loginByCsrf('some-token') + .its('status') + .should('eq', 403) + }); + + it('gets token from response body', () => { + cy.login('test', 'test') + }) + +}); diff --git a/client/cypress/integration/login-page-spec.js b/client/cypress/integration/login-page-spec.js index 04533264..83d50dcf 100644 --- a/client/cypress/integration/login-page-spec.js +++ b/client/cypress/integration/login-page-spec.js @@ -3,8 +3,10 @@ describe('The Login Page', () => { const username = 'test'; const password = 'test'; - cy.login(username, password); + cy.visit('/'); + 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') diff --git a/client/cypress/integration/room-page.spec.js b/client/cypress/integration/room-page.spec.js index 3a4dc1c6..4446ae5a 100644 --- a/client/cypress/integration/room-page.spec.js +++ b/client/cypress/integration/room-page.spec.js @@ -1,5 +1,6 @@ describe('The Room Page', () => { it.only('displays new room entry with author name', () => { + cy.viewport('macbook-15'); cy.login('rahel.cueni', 'test'); cy.visit('/room/ein-historisches-festival'); diff --git a/client/cypress/support/commands.js b/client/cypress/support/commands.js index c65e201b..ac26ef73 100644 --- a/client/cypress/support/commands.js +++ b/client/cypress/support/commands.js @@ -25,9 +25,33 @@ // Cypress.Commands.overwrite("visit", (originalFn, url, options) => { ... }) Cypress.Commands.add("login", (username, password) => { - cy.visit('/'); + cy.request('/') + .its('body') + .then(body => { + console.log(body); + const $html = Cypress.$(body); - cy.get('#id_username').type(username); - cy.get('#id_password').type(`${password}{enter}`); + const csrf = $html.find('input[name=csrfmiddlewaretoken]').val(); + console.log(csrf); + cy.loginByCsrf(username, password, csrf) + .then(resp => { + expect(resp.status).to.eq(200); + expect(resp.body).to.include('skillbox'); + }); + }) }); + +Cypress.Commands.add('loginByCsrf', (username, password, csrftoken) => { + cy.request({ + method: 'POST', + url: '/accounts/login/', + failOnStatusCode: false, + form: true, + body: { + username: username, + password: password, + csrfmiddlewaretoken: csrftoken + } + }); +});