|
|
@@ -19,22 +19,39 @@ const bodyParser = require('body-parser')
|
|
|
const sinon = require('sinon')
|
|
|
|
|
|
app.use(bodyParser.json())
|
|
|
-
|
|
|
-let v1Id = 1000
|
|
|
+const blocklistedDomains = []
|
|
|
|
|
|
module.exports = MockV1Api = {
|
|
|
- nextV1Id() {
|
|
|
- return v1Id++
|
|
|
+ reset() {
|
|
|
+ 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) {
|
|
|
return (this.users[id] = user)
|
|
|
},
|
|
|
|
|
|
- docInfo: {},
|
|
|
-
|
|
|
getDocInfo(token) {
|
|
|
return this.docInfo[token] || null
|
|
|
},
|
|
|
@@ -43,8 +60,6 @@ module.exports = MockV1Api = {
|
|
|
this.docInfo[token] = info
|
|
|
},
|
|
|
|
|
|
- exportId: null,
|
|
|
-
|
|
|
exportParams: null,
|
|
|
|
|
|
setExportId(id) {
|
|
|
@@ -61,30 +76,59 @@ module.exports = MockV1Api = {
|
|
|
|
|
|
syncUserFeatures: sinon.stub(),
|
|
|
|
|
|
- affiliations: [],
|
|
|
-
|
|
|
updateEmail: sinon.stub(),
|
|
|
|
|
|
- existingEmails: [],
|
|
|
+ createInstitution(options = {}) {
|
|
|
+ const id = options.university_id || this.nextInstitutionId()
|
|
|
+ options.id = id // include ID so that it is included in APIs
|
|
|
+ this.institutions[id] = { ...options }
|
|
|
+ if (options && options.hostname) {
|
|
|
+ this.addInstitutionDomain(id, options.hostname)
|
|
|
+ }
|
|
|
+ return id
|
|
|
+ },
|
|
|
|
|
|
- brands: {},
|
|
|
+ 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)
|
|
|
+ },
|
|
|
|
|
|
- brand_variations: {},
|
|
|
+ addAffiliation(userId, email) {
|
|
|
+ let institution
|
|
|
+ if (!email) return
|
|
|
|
|
|
- validation_clients: {},
|
|
|
+ const domain = email.split('@').pop()
|
|
|
|
|
|
- setAffiliations(affiliations) {
|
|
|
- return (this.affiliations = affiliations)
|
|
|
+ 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 })
|
|
|
+ }
|
|
|
},
|
|
|
|
|
|
- doc_exported: {},
|
|
|
+ setAffiliations(userId, affiliations) {
|
|
|
+ this.affiliations[userId] = affiliations
|
|
|
+ },
|
|
|
|
|
|
setDocExported(token, info) {
|
|
|
return (this.doc_exported[token] = info)
|
|
|
},
|
|
|
|
|
|
- templates: {},
|
|
|
-
|
|
|
setTemplates(templates) {
|
|
|
this.templates = templates
|
|
|
},
|
|
|
@@ -150,10 +194,11 @@ module.exports = MockV1Api = {
|
|
|
})
|
|
|
|
|
|
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) => {
|
|
|
+ this.addAffiliation(req.params.userId, req.body.email)
|
|
|
return res.sendStatus(201)
|
|
|
})
|
|
|
|
|
|
@@ -287,6 +332,7 @@ module.exports = MockV1Api = {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+MockV1Api.reset()
|
|
|
MockV1Api.run()
|
|
|
|
|
|
function __guard__(value, transform) {
|