InstitutionsManagerTests.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. const SandboxedModule = require('sandboxed-module')
  2. const path = require('path')
  3. const sinon = require('sinon')
  4. const { expect } = require('chai')
  5. const modulePath = path.join(
  6. __dirname,
  7. '../../../../app/src/Features/Institutions/InstitutionsManager'
  8. )
  9. const Features = require('../../../../app/src/infrastructure/Features')
  10. describe('InstitutionsManager', function () {
  11. beforeEach(function () {
  12. this.institutionId = 123
  13. this.user = {}
  14. this.getInstitutionAffiliations = sinon.stub()
  15. this.getConfirmedInstitutionAffiliations = sinon.stub()
  16. this.addAffiliation = sinon.stub().callsArgWith(3, null)
  17. this.refreshFeatures = sinon.stub().yields()
  18. this.users = [
  19. { _id: 'lapsed', features: {} },
  20. { _id: '1a', features: {} },
  21. { _id: '2b', features: {} },
  22. { _id: '3c', features: {} },
  23. ]
  24. this.ssoUsers = [
  25. {
  26. _id: '1a',
  27. samlIdentifiers: [{ providerId: this.institutionId.toString() }],
  28. },
  29. {
  30. _id: '2b',
  31. samlIdentifiers: [
  32. {
  33. providerId: this.institutionId.toString(),
  34. hasEntitlement: true,
  35. },
  36. ],
  37. },
  38. {
  39. _id: 'lapsed',
  40. samlIdentifiers: [{ providerId: this.institutionId.toString() }],
  41. hasEntitlement: true,
  42. },
  43. ]
  44. this.UserGetter = {
  45. getUsersByAnyConfirmedEmail: sinon.stub().yields(),
  46. getUser: sinon.stub().yields(null, this.user),
  47. promises: {
  48. getUsers: sinon.stub().resolves(this.users),
  49. getUsersByAnyConfirmedEmail: sinon.stub().resolves(),
  50. getSsoUsersAtInstitution: (this.getSsoUsersAtInstitution = sinon
  51. .stub()
  52. .resolves(this.ssoUsers)),
  53. },
  54. }
  55. this.creator = { create: sinon.stub().callsArg(0) }
  56. this.NotificationsBuilder = {
  57. featuresUpgradedByAffiliation: sinon.stub().returns(this.creator),
  58. redundantPersonalSubscription: sinon.stub().returns(this.creator),
  59. }
  60. this.SubscriptionLocator = {
  61. getUsersSubscription: sinon.stub().callsArg(1),
  62. }
  63. this.institutionWithV1Data = { name: 'Wombat University' }
  64. this.institution = {
  65. fetchV1Data: sinon
  66. .stub()
  67. .callsArgWith(0, null, this.institutionWithV1Data),
  68. }
  69. this.InstitutionModel = {
  70. Institution: {
  71. findOne: sinon.stub().callsArgWith(1, null, this.institution),
  72. },
  73. }
  74. this.subscriptionExec = sinon.stub().yields()
  75. const SubscriptionModel = {
  76. Subscription: {
  77. find: () => ({
  78. populate: () => ({
  79. exec: this.subscriptionExec,
  80. }),
  81. }),
  82. },
  83. }
  84. this.Mongo = { ObjectId: sinon.stub().returnsArg(0) }
  85. this.v1Counts = {
  86. user_ids: this.users.map(user => user._id),
  87. current_users_count: 3,
  88. lapsed_user_ids: ['lapsed'],
  89. entitled_via_sso: 1, // 2 entitled, but 1 lapsed
  90. with_confirmed_email: 2, // 1 non entitled SSO + 1 email user
  91. }
  92. // this.InstitutionsController = SandboxedModule.require(modulePath, {
  93. // requires: {
  94. // '../User/UserGetter': {
  95. // getInstitutionUsersByHostname: this.getInstitutionUsersByHostname,
  96. // },
  97. // '../Institutions/InstitutionsAPI': {
  98. // addAffiliation: this.addAffiliation,
  99. // },
  100. // '../Subscription/FeaturesUpdater': {
  101. // refreshFeatures: this.refreshFeatures,
  102. // },
  103. // },
  104. this.InstitutionsManager = SandboxedModule.require(modulePath, {
  105. requires: {
  106. './InstitutionsAPI': {
  107. getInstitutionAffiliations: this.getInstitutionAffiliations,
  108. getConfirmedInstitutionAffiliations:
  109. this.getConfirmedInstitutionAffiliations,
  110. addAffiliation: this.addAffiliation,
  111. promises: {
  112. getInstitutionAffiliations:
  113. (this.getInstitutionAffiliationsPromise = sinon
  114. .stub()
  115. .resolves(this.affiliations)),
  116. getConfirmedInstitutionAffiliations:
  117. (this.getConfirmedInstitutionAffiliationsPromise = sinon
  118. .stub()
  119. .resolves(this.affiliations)),
  120. getInstitutionAffiliationsCounts:
  121. (this.getInstitutionAffiliationsCounts = sinon
  122. .stub()
  123. .resolves(this.v1Counts)),
  124. },
  125. },
  126. '../Subscription/FeaturesUpdater': {
  127. refreshFeatures: this.refreshFeatures,
  128. },
  129. '../Subscription/FeaturesHelper': {
  130. isFeatureSetBetter: (this.isFeatureSetBetter = sinon.stub()),
  131. },
  132. '../User/UserGetter': this.UserGetter,
  133. '../Notifications/NotificationsBuilder': this.NotificationsBuilder,
  134. '../Subscription/SubscriptionLocator': this.SubscriptionLocator,
  135. '../../models/Institution': this.InstitutionModel,
  136. '../../models/Subscription': SubscriptionModel,
  137. mongodb: this.Mongo,
  138. '@overleaf/settings': {
  139. features: { professional: { 'test-feature': true } },
  140. },
  141. },
  142. })
  143. })
  144. describe('refreshInstitutionUsers', function () {
  145. beforeEach(function () {
  146. this.user1Id = '123abc123abc123abc123abc'
  147. this.user2Id = '456def456def456def456def'
  148. this.user3Id = 'trial123abc'
  149. this.user4Id = 'group123abc'
  150. this.affiliations = [
  151. { user_id: this.user1Id },
  152. { user_id: this.user2Id },
  153. { user_id: this.user3Id },
  154. { user_id: this.user4Id },
  155. ]
  156. this.user1 = { _id: this.user1Id }
  157. this.user2 = { _id: this.user2Id }
  158. this.user3 = { _id: this.user3Id }
  159. this.user4 = { _id: this.user4Id }
  160. this.UserGetter.getUser.withArgs(this.user1Id).yields(null, this.user1)
  161. this.UserGetter.getUser.withArgs(this.user2Id).yields(null, this.user2)
  162. this.UserGetter.getUser.withArgs(this.user3Id).yields(null, this.user3)
  163. this.UserGetter.getUser.withArgs(this.user4Id).yields(null, this.user4)
  164. this.SubscriptionLocator.getUsersSubscription
  165. .withArgs(this.user2)
  166. .callsArgWith(1, null, {
  167. planCode: 'pro',
  168. groupPlan: false,
  169. })
  170. this.SubscriptionLocator.getUsersSubscription
  171. .withArgs(this.user3)
  172. .callsArgWith(1, null, {
  173. planCode: 'collaborator_free_trial_7_days',
  174. groupPlan: false,
  175. })
  176. this.SubscriptionLocator.getUsersSubscription
  177. .withArgs(this.user4)
  178. .callsArgWith(1, null, {
  179. planCode: 'collaborator-annual',
  180. groupPlan: true,
  181. })
  182. this.refreshFeatures.withArgs(this.user1Id).yields(null, {}, true)
  183. this.getInstitutionAffiliations.yields(null, this.affiliations)
  184. this.getConfirmedInstitutionAffiliations.yields(null, this.affiliations)
  185. })
  186. it('refresh all users Features', function (done) {
  187. this.InstitutionsManager.refreshInstitutionUsers(
  188. this.institutionId,
  189. false,
  190. error => {
  191. expect(error).not.to.exist
  192. sinon.assert.callCount(this.refreshFeatures, 4)
  193. // expect no notifications
  194. sinon.assert.notCalled(
  195. this.NotificationsBuilder.featuresUpgradedByAffiliation
  196. )
  197. sinon.assert.notCalled(
  198. this.NotificationsBuilder.redundantPersonalSubscription
  199. )
  200. done()
  201. }
  202. )
  203. })
  204. it('notifies users if their features have been upgraded', function (done) {
  205. this.InstitutionsManager.refreshInstitutionUsers(
  206. this.institutionId,
  207. true,
  208. error => {
  209. expect(error).not.to.exist
  210. sinon.assert.calledOnce(
  211. this.NotificationsBuilder.featuresUpgradedByAffiliation
  212. )
  213. sinon.assert.calledWith(
  214. this.NotificationsBuilder.featuresUpgradedByAffiliation,
  215. this.affiliations[0],
  216. this.user1
  217. )
  218. done()
  219. }
  220. )
  221. })
  222. it('notifies users if they have a subscription, or a trial subscription, that should be cancelled', function (done) {
  223. this.InstitutionsManager.refreshInstitutionUsers(
  224. this.institutionId,
  225. true,
  226. error => {
  227. expect(error).not.to.exist
  228. sinon.assert.calledTwice(
  229. this.NotificationsBuilder.redundantPersonalSubscription
  230. )
  231. sinon.assert.calledWith(
  232. this.NotificationsBuilder.redundantPersonalSubscription,
  233. this.affiliations[1],
  234. this.user2
  235. )
  236. sinon.assert.calledWith(
  237. this.NotificationsBuilder.redundantPersonalSubscription,
  238. this.affiliations[2],
  239. this.user3
  240. )
  241. done()
  242. }
  243. )
  244. })
  245. })
  246. describe('checkInstitutionUsers', function () {
  247. it('returns entitled/not, sso/not, lapsed/current, and pro counts', async function () {
  248. if (Features.hasFeature('saas')) {
  249. this.isFeatureSetBetter.returns(true)
  250. const usersSummary =
  251. await this.InstitutionsManager.promises.checkInstitutionUsers(
  252. this.institutionId
  253. )
  254. expect(usersSummary).to.deep.equal({
  255. emailUsers: {
  256. total: 1,
  257. current: 1,
  258. lapsed: 0,
  259. pro: {
  260. current: 1, // isFeatureSetBetter stubbed to return true for all
  261. lapsed: 0,
  262. },
  263. nonPro: {
  264. current: 0,
  265. lapsed: 0,
  266. },
  267. },
  268. ssoUsers: {
  269. total: 3,
  270. lapsed: 1,
  271. current: {
  272. entitled: 1,
  273. notEntitled: 1,
  274. },
  275. pro: {
  276. current: 2,
  277. lapsed: 1, // isFeatureSetBetter stubbed to return true for all users
  278. },
  279. nonPro: {
  280. current: 0,
  281. lapsed: 0,
  282. },
  283. },
  284. })
  285. }
  286. })
  287. it('includes withConfirmedEmailMismatch when v1 and v2 counts do not add up', async function () {
  288. if (Features.hasFeature('saas')) {
  289. this.isFeatureSetBetter.returns(true)
  290. this.v1Counts.with_confirmed_email = 100
  291. const usersSummary =
  292. await this.InstitutionsManager.promises.checkInstitutionUsers(
  293. this.institutionId
  294. )
  295. expect(usersSummary).to.deep.equal({
  296. emailUsers: {
  297. total: 1,
  298. current: 1,
  299. lapsed: 0,
  300. pro: {
  301. current: 1, // isFeatureSetBetter stubbed to return true for all
  302. lapsed: 0,
  303. },
  304. nonPro: {
  305. current: 0,
  306. lapsed: 0,
  307. },
  308. },
  309. ssoUsers: {
  310. total: 3,
  311. lapsed: 1,
  312. current: {
  313. entitled: 1,
  314. notEntitled: 1,
  315. },
  316. pro: {
  317. current: 2,
  318. lapsed: 1, // isFeatureSetBetter stubbed to return true for all users
  319. },
  320. nonPro: {
  321. current: 0,
  322. lapsed: 0,
  323. },
  324. },
  325. databaseMismatch: {
  326. withConfirmedEmail: {
  327. v1: 100,
  328. v2: 2,
  329. },
  330. },
  331. })
  332. }
  333. })
  334. })
  335. describe('getInstitutionUsersSubscriptions', function () {
  336. it('returns all institution users subscriptions', function (done) {
  337. const stubbedUsers = [
  338. { user_id: '123abc123abc123abc123abc' },
  339. { user_id: '456def456def456def456def' },
  340. { user_id: '789def789def789def789def' },
  341. ]
  342. this.getInstitutionAffiliations.yields(null, stubbedUsers)
  343. this.InstitutionsManager.getInstitutionUsersSubscriptions(
  344. this.institutionId,
  345. (error, subscriptions) => {
  346. expect(error).not.to.exist
  347. sinon.assert.calledOnce(this.subscriptionExec)
  348. done()
  349. }
  350. )
  351. })
  352. })
  353. describe('addAffiliations', function () {
  354. beforeEach(function () {
  355. this.host = 'mit.edu'.split('').reverse().join('')
  356. this.stubbedUser1 = {
  357. _id: '3131231',
  358. name: 'bob',
  359. email: 'hello@world.com',
  360. emails: [
  361. { email: 'stubb1@mit.edu', reversedHostname: this.host },
  362. { email: 'test@test.com', reversedHostname: 'test.com' },
  363. { email: 'another@mit.edu', reversedHostname: this.host },
  364. ],
  365. }
  366. this.stubbedUser1DecoratedEmails = [
  367. {
  368. email: 'stubb1@mit.edu',
  369. reversedHostname: this.host,
  370. samlIdentifier: { hasEntitlement: false },
  371. },
  372. { email: 'test@test.com', reversedHostname: 'test.com' },
  373. {
  374. email: 'another@mit.edu',
  375. reversedHostname: this.host,
  376. samlIdentifier: { hasEntitlement: true },
  377. },
  378. ]
  379. this.stubbedUser2 = {
  380. _id: '3131232',
  381. name: 'test',
  382. email: 'hello2@world.com',
  383. emails: [{ email: 'subb2@mit.edu', reversedHostname: this.host }],
  384. }
  385. this.stubbedUser2DecoratedEmails = [
  386. {
  387. email: 'subb2@mit.edu',
  388. reversedHostname: this.host,
  389. },
  390. ]
  391. this.getInstitutionUsersByHostname = sinon.stub().yields(null, [
  392. {
  393. _id: this.stubbedUser1._id,
  394. emails: this.stubbedUser1DecoratedEmails,
  395. },
  396. {
  397. _id: this.stubbedUser2._id,
  398. emails: this.stubbedUser2DecoratedEmails,
  399. },
  400. ])
  401. this.UserGetter.getInstitutionUsersByHostname =
  402. this.getInstitutionUsersByHostname
  403. })
  404. describe('affiliateUsers', function () {
  405. it('should add affiliations for matching users', function (done) {
  406. this.InstitutionsManager.affiliateUsers('mit.edu', error => {
  407. expect(error).not.to.exist
  408. this.getInstitutionUsersByHostname.calledOnce.should.equal(true)
  409. this.addAffiliation.calledThrice.should.equal(true)
  410. this.addAffiliation
  411. .calledWithMatch(
  412. this.stubbedUser1._id,
  413. this.stubbedUser1.emails[0].email,
  414. { entitlement: false }
  415. )
  416. .should.equal(true)
  417. this.addAffiliation
  418. .calledWithMatch(
  419. this.stubbedUser1._id,
  420. this.stubbedUser1.emails[2].email,
  421. { entitlement: true }
  422. )
  423. .should.equal(true)
  424. this.addAffiliation
  425. .calledWithMatch(
  426. this.stubbedUser2._id,
  427. this.stubbedUser2.emails[0].email,
  428. { entitlement: undefined }
  429. )
  430. .should.equal(true)
  431. this.refreshFeatures
  432. .calledWith(this.stubbedUser1._id)
  433. .should.equal(true)
  434. this.refreshFeatures
  435. .calledWith(this.stubbedUser2._id)
  436. .should.equal(true)
  437. this.refreshFeatures.should.have.been.calledTwice
  438. done()
  439. })
  440. })
  441. it('should return errors if last affiliation cannot be added', function (done) {
  442. this.addAffiliation.onCall(2).callsArgWith(3, new Error('error'))
  443. this.InstitutionsManager.affiliateUsers('mit.edu', error => {
  444. expect(error).to.exist
  445. this.getInstitutionUsersByHostname.calledOnce.should.equal(true)
  446. done()
  447. })
  448. })
  449. })
  450. })
  451. })