Merge pull request #3491 from overleaf/jel-v1-mock-affiliations

Affiliations in v1 mock API

GitOrigin-RevId: 7d6044c5477af529c0ab111c24945c97c982220b
This commit is contained in:
Timothée Alby 2021-01-05 11:55:59 +01:00 committed by Copybot
parent 0507b5e1f0
commit f6decd4728
3 changed files with 75 additions and 29 deletions

View file

@ -162,7 +162,7 @@ describe('FeatureUpdater.refreshFeatures', function() {
}) })
it('should not set their features if email is not confirmed', function(done) { it('should not set their features if email is not confirmed', function(done) {
MockV1Api.setAffiliations([this.affiliationData]) MockV1Api.setAffiliations(this.user._id, [this.affiliationData])
return syncUserAndGetFeatures(this.user, (error, features) => { return syncUserAndGetFeatures(this.user, (error, features) => {
expect(features).to.deep.equal(settings.defaultFeatures) expect(features).to.deep.equal(settings.defaultFeatures)
return done() return done()
@ -170,7 +170,7 @@ describe('FeatureUpdater.refreshFeatures', function() {
}) })
it('should set their features if email is confirmed', function(done) { it('should set their features if email is confirmed', function(done) {
MockV1Api.setAffiliations([this.affiliationData]) MockV1Api.setAffiliations(this.user._id, [this.affiliationData])
return this.user.confirmEmail(this.email, error => { return this.user.confirmEmail(this.email, error => {
return syncUserAndGetFeatures(this.user, (error, features) => { return syncUserAndGetFeatures(this.user, (error, features) => {
expect(features).to.deep.equal(this.institutionPlan.features) expect(features).to.deep.equal(this.institutionPlan.features)
@ -181,7 +181,7 @@ describe('FeatureUpdater.refreshFeatures', function() {
it('should not set their features if institution is not confirmed', function(done) { it('should not set their features if institution is not confirmed', function(done) {
this.affiliationData.institution.confirmed = false this.affiliationData.institution.confirmed = false
MockV1Api.setAffiliations([this.affiliationData]) MockV1Api.setAffiliations(this.user._id, [this.affiliationData])
return this.user.confirmEmail(this.email, error => { return this.user.confirmEmail(this.email, error => {
return syncUserAndGetFeatures(this.user, (error, features) => { return syncUserAndGetFeatures(this.user, (error, features) => {
expect(features).to.deep.equal(settings.defaultFeatures) expect(features).to.deep.equal(settings.defaultFeatures)

View file

@ -429,7 +429,7 @@ describe('Subscriptions', function() {
subscription: {}, subscription: {},
subscription_status: {} subscription_status: {}
}) })
MockV1Api.setAffiliations([ MockV1Api.setAffiliations(this.user._id, [
{ {
email: 'confirmed-affiliation-email@stanford.example.edu', email: 'confirmed-affiliation-email@stanford.example.edu',
licence: 'pro_plus', licence: 'pro_plus',

View file

@ -19,22 +19,39 @@ const bodyParser = require('body-parser')
const sinon = require('sinon') const sinon = require('sinon')
app.use(bodyParser.json()) app.use(bodyParser.json())
const blocklistedDomains = []
let v1Id = 1000
module.exports = MockV1Api = { module.exports = MockV1Api = {
nextV1Id() { reset() {
return v1Id++ this.affiliations = []
this.exportId = null
this.v1Id = 1000
this.users = {}
this.docInfo = {}
this.existingEmails = []
this.brands = {}
this.brand_variations = {}
this.validation_clients = {}
this.doc_exported = {}
this.templates = {}
this.institutionId = 1000
this.institutions = {}
this.allInstitutionDomains = new Set()
this.institutionDomains = {}
}, },
users: {}, nextInstitutionId() {
return this.institutionId++
},
nextV1Id() {
return this.v1Id++
},
setUser(id, user) { setUser(id, user) {
return (this.users[id] = user) return (this.users[id] = user)
}, },
docInfo: {},
getDocInfo(token) { getDocInfo(token) {
return this.docInfo[token] || null return this.docInfo[token] || null
}, },
@ -43,8 +60,6 @@ module.exports = MockV1Api = {
this.docInfo[token] = info this.docInfo[token] = info
}, },
exportId: null,
exportParams: null, exportParams: null,
setExportId(id) { setExportId(id) {
@ -61,30 +76,59 @@ module.exports = MockV1Api = {
syncUserFeatures: sinon.stub(), syncUserFeatures: sinon.stub(),
affiliations: [],
updateEmail: sinon.stub(), updateEmail: sinon.stub(),
existingEmails: [], createInstitution(options = {}) {
const id = options.university_id || this.nextInstitutionId()
brands: {}, options.id = id // include ID so that it is included in APIs
this.institutions[id] = { ...options }
brand_variations: {}, if (options && options.hostname) {
this.addInstitutionDomain(id, options.hostname)
validation_clients: {}, }
return id
setAffiliations(affiliations) {
return (this.affiliations = affiliations)
}, },
doc_exported: {}, addInstitutionDomain(id, domain) {
if (this.allInstitutionDomains.has(domain)) return
if (!this.institutionDomains[id]) this.institutionDomains[id] = new Set()
this.institutionDomains[id].add(domain)
this.allInstitutionDomains.add(domain)
},
updateInstitution(id, options) {
Object.assign(this.institutions[id], options)
},
addAffiliation(userId, email) {
let institution
if (!email) return
const domain = email.split('@').pop()
if (blocklistedDomains.indexOf(domain.replace('.com', '')) !== -1) return
if (this.allInstitutionDomains.has(domain)) {
for (const [id, domainSet] of Object.entries(this.institutionDomains)) {
if (domainSet.has(domain)) {
institution = this.institutions[id]
}
}
}
if (institution) {
if (!this.affiliations[userId]) this.affiliations[userId] = []
this.affiliations[userId].push({ email, institution })
}
},
setAffiliations(userId, affiliations) {
this.affiliations[userId] = affiliations
},
setDocExported(token, info) { setDocExported(token, info) {
return (this.doc_exported[token] = info) return (this.doc_exported[token] = info)
}, },
templates: {},
setTemplates(templates) { setTemplates(templates) {
this.templates = templates this.templates = templates
}, },
@ -150,10 +194,11 @@ module.exports = MockV1Api = {
}) })
app.get('/api/v2/users/:userId/affiliations', (req, res, next) => { app.get('/api/v2/users/:userId/affiliations', (req, res, next) => {
return res.json(this.affiliations) return res.json(this.affiliations[req.params.userId] || [])
}) })
app.post('/api/v2/users/:userId/affiliations', (req, res, next) => { app.post('/api/v2/users/:userId/affiliations', (req, res, next) => {
this.addAffiliation(req.params.userId, req.body.email)
return res.sendStatus(201) return res.sendStatus(201)
}) })
@ -287,6 +332,7 @@ module.exports = MockV1Api = {
} }
} }
MockV1Api.reset()
MockV1Api.run() MockV1Api.run()
function __guard__(value, transform) { function __guard__(value, transform) {