Skip to content

Commit aac9123

Browse files
committed
test(cypress): Add session API tests with non-matching baseVersionEtag
Signed-off-by: Jonas <jonas@freesources.org>
1 parent f3439d7 commit aac9123

3 files changed

Lines changed: 54 additions & 3 deletions

File tree

cypress/e2e/api/SessionApi.spec.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,28 @@ describe('The session Api', function() {
344344
.then(() => connection.close())
345345
})
346346

347+
it('refuses create,push,sync,save with non-matching baseVersionEtag', function() {
348+
cy.failToCreateTextSession(undefined, 'wrongBaseVersionEtag', { filePath: '', shareToken })
349+
.its('status')
350+
.should('eql', 412)
351+
352+
connection.setBaseVersionEtag('wrongBaseVersionEtag')
353+
354+
cy.failToPushSteps({ connection, steps: [messages.update], version })
355+
.its('status')
356+
.should('equal', 412)
357+
358+
cy.failToSyncSteps(connection, { version: 0 })
359+
.its('status')
360+
.should('equal', 412)
361+
362+
cy.failToSave(connection)
363+
.its('status')
364+
.should('equal', 412)
365+
366+
cy.then(() => connection.close())
367+
})
368+
347369
it('recovers session even if last person leaves right after create', function() {
348370
let joining
349371
cy.log('Initial user pushes steps')

cypress/support/sessions.js

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ Cypress.Commands.add('createTextSession', (fileId, options = {}) => {
3636
return api.open({ fileId })
3737
})
3838

39-
Cypress.Commands.add('failToCreateTextSession', (fileId) => {
40-
const api = new SessionApi()
41-
return api.open({ fileId })
39+
Cypress.Commands.add('failToCreateTextSession', (fileId, baseVersionEtag = null, options = {}) => {
40+
const api = new SessionApi(options)
41+
return api.open({ fileId, baseVersionEtag })
4242
.then((response) => {
4343
throw new Error('Expected request to fail - but it succeeded!')
4444
})
@@ -50,16 +50,40 @@ Cypress.Commands.add('pushSteps', ({ connection, steps, version, awareness = ''
5050
.then(response => response.data)
5151
})
5252

53+
Cypress.Commands.add('failToPushSteps', ({ connection, steps, version, awareness = '' }) => {
54+
return connection.push({ steps, version, awareness })
55+
.then((response) => {
56+
throw new Error('Expected request to fail - but it succeeded!')
57+
})
58+
.catch((err) => err.response)
59+
})
60+
5361
Cypress.Commands.add('syncSteps', (connection, options = { version: 0 }) => {
5462
return connection.sync(options)
5563
.then(response => response.data)
5664
})
5765

66+
Cypress.Commands.add('failToSyncSteps', (connection, options = { version: 0 }) => {
67+
return connection.sync(options)
68+
.then((response) => {
69+
throw new Error('Expected request to fail - but it succeeded!')
70+
})
71+
.catch((err) => err.response)
72+
})
73+
5874
Cypress.Commands.add('save', (connection, options = { version: 0 }) => {
5975
return connection.save(options)
6076
.then(response => response.data)
6177
})
6278

79+
Cypress.Commands.add('failToSave', (connection, options = { version: 0 }) => {
80+
return connection.save(options)
81+
.then((response) => {
82+
throw new Error('Expected request to fail - but it succeeded!')
83+
})
84+
.catch((err) => err.response)
85+
})
86+
6387
// Used to test for race conditions between the last push and the close request
6488
Cypress.Commands.add('pushAndClose', ({ connection, steps, version, awareness = '' }) => {
6589
cy.log('Race between push and close')

src/services/SessionApi.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,11 @@ export class Connection {
183183
return promise
184184
}
185185

186+
// To be used in Cypress tests only
187+
setBaseVersionEtag(baseVersionEtag) {
188+
this.#document.baseVersionEtag = baseVersionEtag
189+
}
190+
186191
#post(...args) {
187192
if (this.closed) {
188193
return Promise.reject(new ConnectionClosedError())

0 commit comments

Comments
 (0)