filestore-migration.spec.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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.findByPlaceholderText('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. pro: true,
  222. withDataDir: true,
  223. mongoVersion: '6.0',
  224. })
  225. startWith({
  226. pro: true,
  227. withDataDir: true,
  228. mongoVersion: '7.0',
  229. })
  230. before(async function () {
  231. await setMongoFeatureCompatibilityVersion('7.0')
  232. })
  233. startWith({
  234. pro: true,
  235. withDataDir: true,
  236. // implicit mongo upgrade to 8.0
  237. })
  238. before(async function () {
  239. await setMongoFeatureCompatibilityVersion('8.0')
  240. })
  241. } else {
  242. // 5.x
  243. startWith({ pro: true, withDataDir: true })
  244. defaultImage = 'frog.jpg'
  245. ensureUserExists({ email })
  246. before(function () {
  247. login(email)
  248. createProject(projectName, { type: 'Example project', open: false }).then(
  249. id => (projectId = id)
  250. )
  251. ;({ waitForCompileRateLimitCoolOff } = prepareWaitForNextCompileSlot())
  252. })
  253. }
  254. addNewBinaryFileAndCheckPrevious()
  255. function ensureStopOnFirstErrorIsActive() {
  256. cy.findByRole('button', { name: 'Toggle compile options menu' }).click()
  257. cy.findByRole('menuitem', {
  258. name: 'Stop on first error',
  259. }).then(el => {
  260. // NOTE: THIS IS BAD, but the selected option is otherwise not accessible :/
  261. if (
  262. el.get()[0]?.querySelector('.material-symbol')?.textContent !== 'check'
  263. ) {
  264. cy.findByRole('menuitem', {
  265. name: 'Stop on first error',
  266. }).click()
  267. // Clicking on "Stop on first error" closes the mode. Open it again.
  268. cy.findByRole('button', { name: 'Toggle compile options menu' }).click()
  269. }
  270. })
  271. cy.findByRole('menuitem', {
  272. name: 'Stop on first error',
  273. }).within(() => {
  274. cy.findByText('check').should('be.visible')
  275. })
  276. cy.findByRole('button', { name: 'Toggle compile options menu' }).click()
  277. }
  278. // -------------------
  279. // filestore-migration
  280. beforeEach(() => {
  281. login(email)
  282. waitForCompileRateLimitCoolOff(() => {
  283. openProjectById(projectId)
  284. })
  285. ensureStopOnFirstErrorIsActive()
  286. })
  287. function checkFilesAreAccessible() {
  288. it('can upload new binary file and read previous uploads', function () {
  289. previousBinaryFiles.push(prepareFileUploadTest(true))
  290. for (const check of previousBinaryFiles) {
  291. check()
  292. }
  293. })
  294. it('renders image of example project', () => {
  295. cy.findByTestId('file-tree').findByText(defaultImage).click()
  296. cy.get(`[alt="${defaultImage}"]`)
  297. .should('be.visible')
  298. .and('have.prop', 'naturalWidth')
  299. .should('be.greaterThan', 0)
  300. })
  301. it('can recompile from scratch', function () {
  302. const id = uuid()
  303. cy.findByText('\\maketitle').parent().click()
  304. cy.findByText('\\maketitle')
  305. .parent()
  306. .type(`\n\\section{{}Test Section ${id}}`)
  307. waitForCompileRateLimitCoolOff(() => {
  308. cy.findByRole('button', { name: 'Toggle compile options menu' }).click()
  309. cy.findByRole('menuitem', {
  310. name: 'Recompile from scratch',
  311. }).trigger('click')
  312. })
  313. cy.get('.pdf-viewer').should('contain.text', `Test Section ${id}`)
  314. })
  315. }
  316. describe('OVERLEAF_FILESTORE_MIGRATION_LEVEL not set', function () {
  317. startWith({ pro: true, withDataDir: true, vars: {} })
  318. checkFilesAreAccessible()
  319. })
  320. describe('OVERLEAF_FILESTORE_MIGRATION_LEVEL=0', function () {
  321. startWith({
  322. pro: true,
  323. withDataDir: true,
  324. vars: { OVERLEAF_FILESTORE_MIGRATION_LEVEL: '0' },
  325. })
  326. checkFilesAreAccessible()
  327. describe('OVERLEAF_FILESTORE_MIGRATION_LEVEL=1', function () {
  328. startWith({
  329. pro: true,
  330. withDataDir: true,
  331. vars: { OVERLEAF_FILESTORE_MIGRATION_LEVEL: '1' },
  332. })
  333. checkFilesAreAccessible()
  334. describe('OVERLEAF_FILESTORE_MIGRATION_LEVEL=2', function () {
  335. startWith({
  336. pro: true,
  337. withDataDir: true,
  338. vars: { OVERLEAF_FILESTORE_MIGRATION_LEVEL: '1' },
  339. })
  340. before(async function () {
  341. await runScript({
  342. cwd: 'services/history-v1',
  343. script: 'storage/scripts/back_fill_file_hash.mjs',
  344. args: ['--all'],
  345. })
  346. })
  347. startWith({
  348. pro: true,
  349. withDataDir: true,
  350. vars: { OVERLEAF_FILESTORE_MIGRATION_LEVEL: '2' },
  351. })
  352. checkFilesAreAccessible()
  353. describe('purge filestore data', function () {
  354. before(async function () {
  355. await purgeFilestoreData()
  356. })
  357. checkFilesAreAccessible()
  358. })
  359. })
  360. })
  361. })
  362. })