filestore-migration.spec.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. import { DEFAULT_PASSWORD, ensureUserExists, login } from './helpers/login'
  2. import {
  3. createProject,
  4. expectFileExists,
  5. openProjectById,
  6. prepareFileUploadTest,
  7. } from './helpers/project'
  8. import { isExcludedBySharding, startWith } from './helpers/config'
  9. import { prepareWaitForNextCompileSlot } from './helpers/compile'
  10. import { v4 as uuid } from 'uuid'
  11. import {
  12. purgeFilestoreData,
  13. runGruntTask,
  14. runScript,
  15. setMongoFeatureCompatibilityVersion,
  16. } from './helpers/hostAdminClient'
  17. function activateUserVersion1x(url: string, password = DEFAULT_PASSWORD) {
  18. cy.session(url, () => {
  19. cy.visit(url)
  20. cy.url().then(url => {
  21. if (url.includes('/login')) return
  22. cy.url().should('contain', '/user/password/set')
  23. cy.get('input[type="password"]').type(password)
  24. cy.findByRole('button', { name: 'Set new password' }).click()
  25. })
  26. })
  27. }
  28. describe('filestore migration', function () {
  29. if (isExcludedBySharding('LOCAL_ONLY')) return
  30. const email = 'user@example.com'
  31. // Branding of env vars changed in 5.x
  32. const sharelatexBrandedVars = {
  33. SHARELATEX_SITE_URL: 'http://sharelatex',
  34. SHARELATEX_MONGO_URL: 'mongodb://mongo/sharelatex',
  35. SHARELATEX_REDIS_HOST: 'redis',
  36. }
  37. const projectName = `project-${uuid()}`
  38. let defaultImage: string
  39. let projectId: string
  40. let waitForCompileRateLimitCoolOff: (fn: () => void) => void
  41. const previousBinaryFiles: (() => void)[] = []
  42. function avoid502() {
  43. // The next step will likely restart the instance and any following
  44. // requests will fail with a 502/bad gateway. Avoid this by navigating
  45. // away from the editor, which will reload upon receiving a
  46. // 'forceDisconnect' socket.io message.
  47. cy.visit('/project')
  48. }
  49. function addNewBinaryFileAndCheckPrevious(
  50. universeSelector = `img[alt="${defaultImage}"]`
  51. ) {
  52. before(function () {
  53. login(email)
  54. waitForCompileRateLimitCoolOff(() => {
  55. cy.visit(`/project/${projectId}`)
  56. })
  57. previousBinaryFiles.push(prepareFileUploadTest(true))
  58. cy.log('check binary files')
  59. for (const check of previousBinaryFiles) {
  60. check()
  61. }
  62. cy.findByRole('treeitem', { name: defaultImage }).click()
  63. cy.get(universeSelector)
  64. .should('be.visible')
  65. .and('have.prop', 'naturalWidth')
  66. .should('be.greaterThan', 0)
  67. avoid502()
  68. })
  69. }
  70. if (Cypress.env('FULL_FILESTORE_MIGRATION')) {
  71. // --------------
  72. // Server Pro 1.x
  73. startWith({
  74. pro: true,
  75. resetData: true,
  76. withDataDir: true,
  77. vars: sharelatexBrandedVars,
  78. version: '1.2.4',
  79. mongoVersion: '5.0',
  80. })
  81. defaultImage = 'universe.jpg'
  82. let activateURL: string
  83. before(async function () {
  84. const { stdout } = await runGruntTask({
  85. task: 'user:create-admin',
  86. args: ['--email', email],
  87. })
  88. ;[activateURL] = stdout.match(
  89. /http:\/\/.+\/user\/password\/set\?passwordResetToken=\S+/
  90. )!
  91. })
  92. before(function () {
  93. activateUserVersion1x(activateURL)
  94. login(email)
  95. cy.visit('/project')
  96. // Legacy angular based UI uses links instead of buttons
  97. cy.findByRole('link', {
  98. name: /Create First Project|New Project/,
  99. }).click()
  100. cy.findByRole('link', { name: 'Example Project' }).click()
  101. cy.findByLabelText('Project name').type(projectName)
  102. cy.findByRole('button', { name: 'Create' }).click()
  103. cy.url()
  104. .should('match', /\/project\/[a-fA-F0-9]{24}/)
  105. .then(url => (projectId = url.split('/').pop()!))
  106. let queueReset
  107. ;({ waitForCompileRateLimitCoolOff, queueReset } =
  108. prepareWaitForNextCompileSlot())
  109. queueReset()
  110. // Create a new binary file
  111. cy.get(`a[tooltip="Upload"]`).click()
  112. const name = `${uuid()}.txt`
  113. // Binary file detection is not sophisticated in version 1.x
  114. const binName = name.replace('.txt', '.bin')
  115. const content = `Test File Content ${name} \x00`
  116. cy.get('input[type=file]')
  117. .first()
  118. .selectFile(
  119. {
  120. contents: Cypress.Buffer.from(content),
  121. fileName: binName,
  122. lastModified: Date.now(),
  123. },
  124. { force: true }
  125. )
  126. // Rename back to .txt to enable preview
  127. cy.findByText(binName).click()
  128. cy.findByText(binName).dblclick()
  129. cy.focused().type(name + '{del}'.repeat('.bin'.length) + '{enter}')
  130. // Switch back and forth
  131. cy.findByText('universe.jpg').click()
  132. cy.findByText(name).click()
  133. cy.findByText(content)
  134. .parent()
  135. .parent()
  136. .should('have.class', 'text-preview')
  137. previousBinaryFiles.push(() => expectFileExists(name, true, content))
  138. avoid502()
  139. })
  140. // --------------
  141. // Server Pro 2.x
  142. startWith({
  143. pro: true,
  144. withDataDir: true,
  145. vars: sharelatexBrandedVars,
  146. version: '2.7.1',
  147. mongoVersion: '5.0',
  148. })
  149. before(function () {
  150. // Cypress strips the Content-Length header: https://github.com/cypress-io/cypress/issues/16469
  151. // Server Pro 2.x does not gracefully handle a missing value.
  152. cy.intercept(
  153. {
  154. method: 'HEAD',
  155. url: `http://sharelatex/project/${projectId}/file/*`,
  156. times: previousBinaryFiles.length + 1,
  157. },
  158. req => {
  159. req.continue(res => {
  160. res.headers['Content-Length'] = '60'
  161. })
  162. }
  163. )
  164. })
  165. // Server Pro 2.x does not have alt tags on images.
  166. addNewBinaryFileAndCheckPrevious('img')
  167. // ----------------------------------
  168. // Server Pro 3.x + history migration
  169. startWith({
  170. pro: true,
  171. withDataDir: true,
  172. vars: sharelatexBrandedVars,
  173. version: '3.5.13',
  174. mongoVersion: '5.0',
  175. })
  176. addNewBinaryFileAndCheckPrevious() // before history migration
  177. before(async function () {
  178. await runScript({
  179. cwd: 'services/web',
  180. script: 'scripts/history/migrate_history.js',
  181. args: [
  182. '--force-clean',
  183. '--fix-invalid-characters',
  184. '--convert-large-docs-to-file',
  185. ],
  186. hasOverleafEnv: false,
  187. user: 'root',
  188. })
  189. })
  190. before(async function () {
  191. await runScript({
  192. cwd: 'services/web',
  193. script: 'scripts/history/clean_sl_history_data.js',
  194. hasOverleafEnv: false,
  195. })
  196. })
  197. addNewBinaryFileAndCheckPrevious() // after history migration
  198. // ------------------------------
  199. // Server Pro 4.x + mongo upgrade
  200. startWith({
  201. pro: true,
  202. withDataDir: true,
  203. vars: sharelatexBrandedVars,
  204. version: '4.2.9',
  205. mongoVersion: '5.0',
  206. })
  207. startWith({
  208. pro: true,
  209. withDataDir: true,
  210. vars: sharelatexBrandedVars,
  211. version: '4.2.9',
  212. mongoVersion: '6.0',
  213. })
  214. before(async function () {
  215. await setMongoFeatureCompatibilityVersion('6.0')
  216. })
  217. addNewBinaryFileAndCheckPrevious()
  218. // ------------------------------------------
  219. // Server Pro 5.x + mongo upgrade 6 -> 7 -> 8
  220. startWith({
  221. version: '5.5.5',
  222. pro: true,
  223. withDataDir: true,
  224. mongoVersion: '6.0',
  225. })
  226. startWith({
  227. version: '5.5.5',
  228. pro: true,
  229. withDataDir: true,
  230. mongoVersion: '7.0',
  231. })
  232. before(async function () {
  233. await setMongoFeatureCompatibilityVersion('7.0')
  234. })
  235. startWith({
  236. version: '5.5.5',
  237. pro: true,
  238. withDataDir: true,
  239. // implicit mongo upgrade to 8.0
  240. })
  241. before(async function () {
  242. await setMongoFeatureCompatibilityVersion('8.0')
  243. })
  244. } else {
  245. // 5.x
  246. startWith({ version: '5.5.5', pro: true, withDataDir: true })
  247. defaultImage = 'frog.jpg'
  248. ensureUserExists({ email })
  249. before(function () {
  250. login(email)
  251. createProject(projectName, { type: 'Example project', open: false }).then(
  252. id => (projectId = id)
  253. )
  254. ;({ waitForCompileRateLimitCoolOff } = prepareWaitForNextCompileSlot())
  255. })
  256. }
  257. addNewBinaryFileAndCheckPrevious()
  258. function ensureStopOnFirstErrorIsActive() {
  259. cy.findByRole('button', { name: 'Toggle compile options menu' }).click()
  260. cy.findByRole('menuitem', {
  261. name: 'Stop on first error',
  262. }).then(el => {
  263. // NOTE: THIS IS BAD, but the selected option is otherwise not accessible :/
  264. if (
  265. el.get()[0]?.querySelector('.material-symbol')?.textContent !== 'check'
  266. ) {
  267. cy.findByRole('menuitem', {
  268. name: 'Stop on first error',
  269. }).click()
  270. // Clicking on "Stop on first error" closes the mode. Open it again.
  271. cy.findByRole('button', { name: 'Toggle compile options menu' }).click()
  272. }
  273. })
  274. cy.findByRole('menuitem', {
  275. name: 'Stop on first error',
  276. }).within(() => {
  277. cy.findByText('check').should('be.visible')
  278. })
  279. cy.findByRole('button', { name: 'Toggle compile options menu' }).click()
  280. }
  281. // -------------------
  282. // filestore-migration
  283. beforeEach(() => {
  284. login(email)
  285. waitForCompileRateLimitCoolOff(() => {
  286. openProjectById(projectId)
  287. })
  288. ensureStopOnFirstErrorIsActive()
  289. })
  290. function checkFilesAreAccessible() {
  291. it('can upload new binary file and read previous uploads', function () {
  292. previousBinaryFiles.push(prepareFileUploadTest(true))
  293. for (const check of previousBinaryFiles) {
  294. check()
  295. }
  296. })
  297. it('renders image of example project', () => {
  298. cy.findByTestId('file-tree').findByText(defaultImage).click()
  299. cy.get(`[alt="${defaultImage}"]`)
  300. .should('be.visible')
  301. .and('have.prop', 'naturalWidth')
  302. .should('be.greaterThan', 0)
  303. })
  304. it('can recompile from scratch', function () {
  305. const id = uuid()
  306. cy.findByText('\\maketitle').parent().click()
  307. cy.findByText('\\maketitle')
  308. .parent()
  309. .type(`\n\\section{{}Test Section ${id}}`)
  310. waitForCompileRateLimitCoolOff(() => {
  311. cy.findByRole('button', { name: 'Toggle compile options menu' }).click()
  312. cy.findByRole('menuitem', {
  313. name: 'Recompile from scratch',
  314. }).trigger('click')
  315. })
  316. cy.get('.pdf-viewer').should('contain.text', `Test Section ${id}`)
  317. })
  318. }
  319. describe('OVERLEAF_FILESTORE_MIGRATION_LEVEL not set', function () {
  320. startWith({ version: '5.5.5', pro: true, withDataDir: true, vars: {} })
  321. checkFilesAreAccessible()
  322. })
  323. describe('OVERLEAF_FILESTORE_MIGRATION_LEVEL=0', function () {
  324. startWith({
  325. version: '5.5.5',
  326. pro: true,
  327. withDataDir: true,
  328. vars: { OVERLEAF_FILESTORE_MIGRATION_LEVEL: '0' },
  329. })
  330. checkFilesAreAccessible()
  331. describe('OVERLEAF_FILESTORE_MIGRATION_LEVEL=1', function () {
  332. startWith({
  333. version: '5.5.5',
  334. pro: true,
  335. withDataDir: true,
  336. vars: { OVERLEAF_FILESTORE_MIGRATION_LEVEL: '1' },
  337. })
  338. checkFilesAreAccessible()
  339. describe('OVERLEAF_FILESTORE_MIGRATION_LEVEL=2', function () {
  340. startWith({
  341. version: '5.5.5',
  342. pro: true,
  343. withDataDir: true,
  344. vars: { OVERLEAF_FILESTORE_MIGRATION_LEVEL: '1' },
  345. })
  346. before(async function () {
  347. await runScript({
  348. cwd: 'services/history-v1',
  349. script: 'storage/scripts/back_fill_file_hash.mjs',
  350. args: ['--all'],
  351. })
  352. })
  353. startWith({
  354. version: '5.5.5',
  355. pro: true,
  356. withDataDir: true,
  357. vars: { OVERLEAF_FILESTORE_MIGRATION_LEVEL: '2' },
  358. })
  359. checkFilesAreAccessible()
  360. describe('purge filestore data', function () {
  361. before(async function () {
  362. await purgeFilestoreData()
  363. })
  364. checkFilesAreAccessible()
  365. describe('latest', function () {
  366. startWith({
  367. pro: true,
  368. withDataDir: true,
  369. vars: { OVERLEAF_FILESTORE_MIGRATION_LEVEL: '2' },
  370. })
  371. checkFilesAreAccessible()
  372. })
  373. })
  374. })
  375. })
  376. })
  377. })