2020-12-16 17:07:09 -05:00
|
|
|
/*
|
2024-03-23 20:02:26 -04:00
|
|
|
* SPDX-FileCopyrightText: 2024 The HedgeDoc developers (see AUTHORS file)
|
2020-12-16 17:07:09 -05:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
2024-06-12 12:45:49 -04:00
|
|
|
const fakeUuid = '77fdcf1c-35fa-4a65-bdcf-1c35fa8a65d5'
|
2020-12-16 17:07:09 -05:00
|
|
|
|
2021-01-11 17:22:11 -05:00
|
|
|
describe('File upload', () => {
|
2020-12-16 17:07:09 -05:00
|
|
|
beforeEach(() => {
|
2022-01-30 15:46:43 -05:00
|
|
|
cy.visitTestNote()
|
2022-02-05 13:32:58 -05:00
|
|
|
cy.fixture('demo.png').as('demoImage')
|
2020-12-16 17:07:09 -05:00
|
|
|
})
|
|
|
|
|
2021-11-19 12:04:04 -05:00
|
|
|
describe('works', () => {
|
2020-12-16 17:07:09 -05:00
|
|
|
beforeEach(() => {
|
2021-11-19 12:04:04 -05:00
|
|
|
cy.intercept(
|
|
|
|
{
|
2022-04-15 17:03:15 -04:00
|
|
|
method: 'POST',
|
2023-08-12 14:07:38 -04:00
|
|
|
url: '/api/private/media'
|
2021-11-19 12:04:04 -05:00
|
|
|
},
|
|
|
|
{
|
2022-04-15 17:03:15 -04:00
|
|
|
statusCode: 201,
|
2021-11-19 12:04:04 -05:00
|
|
|
body: {
|
2024-06-12 12:45:49 -04:00
|
|
|
uuid: fakeUuid,
|
|
|
|
fileName: 'demo.png'
|
2021-11-19 12:04:04 -05:00
|
|
|
}
|
2020-12-16 17:07:09 -05:00
|
|
|
}
|
2021-11-19 12:04:04 -05:00
|
|
|
)
|
2020-12-16 17:07:09 -05:00
|
|
|
})
|
|
|
|
it('via button', () => {
|
2022-06-07 19:10:49 -04:00
|
|
|
cy.getByCypressId('editor-pane').should('have.attr', 'data-cypress-editor-ready', 'true')
|
2023-05-31 14:14:37 -04:00
|
|
|
cy.getByCypressId('toolbar.uploadImage').should('be.visible')
|
|
|
|
cy.getByCypressId('toolbar.uploadImage.input').selectFile(
|
2022-02-05 13:32:58 -05:00
|
|
|
{
|
|
|
|
contents: '@demoImage',
|
|
|
|
fileName: 'demo.png',
|
|
|
|
mimeType: 'image/png'
|
|
|
|
},
|
|
|
|
{ force: true }
|
|
|
|
)
|
2024-06-12 12:45:49 -04:00
|
|
|
cy.get('.cm-line').contains(`![demo.png](http://127.0.0.1:3001/media/${fakeUuid})`)
|
2020-12-16 17:07:09 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
it('via paste', () => {
|
2022-06-07 19:10:49 -04:00
|
|
|
cy.getByCypressId('editor-pane').should('have.attr', 'data-cypress-editor-ready', 'true')
|
2021-11-19 12:04:04 -05:00
|
|
|
cy.fixture('demo.png').then((image: string) => {
|
|
|
|
const pasteEvent = {
|
|
|
|
clipboardData: {
|
|
|
|
files: [Cypress.Blob.base64StringToBlob(image, 'image/png')],
|
2022-06-07 19:10:49 -04:00
|
|
|
getData: () => ''
|
2021-01-06 07:09:33 -05:00
|
|
|
}
|
2021-11-19 12:04:04 -05:00
|
|
|
}
|
2022-02-13 06:14:01 -05:00
|
|
|
cy.get('.cm-content').trigger('paste', pasteEvent)
|
2024-06-12 12:45:49 -04:00
|
|
|
cy.get('.cm-line').contains(`![](http://127.0.0.1:3001/media/${fakeUuid})`)
|
2021-11-19 12:04:04 -05:00
|
|
|
})
|
2020-12-16 17:07:09 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
it('via drag and drop', () => {
|
2022-06-07 19:10:49 -04:00
|
|
|
cy.getByCypressId('editor-pane').should('have.attr', 'data-cypress-editor-ready', 'true')
|
2022-02-13 06:14:01 -05:00
|
|
|
cy.get('.cm-content').selectFile(
|
2022-02-05 13:32:58 -05:00
|
|
|
{
|
|
|
|
contents: '@demoImage',
|
|
|
|
fileName: 'demo.png',
|
|
|
|
mimeType: 'image/png'
|
|
|
|
},
|
|
|
|
{ action: 'drag-drop', force: true }
|
|
|
|
)
|
2024-06-12 12:45:49 -04:00
|
|
|
cy.get('.cm-line').contains(`![demo.png](http://127.0.0.1:3001/media/${fakeUuid})`)
|
2020-12-16 17:07:09 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-11-19 12:04:04 -05:00
|
|
|
it('fails', () => {
|
2022-06-07 19:10:49 -04:00
|
|
|
cy.getByCypressId('editor-pane').should('have.attr', 'data-cypress-editor-ready', 'true')
|
2021-11-19 12:04:04 -05:00
|
|
|
cy.intercept(
|
|
|
|
{
|
2022-04-15 17:03:15 -04:00
|
|
|
method: 'POST',
|
2023-08-12 14:07:38 -04:00
|
|
|
url: '/api/private/media'
|
2021-11-19 12:04:04 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
statusCode: 400
|
|
|
|
}
|
|
|
|
)
|
2023-05-31 14:14:37 -04:00
|
|
|
cy.getByCypressId('toolbar.uploadImage').should('be.visible')
|
|
|
|
cy.getByCypressId('toolbar.uploadImage.input').selectFile(
|
2022-02-05 13:32:58 -05:00
|
|
|
{
|
|
|
|
contents: '@demoImage',
|
|
|
|
fileName: 'demo.png',
|
|
|
|
mimeType: 'image/png'
|
|
|
|
},
|
|
|
|
{ force: true }
|
|
|
|
)
|
2022-02-13 06:14:01 -05:00
|
|
|
cy.get('.cm-line').contains('![upload of demo.png failed]()')
|
2020-12-16 17:07:09 -05:00
|
|
|
})
|
2020-12-21 05:58:43 -05:00
|
|
|
|
2021-11-19 12:04:04 -05:00
|
|
|
it('lets text paste still work', () => {
|
2022-06-07 19:10:49 -04:00
|
|
|
cy.getByCypressId('editor-pane').should('have.attr', 'data-cypress-editor-ready', 'true')
|
2020-12-21 05:58:43 -05:00
|
|
|
const testText = 'a long test text'
|
2022-06-07 19:10:49 -04:00
|
|
|
|
|
|
|
const pasteEvent: Event = Object.assign(new Event('paste', { bubbles: true, cancelable: true }), {
|
2020-12-21 05:58:43 -05:00
|
|
|
clipboardData: {
|
2022-06-07 19:10:49 -04:00
|
|
|
files: [],
|
|
|
|
getData: () => testText
|
2020-12-21 05:58:43 -05:00
|
|
|
}
|
2022-06-07 19:10:49 -04:00
|
|
|
})
|
|
|
|
|
2022-02-13 06:14:01 -05:00
|
|
|
cy.get('.cm-content').trigger('paste', pasteEvent)
|
|
|
|
cy.get('.cm-line').contains(`${testText}`)
|
2020-12-21 05:58:43 -05:00
|
|
|
})
|
2020-12-16 17:07:09 -05:00
|
|
|
})
|