diff options
Diffstat (limited to 'apps/files_sharing/src/services/SharingService.spec.ts')
-rw-r--r-- | apps/files_sharing/src/services/SharingService.spec.ts | 268 |
1 files changed, 207 insertions, 61 deletions
diff --git a/apps/files_sharing/src/services/SharingService.spec.ts b/apps/files_sharing/src/services/SharingService.spec.ts index 79b91c9826f..936c1afafc4 100644 --- a/apps/files_sharing/src/services/SharingService.spec.ts +++ b/apps/files_sharing/src/services/SharingService.spec.ts @@ -1,47 +1,35 @@ /** - * @copyright Copyright (c) 2023 John Molakvoæ <skjnldsv@protonmail.com> - * - * @author John Molakvoæ <skjnldsv@protonmail.com> - * - * @license AGPL-3.0-or-later - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * + * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ import type { OCSResponse } from '@nextcloud/typings/ocs' -import { expect } from '@jest/globals' -import { Type } from '@nextcloud/sharing' -import * as auth from '@nextcloud/auth' -import axios from '@nextcloud/axios' -import { getContents } from './SharingService' import { File, Folder } from '@nextcloud/files' +import { ShareType } from '@nextcloud/sharing' +import { beforeAll, beforeEach, describe, expect, test, vi } from 'vitest' + +import { getContents } from './SharingService' +import * as auth from '@nextcloud/auth' import logger from './logger' -global.window.OC = { - TAG_FAVORITE: '_$!<Favorite>!$_', -} +const TAG_FAVORITE = '_$!<Favorite>!$_' -// Mock webroot variable +const axios = vi.hoisted(() => ({ get: vi.fn() })) +vi.mock('@nextcloud/auth') +vi.mock('@nextcloud/axios', () => ({ default: axios })) + +// Mock TAG beforeAll(() => { - // eslint-disable-next-line @typescript-eslint/no-explicit-any - (window as any)._oc_webroot = '' + window.OC = { + ...window.OC, + TAG_FAVORITE, + } }) describe('SharingService methods definitions', () => { - beforeAll(() => { - jest.spyOn(axios, 'get').mockImplementation(async (): Promise<any> => { + beforeEach(() => { + vi.resetAllMocks() + axios.get.mockImplementation(async (): Promise<unknown> => { return { data: { ocs: { @@ -52,20 +40,16 @@ describe('SharingService methods definitions', () => { }, data: [], }, - } as OCSResponse<any>, + } as OCSResponse, } }) }) - afterAll(() => { - jest.restoreAllMocks() - }) - test('Shared with you', async () => { await getContents(true, false, false, false, []) expect(axios.get).toHaveBeenCalledTimes(2) - expect(axios.get).toHaveBeenNthCalledWith(1, 'http://localhost/ocs/v2.php/apps/files_sharing/api/v1/shares', { + expect(axios.get).toHaveBeenNthCalledWith(1, 'http://nextcloud.local/ocs/v2.php/apps/files_sharing/api/v1/shares', { headers: { 'Content-Type': 'application/json', }, @@ -74,7 +58,7 @@ describe('SharingService methods definitions', () => { include_tags: true, }, }) - expect(axios.get).toHaveBeenNthCalledWith(2, 'http://localhost/ocs/v2.php/apps/files_sharing/api/v1/remote_shares', { + expect(axios.get).toHaveBeenNthCalledWith(2, 'http://nextcloud.local/ocs/v2.php/apps/files_sharing/api/v1/remote_shares', { headers: { 'Content-Type': 'application/json', }, @@ -88,7 +72,7 @@ describe('SharingService methods definitions', () => { await getContents(false, true, false, false, []) expect(axios.get).toHaveBeenCalledTimes(1) - expect(axios.get).toHaveBeenCalledWith('http://localhost/ocs/v2.php/apps/files_sharing/api/v1/shares', { + expect(axios.get).toHaveBeenCalledWith('http://nextcloud.local/ocs/v2.php/apps/files_sharing/api/v1/shares', { headers: { 'Content-Type': 'application/json', }, @@ -103,7 +87,7 @@ describe('SharingService methods definitions', () => { await getContents(false, false, true, false, []) expect(axios.get).toHaveBeenCalledTimes(2) - expect(axios.get).toHaveBeenNthCalledWith(1, 'http://localhost/ocs/v2.php/apps/files_sharing/api/v1/shares/pending', { + expect(axios.get).toHaveBeenNthCalledWith(1, 'http://nextcloud.local/ocs/v2.php/apps/files_sharing/api/v1/shares/pending', { headers: { 'Content-Type': 'application/json', }, @@ -111,7 +95,7 @@ describe('SharingService methods definitions', () => { include_tags: true, }, }) - expect(axios.get).toHaveBeenNthCalledWith(2, 'http://localhost/ocs/v2.php/apps/files_sharing/api/v1/remote_shares/pending', { + expect(axios.get).toHaveBeenNthCalledWith(2, 'http://nextcloud.local/ocs/v2.php/apps/files_sharing/api/v1/remote_shares/pending', { headers: { 'Content-Type': 'application/json', }, @@ -125,7 +109,7 @@ describe('SharingService methods definitions', () => { await getContents(false, true, false, false, []) expect(axios.get).toHaveBeenCalledTimes(1) - expect(axios.get).toHaveBeenCalledWith('http://localhost/ocs/v2.php/apps/files_sharing/api/v1/shares', { + expect(axios.get).toHaveBeenCalledWith('http://nextcloud.local/ocs/v2.php/apps/files_sharing/api/v1/shares', { headers: { 'Content-Type': 'application/json', }, @@ -137,7 +121,7 @@ describe('SharingService methods definitions', () => { }) test('Unknown owner', async () => { - jest.spyOn(auth, 'getCurrentUser').mockReturnValue(null) + vi.spyOn(auth, 'getCurrentUser').mockReturnValue(null) const results = await getContents(false, true, false, false, []) expect(results.folder.owner).toEqual(null) @@ -145,8 +129,9 @@ describe('SharingService methods definitions', () => { }) describe('SharingService filtering', () => { - beforeAll(() => { - jest.spyOn(axios, 'get').mockImplementation(async (): Promise<any> => { + beforeEach(() => { + vi.resetAllMocks() + axios.get.mockImplementation(async (): Promise<unknown> => { return { data: { ocs: { @@ -158,7 +143,7 @@ describe('SharingService filtering', () => { data: [ { id: '62', - share_type: Type.SHARE_TYPE_USER, + share_type: ShareType.User, uid_owner: 'test', displayname_owner: 'test', permissions: 31, @@ -184,12 +169,8 @@ describe('SharingService filtering', () => { }) }) - afterAll(() => { - jest.restoreAllMocks() - }) - test('Shared with others filtering', async () => { - const shares = await getContents(false, true, false, false, [Type.SHARE_TYPE_USER]) + const shares = await getContents(false, true, false, false, [ShareType.User]) expect(axios.get).toHaveBeenCalledTimes(1) expect(shares.contents).toHaveLength(1) @@ -198,7 +179,7 @@ describe('SharingService filtering', () => { }) test('Shared with others filtering empty', async () => { - const shares = await getContents(false, true, false, false, [Type.SHARE_TYPE_LINK]) + const shares = await getContents(false, true, false, false, [ShareType.Link]) expect(axios.get).toHaveBeenCalledTimes(1) expect(shares.contents).toHaveLength(0) @@ -291,11 +272,65 @@ describe('SharingService share to Node mapping', () => { mail_send: 0, hide_download: 0, attributes: null, - tags: [window.OC.TAG_FAVORITE], + tags: [TAG_FAVORITE], } + const remoteFileAccepted = { + mimetype: 'text/markdown', + mtime: 1688721600, + permissions: 19, + type: 'file', + file_id: 1234, + id: 4, + share_type: ShareType.User, + parent: null, + remote: 'http://exampe.com', + remote_id: '12345', + share_token: 'share-token', + name: '/test.md', + mountpoint: '/shares/test.md', + owner: 'owner-uid', + user: 'sharee-uid', + accepted: true, + } + + const remoteFilePending = { + mimetype: 'text/markdown', + mtime: 1688721600, + permissions: 19, + type: 'file', + file_id: 1234, + id: 4, + share_type: ShareType.User, + parent: null, + remote: 'http://exampe.com', + remote_id: '12345', + share_token: 'share-token', + name: '/test.md', + mountpoint: '/shares/test.md', + owner: 'owner-uid', + user: 'sharee-uid', + accepted: false, + } + + const tempExternalFile = { + id: 65, + share_type: 0, + parent: -1, + remote: 'http://nextcloud1.local/', + remote_id: '71', + share_token: '9GpiAmTIjayclrE', + name: '/test.md', + owner: 'owner-uid', + user: 'sharee-uid', + mountpoint: '{{TemporaryMountPointName#/test.md}}', + accepted: 0, + } + + beforeEach(() => { vi.resetAllMocks() }) + test('File', async () => { - jest.spyOn(axios, 'get').mockReturnValueOnce(Promise.resolve({ + axios.get.mockReturnValueOnce(Promise.resolve({ data: { ocs: { data: [shareFile], @@ -311,7 +346,7 @@ describe('SharingService share to Node mapping', () => { const file = shares.contents[0] as File expect(file).toBeInstanceOf(File) expect(file.fileid).toBe(530936) - expect(file.source).toBe('http://localhost/remote.php/dav/files/test/document.md') + expect(file.source).toBe('http://nextcloud.local/remote.php/dav/files/test/document.md') expect(file.owner).toBe('test') expect(file.mime).toBe('text/markdown') expect(file.mtime).toBeInstanceOf(Date) @@ -320,11 +355,18 @@ describe('SharingService share to Node mapping', () => { expect(file.root).toBe('/files/test') expect(file.attributes).toBeInstanceOf(Object) expect(file.attributes['has-preview']).toBe(true) + expect(file.attributes.sharees).toEqual({ + sharee: { + id: 'user00', + 'display-name': 'User00', + type: 0, + }, + }) expect(file.attributes.favorite).toBe(0) }) test('Folder', async () => { - jest.spyOn(axios, 'get').mockReturnValueOnce(Promise.resolve({ + axios.get.mockReturnValueOnce(Promise.resolve({ data: { ocs: { data: [shareFolder], @@ -340,7 +382,7 @@ describe('SharingService share to Node mapping', () => { const folder = shares.contents[0] as Folder expect(folder).toBeInstanceOf(Folder) expect(folder.fileid).toBe(531080) - expect(folder.source).toBe('http://localhost/remote.php/dav/files/test/Folder') + expect(folder.source).toBe('http://nextcloud.local/remote.php/dav/files/test/Folder') expect(folder.owner).toBe('test') expect(folder.mime).toBe('httpd/unix-directory') expect(folder.mtime).toBeInstanceOf(Date) @@ -353,12 +395,116 @@ describe('SharingService share to Node mapping', () => { expect(folder.attributes.favorite).toBe(1) }) + describe('Remote file', () => { + test('Accepted', async () => { + axios.get.mockReturnValueOnce(Promise.resolve({ + data: { + ocs: { + data: [remoteFileAccepted], + }, + }, + })) + + const shares = await getContents(false, true, false, false) + + expect(axios.get).toHaveBeenCalledTimes(1) + expect(shares.contents).toHaveLength(1) + + const file = shares.contents[0] as File + expect(file).toBeInstanceOf(File) + expect(file.fileid).toBe(1234) + expect(file.source).toBe('http://nextcloud.local/remote.php/dav/files/test/shares/test.md') + expect(file.owner).toBe('owner-uid') + expect(file.mime).toBe('text/markdown') + expect(file.mtime?.getTime()).toBe(remoteFileAccepted.mtime * 1000) + // not available for remote shares + expect(file.size).toBe(undefined) + expect(file.permissions).toBe(19) + expect(file.root).toBe('/files/test') + expect(file.attributes).toBeInstanceOf(Object) + expect(file.attributes.favorite).toBe(0) + }) + + test('Pending', async () => { + axios.get.mockReturnValueOnce(Promise.resolve({ + data: { + ocs: { + data: [remoteFilePending], + }, + }, + })) + + const shares = await getContents(false, true, false, false) + + expect(axios.get).toHaveBeenCalledTimes(1) + expect(shares.contents).toHaveLength(1) + + const file = shares.contents[0] as File + expect(file).toBeInstanceOf(File) + expect(file.fileid).toBe(1234) + expect(file.source).toBe('http://nextcloud.local/remote.php/dav/files/test/shares/test.md') + expect(file.owner).toBe('owner-uid') + expect(file.mime).toBe('text/markdown') + expect(file.mtime?.getTime()).toBe(remoteFilePending.mtime * 1000) + // not available for remote shares + expect(file.size).toBe(undefined) + expect(file.permissions).toBe(0) + expect(file.root).toBe('/files/test') + expect(file.attributes).toBeInstanceOf(Object) + expect(file.attributes.favorite).toBe(0) + }) + }) + + test('External temp file', async () => { + axios.get.mockReturnValueOnce(Promise.resolve({ + data: { + ocs: { + data: [tempExternalFile], + }, + }, + })) + + const shares = await getContents(false, true, false, false) + + expect(axios.get).toHaveBeenCalledTimes(1) + expect(shares.contents).toHaveLength(1) + + const file = shares.contents[0] as File + expect(file).toBeInstanceOf(File) + expect(file.fileid).toBe(65) + expect(file.source).toBe('http://nextcloud.local/remote.php/dav/files/test/test.md') + expect(file.owner).toBe('owner-uid') + expect(file.mime).toBe('text/markdown') + expect(file.mtime?.getTime()).toBe(undefined) + // not available for remote shares + expect(file.size).toBe(undefined) + expect(file.permissions).toBe(0) + expect(file.root).toBe('/files/test') + expect(file.attributes).toBeInstanceOf(Object) + expect(file.attributes.favorite).toBe(0) + }) + + test('Empty', async () => { + vi.spyOn(logger, 'error').mockImplementationOnce(() => {}) + axios.get.mockReturnValueOnce(Promise.resolve({ + data: { + ocs: { + data: [], + }, + }, + })) + + const shares = await getContents(false, true, false, false) + expect(shares.contents).toHaveLength(0) + expect(logger.error).toHaveBeenCalledTimes(0) + }) + test('Error', async () => { - jest.spyOn(logger, 'error').mockImplementationOnce(() => {}) - jest.spyOn(axios, 'get').mockReturnValueOnce(Promise.resolve({ + vi.spyOn(logger, 'error').mockImplementationOnce(() => {}) + axios.get.mockReturnValueOnce(Promise.resolve({ data: { ocs: { - data: [{}], + data: [null], }, }, })) |