TpdsUpdateSenderTests.coffee 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. SandboxedModule = require('sandboxed-module')
  2. assert = require('assert')
  3. require('chai').should()
  4. modulePath = require('path').join __dirname, '../../../../app/js/Features/ThirdPartyDataStore/TpdsUpdateSender.js'
  5. sinon = require('sinon')
  6. ath = require('path')
  7. project_id = "project_id_here"
  8. user_id = "user_id_here"
  9. read_only_ref_1 = "read_only_ref_1_id_here"
  10. collaberator_ref_1 = "collaberator_ref_1_here"
  11. project_name = "project_name_here"
  12. thirdPartyDataStoreApiUrl = "http://third-party-json-store.herokuapp.com"
  13. httpUsername = "user"
  14. httpPass = "pass"
  15. siteUrl = "http://www.localhost:3000"
  16. httpAuthSiteUrl = "http://#{httpUsername}:#{httpPass}@www.localhost:3000"
  17. filestoreUrl = "filestore.sharelatex.com"
  18. describe 'TpdsUpdateSender', ->
  19. beforeEach ->
  20. @requestQueuer = regist:(queue, meth, opts, callback)->
  21. project = {owner_ref:user_id,readOnly_refs:[read_only_ref_1], collaberator_refs:[collaberator_ref_1]}
  22. @Project = findById:sinon.stub().callsArgWith(2, null, project)
  23. @updateSender = SandboxedModule.require modulePath, requires:
  24. 'fairy':{connect:=>{queue:=>@requestQueuer}}
  25. "settings-sharelatex":
  26. siteUrl:siteUrl
  27. httpAuthSiteUrl:httpAuthSiteUrl,
  28. apis:
  29. thirdPartyDataStore: {url: thirdPartyDataStoreApiUrl}
  30. filestore:
  31. url: filestoreUrl
  32. redis:fairy:{}
  33. "logger-sharelatex":{log:->}
  34. '../../models/Project': Project:@Project
  35. 'request':->{pipe:->}
  36. describe 'sending updates', ->
  37. it 'ques a post the file with user and file id', (done)->
  38. file_id = '4545345'
  39. path = '/some/path/here.jpg'
  40. @requestQueuer.enqueue = (uid, method, job, callback)->
  41. uid.should.equal project_id
  42. job.method.should.equal "post"
  43. job.streamOrigin.should.equal "#{filestoreUrl}/project/#{project_id}/file/#{file_id}"
  44. expectedUrl = "#{thirdPartyDataStoreApiUrl}/user/#{user_id}/entity/#{encodeURIComponent(project_name)}#{encodeURIComponent(path)}"
  45. job.uri.should.equal expectedUrl
  46. job.headers.sl_all_user_ids.should.eql(JSON.stringify([collaberator_ref_1, read_only_ref_1, user_id]))
  47. done()
  48. @updateSender.addFile {project_id:project_id, file_id:file_id, path:path, project_name:project_name}, ->
  49. it 'post doc with path and lines', (done)->
  50. doc_id = "4545345"
  51. path = "/some/path/here.tex"
  52. lines = ["line1", "line2", "line3"]
  53. @requestQueuer.enqueue = (uid, method, job, callback)->
  54. uid.should.equal project_id
  55. job.method.should.equal "post"
  56. expectedUrl = "#{thirdPartyDataStoreApiUrl}/user/#{user_id}/entity/#{encodeURIComponent(project_name)}#{encodeURIComponent(path)}"
  57. job.uri.should.equal expectedUrl
  58. assert.deepEqual job.docLines, lines
  59. job.headers.sl_all_user_ids.should.eql(JSON.stringify([collaberator_ref_1, read_only_ref_1, user_id]))
  60. done()
  61. @updateSender.addDoc {project_id:project_id, doc_id:doc_id, path:path, docLines:lines,project_name:project_name}
  62. it 'deleting entity', (done)->
  63. path = "/path/here/t.tex"
  64. @requestQueuer.enqueue = (uid, method, job, callback)->
  65. uid.should.equal project_id
  66. job.method.should.equal "DELETE"
  67. expectedUrl = "#{thirdPartyDataStoreApiUrl}/user/#{user_id}/entity/#{encodeURIComponent(project_name)}#{encodeURIComponent(path)}"
  68. job.headers.sl_all_user_ids.should.eql(JSON.stringify([collaberator_ref_1, read_only_ref_1, user_id]))
  69. job.uri.should.equal expectedUrl
  70. done()
  71. @updateSender.deleteEntity {project_id:project_id, path:path, project_name:project_name}
  72. it 'moving entity', (done)->
  73. startPath = "staring/here/file.tex"
  74. endPath = "ending/here/file.tex"
  75. @requestQueuer.enqueue = (uid, method, job, callback)->
  76. uid.should.equal project_id
  77. job.method.should.equal "put"
  78. job.uri.should.equal "#{thirdPartyDataStoreApiUrl}/user/#{user_id}/entity"
  79. job.json.startPath.should.equal "/#{project_name}/#{startPath}"
  80. job.json.endPath.should.equal "/#{project_name}/#{endPath}"
  81. job.headers.sl_all_user_ids.should.eql(JSON.stringify([collaberator_ref_1, read_only_ref_1, user_id]))
  82. done()
  83. @updateSender.moveEntity {project_id:project_id, startPath:startPath, endPath:endPath, project_name:project_name}
  84. it 'should be able to rename a project using the move entity func', (done)->
  85. oldProjectName = "/oldProjectName/"
  86. newProjectName = "/newProjectName/"
  87. @requestQueuer.enqueue = (uid, method, job, callback)->
  88. uid.should.equal project_id
  89. job.method.should.equal "put"
  90. job.uri.should.equal "#{thirdPartyDataStoreApiUrl}/user/#{user_id}/entity"
  91. job.json.startPath.should.equal oldProjectName
  92. job.json.endPath.should.equal newProjectName
  93. job.headers.sl_all_user_ids.should.eql(JSON.stringify([collaberator_ref_1, read_only_ref_1, user_id]))
  94. done()
  95. @updateSender.moveEntity {project_id:project_id, project_name:oldProjectName, newProjectName:newProjectName}