moved project list to new project controller and added tests

This commit is contained in:
Henry Oswald 2014-04-08 16:40:12 +01:00
parent 6b6c447901
commit 0c921d9188
4 changed files with 90 additions and 27 deletions

View file

@ -5,8 +5,10 @@ projectDuplicator = require("./ProjectDuplicator")
projectCreationHandler = require("./ProjectCreationHandler")
metrics = require('../../infrastructure/Metrics')
sanitize = require('sanitizer')
Project = require('../../models/Project').Project
TagsHandler = require("../Tags/TagsHandler")
SubscriptionLocator = require("../Subscription/SubscriptionLocator")
_ = require("underscore")
module.exports =
@ -50,4 +52,45 @@ module.exports =
res.send 500
else
logger.log project: project, user: user, name: projectName, type: template, "created project"
res.send {project_id:project._id}
res.send {project_id:project._id}
projectListPage: (req, res, next)->
timer = new metrics.Timer("project-list")
user_id = req.session.user._id
SubscriptionLocator.getUsersSubscription user_id, (err, subscription)->
logger.log user_id: user_id, "project list timer - Subscription.getUsersSubscription"
return next(error) if error?
# TODO: Remove this one month after the ability to start free trials was removed
if subscription? and subscription.freeTrial? and subscription.freeTrial.expiresAt?
freeTrial =
expired: !!subscription.freeTrial.downgraded
expiresAt: SubscriptionFormatters.formatDate(subscription.freeTrial.expiresAt)
TagsHandler.getAllTags user_id, (err, tags, tagsGroupedByProject)->
logger.log user_id: user_id, "project list timer - TagsHandler.getAllTags"
Project.findAllUsersProjects user_id, 'name lastUpdated publicAccesLevel', (projects, collabertions, readOnlyProjects)->
logger.log user_id: user_id, "project list timer - Project.findAllUsersProjects"
for project in projects
project.accessLevel = "owner"
for project in collabertions
project.accessLevel = "readWrite"
for project in readOnlyProjects
project.accessLevel = "readOnly"
projects = projects.concat(collabertions).concat(readOnlyProjects)
projects = projects.map (project)->
project.tags = tagsGroupedByProject[project._id] || []
return project
tags = _.sortBy tags, (tag)->
-tag.project_ids.length
logger.log projects:projects, collabertions:collabertions, readOnlyProjects:readOnlyProjects, user_id:user_id, "rendering project list"
sortedProjects = _.sortBy projects, (project)->
return - project.lastUpdated
res.render 'project/list',
title:'Your Projects'
priority_title: true
projects: sortedProjects
freeTrial: freeTrial
tags:tags
projectTabActive: true
logger.log user_id: user_id, "project list timer - Finished"
timer.done()

View file

@ -68,27 +68,7 @@ module.exports = class ProjectController
logger.log user_id: user_id, duration: (new Date() - startTime), "project list timer - Finished"
timer.done()
apiNewProject: (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}
loadEditor: (req, res)->
timer = new metrics.Timer("load-editor")

View file

@ -92,8 +92,8 @@ module.exports = class Router
app.get '/user/personal_info', AuthenticationController.requireLogin(allow_auth_token: true), PersonalInfoController.getLoggedInUsersPersonalInfo
app.get '/user/:user_id/personal_info', httpAuth, PersonalInfoController.getPersonalInfo
app.get '/project', AuthenticationController.requireLogin(), Project.list
app.post '/project/new', AuthenticationController.requireLogin(), ProjectController.NewProject
app.get '/project', AuthenticationController.requireLogin(), ProjectController.projectListPage
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

@ -20,13 +20,21 @@ describe "ProjectController", ->
@ProjectCreationHandler =
createExampleProject: sinon.stub().callsArgWith(2, null, {_id:@project_id})
createBasicProject: sinon.stub().callsArgWith(2, null, {_id:@project_id})
@SubscriptionLocator =
getUsersSubscription: sinon.stub()
@TagsHandler =
getAllTags: sinon.stub()
@ProjectModel =
findAllUsersProjects: sinon.stub()
@ProjectController = SandboxedModule.require modulePath, requires:
"settings-sharelatex":@settings
"logger-sharelatex": log:->
"./ProjectDeleter": @ProjectDeleter
"./ProjectDuplicator": @ProjectDuplicator
"./ProjectCreationHandler": @ProjectCreationHandler
"../Subscription/SubscriptionLocator": @SubscriptionLocator
"../Tags/TagsHandler":@TagsHandler
'../../models/Project': Project:@ProjectModel
@user =
_id:"!£123213kjljkl"
@ -85,4 +93,36 @@ describe "ProjectController", ->
describe "projectListPage", ->
beforeEach ->
@tags = [{name:1, project_ids:["1","2","3"]}, {name:2, project_ids:["a","1"]}, {name:3, project_ids:["a", "b", "c", "d"]}]
@projects = [{lastUpdated:1, _id:1}, {lastUpdated:2, _id:2}]
@collabertions = [{lastUpdated:5, _id:5}]
@readOnly = [{lastUpdated:3, _id:3}]
@SubscriptionLocator.getUsersSubscription.callsArgWith(1, null, {})
@TagsHandler.getAllTags.callsArgWith(1, null, @tags, {})
@ProjectModel.findAllUsersProjects.callsArgWith(2, @projects, @collabertions, @readOnly)
it "should render the project/list page", (done)->
@req.body.template = "example"
@res.render = (pageName, opts)=>
pageName.should.equal "project/list"
done()
@ProjectController.projectListPage @req, @res
it "should send the tags", (done)->
@res.render = (pageName, opts)=>
opts.tags.length.should.equal @tags.length
done()
@ProjectController.projectListPage @req, @res
it "should send the projects", (done)->
@res.render = (pageName, opts)=>
opts.projects.length.should.equal (@projects.length + @collabertions.length + @readOnly.length)
done()
@ProjectController.projectListPage @req, @res