ProjectCollabratecDetailsTest.coffee 9.8 KB

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