ProjectListController.test.mjs 29 KB

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