| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853 |
- const BPromise = require('bluebird')
- const { expect } = require('chai')
- const fs = BPromise.promisifyAll(require('node:fs'))
- const HTTPStatus = require('http-status')
- const fetch = require('node-fetch')
- const cleanup = require('../storage/support/cleanup')
- const fixtures = require('../storage/support/fixtures')
- const testFiles = require('../storage/support/test_files')
- const expectResponse = require('./support/expect_response')
- const testServer = require('./support/test_server')
- const core = require('overleaf-editor-core')
- const testProjects = require('./support/test_projects')
- const Change = core.Change
- const ChunkResponse = core.ChunkResponse
- const File = core.File
- const Operation = core.Operation
- const Origin = core.Origin
- const Snapshot = core.Snapshot
- const TextOperation = core.TextOperation
- const V2DocVersions = core.V2DocVersions
- const knex = require('../../../../storage').knex
- describe('history import', function () {
- beforeEach(cleanup.everything)
- beforeEach(fixtures.create)
- function changeToRaw(change) {
- return change.toRaw()
- }
- function makeChange(operation) {
- return new Change([operation], new Date(), [])
- }
- let basicAuthClient
- let pseudoJwtBasicAuthClient
- let clientForProject
- before(async function () {
- basicAuthClient = testServer.basicAuthClient
- pseudoJwtBasicAuthClient = testServer.pseudoJwtBasicAuthClient
- clientForProject = await testServer.createClientForProject('1')
- })
- it('creates blobs and then imports a snapshot and history', function () {
- // We need to be able to set the projectId to match an existing doc ID.
- const testProjectId = '1'
- const testFilePathname = 'main.tex'
- const testAuthors = [123, null]
- const testTextOperation0 = TextOperation.fromJSON({ textOperation: ['a'] })
- const testTextOperation1 = TextOperation.fromJSON({
- textOperation: [1, 'b'],
- })
- let testSnapshot
- return fetch(
- testServer.url(
- `/api/projects/${testProjectId}/blobs/${File.EMPTY_FILE_HASH}`
- ),
- {
- method: 'PUT',
- body: fs.createReadStream(testFiles.path('empty.tex')),
- headers: {
- Authorization: testServer.basicAuthHeader,
- },
- }
- )
- .then(response => {
- expect(response.ok).to.be.true
- })
- .then(() => {
- // Import project
- testSnapshot = new Snapshot()
- testSnapshot.addFile(
- testFilePathname,
- File.fromHash(File.EMPTY_FILE_HASH)
- )
- return basicAuthClient.apis.ProjectImport.importSnapshot1({
- project_id: testProjectId,
- snapshot: testSnapshot.toRaw(),
- })
- })
- .then(response => {
- // Check project is valid
- expect(response.obj.projectId).to.equal(testProjectId)
- })
- .then(() => {
- // Try importing the project again
- return basicAuthClient.apis.ProjectImport.importSnapshot1({
- project_id: testProjectId,
- snapshot: testSnapshot.toRaw(),
- })
- })
- .then(() => {
- // Check that importing a duplicate fails
- expect.fail()
- })
- .catch(expectResponse.conflict)
- .then(() => {
- // Get project history
- return clientForProject.apis.Project.getLatestHistory({
- project_id: testProjectId,
- })
- })
- .then(response => {
- // Check that the imported history is valid
- const chunk = ChunkResponse.fromRaw(response.obj).getChunk()
- const snapshot = chunk.getSnapshot()
- expect(snapshot.countFiles()).to.equal(1)
- const file = snapshot.getFile(testFilePathname)
- expect(file.getHash()).to.eql(File.EMPTY_FILE_HASH)
- expect(chunk.getChanges().length).to.equal(0)
- expect(chunk.getEndVersion()).to.equal(0)
- })
- .then(() => {
- // Import changes with an end version
- const changes = [
- makeChange(Operation.editFile(testFilePathname, testTextOperation0)),
- makeChange(Operation.editFile(testFilePathname, testTextOperation1)),
- ]
- changes[0].setAuthors(testAuthors)
- return basicAuthClient.apis.ProjectImport.importChanges1({
- project_id: testProjectId,
- changes: changes.map(changeToRaw),
- end_version: 0,
- return_snapshot: 'hashed',
- })
- })
- .then(response => {
- expect(response.status).to.equal(HTTPStatus.CREATED)
- const snapshot = Snapshot.fromRaw(response.obj)
- expect(snapshot.countFiles()).to.equal(1)
- expect(snapshot.getFile('main.tex').getHash()).to.equal(
- testFiles.STRING_AB_HASH
- )
- })
- .then(() => {
- // Get project history
- return clientForProject.apis.Project.getLatestHistory({
- project_id: testProjectId,
- })
- })
- .then(response => {
- // Check that the history is valid
- const chunkResponse = ChunkResponse.fromRaw(response.obj)
- const chunk = chunkResponse.getChunk()
- const snapshot = chunk.getSnapshot()
- expect(snapshot.countFiles()).to.equal(1)
- const file = snapshot.getFile(testFilePathname)
- expect(file.getHash()).to.equal(File.EMPTY_FILE_HASH)
- expect(chunk.getChanges().length).to.equal(2)
- const changeWithAuthors = chunk.getChanges()[0]
- expect(changeWithAuthors.getAuthors().length).to.equal(2)
- expect(changeWithAuthors.getAuthors()).to.deep.equal(testAuthors)
- expect(chunk.getStartVersion()).to.equal(0)
- expect(chunk.getEndVersion()).to.equal(2)
- })
- .then(() => {
- return clientForProject.apis.Project.getLatestHistory({
- project_id: testProjectId,
- })
- })
- .then(response => {
- // it should retrieve the same chunk
- const chunkResponse = ChunkResponse.fromRaw(response.obj)
- const chunk = chunkResponse.getChunk()
- expect(chunk.getChanges().length).to.equal(2)
- expect(chunk.getStartVersion()).to.equal(0)
- expect(chunk.getEndVersion()).to.equal(2)
- })
- .then(() => {
- // Get project's latest content
- return clientForProject.apis.Project.getLatestContent({
- project_id: testProjectId,
- })
- })
- .then(response => {
- // Check that the content is valid
- const snapshot = Snapshot.fromRaw(response.obj)
- expect(snapshot.countFiles()).to.equal(1)
- const file = snapshot.getFile(testFilePathname)
- expect(file.getContent()).to.equal('ab')
- })
- })
- it('rejects invalid changes in history', function () {
- const testProjectId = '1'
- const testFilePathname = 'main.tex'
- const testTextOperation = TextOperation.fromJSON({
- textOperation: ['a', 10],
- })
- let testSnapshot
- return fetch(
- testServer.url(
- `/api/projects/${testProjectId}/blobs/${File.EMPTY_FILE_HASH}`
- ),
- {
- method: 'PUT',
- body: fs.createReadStream(testFiles.path('empty.tex')),
- headers: {
- Authorization: testServer.basicAuthHeader,
- },
- }
- )
- .then(response => {
- expect(response.ok).to.be.true
- })
- .then(() => {
- // Import project
- testSnapshot = new Snapshot()
- testSnapshot.addFile(
- testFilePathname,
- File.fromHash(File.EMPTY_FILE_HASH)
- )
- return basicAuthClient.apis.ProjectImport.importSnapshot1({
- project_id: testProjectId,
- snapshot: testSnapshot.toRaw(),
- })
- })
- .then(response => {
- // Check project is valid
- expect(response.obj.projectId).to.equal(testProjectId)
- })
- .then(() => {
- // Import invalid changes
- const changes = [
- makeChange(Operation.editFile(testFilePathname, testTextOperation)),
- ]
- return basicAuthClient.apis.ProjectImport.importChanges1({
- project_id: testProjectId,
- end_version: 0,
- return_snapshot: 'hashed',
- changes: changes.map(changeToRaw),
- })
- })
- .then(() => {
- // Check that this fails
- expect.fail()
- })
- .catch(expectResponse.unprocessableEntity)
- .then(() => {
- // Get the latest content
- return clientForProject.apis.Project.getLatestContent({
- project_id: testProjectId,
- })
- })
- .then(response => {
- // Check that no changes have been stored
- const snapshot = Snapshot.fromRaw(response.obj)
- expect(snapshot.countFiles()).to.equal(1)
- const file = snapshot.getFile(testFilePathname)
- expect(file.getContent()).to.equal('')
- })
- .then(() => {
- // Send a change with the wrong end version that is not conflicting
- // with the latest snapshot
- const changes = [makeChange(Operation.removeFile(testFilePathname))]
- return basicAuthClient.apis.ProjectImport.importChanges1({
- project_id: testProjectId,
- end_version: 10000,
- changes,
- })
- })
- .then(() => {
- // Check that this fails
- expect.fail()
- })
- .catch(expectResponse.unprocessableEntity)
- .then(() => {
- // Get the latest project history
- return clientForProject.apis.Project.getLatestHistory({
- project_id: testProjectId,
- })
- })
- .then(response => {
- // Check that no changes have been stored
- const chunkResponse = ChunkResponse.fromRaw(response.obj)
- const changes = chunkResponse.getChunk().getChanges()
- expect(changes).to.have.length(0)
- })
- })
- it('creates and edits a file using changes', function () {
- const testProjectId = '1'
- const mainFilePathname = 'main.tex'
- const testFilePathname = 'test.tex'
- const testTextOperation = TextOperation.fromJSON({ textOperation: ['a'] })
- const inexistentAuthors = [1234, 5678]
- const projectVersion = '12345.0'
- const v2DocVersions = new V2DocVersions({
- 'random-doc-id': { pathname: 'doc-path.tex', v: 123 },
- })
- const testLabelOrigin = Origin.fromRaw({
- kind: 'saved ver',
- })
- const testRestoreOrigin = Origin.fromRaw({
- kind: 'restore',
- timestamp: '2016-01-01T00:00:00',
- version: 1,
- })
- let testSnapshot
- return fetch(
- testServer.url(
- `/api/projects/${testProjectId}/blobs/${File.EMPTY_FILE_HASH}`
- ),
- {
- method: 'PUT',
- body: fs.createReadStream(testFiles.path('empty.tex')),
- headers: {
- Authorization: testServer.basicAuthHeader,
- },
- }
- )
- .then(response => {
- expect(response.ok).to.be.true
- })
- .then(() => {
- // Import a project
- testSnapshot = new Snapshot()
- testSnapshot.addFile(
- mainFilePathname,
- File.fromHash(File.EMPTY_FILE_HASH)
- )
- return basicAuthClient.apis.ProjectImport.importSnapshot1({
- project_id: testProjectId,
- snapshot: testSnapshot.toRaw(),
- })
- })
- .then(response => {
- // Check that the project is valid
- expect(response.obj.projectId).to.equal(testProjectId)
- })
- .then(() => {
- // Import changes
- const testFile = File.fromHash(File.EMPTY_FILE_HASH)
- const changes = [
- makeChange(Operation.addFile(testFilePathname, testFile)),
- makeChange(Operation.editFile(testFilePathname, testTextOperation)),
- ]
- changes[0].setProjectVersion(projectVersion)
- changes[1].setAuthors(inexistentAuthors)
- changes[1].setV2DocVersions(v2DocVersions)
- return basicAuthClient.apis.ProjectImport.importChanges1({
- project_id: testProjectId,
- end_version: 0,
- return_snapshot: 'hashed',
- changes: changes.map(changeToRaw),
- })
- })
- .then(response => {
- expect(response.status).to.equal(HTTPStatus.CREATED)
- const snapshot = Snapshot.fromRaw(response.obj)
- expect(snapshot.countFiles()).to.equal(2)
- expect(snapshot.getFile('main.tex').getHash()).to.equal(
- File.EMPTY_FILE_HASH
- )
- expect(snapshot.getFile('test.tex').getHash()).to.equal(
- testFiles.STRING_A_HASH
- )
- })
- .then(() => {
- // Get the project history
- return clientForProject.apis.Project.getLatestHistory({
- project_id: testProjectId,
- })
- })
- .then(response => {
- // it should not fail when the some of the authors do not exist anymore
- const chunkResponse = ChunkResponse.fromRaw(response.obj)
- const changes = chunkResponse.getChunk().getChanges()
- expect(changes.length).to.equal(2)
- const changeWithAuthor = changes[1]
- expect(changeWithAuthor.getAuthors()).to.deep.equal(inexistentAuthors)
- })
- .then(() => {
- // it should retrieve the latest snapshot when the changes set is empty
- return basicAuthClient.apis.ProjectImport.importChanges1({
- project_id: testProjectId,
- end_version: 0,
- return_snapshot: 'hashed',
- changes: [],
- })
- })
- .then(response => {
- // Check latest snapshot
- const snapshot = Snapshot.fromRaw(response.obj)
- expect(snapshot.countFiles()).to.equal(2)
- expect(snapshot.getFile('main.tex').getHash()).to.equal(
- File.EMPTY_FILE_HASH
- )
- expect(snapshot.getFile('test.tex').getHash()).to.equal(
- testFiles.STRING_A_HASH
- )
- expect(snapshot.getProjectVersion()).to.equal(projectVersion)
- expect(snapshot.getV2DocVersions()).to.deep.equal(v2DocVersions)
- })
- .then(() => {
- // Import changes with origin
- const testFile = File.fromHash(File.EMPTY_FILE_HASH)
- const changes = [
- makeChange(Operation.removeFile(testFilePathname)),
- makeChange(Operation.addFile(testFilePathname, testFile)),
- ]
- changes[0].setOrigin(testLabelOrigin)
- changes[1].setOrigin(testRestoreOrigin)
- return basicAuthClient.apis.ProjectImport.importChanges1({
- project_id: testProjectId,
- end_version: 0,
- changes: changes.map(changeToRaw),
- })
- })
- .then(() => {
- // Get the latest history
- return clientForProject.apis.Project.getLatestHistory({
- project_id: testProjectId,
- })
- })
- .then(response => {
- // Check that the origin is stored
- const chunkResponse = ChunkResponse.fromRaw(response.obj)
- const changes = chunkResponse.getChunk().getChanges()
- expect(changes).to.have.length(4)
- expect(changes[2].getOrigin()).to.deep.equal(testLabelOrigin)
- expect(changes[3].getOrigin()).to.deep.equal(testRestoreOrigin)
- })
- .then(() => {
- // Import invalid changes
- const testFile = File.fromHash(File.EMPTY_FILE_HASH)
- const changes = [makeChange(Operation.addFile('../../a.tex', testFile))]
- return basicAuthClient.apis.ProjectImport.importChanges1({
- project_id: testProjectId,
- end_version: 0,
- changes: changes.map(changeToRaw),
- })
- })
- .then(() => {
- // Check that this fails and returns a 422
- expect.fail()
- })
- .catch(expectResponse.unprocessableEntity)
- })
- it('rejects text operations on binary files', function () {
- const testProjectId = '1'
- const testFilePathname = 'main.tex'
- const testTextOperation = TextOperation.fromJSON({ textOperation: ['bb'] })
- let testSnapshot
- return fetch(
- testServer.url(
- `/api/projects/${testProjectId}/blobs/${testFiles.NON_BMP_TXT_HASH}`
- ),
- {
- method: 'PUT',
- body: fs.createReadStream(testFiles.path('non_bmp.txt')),
- headers: {
- Authorization: testServer.basicAuthHeader,
- },
- }
- )
- .then(response => {
- expect(response.ok).to.be.true
- })
- .then(() => {
- // Import a project
- testSnapshot = new Snapshot()
- testSnapshot.addFile(
- testFilePathname,
- File.fromHash(testFiles.NON_BMP_TXT_HASH)
- )
- return basicAuthClient.apis.ProjectImport.importSnapshot1({
- project_id: testProjectId,
- snapshot: testSnapshot.toRaw(),
- })
- })
- .then(response => {
- // Check that the project is valid
- expect(response.obj.projectId).to.equal(testProjectId)
- })
- .then(() => {
- // Import invalid changes
- const changes = [
- makeChange(Operation.editFile(testFilePathname, testTextOperation)),
- ]
- return basicAuthClient.apis.ProjectImport.importChanges1({
- project_id: testProjectId,
- end_version: 0,
- changes: changes.map(changeToRaw),
- })
- })
- .then(() => {
- // Expect invalid changes to fail
- expect.fail()
- })
- .catch(expectResponse.unprocessableEntity)
- .then(() => {
- // Get latest content
- return clientForProject.apis.Project.getLatestContent({
- project_id: testProjectId,
- })
- })
- .then(response => {
- // Check that no changes were stored
- const snapshot = Snapshot.fromRaw(response.obj)
- expect(snapshot.countFiles()).to.equal(1)
- expect(snapshot.getFile(testFilePathname).getHash()).to.equal(
- testFiles.NON_BMP_TXT_HASH
- )
- })
- })
- it('accepts text operation on files with null characters if stringLength is present', function () {
- const testProjectId = '1'
- const mainFilePathname = 'main.tex'
- const testTextOperation = TextOperation.fromJSON({
- textOperation: [3, 'a'],
- })
- let testSnapshot
- function importChanges() {
- const changes = [
- makeChange(Operation.editFile(mainFilePathname, testTextOperation)),
- ]
- return basicAuthClient.apis.ProjectImport.importChanges1({
- project_id: testProjectId,
- end_version: 0,
- changes: changes.map(changeToRaw),
- })
- }
- function getLatestContent() {
- return clientForProject.apis.Project.getLatestContent({
- project_id: testProjectId,
- })
- }
- return fetch(
- testServer.url(
- `/api/projects/${testProjectId}/blobs/${testFiles.NULL_CHARACTERS_TXT_HASH}`
- ),
- {
- method: 'PUT',
- body: fs.createReadStream(testFiles.path('null_characters.txt')),
- headers: {
- Authorization: testServer.basicAuthHeader,
- },
- }
- )
- .then(response => {
- expect(response.ok).to.be.true
- })
- .then(() => {
- // Import project
- testSnapshot = new Snapshot()
- testSnapshot.addFile(
- mainFilePathname,
- File.fromHash(testFiles.NULL_CHARACTERS_TXT_HASH)
- )
- return basicAuthClient.apis.ProjectImport.importSnapshot1({
- project_id: testProjectId,
- snapshot: testSnapshot.toRaw(),
- })
- })
- .then(importChanges)
- .then(() => {
- // Expect invalid changes to fail
- expect.fail()
- })
- .catch(expectResponse.unprocessableEntity)
- .then(getLatestContent)
- .then(response => {
- // Check that no changes were made
- const snapshot = Snapshot.fromRaw(response.obj)
- expect(snapshot.countFiles()).to.equal(1)
- expect(snapshot.getFile(mainFilePathname).getHash()).to.equal(
- testFiles.NULL_CHARACTERS_TXT_HASH
- )
- })
- .then(() => {
- // Set string length
- return knex('project_blobs').update(
- 'string_length',
- testFiles.NULL_CHARACTERS_TXT_BYTE_LENGTH
- )
- })
- .then(importChanges)
- .then(getLatestContent)
- .then(response => {
- const snapshot = Snapshot.fromRaw(response.obj)
- expect(snapshot.countFiles()).to.equal(1)
- expect(snapshot.getFile(mainFilePathname).getContent()).to.equal(
- '\x00\x00\x00a'
- )
- })
- })
- it('returns 404 when chunk is not found in bucket', function () {
- const testProjectId = '1'
- const fooChange = makeChange(Operation.removeFile('foo.tex'))
- return knex('chunks')
- .insert({
- doc_id: testProjectId,
- start_version: 0,
- end_version: 100,
- end_timestamp: null,
- })
- .then(() => {
- // Import changes
- return basicAuthClient.apis.ProjectImport.importChanges1({
- project_id: testProjectId,
- end_version: 100,
- changes: [fooChange.toRaw()],
- })
- })
- .then(() => {
- // Expect invalid changes to fail
- expect.fail()
- })
- .catch(expectResponse.notFound)
- })
- it('creates and returns changes with v2 author ids', function () {
- const testFilePathname = 'test.tex'
- const testTextOperation = TextOperation.fromJSON({ textOperation: ['a'] })
- const v2Authors = ['5a296963ad5e82432674c839', null]
- let testProjectId
- return testProjects
- .createEmptyProject()
- .then(projectId => {
- testProjectId = projectId
- expect(testProjectId).to.be.a('string')
- })
- .then(() => {
- return fetch(
- testServer.url(
- `/api/projects/${testProjectId}/blobs/${File.EMPTY_FILE_HASH}`
- ),
- {
- method: 'PUT',
- body: fs.createReadStream(testFiles.path('empty.tex')),
- headers: {
- Authorization: testServer.basicAuthHeader,
- },
- }
- )
- })
- .then(response => {
- expect(response.ok).to.be.true
- })
- .then(() => {
- // Import changes
- const testFile = File.fromHash(File.EMPTY_FILE_HASH)
- const changes = [
- makeChange(Operation.addFile(testFilePathname, testFile)),
- makeChange(Operation.editFile(testFilePathname, testTextOperation)),
- ]
- changes[1].setV2Authors(v2Authors)
- return basicAuthClient.apis.ProjectImport.importChanges1({
- project_id: testProjectId,
- end_version: 0,
- return_snapshot: 'hashed',
- changes: changes.map(changeToRaw),
- })
- })
- .then(response => {
- expect(response.status).to.equal(HTTPStatus.CREATED)
- const snapshot = Snapshot.fromRaw(response.obj)
- expect(snapshot.countFiles()).to.equal(1)
- expect(snapshot.getFile('test.tex').getHash()).to.equal(
- testFiles.STRING_A_HASH
- )
- })
- .then(() => {
- // Get project history
- return pseudoJwtBasicAuthClient.apis.Project.getLatestHistory({
- project_id: testProjectId,
- })
- })
- .then(response => {
- // it should not fail when the some of the authors do not exist anymore
- const chunkResponse = ChunkResponse.fromRaw(response.obj)
- const changes = chunkResponse.getChunk().getChanges()
- expect(changes.length).to.equal(2)
- const changeWithAuthor = changes[1]
- expect(changeWithAuthor.getV2Authors()).to.deep.equal(v2Authors)
- })
- })
- it('should reject invalid v2 author ids', function () {
- const testFilePathname = 'test.tex'
- const v2Authors = ['not-a-v2-id']
- let testProjectId
- return testProjects
- .createEmptyProject()
- .then(projectId => {
- testProjectId = projectId
- expect(testProjectId).to.be.a('string')
- })
- .then(() => {
- return fetch(
- testServer.url(
- `/api/projects/${testProjectId}/blobs/${File.EMPTY_FILE_HASH}`
- ),
- {
- method: 'PUT',
- body: fs.createReadStream(testFiles.path('empty.tex')),
- headers: {
- Authorization: testServer.basicAuthHeader,
- },
- }
- )
- })
- .then(response => {
- expect(response.ok).to.be.true
- })
- .then(() => {
- // Import changes
- const testFile = File.fromHash(File.EMPTY_FILE_HASH)
- const changes = [
- makeChange(Operation.addFile(testFilePathname, testFile)),
- ]
- changes[0].v2Authors = v2Authors
- return basicAuthClient.apis.ProjectImport.importChanges1({
- project_id: testProjectId,
- end_version: 0,
- changes: changes.map(changeToRaw),
- })
- })
- .then(() => {
- // Check that invalid changes fail
- expect.fail()
- })
- .catch(expectResponse.unprocessableEntity)
- })
- it('should reject changes with both v1 and v2 authors ids', function () {
- const testFilePathname = 'test.tex'
- const v1Authors = [456]
- const v2Authors = ['5a296963ad5e82432674c839', null]
- let testProjectId
- return testProjects
- .createEmptyProject()
- .then(projectId => {
- testProjectId = projectId
- expect(testProjectId).to.be.a('string')
- })
- .then(() => {
- return fetch(
- testServer.url(
- `/api/projects/${testProjectId}/blobs/${File.EMPTY_FILE_HASH}`
- ),
- {
- method: 'PUT',
- body: fs.createReadStream(testFiles.path('empty.tex')),
- headers: {
- Authorization: testServer.basicAuthHeader,
- },
- }
- )
- })
- .then(response => {
- expect(response.ok).to.be.true
- })
- .then(() => {
- // Import changes
- const testFile = File.fromHash(File.EMPTY_FILE_HASH)
- const changes = [
- makeChange(Operation.addFile(testFilePathname, testFile)),
- ]
- changes[0].authors = v1Authors
- changes[0].v2Authors = v2Authors
- return basicAuthClient.apis.ProjectImport.importChanges1({
- project_id: testProjectId,
- end_version: 0,
- changes: changes.map(changeToRaw),
- })
- })
- .then(() => {
- // Check that invalid changes fail
- expect.fail()
- })
- .catch(expectResponse.unprocessableEntity)
- })
- it("returns unprocessable if end_version isn't provided", function () {
- return testProjects
- .createEmptyProject()
- .then(projectId => {
- expect(projectId).to.be.a('string')
- return projectId
- })
- .then(projectId => {
- // Import changes
- return basicAuthClient.apis.ProjectImport.importChanges1({
- project_id: projectId,
- changes: [],
- })
- })
- .then(() => {
- // Check that invalid changes fail
- expect.fail()
- })
- .catch(error => {
- expect(error.message).to.equal(
- 'Required parameter end_version is not provided'
- )
- })
- })
- it('returns unprocessable if return_snapshot is invalid', function () {
- return testProjects
- .createEmptyProject()
- .then(projectId => {
- // Import changes
- return basicAuthClient.apis.ProjectImport.importChanges1({
- project_id: projectId,
- changes: [],
- end_version: 0,
- return_snapshot: 'not_a_valid_value',
- })
- })
- .then(() => {
- // Check that invalid changes fail
- expect.fail()
- })
- .catch(error => {
- expect(error.status).to.equal(HTTPStatus.UNPROCESSABLE_ENTITY)
- expect(error.response.body.message).to.equal(
- 'invalid enum value: return_snapshot'
- )
- })
- })
- })
|