mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-07 20:31:06 -05:00
Add unit test for ProjectController.projectEntitiesJson
This commit is contained in:
parent
b1c1cdecef
commit
4daf062be9
2 changed files with 60 additions and 13 deletions
|
@ -154,21 +154,20 @@ module.exports = ProjectController =
|
||||||
projectEntitiesJson: (req, res, next) ->
|
projectEntitiesJson: (req, res, next) ->
|
||||||
user_id = AuthenticationController.getLoggedInUserId(req)
|
user_id = AuthenticationController.getLoggedInUserId(req)
|
||||||
project_id = req.params.Project_id
|
project_id = req.params.Project_id
|
||||||
AuthorizationManager.canUserReadProject user_id, project_id,
|
AuthorizationManager.canUserReadProject user_id, project_id, null, (err, canRead) ->
|
||||||
null, (err, canRead) ->
|
return next(err) if err?
|
||||||
|
return res.sendStatus(403) if !canRead
|
||||||
|
ProjectGetter.getProject project_id, (err, project) ->
|
||||||
return next(err) if err?
|
return next(err) if err?
|
||||||
return res.status(403) if !canRead
|
ProjectEntityHandler.getAllEntitiesFromProject project, (err, docs, files) ->
|
||||||
ProjectGetter.getProject project_id, (err, project) ->
|
|
||||||
return next(err) if err?
|
return next(err) if err?
|
||||||
ProjectEntityHandler.getAllEntitiesFromProject project, (err, docs, files) ->
|
entities = docs.concat(files)
|
||||||
return next(err) if err?
|
.sort (a, b) -> a.path > b.path # Sort by path ascending
|
||||||
entities = docs.concat(files)
|
.map (e) -> {
|
||||||
.sort (a, b) -> a.path > b.path # Sort by path ascending
|
path: e.path,
|
||||||
.map (e) -> {
|
type: if e.doc? then 'doc' else 'file'
|
||||||
path: e.path,
|
}
|
||||||
type: if e.doc? then 'doc' else 'file'
|
res.json({project_id: project_id, entities: entities})
|
||||||
}
|
|
||||||
res.json({project_id: project_id, entities: entities})
|
|
||||||
|
|
||||||
projectListPage: (req, res, next)->
|
projectListPage: (req, res, next)->
|
||||||
timer = new metrics.Timer("project-list")
|
timer = new metrics.Timer("project-list")
|
||||||
|
|
|
@ -547,6 +547,54 @@ describe "ProjectController", ->
|
||||||
done()
|
done()
|
||||||
@ProjectController.userProjectsJson @req, @res, @next
|
@ProjectController.userProjectsJson @req, @res, @next
|
||||||
|
|
||||||
|
describe 'projectEntitiesJson', ->
|
||||||
|
beforeEach () ->
|
||||||
|
@AuthenticationController.getLoggedInUserId = sinon.stub().returns 'abc'
|
||||||
|
@req.params = {Project_id: 'abcd'}
|
||||||
|
@project = { _id: 'abcd' }
|
||||||
|
@docs = [
|
||||||
|
{path: '/things/b.txt', doc: true},
|
||||||
|
{path: '/main.tex', doc: true}
|
||||||
|
]
|
||||||
|
@files = [
|
||||||
|
{path: '/things/a.txt'}
|
||||||
|
]
|
||||||
|
@ProjectGetter.getProject = sinon.stub().callsArgWith(1, null, @project)
|
||||||
|
@ProjectEntityHandler.getAllEntitiesFromProject = sinon.stub().callsArgWith(1, null, @docs, @files)
|
||||||
|
|
||||||
|
describe 'when the user can access the project', ->
|
||||||
|
beforeEach () ->
|
||||||
|
@AuthorizationManager.canUserReadProject = sinon.stub().callsArgWith(3, null, true)
|
||||||
|
|
||||||
|
it 'should produce a list of entities', (done) ->
|
||||||
|
@res.json = (data) =>
|
||||||
|
expect(data).to.deep.equal {
|
||||||
|
project_id: 'abcd',
|
||||||
|
entities: [
|
||||||
|
{path: '/main.tex', type: 'doc'},
|
||||||
|
{path: '/things/a.txt', type: 'file'},
|
||||||
|
{path: '/things/b.txt', type: 'doc'}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
expect(@ProjectGetter.getProject.callCount).to.equal 1
|
||||||
|
expect(@ProjectEntityHandler.getAllEntitiesFromProject.callCount).to.equal 1
|
||||||
|
done()
|
||||||
|
@ProjectController.projectEntitiesJson @req, @res, @next
|
||||||
|
|
||||||
|
describe 'when the user cannot access the project', ->
|
||||||
|
beforeEach () ->
|
||||||
|
@AuthorizationManager.canUserReadProject = sinon.stub().callsArgWith(3, null, false)
|
||||||
|
|
||||||
|
it 'should send a 403 response', (done) ->
|
||||||
|
@res.json = sinon.stub()
|
||||||
|
@res.sendStatus = (code) =>
|
||||||
|
expect(code).to.equal 403
|
||||||
|
expect(@ProjectGetter.getProject.callCount).to.equal 0
|
||||||
|
expect(@ProjectEntityHandler.getAllEntitiesFromProject.callCount).to.equal 0
|
||||||
|
expect(@res.json.callCount).to.equal 0
|
||||||
|
done()
|
||||||
|
@ProjectController.projectEntitiesJson @req, @res, @next
|
||||||
|
|
||||||
describe '_isInPercentageRollout', ->
|
describe '_isInPercentageRollout', ->
|
||||||
before ->
|
before ->
|
||||||
@ids = [
|
@ids = [
|
||||||
|
|
Loading…
Reference in a new issue