2014-02-12 05:23:40 -05:00
|
|
|
should = require('chai').should()
|
|
|
|
modulePath = "../../../../app/js/Features/Project/ProjectDeleter"
|
|
|
|
SandboxedModule = require('sandboxed-module')
|
|
|
|
sinon = require('sinon')
|
|
|
|
|
|
|
|
|
2014-11-07 07:31:47 -05:00
|
|
|
describe 'ProjectDeleter', ->
|
2014-02-12 05:23:40 -05:00
|
|
|
|
|
|
|
beforeEach ->
|
2014-04-07 08:47:10 -04:00
|
|
|
@project_id = "12312"
|
|
|
|
@project =
|
|
|
|
_id: @project_id
|
|
|
|
rootFolder:[]
|
|
|
|
collaberator_refs:["collab1", "collab2"]
|
|
|
|
readOnly_refs:["readOnly1", "readOnly2"]
|
|
|
|
owner_ref:"owner ref here"
|
2019-03-26 09:43:59 -04:00
|
|
|
remove: sinon.stub().callsArg(0)
|
|
|
|
|
|
|
|
@user_id = 1234
|
|
|
|
|
|
|
|
@mongojs =
|
|
|
|
db:
|
|
|
|
deletedProjects:
|
|
|
|
insert: sinon.stub().callsArg(1)
|
2014-02-12 05:23:40 -05:00
|
|
|
|
|
|
|
@Project =
|
|
|
|
update: sinon.stub().callsArgWith(3)
|
|
|
|
remove: sinon.stub().callsArgWith(1)
|
2019-03-26 09:43:59 -04:00
|
|
|
findOne: sinon.stub().callsArgWith(1, null, @project)
|
|
|
|
find: sinon.stub().callsArgWith(1, null, [@project])
|
2014-04-07 08:47:10 -04:00
|
|
|
applyToAllFilesRecursivly: sinon.stub()
|
|
|
|
@documentUpdaterHandler =
|
|
|
|
flushProjectToMongoAndDelete:sinon.stub().callsArgWith(1)
|
2014-02-12 05:23:40 -05:00
|
|
|
@editorController = notifyUsersProjectHasBeenDeletedOrRenamed : sinon.stub().callsArgWith(1)
|
2014-04-07 08:47:10 -04:00
|
|
|
@TagsHandler =
|
|
|
|
removeProjectFromAllTags: sinon.stub().callsArgWith(2)
|
2016-06-30 09:21:44 -04:00
|
|
|
@CollaboratorsHandler =
|
|
|
|
removeUserFromAllProjets: sinon.stub().yields()
|
2019-03-26 09:43:59 -04:00
|
|
|
getMemberIds: sinon.stub().withArgs(@project_id).yields(null, ["member-id-1", "member-id-2"])
|
2014-02-12 05:23:40 -05:00
|
|
|
@deleter = SandboxedModule.require modulePath, requires:
|
|
|
|
"../Editor/EditorController": @editorController
|
|
|
|
'../../models/Project':{Project:@Project}
|
2014-04-07 08:47:10 -04:00
|
|
|
'../DocumentUpdater/DocumentUpdaterHandler': @documentUpdaterHandler
|
|
|
|
"../Tags/TagsHandler":@TagsHandler
|
2014-10-09 04:13:38 -04:00
|
|
|
"../FileStore/FileStoreHandler": @FileStoreHandler = {}
|
2016-06-30 09:21:44 -04:00
|
|
|
"../Collaborators/CollaboratorsHandler": @CollaboratorsHandler
|
2019-03-26 09:43:59 -04:00
|
|
|
"../../infrastructure/mongojs": @mongojs
|
2014-02-12 05:23:40 -05:00
|
|
|
'logger-sharelatex':
|
|
|
|
log:->
|
|
|
|
|
|
|
|
describe "mark as deleted by external source", ->
|
|
|
|
project_id = 1234
|
|
|
|
it 'should update the project with the flag set to true', (done)->
|
|
|
|
@deleter.markAsDeletedByExternalSource project_id, =>
|
|
|
|
conditions = {_id:project_id}
|
|
|
|
update = {deletedByExternalDataSource:true}
|
|
|
|
@Project.update.calledWith(conditions, update).should.equal true
|
|
|
|
done()
|
|
|
|
|
|
|
|
it 'should tell the editor controler so users are notified', (done)->
|
|
|
|
@deleter.markAsDeletedByExternalSource project_id, =>
|
|
|
|
@editorController.notifyUsersProjectHasBeenDeletedOrRenamed.calledWith(project_id).should.equal true
|
|
|
|
done()
|
2014-10-09 04:13:38 -04:00
|
|
|
|
|
|
|
describe "unmarkAsDeletedByExternalSource", ->
|
|
|
|
beforeEach ->
|
|
|
|
@Project.update = sinon.stub().callsArg(3)
|
|
|
|
@callback = sinon.stub()
|
|
|
|
@project = {
|
|
|
|
_id: @project_id
|
|
|
|
}
|
2014-11-07 07:31:47 -05:00
|
|
|
@deleter.unmarkAsDeletedByExternalSource @project_id, @callback
|
|
|
|
|
|
|
|
it "should remove the flag from the project", ->
|
|
|
|
@Project.update
|
|
|
|
.calledWith({_id: @project_id}, {deletedByExternalDataSource:false})
|
|
|
|
.should.equal true
|
2014-02-12 05:23:40 -05:00
|
|
|
|
|
|
|
describe "deleteUsersProjects", ->
|
2019-03-26 09:43:59 -04:00
|
|
|
beforeEach ->
|
|
|
|
@deleter.deleteProject = sinon.stub().callsArg(1)
|
|
|
|
|
|
|
|
it "should find all the projects owned by the user_id", (done)->
|
|
|
|
@deleter.deleteUsersProjects @user_id, =>
|
|
|
|
sinon.assert.calledWith(@Project.find, owner_ref: @user_id)
|
|
|
|
done()
|
|
|
|
|
|
|
|
it "should call deleteProject on the found projects", (done)->
|
|
|
|
@deleter.deleteUsersProjects @user_id, =>
|
|
|
|
sinon.assert.calledWith(@deleter.deleteProject, @project._id)
|
|
|
|
done()
|
|
|
|
|
|
|
|
it "should call deleteProject once for each project", (done)->
|
|
|
|
@Project.find.callsArgWith(1, null, [
|
|
|
|
{_id: 'potato'}, {_id: 'wombat'}
|
|
|
|
])
|
|
|
|
@deleter.deleteUsersProjects @user_id, =>
|
|
|
|
sinon.assert.calledTwice(@deleter.deleteProject)
|
|
|
|
sinon.assert.calledWith(@deleter.deleteProject, 'wombat')
|
|
|
|
sinon.assert.calledWith(@deleter.deleteProject, 'potato')
|
|
|
|
done()
|
|
|
|
|
|
|
|
it "should remove all the projects the user is a collaborator of", (done)->
|
|
|
|
@deleter.deleteUsersProjects @user_id, =>
|
|
|
|
@CollaboratorsHandler.removeUserFromAllProjets.calledWith(@user_id).should.equal true
|
|
|
|
done()
|
|
|
|
|
|
|
|
describe "softDeleteUsersProjects", ->
|
|
|
|
beforeEach ->
|
|
|
|
@deleter.softDeleteProject = sinon.stub().callsArg(1)
|
|
|
|
|
|
|
|
it "should find all the projects owned by the user_id", (done)->
|
|
|
|
@deleter.softDeleteUsersProjects @user_id, =>
|
|
|
|
@Project.find.calledWith(owner_ref: @user_id).should.equal true
|
|
|
|
done()
|
2014-02-12 05:23:40 -05:00
|
|
|
|
2019-03-26 09:43:59 -04:00
|
|
|
it "should call deleteProject on the found projects", (done)->
|
|
|
|
@deleter.softDeleteUsersProjects @user_id, =>
|
|
|
|
sinon.assert.calledWith(@deleter.softDeleteProject, @project._id)
|
|
|
|
done()
|
|
|
|
|
|
|
|
it "should call deleteProject once for each project", (done)->
|
|
|
|
@Project.find.callsArgWith(1, null, [
|
|
|
|
{_id: 'potato'}, {_id: 'wombat'}
|
|
|
|
])
|
|
|
|
@deleter.softDeleteUsersProjects @user_id, =>
|
|
|
|
sinon.assert.calledTwice(@deleter.softDeleteProject)
|
|
|
|
sinon.assert.calledWith(@deleter.softDeleteProject, 'wombat')
|
|
|
|
sinon.assert.calledWith(@deleter.softDeleteProject, 'potato')
|
2014-02-12 05:23:40 -05:00
|
|
|
done()
|
2014-04-07 08:47:10 -04:00
|
|
|
|
2016-06-30 09:21:44 -04:00
|
|
|
it "should remove all the projects the user is a collaborator of", (done)->
|
2019-03-26 09:43:59 -04:00
|
|
|
@deleter.softDeleteUsersProjects @user_id, =>
|
|
|
|
@CollaboratorsHandler.removeUserFromAllProjets.calledWith(@user_id).should.equal true
|
2016-06-30 09:21:44 -04:00
|
|
|
done()
|
|
|
|
|
2014-06-04 07:45:04 -04:00
|
|
|
describe "deleteProject", ->
|
|
|
|
beforeEach (done) ->
|
|
|
|
@project_id = "mock-project-id-123"
|
2018-11-13 05:34:27 -05:00
|
|
|
@Project.remove.callsArgWith(1)
|
|
|
|
done()
|
2014-06-04 07:45:04 -04:00
|
|
|
|
2018-11-13 05:34:27 -05:00
|
|
|
it "should flushProjectToMongoAndDelete in doc updater", (done)->
|
|
|
|
@deleter.deleteProject @project_id, =>
|
|
|
|
@documentUpdaterHandler.flushProjectToMongoAndDelete.calledWith(@project_id).should.equal true
|
|
|
|
done()
|
2014-06-04 07:45:04 -04:00
|
|
|
|
2018-11-13 05:34:27 -05:00
|
|
|
it "should removeProjectFromAllTags", (done)->
|
|
|
|
@deleter.deleteProject @project_id, =>
|
|
|
|
@TagsHandler.removeProjectFromAllTags.calledWith("member-id-1", @project_id).should.equal true
|
|
|
|
@TagsHandler.removeProjectFromAllTags.calledWith("member-id-2", @project_id).should.equal true
|
|
|
|
done()
|
2014-06-04 07:45:04 -04:00
|
|
|
|
2018-11-13 05:34:27 -05:00
|
|
|
it "should remove the project from Mongo", (done) ->
|
|
|
|
@deleter.deleteProject @project_id, =>
|
|
|
|
@Project.remove.calledWith({
|
|
|
|
_id: @project_id
|
|
|
|
}).should.equal true
|
|
|
|
done()
|
2014-04-07 08:47:10 -04:00
|
|
|
|
2019-03-26 09:43:59 -04:00
|
|
|
describe "softDeleteProject", ->
|
|
|
|
beforeEach ->
|
|
|
|
@deleter.deleteProject = sinon.stub().callsArg(1)
|
|
|
|
|
|
|
|
it "should set the deletedAt time", (done)->
|
|
|
|
@deleter.softDeleteProject @project_id, =>
|
|
|
|
@project.deletedAt.should.exist
|
|
|
|
done()
|
|
|
|
|
|
|
|
it "should insert the project into the deleted projects collection", (done)->
|
|
|
|
@deleter.softDeleteProject @project_id, =>
|
|
|
|
sinon.assert.calledWith(@mongojs.db.deletedProjects.insert, @project)
|
|
|
|
done()
|
|
|
|
|
|
|
|
it "should delete the project", (done)->
|
|
|
|
@deleter.softDeleteProject @project_id, =>
|
|
|
|
sinon.assert.calledWith(@deleter.deleteProject, @project_id)
|
|
|
|
done()
|
|
|
|
|
2014-06-03 12:35:44 -04:00
|
|
|
describe "archiveProject", ->
|
2014-04-07 08:47:10 -04:00
|
|
|
beforeEach ->
|
2014-04-28 07:53:43 -04:00
|
|
|
@Project.update.callsArgWith(2)
|
2014-04-07 08:47:10 -04:00
|
|
|
|
2018-11-13 05:34:27 -05:00
|
|
|
it "should update the project", (done)->
|
2014-06-03 12:35:44 -04:00
|
|
|
@deleter.archiveProject @project_id, =>
|
2014-04-28 07:53:43 -04:00
|
|
|
@Project.update.calledWith({
|
|
|
|
_id:@project_id
|
|
|
|
}, {
|
|
|
|
$set: { archived: true }
|
|
|
|
}).should.equal true
|
2014-04-07 08:47:10 -04:00
|
|
|
done()
|
|
|
|
|
2014-06-03 12:35:44 -04:00
|
|
|
describe "restoreProject", ->
|
|
|
|
beforeEach ->
|
|
|
|
@Project.update.callsArgWith(2)
|
|
|
|
|
|
|
|
it "should unset the archive attribute", (done)->
|
|
|
|
@deleter.restoreProject @project_id, =>
|
|
|
|
@Project.update.calledWith({
|
|
|
|
_id: @project_id
|
|
|
|
}, {
|
|
|
|
$unset: { archived: true }
|
|
|
|
}).should.equal true
|
|
|
|
done()
|
|
|
|
|