2014-06-05 15:18:25 +00:00
|
|
|
SandboxedModule = require('sandboxed-module')
|
|
|
|
sinon = require('sinon')
|
|
|
|
require('chai').should()
|
|
|
|
modulePath = require('path').join __dirname, '../../../../app/js/Features/Editor/EditorHttpController'
|
|
|
|
|
|
|
|
describe "EditorHttpController", ->
|
|
|
|
beforeEach ->
|
|
|
|
@EditorHttpController = SandboxedModule.require modulePath, requires:
|
|
|
|
'../Project/ProjectEntityHandler' : @ProjectEntityHandler = {}
|
2014-11-07 12:31:47 +00:00
|
|
|
'../Project/ProjectDeleter' : @ProjectDeleter = {}
|
2015-02-05 16:37:37 +00:00
|
|
|
'../Project/ProjectGetter' : @ProjectGetter = {}
|
|
|
|
'../User/UserGetter' : @UserGetter = {}
|
2016-03-10 17:17:26 +00:00
|
|
|
"../Authorization/AuthorizationManager": @AuthorizationManager = {}
|
2015-02-05 16:37:37 +00:00
|
|
|
'../Project/ProjectEditorHandler': @ProjectEditorHandler = {}
|
2014-06-05 15:18:25 +00:00
|
|
|
"./EditorRealTimeController": @EditorRealTimeController = {}
|
|
|
|
"logger-sharelatex": @logger = { log: sinon.stub(), error: sinon.stub() }
|
2014-06-22 11:10:42 +00:00
|
|
|
"./EditorController": @EditorController = {}
|
2014-11-07 12:31:47 +00:00
|
|
|
'../../infrastructure/Metrics': @Metrics = {inc: sinon.stub()}
|
2016-03-07 15:25:10 +00:00
|
|
|
"../Collaborators/CollaboratorsHandler": @CollaboratorsHandler = {}
|
2016-08-01 16:05:37 +00:00
|
|
|
"../Collaborators/CollaboratorsInviteHandler": @CollaboratorsInviteHandler = {}
|
|
|
|
|
2014-06-05 15:18:25 +00:00
|
|
|
@project_id = "mock-project-id"
|
|
|
|
@doc_id = "mock-doc-id"
|
2014-11-07 12:31:47 +00:00
|
|
|
@user_id = "mock-user-id"
|
2014-06-22 11:10:42 +00:00
|
|
|
@parent_folder_id = "mock-folder-id"
|
2014-06-05 15:18:25 +00:00
|
|
|
@req = {}
|
|
|
|
@res =
|
|
|
|
send: sinon.stub()
|
2015-07-08 15:56:38 +00:00
|
|
|
sendStatus: sinon.stub()
|
2014-06-05 15:18:25 +00:00
|
|
|
json: sinon.stub()
|
2015-02-05 16:37:37 +00:00
|
|
|
@callback = sinon.stub()
|
2014-11-07 12:31:47 +00:00
|
|
|
|
|
|
|
describe "joinProject", ->
|
|
|
|
beforeEach ->
|
|
|
|
@req.params =
|
|
|
|
Project_id: @project_id
|
|
|
|
@req.query =
|
|
|
|
user_id: @user_id
|
|
|
|
@projectView = {
|
|
|
|
_id: @project_id
|
|
|
|
}
|
2015-02-05 16:37:37 +00:00
|
|
|
@EditorHttpController._buildJoinProjectView = sinon.stub().callsArgWith(2, null, @projectView, "owner")
|
2014-11-07 12:31:47 +00:00
|
|
|
@ProjectDeleter.unmarkAsDeletedByExternalSource = sinon.stub()
|
|
|
|
|
|
|
|
describe "successfully", ->
|
|
|
|
beforeEach ->
|
|
|
|
@EditorHttpController.joinProject @req, @res
|
|
|
|
|
|
|
|
it "should get the project view", ->
|
2015-02-05 16:37:37 +00:00
|
|
|
@EditorHttpController._buildJoinProjectView
|
2014-11-07 12:31:47 +00:00
|
|
|
.calledWith(@project_id, @user_id)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should return the project and privilege level", ->
|
|
|
|
@res.json
|
|
|
|
.calledWith({
|
|
|
|
project: @projectView
|
|
|
|
privilegeLevel: "owner"
|
|
|
|
})
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should not try to unmark the project as deleted", ->
|
|
|
|
@ProjectDeleter.unmarkAsDeletedByExternalSource
|
|
|
|
.called
|
|
|
|
.should.equal false
|
|
|
|
|
|
|
|
it "should send an inc metric", ->
|
|
|
|
@Metrics.inc
|
|
|
|
.calledWith("editor.join-project")
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
describe "when the project is marked as deleted", ->
|
|
|
|
beforeEach ->
|
|
|
|
@projectView.deletedByExternalDataSource = true
|
|
|
|
@EditorHttpController.joinProject @req, @res
|
|
|
|
|
|
|
|
it "should unmark the project as deleted", ->
|
|
|
|
@ProjectDeleter.unmarkAsDeletedByExternalSource
|
|
|
|
.calledWith(@project_id)
|
|
|
|
.should.equal true
|
2016-03-22 09:53:47 +00:00
|
|
|
|
|
|
|
describe "with an anonymous user", ->
|
|
|
|
beforeEach ->
|
|
|
|
@req.query =
|
|
|
|
user_id: "anonymous-user"
|
|
|
|
@EditorHttpController.joinProject @req, @res
|
|
|
|
|
|
|
|
it "should pass the user id as null", ->
|
|
|
|
@EditorHttpController._buildJoinProjectView
|
|
|
|
.calledWith(@project_id, null)
|
|
|
|
.should.equal true
|
2014-06-05 15:18:25 +00:00
|
|
|
|
2015-02-05 16:37:37 +00:00
|
|
|
describe "_buildJoinProjectView", ->
|
|
|
|
beforeEach ->
|
|
|
|
@project =
|
|
|
|
_id: @project_id
|
|
|
|
owner_ref:{_id:"something"}
|
|
|
|
@user =
|
|
|
|
_id: @user_id = "user-id"
|
|
|
|
projects: {}
|
2016-03-07 15:25:10 +00:00
|
|
|
@members = ["members", "mock"]
|
2015-02-05 16:37:37 +00:00
|
|
|
@projectModelView =
|
|
|
|
_id: @project_id
|
|
|
|
owner:{_id:"something"}
|
|
|
|
view: true
|
2016-08-01 16:05:37 +00:00
|
|
|
@invites = [
|
|
|
|
{_id: "invite_one", email: "user-one@example.com", privileges: "readOnly", projectId: @project._id}
|
|
|
|
{_id: "invite_two", email: "user-two@example.com", privileges: "readOnly", projectId: @project._id}
|
|
|
|
]
|
2015-02-05 16:37:37 +00:00
|
|
|
@ProjectEditorHandler.buildProjectModelView = sinon.stub().returns(@projectModelView)
|
|
|
|
@ProjectGetter.getProjectWithoutDocLines = sinon.stub().callsArgWith(1, null, @project)
|
2016-03-07 15:25:10 +00:00
|
|
|
@CollaboratorsHandler.getMembersWithPrivilegeLevels = sinon.stub().callsArgWith(1, null, @members)
|
2016-08-01 16:05:37 +00:00
|
|
|
@CollaboratorsInviteHandler.getAllInvites = sinon.stub().callsArgWith(1, null, @invites)
|
2015-02-05 16:37:37 +00:00
|
|
|
@UserGetter.getUser = sinon.stub().callsArgWith(2, null, @user)
|
|
|
|
|
|
|
|
describe "when authorized", ->
|
|
|
|
beforeEach ->
|
|
|
|
@AuthorizationManager.getPrivilegeLevelForProject =
|
2016-03-14 17:06:57 +00:00
|
|
|
sinon.stub().callsArgWith(2, null, "owner")
|
2015-02-05 16:37:37 +00:00
|
|
|
@EditorHttpController._buildJoinProjectView(@project_id, @user_id, @callback)
|
|
|
|
|
|
|
|
it "should find the project without doc lines", ->
|
|
|
|
@ProjectGetter.getProjectWithoutDocLines
|
|
|
|
.calledWith(@project_id)
|
|
|
|
.should.equal true
|
|
|
|
|
2016-03-07 15:25:10 +00:00
|
|
|
it "should get the list of users in the project", ->
|
|
|
|
@CollaboratorsHandler.getMembersWithPrivilegeLevels
|
2015-02-05 16:37:37 +00:00
|
|
|
.calledWith(@project)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should look up the user", ->
|
|
|
|
@UserGetter.getUser
|
|
|
|
.calledWith(@user_id, { isAdmin: true })
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should check the privilege level", ->
|
|
|
|
@AuthorizationManager.getPrivilegeLevelForProject
|
2016-03-10 17:17:26 +00:00
|
|
|
.calledWith(@user_id, @project_id)
|
2015-02-05 16:37:37 +00:00
|
|
|
.should.equal true
|
|
|
|
|
2016-08-01 16:05:37 +00:00
|
|
|
it 'should include the invites', ->
|
|
|
|
@CollaboratorsInviteHandler.getAllInvites
|
|
|
|
.calledWith(@project._id)
|
|
|
|
.should.equal true
|
|
|
|
|
2015-02-05 16:37:37 +00:00
|
|
|
it "should return the project model view, privilege level and protocol version", ->
|
|
|
|
@callback.calledWith(null, @projectModelView, "owner").should.equal true
|
|
|
|
|
|
|
|
describe "when not authorized", ->
|
|
|
|
beforeEach ->
|
|
|
|
@AuthorizationManager.getPrivilegeLevelForProject =
|
2016-03-14 17:06:57 +00:00
|
|
|
sinon.stub().callsArgWith(2, null, null)
|
2015-02-05 16:37:37 +00:00
|
|
|
@EditorHttpController._buildJoinProjectView(@project_id, @user_id, @callback)
|
|
|
|
|
|
|
|
it "should return false in the callback", ->
|
|
|
|
@callback.calledWith(null, null, false).should.equal true
|
|
|
|
|
2014-06-05 15:18:25 +00:00
|
|
|
describe "restoreDoc", ->
|
|
|
|
beforeEach ->
|
|
|
|
@req.params =
|
|
|
|
Project_id: @project_id
|
|
|
|
doc_id: @doc_id
|
|
|
|
@req.body =
|
|
|
|
name: @name = "doc-name"
|
|
|
|
@ProjectEntityHandler.restoreDoc = sinon.stub().callsArgWith(3, null,
|
|
|
|
@doc = { "mock": "doc", _id: @new_doc_id = "new-doc-id" }
|
|
|
|
@folder_id = "mock-folder-id"
|
|
|
|
)
|
|
|
|
@EditorRealTimeController.emitToRoom = sinon.stub()
|
|
|
|
@EditorHttpController.restoreDoc @req, @res
|
|
|
|
|
|
|
|
it "should restore the doc", ->
|
|
|
|
@ProjectEntityHandler.restoreDoc
|
|
|
|
.calledWith(@project_id, @doc_id, @name)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should the real-time clients about the new doc", ->
|
|
|
|
@EditorRealTimeController.emitToRoom
|
|
|
|
.calledWith(@project_id, 'reciveNewDoc', @folder_id, @doc)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should return the new doc id", ->
|
|
|
|
@res.json
|
|
|
|
.calledWith(doc_id: @new_doc_id)
|
|
|
|
.should.equal true
|
2014-06-22 11:10:42 +00:00
|
|
|
|
|
|
|
describe "addDoc", ->
|
|
|
|
beforeEach ->
|
|
|
|
@doc = { "mock": "doc" }
|
|
|
|
@req.params =
|
|
|
|
Project_id: @project_id
|
|
|
|
@req.body =
|
|
|
|
name: @name = "doc-name"
|
|
|
|
parent_folder_id: @parent_folder_id
|
2014-10-15 14:18:31 +00:00
|
|
|
@EditorController.addDoc = sinon.stub().callsArgWith(5, null, @doc)
|
2014-06-22 11:10:42 +00:00
|
|
|
|
2015-03-02 12:46:02 +00:00
|
|
|
describe "successfully", ->
|
|
|
|
beforeEach ->
|
|
|
|
@EditorHttpController.addDoc @req, @res
|
2014-06-22 11:10:42 +00:00
|
|
|
|
2015-03-02 12:46:02 +00:00
|
|
|
it "should call EditorController.addDoc", ->
|
|
|
|
@EditorController.addDoc
|
|
|
|
.calledWith(@project_id, @parent_folder_id, @name, [], "editor")
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should send the doc back as JSON", ->
|
|
|
|
@res.json
|
|
|
|
.calledWith(@doc)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
describe "unsuccesfully", ->
|
|
|
|
beforeEach ->
|
|
|
|
@req.body.name = ""
|
|
|
|
@EditorHttpController.addDoc @req, @res
|
|
|
|
|
|
|
|
it "should send back a bad request status code", ->
|
2015-07-08 15:56:38 +00:00
|
|
|
@res.sendStatus.calledWith(400).should.equal true
|
2014-06-22 11:10:42 +00:00
|
|
|
|
2014-06-22 13:39:38 +00:00
|
|
|
describe "addFolder", ->
|
|
|
|
beforeEach ->
|
|
|
|
@folder = { "mock": "folder" }
|
|
|
|
@req.params =
|
|
|
|
Project_id: @project_id
|
|
|
|
@req.body =
|
|
|
|
name: @name = "folder-name"
|
|
|
|
parent_folder_id: @parent_folder_id
|
2014-10-16 10:26:57 +00:00
|
|
|
@EditorController.addFolder = sinon.stub().callsArgWith(4, null, @folder)
|
2014-06-22 13:39:38 +00:00
|
|
|
|
2015-03-02 12:46:02 +00:00
|
|
|
describe "successfully", ->
|
|
|
|
beforeEach ->
|
|
|
|
@EditorHttpController.addFolder @req, @res
|
|
|
|
|
|
|
|
it "should call EditorController.addFolder", ->
|
|
|
|
@EditorController.addFolder
|
|
|
|
.calledWith(@project_id, @parent_folder_id, @name, "editor")
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should send the folder back as JSON", ->
|
|
|
|
@res.json
|
|
|
|
.calledWith(@folder)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
describe "unsuccesfully", ->
|
|
|
|
|
|
|
|
beforeEach ->
|
|
|
|
@req.body.name = ""
|
|
|
|
@EditorHttpController.addFolder @req, @res
|
|
|
|
|
|
|
|
it "should send back a bad request status code", ->
|
2015-07-08 15:56:38 +00:00
|
|
|
@res.sendStatus.calledWith(400).should.equal true
|
2014-06-22 13:39:38 +00:00
|
|
|
|
|
|
|
|
2014-06-22 19:08:56 +00:00
|
|
|
describe "renameEntity", ->
|
|
|
|
beforeEach ->
|
|
|
|
@req.params =
|
|
|
|
Project_id: @project_id
|
|
|
|
entity_id: @entity_id = "entity-id-123"
|
|
|
|
entity_type: @entity_type = "entity-type"
|
|
|
|
@req.body =
|
|
|
|
name: @name = "new-name"
|
|
|
|
@EditorController.renameEntity = sinon.stub().callsArg(4)
|
|
|
|
@EditorHttpController.renameEntity @req, @res
|
|
|
|
|
|
|
|
it "should call EditorController.renameEntity", ->
|
|
|
|
@EditorController.renameEntity
|
|
|
|
.calledWith(@project_id, @entity_id, @entity_type, @name)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should send back a success response", ->
|
2015-07-08 15:56:38 +00:00
|
|
|
@res.sendStatus.calledWith(204).should.equal true
|
2014-06-22 19:08:56 +00:00
|
|
|
|
2014-10-07 10:16:26 +00:00
|
|
|
describe "renameEntity with long name", ->
|
|
|
|
beforeEach ->
|
|
|
|
@req.params =
|
|
|
|
Project_id: @project_id
|
|
|
|
entity_id: @entity_id = "entity-id-123"
|
|
|
|
entity_type: @entity_type = "entity-type"
|
|
|
|
@req.body =
|
|
|
|
name: @name = "EDMUBEEBKBXUUUZERMNSXFFWIBHGSDAWGMRIQWJBXGWSBVWSIKLFPRBYSJEKMFHTRZBHVKJSRGKTBHMJRXPHORFHAKRNPZGGYIOTEDMUBEEBKBXUUUZERMNSXFFWIBHGSDAWGMRIQWJBXGWSBVWSIKLFPRBYSJEKMFHTRZBHVKJSRGKTBHMJRXPHORFHAKRNPZGGYIOT"
|
|
|
|
@EditorController.renameEntity = sinon.stub().callsArg(4)
|
|
|
|
@EditorHttpController.renameEntity @req, @res
|
|
|
|
|
|
|
|
it "should send back a bad request status code", ->
|
2015-07-08 15:56:38 +00:00
|
|
|
@res.sendStatus.calledWith(400).should.equal true
|
2014-10-07 10:16:26 +00:00
|
|
|
|
2015-03-02 12:46:02 +00:00
|
|
|
describe "rename entity with 0 length name", ->
|
|
|
|
|
|
|
|
beforeEach ->
|
|
|
|
@req.params =
|
|
|
|
Project_id: @project_id
|
|
|
|
entity_id: @entity_id = "entity-id-123"
|
|
|
|
entity_type: @entity_type = "entity-type"
|
|
|
|
@req.body =
|
|
|
|
name: @name = ""
|
|
|
|
@EditorController.renameEntity = sinon.stub().callsArg(4)
|
|
|
|
@EditorHttpController.renameEntity @req, @res
|
|
|
|
|
|
|
|
it "should send back a bad request status code", ->
|
2015-07-08 15:56:38 +00:00
|
|
|
@res.sendStatus.calledWith(400).should.equal true
|
2015-03-02 12:46:02 +00:00
|
|
|
|
|
|
|
|
2014-06-22 19:08:56 +00:00
|
|
|
describe "moveEntity", ->
|
|
|
|
beforeEach ->
|
|
|
|
@req.params =
|
|
|
|
Project_id: @project_id
|
|
|
|
entity_id: @entity_id = "entity-id-123"
|
|
|
|
entity_type: @entity_type = "entity-type"
|
|
|
|
@req.body =
|
|
|
|
folder_id: @folder_id = "folder-id-123"
|
|
|
|
@EditorController.moveEntity = sinon.stub().callsArg(4)
|
|
|
|
@EditorHttpController.moveEntity @req, @res
|
|
|
|
|
|
|
|
it "should call EditorController.moveEntity", ->
|
|
|
|
@EditorController.moveEntity
|
|
|
|
.calledWith(@project_id, @entity_id, @folder_id, @entity_type)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should send back a success response", ->
|
2015-07-08 15:56:38 +00:00
|
|
|
@res.sendStatus.calledWith(204).should.equal true
|
2014-06-22 19:08:56 +00:00
|
|
|
|
|
|
|
describe "deleteEntity", ->
|
|
|
|
beforeEach ->
|
|
|
|
@req.params =
|
|
|
|
Project_id: @project_id
|
|
|
|
entity_id: @entity_id = "entity-id-123"
|
|
|
|
entity_type: @entity_type = "entity-type"
|
2014-10-15 14:18:31 +00:00
|
|
|
@EditorController.deleteEntity = sinon.stub().callsArg(4)
|
2014-06-22 19:08:56 +00:00
|
|
|
@EditorHttpController.deleteEntity @req, @res
|
|
|
|
|
|
|
|
it "should call EditorController.deleteEntity", ->
|
|
|
|
@EditorController.deleteEntity
|
2014-10-15 14:18:31 +00:00
|
|
|
.calledWith(@project_id, @entity_id, @entity_type, "editor")
|
2014-06-22 19:08:56 +00:00
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should send back a success response", ->
|
2015-07-08 15:56:38 +00:00
|
|
|
@res.sendStatus.calledWith(204).should.equal true
|