moved new project from old proj controller to new one with test

This commit is contained in:
Henry Oswald 2014-04-08 15:25:22 +01:00
parent 6f84f4214f
commit 15bddae931
3 changed files with 57 additions and 2 deletions

View file

@ -1,7 +1,9 @@
logger = require("logger-sharelatex")
projectDeleter = require("./ProjectDeleter")
projectDuplicator = require("./ProjectDuplicator")
projectCreationHandler = require("./ProjectCreationHandler")
metrics = require('../../infrastructure/Metrics')
sanitize = require('sanitizer')
@ -27,4 +29,27 @@ module.exports =
if err?
logger.error err:err, project_id: project_id, user_id: req.session.user._id, "error cloning project"
return next(err)
res.send(project_id:project._id)
res.send(project_id:project._id)
newProject: (req, res)->
user = req.session.user
projectName = sanitize.escape(req.body.projectName)
template = sanitize.escape(req.body.template)
logger.log user: user, type: template, name: projectName, "creating project"
if template == 'example'
projectCreationHandler.createExampleProject user._id, projectName, (err, project)->
if err?
logger.error err: err, project: project, user: user, name: projectName, type: "example", "error creating project"
res.send 500
else
logger.log project: project, user: user, name: projectName, type: "example", "created project"
res.send {project_id:project._id}
else
projectCreationHandler.createBasicProject user._id, projectName, (err, project)->
if err?
logger.error err: err, project: project, user: user, name: projectName, type: "basic", "error creating project"
res.send 500
else
logger.log project: project, user: user, name: projectName, type: "basic", "created project"
res.send {project_id:project._id}

View file

@ -93,7 +93,7 @@ module.exports = class Router
app.get '/user/:user_id/personal_info', httpAuth, PersonalInfoController.getPersonalInfo
app.get '/project', AuthenticationController.requireLogin(), Project.list
app.post '/project/new', AuthenticationController.requireLogin(), Project.apiNewProject
app.post '/project/new', AuthenticationController.requireLogin(), ProjectController.NewProject
app.get '/project/new/template', TemplatesMiddlewear.saveTemplateDataInSession, AuthenticationController.requireLogin(), TemplatesController.createProjectFromZipTemplate
app.get '/Project/:Project_id', SecurityManager.requestCanAccessProject, Project.loadEditor

View file

@ -17,11 +17,15 @@ describe "ProjectController", ->
deleteProject: sinon.stub().callsArgWith(1)
@ProjectDuplicator =
duplicate: sinon.stub().callsArgWith(3, null, {_id:@project_id})
@ProjectCreationHandler =
createExampleProject: sinon.stub().callsArgWith(2, null, {_id:@project_id})
createBasicProject: sinon.stub().callsArgWith(2, null, {_id:@project_id})
@ProjectController = SandboxedModule.require modulePath, requires:
"settings-sharelatex":@settings
"logger-sharelatex": log:->
"./ProjectDeleter": @ProjectDeleter
"./ProjectDuplicator": @ProjectDuplicator
"./ProjectCreationHandler": @ProjectCreationHandler
@user =
@ -56,3 +60,29 @@ describe "ProjectController", ->
json.project_id.should.equal @project_id
done()
@ProjectController.cloneProject @req, @res
describe "newProject", ->
it "should call the projectCreationHandler with createExampleProject", (done)->
@req.body.template = "example"
@res.send = (json)=>
@ProjectCreationHandler.createExampleProject.calledWith(@user._id, @projectName).should.equal true
@ProjectCreationHandler.createBasicProject.called.should.equal false
done()
@ProjectController.newProject @req, @res
it "should call the projectCreationHandler with createBasicProject", (done)->
@req.body.template = "basic"
@res.send = (json)=>
@ProjectCreationHandler.createExampleProject.called.should.equal false
@ProjectCreationHandler.createBasicProject.calledWith(@user._id, @projectName).should.equal true
done()
@ProjectController.newProject @req, @res