Commit dd0eac00 authored by Bogdan Cochior's avatar Bogdan Cochior

refactor(tests): add upload file via API

parent 342382df
......@@ -54,6 +54,36 @@ Cypress.Commands.add(
},
)
Cypress.Commands.add('form_request', (url, formData, token) =>
cy
.server()
.route('POST', url)
.as('form_request')
.window()
.then(win => {
const xhr = new win.XMLHttpRequest()
xhr.open('POST', url)
xhr.setRequestHeader('Accept', `text/plain`)
xhr.setRequestHeader('Authorization', `Bearer ${token}`)
xhr.send(formData)
})
.wait('@form_request'),
)
Cypress.Commands.add(
'uploadFileAPI',
({ fragmentId, fileType = 'manuscripts', token }) => {
const data = new FormData()
const file = new File(['some text'], 'filename.pdf', { type: 'text/plain' })
data.append('fileType', fileType)
data.append('fragmentId', fragmentId)
data.append('file', file)
data.append('newName', 'filename.pdf')
cy.form_request('/api/files', data, token).then(res => res.response.body)
},
)
Cypress.Commands.add('loginApi', (user, pwd) => {
const username = user === 'admin' ? user : Cypress.env('email') + user
cy
......@@ -100,6 +130,7 @@ Cypress.Commands.add(
) => {
let fragmentID = null
let collectionID = null
let fragment = {}
cy.loginApi(author.email, author.password).then(userToken => {
cy
......@@ -149,8 +180,8 @@ Cypress.Commands.add(
})
})
.then(response => {
const { id } = response.body
fragmentID = id
fragment = response.body
fragmentID = fragment.id
Cypress.env('fragmentIdV1', fragmentID)
return fragmentID
})
......@@ -173,9 +204,29 @@ Cypress.Commands.add(
country: 'RO',
},
})
Cypress.env('fragmentId', fragmentID)
})
// .then(() => cy.uploadFile({ fragmentId: fragmentID, token: userToken }))
.then(() =>
cy.uploadFileAPI({ fragmentId: fragmentID, token: userToken }),
)
.then(file => {
cy.request({
method: 'PATCH',
url: `/api/collections/${collectionID}/fragments/${fragmentID}`,
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${userToken}`,
},
body: {
...fragment,
files: {
...fragment.files,
manuscripts: [file],
supplementary: [file],
coverLetter: [file],
},
},
})
})
.then(() => {
cy.request({
method: 'POST',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment