ProjectCollabratecDetailsTest.coffee 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. expect = chai.expect
  8. modulePath = Path.join __dirname, "../../../../app/js/Features/Project/ProjectCollabratecDetailsHandler"
  9. describe "ProjectCollabratecDetailsHandler", ->
  10. beforeEach ->
  11. @projectId = ObjectId("5bea8747c7bba6012fcaceb3")
  12. @userId = ObjectId("5be316a9c7f6aa03802ea8fb")
  13. @userId2 = ObjectId("5c1794b3f0e89b1d1c577eca")
  14. @ProjectModel = {}
  15. @ProjectCollabratecDetailsHandler = SandboxedModule.require modulePath, requires:
  16. "../../models/Project": { Project: @ProjectModel }
  17. @callback = sinon.stub()
  18. describe "initializeCollabratecProject", ->
  19. describe "when update succeeds", ->
  20. beforeEach ->
  21. @ProjectModel.update = sinon.stub().yields()
  22. @ProjectCollabratecDetailsHandler.initializeCollabratecProject @projectId, @userId, "collabratec-document-id", "collabratec-private-group-id", @callback
  23. it "should update project model", ->
  24. update = $set: {
  25. collabratecUsers: [ {
  26. user_id: @userId,
  27. collabratec_document_id: "collabratec-document-id",
  28. collabratec_privategroup_id: "collabratec-private-group-id"
  29. } ]
  30. }
  31. expect(@ProjectModel.update).to.have.been.calledWith { _id: @projectId }, update, @callback
  32. describe "when update has error", ->
  33. beforeEach ->
  34. @ProjectModel.update = sinon.stub().yields("error")
  35. @ProjectCollabratecDetailsHandler.initializeCollabratecProject @projectId, @userId, "collabratec-document-id", "collabratec-private-group-id", @callback
  36. it "should callback with error", ->
  37. expect(@callback).to.have.been.calledWith("error")
  38. describe "with invalid args", ->
  39. beforeEach ->
  40. @ProjectModel.update = sinon.stub()
  41. @ProjectCollabratecDetailsHandler.initializeCollabratecProject "bad-project-id", "bad-user-id", "collabratec-document-id", "collabratec-private-group-id", @callback
  42. it "should not update", ->
  43. expect(@ProjectModel.update).not.to.have.beenCalled
  44. it "should callback with error", ->
  45. expect(@callback.firstCall.args[0]).to.be.instanceOf Error
  46. describe "isLinkedCollabratecUserProject", ->
  47. beforeEach ->
  48. @ProjectModel.findOne = sinon.stub().yields()
  49. describe "when find succeeds", ->
  50. describe "when user project found", ->
  51. beforeEach ->
  52. @ProjectModel.findOne = sinon.stub().yields(null, "project")
  53. @ProjectCollabratecDetailsHandler.isLinkedCollabratecUserProject @projectId, @userId, @callback
  54. it "should call find with project and user id", ->
  55. expect(@ProjectModel.findOne).to.have.been.calledWithMatch {
  56. _id: ObjectId(@projectId)
  57. collabratecUsers: $elemMatch:
  58. user_id: ObjectId(@userId)
  59. }
  60. it "should callback with true", ->
  61. expect(@callback).to.have.been.calledWith null, true
  62. describe "when user project found", ->
  63. beforeEach ->
  64. @ProjectModel.findOne = sinon.stub().yields(null, null)
  65. @ProjectCollabratecDetailsHandler.isLinkedCollabratecUserProject @projectId, @userId, @callback
  66. it "should callback with false", ->
  67. expect(@callback).to.have.been.calledWith null, false
  68. describe "when find has error", ->
  69. beforeEach ->
  70. @ProjectModel.findOne = sinon.stub().yields("error")
  71. @ProjectCollabratecDetailsHandler.isLinkedCollabratecUserProject @projectId, @userId, @callback
  72. it "should callback with error", ->
  73. expect(@callback).to.have.been.calledWith "error"
  74. describe "with invalid args", ->
  75. beforeEach ->
  76. @ProjectModel.findOne = sinon.stub()
  77. @ProjectCollabratecDetailsHandler.isLinkedCollabratecUserProject "bad-project-id", "bad-user-id", @callback
  78. it "should not update", ->
  79. expect(@ProjectModel.findOne).not.to.have.beenCalled
  80. it "should callback with error", ->
  81. expect(@callback.firstCall.args[0]).to.be.instanceOf Error
  82. describe "linkCollabratecUserProject", ->
  83. describe "when update succeeds", ->
  84. beforeEach ->
  85. @ProjectModel.update = sinon.stub().yields()
  86. @ProjectCollabratecDetailsHandler.linkCollabratecUserProject @projectId, @userId, "collabratec-document-id", @callback
  87. it "should update project model", ->
  88. query =
  89. _id: @projectId
  90. collabratecUsers: $not: $elemMatch:
  91. collabratec_document_id: "collabratec-document-id"
  92. user_id: @userId
  93. update = $push: collabratecUsers:
  94. collabratec_document_id: "collabratec-document-id"
  95. user_id: @userId
  96. expect(@ProjectModel.update).to.have.been.calledWith query, update, @callback
  97. describe "when update has error", ->
  98. beforeEach ->
  99. @ProjectModel.update = sinon.stub().yields("error")
  100. @ProjectCollabratecDetailsHandler.linkCollabratecUserProject @projectId, @userId, "collabratec-document-id", @callback
  101. it "should callback with error", ->
  102. expect(@callback).to.have.been.calledWith("error")
  103. describe "with invalid args", ->
  104. beforeEach ->
  105. @ProjectModel.update = sinon.stub()
  106. @ProjectCollabratecDetailsHandler.linkCollabratecUserProject "bad-project-id", "bad-user-id", "collabratec-document-id", @callback
  107. it "should not update", ->
  108. expect(@ProjectModel.update).not.to.have.beenCalled
  109. it "should callback with error", ->
  110. expect(@callback.firstCall.args[0]).to.be.instanceOf Error
  111. describe "setCollabratecUsers", ->
  112. beforeEach ->
  113. @collabratecUsers = [
  114. {
  115. user_id: @userId
  116. collabratec_document_id: "collabratec-document-id-1"
  117. collabratec_privategroup_id: "collabratec-private-group-id-1"
  118. },
  119. {
  120. user_id: @userId2
  121. collabratec_document_id: "collabratec-document-id-2"
  122. collabratec_privategroup_id: "collabratec-private-group-id-2"
  123. }
  124. ]
  125. describe "when update succeeds", ->
  126. beforeEach ->
  127. @ProjectModel.update = sinon.stub().yields()
  128. @ProjectCollabratecDetailsHandler.setCollabratecUsers @projectId, @collabratecUsers, @callback
  129. it "should update project model", ->
  130. update = $set: {
  131. collabratecUsers: @collabratecUsers
  132. }
  133. expect(@ProjectModel.update).to.have.been.calledWith { _id: @projectId }, update, @callback
  134. describe "when update has error", ->
  135. beforeEach ->
  136. @ProjectModel.update = sinon.stub().yields("error")
  137. @ProjectCollabratecDetailsHandler.setCollabratecUsers @projectId, @collabratecUsers, @callback
  138. it "should callback with error", ->
  139. expect(@callback).to.have.been.calledWith("error")
  140. describe "with invalid project_id", ->
  141. beforeEach ->
  142. @ProjectModel.update = sinon.stub()
  143. @ProjectCollabratecDetailsHandler.setCollabratecUsers "bad-project-id", @collabratecUsers, @callback
  144. it "should not update", ->
  145. expect(@ProjectModel.update).not.to.have.beenCalled
  146. it "should callback with error", ->
  147. expect(@callback.firstCall.args[0]).to.be.instanceOf Error
  148. describe "with invalid user_id", ->
  149. beforeEach ->
  150. @collabratecUsers[1].user_id = "bad-user-id"
  151. @ProjectModel.update = sinon.stub()
  152. @ProjectCollabratecDetailsHandler.setCollabratecUsers @projectId, @collabratecUsers, @callback
  153. it "should not update", ->
  154. expect(@ProjectModel.update).not.to.have.beenCalled
  155. it "should callback with error", ->
  156. expect(@callback.firstCall.args[0]).to.be.instanceOf Error
  157. describe "unlinkCollabratecUserProject", ->
  158. describe "when update succeeds", ->
  159. beforeEach ->
  160. @ProjectModel.update = sinon.stub().yields()
  161. @ProjectCollabratecDetailsHandler.unlinkCollabratecUserProject @projectId, @userId, @callback
  162. it "should update project model", ->
  163. query =
  164. _id: @projectId
  165. update = $pull: collabratecUsers:
  166. user_id: @userId
  167. expect(@ProjectModel.update).to.have.been.calledWith query, update, @callback
  168. describe "when update has error", ->
  169. beforeEach ->
  170. @ProjectModel.update = sinon.stub().yields("error")
  171. @ProjectCollabratecDetailsHandler.unlinkCollabratecUserProject @projectId, @userId, @callback
  172. it "should callback with error", ->
  173. expect(@callback).to.have.been.calledWith("error")
  174. describe "with invalid args", ->
  175. beforeEach ->
  176. @ProjectModel.update = sinon.stub()
  177. @ProjectCollabratecDetailsHandler.unlinkCollabratecUserProject "bad-project-id", "bad-user-id", @callback
  178. it "should not update", ->
  179. expect(@ProjectModel.update).not.to.have.beenCalled
  180. it "should callback with error", ->
  181. expect(@callback.firstCall.args[0]).to.be.instanceOf Error
  182. describe "updateCollabratecUserIds", ->
  183. describe "when update succeeds", ->
  184. beforeEach ->
  185. @ProjectModel.update = sinon.stub().yields()
  186. @ProjectCollabratecDetailsHandler.updateCollabratecUserIds @userId, @userId2, @callback
  187. it "should update project model", ->
  188. expect(@ProjectModel.update).to.have.been.calledWith { "collabratecUsers.user_id": @userId }, { $set: "collabratecUsers.$.user_id": @userId2 }, { multi: true}, @callback
  189. describe "when update has error", ->
  190. beforeEach ->
  191. @ProjectModel.update = sinon.stub().yields("error")
  192. @ProjectCollabratecDetailsHandler.updateCollabratecUserIds @userId, @userId2, @callback
  193. it "should callback with error", ->
  194. expect(@callback).to.have.been.calledWith("error")
  195. describe "with invalid old_user_id", ->
  196. beforeEach ->
  197. @ProjectModel.update = sinon.stub()
  198. @ProjectCollabratecDetailsHandler.updateCollabratecUserIds "bad-user-id", @userId2, @callback
  199. it "should not update", ->
  200. expect(@ProjectModel.update).not.to.have.beenCalled
  201. it "should callback with error", ->
  202. expect(@callback.firstCall.args[0]).to.be.instanceOf Error
  203. describe "with invalid new_user_id", ->
  204. beforeEach ->
  205. @ProjectModel.update = sinon.stub()
  206. @ProjectCollabratecDetailsHandler.updateCollabratecUserIds @userId, "bad-user-id", @callback
  207. it "should not update", ->
  208. expect(@ProjectModel.update).not.to.have.beenCalled
  209. it "should callback with error", ->
  210. expect(@callback.firstCall.args[0]).to.be.instanceOf Error