Add some more base for cypress testing

This commit is contained in:
Ramon Wenger 2019-01-24 15:15:14 +01:00
parent fc6cf0c0d9
commit c468a13720
5 changed files with 52 additions and 4 deletions

View File

@ -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')
})
})

View File

@ -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')
})
});

View File

@ -3,8 +3,10 @@ describe('The Login Page', () => {
const username = 'test'; const username = 'test';
const password = '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.getCookie('sessionid').should('exist');
cy.get('.start-page__title').should('contain', 'skillbox') cy.get('.start-page__title').should('contain', 'skillbox')

View File

@ -1,5 +1,6 @@
describe('The Room Page', () => { describe('The Room Page', () => {
it.only('displays new room entry with author name', () => { it.only('displays new room entry with author name', () => {
cy.viewport('macbook-15');
cy.login('rahel.cueni', 'test'); cy.login('rahel.cueni', 'test');
cy.visit('/room/ein-historisches-festival'); cy.visit('/room/ein-historisches-festival');

View File

@ -25,9 +25,33 @@
// Cypress.Commands.overwrite("visit", (originalFn, url, options) => { ... }) // Cypress.Commands.overwrite("visit", (originalFn, url, options) => { ... })
Cypress.Commands.add("login", (username, password) => { 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); const csrf = $html.find('input[name=csrfmiddlewaretoken]').val();
cy.get('#id_password').type(`${password}{enter}`); 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
}
});
});