MongoAWS.coffee 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. chai = require('chai')
  2. chai.should()
  3. sinon = require("sinon")
  4. modulePath = "../../../../app/js/MongoAWS.js"
  5. SandboxedModule = require('sandboxed-module')
  6. {ObjectId} = require("mongojs")
  7. describe "MongoAWS", ->
  8. beforeEach ->
  9. @MongoAWS = SandboxedModule.require modulePath, requires:
  10. "settings-sharelatex": @settings =
  11. filestore:
  12. s3:
  13. secret: "s3-secret"
  14. key: "s3-key"
  15. stores:
  16. user_files: "s3-bucket"
  17. "child_process": @child_process = {}
  18. "mongo-uri": @mongouri = {}
  19. "logger-sharelatex": @logger = {log: sinon.stub(), error: sinon.stub(), err:->}
  20. "aws-sdk": @awssdk = {}
  21. "fs": @fs = {}
  22. "s3-streams": @s3streams = {}
  23. "./mongojs" : { db: @db = {}, ObjectId: ObjectId }
  24. "JSONStream": @JSONStream = {}
  25. "readline-stream": @readline = sinon.stub()
  26. @project_id = ObjectId().toString()
  27. @doc_id = ObjectId().toString()
  28. @update = { v:123 }
  29. @callback = sinon.stub()
  30. describe "archiveDocHistory", ->
  31. beforeEach ->
  32. @awssdk.config = { update: sinon.stub() }
  33. @awssdk.S3 = sinon.stub()
  34. @s3streams.WriteStream = sinon.stub()
  35. @db.docHistory = {}
  36. @db.docHistory.on = sinon.stub()
  37. @db.docHistory.find = sinon.stub().returns @db.docHistory
  38. @db.docHistory.on.returns
  39. pipe:->
  40. pipe:->
  41. on: (type, cb)->
  42. on: (type, cb)->
  43. cb()
  44. @JSONStream.stringify = sinon.stub()
  45. @MongoAWS.archiveDocHistory @project_id, @doc_id, @update, @callback
  46. it "should call the callback", ->
  47. @callback.called.should.equal true
  48. describe "unArchiveDocHistory", ->
  49. beforeEach ->
  50. @awssdk.config = { update: sinon.stub() }
  51. @awssdk.S3 = sinon.stub()
  52. @s3streams.ReadStream = sinon.stub()
  53. @s3streams.ReadStream.returns
  54. #describe on 'open' behavior
  55. on: (type, cb)->
  56. #describe on 'error' behavior
  57. on: (type, cb)->
  58. pipe:->
  59. #describe on 'data' behavior
  60. on: (type, cb)->
  61. cb([])
  62. #describe on 'end' behavior
  63. on: (type, cb)->
  64. cb()
  65. #describe on 'error' behavior
  66. on: sinon.stub()
  67. @MongoAWS.handleBulk = sinon.stub()
  68. @MongoAWS.unArchiveDocHistory @project_id, @doc_id, @callback
  69. it "should call handleBulk", ->
  70. @MongoAWS.handleBulk.called.should.equal true
  71. describe "handleBulk", ->
  72. beforeEach ->
  73. @bulkOps = [{
  74. _id: ObjectId()
  75. doc_id: ObjectId()
  76. project_id: ObjectId()
  77. }, {
  78. _id: ObjectId()
  79. doc_id: ObjectId()
  80. project_id: ObjectId()
  81. }, {
  82. _id: ObjectId()
  83. doc_id: ObjectId()
  84. project_id: ObjectId()
  85. }]
  86. @bulk =
  87. find: sinon.stub().returns
  88. upsert: sinon.stub().returns
  89. updateOne: sinon.stub()
  90. execute: sinon.stub().callsArgWith(0, null, {})
  91. @db.docHistory = {}
  92. @db.docHistory.initializeUnorderedBulkOp = sinon.stub().returns @bulk
  93. @MongoAWS.handleBulk @bulkOps, @bulkOps.length, @callback
  94. it "should call updateOne for each operation", ->
  95. @bulk.find.calledWith({_id:@bulkOps[0]._id}).should.equal true
  96. @bulk.find.calledWith({_id:@bulkOps[1]._id}).should.equal true
  97. @bulk.find.calledWith({_id:@bulkOps[2]._id}).should.equal true
  98. it "should call the callback", ->
  99. @callback.calledWith(null).should.equal true