UploadFileScriptTests.mjs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. import { exec } from 'node:child_process'
  2. import fs from 'node:fs/promises'
  3. import { promisify } from 'node:util'
  4. import logger from '@overleaf/logger'
  5. import { expect } from 'chai'
  6. import Errors from '../../../app/src/Features/Errors/Errors.js'
  7. import ProjectLocator from '../../../app/src/Features/Project/ProjectLocator.mjs'
  8. import UserHelper from './helpers/User.mjs'
  9. const User = UserHelper.promises
  10. const TEST_FILE_PATH = '/tmp/upload-file-script-test.txt'
  11. const TEST_FILE_OVERWRITE_PATH = '/tmp/upload-file-script-overwrite-test.txt'
  12. describe('UploadFileScriptTests', function () {
  13. let user
  14. let projectId
  15. beforeEach('create user and project', async function () {
  16. user = new User()
  17. await user.login()
  18. projectId = await user.createProject('upload-file-script-project')
  19. })
  20. afterEach('cleanup temporary files', async function () {
  21. for (const filePath of [TEST_FILE_PATH, TEST_FILE_OVERWRITE_PATH]) {
  22. try {
  23. await fs.unlink(filePath)
  24. } catch (error) {
  25. if (error.code !== 'ENOENT') {
  26. throw error
  27. }
  28. }
  29. }
  30. })
  31. async function runScript(args) {
  32. let result
  33. try {
  34. result = await promisify(exec)(
  35. ['node', 'scripts/upload_file.mjs'].concat(args).join(' ')
  36. )
  37. } catch (error) {
  38. logger.error({ error }, 'script failed')
  39. throw error
  40. }
  41. return result
  42. }
  43. describe('create file', function () {
  44. it('should upload a local file into a project path', async function () {
  45. await fs.writeFile(TEST_FILE_PATH, 'upload-file-script-created-content')
  46. const destinationPath = '/uploads/created-by-script.txt'
  47. const { stdout } = await runScript([
  48. TEST_FILE_PATH,
  49. `--project-id=${projectId}`,
  50. `--user-id=${user._id.toString()}`,
  51. `--dest=${destinationPath}`,
  52. '-y',
  53. ])
  54. expect(stdout).to.include(
  55. `Applying: will create file at ${destinationPath}`
  56. )
  57. expect(stdout).to.match(
  58. new RegExp(
  59. `Success: created file\\. fileId=.* fileName=created-by-script\\.txt projectId=${projectId} path=${destinationPath}`
  60. )
  61. )
  62. expect(stdout).to.include('Done.')
  63. const { type, element } = await ProjectLocator.promises.findElementByPath(
  64. {
  65. project_id: projectId,
  66. path: destinationPath,
  67. exactCaseMatch: true,
  68. }
  69. )
  70. expect(type).to.equal('file')
  71. expect(element.name).to.equal('created-by-script.txt')
  72. })
  73. })
  74. describe('overwrite existing file', function () {
  75. it('should overwrite an existing destination when --force is provided', async function () {
  76. const project = await user.getProject(projectId)
  77. const rootFolderId = project.rootFolder[0]._id.toString()
  78. await user.uploadFileInProject(
  79. projectId,
  80. rootFolderId,
  81. '1pixel.png',
  82. 'existing.png',
  83. 'image/png'
  84. )
  85. await fs.writeFile(TEST_FILE_OVERWRITE_PATH, 'new-overwritten-content')
  86. const destinationPath = '/existing.png'
  87. const { stdout } = await runScript([
  88. TEST_FILE_OVERWRITE_PATH,
  89. `--project-id=${projectId}`,
  90. `--user-id=${user._id.toString()}`,
  91. `--dest=${destinationPath}`,
  92. '--force',
  93. '-y',
  94. ])
  95. expect(stdout).to.include(
  96. `Applying: will overwrite file at ${destinationPath}`
  97. )
  98. expect(stdout).to.include('Success: overwrote existing file.')
  99. const { type, element } = await ProjectLocator.promises.findElementByPath(
  100. {
  101. project_id: projectId,
  102. path: destinationPath,
  103. exactCaseMatch: true,
  104. }
  105. )
  106. expect(type).to.equal('file')
  107. expect(element.name).to.equal('existing.png')
  108. })
  109. })
  110. describe('dry run', function () {
  111. it('should show intended action without mutating the project', async function () {
  112. await fs.writeFile(TEST_FILE_PATH, 'dry-run-content')
  113. const destinationPath = '/dry-run-file.txt'
  114. const { stdout } = await runScript([
  115. TEST_FILE_PATH,
  116. `--project-id=${projectId}`,
  117. `--user-id=${user._id.toString()}`,
  118. `--dest=${destinationPath}`,
  119. '--dry-run',
  120. '-y',
  121. ])
  122. expect(stdout).to.include(
  123. `DRY RUN: would create file at ${destinationPath}`
  124. )
  125. expect(stdout).to.include('Done.')
  126. expect(stdout).to.not.include('Success:')
  127. try {
  128. await ProjectLocator.promises.findElementByPath({
  129. project_id: projectId,
  130. path: destinationPath,
  131. exactCaseMatch: true,
  132. })
  133. expect.fail('Expected destination path to not exist after dry run')
  134. } catch (error) {
  135. expect(error).to.be.instanceOf(Errors.NotFoundError)
  136. }
  137. })
  138. })
  139. describe('missing --force for existing destination', function () {
  140. it('should fail when destination exists and --force is not provided', async function () {
  141. const project = await user.getProject(projectId)
  142. const rootFolderId = project.rootFolder[0]._id.toString()
  143. await user.uploadFileInProject(
  144. projectId,
  145. rootFolderId,
  146. '1pixel.png',
  147. 'existing-without-force.png',
  148. 'image/png'
  149. )
  150. await fs.writeFile(TEST_FILE_OVERWRITE_PATH, 'content-not-used')
  151. const destinationPath = '/existing-without-force.png'
  152. try {
  153. await runScript([
  154. TEST_FILE_OVERWRITE_PATH,
  155. `--project-id=${projectId}`,
  156. `--user-id=${user._id.toString()}`,
  157. `--dest=${destinationPath}`,
  158. '-y',
  159. ])
  160. expect.fail('Expected upload_file script to fail without --force')
  161. } catch (error) {
  162. expect(error.stderr).to.include(
  163. `destination already exists at ${destinationPath} (type=file). Re-run with --force to overwrite.`
  164. )
  165. }
  166. const { type, element } = await ProjectLocator.promises.findElementByPath(
  167. {
  168. project_id: projectId,
  169. path: destinationPath,
  170. exactCaseMatch: true,
  171. }
  172. )
  173. expect(type).to.equal('file')
  174. expect(element.name).to.equal('existing-without-force.png')
  175. })
  176. })
  177. describe('destination is a folder', function () {
  178. it('should fail when destination path points to an existing folder', async function () {
  179. const project = await user.getProject(projectId)
  180. const rootFolderId = project.rootFolder[0]._id.toString()
  181. const folderName = 'uploads'
  182. const destinationPath = `/${folderName}`
  183. await new Promise((resolve, reject) => {
  184. user.request.post(
  185. {
  186. uri: `/project/${projectId}/folder`,
  187. json: {
  188. name: folderName,
  189. parent_folder_id: rootFolderId,
  190. },
  191. },
  192. (error, response, body) => {
  193. if (error) {
  194. return reject(error)
  195. }
  196. if (response.statusCode !== 200 || !body?._id) {
  197. return reject(
  198. new Error(
  199. `folder creation failed: status=${response.statusCode} body=${JSON.stringify(body)}`
  200. )
  201. )
  202. }
  203. resolve()
  204. }
  205. )
  206. })
  207. await fs.writeFile(TEST_FILE_PATH, 'folder-destination-content')
  208. try {
  209. await runScript([
  210. TEST_FILE_PATH,
  211. `--project-id=${projectId}`,
  212. `--user-id=${user._id.toString()}`,
  213. `--dest=${destinationPath}`,
  214. '-y',
  215. ])
  216. expect.fail(
  217. 'Expected upload_file script to fail for folder destination'
  218. )
  219. } catch (error) {
  220. expect(error.stderr).to.include(
  221. `destination is a folder at ${destinationPath}. Choose a file path within that folder.`
  222. )
  223. }
  224. const { type, element } = await ProjectLocator.promises.findElementByPath(
  225. {
  226. project_id: projectId,
  227. path: destinationPath,
  228. exactCaseMatch: true,
  229. }
  230. )
  231. expect(type).to.equal('folder')
  232. expect(element.name).to.equal(folderName)
  233. })
  234. })
  235. })