refactor into InstitutionsAPI

This commit is contained in:
Tim Alby 2018-07-10 21:49:24 +02:00
parent 7dc82597e1
commit 63c7f756ce
12 changed files with 32 additions and 32 deletions

View file

@ -3,12 +3,12 @@ metrics = require("metrics-sharelatex")
settings = require "settings-sharelatex"
request = require "request"
module.exports = UserAffiliationsManager =
getAffiliations: (userId, callback = (error, body) ->) ->
module.exports = InstitutionsAPI =
getUserAffiliations: (userId, callback = (error, body) ->) ->
makeAffiliationRequest {
method: 'GET'
path: "/api/v2/users/#{userId.toString()}/affiliations"
defaultErrorMessage: "Couldn't get affiliations"
defaultErrorMessage: "Couldn't get user affiliations"
}, callback
@ -77,10 +77,10 @@ makeAffiliationRequest = (requestOptions, callback = (error) ->) ->
callback(null, body)
[
'getAffiliations',
'getUserAffiliations',
'addAffiliation',
'removeAffiliation',
].map (method) ->
metrics.timeAsyncMethod(
UserAffiliationsManager, method, 'mongo.UserAffiliationsManager', logger
InstitutionsAPI, method, 'mongo.InstitutionsAPI', logger
)

View file

@ -1,7 +1,7 @@
User = require("../../models/User").User
logger = require("logger-sharelatex")
metrics = require('metrics-sharelatex')
{ addAffiliation } = require("./UserAffiliationsManager")
{ addAffiliation } = require("../Institutions/InstitutionsAPI")
module.exports = UserCreator =

View file

@ -4,7 +4,7 @@ ProjectDeleter = require("../Project/ProjectDeleter")
logger = require("logger-sharelatex")
SubscriptionHandler = require("../Subscription/SubscriptionHandler")
async = require("async")
{ deleteAffiliations } = require("./UserAffiliationsManager")
{ deleteAffiliations } = require("../Institutions/InstitutionsAPI")
module.exports =

View file

@ -3,7 +3,7 @@ UserGetter = require("./UserGetter")
UserUpdater = require("./UserUpdater")
EmailHelper = require("../Helpers/EmailHelper")
UserEmailsConfirmationHandler = require "./UserEmailsConfirmationHandler"
{ endorseAffiliation } = require("./UserAffiliationsManager")
{ endorseAffiliation } = require("../Institutions/InstitutionsAPI")
logger = require("logger-sharelatex")
Errors = require "../Errors/Errors"

View file

@ -3,7 +3,7 @@ metrics = require('metrics-sharelatex')
logger = require('logger-sharelatex')
db = mongojs.db
ObjectId = mongojs.ObjectId
{ getAffiliations } = require("./UserAffiliationsManager")
{ getUserAffiliations } = require("../Institutions/InstitutionsAPI")
Errors = require("../Errors/Errors")
module.exports = UserGetter =
@ -32,7 +32,7 @@ module.exports = UserGetter =
return callback error if error?
return callback new Error('User not Found') unless user
getAffiliations userId, (error, affiliationsData) ->
getUserAffiliations userId, (error, affiliationsData) ->
return callback error if error?
callback null, decorateFullEmails(user.email, user.emails, affiliationsData)

View file

@ -5,7 +5,7 @@ db = mongojs.db
async = require("async")
ObjectId = mongojs.ObjectId
UserGetter = require("./UserGetter")
{ addAffiliation, removeAffiliation } = require("./UserAffiliationsManager")
{ addAffiliation, removeAffiliation } = require("../Institutions/InstitutionsAPI")
FeaturesUpdater = require("../Subscription/FeaturesUpdater")
EmailHelper = require "../Helpers/EmailHelper"
Errors = require "../Errors/Errors"

View file

@ -4,16 +4,16 @@ SandboxedModule = require('sandboxed-module')
assert = require('assert')
path = require('path')
sinon = require('sinon')
modulePath = path.join __dirname, "../../../../app/js/Features/User/UserAffiliationsManager"
modulePath = path.join __dirname, "../../../../app/js/Features/Institutions/InstitutionsAPI"
expect = require("chai").expect
describe "UserAffiliationsManager", ->
describe "InstitutionsAPI", ->
beforeEach ->
@logger = err: sinon.stub(), log: ->
settings = apis: { v1: { url: 'v1.url', user: '', pass: '' } }
@request = sinon.stub()
@UserAffiliationsManager = SandboxedModule.require modulePath, requires:
@InstitutionsAPI = SandboxedModule.require modulePath, requires:
"logger-sharelatex": @logger
"metrics-sharelatex": timeAsyncMethod: sinon.stub()
'settings-sharelatex': settings
@ -25,11 +25,11 @@ describe "UserAffiliationsManager", ->
email:"hello@world.com"
@newEmail = "bob@bob.com"
describe 'getAffiliations', ->
describe 'getUserAffiliations', ->
it 'get affiliations', (done)->
responseBody = [{ foo: 'bar' }]
@request.callsArgWith(1, null, { statusCode: 201 }, responseBody)
@UserAffiliationsManager.getAffiliations @stubbedUser._id, (err, body) =>
@InstitutionsAPI.getUserAffiliations @stubbedUser._id, (err, body) =>
should.not.exist(err)
@request.calledOnce.should.equal true
requestOptions = @request.lastCall.args[0]
@ -43,7 +43,7 @@ describe "UserAffiliationsManager", ->
it 'handle error', (done)->
body = errors: 'affiliation error message'
@request.callsArgWith(1, null, { statusCode: 503 }, body)
@UserAffiliationsManager.getAffiliations @stubbedUser._id, (err) =>
@InstitutionsAPI.getUserAffiliations @stubbedUser._id, (err) =>
should.exist(err)
err.message.should.have.string 503
err.message.should.have.string body.errors
@ -58,7 +58,7 @@ describe "UserAffiliationsManager", ->
university: { id: 1 }
role: 'Prof'
department: 'Math'
@UserAffiliationsManager.addAffiliation @stubbedUser._id, @newEmail, affiliationOptions, (err)=>
@InstitutionsAPI.addAffiliation @stubbedUser._id, @newEmail, affiliationOptions, (err)=>
should.not.exist(err)
@request.calledOnce.should.equal true
requestOptions = @request.lastCall.args[0]
@ -77,7 +77,7 @@ describe "UserAffiliationsManager", ->
it 'handle error', (done)->
body = errors: 'affiliation error message'
@request.callsArgWith(1, null, { statusCode: 422 }, body)
@UserAffiliationsManager.addAffiliation @stubbedUser._id, @newEmail, {}, (err)=>
@InstitutionsAPI.addAffiliation @stubbedUser._id, @newEmail, {}, (err)=>
should.exist(err)
err.message.should.have.string 422
err.message.should.have.string body.errors
@ -88,7 +88,7 @@ describe "UserAffiliationsManager", ->
@request.callsArgWith(1, null, { statusCode: 404 })
it 'remove affiliation', (done)->
@UserAffiliationsManager.removeAffiliation @stubbedUser._id, @newEmail, (err)=>
@InstitutionsAPI.removeAffiliation @stubbedUser._id, @newEmail, (err)=>
should.not.exist(err)
@request.calledOnce.should.equal true
requestOptions = @request.lastCall.args[0]
@ -100,7 +100,7 @@ describe "UserAffiliationsManager", ->
it 'handle error', (done)->
@request.callsArgWith(1, null, { statusCode: 500 })
@UserAffiliationsManager.removeAffiliation @stubbedUser._id, @newEmail, (err)=>
@InstitutionsAPI.removeAffiliation @stubbedUser._id, @newEmail, (err)=>
should.exist(err)
err.message.should.exist
done()
@ -108,7 +108,7 @@ describe "UserAffiliationsManager", ->
describe 'deleteAffiliations', ->
it 'delete affiliations', (done)->
@request.callsArgWith(1, null, { statusCode: 200 })
@UserAffiliationsManager.deleteAffiliations @stubbedUser._id, (err) =>
@InstitutionsAPI.deleteAffiliations @stubbedUser._id, (err) =>
should.not.exist(err)
@request.calledOnce.should.equal true
requestOptions = @request.lastCall.args[0]
@ -120,7 +120,7 @@ describe "UserAffiliationsManager", ->
it 'handle error', (done)->
body = errors: 'affiliation error message'
@request.callsArgWith(1, null, { statusCode: 518 }, body)
@UserAffiliationsManager.deleteAffiliations @stubbedUser._id, (err) =>
@InstitutionsAPI.deleteAffiliations @stubbedUser._id, (err) =>
should.exist(err)
err.message.should.have.string 518
err.message.should.have.string body.errors
@ -131,7 +131,7 @@ describe "UserAffiliationsManager", ->
@request.callsArgWith(1, null, { statusCode: 204 })
it 'endorse affiliation', (done)->
@UserAffiliationsManager.endorseAffiliation @stubbedUser._id, @newEmail, 'Student','Physics', (err)=>
@InstitutionsAPI.endorseAffiliation @stubbedUser._id, @newEmail, 'Student','Physics', (err)=>
should.not.exist(err)
@request.calledOnce.should.equal true
requestOptions = @request.lastCall.args[0]

View file

@ -22,7 +22,7 @@ describe "UserCreator", ->
"../../models/User": User:@UserModel
"logger-sharelatex":{log:->}
'metrics-sharelatex': {timeAsyncMethod: ()->}
"./UserAffiliationsManager": addAffiliation: @addAffiliation
"../Institutions/InstitutionsAPI": addAffiliation: @addAffiliation
@email = "bob.oswald@gmail.com"

View file

@ -31,7 +31,7 @@ describe "UserDeleter", ->
"../Newsletter/NewsletterManager": @NewsletterManager
"../Subscription/SubscriptionHandler": @SubscriptionHandler
"../Project/ProjectDeleter": @ProjectDeleter
"./UserAffiliationsManager":
"../Institutions/InstitutionsAPI":
deleteAffiliations: @deleteAffiliations
"logger-sharelatex": @logger = { log: sinon.stub() }

View file

@ -34,7 +34,7 @@ describe "UserEmailsController", ->
"./UserUpdater": @UserUpdater
"../Helpers/EmailHelper": @EmailHelper
"./UserEmailsConfirmationHandler": @UserEmailsConfirmationHandler = {}
"./UserAffiliationsManager": endorseAffiliation: @endorseAffiliation
"../Institutions/InstitutionsAPI": endorseAffiliation: @endorseAffiliation
"../Errors/Errors": Errors
"logger-sharelatex":
log: -> console.log(arguments)

View file

@ -22,15 +22,15 @@ describe "UserGetter", ->
db: users: findOne: @findOne
ObjectId: (id) -> return id
settings = apis: { v1: { url: 'v1.url', user: '', pass: '' } }
@getAffiliations = sinon.stub().callsArgWith(1, null, [])
@getUserAffiliations = sinon.stub().callsArgWith(1, null, [])
@UserGetter = SandboxedModule.require modulePath, requires:
"logger-sharelatex": log:->
"../../infrastructure/mongojs": @Mongo
"metrics-sharelatex": timeAsyncMethod: sinon.stub()
'settings-sharelatex': settings
'./UserAffiliationsManager':
getAffiliations: @getAffiliations
'../Institutions/InstitutionsAPI':
getUserAffiliations: @getUserAffiliations
"../Errors/Errors": Errors
describe "getUser", ->
@ -77,7 +77,7 @@ describe "UserGetter", ->
institution: { name: 'University Name', isUniversity: true }
}
]
@getAffiliations.callsArgWith(1, null, affiliationsData)
@getUserAffiliations.callsArgWith(1, null, affiliationsData)
@UserGetter.getUserFullEmails @fakeUser._id, (error, fullEmails) =>
assert.deepEqual fullEmails, [
{

View file

@ -25,7 +25,7 @@ describe "UserUpdater", ->
@UserUpdater = SandboxedModule.require modulePath, requires:
"logger-sharelatex": @logger
"./UserGetter": @UserGetter
'./UserAffiliationsManager':
'../Institutions/InstitutionsAPI':
addAffiliation: @addAffiliation
removeAffiliation: @removeAffiliation
'../Subscription/FeaturesUpdater': refreshFeatures: @refreshFeatures