Factor out common joinProject logic to provide and HTTP end point for the real-time API
This commit is contained in:
@@ -16,7 +16,9 @@ describe "EditorController", ->
|
||||
@doc_id = "test-doc-id"
|
||||
@source = "dropbox"
|
||||
|
||||
@projectModelView = "projectModelView"
|
||||
@projectModelView =
|
||||
_id: @project_id
|
||||
owner:{_id:"something"}
|
||||
|
||||
@user =
|
||||
_id: @user_id = "user-id"
|
||||
@@ -65,6 +67,7 @@ describe "EditorController", ->
|
||||
'../Project/ProjectDetailsHandler': @ProjectDetailsHandler
|
||||
'../Project/ProjectDeleter' : @ProjectDeleter
|
||||
'../Project/ProjectGetter' : @ProjectGetter = {}
|
||||
'../User/UserGetter': @UserGetter = {}
|
||||
'../Collaborators/CollaboratorsHandler': @CollaboratorsHandler
|
||||
'../DocumentUpdater/DocumentUpdaterHandler' : @DocumentUpdaterHandler
|
||||
'../Subscription/LimitationsManager' : @LimitationsManager
|
||||
@@ -85,8 +88,6 @@ describe "EditorController", ->
|
||||
beforeEach ->
|
||||
sinon.spy(@client, "set")
|
||||
sinon.spy(@client, "get")
|
||||
@ProjectGetter.getProjectWithoutDocLines = sinon.stub().callsArgWith(1, null, @project)
|
||||
@ProjectGetter.populateProjectWithUsers = sinon.stub().callsArgWith(1, null, @project)
|
||||
@AuthorizationManager.setPrivilegeLevelOnClient = sinon.stub()
|
||||
@EditorRealTimeController.emitToRoom = sinon.stub()
|
||||
@ConnectedUsersManager.updateUserPosition.callsArgWith(4)
|
||||
@@ -94,20 +95,9 @@ describe "EditorController", ->
|
||||
|
||||
describe "when authorized", ->
|
||||
beforeEach ->
|
||||
@AuthorizationManager.getPrivilegeLevelForProject =
|
||||
sinon.stub().callsArgWith(2, null, true, "owner")
|
||||
@EditorController.buildJoinProjectView = sinon.stub().callsArgWith(2, null, @projectModelView, "owner")
|
||||
@EditorController.joinProject(@client, @user, @project_id, @callback)
|
||||
|
||||
it "should find the project without doc lines", ->
|
||||
@ProjectGetter.getProjectWithoutDocLines
|
||||
.calledWith(@project_id)
|
||||
.should.equal true
|
||||
|
||||
it "should populate the user references in the project", ->
|
||||
@ProjectGetter.populateProjectWithUsers
|
||||
.calledWith(@project)
|
||||
.should.equal true
|
||||
|
||||
it "should set the privilege level on the client", ->
|
||||
@AuthorizationManager.setPrivilegeLevelOnClient
|
||||
.calledWith(@client, "owner")
|
||||
@@ -119,21 +109,15 @@ describe "EditorController", ->
|
||||
it "should set the project_id of the client", ->
|
||||
@client.set.calledWith("project_id", @project_id).should.equal true
|
||||
|
||||
it "should return the project model view, privilege level and protocol version", ->
|
||||
@callback.calledWith(null, @projectModelView, "owner", @EditorController.protocolVersion).should.equal true
|
||||
|
||||
it "should mark the user as connected with the ConnectedUsersManager", ->
|
||||
@ConnectedUsersManager.updateUserPosition.calledWith(@project_id, @client.id, @user, null).should.equal true
|
||||
|
||||
it "should remove the flag to send a user a message about the project being deleted", ->
|
||||
@ProjectDeleter.unmarkAsDeletedByExternalSource
|
||||
.calledWith(@project)
|
||||
.should.equal true
|
||||
|
||||
it "should return the project model view, privilege level and protocol version", ->
|
||||
@callback.calledWith(null, @projectModelView, "owner", @EditorController.protocolVersion).should.equal true
|
||||
|
||||
describe "when not authorized", ->
|
||||
beforeEach ->
|
||||
@AuthorizationManager.getPrivilegeLevelForProject =
|
||||
sinon.stub().callsArgWith(2, null, false)
|
||||
@EditorController.buildJoinProjectView = sinon.stub().callsArgWith(2, null, null, false)
|
||||
@EditorController.joinProject(@client, @user, @project_id, @callback)
|
||||
|
||||
it "should not set the privilege level on the client", ->
|
||||
@@ -148,6 +132,61 @@ describe "EditorController", ->
|
||||
|
||||
it "should return an error", ->
|
||||
@callback.calledWith(sinon.match.truthy).should.equal true
|
||||
|
||||
describe "when the project is marked as deleted", ->
|
||||
beforeEach ->
|
||||
@projectModelView.deletedByExternalDataSource = true
|
||||
@EditorController.buildJoinProjectView = sinon.stub().callsArgWith(2, null, @projectModelView, "owner")
|
||||
@EditorController.joinProject(@client, @user, @project_id, @callback)
|
||||
|
||||
it "should remove the flag to send a user a message about the project being deleted", ->
|
||||
@ProjectDeleter.unmarkAsDeletedByExternalSource
|
||||
.calledWith(@project_id)
|
||||
.should.equal true
|
||||
|
||||
describe "buildJoinProjectView", ->
|
||||
beforeEach ->
|
||||
@ProjectGetter.getProjectWithoutDocLines = sinon.stub().callsArgWith(1, null, @project)
|
||||
@ProjectGetter.populateProjectWithUsers = sinon.stub().callsArgWith(1, null, @project)
|
||||
@UserGetter.getUser = sinon.stub().callsArgWith(2, null, @user)
|
||||
|
||||
describe "when authorized", ->
|
||||
beforeEach ->
|
||||
@AuthorizationManager.getPrivilegeLevelForProject =
|
||||
sinon.stub().callsArgWith(2, null, true, "owner")
|
||||
@EditorController.buildJoinProjectView(@project_id, @user_id, @callback)
|
||||
|
||||
it "should find the project without doc lines", ->
|
||||
@ProjectGetter.getProjectWithoutDocLines
|
||||
.calledWith(@project_id)
|
||||
.should.equal true
|
||||
|
||||
it "should populate the user references in the project", ->
|
||||
@ProjectGetter.populateProjectWithUsers
|
||||
.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
|
||||
.calledWith(@project, @user)
|
||||
.should.equal true
|
||||
|
||||
it "should return the project model view, privilege level and protocol version", ->
|
||||
@callback.calledWith(null, @projectModelView, "owner", @EditorController.protocolVersion).should.equal true
|
||||
|
||||
describe "when not authorized", ->
|
||||
beforeEach ->
|
||||
@AuthorizationManager.getPrivilegeLevelForProject =
|
||||
sinon.stub().callsArgWith(2, null, false, null)
|
||||
@EditorController.buildJoinProjectView(@project_id, @user_id, @callback)
|
||||
|
||||
it "should return false in the callback", ->
|
||||
@callback.calledWith(null, null, false, @EditorController.protocolVersion).should.equal true
|
||||
|
||||
|
||||
describe "leaveProject", ->
|
||||
|
||||
@@ -7,16 +7,69 @@ describe "EditorHttpController", ->
|
||||
beforeEach ->
|
||||
@EditorHttpController = SandboxedModule.require modulePath, requires:
|
||||
'../Project/ProjectEntityHandler' : @ProjectEntityHandler = {}
|
||||
'../Project/ProjectDeleter' : @ProjectDeleter = {}
|
||||
"./EditorRealTimeController": @EditorRealTimeController = {}
|
||||
"logger-sharelatex": @logger = { log: sinon.stub(), error: sinon.stub() }
|
||||
"./EditorController": @EditorController = {}
|
||||
'../../infrastructure/Metrics': @Metrics = {inc: sinon.stub()}
|
||||
|
||||
@project_id = "mock-project-id"
|
||||
@doc_id = "mock-doc-id"
|
||||
@user_id = "mock-user-id"
|
||||
@parent_folder_id = "mock-folder-id"
|
||||
@req = {}
|
||||
@res =
|
||||
send: sinon.stub()
|
||||
json: sinon.stub()
|
||||
|
||||
describe "joinProject", ->
|
||||
beforeEach ->
|
||||
@req.params =
|
||||
Project_id: @project_id
|
||||
@req.query =
|
||||
user_id: @user_id
|
||||
@projectView = {
|
||||
_id: @project_id
|
||||
}
|
||||
@EditorController.buildJoinProjectView = sinon.stub().callsArgWith(2, null, @projectView, "owner")
|
||||
@ProjectDeleter.unmarkAsDeletedByExternalSource = sinon.stub()
|
||||
|
||||
describe "successfully", ->
|
||||
beforeEach ->
|
||||
@EditorHttpController.joinProject @req, @res
|
||||
|
||||
it "should get the project view", ->
|
||||
@EditorController.buildJoinProjectView
|
||||
.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
|
||||
|
||||
describe "restoreDoc", ->
|
||||
beforeEach ->
|
||||
|
||||
@@ -4,7 +4,7 @@ SandboxedModule = require('sandboxed-module')
|
||||
sinon = require('sinon')
|
||||
|
||||
|
||||
describe 'Project deleter', ->
|
||||
describe 'ProjectDeleter', ->
|
||||
|
||||
beforeEach ->
|
||||
@project_id = "12312"
|
||||
@@ -55,24 +55,12 @@ describe 'Project deleter', ->
|
||||
@project = {
|
||||
_id: @project_id
|
||||
}
|
||||
|
||||
describe "when the project does not have the flag set", ->
|
||||
beforeEach ->
|
||||
@project.deletedByExternalDataSource = false
|
||||
@deleter.unmarkAsDeletedByExternalSource @project, @callback
|
||||
|
||||
it "should not update the project", ->
|
||||
@Project.update.called.should.equal false
|
||||
|
||||
describe "when the project does have the flag set", ->
|
||||
beforeEach ->
|
||||
@project.deletedByExternalDataSource = true
|
||||
@deleter.unmarkAsDeletedByExternalSource @project, @callback
|
||||
|
||||
it "should remove the flag from the project", ->
|
||||
@Project.update
|
||||
.calledWith({_id: @project_id}, {deletedByExternalDataSource:false})
|
||||
.should.equal true
|
||||
@deleter.unmarkAsDeletedByExternalSource @project_id, @callback
|
||||
|
||||
it "should remove the flag from the project", ->
|
||||
@Project.update
|
||||
.calledWith({_id: @project_id}, {deletedByExternalDataSource:false})
|
||||
.should.equal true
|
||||
|
||||
describe "deleteUsersProjects", ->
|
||||
|
||||
|
||||
Reference in New Issue
Block a user