123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396 |
- /**
- * @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/>.
- *
- */
- import { action } from './favoriteAction'
- import { expect } from '@jest/globals'
- import { File, Permission, View, FileAction } from '@nextcloud/files'
- import * as eventBus from '@nextcloud/event-bus'
- import * as favoriteAction from './favoriteAction'
- import axios from '@nextcloud/axios'
- import logger from '../logger'
-
- const view = {
- id: 'files',
- name: 'Files',
- } as View
-
- const favoriteView = {
- id: 'favorites',
- name: 'Favorites',
- } as View
-
- global.window.OC = {
- TAG_FAVORITE: '_$!<Favorite>!$_',
- }
-
- // Mock webroot variable
- beforeAll(() => {
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- (window as any)._oc_webroot = ''
- })
-
- describe('Favorite action conditions tests', () => {
- test('Default values', () => {
- const file = new File({
- id: 1,
- source: 'https://cloud.domain.com/remote.php/dav/files/admin/foobar.txt',
- owner: 'admin',
- mime: 'text/plain',
- })
-
- expect(action).toBeInstanceOf(FileAction)
- expect(action.id).toBe('favorite')
- expect(action.displayName([file], view)).toBe('Add to favorites')
- expect(action.iconSvgInline([], view)).toBe('<svg>SvgMock</svg>')
- expect(action.default).toBeUndefined()
- expect(action.order).toBe(-50)
- })
-
- test('Display name is Remove from favorites if already in favorites', () => {
- const file = new File({
- id: 1,
- source: 'https://cloud.domain.com/remote.php/dav/files/admin/foobar.txt',
- owner: 'admin',
- mime: 'text/plain',
- attributes: {
- favorite: 1,
- },
- })
-
- expect(action.displayName([file], view)).toBe('Remove from favorites')
- })
-
- test('Display name for multiple state files', () => {
- const file1 = new File({
- id: 1,
- source: 'https://cloud.domain.com/remote.php/dav/files/admin/foobar.txt',
- owner: 'admin',
- mime: 'text/plain',
- permissions: Permission.ALL,
- attributes: {
- favorite: 1,
- },
- })
- const file2 = new File({
- id: 1,
- source: 'https://cloud.domain.com/remote.php/dav/files/admin/foobar.txt',
- owner: 'admin',
- mime: 'text/plain',
- permissions: Permission.ALL,
- attributes: {
- favorite: 0,
- },
- })
- const file3 = new File({
- id: 1,
- source: 'https://cloud.domain.com/remote.php/dav/files/admin/foobar.txt',
- owner: 'admin',
- mime: 'text/plain',
- permissions: Permission.ALL,
- attributes: {
- favorite: 1,
- },
- })
-
- expect(action.displayName([file1, file2, file3], view)).toBe('Add to favorites')
- expect(action.displayName([file1, file2], view)).toBe('Add to favorites')
- expect(action.displayName([file2, file3], view)).toBe('Add to favorites')
- expect(action.displayName([file1, file3], view)).toBe('Remove from favorites')
- })
- })
-
- describe('Favorite action enabled tests', () => {
- test('Enabled for dav file', () => {
- const file = new File({
- id: 1,
- source: 'https://cloud.domain.com/remote.php/dav/files/admin/foobar.txt',
- owner: 'admin',
- mime: 'text/plain',
- permissions: Permission.ALL,
- })
-
- expect(action.enabled).toBeDefined()
- expect(action.enabled!([file], view)).toBe(true)
- })
-
- test('Disabled for non-dav ressources', () => {
- const file = new File({
- id: 1,
- source: 'https://domain.com/data/foobar.txt',
- owner: 'admin',
- mime: 'text/plain',
- })
-
- expect(action.enabled).toBeDefined()
- expect(action.enabled!([file], view)).toBe(false)
- })
- })
-
- describe('Favorite action execute tests', () => {
- afterEach(() => {
- jest.spyOn(axios, 'post').mockRestore()
- })
-
- test('Favorite triggers tag addition', async () => {
- jest.spyOn(axios, 'post')
- jest.spyOn(eventBus, 'emit')
-
- const file = new File({
- id: 1,
- source: 'http://localhost/remote.php/dav/files/admin/foobar.txt',
- owner: 'admin',
- mime: 'text/plain',
- })
-
- const exec = await action.exec(file, view, '/')
-
- expect(exec).toBe(true)
-
- // Check POST request
- expect(axios.post).toBeCalledTimes(1)
- expect(axios.post).toBeCalledWith('/index.php/apps/files/api/v1/files/foobar.txt', { tags: ['_$!<Favorite>!$_'] })
-
- // Check node change propagation
- expect(file.attributes.favorite).toBe(1)
- expect(eventBus.emit).toBeCalledTimes(1)
- expect(eventBus.emit).toBeCalledWith('files:favorites:added', file)
- })
-
- test('Favorite triggers tag removal', async () => {
- jest.spyOn(axios, 'post')
- jest.spyOn(eventBus, 'emit')
-
- const file = new File({
- id: 1,
- source: 'http://localhost/remote.php/dav/files/admin/foobar.txt',
- owner: 'admin',
- mime: 'text/plain',
- attributes: {
- favorite: 1,
- },
- })
-
- const exec = await action.exec(file, view, '/')
-
- expect(exec).toBe(true)
-
- // Check POST request
- expect(axios.post).toBeCalledTimes(1)
- expect(axios.post).toBeCalledWith('/index.php/apps/files/api/v1/files/foobar.txt', { tags: [] })
-
- // Check node change propagation
- expect(file.attributes.favorite).toBe(0)
- expect(eventBus.emit).toBeCalledTimes(1)
- expect(eventBus.emit).toBeCalledWith('files:favorites:removed', file)
- })
-
- test('Favorite triggers node removal if favorite view and root dir', async () => {
- jest.spyOn(axios, 'post')
- jest.spyOn(eventBus, 'emit')
-
- const file = new File({
- id: 1,
- source: 'http://localhost/remote.php/dav/files/admin/foobar.txt',
- owner: 'admin',
- mime: 'text/plain',
- attributes: {
- favorite: 1,
- },
- })
-
- const exec = await action.exec(file, favoriteView, '/')
-
- expect(exec).toBe(true)
-
- // Check POST request
- expect(axios.post).toBeCalledTimes(1)
- expect(axios.post).toBeCalledWith('/index.php/apps/files/api/v1/files/foobar.txt', { tags: [] })
-
- // Check node change propagation
- expect(file.attributes.favorite).toBe(0)
- expect(eventBus.emit).toBeCalledTimes(2)
- expect(eventBus.emit).toHaveBeenNthCalledWith(1, 'files:node:deleted', file)
- expect(eventBus.emit).toHaveBeenNthCalledWith(2, 'files:favorites:removed', file)
- })
-
- test('Favorite does NOT triggers node removal if favorite view but NOT root dir', async () => {
- jest.spyOn(axios, 'post')
- jest.spyOn(eventBus, 'emit')
-
- const file = new File({
- id: 1,
- source: 'http://localhost/remote.php/dav/files/admin/Foo/Bar/foobar.txt',
- root: '/files/admin',
- owner: 'admin',
- mime: 'text/plain',
- attributes: {
- favorite: 1,
- },
- })
-
- const exec = await action.exec(file, favoriteView, '/')
-
- expect(exec).toBe(true)
-
- // Check POST request
- expect(axios.post).toBeCalledTimes(1)
- expect(axios.post).toBeCalledWith('/index.php/apps/files/api/v1/files/Foo/Bar/foobar.txt', { tags: [] })
-
- // Check node change propagation
- expect(file.attributes.favorite).toBe(0)
- expect(eventBus.emit).toBeCalledTimes(1)
- expect(eventBus.emit).toBeCalledWith('files:favorites:removed', file)
- })
-
- test('Favorite fails and show error', async () => {
- const error = new Error('Mock error')
- jest.spyOn(axios, 'post').mockImplementation(() => { throw new Error('Mock error') })
- jest.spyOn(logger, 'error').mockImplementation(() => jest.fn())
-
- const file = new File({
- id: 1,
- source: 'http://localhost/remote.php/dav/files/admin/foobar.txt',
- owner: 'admin',
- mime: 'text/plain',
- attributes: {
- favorite: 0,
- },
- })
-
- const exec = await action.exec(file, view, '/')
-
- expect(exec).toBe(false)
-
- // Check POST request
- expect(axios.post).toBeCalledTimes(1)
- expect(axios.post).toBeCalledWith('/index.php/apps/files/api/v1/files/foobar.txt', { tags: ['_$!<Favorite>!$_'] })
-
- // Check node change propagation
- expect(logger.error).toBeCalledTimes(1)
- expect(logger.error).toBeCalledWith('Error while adding a file to favourites', { error, source: file.source, node: file })
- expect(file.attributes.favorite).toBe(0)
- expect(eventBus.emit).toBeCalledTimes(0)
- })
-
- test('Removing from favorites fails and show error', async () => {
- const error = new Error('Mock error')
- jest.spyOn(axios, 'post').mockImplementation(() => { throw error })
- jest.spyOn(logger, 'error').mockImplementation(() => jest.fn())
-
- const file = new File({
- id: 1,
- source: 'http://localhost/remote.php/dav/files/admin/foobar.txt',
- owner: 'admin',
- mime: 'text/plain',
- attributes: {
- favorite: 1,
- },
- })
-
- const exec = await action.exec(file, view, '/')
-
- expect(exec).toBe(false)
-
- // Check POST request
- expect(axios.post).toBeCalledTimes(1)
- expect(axios.post).toBeCalledWith('/index.php/apps/files/api/v1/files/foobar.txt', { tags: [] })
-
- // Check node change propagation
- expect(logger.error).toBeCalledTimes(1)
- expect(logger.error).toBeCalledWith('Error while removing a file from favourites', { error, source: file.source, node: file })
- expect(file.attributes.favorite).toBe(1)
- expect(eventBus.emit).toBeCalledTimes(0)
- })
- })
-
- describe('Favorite action batch execute tests', () => {
- test('Favorite action batch execute with mixed files', async () => {
- jest.spyOn(favoriteAction, 'favoriteNode')
- jest.spyOn(axios, 'post')
-
- const file1 = new File({
- id: 1,
- source: 'https://cloud.domain.com/remote.php/dav/files/admin/foo.txt',
- owner: 'admin',
- mime: 'text/plain',
- permissions: Permission.ALL,
- attributes: {
- favorite: 1,
- },
- })
- const file2 = new File({
- id: 1,
- source: 'https://cloud.domain.com/remote.php/dav/files/admin/bar.txt',
- owner: 'admin',
- mime: 'text/plain',
- permissions: Permission.ALL,
- attributes: {
- favorite: 0,
- },
- })
-
- // Mixed states triggers favorite action
- const exec = await action.execBatch!([file1, file2], view, '/')
- expect(exec).toStrictEqual([true, true])
- expect([file1, file2].every(file => file.attributes.favorite === 1)).toBe(true)
-
- expect(favoriteAction.favoriteNode).toBeCalledTimes(2)
- expect(axios.post).toBeCalledTimes(2)
- expect(axios.post).toHaveBeenNthCalledWith(1, '/index.php/apps/files/api/v1/files/foo.txt', { tags: ['_$!<Favorite>!$_'] })
- expect(axios.post).toHaveBeenNthCalledWith(2, '/index.php/apps/files/api/v1/files/bar.txt', { tags: ['_$!<Favorite>!$_'] })
- })
-
- test('Remove from favorite action batch execute with favorites only files', async () => {
- jest.spyOn(favoriteAction, 'favoriteNode')
- jest.spyOn(axios, 'post')
-
- const file1 = new File({
- id: 1,
- source: 'https://cloud.domain.com/remote.php/dav/files/admin/foo.txt',
- owner: 'admin',
- mime: 'text/plain',
- permissions: Permission.ALL,
- attributes: {
- favorite: 1,
- },
- })
- const file2 = new File({
- id: 1,
- source: 'https://cloud.domain.com/remote.php/dav/files/admin/bar.txt',
- owner: 'admin',
- mime: 'text/plain',
- permissions: Permission.ALL,
- attributes: {
- favorite: 1,
- },
- })
-
- // Mixed states triggers favorite action
- const exec = await action.execBatch!([file1, file2], view, '/')
- expect(exec).toStrictEqual([true, true])
- expect([file1, file2].every(file => file.attributes.favorite === 0)).toBe(true)
-
- expect(favoriteAction.favoriteNode).toBeCalledTimes(2)
- expect(axios.post).toBeCalledTimes(2)
- expect(axios.post).toHaveBeenNthCalledWith(1, '/index.php/apps/files/api/v1/files/foo.txt', { tags: [] })
- expect(axios.post).toHaveBeenNthCalledWith(2, '/index.php/apps/files/api/v1/files/bar.txt', { tags: [] })
- })
- })
|