ProjectDeleterTests.coffee 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. should = require('chai').should()
  2. modulePath = "../../../../app/js/Features/Project/ProjectDeleter"
  3. SandboxedModule = require('sandboxed-module')
  4. sinon = require('sinon')
  5. describe 'ProjectDeleter', ->
  6. beforeEach ->
  7. @project_id = "12312"
  8. @project =
  9. _id: @project_id
  10. rootFolder:[]
  11. collaberator_refs:["collab1", "collab2"]
  12. readOnly_refs:["readOnly1", "readOnly2"]
  13. owner_ref:"owner ref here"
  14. remove: sinon.stub().callsArg(0)
  15. @user_id = 1234
  16. @mongojs =
  17. db:
  18. deletedProjects:
  19. insert: sinon.stub().callsArg(1)
  20. @Project =
  21. update: sinon.stub().callsArgWith(3)
  22. remove: sinon.stub().callsArgWith(1)
  23. findOne: sinon.stub().callsArgWith(1, null, @project)
  24. find: sinon.stub().callsArgWith(1, null, [@project])
  25. applyToAllFilesRecursivly: sinon.stub()
  26. @documentUpdaterHandler =
  27. flushProjectToMongoAndDelete:sinon.stub().callsArgWith(1)
  28. @editorController = notifyUsersProjectHasBeenDeletedOrRenamed : sinon.stub().callsArgWith(1)
  29. @TagsHandler =
  30. removeProjectFromAllTags: sinon.stub().callsArgWith(2)
  31. @CollaboratorsHandler =
  32. removeUserFromAllProjets: sinon.stub().yields()
  33. getMemberIds: sinon.stub().withArgs(@project_id).yields(null, ["member-id-1", "member-id-2"])
  34. @deleter = SandboxedModule.require modulePath, requires:
  35. "../Editor/EditorController": @editorController
  36. '../../models/Project':{Project:@Project}
  37. '../DocumentUpdater/DocumentUpdaterHandler': @documentUpdaterHandler
  38. "../Tags/TagsHandler":@TagsHandler
  39. "../FileStore/FileStoreHandler": @FileStoreHandler = {}
  40. "../Collaborators/CollaboratorsHandler": @CollaboratorsHandler
  41. "../../infrastructure/mongojs": @mongojs
  42. 'logger-sharelatex':
  43. log:->
  44. describe "mark as deleted by external source", ->
  45. project_id = 1234
  46. it 'should update the project with the flag set to true', (done)->
  47. @deleter.markAsDeletedByExternalSource project_id, =>
  48. conditions = {_id:project_id}
  49. update = {deletedByExternalDataSource:true}
  50. @Project.update.calledWith(conditions, update).should.equal true
  51. done()
  52. it 'should tell the editor controler so users are notified', (done)->
  53. @deleter.markAsDeletedByExternalSource project_id, =>
  54. @editorController.notifyUsersProjectHasBeenDeletedOrRenamed.calledWith(project_id).should.equal true
  55. done()
  56. describe "unmarkAsDeletedByExternalSource", ->
  57. beforeEach ->
  58. @Project.update = sinon.stub().callsArg(3)
  59. @callback = sinon.stub()
  60. @project = {
  61. _id: @project_id
  62. }
  63. @deleter.unmarkAsDeletedByExternalSource @project_id, @callback
  64. it "should remove the flag from the project", ->
  65. @Project.update
  66. .calledWith({_id: @project_id}, {deletedByExternalDataSource:false})
  67. .should.equal true
  68. describe "deleteUsersProjects", ->
  69. beforeEach ->
  70. @deleter.deleteProject = sinon.stub().callsArg(1)
  71. it "should find all the projects owned by the user_id", (done)->
  72. @deleter.deleteUsersProjects @user_id, =>
  73. sinon.assert.calledWith(@Project.find, owner_ref: @user_id)
  74. done()
  75. it "should call deleteProject on the found projects", (done)->
  76. @deleter.deleteUsersProjects @user_id, =>
  77. sinon.assert.calledWith(@deleter.deleteProject, @project._id)
  78. done()
  79. it "should call deleteProject once for each project", (done)->
  80. @Project.find.callsArgWith(1, null, [
  81. {_id: 'potato'}, {_id: 'wombat'}
  82. ])
  83. @deleter.deleteUsersProjects @user_id, =>
  84. sinon.assert.calledTwice(@deleter.deleteProject)
  85. sinon.assert.calledWith(@deleter.deleteProject, 'wombat')
  86. sinon.assert.calledWith(@deleter.deleteProject, 'potato')
  87. done()
  88. it "should remove all the projects the user is a collaborator of", (done)->
  89. @deleter.deleteUsersProjects @user_id, =>
  90. @CollaboratorsHandler.removeUserFromAllProjets.calledWith(@user_id).should.equal true
  91. done()
  92. describe "softDeleteUsersProjects", ->
  93. beforeEach ->
  94. @deleter.softDeleteProject = sinon.stub().callsArg(1)
  95. it "should find all the projects owned by the user_id", (done)->
  96. @deleter.softDeleteUsersProjects @user_id, =>
  97. @Project.find.calledWith(owner_ref: @user_id).should.equal true
  98. done()
  99. it "should call deleteProject on the found projects", (done)->
  100. @deleter.softDeleteUsersProjects @user_id, =>
  101. sinon.assert.calledWith(@deleter.softDeleteProject, @project._id)
  102. done()
  103. it "should call deleteProject once for each project", (done)->
  104. @Project.find.callsArgWith(1, null, [
  105. {_id: 'potato'}, {_id: 'wombat'}
  106. ])
  107. @deleter.softDeleteUsersProjects @user_id, =>
  108. sinon.assert.calledTwice(@deleter.softDeleteProject)
  109. sinon.assert.calledWith(@deleter.softDeleteProject, 'wombat')
  110. sinon.assert.calledWith(@deleter.softDeleteProject, 'potato')
  111. done()
  112. it "should remove all the projects the user is a collaborator of", (done)->
  113. @deleter.softDeleteUsersProjects @user_id, =>
  114. @CollaboratorsHandler.removeUserFromAllProjets.calledWith(@user_id).should.equal true
  115. done()
  116. describe "deleteProject", ->
  117. beforeEach (done) ->
  118. @project_id = "mock-project-id-123"
  119. @Project.remove.callsArgWith(1)
  120. done()
  121. it "should flushProjectToMongoAndDelete in doc updater", (done)->
  122. @deleter.deleteProject @project_id, =>
  123. @documentUpdaterHandler.flushProjectToMongoAndDelete.calledWith(@project_id).should.equal true
  124. done()
  125. it "should removeProjectFromAllTags", (done)->
  126. @deleter.deleteProject @project_id, =>
  127. @TagsHandler.removeProjectFromAllTags.calledWith("member-id-1", @project_id).should.equal true
  128. @TagsHandler.removeProjectFromAllTags.calledWith("member-id-2", @project_id).should.equal true
  129. done()
  130. it "should remove the project from Mongo", (done) ->
  131. @deleter.deleteProject @project_id, =>
  132. @Project.remove.calledWith({
  133. _id: @project_id
  134. }).should.equal true
  135. done()
  136. describe "softDeleteProject", ->
  137. beforeEach ->
  138. @deleter.deleteProject = sinon.stub().callsArg(1)
  139. it "should set the deletedAt time", (done)->
  140. @deleter.softDeleteProject @project_id, =>
  141. @project.deletedAt.should.exist
  142. done()
  143. it "should insert the project into the deleted projects collection", (done)->
  144. @deleter.softDeleteProject @project_id, =>
  145. sinon.assert.calledWith(@mongojs.db.deletedProjects.insert, @project)
  146. done()
  147. it "should delete the project", (done)->
  148. @deleter.softDeleteProject @project_id, =>
  149. sinon.assert.calledWith(@deleter.deleteProject, @project_id)
  150. done()
  151. describe "archiveProject", ->
  152. beforeEach ->
  153. @Project.update.callsArgWith(2)
  154. it "should update the project", (done)->
  155. @deleter.archiveProject @project_id, =>
  156. @Project.update.calledWith({
  157. _id:@project_id
  158. }, {
  159. $set: { archived: true }
  160. }).should.equal true
  161. done()
  162. describe "restoreProject", ->
  163. beforeEach ->
  164. @Project.update.callsArgWith(2)
  165. it "should unset the archive attribute", (done)->
  166. @deleter.restoreProject @project_id, =>
  167. @Project.update.calledWith({
  168. _id: @project_id
  169. }, {
  170. $unset: { archived: true }
  171. }).should.equal true
  172. done()