From 3f6f5d1e0391eadb309543375d9a42c22bbf70ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julius=20H=C3=A4rtl?= Date: Wed, 31 Aug 2022 09:24:11 +0200 Subject: [PATCH 1/3] Do not try to get a displayname for guests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Julius Härtl --- lib/Controller/ImageController.php | 4 ++-- lib/Db/Session.php | 2 +- lib/Service/SessionService.php | 8 ++++++-- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/lib/Controller/ImageController.php b/lib/Controller/ImageController.php index b8b456392ab..219fa7b6842 100644 --- a/lib/Controller/ImageController.php +++ b/lib/Controller/ImageController.php @@ -218,9 +218,9 @@ public function getImage(int $documentId, int $sessionId, string $sessionToken, * @param int $documentId * @param int $sessionId * @param string $sessionToken - * @return string + * @return ?string */ - private function getUserIdFromSession(int $documentId, int $sessionId, string $sessionToken): string { + private function getUserIdFromSession(int $documentId, int $sessionId, string $sessionToken): ?string { $session = $this->sessionService->getSession($documentId, $sessionId, $sessionToken); return $session->getUserId(); } diff --git a/lib/Db/Session.php b/lib/Db/Session.php index 91fffa3bd40..b0855daffc9 100644 --- a/lib/Db/Session.php +++ b/lib/Db/Session.php @@ -27,7 +27,7 @@ use OCP\AppFramework\Db\Entity; /** - * @method string getUserId() + * @method ?string getUserId() * @method void setUserId(?string $userId) * @method string getToken() * @method void setToken(string $token) diff --git a/lib/Service/SessionService.php b/lib/Service/SessionService.php index f6e297ddac6..eab4fec8c17 100644 --- a/lib/Service/SessionService.php +++ b/lib/Service/SessionService.php @@ -116,7 +116,9 @@ public function getAllSessions($documentId): array { $sessions = $this->sessionMapper->findAll($documentId); return array_map(function (Session $session) { $result = $session->jsonSerialize(); - $result['displayName'] = $this->userManager->getDisplayName($session->getUserId()); + if ($session->getUserId() !== null) { + $result['displayName'] = $this->userManager->getDisplayName($session->getUserId()); + } return $result; }, $sessions); } @@ -125,7 +127,9 @@ public function getActiveSessions($documentId): array { $sessions = $this->sessionMapper->findAllActive($documentId); return array_map(function (Session $session) { $result = $session->jsonSerialize(); - $result['displayName'] = $this->userManager->getDisplayName($session->getUserId()); + if ($session->getUserId() !== null) { + $result['displayName'] = $this->userManager->getDisplayName($session->getUserId()); + } return $result; }, $sessions); } From ba2c4934391debe36b43bad85fd36f1934875e63 Mon Sep 17 00:00:00 2001 From: Max Date: Wed, 31 Aug 2022 10:22:36 +0200 Subject: [PATCH 2/3] cy: only run retries in run mode - not in open mode Signed-off-by: Max --- cypress.config.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/cypress.config.js b/cypress.config.js index f16ced11ae9..90aa4bb1e3e 100644 --- a/cypress.config.js +++ b/cypress.config.js @@ -20,5 +20,9 @@ module.exports = defineConfig({ experimentalSessionAndOrigin: true, specPattern: 'cypress/e2e/**/*.{js,jsx,ts,tsx}', }, - retries: 2, + retries: { + "runMode": 2, + // do not retry in `cypress open` + "openMode": 0 + }, }) From f3d2383e38a2b29b19f4e8fc4bdfab5884cdbce4 Mon Sep 17 00:00:00 2001 From: Max Date: Wed, 31 Aug 2022 11:18:32 +0200 Subject: [PATCH 3/3] cy: use API request to share rather than UI. Signed-off-by: Max --- cypress/e2e/share.spec.js | 63 +++++++++++-------------------------- cypress/support/commands.js | 34 ++++++++++++++++++++ 2 files changed, 52 insertions(+), 45 deletions(-) diff --git a/cypress/e2e/share.spec.js b/cypress/e2e/share.spec.js index 75e55d34894..e8a8aefcfbe 100644 --- a/cypress/e2e/share.spec.js +++ b/cypress/e2e/share.spec.js @@ -45,16 +45,11 @@ describe('Open test.md in viewer', function() { }) it('Shares the file as a public read only link', function() { - cy.visit('/apps/files') - cy.get('.files-fileList tr[data-file="test.md"] a.action-share') - .click({ force: true }) - cy.get('#app-sidebar-vue') - .should('be.visible') - cy.get('#app-sidebar-vue a#sharing').trigger('click') - cy.get('#app-sidebar-vue button.new-share-link').trigger('click') - cy.get('#app-sidebar-vue a.sharing-entry__copy') - .should('have.attr', 'href').and('include', '/s/') - .then((href) => cy.visit(href)) + cy.shareFile('/test.md') + .then((token) => { + cy.logout() + cy.visit(`/s/${token}`) + }) .then(() => { cy.getEditor().should('be.visible') cy.getContent() @@ -64,20 +59,10 @@ describe('Open test.md in viewer', function() { }) it('Shares the file as a public link with write permissions', function() { - cy.visit('/apps/files') - cy.get('.files-fileList tr[data-file="test2.md"] a.action-share') - .click({ force: true }) - cy.get('#app-sidebar-vue') - .should('be.visible') - cy.get('#app-sidebar-vue a#sharing').trigger('click') - cy.get('#app-sidebar-vue button.new-share-link').trigger('click') - cy.get('#app-sidebar-vue .sharing-link-list .action-item__menutoggle').trigger('click') - const checkboxAllowEditing = '.popover.open input[type=checkbox]' - cy.get(checkboxAllowEditing).first().check({ force: true }) - cy.get(checkboxAllowEditing).first().should('be.checked') - cy.get('#app-sidebar-vue a.sharing-entry__copy') - .should('have.attr', 'href').and('include', '/s/') - .then((href) => cy.visit(href)) + cy.shareFile('/test2.md', { edit: true }) + .then((token) => { + cy.visit(`/s/${token}`) + }) .then(() => { cy.getEditor().should('be.visible') cy.getContent() @@ -92,17 +77,10 @@ describe('Open test.md in viewer', function() { }) it('Opens the editor as guest', function() { - cy.visit('/apps/files') - cy.get('.files-fileList tr[data-file="test2.md"] a.action-share') - .click({ force: true }) - cy.get('#app-sidebar-vue') - .should('be.visible') - cy.get('#app-sidebar-vue a#sharing').trigger('click') - cy.get('#app-sidebar-vue a.sharing-entry__copy') - .should('have.attr', 'href').and('include', '/s/') - .then((href) => { - return cy.logout() - .then(() => cy.visit(href)) + cy.shareFile('/test2.md', { edit: true }) + .then((token) => { + cy.logout() + cy.visit(`/s/${token}`) }) .then(() => { cy.getEditor().should('be.visible') @@ -118,16 +96,11 @@ describe('Open test.md in viewer', function() { }) it('Shares a folder as a public read only link', function() { - cy.visit('/apps/files') - cy.get('.files-fileList tr[data-file="folder"] a.action-share') - .click({ force: true }) - cy.get('#app-sidebar-vue') - .should('be.visible') - cy.get('#app-sidebar-vue a#sharing').trigger('click') - cy.get('#app-sidebar-vue button.new-share-link').trigger('click') - cy.get('#app-sidebar-vue a.sharing-entry__copy') - .should('have.attr', 'href').and('include', '/s/') - .then((href) => cy.visit(href)) + cy.shareFile('/folder') + .then((token) => { + cy.logout() + cy.visit(`/s/${token}`) + }) .then(() => { cy.get('#rich-workspace').should('contain', 'Hello world') cy.openFile('test.md') diff --git a/cypress/support/commands.js b/cypress/support/commands.js index fa5acc87774..7076e4ef1fc 100644 --- a/cypress/support/commands.js +++ b/cypress/support/commands.js @@ -162,6 +162,40 @@ Cypress.Commands.add('shareFileToUser', (userId, password, path, targetUserId) = }) }) +Cypress.Commands.add('shareFile', (path, options = {}) => { + return cy.window().then(async window => { + try { + const headers = { requesttoken: window.OC.requestToken } + const request = await axios.post( + `${Cypress.env('baseUrl')}/ocs/v2.php/apps/files_sharing/api/v1/shares`, + { path, shareType: window.OC.Share.SHARE_TYPE_LINK }, + { headers } + ) + const token = request.data?.ocs?.data?.token + const id = request.data?.ocs?.data?.id + if (!token || !id || token.length === 0) { + throw request + } + cy.log(`Share link created: ${token}`) + + if (options.edit) { + // Same permissions makeing the share editable in the UI would set + // 1 = read; 2 = write; 16 = share; + const permissions = 19 + await axios.put( + `${Cypress.env('baseUrl')}/ocs/v2.php/apps/files_sharing/api/v1/shares/${id}`, + { permissions }, + { headers } + ) + cy.log(`Made share ${token} editable.`) + } + return cy.wrap(token) + } catch (error) { + console.error(error) + } + }).should('have.length', 15) +}) + Cypress.Commands.add('createFolder', dirName => cy.window() .then(win => win.OC.Files.getClient().createDirectory(dirName)) )