Skip to content

Commit 212c220

Browse files
authored
Merge pull request #5841 from nextcloud/backport/5378/stable29
[stable29] Cleanup `_oc_webroot` stubs where possible
2 parents 352d2cb + 2a4d984 commit 212c220

File tree

6 files changed

+56
-76
lines changed

6 files changed

+56
-76
lines changed

cypress/e2e/api/SessionApi.spec.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,7 @@ describe('The session Api', function() {
3434

3535
before(function() {
3636
cy.createUser(user)
37-
window.OC = {
38-
config: { modRewriteWorking: false },
39-
webroot: '',
40-
}
41-
window._oc_webroot = ''
37+
cy.prepareWindowForSessionApi()
4238
})
4339

4440
beforeEach(function() {

cypress/e2e/api/SyncServiceProvider.spec.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,7 @@ describe('Sync service provider', function() {
3232

3333
before(function() {
3434
cy.createUser(user)
35-
window.OC = {
36-
config: { modRewriteWorking: false },
37-
}
38-
window._oc_webroot = ''
35+
cy.prepareWindowForSessionApi()
3936
})
4037

4138
beforeEach(function() {

cypress/e2e/api/UsersApi.spec.js

Lines changed: 28 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -30,80 +30,43 @@ describe('The user mention API', function() {
3030

3131
before(function() {
3232
cy.createUser(user)
33-
window.OC = {
34-
config: { modRewriteWorking: false },
35-
}
36-
window._oc_webroot = ''
33+
cy.prepareWindowForSessionApi()
3734
})
3835

39-
let fileId
40-
let requesttoken
41-
4236
beforeEach(function() {
4337
cy.login(user)
44-
cy.prepareSessionApi().then((token) => {
45-
requesttoken = token
46-
cy.uploadTestFile('test.md')
47-
.then(id => {
48-
fileId = id
49-
})
50-
})
38+
cy.uploadTestFile('test.md').as('fileId')
39+
.then(cy.createTextSession).as('connection')
40+
cy.getRequestToken()
5141
})
5242

53-
it('fetches users with valid session', function() {
54-
cy.createTextSession(fileId).then(connection => {
55-
cy.wrap(connection)
56-
.its('document.id')
57-
.should('equal', fileId)
58-
59-
const requestData = {
60-
method: 'POST',
61-
url: '/apps/text/api/v1/users',
62-
body: {
63-
documentId: connection.document.id,
64-
sessionId: connection.session.id,
65-
sessionToken: connection.session.token,
66-
requesttoken,
67-
},
68-
failOnStatusCode: false,
69-
}
70-
const invalidRequestData = { ...requestData }
71-
72-
cy.request(requestData).then(({ status }) => {
73-
expect(status).to.eq(200)
74-
75-
invalidRequestData.body = {
76-
...requestData.body,
77-
sessionToken: 'invalid',
78-
}
79-
})
80-
81-
cy.request(invalidRequestData).then(({ status }) => {
82-
expect(status).to.eq(403)
83-
invalidRequestData.body = {
84-
...requestData.body,
85-
sessionId: 0,
86-
}
87-
})
88-
89-
cy.request(invalidRequestData).then(({ status }) => {
90-
expect(status).to.eq(403)
43+
afterEach(function() {
44+
cy.get('@connection').then(c => c.closed || c.close())
45+
})
9146

92-
invalidRequestData.body = {
93-
...requestData.body,
94-
documentId: 0,
95-
}
96-
})
47+
it('has a valid connection', function() {
48+
cy.get('@connection')
49+
.its('document.id')
50+
.should('equal', this.fileId)
51+
})
9752

98-
cy.request(invalidRequestData).then(({ status }) => {
99-
expect(status).to.eq(403)
100-
})
53+
it('fetches users with valid session', function() {
54+
cy.sessionUsers(this.connection)
55+
.its('status').should('eq', 200)
56+
})
10157

102-
cy.wrap(null).then(() => connection.close())
58+
it('rejects invalid sessions', function() {
59+
cy.sessionUsers(this.connection, { sessionToken: 'invalid' })
60+
.its('status').should('eq', 403)
61+
cy.sessionUsers(this.connection, { sessionId: 0 })
62+
.its('status').should('eq', 403)
63+
cy.sessionUsers(this.connection, { documentId: 0 })
64+
.its('status').should('eq', 403)
65+
})
10366

104-
cy.request(requestData).then(({ status, body }) => {
105-
expect(status).to.eq(403)
106-
})
107-
})
67+
it('rejects closed sessions', function() {
68+
cy.then(() => this.connection.close())
69+
cy.sessionUsers(this.connection)
70+
.its('status').should('eq', 403)
10871
})
10972
})

cypress/support/sessions.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@
2323
import SessionApi from '../../src/services/SessionApi.js'
2424
import { emit } from '@nextcloud/event-bus'
2525

26+
Cypress.Commands.add('prepareWindowForSessionApi', () => {
27+
window.OC = {
28+
config: { modRewriteWorking: false },
29+
}
30+
// Prevent @nextcloud/router from reading window.location
31+
window._oc_webroot = ''
32+
})
33+
2634
Cypress.Commands.add('prepareSessionApi', () => {
2735
return cy.request('/csrftoken')
2836
.then(({ body }) => {
@@ -80,6 +88,22 @@ Cypress.Commands.add('failToSave', (connection, options = { version: 0 }) => {
8088
}, (err) => err.response)
8189
})
8290

91+
Cypress.Commands.add('sessionUsers', function(connection, bodyOptions = {}) {
92+
const body = {
93+
documentId: connection.document.id,
94+
sessionId: connection.session.id,
95+
sessionToken: connection.session.token,
96+
requesttoken: this.requesttoken,
97+
...bodyOptions,
98+
}
99+
cy.request({
100+
method: 'POST',
101+
url: '/apps/text/api/v1/users',
102+
body,
103+
failOnStatusCode: false,
104+
})
105+
})
106+
83107
// Used to test for race conditions between the last push and the close request
84108
Cypress.Commands.add('pushAndClose', ({ connection, steps, version, awareness = '' }) => {
85109
cy.log('Race between push and close')

src/tests/services/AttachmentResolver.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ describe('Image resolver', () => {
8787
const resolver = new AttachmentResolver({ fileId, user, currentDirectory })
8888
const attachment = await resolver.resolve(src)
8989
expect(attachment.isImage).toBe(true)
90-
expect(attachment.previewUrl).toBe('http://localhost/nc-webroot/remote.php/dav/files/user-uid/parentDir/path/to/some%20image.png')
90+
expect(attachment.previewUrl).toBe('http://localhost/remote.php/dav/files/user-uid/parentDir/path/to/some%20image.png')
9191
})
9292

9393
})

src/tests/setup.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ global.OC = {
7373
}
7474
}
7575

76-
global._oc_webroot = '/nc-webroot'
7776
global.OCA = {}
77+
global._oc_webroot = ''
7878

7979

8080
Vue.prototype.t = global.t

0 commit comments

Comments
 (0)