mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-07 20:31:06 -05:00
Merge pull request #10 from sharelatex/hof-history-apis
Add User and Project endpoints by overleaf ids
This commit is contained in:
commit
a9b18e4435
4 changed files with 78 additions and 22 deletions
|
@ -9,7 +9,7 @@ Errors = require("../Errors/Errors")
|
|||
|
||||
module.exports = ProjectDetailsHandler =
|
||||
getDetails: (project_id, callback)->
|
||||
ProjectGetter.getProject project_id, {name:true, description:true, compiler:true, features:true, owner_ref:true}, (err, project)->
|
||||
ProjectGetter.getProject project_id, {name:true, description:true, compiler:true, features:true, owner_ref:true, overleaf:true}, (err, project)->
|
||||
if err?
|
||||
logger.err err:err, project_id:project_id, "error getting project"
|
||||
return callback(err)
|
||||
|
@ -21,7 +21,11 @@ module.exports = ProjectDetailsHandler =
|
|||
description: project.description
|
||||
compiler: project.compiler
|
||||
features: user.features
|
||||
logger.log project_id:project_id, details:details, "getting project details"
|
||||
|
||||
if project.overleaf?
|
||||
details.overleaf = project.overleaf
|
||||
|
||||
logger.log project_id:project_id, details: details, "getting project details"
|
||||
callback(err, details)
|
||||
|
||||
getProjectDescription: (project_id, callback)->
|
||||
|
@ -66,4 +70,4 @@ module.exports = ProjectDetailsHandler =
|
|||
logger.log project_id: project_id, level: newAccessLevel, "set public access level"
|
||||
if project_id? && newAccessLevel? and _.include [PublicAccessLevels.READ_ONLY, PublicAccessLevels.READ_AND_WRITE, PublicAccessLevels.PRIVATE], newAccessLevel
|
||||
Project.update {_id:project_id},{publicAccesLevel:newAccessLevel}, (err)->
|
||||
callback()
|
||||
callback()
|
||||
|
|
|
@ -4,6 +4,7 @@ UserDeleter = require("./UserDeleter")
|
|||
UserUpdater = require("./UserUpdater")
|
||||
sanitize = require('sanitizer')
|
||||
AuthenticationController = require('../Authentication/AuthenticationController')
|
||||
ObjectId = require("mongojs").ObjectId
|
||||
|
||||
module.exports = UserController =
|
||||
getLoggedInUsersPersonalInfo: (req, res, next = (error) ->) ->
|
||||
|
@ -19,8 +20,17 @@ module.exports = UserController =
|
|||
UserController.sendFormattedPersonalInfo(user, res, next)
|
||||
|
||||
getPersonalInfo: (req, res, next = (error) ->) ->
|
||||
UserGetter.getUser req.params.user_id, { _id: true, first_name: true, last_name: true, email: true}, (error, user) ->
|
||||
logger.log user_id: req.params.user_id, "reciving request for getting users personal info"
|
||||
{user_id} = req.params
|
||||
|
||||
if user_id.match(/^\d+$/)
|
||||
query = { "overleaf.id": parseInt(user_id, 10) }
|
||||
else if user_id.match(/^[a-f0-9]{24}$/)
|
||||
query = { _id: ObjectId(user_id) }
|
||||
else
|
||||
return res.send(400)
|
||||
|
||||
UserGetter.getUser query, { _id: true, first_name: true, last_name: true, email: true}, (error, user) ->
|
||||
logger.log user_id: req.params.user_id, "receiving request for getting users personal info"
|
||||
return next(error) if error?
|
||||
return res.send(404) if !user?
|
||||
UserController.sendFormattedPersonalInfo(user, res, next)
|
||||
|
|
|
@ -12,7 +12,7 @@ describe 'ProjectDetailsHandler', ->
|
|||
beforeEach ->
|
||||
@project_id = "321l3j1kjkjl"
|
||||
@user_id = "user-id-123"
|
||||
@project =
|
||||
@project =
|
||||
name: "project"
|
||||
description: "this is a great project"
|
||||
something:"should not exist"
|
||||
|
@ -20,7 +20,7 @@ describe 'ProjectDetailsHandler', ->
|
|||
owner_ref: @user_id
|
||||
@user =
|
||||
features: "mock-features"
|
||||
@ProjectGetter =
|
||||
@ProjectGetter =
|
||||
getProjectWithoutDocLines: sinon.stub().callsArgWith(1, null, @project)
|
||||
getProject: sinon.stub().callsArgWith(2, null, @project)
|
||||
@ProjectModel =
|
||||
|
@ -42,7 +42,7 @@ describe 'ProjectDetailsHandler', ->
|
|||
describe "getDetails", ->
|
||||
|
||||
it "should find the project and owner", (done)->
|
||||
@handler.getDetails @project_id, (err, details)=>
|
||||
@handler.getDetails @project_id, (err, details)=>
|
||||
details.name.should.equal @project.name
|
||||
details.description.should.equal @project.description
|
||||
details.compiler.should.equal @project.compiler
|
||||
|
@ -50,6 +50,13 @@ describe 'ProjectDetailsHandler', ->
|
|||
assert.equal(details.something, undefined)
|
||||
done()
|
||||
|
||||
it "should find overleaf metadata if it exists", (done)->
|
||||
@project.overleaf = { id: 'id' }
|
||||
@handler.getDetails @project_id, (err, details)=>
|
||||
details.overleaf.should.equal @project.overleaf
|
||||
assert.equal(details.something, undefined)
|
||||
done()
|
||||
|
||||
it "should return an error for a non-existent project", (done)->
|
||||
@ProjectGetter.getProject.callsArg(2, null, null)
|
||||
err = new Errors.NotFoundError("project not found")
|
||||
|
@ -79,7 +86,7 @@ describe 'ProjectDetailsHandler', ->
|
|||
@handler.getProjectDescription @project_id, (returnedErr, returnedDescription)=>
|
||||
err.should.equal returnedErr
|
||||
description.should.equal returnedDescription
|
||||
done()
|
||||
done()
|
||||
|
||||
describe "setProjectDescription", ->
|
||||
|
||||
|
@ -110,7 +117,7 @@ describe 'ProjectDetailsHandler', ->
|
|||
@handler.renameProject @project_id, @newName, =>
|
||||
@tpdsUpdateSender.moveEntity.calledWith({project_id:@project_id, project_name:@project.name, newProjectName:@newName}).should.equal true
|
||||
done()
|
||||
|
||||
|
||||
it "should not do anything with an invalid name", (done) ->
|
||||
@handler.validateProjectName = sinon.stub().yields(new Error("invalid name"))
|
||||
@handler.renameProject @project_id, @newName, =>
|
||||
|
|
|
@ -12,9 +12,9 @@ ObjectId = require("mongojs").ObjectId
|
|||
|
||||
describe "UserInfoController", ->
|
||||
beforeEach ->
|
||||
@UserDeleter =
|
||||
@UserDeleter =
|
||||
deleteUser: sinon.stub().callsArgWith(1)
|
||||
@UserUpdater =
|
||||
@UserUpdater =
|
||||
updatePersonalInfo: sinon.stub()
|
||||
@sanitizer = escape:(v)->v
|
||||
sinon.spy @sanitizer, "escape"
|
||||
|
@ -50,23 +50,47 @@ describe "UserInfoController", ->
|
|||
.should.equal true
|
||||
|
||||
describe "getPersonalInfo", ->
|
||||
beforeEach ->
|
||||
@user_id = ObjectId().toString()
|
||||
@user =
|
||||
_id: ObjectId(@user_id)
|
||||
@req.params = user_id: @user_id
|
||||
|
||||
describe "when the user exists", ->
|
||||
describe "when the user exists with sharelatex id", ->
|
||||
beforeEach ->
|
||||
@user_id = ObjectId().toString()
|
||||
@user =
|
||||
_id: ObjectId(@user_id)
|
||||
@req.params = user_id: @user_id
|
||||
@UserGetter.getUser = sinon.stub().callsArgWith(2, null, @user)
|
||||
@UserInfoController.sendFormattedPersonalInfo = sinon.stub()
|
||||
@UserInfoController.getPersonalInfo(@req, @res, @next)
|
||||
|
||||
it "should look up the user in the database", ->
|
||||
@UserGetter.getUser
|
||||
.calledWith(@user_id, { _id: true, first_name: true, last_name: true, email: true })
|
||||
.calledWith(
|
||||
{ _id: ObjectId(@user_id) },
|
||||
{ _id: true, first_name: true, last_name: true, email: true }
|
||||
).should.equal true
|
||||
|
||||
it "should send the formatted details back to the client", ->
|
||||
@UserInfoController.sendFormattedPersonalInfo
|
||||
.calledWith(@user, @res, @next)
|
||||
.should.equal true
|
||||
|
||||
|
||||
describe "when the user exists with overleaf id", ->
|
||||
beforeEach ->
|
||||
@user_id = 12345
|
||||
@user =
|
||||
_id: ObjectId()
|
||||
overleaf:
|
||||
id: @user_id
|
||||
@req.params = user_id: @user_id.toString()
|
||||
@UserGetter.getUser = sinon.stub().callsArgWith(2, null, @user)
|
||||
@UserInfoController.sendFormattedPersonalInfo = sinon.stub()
|
||||
@UserInfoController.getPersonalInfo(@req, @res, @next)
|
||||
|
||||
it "should look up the user in the database", ->
|
||||
@UserGetter.getUser
|
||||
.calledWith(
|
||||
{ "overleaf.id": @user_id },
|
||||
{ _id: true, first_name: true, last_name: true, email: true }
|
||||
).should.equal true
|
||||
|
||||
it "should send the formatted details back to the client", ->
|
||||
@UserInfoController.sendFormattedPersonalInfo
|
||||
.calledWith(@user, @res, @next)
|
||||
|
@ -74,13 +98,24 @@ describe "UserInfoController", ->
|
|||
|
||||
describe "when the user does not exist", ->
|
||||
beforeEach ->
|
||||
@user_id = ObjectId().toString()
|
||||
@req.params = user_id: @user_id
|
||||
@UserGetter.getUser = sinon.stub().callsArgWith(2, null, null)
|
||||
@UserInfoController.sendFormattedPersonalInfo = sinon.stub()
|
||||
@UserInfoController.getPersonalInfo(@req, @res, @next)
|
||||
|
||||
it "should return 404 to the client", ->
|
||||
@res.statusCode.should.equal 404
|
||||
|
||||
describe "when the user id is invalid", ->
|
||||
beforeEach ->
|
||||
@user_id = "invalid"
|
||||
@req.params = user_id: @user_id
|
||||
@UserGetter.getUser = sinon.stub().callsArgWith(2, null, null)
|
||||
@UserInfoController.getPersonalInfo(@req, @res, @next)
|
||||
|
||||
it "should return 400 to the client", ->
|
||||
@res.statusCode.should.equal 400
|
||||
|
||||
describe "sendFormattedPersonalInfo", ->
|
||||
beforeEach ->
|
||||
@user =
|
||||
|
|
Loading…
Reference in a new issue