Skip to content

Commit

Permalink
test(cypress): Add session API tests with non-matching baseVersionEtag
Browse files Browse the repository at this point in the history
Signed-off-by: Jonas <jonas@freesources.org>
  • Loading branch information
mejo- committed Mar 20, 2024
1 parent f3439d7 commit aac9123
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 3 deletions.
22 changes: 22 additions & 0 deletions cypress/e2e/api/SessionApi.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
30 changes: 27 additions & 3 deletions cypress/support/sessions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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!')
})
Expand All @@ -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')
Expand Down
5 changes: 5 additions & 0 deletions src/services/SessionApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down

0 comments on commit aac9123

Please sign in to comment.