diff options
Diffstat (limited to 'apps/files_sharing/src/services/SharingService.spec.ts')
-rw-r--r-- | apps/files_sharing/src/services/SharingService.spec.ts | 234 |
1 files changed, 191 insertions, 43 deletions
diff --git a/apps/files_sharing/src/services/SharingService.spec.ts b/apps/files_sharing/src/services/SharingService.spec.ts index d756c4755d7..936c1afafc4 100644 --- a/apps/files_sharing/src/services/SharingService.spec.ts +++ b/apps/files_sharing/src/services/SharingService.spec.ts @@ -3,28 +3,33 @@ * 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>!$_' + +const axios = vi.hoisted(() => ({ get: vi.fn() })) +vi.mock('@nextcloud/auth') +vi.mock('@nextcloud/axios', () => ({ default: axios })) -// Mock webroot variable +// 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: { @@ -35,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', }, @@ -57,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', }, @@ -71,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', }, @@ -86,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', }, @@ -94,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', }, @@ -108,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', }, @@ -120,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) @@ -128,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: { @@ -141,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, @@ -167,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) @@ -181,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) @@ -274,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], @@ -294,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) @@ -303,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], @@ -323,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) @@ -336,9 +395,98 @@ 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 () => { - 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: [], @@ -352,8 +500,8 @@ describe('SharingService share to Node mapping', () => { }) 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: [null], |