Skip to content

Commit

Permalink
Merge pull request #5841 from nextcloud/backport/5378/stable29
Browse files Browse the repository at this point in the history
[stable29] Cleanup `_oc_webroot` stubs where possible
  • Loading branch information
mejo- committed May 28, 2024
2 parents 352d2cb + 2a4d984 commit 212c220
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 76 deletions.
6 changes: 1 addition & 5 deletions cypress/e2e/api/SessionApi.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,7 @@ describe('The session Api', function() {

before(function() {
cy.createUser(user)
window.OC = {
config: { modRewriteWorking: false },
webroot: '',
}
window._oc_webroot = ''
cy.prepareWindowForSessionApi()
})

beforeEach(function() {
Expand Down
5 changes: 1 addition & 4 deletions cypress/e2e/api/SyncServiceProvider.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,7 @@ describe('Sync service provider', function() {

before(function() {
cy.createUser(user)
window.OC = {
config: { modRewriteWorking: false },
}
window._oc_webroot = ''
cy.prepareWindowForSessionApi()
})

beforeEach(function() {
Expand Down
93 changes: 28 additions & 65 deletions cypress/e2e/api/UsersApi.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,80 +30,43 @@ describe('The user mention API', function() {

before(function() {
cy.createUser(user)
window.OC = {
config: { modRewriteWorking: false },
}
window._oc_webroot = ''
cy.prepareWindowForSessionApi()
})

let fileId
let requesttoken

beforeEach(function() {
cy.login(user)
cy.prepareSessionApi().then((token) => {
requesttoken = token
cy.uploadTestFile('test.md')
.then(id => {
fileId = id
})
})
cy.uploadTestFile('test.md').as('fileId')
.then(cy.createTextSession).as('connection')
cy.getRequestToken()
})

it('fetches users with valid session', function() {
cy.createTextSession(fileId).then(connection => {
cy.wrap(connection)
.its('document.id')
.should('equal', fileId)

const requestData = {
method: 'POST',
url: '/apps/text/api/v1/users',
body: {
documentId: connection.document.id,
sessionId: connection.session.id,
sessionToken: connection.session.token,
requesttoken,
},
failOnStatusCode: false,
}
const invalidRequestData = { ...requestData }

cy.request(requestData).then(({ status }) => {
expect(status).to.eq(200)

invalidRequestData.body = {
...requestData.body,
sessionToken: 'invalid',
}
})

cy.request(invalidRequestData).then(({ status }) => {
expect(status).to.eq(403)
invalidRequestData.body = {
...requestData.body,
sessionId: 0,
}
})

cy.request(invalidRequestData).then(({ status }) => {
expect(status).to.eq(403)
afterEach(function() {
cy.get('@connection').then(c => c.closed || c.close())
})

invalidRequestData.body = {
...requestData.body,
documentId: 0,
}
})
it('has a valid connection', function() {
cy.get('@connection')
.its('document.id')
.should('equal', this.fileId)
})

cy.request(invalidRequestData).then(({ status }) => {
expect(status).to.eq(403)
})
it('fetches users with valid session', function() {
cy.sessionUsers(this.connection)
.its('status').should('eq', 200)
})

cy.wrap(null).then(() => connection.close())
it('rejects invalid sessions', function() {
cy.sessionUsers(this.connection, { sessionToken: 'invalid' })
.its('status').should('eq', 403)
cy.sessionUsers(this.connection, { sessionId: 0 })
.its('status').should('eq', 403)
cy.sessionUsers(this.connection, { documentId: 0 })
.its('status').should('eq', 403)
})

cy.request(requestData).then(({ status, body }) => {
expect(status).to.eq(403)
})
})
it('rejects closed sessions', function() {
cy.then(() => this.connection.close())
cy.sessionUsers(this.connection)
.its('status').should('eq', 403)
})
})
24 changes: 24 additions & 0 deletions cypress/support/sessions.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@
import SessionApi from '../../src/services/SessionApi.js'
import { emit } from '@nextcloud/event-bus'

Cypress.Commands.add('prepareWindowForSessionApi', () => {
window.OC = {
config: { modRewriteWorking: false },
}
// Prevent @nextcloud/router from reading window.location
window._oc_webroot = ''
})

Cypress.Commands.add('prepareSessionApi', () => {
return cy.request('/csrftoken')
.then(({ body }) => {
Expand Down Expand Up @@ -80,6 +88,22 @@ Cypress.Commands.add('failToSave', (connection, options = { version: 0 }) => {
}, (err) => err.response)
})

Cypress.Commands.add('sessionUsers', function(connection, bodyOptions = {}) {
const body = {
documentId: connection.document.id,
sessionId: connection.session.id,
sessionToken: connection.session.token,
requesttoken: this.requesttoken,
...bodyOptions,
}
cy.request({
method: 'POST',
url: '/apps/text/api/v1/users',
body,
failOnStatusCode: false,
})
})

// 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
2 changes: 1 addition & 1 deletion src/tests/services/AttachmentResolver.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ describe('Image resolver', () => {
const resolver = new AttachmentResolver({ fileId, user, currentDirectory })
const attachment = await resolver.resolve(src)
expect(attachment.isImage).toBe(true)
expect(attachment.previewUrl).toBe('http://localhost/nc-webroot/remote.php/dav/files/user-uid/parentDir/path/to/some%20image.png')
expect(attachment.previewUrl).toBe('http://localhost/remote.php/dav/files/user-uid/parentDir/path/to/some%20image.png')
})

})
2 changes: 1 addition & 1 deletion src/tests/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ global.OC = {
}
}

global._oc_webroot = '/nc-webroot'
global.OCA = {}
global._oc_webroot = ''


Vue.prototype.t = global.t
Expand Down

0 comments on commit 212c220

Please sign in to comment.