From 05fa8134d4cc06aab23381efdc3bf09db149ade1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julius=20H=C3=A4rtl?= Date: Sun, 31 Mar 2024 18:30:49 +0200 Subject: [PATCH] ci: Make cypress test more stable by closing connections MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Julius Härtl --- cypress/e2e/initial.spec.js | 24 +++++++++++++++--------- cypress/support/commands.js | 30 ++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 9 deletions(-) diff --git a/cypress/e2e/initial.spec.js b/cypress/e2e/initial.spec.js index e9cc0fb7072..c9372c22ff7 100644 --- a/cypress/e2e/initial.spec.js +++ b/cypress/e2e/initial.spec.js @@ -35,9 +35,6 @@ describe('Test state loading of documents', function() { }) beforeEach(function() { cy.login(user) - cy.intercept({ method: 'POST', url: '**/session/*/push' }).as('push') - cy.intercept({ method: 'POST', url: '**/session/*/sync' }).as('sync') - cy.intercept({ method: 'POST', url: '**/session/*/save' }).as('save') }) it('Initial content can not be undone', function() { @@ -62,11 +59,13 @@ describe('Test state loading of documents', function() { it('Consecutive sessions work properly', function() { let readToken = null let writeToken = null + cy.interceptCreate() cy.shareFile('/test2.md') .then((token) => { readToken = token cy.logout() cy.visit(`/s/${readToken}`) + cy.wait('@create') }) .then(() => { // Open read only for the first time @@ -74,6 +73,7 @@ describe('Test state loading of documents', function() { cy.getContent() .should('contain', 'Hello world') .find('h2').should('contain', 'Hello world') + cy.closeInterceptedSession(readToken) // Open read only for the second time cy.reload() @@ -81,6 +81,7 @@ describe('Test state loading of documents', function() { cy.getContent() .should('contain', 'Hello world') .find('h2').should('contain', 'Hello world') + cy.closeInterceptedSession(readToken) cy.login(user) cy.shareFile('/test2.md', { edit: true }) @@ -94,15 +95,17 @@ describe('Test state loading of documents', function() { .find('h2').should('contain', 'Hello world') cy.getContent() .type('Something new {end}') + cy.intercept({ method: 'POST', url: '**/session/*/push' }).as('push') + cy.intercept({ method: 'POST', url: '**/session/*/sync' }).as('sync') cy.wait('@push') cy.wait('@sync') + cy.closeInterceptedSession(writeToken) // Reopen read only link and check if changes are there cy.visit(`/s/${readToken}`) cy.getEditor().should('be.visible') cy.getContent() .find('h2').should('contain', 'Something new Hello world') - }) }) }) @@ -110,6 +113,7 @@ describe('Test state loading of documents', function() { it('Load after state has been saved', function() { let readToken = null let writeToken = null + cy.interceptCreate() cy.shareFile('/test3.md', { edit: true }) .then((token) => { writeToken = token @@ -117,17 +121,19 @@ describe('Test state loading of documents', function() { cy.visit(`/s/${writeToken}`) }) .then(() => { - // Open read only for the first time + // Open a file, write and save cy.getEditor().should('be.visible') cy.getContent() .should('contain', 'Hello world') .find('h2').should('contain', 'Hello world') cy.getContent() .type('Something new {end}') + cy.intercept({ method: 'POST', url: '**/session/*/save' }).as('save') cy.get('.save-status button').click() - cy.wait('@save') + cy.wait('@save', { timeout: 10000 }) + cy.closeInterceptedSession(writeToken) - // Open read only for the second time + // Open writable file again and assert the content cy.reload() cy.getEditor().should('be.visible') cy.getContent() @@ -142,11 +148,11 @@ describe('Test state loading of documents', function() { cy.visit(`/s/${readToken}`) }) .then(() => { - // Open read only for the first time + // Open read only file again and assert the content cy.getEditor().should('be.visible') cy.getContent() .should('contain', 'Hello world') - .find('h2').should('contain', 'Hello world') + .find('h2').should('contain', 'Something new Hello world') }) }) }) diff --git a/cypress/support/commands.js b/cypress/support/commands.js index df0842bec74..13a97adf176 100644 --- a/cypress/support/commands.js +++ b/cypress/support/commands.js @@ -316,6 +316,36 @@ Cypress.Commands.add('closeFile', (fileName, params = {}) => { cy.wait('@close', { timeout: 7000 }) }) +let closeData = null +Cypress.Commands.add('interceptCreate', () => { + return cy.intercept({ method: 'PUT', url: '**/session/*/create' }, (req) => { + closeData = { + url: ('' + req.url).replace('create', 'close'), + } + req.continue((res) => { + closeData = { + ...closeData, + ...res.body, + } + }) + }).as('create') +}) + +Cypress.Commands.add('closeInterceptedSession', (shareToken = undefined) => { + return cy.window().then(win => { + return axios.post( + closeData.url, + { + documentId: closeData.session.documentId, + sessionId: closeData.session.id, + sessionToken: closeData.session.token, + token: shareToken, + }, + { headers: { requesttoken: win.OC.requestToken } }, + ) + }) +}) + Cypress.Commands.add('getFile', fileName => { return cy.get(`.files-fileList tr[data-file="${fileName}"]`) })