overleaf/services/track-changes/test/unit/js/HttpController/HttpControllerTests.js

200 lines
5.8 KiB
JavaScript
Raw Normal View History

/* eslint-disable
no-return-assign,
no-unused-vars,
*/
// TODO: This file was created by bulk-decaffeinate.
// Fix any style issues and re-enable lint.
/*
* decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
const sinon = require('sinon')
const { expect } = require('chai')
const modulePath = '../../../../app/js/HttpController.js'
const SandboxedModule = require('sandboxed-module')
2020-06-04 04:24:21 -04:00
describe('HttpController', function () {
beforeEach(function () {
this.HttpController = SandboxedModule.require(modulePath, {
2020-03-23 06:34:57 -04:00
singleOnly: true,
requires: {
'./UpdatesManager': (this.UpdatesManager = {}),
'./DiffManager': (this.DiffManager = {}),
'./RestoreManager': (this.RestoreManager = {}),
'./PackManager': (this.PackManager = {}),
'./DocArchiveManager': (this.DocArchiveManager = {}),
2021-07-13 07:04:43 -04:00
'./HealthChecker': (this.HealthChecker = {}),
},
})
this.doc_id = 'doc-id-123'
this.project_id = 'project-id-123'
this.next = sinon.stub()
this.user_id = 'mock-user-123'
return (this.now = Date.now())
})
2020-06-04 04:24:21 -04:00
describe('flushDoc', function () {
beforeEach(function () {
this.req = {
params: {
doc_id: this.doc_id,
2021-07-13 07:04:43 -04:00
project_id: this.project_id,
},
}
this.res = { sendStatus: sinon.stub() }
this.UpdatesManager.processUncompressedUpdatesWithLock = sinon
.stub()
.callsArg(2)
return this.HttpController.flushDoc(this.req, this.res, this.next)
})
2020-06-04 04:24:21 -04:00
it('should process the updates', function () {
return this.UpdatesManager.processUncompressedUpdatesWithLock
.calledWith(this.project_id, this.doc_id)
.should.equal(true)
})
2020-06-04 04:24:21 -04:00
return it('should return a success code', function () {
return this.res.sendStatus.calledWith(204).should.equal(true)
})
})
2020-06-04 04:24:21 -04:00
describe('flushProject', function () {
beforeEach(function () {
this.req = {
params: {
2021-07-13 07:04:43 -04:00
project_id: this.project_id,
},
}
this.res = { sendStatus: sinon.stub() }
this.UpdatesManager.processUncompressedUpdatesForProject = sinon
.stub()
.callsArg(1)
return this.HttpController.flushProject(this.req, this.res, this.next)
})
2020-06-04 04:24:21 -04:00
it('should process the updates', function () {
return this.UpdatesManager.processUncompressedUpdatesForProject
.calledWith(this.project_id)
.should.equal(true)
})
2020-06-04 04:24:21 -04:00
return it('should return a success code', function () {
return this.res.sendStatus.calledWith(204).should.equal(true)
})
})
2020-06-04 04:24:21 -04:00
describe('getDiff', function () {
beforeEach(function () {
this.from = 42
this.to = 45
this.req = {
params: {
doc_id: this.doc_id,
2021-07-13 07:04:43 -04:00
project_id: this.project_id,
},
query: {
from: this.from.toString(),
2021-07-13 07:04:43 -04:00
to: this.to.toString(),
},
}
this.res = { json: sinon.stub() }
this.diff = [{ u: 'mock-diff' }]
this.DiffManager.getDiff = sinon.stub().callsArgWith(4, null, this.diff)
return this.HttpController.getDiff(this.req, this.res, this.next)
})
2020-06-04 04:24:21 -04:00
it('should get the diff', function () {
return this.DiffManager.getDiff
.calledWith(
this.project_id,
this.doc_id,
parseInt(this.from, 10),
parseInt(this.to, 10)
)
.should.equal(true)
})
2020-06-04 04:24:21 -04:00
return it('should return the diff', function () {
return this.res.json.calledWith({ diff: this.diff }).should.equal(true)
})
})
2020-06-04 04:24:21 -04:00
describe('getUpdates', function () {
beforeEach(function () {
this.before = Date.now()
this.nextBeforeTimestamp = this.before - 100
this.min_count = 10
this.req = {
params: {
2021-07-13 07:04:43 -04:00
project_id: this.project_id,
},
query: {
before: this.before.toString(),
2021-07-13 07:04:43 -04:00
min_count: this.min_count.toString(),
},
}
this.res = { json: sinon.stub() }
this.updates = ['mock-summarized-updates']
this.UpdatesManager.getSummarizedProjectUpdates = sinon
.stub()
.callsArgWith(2, null, this.updates, this.nextBeforeTimestamp)
return this.HttpController.getUpdates(this.req, this.res, this.next)
})
2020-06-04 04:24:21 -04:00
it('should get the updates', function () {
return this.UpdatesManager.getSummarizedProjectUpdates
.calledWith(this.project_id, {
before: this.before,
2021-07-13 07:04:43 -04:00
min_count: this.min_count,
})
.should.equal(true)
})
2020-06-04 04:24:21 -04:00
return it('should return the formatted updates', function () {
return this.res.json
.calledWith({
updates: this.updates,
2021-07-13 07:04:43 -04:00
nextBeforeTimestamp: this.nextBeforeTimestamp,
})
.should.equal(true)
})
})
2020-06-04 04:24:21 -04:00
return describe('RestoreManager', function () {
beforeEach(function () {
this.version = '42'
this.req = {
params: {
doc_id: this.doc_id,
project_id: this.project_id,
2021-07-13 07:04:43 -04:00
version: this.version,
},
headers: {
2021-07-13 07:04:43 -04:00
'x-user-id': this.user_id,
},
}
this.res = { sendStatus: sinon.stub() }
this.RestoreManager.restoreToBeforeVersion = sinon.stub().callsArg(4)
return this.HttpController.restore(this.req, this.res, this.next)
})
2020-06-04 04:24:21 -04:00
it('should restore the document', function () {
return this.RestoreManager.restoreToBeforeVersion
.calledWith(
this.project_id,
this.doc_id,
parseInt(this.version, 10),
this.user_id
)
.should.equal(true)
})
2020-06-04 04:24:21 -04:00
return it('should return a success code', function () {
return this.res.sendStatus.calledWith(204).should.equal(true)
})
})
})