From aac912379f256d1125c5b703112740f60b6cb1f5 Mon Sep 17 00:00:00 2001 From: Jonas Date: Wed, 20 Mar 2024 15:10:57 +0100 Subject: [PATCH] test(cypress): Add session API tests with non-matching baseVersionEtag Signed-off-by: Jonas --- cypress/e2e/api/SessionApi.spec.js | 22 ++++++++++++++++++++++ cypress/support/sessions.js | 30 +++++++++++++++++++++++++++--- src/services/SessionApi.js | 5 +++++ 3 files changed, 54 insertions(+), 3 deletions(-) diff --git a/cypress/e2e/api/SessionApi.spec.js b/cypress/e2e/api/SessionApi.spec.js index 6a102316a71..666737b1ebf 100644 --- a/cypress/e2e/api/SessionApi.spec.js +++ b/cypress/e2e/api/SessionApi.spec.js @@ -344,6 +344,28 @@ describe('The session Api', function() { .then(() => connection.close()) }) + it('refuses create,push,sync,save with non-matching baseVersionEtag', function() { + cy.failToCreateTextSession(undefined, 'wrongBaseVersionEtag', { filePath: '', shareToken }) + .its('status') + .should('eql', 412) + + connection.setBaseVersionEtag('wrongBaseVersionEtag') + + cy.failToPushSteps({ connection, steps: [messages.update], version }) + .its('status') + .should('equal', 412) + + cy.failToSyncSteps(connection, { version: 0 }) + .its('status') + .should('equal', 412) + + cy.failToSave(connection) + .its('status') + .should('equal', 412) + + cy.then(() => connection.close()) + }) + it('recovers session even if last person leaves right after create', function() { let joining cy.log('Initial user pushes steps') diff --git a/cypress/support/sessions.js b/cypress/support/sessions.js index 947185529e5..79e5d327b82 100644 --- a/cypress/support/sessions.js +++ b/cypress/support/sessions.js @@ -36,9 +36,9 @@ Cypress.Commands.add('createTextSession', (fileId, options = {}) => { return api.open({ fileId }) }) -Cypress.Commands.add('failToCreateTextSession', (fileId) => { - const api = new SessionApi() - return api.open({ fileId }) +Cypress.Commands.add('failToCreateTextSession', (fileId, baseVersionEtag = null, options = {}) => { + const api = new SessionApi(options) + return api.open({ fileId, baseVersionEtag }) .then((response) => { throw new Error('Expected request to fail - but it succeeded!') }) @@ -50,16 +50,40 @@ Cypress.Commands.add('pushSteps', ({ connection, steps, version, awareness = '' .then(response => response.data) }) +Cypress.Commands.add('failToPushSteps', ({ connection, steps, version, awareness = '' }) => { + return connection.push({ steps, version, awareness }) + .then((response) => { + throw new Error('Expected request to fail - but it succeeded!') + }) + .catch((err) => err.response) +}) + Cypress.Commands.add('syncSteps', (connection, options = { version: 0 }) => { return connection.sync(options) .then(response => response.data) }) +Cypress.Commands.add('failToSyncSteps', (connection, options = { version: 0 }) => { + return connection.sync(options) + .then((response) => { + throw new Error('Expected request to fail - but it succeeded!') + }) + .catch((err) => err.response) +}) + Cypress.Commands.add('save', (connection, options = { version: 0 }) => { return connection.save(options) .then(response => response.data) }) +Cypress.Commands.add('failToSave', (connection, options = { version: 0 }) => { + return connection.save(options) + .then((response) => { + throw new Error('Expected request to fail - but it succeeded!') + }) + .catch((err) => err.response) +}) + // Used to test for race conditions between the last push and the close request Cypress.Commands.add('pushAndClose', ({ connection, steps, version, awareness = '' }) => { cy.log('Race between push and close') diff --git a/src/services/SessionApi.js b/src/services/SessionApi.js index c8f49e0a77b..b0296b19c8f 100644 --- a/src/services/SessionApi.js +++ b/src/services/SessionApi.js @@ -183,6 +183,11 @@ export class Connection { return promise } + // To be used in Cypress tests only + setBaseVersionEtag(baseVersionEtag) { + this.#document.baseVersionEtag = baseVersionEtag + } + #post(...args) { if (this.closed) { return Promise.reject(new ConnectionClosedError())