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.

editLocallyAction.spec.ts 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 './editLocallyAction'
  23. import { expect } from '@jest/globals'
  24. import { File, Permission, View, FileAction } from '@nextcloud/files'
  25. import * as ncDialogs from '@nextcloud/dialogs'
  26. import axios from '@nextcloud/axios'
  27. const view = {
  28. id: 'files',
  29. name: 'Files',
  30. } as View
  31. // Mock webroot variable
  32. beforeAll(() => {
  33. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  34. (window as any)._oc_webroot = ''
  35. })
  36. describe('Edit locally action conditions tests', () => {
  37. test('Default values', () => {
  38. expect(action).toBeInstanceOf(FileAction)
  39. expect(action.id).toBe('edit-locally')
  40. expect(action.displayName([], view)).toBe('Edit locally')
  41. expect(action.iconSvgInline([], view)).toBe('<svg>SvgMock</svg>')
  42. expect(action.default).toBeUndefined()
  43. expect(action.order).toBe(25)
  44. })
  45. })
  46. describe('Edit locally action enabled tests', () => {
  47. test('Enabled for file with UPDATE permission', () => {
  48. const file = new File({
  49. id: 1,
  50. source: 'https://cloud.domain.com/remote.php/dav/files/admin/foobar.txt',
  51. owner: 'admin',
  52. mime: 'text/plain',
  53. permissions: Permission.ALL,
  54. })
  55. expect(action.enabled).toBeDefined()
  56. expect(action.enabled!([file], view)).toBe(true)
  57. })
  58. test('Disabled for non-dav ressources', () => {
  59. const file = new File({
  60. id: 1,
  61. source: 'https://domain.com/data/foobar.txt',
  62. owner: 'admin',
  63. mime: 'text/plain',
  64. })
  65. expect(action.enabled).toBeDefined()
  66. expect(action.enabled!([file], view)).toBe(false)
  67. })
  68. test('Disabled if more than one node', () => {
  69. const file1 = new File({
  70. id: 1,
  71. source: 'https://cloud.domain.com/remote.php/dav/files/admin/foo.txt',
  72. owner: 'admin',
  73. mime: 'text/plain',
  74. permissions: Permission.ALL,
  75. })
  76. const file2 = new File({
  77. id: 1,
  78. source: 'https://cloud.domain.com/remote.php/dav/files/admin/bar.txt',
  79. owner: 'admin',
  80. mime: 'text/plain',
  81. permissions: Permission.ALL,
  82. })
  83. expect(action.enabled).toBeDefined()
  84. expect(action.enabled!([file1, file2], view)).toBe(false)
  85. })
  86. test('Disabled for files', () => {
  87. const file = new File({
  88. id: 1,
  89. source: 'https://cloud.domain.com/remote.php/dav/files/admin/Foo/',
  90. owner: 'admin',
  91. mime: 'text/plain',
  92. })
  93. expect(action.enabled).toBeDefined()
  94. expect(action.enabled!([file], view)).toBe(false)
  95. })
  96. test('Disabled without UPDATE permissions', () => {
  97. const file = new File({
  98. id: 1,
  99. source: 'https://cloud.domain.com/remote.php/dav/files/admin/foobar.txt',
  100. owner: 'admin',
  101. mime: 'text/plain',
  102. permissions: Permission.READ,
  103. })
  104. expect(action.enabled).toBeDefined()
  105. expect(action.enabled!([file], view)).toBe(false)
  106. })
  107. })
  108. describe('Edit locally action execute tests', () => {
  109. test('Edit locally opens proper URL', async () => {
  110. jest.spyOn(axios, 'post').mockImplementation(async () => ({ data: { ocs: { data: { token: 'foobar' } } } }))
  111. jest.spyOn(ncDialogs, 'showError')
  112. const file = new File({
  113. id: 1,
  114. source: 'http://localhost/remote.php/dav/files/admin/foobar.txt',
  115. owner: 'admin',
  116. mime: 'text/plain',
  117. permissions: Permission.UPDATE,
  118. })
  119. const exec = await action.exec(file, view, '/')
  120. // Silent action
  121. expect(exec).toBe(null)
  122. expect(axios.post).toBeCalledTimes(1)
  123. expect(axios.post).toBeCalledWith('http://localhost/ocs/v2.php/apps/files/api/v1/openlocaleditor?format=json', { path: '/foobar.txt' })
  124. expect(ncDialogs.showError).toBeCalledTimes(0)
  125. expect(window.location.href).toBe('nc://open/test@localhost/foobar.txt?token=foobar')
  126. })
  127. test('Edit locally fails and show error', async () => {
  128. jest.spyOn(axios, 'post').mockImplementation(async () => ({}))
  129. jest.spyOn(ncDialogs, 'showError')
  130. const file = new File({
  131. id: 1,
  132. source: 'http://localhost/remote.php/dav/files/admin/foobar.txt',
  133. owner: 'admin',
  134. mime: 'text/plain',
  135. permissions: Permission.UPDATE,
  136. })
  137. const exec = await action.exec(file, view, '/')
  138. // Silent action
  139. expect(exec).toBe(null)
  140. expect(axios.post).toBeCalledTimes(1)
  141. expect(axios.post).toBeCalledWith('http://localhost/ocs/v2.php/apps/files/api/v1/openlocaleditor?format=json', { path: '/foobar.txt' })
  142. expect(ncDialogs.showError).toBeCalledTimes(1)
  143. expect(ncDialogs.showError).toBeCalledWith('Failed to redirect to client')
  144. expect(window.location.href).toBe('http://localhost/')
  145. })
  146. })