InstitutionsManagerTests.js 14 KB

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