Skip to content

Commit d264a64

Browse files
Merge pull request #7446 from nextcloud/refactor/upload-with-separate-api
Refactor: use separate apis for polling and attachment handling
2 parents 408f7b1 + 889dd69 commit d264a64

18 files changed

Lines changed: 317 additions & 250 deletions

File tree

cypress/e2e/api/SessionApi.spec.js

Lines changed: 49 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -38,20 +38,20 @@ describe('The session Api', function() {
3838
})
3939

4040
it('returns connection', function() {
41-
cy.createTextSession(fileId).then(connection => {
41+
cy.openConnection({ fileId }).then(({ connection }) => {
4242
cy.wrap(connection)
43-
.its('document.id')
43+
.its('documentId')
4444
.should('equal', fileId)
45-
cy.destroySession(connection)
45+
cy.closeConnection(connection)
4646
})
4747
})
4848

4949
it('provides initial content', function() {
50-
cy.createTextSession(fileId, { filePath }).then(connection => {
51-
cy.wrap(connection)
52-
.its('state.documentSource')
50+
cy.openConnection({fileId, filePath }).then(({ connection, data }) => {
51+
cy.wrap(data)
52+
.its('content')
5353
.should('eql', '## Hello world\n')
54-
cy.destroySession(connection)
54+
cy.closeConnection(connection)
5555
})
5656
})
5757

@@ -74,14 +74,14 @@ describe('The session Api', function() {
7474

7575
beforeEach(function() {
7676
cy.uploadTestFile()
77-
.then(cy.createTextSession)
78-
.then(con => {
77+
.then((fileId) => cy.openConnection({ fileId }))
78+
.then(({ connection: con }) => {
7979
connection = con
8080
})
8181
})
8282

8383
afterEach(function() {
84-
cy.destroySession(connection)
84+
cy.closeConnection(connection)
8585
})
8686

8787
// Echoes all message types but queries
@@ -135,9 +135,9 @@ describe('The session Api', function() {
135135
cy.uploadTestFile()
136136
.then(id => {
137137
fileId = id
138-
return cy.createTextSession(fileId, { filePath })
138+
return cy.openConnection({ fileId, filePath })
139139
})
140-
.then(con => {
140+
.then(({ connection: con }) => {
141141
connection = con
142142
})
143143
})
@@ -169,18 +169,18 @@ describe('The session Api', function() {
169169
documentState,
170170
manualSave: true,
171171
})
172-
cy.createTextSession(fileId, { filePath })
173-
.then(con => {
172+
cy.openConnection({ fileId, filePath })
173+
.then(({ connection: con, data }) => {
174174
joining = con
175-
return joining
175+
return data
176176
})
177-
.its('state.documentState')
177+
.its('documentState')
178178
.should('eql', documentState)
179-
.then(() => joining.close())
179+
cy.closeConnection(joining)
180180
})
181181

182182
afterEach(function() {
183-
cy.destroySession(connection)
183+
cy.closeConnection(connection)
184184
})
185185
})
186186

@@ -204,15 +204,15 @@ describe('The session Api', function() {
204204
})
205205
.then(() => cy.clearCookies())
206206
.then(() => {
207-
return cy.createTextSession(undefined, { filePath: '', shareToken })
208-
.then(con => {
207+
return cy.openConnection({ filePath: '', token: shareToken })
208+
.then(({ connection: con }) => {
209209
connection = con
210210
})
211211
})
212212
})
213213

214214
afterEach(function() {
215-
cy.destroySession(connection)
215+
cy.closeConnection(connection)
216216
})
217217

218218
it('starts empty public', function() {
@@ -243,14 +243,14 @@ describe('The session Api', function() {
243243
documentState,
244244
manualSave: true,
245245
})
246-
cy.createTextSession(undefined, { filePath: '', shareToken })
247-
.then(con => {
246+
cy.openConnection({ filePath: '', token: shareToken })
247+
.then(({ connection: con, data }) => {
248248
joining = con
249-
return con
249+
return data
250250
})
251-
.its('state.documentState')
251+
.its('documentState')
252252
.should('eql', documentState)
253-
.then(() => joining.close())
253+
cy.closeConnection(joining)
254254
})
255255

256256
})
@@ -269,49 +269,48 @@ describe('The session Api', function() {
269269
cy.log(token)
270270
shareToken = token
271271
cy.clearCookies()
272-
cy.createTextSession(undefined, { filePath: '', shareToken })
273-
.then(con => {
272+
cy.openConnection({ filePath: '', token: shareToken })
273+
.then(({ connection: con }) => {
274274
connection = con
275275
})
276276
})
277277
})
278278

279279
it('does not send initial content if other session is alive but did not push any steps', function() {
280280
let joining
281-
cy.createTextSession(undefined, { filePath: '', shareToken })
282-
.then(con => {
281+
cy.openConnection({ filePath: '', token: shareToken })
282+
.then(({ connection: con, data }) => {
283283
joining = con
284-
return con
284+
return data
285285
})
286-
.its('state.documentSource')
286+
.its('content')
287287
.should('eql', '## Hello world\n')
288-
.then(() => cy.destroySession(joining))
289-
cy.destroySession(connection)
288+
.then(() => cy.closeConnection(joining))
289+
cy.closeConnection(connection)
290290
})
291291

292292
it('does not send initial content if session is alive even without saved state', function() {
293293
let joining
294294
cy.pushSteps({ connection, steps: [messages.update], version })
295295
.its('version')
296296
.should('be.at.least', 1)
297-
cy.createTextSession(undefined, { filePath: '', shareToken })
298-
.then(con => {
297+
cy.openConnection({ filePath: '', token: shareToken })
298+
.then(({ connection: con, data }) => {
299299
joining = con
300-
return con
300+
return data
301301
})
302-
.its('state.documentSource')
302+
.its('content')
303303
.should('eql', '## Hello world\n')
304-
.then(() => cy.destroySession(joining))
305-
cy.destroySession(connection)
304+
.then(() => cy.closeConnection(joining))
305+
cy.closeConnection(connection)
306306
})
307307

308308
it('refuses create,push,sync,save with non-matching baseVersionEtag', function() {
309309
cy.failToCreateTextSession(undefined, 'wrongBaseVersionEtag', { filePath: '', token: shareToken })
310310
.its('status')
311311
.should('eql', 412)
312312

313-
connection.setBaseVersionEtag('wrongBaseVersionEtag')
314-
connection.connection.baseVersionEtag = 'wrongBaseVersionEtag'
313+
connection.baseVersionEtag = 'wrongBaseVersionEtag'
315314

316315
cy.failToPushSteps({ connection, steps: [messages.update], version })
317316
.its('status')
@@ -325,7 +324,7 @@ describe('The session Api', function() {
325324
.its('status')
326325
.should('equal', 412)
327326

328-
cy.destroySession(connection)
327+
cy.closeConnection(connection)
329328
})
330329

331330
it('recovers session even if last person leaves right after create', function() {
@@ -335,12 +334,12 @@ describe('The session Api', function() {
335334
.its('version')
336335
.should('be.at.least', 1)
337336
cy.log('Other user creates session')
338-
cy.createTextSession(undefined, { filePath: '', shareToken })
339-
.then(con => {
337+
cy.openConnection({ filePath: '', token: shareToken })
338+
.then(({ connection: con }) => {
340339
joining = con
341340
})
342341
cy.log('Initial user closes session')
343-
cy.destroySession(connection)
342+
cy.closeConnection(connection)
344343
cy.log('Other user still finds the steps')
345344
.then(() => {
346345
cy.syncSteps(joining, {
@@ -354,11 +353,12 @@ describe('The session Api', function() {
354353
// Skipped for now since the behaviour chanced by not cleaning up the state on close/create
355354
it.skip('ignores steps stored after close cleaned up', function() {
356355
cy.pushAndClose({ connection, steps: [messages.update], version })
357-
cy.createTextSession(undefined, { filePath: '', shareToken })
358-
.then(con => {
356+
cy.openConnection({ filePath: '', token: shareToken })
357+
.then(({ connection: con, data }) => {
359358
connection = con
359+
return data
360360
})
361-
.its('state.documentSource')
361+
.its('content')
362362
.should('eql', '## Hello world\n')
363363
})
364364

cypress/e2e/api/UsersApi.spec.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,22 @@ describe('The user mention API', function() {
1616
beforeEach(function() {
1717
cy.login(user)
1818
cy.uploadTestFile('test.md').as('fileId')
19-
.then(cy.createTextSession).as('connection')
20-
})
21-
22-
afterEach(function() {
23-
cy.get('@connection').then(c => c.closed || c.close())
19+
.then((fileId) => cy.openConnection({ fileId }))
20+
.its('connection')
21+
.as('connection')
2422
})
2523

2624
it('has a valid connection', function() {
2725
cy.get('@connection')
28-
.its('document.id')
26+
.its('documentId')
2927
.should('equal', this.fileId)
28+
cy.closeConnection(this.connection)
3029
})
3130

3231
it('fetches users with valid session', function() {
3332
cy.sessionUsers(this.connection)
3433
.its('status').should('eq', 200)
34+
cy.closeConnection(this.connection)
3535
})
3636

3737
it('rejects invalid sessions', function() {
@@ -41,10 +41,11 @@ describe('The user mention API', function() {
4141
.its('status').should('eq', 403)
4242
cy.sessionUsers(this.connection, { documentId: 0 })
4343
.its('status').should('eq', 403)
44+
cy.closeConnection(this.connection)
4445
})
4546

4647
it('rejects closed sessions', function() {
47-
cy.destroySession(this.connection)
48+
cy.closeConnection(this.connection)
4849
cy.sessionUsers(this.connection)
4950
.its('status').should('eq', 403)
5051
})

cypress/support/sessions.js

Lines changed: 31 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,15 @@
44
*/
55

66
import axios from '@nextcloud/axios'
7-
import { SessionConnection } from '../../src/services/SessionConnection.js'
8-
import { open, close } from '../../src/apis/Connect.ts'
9-
import { push } from '../../src/apis/Sync.ts'
10-
import { save } from '../../src/apis/Save.ts'
7+
import { open, close } from '../../src/apis/connect.ts'
8+
import { push, sync } from '../../src/apis/sync.ts'
9+
import { save } from '../../src/apis/save.ts'
1110

1211
const url = Cypress.config('baseUrl').replace(/\/index.php\/?$/g, '')
1312

14-
Cypress.Commands.add('createTextSession', async (fileId, options = {}) => {
15-
const { connection, data } = await open({ fileId, token: options.shareToken, ...options })
16-
return new SessionConnection(data, connection)
17-
})
13+
Cypress.Commands.add('openConnection', open)
1814

19-
Cypress.Commands.add('destroySession', async (sessionConnection) => {
20-
const { documentId, id, token } = sessionConnection.session
21-
await close({ documentId, sessionId: id, sessionToken: token })
22-
sessionConnection.close()
23-
})
15+
Cypress.Commands.add('closeConnection', close)
2416

2517
Cypress.Commands.add('failToCreateTextSession', (fileId, baseVersionEtag = null, options = {}) => {
2618
return open({ fileId, ...options, baseVersionEtag })
@@ -29,55 +21,48 @@ Cypress.Commands.add('failToCreateTextSession', (fileId, baseVersionEtag = null,
2921
}, (err) => err.response)
3022
})
3123

32-
Cypress.Commands.add('pushSteps', ({ connection: sessionConnection, steps, version, awareness = '' }) => {
33-
return push(
34-
sessionConnection.connection,
35-
{ steps, version, awareness }
36-
).then(response => response.data)
24+
Cypress.Commands.add('pushSteps', ({ connection, steps, version, awareness = '' }) => {
25+
return push(connection, { steps, version, awareness })
26+
.then(response => response.data)
3727
})
3828

39-
Cypress.Commands.add('failToPushSteps', ({ connection: sessionConnection, steps, version, awareness = '' }) => {
40-
return push(
41-
sessionConnection.connection,
42-
{ steps, version, awareness }
43-
).then((_response) => {
44-
throw new Error('Expected request to fail - but it succeeded!')
45-
}, (err) => err.response)
29+
Cypress.Commands.add('failToPushSteps', ({ connection, steps, version, awareness = '' }) => {
30+
return push( connection, { steps, version, awareness })
31+
.then((_response) => {
32+
throw new Error('Expected request to fail - but it succeeded!')
33+
}, (err) => err.response)
4634
})
4735

4836
Cypress.Commands.add('syncSteps', (connection, options = { version: 0 }) => {
49-
return connection.sync(options)
37+
return sync(connection, options)
5038
.then(response => response.data)
5139
})
5240

5341
Cypress.Commands.add('failToSyncSteps', (connection, options = { version: 0 }) => {
54-
return connection.sync(options)
55-
.then((response) => {
42+
return sync(connection, options)
43+
.then((_response) => {
5644
throw new Error('Expected request to fail - but it succeeded!')
5745
}, (err) => err.response)
5846
})
5947

60-
Cypress.Commands.add('save', (sessionConnection, options = { version: 0 }) => {
61-
return save(
62-
sessionConnection.connection,
63-
options
64-
).then(response => response.data)
48+
Cypress.Commands.add('save', (connection, options = { version: 0 }) => {
49+
return save( connection, options)
50+
.then(response => response.data)
6551
})
6652

67-
Cypress.Commands.add('failToSave', (sessionConnection, options = { version: 0 }) => {
68-
return save(
69-
sessionConnection.connection,
70-
options
71-
).then((response) => {
72-
throw new Error('Expected request to fail - but it succeeded!')
73-
}, (err) => err.response)
53+
Cypress.Commands.add('failToSave', (connection, options = { version: 0 }) => {
54+
return save( connection, options)
55+
.then((_response) => {
56+
throw new Error('Expected request to fail - but it succeeded!')
57+
}, (err) => err.response)
7458
})
7559

7660
Cypress.Commands.add('sessionUsers', function(connection, bodyOptions = {}) {
61+
const { documentId, sessionId, sessionToken } = connection
7762
const data = {
78-
documentId: connection.document.id,
79-
sessionId: connection.session.id,
80-
sessionToken: connection.session.token,
63+
documentId,
64+
sessionId,
65+
sessionToken,
8166
requesttoken: this.requesttoken,
8267
...bodyOptions,
8368
}
@@ -93,14 +78,14 @@ Cypress.Commands.add('sessionUsers', function(connection, bodyOptions = {}) {
9378
Cypress.Commands.add('pushAndClose', ({ connection, steps, version, awareness = '' }) => {
9479
cy.log('Race between push and close')
9580
.then(() => {
96-
const push = connection.push({ steps, version, awareness })
81+
const pushed = push(connection, { steps, version, awareness })
9782
.catch(error => {
9883
// handle 403 gracefully
9984
if (error.response?.status !== 403) {
10085
throw error
10186
}
10287
})
103-
const close = connection.close()
104-
return Promise.all([push, close])
88+
const closed = close(connection)
89+
return Promise.all([pushed, closed])
10590
})
10691
})

0 commit comments

Comments
 (0)