ProjectListController.test.mjs 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059
  1. import { beforeEach, describe, it, expect, vi } from 'vitest'
  2. import sinon from 'sinon'
  3. import mongodb from 'mongodb-legacy'
  4. import Errors from '../../../../app/src/Features/Errors/Errors.js'
  5. import Settings from '@overleaf/settings'
  6. const ObjectId = mongodb.ObjectId
  7. const MODULE_PATH = `${import.meta.dirname}/../../../../app/src/Features/Project/ProjectListController`
  8. // Mock AnalyticsManager as it isn't used in these tests but causes the User model to be imported and redeclares queues
  9. vi.mock('../../../../app/src/Features/Analytics/AnalyticsManager.mjs', () => {
  10. return {
  11. default: {
  12. setUserPropertyForUserInBackground: () => {},
  13. },
  14. }
  15. })
  16. describe('ProjectListController', function () {
  17. beforeEach(async function (ctx) {
  18. ctx.project_id = new ObjectId('abcdefabcdefabcdefabcdef')
  19. ctx.user = {
  20. _id: new ObjectId('123456123456123456123456'),
  21. email: 'test@overleaf.com',
  22. first_name: 'bjkdsjfk',
  23. features: {},
  24. emails: [{ email: 'test@overleaf.com' }],
  25. lastActive: new Date(2),
  26. signUpDate: new Date(1),
  27. lastLoginIp: '111.111.111.112',
  28. ace: {
  29. syntaxValidation: true,
  30. pdfViewer: 'pdfjs',
  31. spellCheckLanguage: 'en',
  32. autoPairDelimiters: true,
  33. autoComplete: true,
  34. fontSize: 12,
  35. theme: 'textmate',
  36. mode: 'none',
  37. },
  38. aiFeatures: { enabled: false },
  39. }
  40. ctx.users = {
  41. 'user-1': {
  42. first_name: 'James',
  43. },
  44. 'user-2': {
  45. first_name: 'Henry',
  46. },
  47. }
  48. ctx.users[ctx.user._id] = ctx.user // Owner
  49. ctx.usersArr = Object.entries(ctx.users).map(([key, value]) => ({
  50. _id: key,
  51. ...value,
  52. }))
  53. ctx.tags = [
  54. { name: 1, project_ids: ['1', '2', '3'] },
  55. { name: 2, project_ids: ['a', '1'] },
  56. { name: 3, project_ids: ['a', 'b', 'c', 'd'] },
  57. ]
  58. ctx.notifications = [
  59. {
  60. _id: '1',
  61. user_id: '2',
  62. templateKey: '3',
  63. messageOpts: '4',
  64. key: '5',
  65. },
  66. ]
  67. ctx.settings = {
  68. ...Settings,
  69. siteUrl: 'https://overleaf.com',
  70. }
  71. ctx.onboardingDataCollection = {
  72. companyDivisionDepartment: '',
  73. companyJobTitle: '',
  74. firstName: 'Dos',
  75. governmentJobTitle: '',
  76. institutionName: '',
  77. lastName: 'Mukasan',
  78. nonprofitDivisionDepartment: '',
  79. nonprofitJobTitle: '',
  80. otherJobTitle: '',
  81. primaryOccupation: 'company',
  82. role: 'conductor',
  83. subjectArea: 'music',
  84. updatedAt: '2025-09-04T12:12:21.628Z',
  85. usedLatex: 'occasionally',
  86. }
  87. ctx.TagsHandler = {
  88. promises: {
  89. getAllTags: sinon.stub().resolves(ctx.tags),
  90. },
  91. }
  92. ctx.NotificationsHandler = {
  93. promises: {
  94. getUserNotifications: sinon.stub().resolves(ctx.notifications),
  95. },
  96. }
  97. ctx.UserModel = {
  98. findById: sinon.stub().resolves(ctx.user),
  99. }
  100. ctx.OnboardingDataCollectionModel = {
  101. findById: sinon.stub().resolves(ctx.onboardingDataCollection),
  102. }
  103. ctx.UserPrimaryEmailCheckHandler = {
  104. requiresPrimaryEmailCheck: sinon.stub().returns(false),
  105. }
  106. ctx.ProjectGetter = {
  107. promises: {
  108. findAllUsersProjects: sinon.stub(),
  109. },
  110. }
  111. ctx.ProjectHelper = {
  112. isArchived: sinon.stub(),
  113. isTrashed: sinon.stub(),
  114. }
  115. ctx.SessionManager = {
  116. getLoggedInUserId: sinon.stub().returns(ctx.user._id),
  117. }
  118. ctx.UserController = {
  119. logout: sinon.stub(),
  120. }
  121. ctx.UserGetter = {
  122. promises: {
  123. getUsers: sinon.stub().resolves(ctx.usersArr),
  124. getUserFullEmails: sinon.stub().resolves([]),
  125. getWritefullData: sinon.stub().resolves({ isPremium: true }),
  126. },
  127. }
  128. ctx.Features = {
  129. hasFeature: sinon.stub(),
  130. }
  131. ctx.Metrics = {
  132. inc: sinon.stub(),
  133. }
  134. ctx.SplitTestHandler = {
  135. promises: {
  136. getAssignment: sinon.stub().resolves({ variant: 'default' }),
  137. featureFlagEnabledForUser: sinon.stub().resolves(false),
  138. hasUserBeenAssignedToVariant: sinon.stub().resolves(false),
  139. },
  140. }
  141. ctx.SplitTestSessionHandler = {
  142. promises: {
  143. sessionMaintenance: sinon.stub().resolves(),
  144. },
  145. }
  146. ctx.SubscriptionViewModelBuilder = {
  147. promises: {
  148. getUsersSubscriptionDetails: sinon.stub().resolves({
  149. bestSubscription: { type: 'free' },
  150. individualSubscription: null,
  151. memberGroupSubscriptions: [],
  152. managedGroupSubscriptions: [],
  153. }),
  154. },
  155. }
  156. ctx.SurveyHandler = {
  157. promises: {
  158. getSurvey: sinon.stub().resolves({}),
  159. },
  160. }
  161. ctx.NotificationBuilder = {
  162. promises: {
  163. ipMatcherAffiliation: sinon.stub().returns({ create: sinon.stub() }),
  164. },
  165. }
  166. ctx.GeoIpLookup = {
  167. promises: {
  168. getCurrencyCode: sinon.stub().resolves({
  169. countryCode: 'US',
  170. currencyCode: 'USD',
  171. }),
  172. },
  173. }
  174. ctx.TutorialHandler = {
  175. getInactiveTutorials: sinon.stub().returns([]),
  176. }
  177. ctx.Modules = {
  178. promises: {
  179. hooks: {
  180. fire: sinon.stub().resolves([]),
  181. },
  182. },
  183. }
  184. ctx.PermissionsManager = {
  185. promises: {
  186. checkUserPermissions: sinon.stub().resolves(true),
  187. },
  188. }
  189. ctx.SubscriptionLocator = {
  190. promises: {
  191. getUsersSubscription: sinon.stub().resolves({}),
  192. },
  193. }
  194. vi.doMock('mongodb-legacy', () => ({
  195. default: { ObjectId },
  196. }))
  197. vi.doMock('@overleaf/settings', () => ({
  198. default: ctx.settings,
  199. }))
  200. vi.doMock('@overleaf/metrics', () => ({
  201. default: ctx.Metrics,
  202. }))
  203. vi.doMock(
  204. '../../../../app/src/Features/SplitTests/SplitTestHandler',
  205. () => ({
  206. default: ctx.SplitTestHandler,
  207. })
  208. )
  209. vi.doMock(
  210. '../../../../app/src/Features/SplitTests/SplitTestSessionHandler',
  211. () => ({
  212. default: ctx.SplitTestSessionHandler,
  213. })
  214. )
  215. vi.doMock('../../../../app/src/Features/User/UserController', () => ({
  216. default: ctx.UserController,
  217. }))
  218. vi.doMock('../../../../app/src/Features/Project/ProjectHelper', () => ({
  219. default: ctx.ProjectHelper,
  220. }))
  221. vi.doMock('../../../../app/src/Features/Tags/TagsHandler', () => ({
  222. default: ctx.TagsHandler,
  223. }))
  224. vi.doMock(
  225. '../../../../app/src/Features/Notifications/NotificationsHandler',
  226. () => ({
  227. default: ctx.NotificationsHandler,
  228. })
  229. )
  230. vi.doMock('../../../../app/src/models/User', () => ({
  231. User: ctx.UserModel,
  232. }))
  233. vi.doMock('../../../../app/src/models/OnboardingDataCollection', () => ({
  234. OnboardingDataCollection: ctx.OnboardingDataCollectionModel,
  235. }))
  236. vi.doMock('../../../../app/src/Features/Project/ProjectGetter', () => ({
  237. default: ctx.ProjectGetter,
  238. }))
  239. vi.doMock(
  240. '../../../../app/src/Features/Authentication/SessionManager',
  241. () => ({
  242. default: ctx.SessionManager,
  243. })
  244. )
  245. vi.doMock('../../../../app/src/infrastructure/Features', () => ({
  246. default: ctx.Features,
  247. }))
  248. vi.doMock('../../../../app/src/Features/User/UserGetter', () => ({
  249. default: ctx.UserGetter,
  250. }))
  251. vi.doMock(
  252. '../../../../app/src/Features/Subscription/SubscriptionViewModelBuilder',
  253. () => ({
  254. default: ctx.SubscriptionViewModelBuilder,
  255. })
  256. )
  257. vi.doMock('../../../../app/src/infrastructure/Modules', () => ({
  258. default: ctx.Modules,
  259. }))
  260. vi.doMock('../../../../app/src/Features/Survey/SurveyHandler', () => ({
  261. default: ctx.SurveyHandler,
  262. }))
  263. vi.doMock(
  264. '../../../../app/src/Features/User/UserPrimaryEmailCheckHandler',
  265. () => ({
  266. default: ctx.UserPrimaryEmailCheckHandler,
  267. })
  268. )
  269. vi.doMock(
  270. '../../../../app/src/Features/Notifications/NotificationsBuilder',
  271. () => ({
  272. default: ctx.NotificationBuilder,
  273. })
  274. )
  275. vi.doMock('../../../../app/src/infrastructure/GeoIpLookup', () => ({
  276. default: ctx.GeoIpLookup,
  277. }))
  278. vi.doMock('../../../../app/src/Features/Tutorial/TutorialHandler', () => ({
  279. default: ctx.TutorialHandler,
  280. }))
  281. vi.doMock(
  282. '../../../../app/src/Features/Authorization/PermissionsManager',
  283. () => ({
  284. default: ctx.PermissionsManager,
  285. })
  286. )
  287. vi.doMock(
  288. '../../../../app/src/Features/Subscription/SubscriptionLocator',
  289. () => ({
  290. default: ctx.SubscriptionLocator,
  291. })
  292. )
  293. ctx.ProjectListController = (await import(MODULE_PATH)).default
  294. ctx.req = {
  295. query: {},
  296. params: {
  297. Project_id: ctx.project_id,
  298. },
  299. headers: {},
  300. session: {
  301. user: ctx.user,
  302. },
  303. body: {},
  304. i18n: {
  305. translate() {},
  306. },
  307. }
  308. ctx.res = {}
  309. })
  310. describe('projectListPage', function () {
  311. beforeEach(function (ctx) {
  312. ctx.projects = [
  313. { _id: 1, lastUpdated: new Date(1), owner_ref: 'user-1' },
  314. {
  315. _id: 2,
  316. lastUpdated: new Date(2),
  317. owner_ref: 'user-2',
  318. lastUpdatedBy: 'user-1',
  319. },
  320. ]
  321. ctx.readAndWrite = [
  322. { _id: 5, lastUpdated: new Date(5), owner_ref: 'user-1' },
  323. ]
  324. ctx.readOnly = [{ _id: 3, lastUpdated: new Date(3), owner_ref: 'user-1' }]
  325. ctx.tokenReadAndWrite = [
  326. { _id: 6, lastUpdated: new Date(5), owner_ref: 'user-4' },
  327. ]
  328. ctx.tokenReadOnly = [
  329. { _id: 7, lastUpdated: new Date(4), owner_ref: 'user-5' },
  330. ]
  331. ctx.review = [{ _id: 8, lastUpdated: new Date(4), owner_ref: 'user-6' }]
  332. ctx.allProjects = {
  333. owned: ctx.projects,
  334. readAndWrite: ctx.readAndWrite,
  335. readOnly: ctx.readOnly,
  336. tokenReadAndWrite: ctx.tokenReadAndWrite,
  337. tokenReadOnly: ctx.tokenReadOnly,
  338. review: ctx.review,
  339. }
  340. ctx.ProjectGetter.promises.findAllUsersProjects.resolves(ctx.allProjects)
  341. })
  342. it('should render the project/list-react page', async function (ctx) {
  343. ctx.res.render = (pageName, opts) => {
  344. pageName.should.equal('project/list-react')
  345. }
  346. await ctx.ProjectListController.projectListPage(ctx.req, ctx.res)
  347. })
  348. it('should invoke the session maintenance', async function (ctx) {
  349. ctx.Features.hasFeature.withArgs('saas').returns(true)
  350. ctx.res.render = () => {
  351. ctx.SplitTestSessionHandler.promises.sessionMaintenance.should.have.been.calledWith(
  352. ctx.req,
  353. ctx.user
  354. )
  355. }
  356. await ctx.ProjectListController.projectListPage(ctx.req, ctx.res)
  357. })
  358. it('should send the tags', async function (ctx) {
  359. ctx.res.render = (pageName, opts) => {
  360. opts.tags.length.should.equal(ctx.tags.length)
  361. }
  362. await ctx.ProjectListController.projectListPage(ctx.req, ctx.res)
  363. })
  364. it('should create trigger ip matcher notifications', async function (ctx) {
  365. ctx.settings.overleaf = true
  366. ctx.req.ip = '111.111.111.111'
  367. ctx.res.render = (pageName, opts) => {
  368. ctx.NotificationBuilder.promises.ipMatcherAffiliation.called.should.equal(
  369. true
  370. )
  371. }
  372. await ctx.ProjectListController.projectListPage(ctx.req, ctx.res)
  373. })
  374. it('should send the projects', async function (ctx) {
  375. ctx.res.render = (pageName, opts) => {
  376. opts.prefetchedProjectsBlob.projects.length.should.equal(
  377. ctx.projects.length +
  378. ctx.readAndWrite.length +
  379. ctx.readOnly.length +
  380. ctx.tokenReadAndWrite.length +
  381. ctx.tokenReadOnly.length +
  382. ctx.review.length
  383. )
  384. }
  385. await ctx.ProjectListController.projectListPage(ctx.req, ctx.res)
  386. })
  387. it('should send the user', async function (ctx) {
  388. ctx.res.render = (pageName, opts) => {
  389. opts.user.should.deep.equal(ctx.user)
  390. }
  391. await ctx.ProjectListController.projectListPage(ctx.req, ctx.res)
  392. })
  393. it('should inject the users', async function (ctx) {
  394. ctx.res.render = (pageName, opts) => {
  395. const projects = opts.prefetchedProjectsBlob.projects
  396. projects
  397. .filter(p => p.id === '1')[0]
  398. .owner.firstName.should.equal(
  399. ctx.users[ctx.projects.filter(p => p._id === 1)[0].owner_ref]
  400. .first_name
  401. )
  402. projects
  403. .filter(p => p.id === '2')[0]
  404. .owner.firstName.should.equal(
  405. ctx.users[ctx.projects.filter(p => p._id === 2)[0].owner_ref]
  406. .first_name
  407. )
  408. projects
  409. .filter(p => p.id === '2')[0]
  410. .lastUpdatedBy.firstName.should.equal(
  411. ctx.users[ctx.projects.filter(p => p._id === 2)[0].lastUpdatedBy]
  412. .first_name
  413. )
  414. }
  415. await ctx.ProjectListController.projectListPage(ctx.req, ctx.res)
  416. })
  417. it("should send the user's best subscription when saas feature present", async function (ctx) {
  418. ctx.Features.hasFeature.withArgs('saas').returns(true)
  419. ctx.res.render = (pageName, opts) => {
  420. expect(opts.usersBestSubscription).to.deep.include({ type: 'free' })
  421. }
  422. await ctx.ProjectListController.projectListPage(ctx.req, ctx.res)
  423. })
  424. it('should not return a best subscription without saas feature', async function (ctx) {
  425. ctx.Features.hasFeature.withArgs('saas').returns(false)
  426. ctx.res.render = (pageName, opts) => {
  427. expect(opts.usersBestSubscription).to.be.undefined
  428. }
  429. await ctx.ProjectListController.projectListPage(ctx.req, ctx.res)
  430. })
  431. it('should show INR Banner for Indian users with free account', async function (ctx) {
  432. // usersBestSubscription is only available when saas feature is present
  433. ctx.Features.hasFeature.withArgs('saas').returns(true)
  434. ctx.SubscriptionViewModelBuilder.promises.getUsersSubscriptionDetails.resolves(
  435. {
  436. memberGroupSubscriptions: [],
  437. managedGroupSubscriptions: [],
  438. bestSubscription: {
  439. type: 'free',
  440. },
  441. }
  442. )
  443. ctx.GeoIpLookup.promises.getCurrencyCode.resolves({
  444. countryCode: 'IN',
  445. })
  446. ctx.res.render = (pageName, opts) => {
  447. expect(opts.showInrGeoBanner).to.be.true
  448. }
  449. await ctx.ProjectListController.projectListPage(ctx.req, ctx.res)
  450. })
  451. it('should not show INR Banner for Indian users with premium account', async function (ctx) {
  452. // usersBestSubscription is only available when saas feature is present
  453. ctx.Features.hasFeature.withArgs('saas').returns(true)
  454. ctx.SubscriptionViewModelBuilder.promises.getUsersSubscriptionDetails.resolves(
  455. {
  456. memberGroupSubscriptions: [],
  457. managedGroupSubscriptions: [],
  458. bestSubscription: {
  459. type: 'individual',
  460. },
  461. }
  462. )
  463. ctx.GeoIpLookup.promises.getCurrencyCode.resolves({
  464. countryCode: 'IN',
  465. })
  466. ctx.res.render = (pageName, opts) => {
  467. expect(opts.showInrGeoBanner).to.be.false
  468. }
  469. await ctx.ProjectListController.projectListPage(ctx.req, ctx.res)
  470. })
  471. it('should redirect to domain capture page', async function (ctx) {
  472. ctx.Features.hasFeature.withArgs('saas').returns(true)
  473. ctx.SplitTestHandler.promises.getAssignment
  474. .withArgs(ctx.req, ctx.res, 'domain-capture-redirect')
  475. .resolves({ variant: 'enabled' })
  476. ctx.Modules.promises.hooks.fire
  477. .withArgs('findDomainCaptureGroupsUserCouldBePartOf', ctx.user._id)
  478. .resolves([
  479. [
  480. {
  481. subscription: { managedUsersEnabled: true },
  482. },
  483. ],
  484. ])
  485. ctx.res.redirect = url => {
  486. url.should.equal('/domain-capture')
  487. }
  488. await ctx.ProjectListController.projectListPage(ctx.req, ctx.res)
  489. })
  490. describe('when user linked to SSO', function () {
  491. const linkedEmail = 'picard@starfleet.com'
  492. const universityName = 'Starfleet'
  493. const notificationData = {
  494. email: linkedEmail,
  495. institutionName: universityName,
  496. }
  497. beforeEach(function (ctx) {
  498. ctx.Features.hasFeature.withArgs('saml').returns(true)
  499. ctx.req.session.saml = {
  500. institutionEmail: linkedEmail,
  501. linked: {
  502. universityName,
  503. },
  504. }
  505. })
  506. it('should render with Commons template when Commons was linked', async function (ctx) {
  507. ctx.res.render = (pageName, opts) => {
  508. expect(opts.notificationsInstitution).to.deep.equal([
  509. Object.assign(
  510. { templateKey: 'notification_institution_sso_linked' },
  511. notificationData
  512. ),
  513. ])
  514. }
  515. await ctx.ProjectListController.projectListPage(ctx.req, ctx.res)
  516. })
  517. describe('when via domain capture', function () {
  518. beforeEach(function (ctx) {
  519. ctx.req.session.saml.domainCaptureEnabled = true
  520. })
  521. it('should render with group template', async function (ctx) {
  522. ctx.res.render = (pageName, opts) => {
  523. expect(opts.notificationsInstitution).to.deep.equal([
  524. Object.assign(
  525. { templateKey: 'notification_group_sso_linked' },
  526. notificationData
  527. ),
  528. ])
  529. }
  530. await ctx.ProjectListController.projectListPage(ctx.req, ctx.res)
  531. })
  532. describe('user created via domain capture and group is managed', function () {
  533. beforeEach(function (ctx) {
  534. ctx.req.session.saml.userCreatedViaDomainCapture = true
  535. })
  536. it('should render with notification_group_sso_linked', async function (ctx) {
  537. ctx.res.render = (pageName, opts) => {
  538. expect(opts.notificationsInstitution).to.deep.equal([
  539. Object.assign(
  540. {
  541. templateKey: 'notification_group_sso_linked',
  542. },
  543. notificationData
  544. ),
  545. ])
  546. }
  547. await ctx.ProjectListController.projectListPage(ctx.req, ctx.res)
  548. })
  549. it('should render with notification_account_created_via_group_domain_capture_and_managed_users_enabled when managed user is enabled', async function (ctx) {
  550. ctx.req.session.saml.managedUsersEnabled = true
  551. ctx.res.render = (pageName, opts) => {
  552. expect(opts.notificationsInstitution).to.deep.equal([
  553. Object.assign(
  554. {
  555. templateKey:
  556. 'notification_account_created_via_group_domain_capture_and_managed_users_enabled',
  557. },
  558. notificationData
  559. ),
  560. ])
  561. }
  562. await ctx.ProjectListController.projectListPage(ctx.req, ctx.res)
  563. })
  564. })
  565. })
  566. })
  567. describe('With Institution SSO feature', function () {
  568. beforeEach(function (ctx) {
  569. ctx.institutionEmail = 'test@overleaf.com'
  570. ctx.institutionName = 'Overleaf'
  571. ctx.Features.hasFeature.withArgs('saml').returns(true)
  572. ctx.Features.hasFeature.withArgs('affiliations').returns(true)
  573. ctx.Features.hasFeature.withArgs('saas').returns(true)
  574. })
  575. it('should show institution SSO available notification for confirmed domains', async function (ctx) {
  576. ctx.UserGetter.promises.getUserFullEmails.resolves([
  577. {
  578. email: 'test@overleaf.com',
  579. affiliation: {
  580. institution: {
  581. id: 1,
  582. confirmed: true,
  583. name: 'Overleaf',
  584. ssoBeta: false,
  585. ssoEnabled: true,
  586. },
  587. },
  588. },
  589. ])
  590. ctx.res.render = (pageName, opts) => {
  591. expect(opts.notificationsInstitution).to.deep.include({
  592. email: ctx.institutionEmail,
  593. institutionId: 1,
  594. institutionName: ctx.institutionName,
  595. templateKey: 'notification_institution_sso_available',
  596. })
  597. }
  598. await ctx.ProjectListController.projectListPage(ctx.req, ctx.res)
  599. })
  600. it('should show a linked notification', async function (ctx) {
  601. ctx.req.session.saml = {
  602. institutionEmail: ctx.institutionEmail,
  603. linked: {
  604. hasEntitlement: false,
  605. universityName: ctx.institutionName,
  606. },
  607. }
  608. ctx.res.render = (pageName, opts) => {
  609. expect(opts.notificationsInstitution).to.deep.include({
  610. email: ctx.institutionEmail,
  611. institutionName: ctx.institutionName,
  612. templateKey: 'notification_institution_sso_linked',
  613. })
  614. }
  615. await ctx.ProjectListController.projectListPage(ctx.req, ctx.res)
  616. })
  617. it('should show a group linked notification when domain capture enabled', async function (ctx) {
  618. ctx.req.session.saml = {
  619. institutionEmail: ctx.institutionEmail,
  620. linked: {
  621. hasEntitlement: false,
  622. universityName: ctx.institutionName,
  623. },
  624. domainCaptureEnabled: true,
  625. }
  626. ctx.res.render = (pageName, opts) => {
  627. expect(opts.notificationsInstitution).to.deep.include({
  628. email: ctx.institutionEmail,
  629. institutionName: ctx.institutionName,
  630. templateKey: 'notification_group_sso_linked',
  631. })
  632. }
  633. await ctx.ProjectListController.projectListPage(ctx.req, ctx.res)
  634. })
  635. it('should show a success notification when joining group via domain capture page', async function (ctx) {
  636. ctx.req.session.saml = {
  637. linkedGroup: true,
  638. universityName: ctx.institutionName,
  639. domainCaptureJoin: true,
  640. }
  641. ctx.res.render = (pageName, opts) => {
  642. expect(opts).to.deep.include({
  643. groupSsoSetupSuccess: true,
  644. joinedGroupName: ctx.institutionName,
  645. viaDomainCapture: true,
  646. })
  647. }
  648. await ctx.ProjectListController.projectListPage(ctx.req, ctx.res)
  649. })
  650. it('should show a linked another email notification', async function (ctx) {
  651. // when they request to link an email but the institution returns
  652. // a different email
  653. ctx.res.render = (pageName, opts) => {
  654. expect(opts.notificationsInstitution).to.deep.include({
  655. institutionEmail: ctx.institutionEmail,
  656. requestedEmail: 'requested@overleaf.com',
  657. templateKey: 'notification_institution_sso_non_canonical',
  658. })
  659. }
  660. ctx.req.session.saml = {
  661. emailNonCanonical: ctx.institutionEmail,
  662. institutionEmail: ctx.institutionEmail,
  663. requestedEmail: 'requested@overleaf.com',
  664. linked: {
  665. hasEntitlement: false,
  666. universityName: ctx.institutionName,
  667. },
  668. }
  669. await ctx.ProjectListController.projectListPage(ctx.req, ctx.res)
  670. })
  671. it('should show a notification when intent was to register via SSO but account existed', async function (ctx) {
  672. ctx.res.render = (pageName, opts) => {
  673. expect(opts.notificationsInstitution).to.deep.include({
  674. email: ctx.institutionEmail,
  675. templateKey: 'notification_institution_sso_already_registered',
  676. })
  677. }
  678. ctx.req.session.saml = {
  679. institutionEmail: ctx.institutionEmail,
  680. linked: {
  681. hasEntitlement: false,
  682. universityName: 'Overleaf',
  683. },
  684. registerIntercept: {
  685. id: 1,
  686. name: 'Example University',
  687. },
  688. }
  689. await ctx.ProjectListController.projectListPage(ctx.req, ctx.res)
  690. })
  691. it('should not show a register notification if the flow was abandoned', async function (ctx) {
  692. // could initially start to register with an SSO email and then
  693. // abandon flow and login with an existing non-institution SSO email
  694. ctx.res.render = (pageName, opts) => {
  695. expect(opts.notificationsInstitution).to.deep.not.include({
  696. email: 'test@overleaf.com',
  697. templateKey: 'notification_institution_sso_already_registered',
  698. })
  699. }
  700. ctx.req.session.saml = {
  701. registerIntercept: {
  702. id: 1,
  703. name: 'Example University',
  704. },
  705. }
  706. await ctx.ProjectListController.projectListPage(ctx.req, ctx.res)
  707. })
  708. it('should show error notification', async function (ctx) {
  709. ctx.res.render = (pageName, opts) => {
  710. expect(opts.notificationsInstitution.length).to.equal(1)
  711. expect(opts.notificationsInstitution[0].templateKey).to.equal(
  712. 'notification_institution_sso_error'
  713. )
  714. expect(opts.notificationsInstitution[0].error).to.be.instanceof(
  715. Errors.SAMLAlreadyLinkedError
  716. )
  717. }
  718. ctx.req.session.saml = {
  719. institutionEmail: ctx.institutionEmail,
  720. error: new Errors.SAMLAlreadyLinkedError(),
  721. }
  722. await ctx.ProjectListController.projectListPage(ctx.req, ctx.res)
  723. })
  724. describe('for an unconfirmed domain for an SSO institution', function () {
  725. beforeEach(function (ctx) {
  726. ctx.UserGetter.promises.getUserFullEmails.resolves([
  727. {
  728. email: 'test@overleaf-uncofirmed.com',
  729. affiliation: {
  730. institution: {
  731. id: 1,
  732. confirmed: false,
  733. name: 'Overleaf',
  734. ssoBeta: false,
  735. ssoEnabled: true,
  736. },
  737. },
  738. },
  739. ])
  740. })
  741. it('should not show institution SSO available notification', async function (ctx) {
  742. ctx.res.render = (pageName, opts) => {
  743. expect(opts.notificationsInstitution.length).to.equal(0)
  744. }
  745. await ctx.ProjectListController.projectListPage(ctx.req, ctx.res)
  746. })
  747. })
  748. describe('when linking/logging in initiated on institution side', function () {
  749. it('should not show a linked another email notification', async function (ctx) {
  750. // this is only used when initated on Overleaf,
  751. // because we keep track of the requested email they tried to link
  752. ctx.res.render = (pageName, opts) => {
  753. expect(opts.notificationsInstitution).to.not.deep.include({
  754. institutionEmail: ctx.institutionEmail,
  755. requestedEmail: undefined,
  756. templateKey: 'notification_institution_sso_non_canonical',
  757. })
  758. }
  759. ctx.req.session.saml = {
  760. emailNonCanonical: ctx.institutionEmail,
  761. institutionEmail: ctx.institutionEmail,
  762. linked: {
  763. hasEntitlement: false,
  764. universityName: ctx.institutionName,
  765. },
  766. }
  767. await ctx.ProjectListController.projectListPage(ctx.req, ctx.res)
  768. })
  769. })
  770. describe('Institution with SSO beta testable', function () {
  771. beforeEach(function (ctx) {
  772. ctx.UserGetter.promises.getUserFullEmails.resolves([
  773. {
  774. email: 'beta@beta.com',
  775. affiliation: {
  776. institution: {
  777. id: 2,
  778. confirmed: true,
  779. name: 'Beta University',
  780. ssoBeta: true,
  781. ssoEnabled: false,
  782. },
  783. },
  784. },
  785. ])
  786. })
  787. it('should show institution SSO available notification when on a beta testing session', async function (ctx) {
  788. ctx.req.session.samlBeta = true
  789. ctx.res.render = (pageName, opts) => {
  790. expect(opts.notificationsInstitution).to.deep.include({
  791. email: 'beta@beta.com',
  792. institutionId: 2,
  793. institutionName: 'Beta University',
  794. templateKey: 'notification_institution_sso_available',
  795. })
  796. }
  797. await ctx.ProjectListController.projectListPage(ctx.req, ctx.res)
  798. })
  799. it('should not show institution SSO available notification when not on a beta testing session', async function (ctx) {
  800. ctx.req.session.samlBeta = false
  801. ctx.res.render = (pageName, opts) => {
  802. expect(opts.notificationsInstitution).to.deep.not.include({
  803. email: 'test@overleaf.com',
  804. institutionId: 1,
  805. institutionName: 'Overleaf',
  806. templateKey: 'notification_institution_sso_available',
  807. })
  808. }
  809. await ctx.ProjectListController.projectListPage(ctx.req, ctx.res)
  810. })
  811. })
  812. describe('group domain capture enabled for domain', function () {
  813. it('does not show institution SSO available notification', async function (ctx) {
  814. ctx.UserGetter.promises.getUserFullEmails.resolves([
  815. {
  816. email: 'test@overleaf.com',
  817. affiliation: {
  818. group: { domainCaptureEnabled: true },
  819. institution: {
  820. id: 1,
  821. confirmed: true,
  822. name: 'Overleaf',
  823. ssoBeta: false,
  824. ssoEnabled: true,
  825. },
  826. },
  827. },
  828. ])
  829. ctx.res.render = (pageName, opts) => {
  830. expect(opts.notificationsInstitution).to.deep.equal([])
  831. }
  832. await ctx.ProjectListController.projectListPage(ctx.req, ctx.res)
  833. })
  834. })
  835. })
  836. describe('Without Institution SSO feature', function () {
  837. beforeEach(function (ctx) {
  838. ctx.Features.hasFeature.withArgs('saml').returns(false)
  839. })
  840. it('should not show institution sso available notification', async function (ctx) {
  841. ctx.res.render = (pageName, opts) => {
  842. expect(opts.notificationsInstitution).to.deep.not.include({
  843. email: 'test@overleaf.com',
  844. institutionId: 1,
  845. institutionName: 'Overleaf',
  846. templateKey: 'notification_institution_sso_available',
  847. })
  848. }
  849. await ctx.ProjectListController.projectListPage(ctx.req, ctx.res)
  850. })
  851. })
  852. describe('enterprise banner', function () {
  853. beforeEach(function (ctx) {
  854. ctx.Features.hasFeature.withArgs('saas').returns(true)
  855. ctx.SubscriptionViewModelBuilder.promises.getUsersSubscriptionDetails.resolves(
  856. { memberGroupSubscriptions: [], managedGroupSubscriptions: [] }
  857. )
  858. ctx.UserGetter.promises.getUserFullEmails.resolves([
  859. {
  860. email: 'test@test-domain.com',
  861. },
  862. ])
  863. })
  864. describe('normal enterprise banner', function () {
  865. it('shows banner', async function (ctx) {
  866. ctx.res.render = (pageName, opts) => {
  867. expect(opts.showGroupsAndEnterpriseBanner).to.be.true
  868. }
  869. await ctx.ProjectListController.projectListPage(ctx.req, ctx.res)
  870. })
  871. it('does not show banner if user is part of any affiliation', async function (ctx) {
  872. ctx.UserGetter.promises.getUserFullEmails.resolves([
  873. {
  874. email: 'test@overleaf.com',
  875. affiliation: {
  876. licence: 'pro_plus',
  877. institution: {
  878. id: 1,
  879. confirmed: true,
  880. name: 'Overleaf',
  881. ssoBeta: false,
  882. ssoEnabled: true,
  883. },
  884. },
  885. },
  886. ])
  887. ctx.res.render = (pageName, opts) => {
  888. expect(opts.showGroupsAndEnterpriseBanner).to.be.false
  889. }
  890. await ctx.ProjectListController.projectListPage(ctx.req, ctx.res)
  891. })
  892. it('does not show banner if user is part of any group subscription', async function (ctx) {
  893. ctx.SubscriptionViewModelBuilder.promises.getUsersSubscriptionDetails.resolves(
  894. { memberGroupSubscriptions: [{}] }
  895. )
  896. ctx.res.render = (pageName, opts) => {
  897. expect(opts.showGroupsAndEnterpriseBanner).to.be.false
  898. }
  899. await ctx.ProjectListController.projectListPage(ctx.req, ctx.res)
  900. })
  901. it('have a banner variant of "FOMO" or "on-premise"', async function (ctx) {
  902. ctx.res.render = (pageName, opts) => {
  903. expect(opts.groupsAndEnterpriseBannerVariant).to.be.oneOf([
  904. 'FOMO',
  905. 'on-premise',
  906. ])
  907. }
  908. await ctx.ProjectListController.projectListPage(ctx.req, ctx.res)
  909. })
  910. })
  911. describe('US government enterprise banner', function () {
  912. it('does not show enterprise banner if US government enterprise banner is shown', async function (ctx) {
  913. const emails = [
  914. {
  915. email: 'test@test.mil',
  916. confirmedAt: new Date('2024-01-01'),
  917. },
  918. ]
  919. ctx.UserGetter.promises.getUserFullEmails.resolves(emails)
  920. ctx.Modules.promises.hooks.fire
  921. .withArgs('getUSGovBanner', emails, false, [])
  922. .resolves([
  923. {
  924. showUSGovBanner: true,
  925. usGovBannerVariant: 'variant',
  926. },
  927. ])
  928. ctx.res.render = (pageName, opts) => {
  929. expect(opts.showGroupsAndEnterpriseBanner).to.be.false
  930. expect(opts.showUSGovBanner).to.be.true
  931. }
  932. await ctx.ProjectListController.projectListPage(ctx.req, ctx.res)
  933. })
  934. })
  935. })
  936. })
  937. describe('projectListReactPage with duplicate projects', function () {
  938. beforeEach(function (ctx) {
  939. ctx.projects = [
  940. { _id: 1, lastUpdated: new Date(1), owner_ref: 'user-1' },
  941. { _id: 2, lastUpdated: new Date(2), owner_ref: 'user-2' },
  942. ]
  943. ctx.readAndWrite = [
  944. { _id: 5, lastUpdated: new Date(5), owner_ref: 'user-1' },
  945. ]
  946. ctx.readOnly = [{ _id: 3, lastUpdated: new Date(3), owner_ref: 'user-1' }]
  947. ctx.tokenReadAndWrite = [
  948. { _id: 6, lastUpdated: new Date(5), owner_ref: 'user-4' },
  949. ]
  950. ctx.tokenReadOnly = [
  951. { _id: 6, lastUpdated: new Date(5), owner_ref: 'user-4' }, // Also in tokenReadAndWrite
  952. { _id: 7, lastUpdated: new Date(4), owner_ref: 'user-5' },
  953. ]
  954. ctx.review = [{ _id: 8, lastUpdated: new Date(5), owner_ref: 'user-6' }]
  955. ctx.allProjects = {
  956. owned: ctx.projects,
  957. readAndWrite: ctx.readAndWrite,
  958. readOnly: ctx.readOnly,
  959. tokenReadAndWrite: ctx.tokenReadAndWrite,
  960. tokenReadOnly: ctx.tokenReadOnly,
  961. review: ctx.review,
  962. }
  963. ctx.ProjectGetter.promises.findAllUsersProjects.resolves(ctx.allProjects)
  964. })
  965. it('should render the project/list-react page', async function (ctx) {
  966. ctx.res.render = (pageName, opts) => {
  967. pageName.should.equal('project/list-react')
  968. }
  969. await ctx.ProjectListController.projectListPage(ctx.req, ctx.res)
  970. })
  971. it('should omit one of the projects', async function (ctx) {
  972. ctx.res.render = (pageName, opts) => {
  973. opts.prefetchedProjectsBlob.projects.length.should.equal(
  974. ctx.projects.length +
  975. ctx.readAndWrite.length +
  976. ctx.readOnly.length +
  977. ctx.tokenReadAndWrite.length +
  978. ctx.tokenReadOnly.length +
  979. ctx.review.length -
  980. 1
  981. )
  982. }
  983. await ctx.ProjectListController.projectListPage(ctx.req, ctx.res)
  984. })
  985. })
  986. })