MongoAWS.coffee 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. @bulkLimit = @MongoAWS.bulkLimit
  27. @project_id = ObjectId().toString()
  28. @doc_id = ObjectId().toString()
  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.pipe = sinon.stub()
  37. @db.docHistory.find = sinon.stub().returns @db.docHistory
  38. @db.docHistory.pipe.returns
  39. pipe:->
  40. on: (type, cb)->
  41. if type == "finish"
  42. cb()
  43. @JSONStream.stringify = sinon.stub()
  44. @MongoAWS.archiveDocHistory @project_id, @doc_id, @callback
  45. it "should call the callback", ->
  46. @callback.calledWith(null).should.equal true
  47. describe "unArchiveDocHistory", ->
  48. beforeEach ->
  49. @awssdk.config = { update: sinon.stub() }
  50. @awssdk.S3 = sinon.stub()
  51. @s3streams.ReadStream = sinon.stub()
  52. @s3streams.ReadStream.returns
  53. #describe on 'open' behavior
  54. on: (type, cb)->
  55. pipe:->
  56. #describe on 'data' behavior
  57. on: (type, cb)->
  58. cb([])
  59. #describe on 'end' behavior
  60. on: (type, cb)->
  61. cb()
  62. #describe on 'error' behavior
  63. on: sinon.stub()
  64. @MongoAWS.handleBulk = sinon.stub()
  65. @MongoAWS.unArchiveDocHistory @project_id, @doc_id, @callback
  66. it "should call handleBulk", ->
  67. @MongoAWS.handleBulk.calledWith([],@callback).should.equal true
  68. describe "handleBulk", ->
  69. beforeEach ->
  70. @bulkOps = [{
  71. _id: ObjectId()
  72. doc_id: ObjectId()
  73. project_id: ObjectId()
  74. }, {
  75. _id: ObjectId()
  76. doc_id: ObjectId()
  77. project_id: ObjectId()
  78. }, {
  79. _id: ObjectId()
  80. doc_id: ObjectId()
  81. project_id: ObjectId()
  82. }]
  83. @bulk =
  84. find: sinon.stub().returns
  85. upsert: sinon.stub().returns
  86. updateOne: sinon.stub()
  87. execute: sinon.stub().callsArgWith(0, null, {})
  88. @db.docHistory = {}
  89. @db.docHistory.initializeUnorderedBulkOp = sinon.stub().returns @bulk
  90. @MongoAWS.handleBulk @bulkOps, @callback
  91. it "should call updateOne for each operation", ->
  92. @bulk.find.calledWith({_id:@bulkOps[0]._id}).should.equal true
  93. @bulk.find.calledWith({_id:@bulkOps[1]._id}).should.equal true
  94. @bulk.find.calledWith({_id:@bulkOps[2]._id}).should.equal true
  95. it "should call the callback", ->
  96. @callback.calledWith(null).should.equal true