From f6decd472800a0394f0afe7532f602f1183d4db4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Alby?= Date: Tue, 5 Jan 2021 11:55:59 +0100 Subject: [PATCH] Merge pull request #3491 from overleaf/jel-v1-mock-affiliations Affiliations in v1 mock API GitOrigin-RevId: 7d6044c5477af529c0ab111c24945c97c982220b --- .../acceptance/src/FeatureUpdaterTests.js | 6 +- .../src/SubscriptionDashboardTests.js | 2 +- .../test/acceptance/src/helpers/MockV1Api.js | 96 ++++++++++++++----- 3 files changed, 75 insertions(+), 29 deletions(-) diff --git a/services/web/test/acceptance/src/FeatureUpdaterTests.js b/services/web/test/acceptance/src/FeatureUpdaterTests.js index 6aea5f72d3..c3a808c0d3 100644 --- a/services/web/test/acceptance/src/FeatureUpdaterTests.js +++ b/services/web/test/acceptance/src/FeatureUpdaterTests.js @@ -162,7 +162,7 @@ describe('FeatureUpdater.refreshFeatures', function() { }) 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) => { expect(features).to.deep.equal(settings.defaultFeatures) return done() @@ -170,7 +170,7 @@ describe('FeatureUpdater.refreshFeatures', function() { }) 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 syncUserAndGetFeatures(this.user, (error, 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) { this.affiliationData.institution.confirmed = false - MockV1Api.setAffiliations([this.affiliationData]) + MockV1Api.setAffiliations(this.user._id, [this.affiliationData]) return this.user.confirmEmail(this.email, error => { return syncUserAndGetFeatures(this.user, (error, features) => { expect(features).to.deep.equal(settings.defaultFeatures) diff --git a/services/web/test/acceptance/src/SubscriptionDashboardTests.js b/services/web/test/acceptance/src/SubscriptionDashboardTests.js index cfde961757..8c0185d807 100644 --- a/services/web/test/acceptance/src/SubscriptionDashboardTests.js +++ b/services/web/test/acceptance/src/SubscriptionDashboardTests.js @@ -429,7 +429,7 @@ describe('Subscriptions', function() { subscription: {}, subscription_status: {} }) - MockV1Api.setAffiliations([ + MockV1Api.setAffiliations(this.user._id, [ { email: 'confirmed-affiliation-email@stanford.example.edu', licence: 'pro_plus', diff --git a/services/web/test/acceptance/src/helpers/MockV1Api.js b/services/web/test/acceptance/src/helpers/MockV1Api.js index ab62cdfbc6..8f8bb8fd22 100644 --- a/services/web/test/acceptance/src/helpers/MockV1Api.js +++ b/services/web/test/acceptance/src/helpers/MockV1Api.js @@ -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: [], - - brands: {}, - - brand_variations: {}, - - validation_clients: {}, - - setAffiliations(affiliations) { - return (this.affiliations = affiliations) + 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 }, - 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) { 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) {