Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
coutoPL committed Sep 29, 2024
1 parent b15373d commit ec1fa1c
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 33 deletions.
1 change: 0 additions & 1 deletion e2e-tests/cypress/e2e/Direct-kibana-request.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ describe('Direct kibana request', () => {
cy.log('Get imported saved objects for user1 infosec group');
kbnApiAdvancedClient.getSavedObjects(user1, "infosec_group")
.then(result => {
debugger;
const actual = result.saved_objects.some(
saved_object => saved_object.id === 'my-pattern' || saved_object.id === 'my-dashboard'
);
Expand Down
80 changes: 48 additions & 32 deletions e2e-tests/cypress/support/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,43 +109,59 @@ Cypress.Commands.add(
}
);

function call(method: string, url: string, credentials: string, payload?: Cypress.RequestBody, headers?: { [key: string]: string }) {
cy.request({
method: method,
url: url,
headers: {
authorization: `Basic ${btoa(credentials)}`,
...headers
},
body: payload || null,
}).then((response) => {
console.log(`RR: ${method} ${url} ${credentials} ${JSON.stringify(headers)} = ${JSON.stringify(response)}`)
// expect(response.status).to.be.within(200, 299);
function call(method: string, url: string, credentials: string, payload?: Cypress.RequestBody, headers?: { [key: string]: string }): Cypress.Chainable<any> {
return withIgnoredCookies(() =>
cy.request({
method: method,
url: url,
headers: {
authorization: `Basic ${btoa(credentials)}`,
...headers
},
body: payload || null,
})
).then((response) => {
expect(response.status).to.be.within(200, 299);
return isJsonString(response.body) ? JSON.parse(response.body) : response.body;
})
}

function uploadFile(url: string, credentials: string, fixtureFilename: string, headers?: { [key: string]: string }) {
cy.fixture(fixtureFilename, 'base64').then((fileContent) => {
const formData = new FormData();
formData.append('file', Cypress.Blob.base64StringToBlob(fileContent, 'application/octet-stream'), fixtureFilename);

const requestHeaders = {
authorization: `Basic ${btoa(credentials)}`,
...(headers || {})
};
function uploadFile(url: string, credentials: string, fixtureFilename: string, headers?: { [key: string]: string }): Cypress.Chainable<any> {
return withIgnoredCookies(() => {
return cy.fixture(fixtureFilename, 'base64').then((fileContent) => {
const formData = new FormData();
formData.append('file', Cypress.Blob.base64StringToBlob(fileContent, 'application/octet-stream'), fixtureFilename);

const requestHeaders = {
authorization: `Basic ${btoa(credentials)}`,
...(headers || {})
};

cy.request({
method: "POST",
url: url,
headers: requestHeaders,
body: formData,
}).then((response) => {
expect(response.status).to.be.within(200, 299);
return isJsonString(response.body) ? JSON.parse(response.body) : response.body;
});
});
});
}

cy.request({
method: "POST",
url: url,
headers: requestHeaders,
body: formData,
// You might want to comment this out unless you're sure it should be sent
// contentType: false, // This tells Cypress not to set the content-type, allowing FormData to set it
// failOnStatusCode: false // Uncomment if you want to ignore 4xx/5xx responses temporarily
}).then((response) => {
expect(response.status).to.be.within(200, 299);
return isJsonString(response.body) ? JSON.parse(response.body) : response.body;
// it's a workaround for this: https://github.com/cypress-io/cypress/issues/8909
function withIgnoredCookies(callback: () => Cypress.Chainable<Cypress.Response<any>>): Cypress.Chainable<Cypress.Response<any>> {
return cy.getCookies().then((cookies) => {
return cy.clearAllCookies().then(() => {
return callback().then((result) => {
const setCookiePromises = cookies.map(({ name, value, ...rest }) => {
return cy.setCookie(name, value, rest);
});
return Cypress.Promise.all(setCookiePromises).then(() => {
return result;
});
})
});
});
}
Expand Down

0 comments on commit ec1fa1c

Please sign in to comment.