diff --git a/cypress/e2e/api/SessionApi.spec.js b/cypress/e2e/api/SessionApi.spec.js index 0f4eaaf46dd..40665aa75e0 100644 --- a/cypress/e2e/api/SessionApi.spec.js +++ b/cypress/e2e/api/SessionApi.spec.js @@ -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() { diff --git a/cypress/e2e/api/SyncServiceProvider.spec.js b/cypress/e2e/api/SyncServiceProvider.spec.js index aebb89c2e14..545aaff47b2 100644 --- a/cypress/e2e/api/SyncServiceProvider.spec.js +++ b/cypress/e2e/api/SyncServiceProvider.spec.js @@ -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() { diff --git a/cypress/e2e/api/UsersApi.spec.js b/cypress/e2e/api/UsersApi.spec.js index ea85a3e669b..9b21899e3fc 100644 --- a/cypress/e2e/api/UsersApi.spec.js +++ b/cypress/e2e/api/UsersApi.spec.js @@ -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) }) }) diff --git a/cypress/support/sessions.js b/cypress/support/sessions.js index 8f598887481..fc573c87f00 100644 --- a/cypress/support/sessions.js +++ b/cypress/support/sessions.js @@ -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 }) => { @@ -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') diff --git a/src/tests/services/AttachmentResolver.spec.js b/src/tests/services/AttachmentResolver.spec.js index 5368394cd50..24de3e41f4e 100644 --- a/src/tests/services/AttachmentResolver.spec.js +++ b/src/tests/services/AttachmentResolver.spec.js @@ -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') }) }) diff --git a/src/tests/setup.js b/src/tests/setup.js index be43ce44bbf..aadca63d83b 100644 --- a/src/tests/setup.js +++ b/src/tests/setup.js @@ -73,8 +73,8 @@ global.OC = { } } -global._oc_webroot = '/nc-webroot' global.OCA = {} +global._oc_webroot = '' Vue.prototype.t = global.t