ProjectCollabratecDetailsTest.coffee 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. ObjectId = require("mongojs").ObjectId
  2. Path = require "path"
  3. SandboxedModule = require "sandboxed-module"
  4. assert = require "assert"
  5. chai = require "chai"
  6. sinon = require "sinon"
  7. sinonChai = require "sinon-chai"
  8. chai.use sinonChai
  9. expect = chai.expect
  10. modulePath = Path.join __dirname, "../../../../app/js/Features/Project/ProjectCollabratecDetailsHandler"
  11. describe "ProjectCollabratecDetailsHandler", ->
  12. beforeEach ->
  13. @projectId = ObjectId("5bea8747c7bba6012fcaceb3")
  14. @userId = ObjectId("5be316a9c7f6aa03802ea8fb")
  15. @ProjectModel = {}
  16. @ProjectCollabratecDetailsHandler = SandboxedModule.require modulePath, requires:
  17. "../../models/Project": { Project: @ProjectModel }
  18. @callback = sinon.stub()
  19. describe "initializeCollabratecProject", ->
  20. describe "when update succeeds", ->
  21. beforeEach ->
  22. @ProjectModel.update = sinon.stub().yields()
  23. @ProjectCollabratecDetailsHandler.initializeCollabratecProject @projectId, @userId, "collabratec-document-id", "collabratec-private-group-id", @callback
  24. it "should update project model", ->
  25. update = $set: {
  26. collabratecUsers: [ {
  27. user_id: @userId,
  28. collabratec_document_id: "collabratec-document-id",
  29. collabratec_privategroup_id: "collabratec-private-group-id"
  30. } ]
  31. }
  32. expect(@ProjectModel.update).to.have.been.calledWith { _id: @projectId }, update, @callback
  33. describe "when update has error", ->
  34. beforeEach ->
  35. @ProjectModel.update = sinon.stub().yields("error")
  36. @ProjectCollabratecDetailsHandler.initializeCollabratecProject @projectId, @userId, "collabratec-document-id", "collabratec-private-group-id", @callback
  37. it "should callback with error", ->
  38. expect(@callback).to.have.been.calledWith("error")
  39. describe "with invalid args", ->
  40. beforeEach ->
  41. @ProjectModel.update = sinon.stub()
  42. @ProjectCollabratecDetailsHandler.initializeCollabratecProject "bad-project-id", "bad-user-id", "collabratec-document-id", "collabratec-private-group-id", @callback
  43. it "should not update", ->
  44. expect(@ProjectModel.update).not.to.have.beenCalled
  45. it "should callback with error", ->
  46. expect(@callback.firstCall.args[0]).to.be.instanceOf Error
  47. describe "isLinkedCollabratecUserProject", ->
  48. beforeEach ->
  49. @ProjectModel.findOne = sinon.stub().yields()
  50. describe "when find succeeds", ->
  51. describe "when user project found", ->
  52. beforeEach ->
  53. @ProjectModel.findOne = sinon.stub().yields(null, "project")
  54. @ProjectCollabratecDetailsHandler.isLinkedCollabratecUserProject @projectId, @userId, @callback
  55. it "should call find with project and user id", ->
  56. expect(@ProjectModel.findOne).to.have.been.calledWithMatch {
  57. _id: ObjectId(@projectId)
  58. collabratecUsers: $elemMatch:
  59. user_id: ObjectId(@userId)
  60. }
  61. it "should callback with true", ->
  62. expect(@callback).to.have.been.calledWith null, true
  63. describe "when user project found", ->
  64. beforeEach ->
  65. @ProjectModel.findOne = sinon.stub().yields(null, null)
  66. @ProjectCollabratecDetailsHandler.isLinkedCollabratecUserProject @projectId, @userId, @callback
  67. it "should callback with false", ->
  68. expect(@callback).to.have.been.calledWith null, false
  69. describe "when find has error", ->
  70. beforeEach ->
  71. @ProjectModel.findOne = sinon.stub().yields("error")
  72. @ProjectCollabratecDetailsHandler.isLinkedCollabratecUserProject @projectId, @userId, @callback
  73. it "should callback with error", ->
  74. expect(@callback).to.have.been.calledWith "error"
  75. describe "with invalid args", ->
  76. beforeEach ->
  77. @ProjectModel.findOne = sinon.stub()
  78. @ProjectCollabratecDetailsHandler.isLinkedCollabratecUserProject "bad-project-id", "bad-user-id", @callback
  79. it "should not update", ->
  80. expect(@ProjectModel.findOne).not.to.have.beenCalled
  81. it "should callback with error", ->
  82. expect(@callback.firstCall.args[0]).to.be.instanceOf Error
  83. describe "linkCollabratecUserProject", ->
  84. describe "when update succeeds", ->
  85. beforeEach ->
  86. @ProjectModel.update = sinon.stub().yields()
  87. @ProjectCollabratecDetailsHandler.linkCollabratecUserProject @projectId, @userId, "collabratec-document-id", @callback
  88. it "should update project model", ->
  89. query =
  90. _id: @projectId
  91. collabratecUsers: $not: $elemMatch:
  92. collabratec_document_id: "collabratec-document-id"
  93. user_id: @userId
  94. update = $push: collabratecUsers:
  95. collabratec_document_id: "collabratec-document-id"
  96. user_id: @userId
  97. expect(@ProjectModel.update).to.have.been.calledWith query, update, @callback
  98. describe "when update has error", ->
  99. beforeEach ->
  100. @ProjectModel.update = sinon.stub().yields("error")
  101. @ProjectCollabratecDetailsHandler.linkCollabratecUserProject @projectId, @userId, "collabratec-document-id", @callback
  102. it "should callback with error", ->
  103. expect(@callback).to.have.been.calledWith("error")
  104. describe "with invalid args", ->
  105. beforeEach ->
  106. @ProjectModel.update = sinon.stub()
  107. @ProjectCollabratecDetailsHandler.linkCollabratecUserProject "bad-project-id", "bad-user-id", "collabratec-document-id", @callback
  108. it "should not update", ->
  109. expect(@ProjectModel.update).not.to.have.beenCalled
  110. it "should callback with error", ->
  111. expect(@callback.firstCall.args[0]).to.be.instanceOf Error
  112. describe "unlinkCollabratecUserProject", ->
  113. describe "when update succeeds", ->
  114. beforeEach ->
  115. @ProjectModel.update = sinon.stub().yields()
  116. @ProjectCollabratecDetailsHandler.unlinkCollabratecUserProject @projectId, @userId, @callback
  117. it "should update project model", ->
  118. query =
  119. _id: @projectId
  120. update = $pull: collabratecUsers:
  121. user_id: @userId
  122. expect(@ProjectModel.update).to.have.been.calledWith query, update, @callback
  123. describe "when update has error", ->
  124. beforeEach ->
  125. @ProjectModel.update = sinon.stub().yields("error")
  126. @ProjectCollabratecDetailsHandler.unlinkCollabratecUserProject @projectId, @userId, @callback
  127. it "should callback with error", ->
  128. expect(@callback).to.have.been.calledWith("error")
  129. describe "with invalid args", ->
  130. beforeEach ->
  131. @ProjectModel.update = sinon.stub()
  132. @ProjectCollabratecDetailsHandler.unlinkCollabratecUserProject "bad-project-id", "bad-user-id", @callback
  133. it "should not update", ->
  134. expect(@ProjectModel.update).not.to.have.beenCalled
  135. it "should callback with error", ->
  136. expect(@callback.firstCall.args[0]).to.be.instanceOf Error