ProjectListController.test.mjs 28 KB

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