ProjectListController.test.mjs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918
  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: new Date(1), owner_ref: 'user-1' },
  270. {
  271. _id: 2,
  272. lastUpdated: new Date(2),
  273. owner_ref: 'user-2',
  274. lastUpdatedBy: 'user-1',
  275. },
  276. ]
  277. ctx.readAndWrite = [
  278. { _id: 5, lastUpdated: new Date(5), owner_ref: 'user-1' },
  279. ]
  280. ctx.readOnly = [{ _id: 3, lastUpdated: new Date(3), owner_ref: 'user-1' }]
  281. ctx.tokenReadAndWrite = [
  282. { _id: 6, lastUpdated: new Date(5), owner_ref: 'user-4' },
  283. ]
  284. ctx.tokenReadOnly = [
  285. { _id: 7, lastUpdated: new Date(4), owner_ref: 'user-5' },
  286. ]
  287. ctx.review = [{ _id: 8, lastUpdated: new Date(4), owner_ref: 'user-6' }]
  288. ctx.allProjects = {
  289. owned: ctx.projects,
  290. readAndWrite: ctx.readAndWrite,
  291. readOnly: ctx.readOnly,
  292. tokenReadAndWrite: ctx.tokenReadAndWrite,
  293. tokenReadOnly: ctx.tokenReadOnly,
  294. review: ctx.review,
  295. }
  296. ctx.ProjectGetter.promises.findAllUsersProjects.resolves(ctx.allProjects)
  297. })
  298. it('should render the project/list-react page', async function (ctx) {
  299. await new Promise(resolve => {
  300. ctx.res.render = (pageName, opts) => {
  301. pageName.should.equal('project/list-react')
  302. resolve()
  303. }
  304. ctx.ProjectListController.projectListPage(ctx.req, ctx.res)
  305. })
  306. })
  307. it('should invoke the session maintenance', async function (ctx) {
  308. await new Promise(resolve => {
  309. ctx.Features.hasFeature.withArgs('saas').returns(true)
  310. ctx.res.render = () => {
  311. ctx.SplitTestSessionHandler.promises.sessionMaintenance.should.have.been.calledWith(
  312. ctx.req,
  313. ctx.user
  314. )
  315. resolve()
  316. }
  317. ctx.ProjectListController.projectListPage(ctx.req, ctx.res)
  318. })
  319. })
  320. it('should send the tags', async function (ctx) {
  321. await new Promise(resolve => {
  322. ctx.res.render = (pageName, opts) => {
  323. opts.tags.length.should.equal(ctx.tags.length)
  324. resolve()
  325. }
  326. ctx.ProjectListController.projectListPage(ctx.req, ctx.res)
  327. })
  328. })
  329. it('should create trigger ip matcher notifications', async function (ctx) {
  330. await new Promise(resolve => {
  331. ctx.settings.overleaf = true
  332. ctx.req.ip = '111.111.111.111'
  333. ctx.res.render = (pageName, opts) => {
  334. ctx.NotificationBuilder.promises.ipMatcherAffiliation.called.should.equal(
  335. true
  336. )
  337. resolve()
  338. }
  339. ctx.ProjectListController.projectListPage(ctx.req, ctx.res)
  340. })
  341. })
  342. it('should send the projects', async function (ctx) {
  343. await new Promise(resolve => {
  344. ctx.res.render = (pageName, opts) => {
  345. opts.prefetchedProjectsBlob.projects.length.should.equal(
  346. ctx.projects.length +
  347. ctx.readAndWrite.length +
  348. ctx.readOnly.length +
  349. ctx.tokenReadAndWrite.length +
  350. ctx.tokenReadOnly.length +
  351. ctx.review.length
  352. )
  353. resolve()
  354. }
  355. ctx.ProjectListController.projectListPage(ctx.req, ctx.res)
  356. })
  357. })
  358. it('should send the user', async function (ctx) {
  359. await new Promise(resolve => {
  360. ctx.res.render = (pageName, opts) => {
  361. opts.user.should.deep.equal(ctx.user)
  362. resolve()
  363. }
  364. ctx.ProjectListController.projectListPage(ctx.req, ctx.res)
  365. })
  366. })
  367. it('should inject the users', async function (ctx) {
  368. await new Promise(resolve => {
  369. ctx.res.render = (pageName, opts) => {
  370. const projects = opts.prefetchedProjectsBlob.projects
  371. projects
  372. .filter(p => p.id === '1')[0]
  373. .owner.firstName.should.equal(
  374. ctx.users[ctx.projects.filter(p => p._id === 1)[0].owner_ref]
  375. .first_name
  376. )
  377. projects
  378. .filter(p => p.id === '2')[0]
  379. .owner.firstName.should.equal(
  380. ctx.users[ctx.projects.filter(p => p._id === 2)[0].owner_ref]
  381. .first_name
  382. )
  383. projects
  384. .filter(p => p.id === '2')[0]
  385. .lastUpdatedBy.firstName.should.equal(
  386. ctx.users[ctx.projects.filter(p => p._id === 2)[0].lastUpdatedBy]
  387. .first_name
  388. )
  389. resolve()
  390. }
  391. ctx.ProjectListController.projectListPage(ctx.req, ctx.res)
  392. })
  393. })
  394. it("should send the user's best subscription when saas feature present", async function (ctx) {
  395. await new Promise(resolve => {
  396. ctx.Features.hasFeature.withArgs('saas').returns(true)
  397. ctx.res.render = (pageName, opts) => {
  398. expect(opts.usersBestSubscription).to.deep.include({ type: 'free' })
  399. resolve()
  400. }
  401. ctx.ProjectListController.projectListPage(ctx.req, ctx.res)
  402. })
  403. })
  404. it('should not return a best subscription without saas feature', async function (ctx) {
  405. await new Promise(resolve => {
  406. ctx.Features.hasFeature.withArgs('saas').returns(false)
  407. ctx.res.render = (pageName, opts) => {
  408. expect(opts.usersBestSubscription).to.be.undefined
  409. resolve()
  410. }
  411. ctx.ProjectListController.projectListPage(ctx.req, ctx.res)
  412. })
  413. })
  414. it('should show INR Banner for Indian users with free account', async function (ctx) {
  415. await new Promise(resolve => {
  416. // usersBestSubscription is only available when saas feature is present
  417. ctx.Features.hasFeature.withArgs('saas').returns(true)
  418. ctx.SubscriptionViewModelBuilder.promises.getUsersSubscriptionDetails.resolves(
  419. {
  420. bestSubscription: {
  421. type: 'free',
  422. },
  423. }
  424. )
  425. ctx.GeoIpLookup.promises.getCurrencyCode.resolves({
  426. countryCode: 'IN',
  427. })
  428. ctx.res.render = (pageName, opts) => {
  429. expect(opts.showInrGeoBanner).to.be.true
  430. resolve()
  431. }
  432. ctx.ProjectListController.projectListPage(ctx.req, ctx.res)
  433. })
  434. })
  435. it('should not show INR Banner for Indian users with premium account', async function (ctx) {
  436. await new Promise(resolve => {
  437. // usersBestSubscription is only available when saas feature is present
  438. ctx.Features.hasFeature.withArgs('saas').returns(true)
  439. ctx.SubscriptionViewModelBuilder.promises.getUsersSubscriptionDetails.resolves(
  440. {
  441. bestSubscription: {
  442. type: 'individual',
  443. },
  444. }
  445. )
  446. ctx.GeoIpLookup.promises.getCurrencyCode.resolves({
  447. countryCode: 'IN',
  448. })
  449. ctx.res.render = (pageName, opts) => {
  450. expect(opts.showInrGeoBanner).to.be.false
  451. resolve()
  452. }
  453. ctx.ProjectListController.projectListPage(ctx.req, ctx.res)
  454. })
  455. })
  456. it('should redirect to domain capture page', async function (ctx) {
  457. await new Promise(resolve => {
  458. ctx.Features.hasFeature.withArgs('saas').returns(true)
  459. ctx.SplitTestHandler.promises.getAssignment
  460. .withArgs(ctx.req, ctx.res, 'domain-capture-redirect')
  461. .resolves({ variant: 'enabled' })
  462. ctx.Modules.promises.hooks.fire
  463. .withArgs('findDomainCaptureGroupUserCouldBePartOf', ctx.user._id)
  464. .resolves([{ _id: new ObjectId(), managedUsersEnabled: true }])
  465. ctx.res.redirect = url => {
  466. url.should.equal('/domain-capture')
  467. resolve()
  468. }
  469. ctx.ProjectListController.projectListPage(ctx.req, ctx.res)
  470. })
  471. })
  472. describe('With Institution SSO feature', function () {
  473. beforeEach(async function (ctx) {
  474. await new Promise(resolve => {
  475. ctx.institutionEmail = 'test@overleaf.com'
  476. ctx.institutionName = 'Overleaf'
  477. ctx.Features.hasFeature.withArgs('saml').returns(true)
  478. ctx.Features.hasFeature.withArgs('affiliations').returns(true)
  479. ctx.Features.hasFeature.withArgs('saas').returns(true)
  480. resolve()
  481. })
  482. })
  483. it('should show institution SSO available notification for confirmed domains', function (ctx) {
  484. ctx.UserGetter.promises.getUserFullEmails.resolves([
  485. {
  486. email: 'test@overleaf.com',
  487. affiliation: {
  488. institution: {
  489. id: 1,
  490. confirmed: true,
  491. name: 'Overleaf',
  492. ssoBeta: false,
  493. ssoEnabled: true,
  494. },
  495. },
  496. },
  497. ])
  498. ctx.res.render = (pageName, opts) => {
  499. expect(opts.notificationsInstitution).to.deep.include({
  500. email: ctx.institutionEmail,
  501. institutionId: 1,
  502. institutionName: ctx.institutionName,
  503. templateKey: 'notification_institution_sso_available',
  504. })
  505. }
  506. ctx.ProjectListController.projectListPage(ctx.req, ctx.res)
  507. })
  508. it('should show a linked notification', function (ctx) {
  509. ctx.req.session.saml = {
  510. institutionEmail: ctx.institutionEmail,
  511. linked: {
  512. hasEntitlement: false,
  513. universityName: ctx.institutionName,
  514. },
  515. }
  516. ctx.res.render = (pageName, opts) => {
  517. expect(opts.notificationsInstitution).to.deep.include({
  518. email: ctx.institutionEmail,
  519. institutionName: ctx.institutionName,
  520. templateKey: 'notification_institution_sso_linked',
  521. })
  522. }
  523. ctx.ProjectListController.projectListPage(ctx.req, ctx.res)
  524. })
  525. it('should show a linked another email notification', function (ctx) {
  526. // when they request to link an email but the institution returns
  527. // a different email
  528. ctx.res.render = (pageName, opts) => {
  529. expect(opts.notificationsInstitution).to.deep.include({
  530. institutionEmail: ctx.institutionEmail,
  531. requestedEmail: 'requested@overleaf.com',
  532. templateKey: 'notification_institution_sso_non_canonical',
  533. })
  534. }
  535. ctx.req.session.saml = {
  536. emailNonCanonical: ctx.institutionEmail,
  537. institutionEmail: ctx.institutionEmail,
  538. requestedEmail: 'requested@overleaf.com',
  539. linked: {
  540. hasEntitlement: false,
  541. universityName: ctx.institutionName,
  542. },
  543. }
  544. ctx.ProjectListController.projectListPage(ctx.req, ctx.res)
  545. })
  546. it('should show a notification when intent was to register via SSO but account existed', function (ctx) {
  547. ctx.res.render = (pageName, opts) => {
  548. expect(opts.notificationsInstitution).to.deep.include({
  549. email: ctx.institutionEmail,
  550. templateKey: 'notification_institution_sso_already_registered',
  551. })
  552. }
  553. ctx.req.session.saml = {
  554. institutionEmail: ctx.institutionEmail,
  555. linked: {
  556. hasEntitlement: false,
  557. universityName: 'Overleaf',
  558. },
  559. registerIntercept: {
  560. id: 1,
  561. name: 'Example University',
  562. },
  563. }
  564. ctx.ProjectListController.projectListPage(ctx.req, ctx.res)
  565. })
  566. it('should not show a register notification if the flow was abandoned', function (ctx) {
  567. // could initially start to register with an SSO email and then
  568. // abandon flow and login with an existing non-institution SSO email
  569. ctx.res.render = (pageName, opts) => {
  570. expect(opts.notificationsInstitution).to.deep.not.include({
  571. email: 'test@overleaf.com',
  572. templateKey: 'notification_institution_sso_already_registered',
  573. })
  574. }
  575. ctx.req.session.saml = {
  576. registerIntercept: {
  577. id: 1,
  578. name: 'Example University',
  579. },
  580. }
  581. ctx.ProjectListController.projectListPage(ctx.req, ctx.res)
  582. })
  583. it('should show error notification', function (ctx) {
  584. ctx.res.render = (pageName, opts) => {
  585. expect(opts.notificationsInstitution.length).to.equal(1)
  586. expect(opts.notificationsInstitution[0].templateKey).to.equal(
  587. 'notification_institution_sso_error'
  588. )
  589. expect(opts.notificationsInstitution[0].error).to.be.instanceof(
  590. Errors.SAMLAlreadyLinkedError
  591. )
  592. }
  593. ctx.req.session.saml = {
  594. institutionEmail: ctx.institutionEmail,
  595. error: new Errors.SAMLAlreadyLinkedError(),
  596. }
  597. ctx.ProjectListController.projectListPage(ctx.req, ctx.res)
  598. })
  599. describe('for an unconfirmed domain for an SSO institution', function () {
  600. beforeEach(async function (ctx) {
  601. await new Promise(resolve => {
  602. ctx.UserGetter.promises.getUserFullEmails.resolves([
  603. {
  604. email: 'test@overleaf-uncofirmed.com',
  605. affiliation: {
  606. institution: {
  607. id: 1,
  608. confirmed: false,
  609. name: 'Overleaf',
  610. ssoBeta: false,
  611. ssoEnabled: true,
  612. },
  613. },
  614. },
  615. ])
  616. resolve()
  617. })
  618. })
  619. it('should not show institution SSO available notification', function (ctx) {
  620. ctx.res.render = (pageName, opts) => {
  621. expect(opts.notificationsInstitution.length).to.equal(0)
  622. }
  623. ctx.ProjectListController.projectListPage(ctx.req, ctx.res)
  624. })
  625. })
  626. describe('when linking/logging in initiated on institution side', function () {
  627. it('should not show a linked another email notification', function (ctx) {
  628. // this is only used when initated on Overleaf,
  629. // because we keep track of the requested email they tried to link
  630. ctx.res.render = (pageName, opts) => {
  631. expect(opts.notificationsInstitution).to.not.deep.include({
  632. institutionEmail: ctx.institutionEmail,
  633. requestedEmail: undefined,
  634. templateKey: 'notification_institution_sso_non_canonical',
  635. })
  636. }
  637. ctx.req.session.saml = {
  638. emailNonCanonical: ctx.institutionEmail,
  639. institutionEmail: ctx.institutionEmail,
  640. linked: {
  641. hasEntitlement: false,
  642. universityName: ctx.institutionName,
  643. },
  644. }
  645. ctx.ProjectListController.projectListPage(ctx.req, ctx.res)
  646. })
  647. })
  648. describe('Institution with SSO beta testable', function () {
  649. beforeEach(async function (ctx) {
  650. await new Promise(resolve => {
  651. ctx.UserGetter.promises.getUserFullEmails.resolves([
  652. {
  653. email: 'beta@beta.com',
  654. affiliation: {
  655. institution: {
  656. id: 2,
  657. confirmed: true,
  658. name: 'Beta University',
  659. ssoBeta: true,
  660. ssoEnabled: false,
  661. },
  662. },
  663. },
  664. ])
  665. resolve()
  666. })
  667. })
  668. it('should show institution SSO available notification when on a beta testing session', function (ctx) {
  669. ctx.req.session.samlBeta = true
  670. ctx.res.render = (pageName, opts) => {
  671. expect(opts.notificationsInstitution).to.deep.include({
  672. email: 'beta@beta.com',
  673. institutionId: 2,
  674. institutionName: 'Beta University',
  675. templateKey: 'notification_institution_sso_available',
  676. })
  677. }
  678. ctx.ProjectListController.projectListPage(ctx.req, ctx.res)
  679. })
  680. it('should not show institution SSO available notification when not on a beta testing session', function (ctx) {
  681. ctx.req.session.samlBeta = false
  682. ctx.res.render = (pageName, opts) => {
  683. expect(opts.notificationsInstitution).to.deep.not.include({
  684. email: 'test@overleaf.com',
  685. institutionId: 1,
  686. institutionName: 'Overleaf',
  687. templateKey: 'notification_institution_sso_available',
  688. })
  689. }
  690. ctx.ProjectListController.projectListPage(ctx.req, ctx.res)
  691. })
  692. })
  693. })
  694. describe('Without Institution SSO feature', function () {
  695. beforeEach(async function (ctx) {
  696. await new Promise(resolve => {
  697. ctx.Features.hasFeature.withArgs('saml').returns(false)
  698. resolve()
  699. })
  700. })
  701. it('should not show institution sso available notification', function (ctx) {
  702. ctx.res.render = (pageName, opts) => {
  703. expect(opts.notificationsInstitution).to.deep.not.include({
  704. email: 'test@overleaf.com',
  705. institutionId: 1,
  706. institutionName: 'Overleaf',
  707. templateKey: 'notification_institution_sso_available',
  708. })
  709. }
  710. ctx.ProjectListController.projectListPage(ctx.req, ctx.res)
  711. })
  712. })
  713. describe('enterprise banner', function () {
  714. beforeEach(function (ctx) {
  715. ctx.Features.hasFeature.withArgs('saas').returns(true)
  716. ctx.SubscriptionViewModelBuilder.promises.getUsersSubscriptionDetails.resolves(
  717. { memberGroupSubscriptions: [] }
  718. )
  719. ctx.UserGetter.promises.getUserFullEmails.resolves([
  720. {
  721. email: 'test@test-domain.com',
  722. },
  723. ])
  724. })
  725. describe('normal enterprise banner', function () {
  726. it('shows banner', function (ctx) {
  727. ctx.res.render = (pageName, opts) => {
  728. expect(opts.showGroupsAndEnterpriseBanner).to.be.true
  729. }
  730. ctx.ProjectListController.projectListPage(ctx.req, ctx.res)
  731. })
  732. it('does not show banner if user is part of any affiliation', function (ctx) {
  733. ctx.UserGetter.promises.getUserFullEmails.resolves([
  734. {
  735. email: 'test@overleaf.com',
  736. affiliation: {
  737. licence: 'pro_plus',
  738. institution: {
  739. id: 1,
  740. confirmed: true,
  741. name: 'Overleaf',
  742. ssoBeta: false,
  743. ssoEnabled: true,
  744. },
  745. },
  746. },
  747. ])
  748. ctx.res.render = (pageName, opts) => {
  749. expect(opts.showGroupsAndEnterpriseBanner).to.be.false
  750. }
  751. ctx.ProjectListController.projectListPage(ctx.req, ctx.res)
  752. })
  753. it('does not show banner if user is part of any group subscription', function (ctx) {
  754. ctx.SubscriptionViewModelBuilder.promises.getUsersSubscriptionDetails.resolves(
  755. { memberGroupSubscriptions: [{}] }
  756. )
  757. ctx.res.render = (pageName, opts) => {
  758. expect(opts.showGroupsAndEnterpriseBanner).to.be.false
  759. }
  760. ctx.ProjectListController.projectListPage(ctx.req, ctx.res)
  761. })
  762. it('have a banner variant of "FOMO" or "on-premise"', function (ctx) {
  763. ctx.res.render = (pageName, opts) => {
  764. expect(opts.groupsAndEnterpriseBannerVariant).to.be.oneOf([
  765. 'FOMO',
  766. 'on-premise',
  767. ])
  768. }
  769. ctx.ProjectListController.projectListPage(ctx.req, ctx.res)
  770. })
  771. })
  772. describe('US government enterprise banner', function () {
  773. it('does not show enterprise banner if US government enterprise banner is shown', function (ctx) {
  774. const emails = [
  775. {
  776. email: 'test@test.mil',
  777. confirmedAt: new Date('2024-01-01'),
  778. },
  779. ]
  780. ctx.UserGetter.promises.getUserFullEmails.resolves(emails)
  781. ctx.Modules.promises.hooks.fire
  782. .withArgs('getUSGovBanner', emails, false, [])
  783. .resolves([
  784. {
  785. showUSGovBanner: true,
  786. usGovBannerVariant: 'variant',
  787. },
  788. ])
  789. ctx.res.render = (pageName, opts) => {
  790. expect(opts.showGroupsAndEnterpriseBanner).to.be.false
  791. expect(opts.showUSGovBanner).to.be.true
  792. }
  793. ctx.ProjectListController.projectListPage(ctx.req, ctx.res)
  794. })
  795. })
  796. })
  797. })
  798. describe('projectListReactPage with duplicate projects', function () {
  799. beforeEach(function (ctx) {
  800. ctx.projects = [
  801. { _id: 1, lastUpdated: new Date(1), owner_ref: 'user-1' },
  802. { _id: 2, lastUpdated: new Date(2), owner_ref: 'user-2' },
  803. ]
  804. ctx.readAndWrite = [
  805. { _id: 5, lastUpdated: new Date(5), owner_ref: 'user-1' },
  806. ]
  807. ctx.readOnly = [{ _id: 3, lastUpdated: new Date(3), owner_ref: 'user-1' }]
  808. ctx.tokenReadAndWrite = [
  809. { _id: 6, lastUpdated: new Date(5), owner_ref: 'user-4' },
  810. ]
  811. ctx.tokenReadOnly = [
  812. { _id: 6, lastUpdated: new Date(5), owner_ref: 'user-4' }, // Also in tokenReadAndWrite
  813. { _id: 7, lastUpdated: new Date(4), owner_ref: 'user-5' },
  814. ]
  815. ctx.review = [{ _id: 8, lastUpdated: new Date(5), owner_ref: 'user-6' }]
  816. ctx.allProjects = {
  817. owned: ctx.projects,
  818. readAndWrite: ctx.readAndWrite,
  819. readOnly: ctx.readOnly,
  820. tokenReadAndWrite: ctx.tokenReadAndWrite,
  821. tokenReadOnly: ctx.tokenReadOnly,
  822. review: ctx.review,
  823. }
  824. ctx.ProjectGetter.promises.findAllUsersProjects.resolves(ctx.allProjects)
  825. })
  826. it('should render the project/list-react page', async function (ctx) {
  827. await new Promise(resolve => {
  828. ctx.res.render = (pageName, opts) => {
  829. pageName.should.equal('project/list-react')
  830. resolve()
  831. }
  832. ctx.ProjectListController.projectListPage(ctx.req, ctx.res)
  833. })
  834. })
  835. it('should omit one of the projects', async function (ctx) {
  836. await new Promise(resolve => {
  837. ctx.res.render = (pageName, opts) => {
  838. opts.prefetchedProjectsBlob.projects.length.should.equal(
  839. ctx.projects.length +
  840. ctx.readAndWrite.length +
  841. ctx.readOnly.length +
  842. ctx.tokenReadAndWrite.length +
  843. ctx.tokenReadOnly.length +
  844. ctx.review.length -
  845. 1
  846. )
  847. resolve()
  848. }
  849. ctx.ProjectListController.projectListPage(ctx.req, ctx.res)
  850. })
  851. })
  852. })
  853. })