ProjectDeleterTests.coffee 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. @Project =
  15. update: sinon.stub().callsArgWith(3)
  16. remove: sinon.stub().callsArgWith(1)
  17. findById: sinon.stub().callsArgWith(1, null, @project)
  18. applyToAllFilesRecursivly: sinon.stub()
  19. @documentUpdaterHandler =
  20. flushProjectToMongoAndDelete:sinon.stub().callsArgWith(1)
  21. @editorController = notifyUsersProjectHasBeenDeletedOrRenamed : sinon.stub().callsArgWith(1)
  22. @TagsHandler =
  23. removeProjectFromAllTags: sinon.stub().callsArgWith(2)
  24. @deleter = SandboxedModule.require modulePath, requires:
  25. "../Editor/EditorController": @editorController
  26. '../../models/Project':{Project:@Project}
  27. '../DocumentUpdater/DocumentUpdaterHandler': @documentUpdaterHandler
  28. "../Tags/TagsHandler":@TagsHandler
  29. "../FileStore/FileStoreHandler": @FileStoreHandler = {}
  30. "../Collaborators/CollaboratorsHandler": @CollaboratorsHandler = {}
  31. 'logger-sharelatex':
  32. log:->
  33. describe "mark as deleted by external source", ->
  34. project_id = 1234
  35. it 'should update the project with the flag set to true', (done)->
  36. @deleter.markAsDeletedByExternalSource project_id, =>
  37. conditions = {_id:project_id}
  38. update = {deletedByExternalDataSource:true}
  39. @Project.update.calledWith(conditions, update).should.equal true
  40. done()
  41. it 'should tell the editor controler so users are notified', (done)->
  42. @deleter.markAsDeletedByExternalSource project_id, =>
  43. @editorController.notifyUsersProjectHasBeenDeletedOrRenamed.calledWith(project_id).should.equal true
  44. done()
  45. describe "unmarkAsDeletedByExternalSource", ->
  46. beforeEach ->
  47. @Project.update = sinon.stub().callsArg(3)
  48. @callback = sinon.stub()
  49. @project = {
  50. _id: @project_id
  51. }
  52. @deleter.unmarkAsDeletedByExternalSource @project_id, @callback
  53. it "should remove the flag from the project", ->
  54. @Project.update
  55. .calledWith({_id: @project_id}, {deletedByExternalDataSource:false})
  56. .should.equal true
  57. describe "deleteUsersProjects", ->
  58. it "should remove all the projects owned by the user_id", (done)->
  59. user_id = 1234
  60. @deleter.deleteUsersProjects user_id, =>
  61. @Project.remove.calledWith(owner_ref:user_id).should.equal true
  62. done()
  63. describe "deleteProject", ->
  64. beforeEach (done) ->
  65. @project_id = "mock-project-id-123"
  66. @deleter.archiveProject = sinon.stub().callsArg(1)
  67. @deleter.deleteProject @project_id, done
  68. it "should archive the project to clean it up", ->
  69. @deleter.archiveProject
  70. .calledWith(@project_id)
  71. .should.equal true
  72. it "should remove the project from Mongo", ->
  73. @Project.remove
  74. .calledWith(_id: @project_id)
  75. .should.equal true
  76. describe "archiveProject", ->
  77. beforeEach ->
  78. @CollaboratorsHandler.getMemberIds = sinon.stub()
  79. @CollaboratorsHandler.getMemberIds.withArgs(@project_id).yields(null, ["member-id-1", "member-id-2"])
  80. @Project.update.callsArgWith(2)
  81. it "should flushProjectToMongoAndDelete in doc updater", (done)->
  82. @deleter.archiveProject @project_id, =>
  83. @documentUpdaterHandler.flushProjectToMongoAndDelete.calledWith(@project_id).should.equal true
  84. done()
  85. it "should remove the project", (done)->
  86. @deleter.archiveProject @project_id, =>
  87. @Project.update.calledWith({
  88. _id:@project_id
  89. }, {
  90. $set: { archived: true }
  91. }).should.equal true
  92. done()
  93. it "should removeProjectFromAllTags", (done)->
  94. @deleter.archiveProject @project_id, =>
  95. @TagsHandler.removeProjectFromAllTags.calledWith("member-id-1", @project_id).should.equal true
  96. @TagsHandler.removeProjectFromAllTags.calledWith("member-id-2", @project_id).should.equal true
  97. done()
  98. describe "restoreProject", ->
  99. beforeEach ->
  100. @Project.update.callsArgWith(2)
  101. it "should unset the archive attribute", (done)->
  102. @deleter.restoreProject @project_id, =>
  103. @Project.update.calledWith({
  104. _id: @project_id
  105. }, {
  106. $unset: { archived: true }
  107. }).should.equal true
  108. done()