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.0KB

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