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

171 lines
4.8 KiB
CoffeeScript
Raw Normal View History

sinon = require('sinon')
chai = require('chai')
should = chai.should()
expect = chai.expect
modulePath = "../../../../app/js/HttpController.js"
SandboxedModule = require('sandboxed-module')
describe "HttpController", ->
beforeEach ->
@HttpController = SandboxedModule.require modulePath, requires:
"logger-sharelatex": { log: sinon.stub() }
"./UpdatesManager": @UpdatesManager = {}
"./DiffManager": @DiffManager = {}
2014-03-10 12:58:26 -04:00
"./RestoreManager": @RestoreManager = {}
"./PackManager": @PackManager = {}
2015-08-31 17:13:18 -04:00
"./DocArchiveManager": @DocArchiveManager = {}
"./HealthChecker": @HealthChecker = {}
@doc_id = "doc-id-123"
@project_id = "project-id-123"
@next = sinon.stub()
@user_id = "mock-user-123"
2014-03-11 13:39:40 -04:00
@now = Date.now()
2014-03-21 11:57:17 -04:00
describe "flushDoc", ->
beforeEach ->
@req =
params:
doc_id: @doc_id
2014-03-19 12:40:55 -04:00
project_id: @project_id
@res =
send: sinon.stub()
2014-03-19 12:40:55 -04:00
@UpdatesManager.processUncompressedUpdatesWithLock = sinon.stub().callsArg(2)
2014-03-21 11:57:17 -04:00
@HttpController.flushDoc @req, @res, @next
2014-02-26 06:34:56 -05:00
it "should process the updates", ->
@UpdatesManager.processUncompressedUpdatesWithLock
2014-03-19 12:40:55 -04:00
.calledWith(@project_id, @doc_id)
.should.equal true
it "should return a success code", ->
@res.send.calledWith(204).should.equal true
2014-03-21 11:57:17 -04:00
describe "flushProject", ->
beforeEach ->
@req =
params:
project_id: @project_id
@res =
send: sinon.stub()
@UpdatesManager.processUncompressedUpdatesForProject = sinon.stub().callsArg(1)
@HttpController.flushProject @req, @res, @next
it "should process the updates", ->
@UpdatesManager.processUncompressedUpdatesForProject
.calledWith(@project_id)
.should.equal true
it "should return a success code", ->
@res.send.calledWith(204).should.equal true
describe "getDiff", ->
beforeEach ->
2014-03-10 12:58:26 -04:00
@from = 42
@to = 45
@req =
params:
doc_id: @doc_id
project_id: @project_id
query:
from: @from.toString()
to: @to.toString()
@res =
send: sinon.stub()
@diff = [ u: "mock-diff" ]
@DiffManager.getDiff = sinon.stub().callsArgWith(4, null, @diff)
@HttpController.getDiff @req, @res, @next
it "should get the diff", ->
@DiffManager.getDiff
.calledWith(@project_id, @doc_id, parseInt(@from, 10), parseInt(@to, 10))
.should.equal true
it "should return the diff", ->
@res.send.calledWith(JSON.stringify(diff: @diff)).should.equal true
describe "getUpdates", ->
beforeEach ->
@before = Date.now()
@nextBeforeTimestamp = @before - 100
@min_count = 10
@req =
params:
project_id: @project_id
query:
before: @before.toString()
min_count: @min_count.toString()
@res =
send: sinon.stub()
@updates = ["mock-summarized-updates"]
@UpdatesManager.getSummarizedProjectUpdates = sinon.stub().callsArgWith(2, null, @updates, @nextBeforeTimestamp)
@HttpController.getUpdates @req, @res, @next
it "should get the updates", ->
@UpdatesManager.getSummarizedProjectUpdates
.calledWith(@project_id, before: @before, min_count: @min_count)
.should.equal true
it "should return the formatted updates", ->
@res.send.calledWith(JSON.stringify(updates: @updates, nextBeforeTimestamp: @nextBeforeTimestamp)).should.equal true
2014-03-10 12:58:26 -04:00
describe "RestoreManager", ->
beforeEach ->
@version = "42"
@req =
params:
doc_id: @doc_id
project_id: @project_id
version: @version
headers:
"x-user-id": @user_id
2014-03-10 12:58:26 -04:00
@res =
send: sinon.stub()
@RestoreManager.restoreToBeforeVersion = sinon.stub().callsArg(4)
2014-03-10 12:58:26 -04:00
@HttpController.restore @req, @res, @next
it "should restore the document", ->
@RestoreManager.restoreToBeforeVersion
.calledWith(@project_id, @doc_id, parseInt(@version, 10), @user_id)
2014-03-10 12:58:26 -04:00
.should.equal true
it "should return a success code", ->
@res.send.calledWith(204).should.equal true
2015-08-31 17:13:18 -04:00
# describe "archiveProject", ->
# beforeEach ->
# @req =
# params:
# project_id: @project_id
# @res =
# send: sinon.stub()
# @DocArchiveManager.archiveAllDocsChanges = sinon.stub().callsArg(1)
# @HttpController.archiveProject @req, @res, @next
# it "should process archive doc changes", ->
# @DocArchiveManager.archiveAllDocsChanges
# .calledWith(@project_id)
# .should.equal true
# it "should return a success code", ->
# @res.send.calledWith(204).should.equal true
# describe "unArchiveProject", ->
# beforeEach ->
# @req =
# params:
# project_id: @project_id
# @res =
# send: sinon.stub()
# @DocArchiveManager.unArchiveAllDocsChanges = sinon.stub().callsArg(1)
# @HttpController.unArchiveProject @req, @res, @next
# it "should process unarchive doc changes", ->
# @DocArchiveManager.unArchiveAllDocsChanges
# .calledWith(@project_id)
# .should.equal true
# it "should return a success code", ->
# @res.send.calledWith(204).should.equal true