overleaf/services/web/test/UnitTests/coffee/ThirdPartyDataStore/TpdsUpdateSenderTests.coffee

150 lines
6 KiB
CoffeeScript
Raw Normal View History

2014-02-12 05:23:40 -05:00
SandboxedModule = require('sandboxed-module')
assert = require('assert')
require('chai').should()
modulePath = require('path').join __dirname, '../../../../app/js/Features/ThirdPartyDataStore/TpdsUpdateSender.js'
sinon = require('sinon')
ath = require('path')
project_id = "project_id_here"
user_id = "user_id_here"
read_only_ref_1 = "read_only_ref_1_id_here"
collaberator_ref_1 = "collaberator_ref_1_here"
project_name = "project_name_here"
thirdPartyDataStoreApiUrl = "http://third-party-json-store.herokuapp.com"
httpUsername = "user"
httpPass = "pass"
siteUrl = "http://www.localhost:3000"
httpAuthSiteUrl = "http://#{httpUsername}:#{httpPass}@www.localhost:3000"
filestoreUrl = "filestore.sharelatex.com"
describe 'TpdsUpdateSender', ->
beforeEach ->
@requestQueuer = (queue, meth, opts, callback)->
2014-02-12 05:23:40 -05:00
project = {owner_ref:user_id,readOnly_refs:[read_only_ref_1], collaberator_refs:[collaberator_ref_1]}
@Project = findById:sinon.stub().callsArgWith(2, null, project)
@docstoreUrl = "docstore.sharelatex.env"
@request = sinon.stub().returns(pipe:->)
@settings =
siteUrl:siteUrl
httpAuthSiteUrl:httpAuthSiteUrl,
apis:
thirdPartyDataStore: {url: thirdPartyDataStoreApiUrl}
filestore:
url: filestoreUrl
docstore:
pubUrl: @docstoreUrl
2014-02-12 05:23:40 -05:00
@updateSender = SandboxedModule.require modulePath, requires:
"settings-sharelatex": @settings
2014-02-12 05:23:40 -05:00
"logger-sharelatex":{log:->}
'../../models/Project': Project:@Project
'request':@request
"../../infrastructure/Metrics":
inc:->
describe "_enqueue", ->
it "should not call request if there is no tpdsworker url", (done)->
@updateSender._enqueue null, null, null, (err)=>
@request.called.should.equal false
done()
it "should post the message to the tpdsworker", (done)->
@settings.apis.tpdsworker = url:"www.tpdsworker.env"
group = "myproject"
method = "somemethod"
job = "do something"
@request.callsArgWith(1)
@updateSender._enqueue group, method, job, (err)=>
args = @request.args[0][0]
args.json.group.should.equal group
args.json.job.should.equal job
args.json.method.should.equal method
args.uri.should.equal "www.tpdsworker.env/enqueue/web_to_tpds_http_requests"
done()
2014-02-12 05:23:40 -05:00
describe 'sending updates', ->
it 'queues a post the file with user and file id', (done)->
2014-02-12 05:23:40 -05:00
file_id = '4545345'
path = '/some/path/here.jpg'
@updateSender._enqueue = (uid, method, job, callback)->
2014-02-12 05:23:40 -05:00
uid.should.equal project_id
job.method.should.equal "post"
job.streamOrigin.should.equal "#{filestoreUrl}/project/#{project_id}/file/#{file_id}"
expectedUrl = "#{thirdPartyDataStoreApiUrl}/user/#{user_id}/entity/#{encodeURIComponent(project_name)}#{encodeURIComponent(path)}"
job.uri.should.equal expectedUrl
job.headers.sl_all_user_ids.should.eql(JSON.stringify([collaberator_ref_1, read_only_ref_1, user_id]))
done()
@updateSender.addFile {project_id:project_id, file_id:file_id, path:path, project_name:project_name}, ->
it 'post doc with stream origin of docstore', (done)->
2014-02-12 05:23:40 -05:00
doc_id = "4545345"
path = "/some/path/here.tex"
lines = ["line1", "line2", "line3"]
@updateSender._enqueue = (uid, method, job, callback)=>
2014-02-12 05:23:40 -05:00
uid.should.equal project_id
job.method.should.equal "post"
expectedUrl = "#{thirdPartyDataStoreApiUrl}/user/#{user_id}/entity/#{encodeURIComponent(project_name)}#{encodeURIComponent(path)}"
job.uri.should.equal expectedUrl
job.streamOrigin.should.equal "#{@docstoreUrl}/project/#{project_id}/doc/#{doc_id}/raw"
2014-02-12 05:23:40 -05:00
job.headers.sl_all_user_ids.should.eql(JSON.stringify([collaberator_ref_1, read_only_ref_1, user_id]))
done()
@updateSender.addDoc {project_id:project_id, doc_id:doc_id, path:path, docLines:lines,project_name:project_name}
it 'deleting entity', (done)->
path = "/path/here/t.tex"
@updateSender._enqueue = (uid, method, job, callback)->
2014-02-12 05:23:40 -05:00
uid.should.equal project_id
job.method.should.equal "DELETE"
expectedUrl = "#{thirdPartyDataStoreApiUrl}/user/#{user_id}/entity/#{encodeURIComponent(project_name)}#{encodeURIComponent(path)}"
job.headers.sl_all_user_ids.should.eql(JSON.stringify([collaberator_ref_1, read_only_ref_1, user_id]))
job.uri.should.equal expectedUrl
done()
@updateSender.deleteEntity {project_id:project_id, path:path, project_name:project_name}
it 'moving entity', (done)->
startPath = "staring/here/file.tex"
endPath = "ending/here/file.tex"
@updateSender._enqueue = (uid, method, job, callback)->
2014-02-12 05:23:40 -05:00
uid.should.equal project_id
job.method.should.equal "put"
job.uri.should.equal "#{thirdPartyDataStoreApiUrl}/user/#{user_id}/entity"
job.json.startPath.should.equal "/#{project_name}/#{startPath}"
job.json.endPath.should.equal "/#{project_name}/#{endPath}"
job.headers.sl_all_user_ids.should.eql(JSON.stringify([collaberator_ref_1, read_only_ref_1, user_id]))
done()
@updateSender.moveEntity {project_id:project_id, startPath:startPath, endPath:endPath, project_name:project_name}
it 'should be able to rename a project using the move entity func', (done)->
oldProjectName = "/oldProjectName/"
newProjectName = "/newProjectName/"
@updateSender._enqueue = (uid, method, job, callback)->
2014-02-12 05:23:40 -05:00
uid.should.equal project_id
job.method.should.equal "put"
job.uri.should.equal "#{thirdPartyDataStoreApiUrl}/user/#{user_id}/entity"
job.json.startPath.should.equal oldProjectName
job.json.endPath.should.equal newProjectName
job.headers.sl_all_user_ids.should.eql(JSON.stringify([collaberator_ref_1, read_only_ref_1, user_id]))
done()
@updateSender.moveEntity {project_id:project_id, project_name:oldProjectName, newProjectName:newProjectName}
2014-08-13 12:26:18 -04:00
it "pollDropboxForUser", (done) ->
@updateSender._enqueue = sinon.stub().callsArg(3)
2014-08-13 12:26:18 -04:00
@updateSender.pollDropboxForUser user_id, (error) =>
@updateSender._enqueue
2014-08-13 12:26:18 -04:00
.calledWith(
"poll-dropbox:#{user_id}",
"standardHttpRequest",
{
method: "POST"
uri: "#{thirdPartyDataStoreApiUrl}/user/poll"
json:
user_ids: [user_id]
}
)
.should.equal true
done()