Dynamically monkey patch impl to check integration once instead of each request

This commit is contained in:
Alasdair Smith 2017-11-03 10:16:03 +00:00
parent 3bc48a7a05
commit e32406a4c4
2 changed files with 30 additions and 26 deletions

View file

@ -4,19 +4,18 @@ logger = require 'logger-sharelatex'
INTEGRATION_MODULE_PATH = path.resolve(__dirname, '../../../../modules/overleaf-integration-web-module')
module.exports = V1ProjectGetter =
integrationModuleExists: (callback = (error, stats) ->) ->
fs.stat INTEGRATION_MODULE_PATH, (error, stats) ->
if error? or !stats.isDirectory()
return callback(false)
return callback(true)
V1ProjectGetter =
# Default implementation is a no-op
findAllUsersProjects: (userId, callback = (error, projects) ->) ->
V1ProjectGetter.integrationModuleExists (exists) ->
if exists
logger.log {exists}, "integration module does exist, loading V1 projects"
V1ProjectListGetter = require(path.join(INTEGRATION_MODULE_PATH, 'app/coffee/ProjectList/ProjectListGetter'))
V1ProjectListGetter.findAllUsersProjects(userId, callback)
else
logger.log {exists}, "integration modules doesn't exists, not loading V1 projects"
return callback()
logger.log {}, "integration modules doesn't exist, not loading V1 projects"
return callback()
fs.stat INTEGRATION_MODULE_PATH, (error, stats) ->
return if error? or !stats.isDirectory()
logger.log {isDirectory: stats.isDirectory}, "integration module does exist, loading V1 projects"
# Monkey patch impl to actually fetch projects
V1ProjectGetter.findAllUsersProjects = (userId, callback = (error, projects) ->) ->
IntegrationProjectListGetter = require(path.join(INTEGRATION_MODULE_PATH, 'app/coffee/ProjectList/ProjectListGetter'))
IntegrationProjectListGetter.findAllUsersProjects(userId, callback)
module.exports = V1ProjectGetter

View file

@ -6,25 +6,24 @@ modulePath = '../../../../app/js/Features/Project/V1ProjectGetter.js'
describe 'V1ProjectGetter', ->
beforeEach ->
@fs =
stat: sinon.stub()
@path =
resolve: sinon.stub().returns('path/to/integration/module')
join: sinon.stub().returns('path/to/file/in/integration/module')
@IntegrationProjectListGetter =
findAllUsersProjects: sinon.stub()
@V1ProjectGetter = SandboxedModule.require modulePath, requires:
'fs': @fs
'path': @path
'logger-sharelatex': log: ->
'path/to/file/in/integration/module': @IntegrationProjectListGetter
@userId = 123
@callback = sinon.stub()
describe 'without overleaf-integration-web-module', ->
beforeEach ->
# Mock not finding integration module
@fs.stat.yields({ code: 'mock-ENOENT-error' })
@fs =
stat: sinon.stub().yields({code: 'mock-ENOENT-error'})
@V1ProjectGetter = SandboxedModule.require modulePath, requires:
# Mock not finding integration module
'fs': @fs
'path': @path
'logger-sharelatex': log: ->
'path/to/file/in/integration/module': @IntegrationProjectListGetter
# Call method
@V1ProjectGetter.findAllUsersProjects @userId, @callback
@ -33,8 +32,14 @@ describe 'V1ProjectGetter', ->
describe 'with overleaf-integration-web-module', ->
beforeEach ->
# Mock finding integration module
@fs.stat.yields(null, isDirectory: sinon.stub().returns(true))
@fs =
stat: sinon.stub().yields(null, isDirectory: sinon.stub().returns(true))
@V1ProjectGetter = SandboxedModule.require modulePath, requires:
# Mock finding integration module
'fs': @fs
'path': @path
'logger-sharelatex': log: ->
'path/to/file/in/integration/module': @IntegrationProjectListGetter
# Mock integration module response
@IntegrationProjectListGetter.findAllUsersProjects.yields(null, @response = {
projects: [{ id: '123mockV1Id', title: 'mock title' }]