use v1 doc info api

This commit is contained in:
Ersun Warncke 2018-10-02 11:16:46 -04:00
parent 052cbda507
commit 642b45d0d6
5 changed files with 72 additions and 70 deletions

View file

@ -1,7 +1,6 @@
ProjectController = require "../Project/ProjectController"
AuthenticationController = require '../Authentication/AuthenticationController'
TokenAccessHandler = require './TokenAccessHandler'
V1Api = require '../V1/V1Api'
Errors = require '../Errors/Errors'
logger = require 'logger-sharelatex'
settings = require 'settings-sharelatex'
@ -38,9 +37,9 @@ module.exports = TokenAccessController =
if !projectExists and settings.overleaf
logger.log {token, userId},
"[TokenAccess] no project found for this token"
TokenAccessHandler.checkV1ProjectExported token, (err, exported) ->
TokenAccessHandler.getV1DocInfo token, (err, doc_info) ->
return next err if err?
return next(new Errors.NotFoundError()) if exported
return next(new Errors.NotFoundError()) if doc_info.exported
return res.redirect(302, "/sign_in_to_v1?return_to=/#{token}")
else if !project?
logger.log {token, userId},
@ -80,30 +79,28 @@ module.exports = TokenAccessController =
userId = AuthenticationController.getLoggedInUserId(req)
token = req.params['read_only_token']
logger.log {userId, token}, "[TokenAccess] requesting read-only token access"
TokenAccessHandler.findProjectWithReadOnlyToken token, (err, project, projectExists) ->
if err?
logger.err {err, token, userId},
"[TokenAccess] error getting project by readOnly token"
return next(err)
if !projectExists and settings.overleaf
logger.log {token, userId},
"[TokenAccess] no project found for this token"
TokenAccessHandler.checkV1ProjectExported token, (err, exported) ->
return next err if err?
return next(new Errors.NotFoundError()) if exported
TokenAccessHandler.getV1DocInfo token, (err, doc_info) ->
return res.redirect doc_info.published_path if doc_info.allow == false
TokenAccessHandler.findProjectWithReadOnlyToken token, (err, project, projectExists) ->
if err?
logger.err {err, token, userId},
"[TokenAccess] error getting project by readOnly token"
return next(err)
if !projectExists and settings.overleaf
logger.log {token, userId},
"[TokenAccess] no project found for this token"
return next(new Errors.NotFoundError()) if doc_info.exported
return res.redirect(302, "/sign_in_to_v1?return_to=/read/#{token}")
else if !project?
logger.log {token, userId},
"[TokenAccess] no project found for readOnly token"
if !userId?
logger.log {token},
"[TokenAccess] No project found with readOnly token, anonymous user, deny"
return next(new Errors.NotFoundError())
TokenAccessController._tryHigherAccess(token, userId, req, res, next)
else
TokenAccessHandler.checkV1Access token, (err, allow_access, redirect_path) ->
return next err if err?
return res.redirect redirect_path unless allow_access
else if !project?
logger.log {token, userId},
"[TokenAccess] no project found for readOnly token"
if !userId?
logger.log {token},
"[TokenAccess] No project found with readOnly token, anonymous user, deny"
return next(new Errors.NotFoundError())
TokenAccessController._tryHigherAccess(token, userId, req, res, next)
else
if !userId?
logger.log {userId, projectId: project._id},
"[TokenAccess] adding anonymous user to project with readOnly token"
@ -123,4 +120,3 @@ module.exports = TokenAccessController =
"[TokenAccess] error adding user to project with readAndWrite token"
return next(err)
return TokenAccessController._loadEditor(project._id, req, res, next)

View file

@ -110,15 +110,14 @@ module.exports = TokenAccessHandler =
if privilegeLevel != PrivilegeLevels.READ_ONLY
project.tokens.readOnly = ''
checkV1Access: (token, callback=(err, allow, redirect)->) ->
return callback(null, true) unless Settings.apis?.v1?
V1Api.request { url: "/api/v1/sharelatex/docs/#{token}/is_published" }, (err, response, body) ->
return callback err if err?
callback null, false, body.published_path if body.allow == false
callback null, true
getV1DocInfo: (token, callback=(err, info)->) ->
# default to allowing access and not exported
return callback(null, {
allow: true
exists: true
exported: false
}) unless Settings.apis?.v1?
checkV1ProjectExported: (token, callback = (err, exists) ->) ->
return callback(null, false) unless Settings.apis?.v1?
V1Api.request { url: "/api/v1/sharelatex/docs/#{token}/exported_to_v2" }, (err, response, body) ->
V1Api.request { url: "/api/v1/sharelatex/docs/#{token}/info" }, (err, response, body) ->
return callback err if err?
callback null, body.exported
callback null, body

View file

@ -82,10 +82,7 @@ module.exports = MockV1Api =
console.error "error starting MockV1Api:", error.message
process.exit(1)
app.get '/api/v1/sharelatex/docs/:token/is_published', (req, res, next) =>
res.json { allow: true }
app.get '/api/v1/sharelatex/docs/:token/exported_to_v2', (req, res, next) =>
res.json { exported: false }
app.get '/api/v1/sharelatex/docs/:token/info', (req, res, next) =>
res.json { allow: true, exported: false }
MockV1Api.run()

View file

@ -28,7 +28,13 @@ describe "TokenAccessController", ->
@TokenAccessController = SandboxedModule.require modulePath, requires:
'../Project/ProjectController': @ProjectController = {}
'../Authentication/AuthenticationController': @AuthenticationController = {}
'./TokenAccessHandler': @TokenAccessHandler = {}
'./TokenAccessHandler': @TokenAccessHandler = {
getV1DocInfo: sinon.stub().yields(null, {
allow: true
exists: true
exported: false
})
}
'logger-sharelatex': {log: sinon.stub(), err: sinon.stub()}
'settings-sharelatex': {
overleaf:
@ -420,7 +426,12 @@ describe "TokenAccessController", ->
@next = sinon.stub()
@TokenAccessHandler.findProjectWithReadOnlyToken = sinon.stub()
.callsArgWith(1, null, @project, true)
@TokenAccessHandler.checkV1Access = sinon.stub().callsArgWith(1, null, false, 'doc-url')
@TokenAccessHandler.getV1DocInfo = sinon.stub().yields(null, {
allow: false
exists: true
exported: false
published_path: 'doc-url'
})
@TokenAccessController.readOnlyToken @req, @res, @next
it 'should redirect to doc-url', ->
@ -563,8 +574,11 @@ describe "TokenAccessController", ->
@req.params['read_only_token'] = 'abcd'
@TokenAccessHandler.findProjectWithReadOnlyToken = sinon.stub()
.callsArgWith(1, null, null, false)
@TokenAccessHandler.checkV1ProjectExported = sinon.stub()
.callsArgWith(1, null, true)
@TokenAccessHandler.getV1DocInfo = sinon.stub().yields(null, {
allow: true
exists: true
exported: true
})
@TokenAccessController.readOnlyToken @req, @res, @next
it 'should call next with a not-found error', (done) ->
@ -830,8 +844,11 @@ describe "TokenAccessController", ->
describe 'when project was exported to v2', ->
beforeEach ->
@TokenAccessHandler.checkV1ProjectExported = sinon.stub()
.callsArgWith(1, null, true)
@TokenAccessHandler.getV1DocInfo = sinon.stub().yields(null, {
allow: true
exists: true
exported: true
})
@TokenAccessController.readOnlyToken @req, @res, @next
it 'should redirect to v1', (done) ->

View file

@ -472,7 +472,6 @@ describe "TokenAccessHandler", ->
expect(ro).to.equal false
done()
describe 'protectTokens', ->
beforeEach ->
@project = {tokens: {readAndWrite: 'rw', readOnly: 'ro'}}
@ -492,45 +491,39 @@ describe "TokenAccessHandler", ->
expect(@project.tokens.readAndWrite).to.equal 'rw'
expect(@project.tokens.readOnly).to.equal 'ro'
describe 'checkV1Access', ->
describe 'getV1DocInfo', ->
beforeEach ->
@callback = sinon.stub()
describe 'when v1 api not set', ->
beforeEach ->
@TokenAccessHandler.checkV1Access @token, @callback
@TokenAccessHandler.getV1DocInfo @token, @callback
it 'should not check access and return true', ->
it 'should not check access and return default info', ->
expect(@V1Api.request.called).to.equal false
expect(@callback.calledWith null, true).to.equal true
expect(@callback.calledWith null, {
allow: true
exists: true
exported: false
}).to.equal true
describe 'when v1 api is set', ->
beforeEach ->
@settings.apis = { v1: 'v1' }
describe 'when access allowed', ->
describe 'on success', ->
beforeEach ->
@V1Api.request = sinon.stub().callsArgWith(1, null, {}, { allow: true} )
@TokenAccessHandler.checkV1Access @token, @callback
@V1Api.request = sinon.stub().callsArgWith(1, null, null, 'mock-data')
@TokenAccessHandler.getV1DocInfo @token, @callback
it 'should check api', ->
expect(@V1Api.request.calledWith { url: "/api/v1/sharelatex/docs/#{@token}/is_published" }).to.equal true
it 'should callback with true', ->
expect(@callback.calledWith null, true).to.equal true
describe 'when access denied', ->
beforeEach ->
@V1Api.request = sinon.stub().callsArgWith(1, null, {}, { allow: false, published_path: 'doc-url'} )
@TokenAccessHandler.checkV1Access @token, @callback
it 'should callback with false and redirect', ->
expect(@callback.calledWith null, false, 'doc-url').to.equal true
it 'should return response body', ->
expect(@V1Api.request.calledWith { url: "/api/v1/sharelatex/docs/#{@token}/info" }).to.equal true
expect(@callback.calledWith null, 'mock-data').to.equal true
describe 'on error', ->
beforeEach ->
@V1Api.request = sinon.stub().callsArgWith(1, 'error')
@TokenAccessHandler.checkV1Access @token, @callback
@TokenAccessHandler.getV1DocInfo @token, @callback
it 'should callback with error', ->
expect(@callback.calledWith 'error').to.equal true