From 0c921d9188cdb88416e80a00609ff34fe51ebe81 Mon Sep 17 00:00:00 2001 From: Henry Oswald Date: Tue, 8 Apr 2014 16:40:12 +0100 Subject: [PATCH] moved project list to new project controller and added tests --- .../Features/Project/ProjectController.coffee | 49 +++++++++++++++++-- .../controllers/ProjectController.coffee | 22 +-------- services/web/app/coffee/router.coffee | 4 +- .../Project/ProjectControllerTests.coffee | 42 +++++++++++++++- 4 files changed, 90 insertions(+), 27 deletions(-) diff --git a/services/web/app/coffee/Features/Project/ProjectController.coffee b/services/web/app/coffee/Features/Project/ProjectController.coffee index 15ce3fc6f6..eb410d3b9a 100644 --- a/services/web/app/coffee/Features/Project/ProjectController.coffee +++ b/services/web/app/coffee/Features/Project/ProjectController.coffee @@ -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} \ No newline at end of file + 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() diff --git a/services/web/app/coffee/controllers/ProjectController.coffee b/services/web/app/coffee/controllers/ProjectController.coffee index 310caa5804..02316995c2 100755 --- a/services/web/app/coffee/controllers/ProjectController.coffee +++ b/services/web/app/coffee/controllers/ProjectController.coffee @@ -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") diff --git a/services/web/app/coffee/router.coffee b/services/web/app/coffee/router.coffee index 4f008d207f..bfe7185dae 100644 --- a/services/web/app/coffee/router.coffee +++ b/services/web/app/coffee/router.coffee @@ -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 diff --git a/services/web/test/UnitTests/coffee/Project/ProjectControllerTests.coffee b/services/web/test/UnitTests/coffee/Project/ProjectControllerTests.coffee index bab6fba90a..808d45207a 100644 --- a/services/web/test/UnitTests/coffee/Project/ProjectControllerTests.coffee +++ b/services/web/test/UnitTests/coffee/Project/ProjectControllerTests.coffee @@ -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 +