ProjectDeleterTests.coffee 7.1 KB

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