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,15 +38,38 @@ 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) ->
project_id = req.params.Project_id
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" logger.log user_id:user_id, project_id:project_id, "receiving request to publish project as template"
TemplatesPublisher.publish user_id, project_id, callback 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?
user_id = project.owner_ref.toString()
logger.log user_id:user_id, project_id:project_id, "receiving request to unpublish project"
TemplatesPublisher.unpublish user_id, project_id, (error) ->
return next(error) if error?
res.send 204
getTemplateDetails: (user_id, project_id, callback)-> updateProjectDescription: (req, res, next) ->
project_id = req.params.Project_id
{description} = req.body
EditorController.updateProjectDescription project_id, description, (error) ->
return next(error) if error?
res.send 204
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 { async.parallel {
details: (cb)-> details: (cb)->
TemplatesPublisher.getTemplateDetails user_id, project_id, cb TemplatesPublisher.getTemplateDetails user_id, project_id, cb
@ -53,11 +78,10 @@ module.exports =
}, (err, results)-> }, (err, results)->
if err? if err?
logger.err err:err, user_id:user_id, project_id:project_id, "something went wrong getting template details" logger.err err:err, user_id:user_id, project_id:project_id, "something went wrong getting template details"
return callback(err) return next(err)
details = results.details details = results.details
details.description = results.description details.description = results.description
callback(err, details) 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", {
_csrf: window.csrfToken
})
.success () ->
refreshPublishedStatus() refreshPublishedStatus()
$scope.state.publishInflight = false $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", {
_csrf: window.csrfToken
})
.success () ->
refreshPublishedStatus() refreshPublishedStatus()
$scope.state.unpublishInflight = false $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
it 'should pass the user id and project id to the handler', (done)-> @ProjectGetter.getProject = sinon.stub().callsArgWith(2, null, {owner_ref: @user_id})
@TemplatesPublisher.publish.callsArgWith(2) @TemplatesPublisher.publish = sinon.stub().callsArgWith(2)
@controller.publishProject @user_id, @project_id, => @controller.publishProject @req, @res
@TemplatesPublisher.publish.calledWith(@user_id, @project_id).should.equal true
done()
it 'should pass the error back', (done)-> it "should look up the project owner", ->
error = "error" @ProjectGetter.getProject
@TemplatesPublisher.publish.callsArgWith(2, error) .calledWith(@project_id, { owner_ref: 1 })
@controller.publishProject @user_id, @project_id, (passedError)=> .should.equal true
passedError.should.equal error
done()
describe 'unpublish Project', (done)-> 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
describe 'unpublishProject', ->
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
it 'should pass the user id and project id to the handler', (done)-> @ProjectGetter.getProject = sinon.stub().callsArgWith(2, null, {owner_ref: @user_id})
@TemplatesPublisher.unpublish.callsArgWith(2) @TemplatesPublisher.unpublish = sinon.stub().callsArgWith(2)
@controller.unPublishProject @user_id, @project_id, => @controller.unpublishProject @req, @res
@TemplatesPublisher.unpublish.calledWith(@user_id, @project_id).should.equal true
done()
it 'should pass the error back', (done)-> it "should look up the project owner", ->
error = "error" @ProjectGetter.getProject
@TemplatesPublisher.unpublish.callsArgWith(2, error) .calledWith(@project_id, { owner_ref: 1 })
@controller.unPublishProject @user_id, @project_id, (passedError)=> .should.equal true
passedError.should.equal error
done()
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
it "should return an error the templatePublisher", (done)-> @ProjectGetter.getProject = sinon.stub().callsArgWith(2, null, {owner_ref: @user_id})
error = "error" @TemplatesPublisher.getTemplateDetails.callsArgWith(2, null, @details = {exists: true})
@TemplatesPublisher.getTemplateDetails.callsArgWith(2, error) @ProjectDetailsHandler.getProjectDescription.callsArgWith(1, null, @description = "test description")
@controller.getTemplateDetails @user_id, @project_id, (passedError)=>
passedError.should.equal error
done()
it "should return the details", (done)-> @controller.getTemplateDetails @req, @res
details = {exists:true}
@TemplatesPublisher.getTemplateDetails.callsArgWith(2, null, details)
@controller.getTemplateDetails @user_id, @project_id, (err, passedDetails)=>
details.should.equal passedDetails
done()
it "should get the template description", (done)-> it "should get the template details for the user_id and project_id", ->
@TemplatesPublisher.getTemplateDetails.callsArgWith(2, null, {}) @TemplatesPublisher.getTemplateDetails
@controller.getTemplateDetails @user_id, @project_id, (err, passedDetails)=> .calledWith(@user_id, @project_id)
passedDetails.description.should.equal @description .should.equal true
done()
it "should return the details and description", ->
@res.json
.calledWith({
exists: @details.exists
description: @description
})
.should.equal true