strapi/cypress/support/commands.js

201 lines
5.0 KiB
JavaScript
Raw Normal View History

// ***********************************************
// This example commands.js shows you how to
// create various custom commands and overwrite
// existing commands.
//
// For more comprehensive examples of custom
// commands please read more here:
// https://on.cypress.io/custom-commands
// ***********************************************
//
//
// -- This is a parent command --
// Cypress.Commands.add("login", (email, password) => { ... })
//
//
// -- This is a child command --
// Cypress.Commands.add("drag", { prevSubject: 'element'}, (subject, options) => { ... })
//
//
// -- This is a dual command --
// Cypress.Commands.add("dismiss", { prevSubject: 'optional'}, (subject, options) => { ... })
//
//
// -- This is will overwrite an existing command --
// Cypress.Commands.overwrite("visit", (originalFn, url, options) => { ... })
const stringify = JSON.stringify;
const backendUrl = Cypress.config('backendUrl');
const serverRestartDelay = Cypress.config('serverRestartDelay');
2019-03-12 11:58:30 +01:00
const WAIT_ON_CMD = `wait-on ${backendUrl}`;
2019-03-11 10:42:43 +01:00
Cypress.Commands.add('waitRestart', () => {
return cy.exec(WAIT_ON_CMD);
});
Cypress.Commands.add('createUser', () => {
const user = {
2018-11-27 14:13:07 +01:00
username: 'admin',
email: 'admin@strapi.io',
password: 'pcw123',
};
2018-11-27 14:13:07 +01:00
return cy
.request({ url: `${backendUrl}/users-permissions/init`, method: 'GET' })
.then(response => {
2018-11-27 14:13:07 +01:00
const {
body: { hasAdmin },
} = response;
if (!hasAdmin) {
// Create one
2019-04-09 18:35:49 +02:00
cy.request({
url: `${backendUrl}/admin/auth/local/register`,
method: 'POST',
body: user,
});
}
});
});
Cypress.Commands.add('checkModalOpening', () => {
return cy.get('.modal').invoke('show');
2018-11-27 14:13:07 +01:00
});
Cypress.Commands.add('deleteUser', (id, jwt) => {
cy.request({
2019-04-10 15:57:12 +02:00
url: `${backendUrl}/content-manager/explorer/administrator/${id}?source=admin`,
method: 'DELETE',
headers: {
2018-11-27 14:13:07 +01:00
Authorization: `Bearer ${jwt}`,
},
});
});
Cypress.Commands.add('createProductAndTagApis', (jwt = null) => {
2019-03-11 10:42:43 +01:00
return cy
.exec(WAIT_ON_CMD)
.fixture('api/tag.json')
.then(body => {
return cy
.request({
url: `${backendUrl}/content-type-builder/models`,
method: 'POST',
headers: {
Authorization: `Bearer ${jwt}`,
},
body,
})
.exec(WAIT_ON_CMD)
.fixture('api/product.json')
.then(body => {
return cy
.request({
url: `${backendUrl}/content-type-builder/models`,
method: 'POST',
headers: {
Authorization: `Bearer ${jwt}`,
},
body,
})
.exec(WAIT_ON_CMD);
});
});
});
Cypress.Commands.add('createCTMApis', (jwt = null) => {
return cy
.createProductAndTagApis(jwt)
2019-03-11 10:42:43 +01:00
.exec(WAIT_ON_CMD)
.fixture('api/category.json')
.then(body => {
return cy
.request({
url: `${backendUrl}/content-type-builder/models`,
method: 'POST',
headers: {
Authorization: `Bearer ${jwt}`,
},
body,
})
2019-03-11 10:42:43 +01:00
.exec(WAIT_ON_CMD);
});
});
Cypress.Commands.add('deleteAllModelData', (model, jwt, source = null) => {
// GET all data;
cy.request({
url: `${backendUrl}/content-manager/explorer/${model}`,
method: 'GET',
headers: {
Authorization: `Bearer ${jwt}`,
},
2018-11-27 14:13:07 +01:00
}).then(data => {
const entriesToDelete = data.body.reduce((acc, curr) => {
return acc.concat(curr.id);
}, []);
const qs = Object.assign(entriesToDelete, source ? { source } : {});
return cy.request({
url: `${backendUrl}/content-manager/explorer/deleteAll/${model}`,
method: 'DELETE',
headers: {
Authorization: `Bearer ${jwt}`,
},
qs,
});
});
});
Cypress.Commands.add('deleteApi', (model, jwt) => {
2018-11-27 14:13:07 +01:00
return cy
2019-03-11 10:42:43 +01:00
.exec(WAIT_ON_CMD)
2018-11-27 14:13:07 +01:00
.request({
url: `${backendUrl}/content-type-builder/models/${model}`,
method: 'DELETE',
headers: {
Authorization: `Bearer ${jwt}`,
},
})
2019-03-11 10:42:43 +01:00
.exec(WAIT_ON_CMD);
});
Cypress.Commands.add('login', () => {
2018-11-27 14:13:07 +01:00
cy.createUser();
return cy
.request({
2019-04-09 18:35:49 +02:00
url: `${backendUrl}/admin/auth/local`,
2018-11-27 14:13:07 +01:00
method: 'POST',
body: {
identifier: 'admin',
password: 'pcw123',
},
})
.then(response => {
window.localStorage.setItem('jwtToken', stringify(response.body.jwt));
window.localStorage.setItem('userInfo', stringify(response.body.user));
2018-11-27 14:13:07 +01:00
return response.body;
});
});
Cypress.Commands.add('seedData', (model, jwt, source = null) => {
2018-11-27 14:13:07 +01:00
return cy.fixture(`seeds/${model}.json`).then(seed => {
seed.forEach(body => {
cy.request({
method: 'POST',
url: `${backendUrl}/content-manager/explorer/${model}?source='content-manager`,
headers: {
Authorization: `Bearer ${jwt}`,
},
body,
});
});
2018-11-27 14:13:07 +01:00
});
});
Cypress.Commands.add('submitForm', () => {
return cy.get('form').submit();
2018-11-27 14:13:07 +01:00
});