You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

favoriteAction.spec.ts 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /**
  2. * @copyright Copyright (c) 2023 John Molakvoæ <skjnldsv@protonmail.com>
  3. *
  4. * @author John Molakvoæ <skjnldsv@protonmail.com>
  5. *
  6. * @license AGPL-3.0-or-later
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License as
  10. * published by the Free Software Foundation, either version 3 of the
  11. * License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. import { action } from './favoriteAction'
  23. import { expect } from '@jest/globals'
  24. import { File, Permission, View, FileAction } from '@nextcloud/files'
  25. import * as eventBus from '@nextcloud/event-bus'
  26. import * as favoriteAction from './favoriteAction'
  27. import axios from '@nextcloud/axios'
  28. import logger from '../logger'
  29. const view = {
  30. id: 'files',
  31. name: 'Files',
  32. } as View
  33. const favoriteView = {
  34. id: 'favorites',
  35. name: 'Favorites',
  36. } as View
  37. global.window.OC = {
  38. TAG_FAVORITE: '_$!<Favorite>!$_',
  39. }
  40. // Mock webroot variable
  41. beforeAll(() => {
  42. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  43. (window as any)._oc_webroot = ''
  44. })
  45. describe('Favorite action conditions tests', () => {
  46. test('Default values', () => {
  47. const file = new File({
  48. id: 1,
  49. source: 'https://cloud.domain.com/remote.php/dav/files/admin/foobar.txt',
  50. owner: 'admin',
  51. mime: 'text/plain',
  52. })
  53. expect(action).toBeInstanceOf(FileAction)
  54. expect(action.id).toBe('favorite')
  55. expect(action.displayName([file], view)).toBe('Add to favorites')
  56. expect(action.iconSvgInline([], view)).toBe('<svg>SvgMock</svg>')
  57. expect(action.default).toBeUndefined()
  58. expect(action.order).toBe(-50)
  59. })
  60. test('Display name is Remove from favorites if already in favorites', () => {
  61. const file = new File({
  62. id: 1,
  63. source: 'https://cloud.domain.com/remote.php/dav/files/admin/foobar.txt',
  64. owner: 'admin',
  65. mime: 'text/plain',
  66. attributes: {
  67. favorite: 1,
  68. },
  69. })
  70. expect(action.displayName([file], view)).toBe('Remove from favorites')
  71. })
  72. test('Display name for multiple state files', () => {
  73. const file1 = new File({
  74. id: 1,
  75. source: 'https://cloud.domain.com/remote.php/dav/files/admin/foobar.txt',
  76. owner: 'admin',
  77. mime: 'text/plain',
  78. permissions: Permission.ALL,
  79. attributes: {
  80. favorite: 1,
  81. },
  82. })
  83. const file2 = new File({
  84. id: 1,
  85. source: 'https://cloud.domain.com/remote.php/dav/files/admin/foobar.txt',
  86. owner: 'admin',
  87. mime: 'text/plain',
  88. permissions: Permission.ALL,
  89. attributes: {
  90. favorite: 0,
  91. },
  92. })
  93. const file3 = new File({
  94. id: 1,
  95. source: 'https://cloud.domain.com/remote.php/dav/files/admin/foobar.txt',
  96. owner: 'admin',
  97. mime: 'text/plain',
  98. permissions: Permission.ALL,
  99. attributes: {
  100. favorite: 1,
  101. },
  102. })
  103. expect(action.displayName([file1, file2, file3], view)).toBe('Add to favorites')
  104. expect(action.displayName([file1, file2], view)).toBe('Add to favorites')
  105. expect(action.displayName([file2, file3], view)).toBe('Add to favorites')
  106. expect(action.displayName([file1, file3], view)).toBe('Remove from favorites')
  107. })
  108. })
  109. describe('Favorite action enabled tests', () => {
  110. test('Enabled for dav file', () => {
  111. const file = new File({
  112. id: 1,
  113. source: 'https://cloud.domain.com/remote.php/dav/files/admin/foobar.txt',
  114. owner: 'admin',
  115. mime: 'text/plain',
  116. permissions: Permission.ALL,
  117. })
  118. expect(action.enabled).toBeDefined()
  119. expect(action.enabled!([file], view)).toBe(true)
  120. })
  121. test('Disabled for non-dav ressources', () => {
  122. const file = new File({
  123. id: 1,
  124. source: 'https://domain.com/data/foobar.txt',
  125. owner: 'admin',
  126. mime: 'text/plain',
  127. })
  128. expect(action.enabled).toBeDefined()
  129. expect(action.enabled!([file], view)).toBe(false)
  130. })
  131. })
  132. describe('Favorite action execute tests', () => {
  133. afterEach(() => {
  134. jest.spyOn(axios, 'post').mockRestore()
  135. })
  136. test('Favorite triggers tag addition', async () => {
  137. jest.spyOn(axios, 'post')
  138. jest.spyOn(eventBus, 'emit')
  139. const file = new File({
  140. id: 1,
  141. source: 'http://localhost/remote.php/dav/files/admin/foobar.txt',
  142. owner: 'admin',
  143. mime: 'text/plain',
  144. })
  145. const exec = await action.exec(file, view, '/')
  146. expect(exec).toBe(true)
  147. // Check POST request
  148. expect(axios.post).toBeCalledTimes(1)
  149. expect(axios.post).toBeCalledWith('/index.php/apps/files/api/v1/files/foobar.txt', { tags: ['_$!<Favorite>!$_'] })
  150. // Check node change propagation
  151. expect(file.attributes.favorite).toBe(1)
  152. expect(eventBus.emit).toBeCalledTimes(1)
  153. expect(eventBus.emit).toBeCalledWith('files:favorites:added', file)
  154. })
  155. test('Favorite triggers tag removal', async () => {
  156. jest.spyOn(axios, 'post')
  157. jest.spyOn(eventBus, 'emit')
  158. const file = new File({
  159. id: 1,
  160. source: 'http://localhost/remote.php/dav/files/admin/foobar.txt',
  161. owner: 'admin',
  162. mime: 'text/plain',
  163. attributes: {
  164. favorite: 1,
  165. },
  166. })
  167. const exec = await action.exec(file, view, '/')
  168. expect(exec).toBe(true)
  169. // Check POST request
  170. expect(axios.post).toBeCalledTimes(1)
  171. expect(axios.post).toBeCalledWith('/index.php/apps/files/api/v1/files/foobar.txt', { tags: [] })
  172. // Check node change propagation
  173. expect(file.attributes.favorite).toBe(0)
  174. expect(eventBus.emit).toBeCalledTimes(1)
  175. expect(eventBus.emit).toBeCalledWith('files:favorites:removed', file)
  176. })
  177. test('Favorite triggers node removal if favorite view and root dir', async () => {
  178. jest.spyOn(axios, 'post')
  179. jest.spyOn(eventBus, 'emit')
  180. const file = new File({
  181. id: 1,
  182. source: 'http://localhost/remote.php/dav/files/admin/foobar.txt',
  183. owner: 'admin',
  184. mime: 'text/plain',
  185. attributes: {
  186. favorite: 1,
  187. },
  188. })
  189. const exec = await action.exec(file, favoriteView, '/')
  190. expect(exec).toBe(true)
  191. // Check POST request
  192. expect(axios.post).toBeCalledTimes(1)
  193. expect(axios.post).toBeCalledWith('/index.php/apps/files/api/v1/files/foobar.txt', { tags: [] })
  194. // Check node change propagation
  195. expect(file.attributes.favorite).toBe(0)
  196. expect(eventBus.emit).toBeCalledTimes(2)
  197. expect(eventBus.emit).toHaveBeenNthCalledWith(1, 'files:node:deleted', file)
  198. expect(eventBus.emit).toHaveBeenNthCalledWith(2, 'files:favorites:removed', file)
  199. })
  200. test('Favorite does NOT triggers node removal if favorite view but NOT root dir', async () => {
  201. jest.spyOn(axios, 'post')
  202. jest.spyOn(eventBus, 'emit')
  203. const file = new File({
  204. id: 1,
  205. source: 'http://localhost/remote.php/dav/files/admin/Foo/Bar/foobar.txt',
  206. root: '/files/admin',
  207. owner: 'admin',
  208. mime: 'text/plain',
  209. attributes: {
  210. favorite: 1,
  211. },
  212. })
  213. const exec = await action.exec(file, favoriteView, '/')
  214. expect(exec).toBe(true)
  215. // Check POST request
  216. expect(axios.post).toBeCalledTimes(1)
  217. expect(axios.post).toBeCalledWith('/index.php/apps/files/api/v1/files/Foo/Bar/foobar.txt', { tags: [] })
  218. // Check node change propagation
  219. expect(file.attributes.favorite).toBe(0)
  220. expect(eventBus.emit).toBeCalledTimes(1)
  221. expect(eventBus.emit).toBeCalledWith('files:favorites:removed', file)
  222. })
  223. test('Favorite fails and show error', async () => {
  224. const error = new Error('Mock error')
  225. jest.spyOn(axios, 'post').mockImplementation(() => { throw new Error('Mock error') })
  226. jest.spyOn(logger, 'error').mockImplementation(() => jest.fn())
  227. const file = new File({
  228. id: 1,
  229. source: 'http://localhost/remote.php/dav/files/admin/foobar.txt',
  230. owner: 'admin',
  231. mime: 'text/plain',
  232. attributes: {
  233. favorite: 0,
  234. },
  235. })
  236. const exec = await action.exec(file, view, '/')
  237. expect(exec).toBe(false)
  238. // Check POST request
  239. expect(axios.post).toBeCalledTimes(1)
  240. expect(axios.post).toBeCalledWith('/index.php/apps/files/api/v1/files/foobar.txt', { tags: ['_$!<Favorite>!$_'] })
  241. // Check node change propagation
  242. expect(logger.error).toBeCalledTimes(1)
  243. expect(logger.error).toBeCalledWith('Error while adding a file to favourites', { error, source: file.source, node: file })
  244. expect(file.attributes.favorite).toBe(0)
  245. expect(eventBus.emit).toBeCalledTimes(0)
  246. })
  247. test('Removing from favorites fails and show error', async () => {
  248. const error = new Error('Mock error')
  249. jest.spyOn(axios, 'post').mockImplementation(() => { throw error })
  250. jest.spyOn(logger, 'error').mockImplementation(() => jest.fn())
  251. const file = new File({
  252. id: 1,
  253. source: 'http://localhost/remote.php/dav/files/admin/foobar.txt',
  254. owner: 'admin',
  255. mime: 'text/plain',
  256. attributes: {
  257. favorite: 1,
  258. },
  259. })
  260. const exec = await action.exec(file, view, '/')
  261. expect(exec).toBe(false)
  262. // Check POST request
  263. expect(axios.post).toBeCalledTimes(1)
  264. expect(axios.post).toBeCalledWith('/index.php/apps/files/api/v1/files/foobar.txt', { tags: [] })
  265. // Check node change propagation
  266. expect(logger.error).toBeCalledTimes(1)
  267. expect(logger.error).toBeCalledWith('Error while removing a file from favourites', { error, source: file.source, node: file })
  268. expect(file.attributes.favorite).toBe(1)
  269. expect(eventBus.emit).toBeCalledTimes(0)
  270. })
  271. })
  272. describe('Favorite action batch execute tests', () => {
  273. test('Favorite action batch execute with mixed files', async () => {
  274. jest.spyOn(favoriteAction, 'favoriteNode')
  275. jest.spyOn(axios, 'post')
  276. const file1 = new File({
  277. id: 1,
  278. source: 'https://cloud.domain.com/remote.php/dav/files/admin/foo.txt',
  279. owner: 'admin',
  280. mime: 'text/plain',
  281. permissions: Permission.ALL,
  282. attributes: {
  283. favorite: 1,
  284. },
  285. })
  286. const file2 = new File({
  287. id: 1,
  288. source: 'https://cloud.domain.com/remote.php/dav/files/admin/bar.txt',
  289. owner: 'admin',
  290. mime: 'text/plain',
  291. permissions: Permission.ALL,
  292. attributes: {
  293. favorite: 0,
  294. },
  295. })
  296. // Mixed states triggers favorite action
  297. const exec = await action.execBatch!([file1, file2], view, '/')
  298. expect(exec).toStrictEqual([true, true])
  299. expect([file1, file2].every(file => file.attributes.favorite === 1)).toBe(true)
  300. expect(favoriteAction.favoriteNode).toBeCalledTimes(2)
  301. expect(axios.post).toBeCalledTimes(2)
  302. expect(axios.post).toHaveBeenNthCalledWith(1, '/index.php/apps/files/api/v1/files/foo.txt', { tags: ['_$!<Favorite>!$_'] })
  303. expect(axios.post).toHaveBeenNthCalledWith(2, '/index.php/apps/files/api/v1/files/bar.txt', { tags: ['_$!<Favorite>!$_'] })
  304. })
  305. test('Remove from favorite action batch execute with favorites only files', async () => {
  306. jest.spyOn(favoriteAction, 'favoriteNode')
  307. jest.spyOn(axios, 'post')
  308. const file1 = new File({
  309. id: 1,
  310. source: 'https://cloud.domain.com/remote.php/dav/files/admin/foo.txt',
  311. owner: 'admin',
  312. mime: 'text/plain',
  313. permissions: Permission.ALL,
  314. attributes: {
  315. favorite: 1,
  316. },
  317. })
  318. const file2 = new File({
  319. id: 1,
  320. source: 'https://cloud.domain.com/remote.php/dav/files/admin/bar.txt',
  321. owner: 'admin',
  322. mime: 'text/plain',
  323. permissions: Permission.ALL,
  324. attributes: {
  325. favorite: 1,
  326. },
  327. })
  328. // Mixed states triggers favorite action
  329. const exec = await action.execBatch!([file1, file2], view, '/')
  330. expect(exec).toStrictEqual([true, true])
  331. expect([file1, file2].every(file => file.attributes.favorite === 0)).toBe(true)
  332. expect(favoriteAction.favoriteNode).toBeCalledTimes(2)
  333. expect(axios.post).toBeCalledTimes(2)
  334. expect(axios.post).toHaveBeenNthCalledWith(1, '/index.php/apps/files/api/v1/files/foo.txt', { tags: [] })
  335. expect(axios.post).toHaveBeenNthCalledWith(2, '/index.php/apps/files/api/v1/files/bar.txt', { tags: [] })
  336. })
  337. })