Skip to content

Commit

Permalink
ci(cypress): Use cy.request instead of axios
Browse files Browse the repository at this point in the history
Signed-off-by: Julius Härtl <[email protected]>
  • Loading branch information
juliusknorr authored and mejo- committed Jun 27, 2023
1 parent 30b397c commit b217d0f
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 38 deletions.
1 change: 0 additions & 1 deletion cypress/e2e/nodes/ImageView.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ describe('Image View', () => {
before(() => {
cy.createUser(user)
cy.login(user)
cy.visit('/apps/files')

// Upload test files to user's storage
cy.createFolder('child-folder')
Expand Down
85 changes: 48 additions & 37 deletions cypress/support/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,43 +41,53 @@ Cypress.Commands.overwrite('login', (login, user) => {
})

Cypress.Commands.add('ocsRequest', (options) => {
return cy.request({
form: true,
auth,
headers: {
'OCS-ApiRequest': 'true',
'Content-Type': 'application/x-www-form-urlencoded',
},
...options,
})
return cy.request('/csrftoken')
.then(({ body }) => body.token)
.then(requesttoken => {
return cy.request({
form: true,
auth,
headers: {
'OCS-ApiRequest': 'true',
'Content-Type': 'application/x-www-form-urlencoded',
requesttoken,
},
...options,
})
})
})

Cypress.Commands.add('uploadFile', (fileName, mimeType, target) => {
return cy.fixture(fileName, 'base64')
.then(Cypress.Blob.base64StringToBlob)
return cy.fixture(fileName, 'binary')
.then(Cypress.Blob.binaryStringToBlob)
.then(blob => {
const file = new File([blob], fileName, { type: mimeType })
if (typeof target !== 'undefined') {
fileName = target
}
return cy.request('/csrftoken')
.then(({ body }) => body.token)
.then(requesttoken => {
return axios.put(`${url}/remote.php/webdav/${fileName}`, file, {
cy.request('/csrftoken')
.then(({ body }) => {
return cy.wrap(body.token)
})
.then(async (requesttoken) => {
return cy.request({
url: `${url}/remote.php/webdav/${fileName}`,
method: 'put',
body: blob.size > 0 ? blob : '',
auth,
headers: {
requesttoken,
'Content-Type': mimeType,
},
}).then(response => {
const fileId = Number(
response.headers['oc-fileid']?.split('oc')?.[0]
)
cy.log(`Uploaded ${fileName}`,
response.status,
{ fileId }
)
return fileId
})
}).then(response => {
const fileId = Number(
response.headers['oc-fileid']?.split('oc')?.[0]
)
cy.log(`Uploaded ${fileName}`,
response.status,
{ fileId }
)
return cy.wrap(fileId)
})
})
})
Expand All @@ -95,27 +105,27 @@ Cypress.Commands.add('downloadFile', (fileName) => {
})

Cypress.Commands.add('createFile', (target, content, mimeType = 'text/markdown') => {
const fileName = target.split('/').pop()

const blob = new Blob([content], { type: mimeType })
const file = new File([blob], fileName, { type: mimeType })

return cy.window()
.then(async win => {
const response = await axios.put(`${url}/remote.php/webdav/${target}`, file, {
return cy.request('/csrftoken')
.then(({ body }) => body.token)
.then(requesttoken => {
return cy.request({
url: `${url}/remote.php/webdav/${target}`,
method: 'put',
body: blob.size > 0 ? blob : '',
auth,
headers: {
requesttoken: win.OC.requestToken,
'Content-Type': mimeType,
requesttoken,
},
}).then((response) => {
return cy.log(`Uploaded ${target}`, response.status)
})

return cy.log(`Uploaded ${fileName}`, response.status)
})

})

Cypress.Commands.add('shareFileToUser', (path, targetUser, shareData = {}) => {
cy.clearCookies()
cy.ocsRequest({
method: 'POST',
url: `${url}/ocs/v2.php/apps/files_sharing/api/v1/shares`,
Expand Down Expand Up @@ -212,7 +222,8 @@ Cypress.Commands.add('createFolder', (target) => {
return cy.request('/csrftoken')
.then(({ body }) => body.token)
.then(requesttoken => {
return axios.request(`${rootPath}/${dirPath}`, {
return cy.request({
url: `${rootPath}/${dirPath}`,
method: 'MKCOL',
auth,
headers: {
Expand Down

0 comments on commit b217d0f

Please sign in to comment.