ProjectGetterTests.coffee 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. sinon = require('sinon')
  2. chai = require('chai')
  3. should = chai.should()
  4. expect = chai.expect
  5. modulePath = "../../../../app/js/Features/Project/ProjectGetter.js"
  6. SandboxedModule = require('sandboxed-module')
  7. ObjectId = require("mongojs").ObjectId
  8. assert = require("chai").assert
  9. describe "ProjectGetter", ->
  10. beforeEach ->
  11. @callback = sinon.stub()
  12. @ProjectGetter = SandboxedModule.require modulePath, requires:
  13. "../../infrastructure/mongojs":
  14. db: @db =
  15. projects: {}
  16. users: {}
  17. ObjectId: ObjectId
  18. "metrics-sharelatex": timeAsyncMethod: sinon.stub()
  19. "../../models/Project": Project: @Project = {}
  20. "../Collaborators/CollaboratorsHandler": @CollaboratorsHandler = {}
  21. "../../infrastructure/LockManager": @LockManager =
  22. runWithLock : sinon.spy((namespace, id, runner, callback) -> runner(callback))
  23. './ProjectEntityMongoUpdateHandler':
  24. lockKey: (project_id) -> project_id
  25. "logger-sharelatex":
  26. err:->
  27. log:->
  28. describe "getProjectWithoutDocLines", ->
  29. beforeEach ->
  30. @project =
  31. _id: @project_id = "56d46b0a1d3422b87c5ebcb1"
  32. @ProjectGetter.getProject = sinon.stub().yields()
  33. describe "passing an id", ->
  34. beforeEach ->
  35. @ProjectGetter.getProjectWithoutDocLines @project_id, @callback
  36. it "should call find with the project id", ->
  37. @ProjectGetter.getProject
  38. .calledWith(@project_id)
  39. .should.equal true
  40. it "should exclude the doc lines", ->
  41. excludes =
  42. "rootFolder.docs.lines": 0
  43. "rootFolder.folders.docs.lines": 0
  44. "rootFolder.folders.folders.docs.lines": 0
  45. "rootFolder.folders.folders.folders.docs.lines": 0
  46. "rootFolder.folders.folders.folders.folders.docs.lines": 0
  47. "rootFolder.folders.folders.folders.folders.folders.docs.lines": 0
  48. "rootFolder.folders.folders.folders.folders.folders.folders.docs.lines": 0
  49. "rootFolder.folders.folders.folders.folders.folders.folders.folders.docs.lines": 0
  50. @ProjectGetter.getProject
  51. .calledWith(@project_id, excludes)
  52. .should.equal true
  53. it "should call the callback", ->
  54. @callback.called.should.equal true
  55. describe "getProjectWithOnlyFolders", ->
  56. beforeEach ()->
  57. @project =
  58. _id: @project_id = "56d46b0a1d3422b87c5ebcb1"
  59. @ProjectGetter.getProject = sinon.stub().yields()
  60. describe "passing an id", ->
  61. beforeEach ->
  62. @ProjectGetter.getProjectWithOnlyFolders @project_id, @callback
  63. it "should call find with the project id", ->
  64. @ProjectGetter.getProject
  65. .calledWith(@project_id)
  66. .should.equal true
  67. it "should exclude the docs and files linesaaaa", ->
  68. excludes =
  69. "rootFolder.docs": 0
  70. "rootFolder.fileRefs": 0
  71. "rootFolder.folders.docs": 0
  72. "rootFolder.folders.fileRefs": 0
  73. "rootFolder.folders.folders.docs": 0
  74. "rootFolder.folders.folders.fileRefs": 0
  75. "rootFolder.folders.folders.folders.docs": 0
  76. "rootFolder.folders.folders.folders.fileRefs": 0
  77. "rootFolder.folders.folders.folders.folders.docs": 0
  78. "rootFolder.folders.folders.folders.folders.fileRefs": 0
  79. "rootFolder.folders.folders.folders.folders.folders.docs": 0
  80. "rootFolder.folders.folders.folders.folders.folders.fileRefs": 0
  81. "rootFolder.folders.folders.folders.folders.folders.folders.docs": 0
  82. "rootFolder.folders.folders.folders.folders.folders.folders.fileRefs": 0
  83. "rootFolder.folders.folders.folders.folders.folders.folders.folders.docs": 0
  84. "rootFolder.folders.folders.folders.folders.folders.folders.folders.fileRefs": 0
  85. @ProjectGetter.getProject
  86. .calledWith(@project_id, excludes)
  87. .should.equal true
  88. it "should call the callback with the project", ->
  89. @callback.called.should.equal true
  90. describe "getProject", ->
  91. beforeEach ()->
  92. @project =
  93. _id: @project_id = "56d46b0a1d3422b87c5ebcb1"
  94. @db.projects.find = sinon.stub().callsArgWith(2, null, [@project])
  95. describe "without projection", ->
  96. describe "with project id", ->
  97. beforeEach ->
  98. @ProjectGetter.getProject @project_id, @callback
  99. it "should call find with the project id", ->
  100. expect(@db.projects.find.callCount).to.equal 1
  101. expect(@db.projects.find.lastCall.args[0]).to.deep.equal {
  102. _id: ObjectId(@project_id)
  103. }
  104. describe "without project id", ->
  105. beforeEach ->
  106. @ProjectGetter.getProject null, @callback
  107. it "should callback with error", ->
  108. expect(@db.projects.find.callCount).to.equal 0
  109. expect(@callback.lastCall.args[0]).to.be.instanceOf Error
  110. describe "with projection", ->
  111. beforeEach ->
  112. @projection = {_id: 1}
  113. describe "with project id", ->
  114. beforeEach ->
  115. @ProjectGetter.getProject @project_id, @projection, @callback
  116. it "should call find with the project id", ->
  117. expect(@db.projects.find.callCount).to.equal 1
  118. expect(@db.projects.find.lastCall.args[0]).to.deep.equal {
  119. _id: ObjectId(@project_id)
  120. }
  121. expect(@db.projects.find.lastCall.args[1]).to.deep.equal @projection
  122. describe "without project id", ->
  123. beforeEach ->
  124. @ProjectGetter.getProject null, @callback
  125. it "should callback with error", ->
  126. expect(@db.projects.find.callCount).to.equal 0
  127. expect(@callback.lastCall.args[0]).to.be.instanceOf Error
  128. describe "getProjectWithoutLock", ->
  129. beforeEach ()->
  130. @project =
  131. _id: @project_id = "56d46b0a1d3422b87c5ebcb1"
  132. @db.projects.find = sinon.stub().callsArgWith(2, null, [@project])
  133. describe "without projection", ->
  134. describe "with project id", ->
  135. beforeEach ->
  136. @ProjectGetter.getProjectWithoutLock @project_id, @callback
  137. it "should call find with the project id", ->
  138. expect(@db.projects.find.callCount).to.equal 1
  139. expect(@db.projects.find.lastCall.args[0]).to.deep.equal {
  140. _id: ObjectId(@project_id)
  141. }
  142. describe "without project id", ->
  143. beforeEach ->
  144. @ProjectGetter.getProjectWithoutLock null, @callback
  145. it "should callback with error", ->
  146. expect(@db.projects.find.callCount).to.equal 0
  147. expect(@callback.lastCall.args[0]).to.be.instanceOf Error
  148. describe "with projection", ->
  149. beforeEach ->
  150. @projection = {_id: 1}
  151. describe "with project id", ->
  152. beforeEach ->
  153. @ProjectGetter.getProjectWithoutLock @project_id, @projection, @callback
  154. it "should call find with the project id", ->
  155. expect(@db.projects.find.callCount).to.equal 1
  156. expect(@db.projects.find.lastCall.args[0]).to.deep.equal {
  157. _id: ObjectId(@project_id)
  158. }
  159. expect(@db.projects.find.lastCall.args[1]).to.deep.equal @projection
  160. describe "without project id", ->
  161. beforeEach ->
  162. @ProjectGetter.getProjectWithoutLock null, @callback
  163. it "should callback with error", ->
  164. expect(@db.projects.find.callCount).to.equal 0
  165. expect(@callback.lastCall.args[0]).to.be.instanceOf Error
  166. describe "findAllUsersProjects", ->
  167. beforeEach ->
  168. @fields = {"mock": "fields"}
  169. @Project.find = sinon.stub()
  170. @Project.find.withArgs({owner_ref: @user_id}, @fields).yields(null, ["mock-owned-projects"])
  171. @CollaboratorsHandler.getProjectsUserIsMemberOf = sinon.stub()
  172. @CollaboratorsHandler.getProjectsUserIsMemberOf.withArgs(@user_id, @fields).yields(
  173. null,
  174. {
  175. readAndWrite: ["mock-rw-projects"],
  176. readOnly: ["mock-ro-projects"],
  177. tokenReadAndWrite: ['mock-token-rw-projects'],
  178. tokenReadOnly: ['mock-token-ro-projects']
  179. }
  180. )
  181. @ProjectGetter.findAllUsersProjects @user_id, @fields, @callback
  182. it "should call the callback with all the projects", ->
  183. @callback
  184. .calledWith(null, {
  185. owned: ["mock-owned-projects"],
  186. readAndWrite: ["mock-rw-projects"],
  187. readOnly: ["mock-ro-projects"]
  188. tokenReadAndWrite: ['mock-token-rw-projects'],
  189. tokenReadOnly: ['mock-token-ro-projects']
  190. })
  191. .should.equal true
  192. describe "getProjectIdByReadAndWriteToken", ->
  193. describe "when project find returns project", ->
  194. @beforeEach ->
  195. @Project.findOne = sinon.stub().yields(null, {_id: "project-id"})
  196. @ProjectGetter.getProjectIdByReadAndWriteToken "token", @callback
  197. it "should find project with token", ->
  198. @Project.findOne.calledWithMatch(
  199. {'tokens.readAndWrite': "token"}
  200. ).should.equal true
  201. it "should callback with project id", ->
  202. @callback.calledWith(null, "project-id").should.equal true
  203. describe "when project not found", ->
  204. @beforeEach ->
  205. @Project.findOne = sinon.stub().yields()
  206. @ProjectGetter.getProjectIdByReadAndWriteToken "token", @callback
  207. it "should callback empty", ->
  208. expect(@callback.firstCall.args.length).to.equal 0
  209. describe "when project find returns error", ->
  210. @beforeEach ->
  211. @Project.findOne = sinon.stub().yields("error")
  212. @ProjectGetter.getProjectIdByReadAndWriteToken "token", @callback
  213. it "should callback with error", ->
  214. @callback.calledWith("error").should.equal true