Convert template publishing to use HTTP end points

This commit is contained in:
James Allen 2014-11-06 14:39:25 +00:00
parent 7b6ec86cef
commit d27af25bc9
5 changed files with 191 additions and 107 deletions

View file

@ -2,6 +2,8 @@ path = require('path')
ProjectUploadManager = require('../Uploads/ProjectUploadManager') ProjectUploadManager = require('../Uploads/ProjectUploadManager')
ProjectOptionsHandler = require("../Project/ProjectOptionsHandler") ProjectOptionsHandler = require("../Project/ProjectOptionsHandler")
ProjectDetailsHandler = require('../Project/ProjectDetailsHandler') ProjectDetailsHandler = require('../Project/ProjectDetailsHandler')
ProjectGetter = require('../Project/ProjectGetter')
EditorController = require('../Editor/EditorController')
TemplatesPublisher = require("./TemplatesPublisher") TemplatesPublisher = require("./TemplatesPublisher")
settings = require('settings-sharelatex') settings = require('settings-sharelatex')
fs = require('fs') fs = require('fs')
@ -36,28 +38,50 @@ module.exports =
delete req.session.templateData delete req.session.templateData
res.redirect "/project/#{project._id}" res.redirect "/project/#{project._id}"
publishProject: (user_id, project_id, callback)-> publishProject: (req, res, next) ->
logger.log user_id:user_id, project_id:project_id, "receiving request to publish project as template" project_id = req.params.Project_id
TemplatesPublisher.publish user_id, project_id, callback ProjectGetter.getProject project_id, {owner_ref: 1}, (error, project) ->
return callback(error) if error?
user_id = project.owner_ref.toString()
logger.log user_id:user_id, project_id:project_id, "receiving request to publish project as template"
TemplatesPublisher.publish user_id, project_id, (error) ->
return next(error) if error?
res.send 204
unPublishProject: (user_id, project_id, callback)-> unpublishProject: (req, res, next) ->
logger.log user_id:user_id, project_id:project_id, "receiving request to unpublish project as template" project_id = req.params.Project_id
TemplatesPublisher.unpublish user_id, project_id, callback ProjectGetter.getProject project_id, {owner_ref: 1}, (error, project) ->
return callback(error) if error?
getTemplateDetails: (user_id, project_id, callback)-> user_id = project.owner_ref.toString()
async.parallel { logger.log user_id:user_id, project_id:project_id, "receiving request to unpublish project"
details: (cb)-> TemplatesPublisher.unpublish user_id, project_id, (error) ->
TemplatesPublisher.getTemplateDetails user_id, project_id, cb return next(error) if error?
description: (cb)-> res.send 204
ProjectDetailsHandler.getProjectDescription project_id, cb
}, (err, results)-> updateProjectDescription: (req, res, next) ->
if err? project_id = req.params.Project_id
logger.err err:err, user_id:user_id, project_id:project_id, "something went wrong getting template details" {description} = req.body
return callback(err) EditorController.updateProjectDescription project_id, description, (error) ->
details = results.details return next(error) if error?
details.description = results.description res.send 204
callback(err, details)
getTemplateDetails: (req, res, next)->
project_id = req.params.Project_id
ProjectGetter.getProject project_id, {owner_ref: 1}, (error, project) ->
return next(error) if error?
user_id = project.owner_ref.toString()
async.parallel {
details: (cb)->
TemplatesPublisher.getTemplateDetails user_id, project_id, cb
description: (cb)->
ProjectDetailsHandler.getProjectDescription project_id, cb
}, (err, results)->
if err?
logger.err err:err, user_id:user_id, project_id:project_id, "something went wrong getting template details"
return next(err)
details = results.details
details.description = results.description
res.json details
setCompiler = (project_id, compiler, callback)-> setCompiler = (project_id, compiler, callback)->
if compiler? if compiler?

View file

@ -1,5 +1,6 @@
SecurityManager = require("../../managers/SecurityManager")
TemplatesWebController = require("./TemplatesWebController") TemplatesWebController = require("./TemplatesWebController")
TemplatesController = require("./TemplatesController")
middleWear = require("./TemplatesMiddlewear") middleWear = require("./TemplatesMiddlewear")
module.exports = module.exports =
@ -16,4 +17,7 @@ module.exports =
app.get "/templates/:template_id/v/:version/:file_type", TemplatesWebController.proxyToTemplatesApi app.get "/templates/:template_id/v/:version/:file_type", TemplatesWebController.proxyToTemplatesApi
app.post "/project/:Project_id/template/publish", SecurityManager.requestIsOwner, TemplatesController.publishProject
app.post "/project/:Project_id/template/unpublish", SecurityManager.requestIsOwner, TemplatesController.unpublishProject
app.post "/project/:Project_id/template/description", SecurityManager.requestCanModifyProject, TemplatesController.updateProjectDescription
app.get "/project/:Project_id/template", SecurityManager.requestCanAccessProject, TemplatesController.getTemplateDetails

View file

@ -269,19 +269,19 @@ module.exports = class Router
AuthorizationManager.ensureClientCanAdminProject client, (error, project_id) => AuthorizationManager.ensureClientCanAdminProject client, (error, project_id) =>
dropboxHandler.getUserRegistrationStatus owner_id, callback dropboxHandler.getUserRegistrationStatus owner_id, callback
client.on 'publishProjectAsTemplate', (user_id, callback)-> # client.on 'publishProjectAsTemplate', (user_id, callback)->
AuthorizationManager.ensureClientCanAdminProject client, (error, project_id) => # AuthorizationManager.ensureClientCanAdminProject client, (error, project_id) =>
TemplatesController.publishProject user_id, project_id, callback # TemplatesController.publishProject user_id, project_id, callback
#
client.on 'unPublishProjectAsTemplate', (user_id, callback)-> # client.on 'unPublishProjectAsTemplate', (user_id, callback)->
AuthorizationManager.ensureClientCanAdminProject client, (error, project_id) => # AuthorizationManager.ensureClientCanAdminProject client, (error, project_id) =>
TemplatesController.unPublishProject user_id, project_id, callback # TemplatesController.unPublishProject user_id, project_id, callback
#
client.on 'updateProjectDescription', (description, callback)-> # client.on 'updateProjectDescription', (description, callback)->
AuthorizationManager.ensureClientCanEditProject client, (error, project_id) => # AuthorizationManager.ensureClientCanEditProject client, (error, project_id) =>
EditorController.updateProjectDescription project_id, description, callback # EditorController.updateProjectDescription project_id, description, callback
#
client.on "getPublishedDetails", (user_id, callback)-> # client.on "getPublishedDetails", (user_id, callback)->
AuthorizationManager.ensureClientCanViewProject client, (error, project_id) => # AuthorizationManager.ensureClientCanViewProject client, (error, project_id) =>
TemplatesController.getTemplateDetails user_id, project_id, callback # TemplatesController.getTemplateDetails user_id, project_id, callback

View file

@ -17,7 +17,7 @@ define [
} }
modal.result.then(resetState, resetState) modal.result.then(resetState, resetState)
App.controller "PublishProjectAsTemplateModalController", ($scope, $modalInstance, ide) -> App.controller "PublishProjectAsTemplateModalController", ($scope, $modalInstance, ide, $http) ->
user_id = ide.$scope.user.id user_id = ide.$scope.user.id
$scope.templateDetails = {exists:false} $scope.templateDetails = {exists:false}
@ -29,11 +29,13 @@ define [
$scope.problemTalkingToTemplateApi = true $scope.problemTalkingToTemplateApi = true
refreshPublishedStatus = -> refreshPublishedStatus = ->
ide.socket.emit "getPublishedDetails", user_id, (err, data)-> $http.get("/project/#{ide.project_id}/template")
if !data? or err? then return problemTalkingToTemplateApi() .success (data) ->
$scope.templateDetails = data $scope.templateDetails = data
$scope.templateDetails.publishedDate = moment(data.publishedDate).format("Do MMM YYYY, h:mm a") $scope.templateDetails.publishedDate = moment(data.publishedDate).format("Do MMM YYYY, h:mm a")
$scope.templateDetails.description = data.description $scope.templateDetails.description = data.description
.error () ->
problemTalkingToTemplateApi()
refreshPublishedStatus() refreshPublishedStatus()
$scope.$watch $scope.problemTalkingToTemplateApi, refreshPublishedStatus $scope.$watch $scope.problemTalkingToTemplateApi, refreshPublishedStatus
@ -41,22 +43,37 @@ define [
$scope.updateProjectDescription = -> $scope.updateProjectDescription = ->
description = $scope.templateDetails.description description = $scope.templateDetails.description
if description? if description?
ide.socket.emit 'updateProjectDescription', description, (err) => $http
if err? then return problemTalkingToTemplateApi() .post("/project/#{ide.project_id}/template/description", {
description: description
_csrf: window.csrfToken
})
.error () ->
problemTalkingToTemplateApi()
$scope.publishTemplate = -> $scope.publishTemplate = ->
$scope.state.publishInflight = true $scope.state.publishInflight = true
ide.socket.emit 'publishProjectAsTemplate', user_id, (error) => $http
if err? then return problemTalkingToTemplateApi() .post("/project/#{ide.project_id}/template/publish", {
refreshPublishedStatus() _csrf: window.csrfToken
$scope.state.publishInflight = false })
.success () ->
refreshPublishedStatus()
$scope.state.publishInflight = false
.error () ->
problemTalkingToTemplateApi()
$scope.unpublishTemplate = -> $scope.unpublishTemplate = ->
$scope.state.unpublishInflight = true $scope.state.unpublishInflight = true
ide.socket.emit 'unPublishProjectAsTemplate', user_id, (error) => $http
if err? then return problemTalkingToTemplateApi() .post("/project/#{ide.project_id}/template/unpublish", {
refreshPublishedStatus() _csrf: window.csrfToken
$scope.state.unpublishInflight = false })
.success () ->
refreshPublishedStatus()
$scope.state.unpublishInflight = false
.error () ->
problemTalkingToTemplateApi()
$scope.cancel = () -> $scope.cancel = () ->
$modalInstance.dismiss() $modalInstance.dismiss()

View file

@ -31,6 +31,8 @@ describe 'TemplatesController', ->
'../Uploads/ProjectUploadManager':@ProjectUploadManager '../Uploads/ProjectUploadManager':@ProjectUploadManager
'../Project/ProjectOptionsHandler':@ProjectOptionsHandler '../Project/ProjectOptionsHandler':@ProjectOptionsHandler
'../Project/ProjectDetailsHandler':@ProjectDetailsHandler '../Project/ProjectDetailsHandler':@ProjectDetailsHandler
'../Project/ProjectGetter':@ProjectGetter = {}
'../Editor/EditorController': @EditorController = {}
'./TemplatesPublisher':@TemplatesPublisher './TemplatesPublisher':@TemplatesPublisher
"logger-sharelatex": "logger-sharelatex":
log:-> log:->
@ -86,42 +88,57 @@ describe 'TemplatesController', ->
res = redirect:redirect res = redirect:redirect
@controller.createProjectFromZipTemplate @req, res @controller.createProjectFromZipTemplate @req, res
describe 'publishProject', (done)-> describe 'publishProject', ->
beforeEach -> beforeEach ->
@user_id = "sdijdlsakjlkajdklaj" @user_id = "user-id-123"
@project_id = "23213kl2j13lk1" @project_id = "project-id-123"
@res =
send: sinon.stub()
@req.params =
Project_id: @project_id
@ProjectGetter.getProject = sinon.stub().callsArgWith(2, null, {owner_ref: @user_id})
@TemplatesPublisher.publish = sinon.stub().callsArgWith(2)
@controller.publishProject @req, @res
it "should look up the project owner", ->
@ProjectGetter.getProject
.calledWith(@project_id, { owner_ref: 1 })
.should.equal true
it "should publish the template", ->
@TemplatesPublisher.publish
.calledWith(@user_id, @project_id)
.should.equal true
it "should return a success status", ->
@res.send.calledWith(204).should.equal true
it 'should pass the user id and project id to the handler', (done)-> describe 'unpublishProject', ->
@TemplatesPublisher.publish.callsArgWith(2)
@controller.publishProject @user_id, @project_id, =>
@TemplatesPublisher.publish.calledWith(@user_id, @project_id).should.equal true
done()
it 'should pass the error back', (done)->
error = "error"
@TemplatesPublisher.publish.callsArgWith(2, error)
@controller.publishProject @user_id, @project_id, (passedError)=>
passedError.should.equal error
done()
describe 'unpublish Project', (done)->
beforeEach -> beforeEach ->
@user_id = "sdijdlsakjlkajdklaj" @user_id = "user-id-123"
@project_id = "23213kl2j13lk1" @project_id = "project-id-123"
@res =
it 'should pass the user id and project id to the handler', (done)-> send: sinon.stub()
@TemplatesPublisher.unpublish.callsArgWith(2) @req.params =
@controller.unPublishProject @user_id, @project_id, => Project_id: @project_id
@TemplatesPublisher.unpublish.calledWith(@user_id, @project_id).should.equal true
done() @ProjectGetter.getProject = sinon.stub().callsArgWith(2, null, {owner_ref: @user_id})
@TemplatesPublisher.unpublish = sinon.stub().callsArgWith(2)
it 'should pass the error back', (done)-> @controller.unpublishProject @req, @res
error = "error"
@TemplatesPublisher.unpublish.callsArgWith(2, error) it "should look up the project owner", ->
@controller.unPublishProject @user_id, @project_id, (passedError)=> @ProjectGetter.getProject
passedError.should.equal error .calledWith(@project_id, { owner_ref: 1 })
done() .should.equal true
it "should publish the template", ->
@TemplatesPublisher.unpublish
.calledWith(@user_id, @project_id)
.should.equal true
it "should return a success status", ->
@res.send.calledWith(204).should.equal true
describe 'settings the compiler from the query string', -> describe 'settings the compiler from the query string', ->
it 'should use the said compiler', (done)-> it 'should use the said compiler', (done)->
@ -139,29 +156,51 @@ describe 'TemplatesController', ->
res = redirect:redirect res = redirect:redirect
@controller.createProjectFromZipTemplate @req, res @controller.createProjectFromZipTemplate @req, res
describe "updateProjectDescription", ->
beforeEach ->
@EditorController.updateProjectDescription = sinon.stub().callsArg(2)
@res =
send: sinon.stub()
@req.params =
Project_id: @project_id = "project-id-123"
@req.body =
description: @description = "test description"
@controller.updateProjectDescription @req, @res
it "should update the project description", ->
@EditorController.updateProjectDescription
.calledWith(@project_id, @description)
.should.equal true
it "should return a success code", ->
@res.send.calledWith(204).should.equal true
describe 'getTemplateDetails', -> describe 'getTemplateDetails', ->
beforeEach -> beforeEach ->
@description = "this project is nice" @user_id = "user-id-123"
@ProjectDetailsHandler.getProjectDescription.callsArgWith(1, null, @description) @project_id = "project-id-123"
@res =
json: sinon.stub()
@req.params =
Project_id: @project_id
@ProjectGetter.getProject = sinon.stub().callsArgWith(2, null, {owner_ref: @user_id})
@TemplatesPublisher.getTemplateDetails.callsArgWith(2, null, @details = {exists: true})
@ProjectDetailsHandler.getProjectDescription.callsArgWith(1, null, @description = "test description")
@controller.getTemplateDetails @req, @res
it "should return an error the templatePublisher", (done)-> it "should get the template details for the user_id and project_id", ->
error = "error" @TemplatesPublisher.getTemplateDetails
@TemplatesPublisher.getTemplateDetails.callsArgWith(2, error) .calledWith(@user_id, @project_id)
@controller.getTemplateDetails @user_id, @project_id, (passedError)=> .should.equal true
passedError.should.equal error
done() it "should return the details and description", ->
@res.json
it "should return the details", (done)-> .calledWith({
details = {exists:true} exists: @details.exists
@TemplatesPublisher.getTemplateDetails.callsArgWith(2, null, details) description: @description
@controller.getTemplateDetails @user_id, @project_id, (err, passedDetails)=> })
details.should.equal passedDetails .should.equal true
done()
it "should get the template description", (done)->
@TemplatesPublisher.getTemplateDetails.callsArgWith(2, null, {})
@controller.getTemplateDetails @user_id, @project_id, (err, passedDetails)=>
passedDetails.description.should.equal @description
done()