Skip to content

Commit

Permalink
Enhance Cypress content creation command (#4210)
Browse files Browse the repository at this point in the history
  • Loading branch information
sneridagh committed Jan 10, 2023
1 parent eb7ad88 commit e067d40
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 46 deletions.
Binary file added cypress/fixtures/halfdome2022.jpeg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
136 changes: 90 additions & 46 deletions cypress/support/commands.js
Expand Up @@ -46,10 +46,12 @@ Cypress.Commands.add(
contentType,
contentId,
contentTitle,
contentDescription,
path = '',
allow_discussion = false,
transition = '',
bodyModifier = (body) => body,
image = false,
}) => {
let api_url, auth;
if (Cypress.env('API') === 'guillotina') {
Expand All @@ -62,40 +64,44 @@ Cypress.Commands.add(
api_url = PLONE_API_URL;
auth = ploneAuthObj;
}

const defaultParams = {
method: 'POST',
url: `${api_url}/${path}`,
headers: {
Accept: 'application/json',
},
auth: auth,
body: {
'@type': contentType,
id: contentId,
title: contentTitle,
description: contentDescription,
allow_discussion: allow_discussion,
},
};

if (contentType === 'File') {
return cy.request({
method: 'POST',
url: `${api_url}/${path}`,
headers: {
Accept: 'application/json',
},
auth: auth,
const params = {
...defaultParams,
body: bodyModifier({
'@type': contentType,
id: contentId,
title: contentTitle,
...defaultParams.body,
file: {
data: 'dGVzdGZpbGUK',
encoding: 'base64',
filename: 'lorem.txt',
'content-type': 'text/plain',
},
allow_discussion: allow_discussion,
}),
});
};

return cy.request(params);
}
if (contentType === 'Image') {
return cy.request({
method: 'POST',
url: `${api_url}/${path}`,
headers: {
Accept: 'application/json',
},
auth: auth,
const params = {
...defaultParams,
body: bodyModifier({
'@type': contentType,
id: contentId,
title: contentTitle,
...defaultParams.body,
image: {
data:
'iVBORw0KGgoAAAANSUhEUgAAANcAAAA4CAMAAABZsZ3QAAAAM1BMVEX29fK42OU+oMvn7u9drtIPisHI4OhstdWZyt4fkcXX5+sAg74umMhNp86p0eJ7vNiKw9v/UV4wAAAAAXRST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ikJTL93gs3y7Yy4VYbIxOHrcMizPqWOjqO9l3Uz52kibQy4xxOgqhJvD+w5rvokOcAlGvNCfeqCv1ste1stzLm0f71Iq3ZfTrPfuE5nhPtF+LvQE2lffQC7pYtQy3tdzdrKvd5TLVVzDetScS3nEKmmwDyt1Cev1kX3YfbvzNK4fzrlw+cB6vm+uiUgf2zdXI62241LawCb7Pi5FXFPF8KpzDoF/Sw2lg+GrHNbno1mhPu+VCF/vfMnw06PnUl6j48dVHD3jHNHPua+fc3o/5yp/zsGi0vYtzi3Pz5mHd4T6BWMIlewacd63AAAAAElFTkSuQmCC',
Expand All @@ -104,37 +110,74 @@ Cypress.Commands.add(
'content-type': 'image/png',
},
}),
});
};

return cy.request(params);
}
if (
['Document', 'News Item', 'Folder', 'CMSFolder'].includes(contentType)
) {
return cy
.request({
method: 'POST',
url: `${api_url}/${path}`,
headers: {
Accept: 'application/json',
const params = {
...defaultParams,
body: {
...defaultParams.body,
blocks: {
'd3f1c443-583f-4e8e-a682-3bf25752a300': { '@type': 'title' },
'7624cf59-05d0-4055-8f55-5fd6597d84b0': { '@type': 'slate' },
},
auth: auth,
blocks_layout: {
items: [
'd3f1c443-583f-4e8e-a682-3bf25752a300',
'7624cf59-05d0-4055-8f55-5fd6597d84b0',
],
},
},
};

if (image) {
let sourceFilename = 'cypress/fixtures/halfdome2022.jpg';
let imageObject = {
encoding: 'base64',
filename: 'image.jpg',
'content-type': 'image/jpg',
};
if (typeof image === 'object') {
sourceFilename = image.sourceFilename;
imageObject = {
...imageObject,
...image,
};
}
cy.readFile(sourceFilename, 'base64').then((encodedImage) => {
const withImageParams = {
...params,
body: bodyModifier({
...params.body,
preview_image: {
...imageObject,
data: encodedImage,
},
}),
};

return cy.request(withImageParams).then(() => {
if (transition) {
cy.setWorkflow({
path: path || contentId,
review_state: transition,
});
}
console.log(`${contentType} created`);
});
});
} else {
const documentParams = {
...params,
body: bodyModifier({
'@type': contentType,
id: contentId,
title: contentTitle,
blocks: {
'd3f1c443-583f-4e8e-a682-3bf25752a300': { '@type': 'title' },
'7624cf59-05d0-4055-8f55-5fd6597d84b0': { '@type': 'slate' },
},
blocks_layout: {
items: [
'd3f1c443-583f-4e8e-a682-3bf25752a300',
'7624cf59-05d0-4055-8f55-5fd6597d84b0',
],
},
allow_discussion: allow_discussion,
...params.body,
}),
})
.then(() => {
};
return cy.request(documentParams).then(() => {
if (transition) {
cy.setWorkflow({
path: path || contentId,
Expand All @@ -143,6 +186,7 @@ Cypress.Commands.add(
}
console.log(`${contentType} created`);
});
}
} else {
return cy
.request({
Expand Down
1 change: 1 addition & 0 deletions news/4210.feature
@@ -0,0 +1 @@
Enhance Cypress content creation command @sneridagh

0 comments on commit e067d40

Please sign in to comment.