Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
test(cypress): Add session API tests with non-matching baseVersionEtag
Signed-off-by: Jonas <[email protected]>
  • Loading branch information
mejo- committed Mar 20, 2024
commit fc604e236f7d4dd316ff276aa0f5965433409d02
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: 25 additions & 5 deletions cypress/support/sessions.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,30 +36,50 @@ 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!')
})
.catch((err) => err.response)
}, (err) => err.response)
})

Cypress.Commands.add('pushSteps', ({ connection, steps, version, awareness = '' }) => {
return connection.push({ 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!')
}, (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!')
}, (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!')
}, (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