project_updates.test.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  1. const BPromise = require('bluebird')
  2. const { expect } = require('chai')
  3. const fs = BPromise.promisifyAll(require('fs'))
  4. const HTTPStatus = require('http-status')
  5. const fetch = require('node-fetch')
  6. const cleanup = require('../storage/support/cleanup')
  7. const fixtures = require('../storage/support/fixtures')
  8. const testFiles = require('../storage/support/test_files')
  9. const expectResponse = require('./support/expect_response')
  10. const testServer = require('./support/test_server')
  11. const core = require('overleaf-editor-core')
  12. const Change = core.Change
  13. const ChunkResponse = core.ChunkResponse
  14. const File = core.File
  15. const Operation = core.Operation
  16. const Origin = core.Origin
  17. const Snapshot = core.Snapshot
  18. const TextOperation = core.TextOperation
  19. const V2DocVersions = core.V2DocVersions
  20. const knex = require('../../../../storage').knex
  21. describe('history import', function () {
  22. beforeEach(cleanup.everything)
  23. beforeEach(fixtures.create)
  24. function changeToRaw(change) {
  25. return change.toRaw()
  26. }
  27. function makeChange(operation) {
  28. return new Change([operation], new Date(), [])
  29. }
  30. let basicAuthClient
  31. let pseudoJwtBasicAuthClient
  32. let clientForProject
  33. before(async function () {
  34. basicAuthClient = testServer.basicAuthClient
  35. pseudoJwtBasicAuthClient = testServer.pseudoJwtBasicAuthClient
  36. clientForProject = await testServer.createClientForProject('1')
  37. })
  38. it('creates blobs and then imports a snapshot and history', function () {
  39. // We need to be able to set the projectId to match an existing doc ID.
  40. const testProjectId = '1'
  41. const testFilePathname = 'main.tex'
  42. const testAuthors = [123, null]
  43. const testTextOperation0 = TextOperation.fromJSON({ textOperation: ['a'] })
  44. const testTextOperation1 = TextOperation.fromJSON({
  45. textOperation: [1, 'b'],
  46. })
  47. let testSnapshot
  48. return fetch(
  49. testServer.url(
  50. `/api/projects/${testProjectId}/blobs/${File.EMPTY_FILE_HASH}`
  51. ),
  52. {
  53. method: 'PUT',
  54. body: fs.createReadStream(testFiles.path('empty.tex')),
  55. headers: {
  56. Authorization: testServer.basicAuthHeader,
  57. },
  58. }
  59. )
  60. .then(response => {
  61. expect(response.ok).to.be.true
  62. })
  63. .then(() => {
  64. // Import project
  65. testSnapshot = new Snapshot()
  66. testSnapshot.addFile(
  67. testFilePathname,
  68. File.fromHash(File.EMPTY_FILE_HASH)
  69. )
  70. return basicAuthClient.apis.ProjectImport.importSnapshot1({
  71. project_id: testProjectId,
  72. snapshot: testSnapshot.toRaw(),
  73. })
  74. })
  75. .then(response => {
  76. // Check project is valid
  77. expect(response.obj.projectId).to.equal(testProjectId)
  78. })
  79. .then(() => {
  80. // Try importing the project again
  81. return basicAuthClient.apis.ProjectImport.importSnapshot1({
  82. project_id: testProjectId,
  83. snapshot: testSnapshot.toRaw(),
  84. })
  85. })
  86. .then(() => {
  87. // Check that importing a duplicate fails
  88. expect.fail()
  89. })
  90. .catch(expectResponse.conflict)
  91. .then(() => {
  92. // Get project history
  93. return clientForProject.apis.Project.getLatestHistory({
  94. project_id: testProjectId,
  95. })
  96. })
  97. .then(response => {
  98. // Check that the imported history is valid
  99. const chunk = ChunkResponse.fromRaw(response.obj).getChunk()
  100. const snapshot = chunk.getSnapshot()
  101. expect(snapshot.countFiles()).to.equal(1)
  102. const file = snapshot.getFile(testFilePathname)
  103. expect(file.getHash()).to.eql(File.EMPTY_FILE_HASH)
  104. expect(chunk.getChanges().length).to.equal(0)
  105. expect(chunk.getEndVersion()).to.equal(0)
  106. })
  107. .then(() => {
  108. // Import changes with an end version
  109. const changes = [
  110. makeChange(Operation.editFile(testFilePathname, testTextOperation0)),
  111. makeChange(Operation.editFile(testFilePathname, testTextOperation1)),
  112. ]
  113. changes[0].setAuthors(testAuthors)
  114. return basicAuthClient.apis.ProjectImport.importChanges1({
  115. project_id: testProjectId,
  116. changes: changes.map(changeToRaw),
  117. end_version: 0,
  118. return_snapshot: 'hashed',
  119. })
  120. })
  121. .then(response => {
  122. expect(response.status).to.equal(HTTPStatus.CREATED)
  123. const snapshot = Snapshot.fromRaw(response.obj)
  124. expect(snapshot.countFiles()).to.equal(1)
  125. expect(snapshot.getFile('main.tex').getHash()).to.equal(
  126. testFiles.STRING_AB_HASH
  127. )
  128. })
  129. .then(() => {
  130. // Get project history
  131. return clientForProject.apis.Project.getLatestHistory({
  132. project_id: testProjectId,
  133. })
  134. })
  135. .then(response => {
  136. // Check that the history is valid
  137. const chunkResponse = ChunkResponse.fromRaw(response.obj)
  138. const chunk = chunkResponse.getChunk()
  139. const snapshot = chunk.getSnapshot()
  140. expect(snapshot.countFiles()).to.equal(1)
  141. const file = snapshot.getFile(testFilePathname)
  142. expect(file.getHash()).to.equal(File.EMPTY_FILE_HASH)
  143. expect(chunk.getChanges().length).to.equal(2)
  144. const changeWithAuthors = chunk.getChanges()[0]
  145. expect(changeWithAuthors.getAuthors().length).to.equal(2)
  146. expect(changeWithAuthors.getAuthors()).to.deep.equal(testAuthors)
  147. expect(chunk.getStartVersion()).to.equal(0)
  148. expect(chunk.getEndVersion()).to.equal(2)
  149. })
  150. .then(() => {
  151. return clientForProject.apis.Project.getLatestHistory({
  152. project_id: testProjectId,
  153. })
  154. })
  155. .then(response => {
  156. // it should retrieve the same chunk
  157. const chunkResponse = ChunkResponse.fromRaw(response.obj)
  158. const chunk = chunkResponse.getChunk()
  159. expect(chunk.getChanges().length).to.equal(2)
  160. expect(chunk.getStartVersion()).to.equal(0)
  161. expect(chunk.getEndVersion()).to.equal(2)
  162. })
  163. .then(() => {
  164. // Get project's latest content
  165. return clientForProject.apis.Project.getLatestContent({
  166. project_id: testProjectId,
  167. })
  168. })
  169. .then(response => {
  170. // Check that the content is valid
  171. const snapshot = Snapshot.fromRaw(response.obj)
  172. expect(snapshot.countFiles()).to.equal(1)
  173. const file = snapshot.getFile(testFilePathname)
  174. expect(file.getContent()).to.equal('ab')
  175. })
  176. })
  177. it('rejects invalid changes in history', function () {
  178. const testProjectId = '1'
  179. const testFilePathname = 'main.tex'
  180. const testTextOperation = TextOperation.fromJSON({
  181. textOperation: ['a', 10],
  182. })
  183. let testSnapshot
  184. return fetch(
  185. testServer.url(
  186. `/api/projects/${testProjectId}/blobs/${File.EMPTY_FILE_HASH}`
  187. ),
  188. {
  189. method: 'PUT',
  190. body: fs.createReadStream(testFiles.path('empty.tex')),
  191. headers: {
  192. Authorization: testServer.basicAuthHeader,
  193. },
  194. }
  195. )
  196. .then(response => {
  197. expect(response.ok).to.be.true
  198. })
  199. .then(() => {
  200. // Import project
  201. testSnapshot = new Snapshot()
  202. testSnapshot.addFile(
  203. testFilePathname,
  204. File.fromHash(File.EMPTY_FILE_HASH)
  205. )
  206. return basicAuthClient.apis.ProjectImport.importSnapshot1({
  207. project_id: testProjectId,
  208. snapshot: testSnapshot.toRaw(),
  209. })
  210. })
  211. .then(response => {
  212. // Check project is valid
  213. expect(response.obj.projectId).to.equal(testProjectId)
  214. })
  215. .then(() => {
  216. // Import invalid changes
  217. const changes = [
  218. makeChange(Operation.editFile(testFilePathname, testTextOperation)),
  219. ]
  220. return basicAuthClient.apis.ProjectImport.importChanges1({
  221. project_id: testProjectId,
  222. end_version: 0,
  223. return_snapshot: 'hashed',
  224. changes: changes.map(changeToRaw),
  225. })
  226. })
  227. .then(() => {
  228. // Check that this fails
  229. expect.fail()
  230. })
  231. .catch(expectResponse.unprocessableEntity)
  232. .then(() => {
  233. // Get the latest content
  234. return clientForProject.apis.Project.getLatestContent({
  235. project_id: testProjectId,
  236. })
  237. })
  238. .then(response => {
  239. // Check that no changes have been stored
  240. const snapshot = Snapshot.fromRaw(response.obj)
  241. expect(snapshot.countFiles()).to.equal(1)
  242. const file = snapshot.getFile(testFilePathname)
  243. expect(file.getContent()).to.equal('')
  244. })
  245. .then(() => {
  246. // Send a change with the wrong end version that is not conflicting
  247. // with the latest snapshot
  248. const changes = [makeChange(Operation.removeFile(testFilePathname))]
  249. return basicAuthClient.apis.ProjectImport.importChanges1({
  250. project_id: testProjectId,
  251. end_version: 10000,
  252. changes,
  253. })
  254. })
  255. .then(() => {
  256. // Check that this fails
  257. expect.fail()
  258. })
  259. .catch(expectResponse.unprocessableEntity)
  260. .then(() => {
  261. // Get the latest project history
  262. return clientForProject.apis.Project.getLatestHistory({
  263. project_id: testProjectId,
  264. })
  265. })
  266. .then(response => {
  267. // Check that no changes have been stored
  268. const chunkResponse = ChunkResponse.fromRaw(response.obj)
  269. const changes = chunkResponse.getChunk().getChanges()
  270. expect(changes).to.have.length(0)
  271. })
  272. })
  273. it('creates and edits a file using changes', function () {
  274. const testProjectId = '1'
  275. const mainFilePathname = 'main.tex'
  276. const testFilePathname = 'test.tex'
  277. const testTextOperation = TextOperation.fromJSON({ textOperation: ['a'] })
  278. const inexistentAuthors = [1234, 5678]
  279. const projectVersion = '12345.0'
  280. const v2DocVersions = new V2DocVersions({
  281. 'random-doc-id': { pathname: 'doc-path.tex', v: 123 },
  282. })
  283. const testLabelOrigin = Origin.fromRaw({
  284. kind: 'saved ver',
  285. })
  286. const testRestoreOrigin = Origin.fromRaw({
  287. kind: 'restore',
  288. timestamp: '2016-01-01T00:00:00',
  289. version: 1,
  290. })
  291. let testSnapshot
  292. return fetch(
  293. testServer.url(
  294. `/api/projects/${testProjectId}/blobs/${File.EMPTY_FILE_HASH}`
  295. ),
  296. {
  297. method: 'PUT',
  298. body: fs.createReadStream(testFiles.path('empty.tex')),
  299. headers: {
  300. Authorization: testServer.basicAuthHeader,
  301. },
  302. }
  303. )
  304. .then(response => {
  305. expect(response.ok).to.be.true
  306. })
  307. .then(() => {
  308. // Import a project
  309. testSnapshot = new Snapshot()
  310. testSnapshot.addFile(
  311. mainFilePathname,
  312. File.fromHash(File.EMPTY_FILE_HASH)
  313. )
  314. return basicAuthClient.apis.ProjectImport.importSnapshot1({
  315. project_id: testProjectId,
  316. snapshot: testSnapshot.toRaw(),
  317. })
  318. })
  319. .then(response => {
  320. // Check that the project is valid
  321. expect(response.obj.projectId).to.equal(testProjectId)
  322. })
  323. .then(() => {
  324. // Import changes
  325. const testFile = File.fromHash(File.EMPTY_FILE_HASH)
  326. const changes = [
  327. makeChange(Operation.addFile(testFilePathname, testFile)),
  328. makeChange(Operation.editFile(testFilePathname, testTextOperation)),
  329. ]
  330. changes[0].setProjectVersion(projectVersion)
  331. changes[1].setAuthors(inexistentAuthors)
  332. changes[1].setV2DocVersions(v2DocVersions)
  333. return basicAuthClient.apis.ProjectImport.importChanges1({
  334. project_id: testProjectId,
  335. end_version: 0,
  336. return_snapshot: 'hashed',
  337. changes: changes.map(changeToRaw),
  338. })
  339. })
  340. .then(response => {
  341. expect(response.status).to.equal(HTTPStatus.CREATED)
  342. const snapshot = Snapshot.fromRaw(response.obj)
  343. expect(snapshot.countFiles()).to.equal(2)
  344. expect(snapshot.getFile('main.tex').getHash()).to.equal(
  345. File.EMPTY_FILE_HASH
  346. )
  347. expect(snapshot.getFile('test.tex').getHash()).to.equal(
  348. testFiles.STRING_A_HASH
  349. )
  350. })
  351. .then(() => {
  352. // Get the project history
  353. return clientForProject.apis.Project.getLatestHistory({
  354. project_id: testProjectId,
  355. })
  356. })
  357. .then(response => {
  358. // it should not fail when the some of the authors do not exist anymore
  359. const chunkResponse = ChunkResponse.fromRaw(response.obj)
  360. const changes = chunkResponse.getChunk().getChanges()
  361. expect(changes.length).to.equal(2)
  362. const changeWithAuthor = changes[1]
  363. expect(changeWithAuthor.getAuthors()).to.deep.equal(inexistentAuthors)
  364. })
  365. .then(() => {
  366. // it should retrieve the latest snapshot when the changes set is empty
  367. return basicAuthClient.apis.ProjectImport.importChanges1({
  368. project_id: testProjectId,
  369. end_version: 0,
  370. return_snapshot: 'hashed',
  371. changes: [],
  372. })
  373. })
  374. .then(response => {
  375. // Check latest snapshot
  376. const snapshot = Snapshot.fromRaw(response.obj)
  377. expect(snapshot.countFiles()).to.equal(2)
  378. expect(snapshot.getFile('main.tex').getHash()).to.equal(
  379. File.EMPTY_FILE_HASH
  380. )
  381. expect(snapshot.getFile('test.tex').getHash()).to.equal(
  382. testFiles.STRING_A_HASH
  383. )
  384. expect(snapshot.getProjectVersion()).to.equal(projectVersion)
  385. expect(snapshot.getV2DocVersions()).to.deep.equal(v2DocVersions)
  386. })
  387. .then(() => {
  388. // Import changes with origin
  389. const testFile = File.fromHash(File.EMPTY_FILE_HASH)
  390. const changes = [
  391. makeChange(Operation.removeFile(testFilePathname)),
  392. makeChange(Operation.addFile(testFilePathname, testFile)),
  393. ]
  394. changes[0].setOrigin(testLabelOrigin)
  395. changes[1].setOrigin(testRestoreOrigin)
  396. return basicAuthClient.apis.ProjectImport.importChanges1({
  397. project_id: testProjectId,
  398. end_version: 0,
  399. changes: changes.map(changeToRaw),
  400. })
  401. })
  402. .then(() => {
  403. // Get the latest history
  404. return clientForProject.apis.Project.getLatestHistory({
  405. project_id: testProjectId,
  406. })
  407. })
  408. .then(response => {
  409. // Check that the origin is stored
  410. const chunkResponse = ChunkResponse.fromRaw(response.obj)
  411. const changes = chunkResponse.getChunk().getChanges()
  412. expect(changes).to.have.length(4)
  413. expect(changes[2].getOrigin()).to.deep.equal(testLabelOrigin)
  414. expect(changes[3].getOrigin()).to.deep.equal(testRestoreOrigin)
  415. })
  416. .then(() => {
  417. // Import invalid changes
  418. const testFile = File.fromHash(File.EMPTY_FILE_HASH)
  419. const changes = [makeChange(Operation.addFile('../../a.tex', testFile))]
  420. return basicAuthClient.apis.ProjectImport.importChanges1({
  421. project_id: testProjectId,
  422. end_version: 0,
  423. changes: changes.map(changeToRaw),
  424. })
  425. })
  426. .then(() => {
  427. // Check that this fails and returns a 422
  428. expect.fail()
  429. })
  430. .catch(expectResponse.unprocessableEntity)
  431. })
  432. it('rejects text operations on binary files', function () {
  433. const testProjectId = '1'
  434. const testFilePathname = 'main.tex'
  435. const testTextOperation = TextOperation.fromJSON({ textOperation: ['bb'] })
  436. let testSnapshot
  437. return fetch(
  438. testServer.url(
  439. `/api/projects/${testProjectId}/blobs/${testFiles.NON_BMP_TXT_HASH}`
  440. ),
  441. {
  442. method: 'PUT',
  443. body: fs.createReadStream(testFiles.path('non_bmp.txt')),
  444. headers: {
  445. Authorization: testServer.basicAuthHeader,
  446. },
  447. }
  448. )
  449. .then(response => {
  450. expect(response.ok).to.be.true
  451. })
  452. .then(() => {
  453. // Import a project
  454. testSnapshot = new Snapshot()
  455. testSnapshot.addFile(
  456. testFilePathname,
  457. File.fromHash(testFiles.NON_BMP_TXT_HASH)
  458. )
  459. return basicAuthClient.apis.ProjectImport.importSnapshot1({
  460. project_id: testProjectId,
  461. snapshot: testSnapshot.toRaw(),
  462. })
  463. })
  464. .then(response => {
  465. // Check that the project is valid
  466. expect(response.obj.projectId).to.equal(testProjectId)
  467. })
  468. .then(() => {
  469. // Import invalid changes
  470. const changes = [
  471. makeChange(Operation.editFile(testFilePathname, testTextOperation)),
  472. ]
  473. return basicAuthClient.apis.ProjectImport.importChanges1({
  474. project_id: testProjectId,
  475. end_version: 0,
  476. changes: changes.map(changeToRaw),
  477. })
  478. })
  479. .then(() => {
  480. // Expect invalid changes to fail
  481. expect.fail()
  482. })
  483. .catch(expectResponse.unprocessableEntity)
  484. .then(() => {
  485. // Get latest content
  486. return clientForProject.apis.Project.getLatestContent({
  487. project_id: testProjectId,
  488. })
  489. })
  490. .then(response => {
  491. // Check that no changes were stored
  492. const snapshot = Snapshot.fromRaw(response.obj)
  493. expect(snapshot.countFiles()).to.equal(1)
  494. expect(snapshot.getFile(testFilePathname).getHash()).to.equal(
  495. testFiles.NON_BMP_TXT_HASH
  496. )
  497. })
  498. })
  499. it('accepts text operation on files with null characters if stringLength is present', function () {
  500. const testProjectId = '1'
  501. const mainFilePathname = 'main.tex'
  502. const testTextOperation = TextOperation.fromJSON({
  503. textOperation: [3, 'a'],
  504. })
  505. let testSnapshot
  506. function importChanges() {
  507. const changes = [
  508. makeChange(Operation.editFile(mainFilePathname, testTextOperation)),
  509. ]
  510. return basicAuthClient.apis.ProjectImport.importChanges1({
  511. project_id: testProjectId,
  512. end_version: 0,
  513. changes: changes.map(changeToRaw),
  514. })
  515. }
  516. function getLatestContent() {
  517. return clientForProject.apis.Project.getLatestContent({
  518. project_id: testProjectId,
  519. })
  520. }
  521. return fetch(
  522. testServer.url(
  523. `/api/projects/${testProjectId}/blobs/${testFiles.NULL_CHARACTERS_TXT_HASH}`
  524. ),
  525. {
  526. method: 'PUT',
  527. body: fs.createReadStream(testFiles.path('null_characters.txt')),
  528. headers: {
  529. Authorization: testServer.basicAuthHeader,
  530. },
  531. }
  532. )
  533. .then(response => {
  534. expect(response.ok).to.be.true
  535. })
  536. .then(() => {
  537. // Import project
  538. testSnapshot = new Snapshot()
  539. testSnapshot.addFile(
  540. mainFilePathname,
  541. File.fromHash(testFiles.NULL_CHARACTERS_TXT_HASH)
  542. )
  543. return basicAuthClient.apis.ProjectImport.importSnapshot1({
  544. project_id: testProjectId,
  545. snapshot: testSnapshot.toRaw(),
  546. })
  547. })
  548. .then(importChanges)
  549. .then(() => {
  550. // Expect invalid changes to fail
  551. expect.fail()
  552. })
  553. .catch(expectResponse.unprocessableEntity)
  554. .then(getLatestContent)
  555. .then(response => {
  556. // Check that no chaes were made
  557. const snapshot = Snapshot.fromRaw(response.obj)
  558. expect(snapshot.countFiles()).to.equal(1)
  559. expect(snapshot.getFile(mainFilePathname).getHash()).to.equal(
  560. testFiles.NULL_CHARACTERS_TXT_HASH
  561. )
  562. })
  563. .then(() => {
  564. // Set string length
  565. return knex('project_blobs').update(
  566. 'string_length',
  567. testFiles.NULL_CHARACTERS_TXT_BYTE_LENGTH
  568. )
  569. })
  570. .then(importChanges)
  571. .then(getLatestContent)
  572. .then(response => {
  573. const snapshot = Snapshot.fromRaw(response.obj)
  574. expect(snapshot.countFiles()).to.equal(1)
  575. expect(snapshot.getFile(mainFilePathname).getContent()).to.equal(
  576. '\x00\x00\x00a'
  577. )
  578. })
  579. })
  580. it('returns 404 when chunk is not found in bucket', function () {
  581. const testProjectId = '1'
  582. const fooChange = makeChange(Operation.removeFile('foo.tex'))
  583. return knex('chunks')
  584. .insert({
  585. doc_id: testProjectId,
  586. start_version: 0,
  587. end_version: 100,
  588. end_timestamp: null,
  589. })
  590. .then(() => {
  591. // Import changes
  592. return basicAuthClient.apis.ProjectImport.importChanges1({
  593. project_id: testProjectId,
  594. end_version: 100,
  595. changes: [fooChange.toRaw()],
  596. })
  597. })
  598. .then(() => {
  599. // Expect invalid changes to fail
  600. expect.fail()
  601. })
  602. .catch(expectResponse.notFound)
  603. })
  604. it('creates and returns changes with v2 author ids', function () {
  605. const testFilePathname = 'test.tex'
  606. const testTextOperation = TextOperation.fromJSON({ textOperation: ['a'] })
  607. const v2Authors = ['5a296963ad5e82432674c839', null]
  608. let testProjectId
  609. return BPromise.resolve(basicAuthClient.apis.Project.initializeProject())
  610. .then(response => {
  611. expect(response.status).to.equal(HTTPStatus.OK)
  612. testProjectId = response.obj.projectId
  613. expect(testProjectId).to.be.a('string')
  614. })
  615. .then(() => {
  616. return fetch(
  617. testServer.url(
  618. `/api/projects/${testProjectId}/blobs/${File.EMPTY_FILE_HASH}`
  619. ),
  620. {
  621. method: 'PUT',
  622. body: fs.createReadStream(testFiles.path('empty.tex')),
  623. headers: {
  624. Authorization: testServer.basicAuthHeader,
  625. },
  626. }
  627. )
  628. })
  629. .then(response => {
  630. expect(response.ok).to.be.true
  631. })
  632. .then(() => {
  633. // Import changes
  634. const testFile = File.fromHash(File.EMPTY_FILE_HASH)
  635. const changes = [
  636. makeChange(Operation.addFile(testFilePathname, testFile)),
  637. makeChange(Operation.editFile(testFilePathname, testTextOperation)),
  638. ]
  639. changes[1].setV2Authors(v2Authors)
  640. return basicAuthClient.apis.ProjectImport.importChanges1({
  641. project_id: testProjectId,
  642. end_version: 0,
  643. return_snapshot: 'hashed',
  644. changes: changes.map(changeToRaw),
  645. })
  646. })
  647. .then(response => {
  648. expect(response.status).to.equal(HTTPStatus.CREATED)
  649. const snapshot = Snapshot.fromRaw(response.obj)
  650. expect(snapshot.countFiles()).to.equal(1)
  651. expect(snapshot.getFile('test.tex').getHash()).to.equal(
  652. testFiles.STRING_A_HASH
  653. )
  654. })
  655. .then(() => {
  656. // Get project history
  657. return pseudoJwtBasicAuthClient.apis.Project.getLatestHistory({
  658. project_id: testProjectId,
  659. })
  660. })
  661. .then(response => {
  662. // it should not fail when the some of the authors do not exist anymore
  663. const chunkResponse = ChunkResponse.fromRaw(response.obj)
  664. const changes = chunkResponse.getChunk().getChanges()
  665. expect(changes.length).to.equal(2)
  666. const changeWithAuthor = changes[1]
  667. expect(changeWithAuthor.getV2Authors()).to.deep.equal(v2Authors)
  668. })
  669. })
  670. it('should reject invalid v2 author ids', function () {
  671. const testFilePathname = 'test.tex'
  672. const v2Authors = ['not-a-v2-id']
  673. let testProjectId
  674. return basicAuthClient.apis.Project.initializeProject()
  675. .then(response => {
  676. expect(response.status).to.equal(HTTPStatus.OK)
  677. testProjectId = response.obj.projectId
  678. expect(testProjectId).to.be.a('string')
  679. })
  680. .then(() => {
  681. return fetch(
  682. testServer.url(
  683. `/api/projects/${testProjectId}/blobs/${File.EMPTY_FILE_HASH}`
  684. ),
  685. {
  686. method: 'PUT',
  687. body: fs.createReadStream(testFiles.path('empty.tex')),
  688. headers: {
  689. Authorization: testServer.basicAuthHeader,
  690. },
  691. }
  692. )
  693. })
  694. .then(response => {
  695. expect(response.ok).to.be.true
  696. })
  697. .then(() => {
  698. // Import changes
  699. const testFile = File.fromHash(File.EMPTY_FILE_HASH)
  700. const changes = [
  701. makeChange(Operation.addFile(testFilePathname, testFile)),
  702. ]
  703. changes[0].v2Authors = v2Authors
  704. return basicAuthClient.apis.ProjectImport.importChanges1({
  705. project_id: testProjectId,
  706. end_version: 0,
  707. changes: changes.map(changeToRaw),
  708. })
  709. })
  710. .then(() => {
  711. // Check that invalid changes fail
  712. expect.fail()
  713. })
  714. .catch(expectResponse.unprocessableEntity)
  715. })
  716. it('should reject changes with both v1 and v2 authors ids', function () {
  717. const testFilePathname = 'test.tex'
  718. const v1Authors = [456]
  719. const v2Authors = ['5a296963ad5e82432674c839', null]
  720. let testProjectId
  721. return basicAuthClient.apis.Project.initializeProject()
  722. .then(response => {
  723. expect(response.status).to.equal(HTTPStatus.OK)
  724. testProjectId = response.obj.projectId
  725. expect(testProjectId).to.be.a('string')
  726. })
  727. .then(() => {
  728. return fetch(
  729. testServer.url(
  730. `/api/projects/${testProjectId}/blobs/${File.EMPTY_FILE_HASH}`
  731. ),
  732. {
  733. method: 'PUT',
  734. body: fs.createReadStream(testFiles.path('empty.tex')),
  735. headers: {
  736. Authorization: testServer.basicAuthHeader,
  737. },
  738. }
  739. )
  740. })
  741. .then(response => {
  742. expect(response.ok).to.be.true
  743. })
  744. .then(() => {
  745. // Import changes
  746. const testFile = File.fromHash(File.EMPTY_FILE_HASH)
  747. const changes = [
  748. makeChange(Operation.addFile(testFilePathname, testFile)),
  749. ]
  750. changes[0].authors = v1Authors
  751. changes[0].v2Authors = v2Authors
  752. return basicAuthClient.apis.ProjectImport.importChanges1({
  753. project_id: testProjectId,
  754. end_version: 0,
  755. changes: changes.map(changeToRaw),
  756. })
  757. })
  758. .then(() => {
  759. // Check that invalid changes fail
  760. expect.fail()
  761. })
  762. .catch(expectResponse.unprocessableEntity)
  763. })
  764. it("returns unprocessable if end_version isn't provided", function () {
  765. return basicAuthClient.apis.Project.initializeProject()
  766. .then(response => {
  767. expect(response.status).to.equal(HTTPStatus.OK)
  768. const projectId = response.obj.projectId
  769. expect(projectId).to.be.a('string')
  770. return projectId
  771. })
  772. .then(projectId => {
  773. // Import changes
  774. return basicAuthClient.apis.ProjectImport.importChanges1({
  775. project_id: projectId,
  776. changes: [],
  777. })
  778. })
  779. .then(() => {
  780. // Check that invalid changes fail
  781. expect.fail()
  782. })
  783. .catch(error => {
  784. expect(error.message).to.equal(
  785. 'Required parameter end_version is not provided'
  786. )
  787. })
  788. })
  789. it('returns unprocessable if return_snapshot is invalid', function () {
  790. return basicAuthClient.apis.Project.initializeProject()
  791. .then(response => {
  792. expect(response.status).to.equal(HTTPStatus.OK)
  793. return response.obj.projectId
  794. })
  795. .then(projectId => {
  796. // Import changes
  797. return basicAuthClient.apis.ProjectImport.importChanges1({
  798. project_id: projectId,
  799. changes: [],
  800. end_version: 0,
  801. return_snapshot: 'not_a_valid_value',
  802. })
  803. })
  804. .then(() => {
  805. // Check that invalid changes fail
  806. expect.fail()
  807. })
  808. .catch(error => {
  809. expect(error.status).to.equal(HTTPStatus.UNPROCESSABLE_ENTITY)
  810. expect(error.response.body.message).to.equal(
  811. 'invalid enum value: return_snapshot'
  812. )
  813. })
  814. })
  815. })