From d361fd0dcc2bdda98dd7bc7249ba7507b08926b8 Mon Sep 17 00:00:00 2001 From: Max Date: Sun, 11 Feb 2024 11:59:48 +0100 Subject: [PATCH 1/5] test(jest): remove internal _oc_webroot stub We do not need to test functionality internal to `@nextcloud/router`. Signed-off-by: Max --- src/tests/services/AttachmentResolver.spec.js | 2 +- src/tests/setup.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) 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 From f29544683ebc5445f87131558b30f869c9372db7 Mon Sep 17 00:00:00 2001 From: Max Date: Sun, 11 Feb 2024 12:11:08 +0100 Subject: [PATCH 2/5] test(cy): introduce `cy.prepareWindowForSessionApi` * Prevent @nextcloud/router from reading `window.location.pathname` which would include `cypress/_iframe`. * Keep the `window.OC` stub for now for easier backports. Tests pass without it these days. Signed-off-by: Max --- cypress/e2e/api/SessionApi.spec.js | 6 +----- cypress/e2e/api/SyncServiceProvider.spec.js | 5 +---- cypress/e2e/api/UsersApi.spec.js | 5 +---- cypress/support/sessions.js | 8 ++++++++ 4 files changed, 11 insertions(+), 13 deletions(-) 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..9e68fcedd74 100644 --- a/cypress/e2e/api/UsersApi.spec.js +++ b/cypress/e2e/api/UsersApi.spec.js @@ -30,10 +30,7 @@ describe('The user mention API', function() { before(function() { cy.createUser(user) - window.OC = { - config: { modRewriteWorking: false }, - } - window._oc_webroot = '' + cy.prepareWindowForSessionApi() }) let fileId diff --git a/cypress/support/sessions.js b/cypress/support/sessions.js index 8f598887481..03d963e8ac5 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 }) => { From 169b80302673cbe7c9938837fa845dc56de69b76 Mon Sep 17 00:00:00 2001 From: Max Date: Sun, 11 Feb 2024 13:35:28 +0100 Subject: [PATCH 3/5] test(cy): refactor to use aliases Signed-off-by: Max --- cypress/e2e/api/UsersApi.spec.js | 122 +++++++++++++++---------------- 1 file changed, 60 insertions(+), 62 deletions(-) diff --git a/cypress/e2e/api/UsersApi.spec.js b/cypress/e2e/api/UsersApi.spec.js index 9e68fcedd74..91082699ed0 100644 --- a/cypress/e2e/api/UsersApi.spec.js +++ b/cypress/e2e/api/UsersApi.spec.js @@ -33,74 +33,72 @@ describe('The user mention API', function() { 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().as('requesttoken') + }) + + afterEach(function() { + cy.get('@connection').then(c => c.closed || c.close()) + }) + + it('has a valid connection', function() { + cy.get('@connection') + .its('document.id') + .should('equal', this.fileId) }) 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 body = { + documentId: this.connection.document.id, + sessionId: this.connection.session.id, + sessionToken: this.connection.session.token, + requesttoken: this.requesttoken, + } + const requestData = { + method: 'POST', + url: '/apps/text/api/v1/users', + body, + 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, } - 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) - - invalidRequestData.body = { - ...requestData.body, - documentId: 0, - } - }) - - cy.request(invalidRequestData).then(({ status }) => { - expect(status).to.eq(403) - }) - - cy.wrap(null).then(() => connection.close()) - - cy.request(requestData).then(({ status, body }) => { - expect(status).to.eq(403) - }) + }) + + cy.request(invalidRequestData).then(({ status }) => { + expect(status).to.eq(403) + + invalidRequestData.body = { + ...requestData.body, + documentId: 0, + } + }) + + cy.request(invalidRequestData).then(({ status }) => { + expect(status).to.eq(403) + }) + + cy.then(() => this.connection.close()) + + cy.request(requestData).then(({ status, body }) => { + expect(status).to.eq(403) }) }) }) From 615c4b34848dd0de96a25a28c1e61571e9883db1 Mon Sep 17 00:00:00 2001 From: Max Date: Sun, 11 Feb 2024 13:51:42 +0100 Subject: [PATCH 4/5] test(cy): introduce `cy.sessionUsers()` for users endpoint Signed-off-by: Max --- cypress/e2e/api/UsersApi.spec.js | 42 +++++++------------------------- cypress/support/sessions.js | 16 ++++++++++++ 2 files changed, 25 insertions(+), 33 deletions(-) diff --git a/cypress/e2e/api/UsersApi.spec.js b/cypress/e2e/api/UsersApi.spec.js index 91082699ed0..e34bd7c9320 100644 --- a/cypress/e2e/api/UsersApi.spec.js +++ b/cypress/e2e/api/UsersApi.spec.js @@ -37,7 +37,7 @@ describe('The user mention API', function() { cy.login(user) cy.uploadTestFile('test.md').as('fileId') .then(cy.createTextSession).as('connection') - cy.getRequestToken().as('requesttoken') + cy.getRequestToken() }) afterEach(function() { @@ -51,53 +51,29 @@ describe('The user mention API', function() { }) it('fetches users with valid session', function() { - const body = { - documentId: this.connection.document.id, - sessionId: this.connection.session.id, - sessionToken: this.connection.session.token, - requesttoken: this.requesttoken, - } - const requestData = { - method: 'POST', - url: '/apps/text/api/v1/users', - body, - failOnStatusCode: false, - } - const invalidRequestData = { ...requestData } - cy.request(requestData).then(({ status }) => { + cy.sessionUsers(this.connection).then(({ status }) => { expect(status).to.eq(200) - - invalidRequestData.body = { - ...requestData.body, - sessionToken: 'invalid', - } }) - cy.request(invalidRequestData).then(({ status }) => { + cy.sessionUsers(this.connection, { sessionToken: 'invalid' }) + .then(({ status }) => { expect(status).to.eq(403) - invalidRequestData.body = { - ...requestData.body, - sessionId: 0, - } }) - cy.request(invalidRequestData).then(({ status }) => { + cy.sessionUsers(this.connection, { sessionId: 0 }) + .then(({ status }) => { expect(status).to.eq(403) - - invalidRequestData.body = { - ...requestData.body, - documentId: 0, - } }) - cy.request(invalidRequestData).then(({ status }) => { + cy.sessionUsers(this.connection, { documentId: 0 }) + .then(({ status }) => { expect(status).to.eq(403) }) cy.then(() => this.connection.close()) - cy.request(requestData).then(({ status, body }) => { + cy.sessionUsers(this.connection).then(({ status }) => { expect(status).to.eq(403) }) }) diff --git a/cypress/support/sessions.js b/cypress/support/sessions.js index 03d963e8ac5..fc573c87f00 100644 --- a/cypress/support/sessions.js +++ b/cypress/support/sessions.js @@ -88,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') From 2a4d9845200c7a4d815adaed04c3ed50beb88598 Mon Sep 17 00:00:00 2001 From: Max Date: Sun, 11 Feb 2024 13:59:14 +0100 Subject: [PATCH 5/5] test(api): split test and use `.its` and `.should` Signed-off-by: Max --- cypress/e2e/api/UsersApi.spec.js | 30 +++++++++++------------------- 1 file changed, 11 insertions(+), 19 deletions(-) diff --git a/cypress/e2e/api/UsersApi.spec.js b/cypress/e2e/api/UsersApi.spec.js index e34bd7c9320..9b21899e3fc 100644 --- a/cypress/e2e/api/UsersApi.spec.js +++ b/cypress/e2e/api/UsersApi.spec.js @@ -51,30 +51,22 @@ describe('The user mention API', function() { }) it('fetches users with valid session', function() { + cy.sessionUsers(this.connection) + .its('status').should('eq', 200) + }) - cy.sessionUsers(this.connection).then(({ status }) => { - expect(status).to.eq(200) - }) - + it('rejects invalid sessions', function() { cy.sessionUsers(this.connection, { sessionToken: 'invalid' }) - .then(({ status }) => { - expect(status).to.eq(403) - }) - + .its('status').should('eq', 403) cy.sessionUsers(this.connection, { sessionId: 0 }) - .then(({ status }) => { - expect(status).to.eq(403) - }) - + .its('status').should('eq', 403) cy.sessionUsers(this.connection, { documentId: 0 }) - .then(({ status }) => { - expect(status).to.eq(403) - }) + .its('status').should('eq', 403) + }) + it('rejects closed sessions', function() { cy.then(() => this.connection.close()) - - cy.sessionUsers(this.connection).then(({ status }) => { - expect(status).to.eq(403) - }) + cy.sessionUsers(this.connection) + .its('status').should('eq', 403) }) })