Merge pull request #728 from sharelatex/ta-endorse-affiliation

Add Endpoint to Endorse Affiliation
This commit is contained in:
Timothée Alby 2018-07-05 16:00:47 +02:00 committed by GitHub
commit cc837236b4
5 changed files with 62 additions and 0 deletions

View file

@ -31,6 +31,15 @@ module.exports = UserAffiliationsManager =
}, callback
endorseAffiliation: (userId, email, role, department, callback = (error) ->) ->
makeAffiliationRequest {
method: 'POST'
path: "/api/v2/users/#{userId.toString()}/affiliations/endorse"
body: { email, role, department }
defaultErrorMessage: "Couldn't endorse affiliation"
}, callback
deleteAffiliations: (userId, callback = (error) ->) ->
makeAffiliationRequest {
method: 'DELETE'

View file

@ -3,6 +3,7 @@ UserGetter = require("./UserGetter")
UserUpdater = require("./UserUpdater")
EmailHelper = require("../Helpers/EmailHelper")
UserEmailsConfirmationHandler = require "./UserEmailsConfirmationHandler"
{ endorseAffiliation } = require("./UserAffiliationsManager")
logger = require("logger-sharelatex")
Errors = require "../Errors/Errors"
@ -50,6 +51,17 @@ module.exports = UserEmailsController =
return next(error) if error?
res.sendStatus 200
endorse: (req, res, next) ->
userId = AuthenticationController.getLoggedInUserId(req)
email = EmailHelper.parseEmail(req.body.email)
return res.sendStatus 422 unless email?
endorseAffiliation userId, email, req.body.role, req.body.department, (error)->
return next(error) if error?
res.sendStatus 204
showConfirm: (req, res, next) ->
res.render 'user/confirm_email', {
token: req.query.token,

View file

@ -126,6 +126,9 @@ module.exports = class Router
webRouter.post '/user/emails/default',
AuthenticationController.requireLogin(),
UserEmailsController.setDefault
webRouter.post '/user/emails/endorse',
AuthenticationController.requireLogin(),
UserEmailsController.endorse
webRouter.get '/user/sessions',

View file

@ -125,3 +125,23 @@ describe "UserAffiliationsManager", ->
err.message.should.have.string 518
err.message.should.have.string body.errors
done()
describe 'endorseAffiliation', ->
beforeEach ->
@request.callsArgWith(1, null, { statusCode: 204 })
it 'endorse affiliation', (done)->
@UserAffiliationsManager.endorseAffiliation @stubbedUser._id, @newEmail, 'Student','Physics', (err)=>
should.not.exist(err)
@request.calledOnce.should.equal true
requestOptions = @request.lastCall.args[0]
expectedUrl = "v1.url/api/v2/users/#{@stubbedUser._id}/affiliations/endorse"
requestOptions.url.should.equal expectedUrl
requestOptions.method.should.equal 'POST'
body = requestOptions.body
Object.keys(body).length.should.equal 3
body.email.should.equal @newEmail
body.role.should.equal 'Student'
body.department.should.equal 'Physics'
done()

View file

@ -26,12 +26,14 @@ describe "UserEmailsController", ->
setDefaultEmailAddress: sinon.stub()
@EmailHelper =
parseEmail: sinon.stub()
@endorseAffiliation = sinon.stub().yields()
@UserEmailsController = SandboxedModule.require modulePath, requires:
"../Authentication/AuthenticationController": @AuthenticationController
"./UserGetter": @UserGetter
"./UserUpdater": @UserUpdater
"../Helpers/EmailHelper": @EmailHelper
"./UserEmailsConfirmationHandler": @UserEmailsConfirmationHandler = {}
"./UserAffiliationsManager": endorseAffiliation: @endorseAffiliation
"../Errors/Errors": Errors
"logger-sharelatex":
log: -> console.log(arguments)
@ -142,6 +144,22 @@ describe "UserEmailsController", ->
assertNotCalled @UserUpdater.setDefaultEmailAddress
done()
describe 'endorse', ->
beforeEach ->
@email = 'email_to_endorse@bar.com'
@req.body.email = @email
@EmailHelper.parseEmail.returns @email
it 'endorses affiliation', (done) ->
@req.body.role = 'Role'
@req.body.department = 'Department'
@UserEmailsController.endorse @req,
sendStatus: (code) =>
code.should.equal 204
assertCalledWith @endorseAffiliation, @user._id, @email, 'Role', 'Department'
done()
describe 'confirm', ->
beforeEach ->
@UserEmailsConfirmationHandler.confirmEmailFromToken = sinon.stub().yields()