Merge pull request #3152 from overleaf/ta-jpa-confirm-entitlement

Send Entitlement for Affiliations During Domains Confirmation

GitOrigin-RevId: 9d6b41022adfdb5e1a797b9471830014b1ef43e3
This commit is contained in:
Timothée Alby 2020-09-15 14:49:42 +02:00 committed by Copybot
parent 44b10c781f
commit ede3b6a248
4 changed files with 79 additions and 20 deletions

View file

@ -60,12 +60,18 @@ const InstitutionsAPI = {
affiliationOptions = {} affiliationOptions = {}
} }
const { university, department, role, confirmedAt } = affiliationOptions const {
university,
department,
role,
confirmedAt,
entitlement
} = affiliationOptions
makeAffiliationRequest( makeAffiliationRequest(
{ {
method: 'POST', method: 'POST',
path: `/api/v2/users/${userId.toString()}/affiliations`, path: `/api/v2/users/${userId.toString()}/affiliations`,
body: { email, university, department, role, confirmedAt }, body: { email, university, department, role, confirmedAt, entitlement },
defaultErrorMessage: "Couldn't create affiliation" defaultErrorMessage: "Couldn't create affiliation"
}, },
function(error, body) { function(error, body) {

View file

@ -23,10 +23,7 @@ var affiliateUsers = function(hostname, callback) {
.split('') .split('')
.reverse() .reverse()
.join('') .join('')
UserGetter.getUsersByHostname(hostname, { _id: 1, emails: 1 }, function( UserGetter.getUsersByHostname(hostname, { _id: 1 }, function(error, users) {
error,
users
) {
if (error) { if (error) {
OError.tag(error, 'problem fetching users by hostname') OError.tag(error, 'problem fetching users by hostname')
return callback(error) return callback(error)
@ -35,8 +32,13 @@ var affiliateUsers = function(hostname, callback) {
async.mapLimit( async.mapLimit(
users, users,
ASYNC_AFFILIATIONS_LIMIT, ASYNC_AFFILIATIONS_LIMIT,
(user, innerCallback) => (user, innerCallback) => {
affiliateUserByReversedHostname(user, reversedHostname, innerCallback), UserGetter.getUserFullEmails(user._id, (error, emails) => {
if (error) return innerCallback(error)
user.emails = emails
affiliateUserByReversedHostname(user, reversedHostname, innerCallback)
})
},
callback callback
) )
}) })
@ -52,11 +54,15 @@ var affiliateUserByReversedHostname = function(
) )
async.mapSeries( async.mapSeries(
matchingEmails, matchingEmails,
(email, innerCallback) => (email, innerCallback) => {
addAffiliation( addAffiliation(
user._id, user._id,
email.email, email.email,
{ confirmedAt: email.confirmedAt }, {
confirmedAt: email.confirmedAt,
entitlement:
email.samlIdentifier && email.samlIdentifier.hasEntitlement
},
error => { error => {
if (error) { if (error) {
OError.tag( OError.tag(
@ -67,7 +73,8 @@ var affiliateUserByReversedHostname = function(
} }
FeaturesUpdater.refreshFeatures(user._id, innerCallback) FeaturesUpdater.refreshFeatures(user._id, innerCallback)
} }
), )
},
callback callback
) )
} }

View file

@ -190,7 +190,8 @@ describe('InstitutionsAPI', function() {
university: { id: 1 }, university: { id: 1 },
role: 'Prof', role: 'Prof',
department: 'Math', department: 'Math',
confirmedAt: new Date() confirmedAt: new Date(),
entitlement: true
} }
return this.InstitutionsAPI.addAffiliation( return this.InstitutionsAPI.addAffiliation(
this.stubbedUser._id, this.stubbedUser._id,
@ -207,12 +208,13 @@ describe('InstitutionsAPI', function() {
requestOptions.method.should.equal('POST') requestOptions.method.should.equal('POST')
const { body } = requestOptions const { body } = requestOptions
Object.keys(body).length.should.equal(5) Object.keys(body).length.should.equal(6)
body.email.should.equal(this.newEmail) body.email.should.equal(this.newEmail)
body.university.should.equal(affiliationOptions.university) body.university.should.equal(affiliationOptions.university)
body.department.should.equal(affiliationOptions.department) body.department.should.equal(affiliationOptions.department)
body.role.should.equal(affiliationOptions.role) body.role.should.equal(affiliationOptions.role)
body.confirmedAt.should.equal(affiliationOptions.confirmedAt) body.confirmedAt.should.equal(affiliationOptions.confirmedAt)
body.entitlement.should.equal(affiliationOptions.entitlement)
this.markAsReadIpMatcher.calledOnce.should.equal(true) this.markAsReadIpMatcher.calledOnce.should.equal(true)
return done() return done()
} }

View file

@ -38,18 +38,48 @@ describe('InstitutionsController', function() {
{ email: 'another@mit.edu', reversedHostname: this.host } { email: 'another@mit.edu', reversedHostname: this.host }
] ]
} }
this.stubbedUser1DecoratedEmails = [
{
email: 'stubb1@mit.edu',
reversedHostname: this.host,
samlIdentifier: { hasEntitlement: false }
},
{ email: 'test@test.com', reversedHostname: 'test.com' },
{
email: 'another@mit.edu',
reversedHostname: this.host,
samlIdentifier: { hasEntitlement: true }
}
]
this.stubbedUser2 = { this.stubbedUser2 = {
_id: '3131232', _id: '3131232',
name: 'test', name: 'test',
email: 'hello2@world.com', email: 'hello2@world.com',
emails: [{ email: 'subb2@mit.edu', reversedHostname: this.host }] emails: [{ email: 'subb2@mit.edu', reversedHostname: this.host }]
} }
this.stubbedUser2DecoratedEmails = [
{
email: 'subb2@mit.edu',
reversedHostname: this.host
}
]
this.getUsersByHostname = sinon this.getUsersByHostname = sinon.stub().callsArgWith(
.stub() 2,
.callsArgWith(2, null, [this.stubbedUser1, this.stubbedUser2]) null,
[this.stubbedUser1, this.stubbedUser2].map(user => {
return { _id: user._id }
})
)
this.addAffiliation = sinon.stub().callsArgWith(3, null) this.addAffiliation = sinon.stub().callsArgWith(3, null)
this.refreshFeatures = sinon.stub().yields(null) this.refreshFeatures = sinon.stub().yields(null)
this.getUserFullEmails = sinon.stub()
this.getUserFullEmails
.withArgs(this.stubbedUser1._id)
.yields(null, this.stubbedUser1DecoratedEmails)
this.getUserFullEmails
.withArgs(this.stubbedUser2._id)
.yields(null, this.stubbedUser2DecoratedEmails)
this.InstitutionsController = SandboxedModule.require(modulePath, { this.InstitutionsController = SandboxedModule.require(modulePath, {
globals: { globals: {
console: console console: console
@ -57,7 +87,8 @@ describe('InstitutionsController', function() {
requires: { requires: {
'logger-sharelatex': this.logger, 'logger-sharelatex': this.logger,
'../User/UserGetter': { '../User/UserGetter': {
getUsersByHostname: this.getUsersByHostname getUsersByHostname: this.getUsersByHostname,
getUserFullEmails: this.getUserFullEmails
}, },
'../Institutions/InstitutionsAPI': { '../Institutions/InstitutionsAPI': {
addAffiliation: this.addAffiliation addAffiliation: this.addAffiliation
@ -84,13 +115,25 @@ describe('InstitutionsController', function() {
this.getUsersByHostname.calledOnce.should.equal(true) this.getUsersByHostname.calledOnce.should.equal(true)
this.addAffiliation.calledThrice.should.equal(true) this.addAffiliation.calledThrice.should.equal(true)
this.addAffiliation this.addAffiliation
.calledWith(this.stubbedUser1._id, this.stubbedUser1.emails[0].email) .calledWithMatch(
this.stubbedUser1._id,
this.stubbedUser1.emails[0].email,
{ entitlement: false }
)
.should.equal(true) .should.equal(true)
this.addAffiliation this.addAffiliation
.calledWith(this.stubbedUser1._id, this.stubbedUser1.emails[2].email) .calledWithMatch(
this.stubbedUser1._id,
this.stubbedUser1.emails[2].email,
{ entitlement: true }
)
.should.equal(true) .should.equal(true)
this.addAffiliation this.addAffiliation
.calledWith(this.stubbedUser2._id, this.stubbedUser2.emails[0].email) .calledWithMatch(
this.stubbedUser2._id,
this.stubbedUser2.emails[0].email,
{ entitlement: undefined }
)
.should.equal(true) .should.equal(true)
this.refreshFeatures this.refreshFeatures
.calledWith(this.stubbedUser1._id) .calledWith(this.stubbedUser1._id)
@ -100,6 +143,7 @@ describe('InstitutionsController', function() {
.should.equal(true) .should.equal(true)
return done() return done()
} }
this.next.callsFake(done)
return this.InstitutionsController.confirmDomain( return this.InstitutionsController.confirmDomain(
this.req, this.req,
this.res, this.res,