ProjectListControllerTests.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  1. const SandboxedModule = require('sandboxed-module')
  2. const path = require('path')
  3. const sinon = require('sinon')
  4. const { expect } = require('chai')
  5. const { ObjectId } = require('mongodb-legacy')
  6. const Errors = require('../../../../app/src/Features/Errors/Errors')
  7. const MODULE_PATH = path.join(
  8. __dirname,
  9. '../../../../app/src/Features/Project/ProjectListController'
  10. )
  11. describe('ProjectListController', function () {
  12. beforeEach(function () {
  13. this.project_id = new ObjectId('abcdefabcdefabcdefabcdef')
  14. this.user = {
  15. _id: new ObjectId('123456123456123456123456'),
  16. email: 'test@overleaf.com',
  17. first_name: 'bjkdsjfk',
  18. features: {},
  19. emails: [{ email: 'test@overleaf.com' }],
  20. lastLoginIp: '111.111.111.112',
  21. }
  22. this.users = {
  23. 'user-1': {
  24. first_name: 'James',
  25. },
  26. 'user-2': {
  27. first_name: 'Henry',
  28. },
  29. }
  30. this.users[this.user._id] = this.user // Owner
  31. this.usersArr = Object.entries(this.users).map(([key, value]) => ({
  32. _id: key,
  33. ...value,
  34. }))
  35. this.tags = [
  36. { name: 1, project_ids: ['1', '2', '3'] },
  37. { name: 2, project_ids: ['a', '1'] },
  38. { name: 3, project_ids: ['a', 'b', 'c', 'd'] },
  39. ]
  40. this.notifications = [
  41. {
  42. _id: '1',
  43. user_id: '2',
  44. templateKey: '3',
  45. messageOpts: '4',
  46. key: '5',
  47. },
  48. ]
  49. this.settings = {
  50. siteUrl: 'https://overleaf.com',
  51. }
  52. this.LimitationsManager = {
  53. promises: {
  54. userIsMemberOfGroupSubscription: sinon.stub().resolves(false),
  55. },
  56. }
  57. this.TagsHandler = {
  58. promises: {
  59. getAllTags: sinon.stub().resolves(this.tags),
  60. },
  61. }
  62. this.NotificationsHandler = {
  63. promises: {
  64. getUserNotifications: sinon.stub().resolves(this.notifications),
  65. },
  66. }
  67. this.UserModel = {
  68. findById: sinon.stub().resolves(this.user),
  69. }
  70. this.UserPrimaryEmailCheckHandler = {
  71. requiresPrimaryEmailCheck: sinon.stub().returns(false),
  72. }
  73. this.ProjectGetter = {
  74. promises: {
  75. findAllUsersProjects: sinon.stub(),
  76. },
  77. }
  78. this.ProjectHelper = {
  79. isArchived: sinon.stub(),
  80. isTrashed: sinon.stub(),
  81. }
  82. this.SessionManager = {
  83. getLoggedInUserId: sinon.stub().returns(this.user._id),
  84. }
  85. this.UserController = {
  86. logout: sinon.stub(),
  87. }
  88. this.UserGetter = {
  89. promises: {
  90. getUsers: sinon.stub().resolves(this.usersArr),
  91. getUserFullEmails: sinon.stub().resolves([]),
  92. },
  93. }
  94. this.Features = {
  95. hasFeature: sinon.stub(),
  96. }
  97. this.Metrics = {
  98. inc: sinon.stub(),
  99. }
  100. this.SplitTestHandler = {
  101. promises: {
  102. getAssignment: sinon.stub().resolves({ variant: 'default' }),
  103. },
  104. }
  105. this.SplitTestSessionHandler = {
  106. promises: {
  107. sessionMaintenance: sinon.stub().resolves(),
  108. },
  109. }
  110. this.SubscriptionViewModelBuilder = {
  111. promises: {
  112. getBestSubscription: sinon.stub().resolves({ type: 'free' }),
  113. },
  114. }
  115. this.SurveyHandler = {
  116. promises: {
  117. getSurvey: sinon.stub().resolves({}),
  118. },
  119. }
  120. this.NotificationBuilder = {
  121. promises: {
  122. ipMatcherAffiliation: sinon.stub().returns({ create: sinon.stub() }),
  123. },
  124. }
  125. this.SubscriptionLocator = {
  126. promises: {
  127. getUserSubscription: sinon.stub().resolves({}),
  128. },
  129. }
  130. this.GeoIpLookup = {
  131. promises: {
  132. getCurrencyCode: sinon.stub().resolves({
  133. countryCode: 'US',
  134. currencyCode: 'USD',
  135. }),
  136. },
  137. }
  138. this.ProjectListController = SandboxedModule.require(MODULE_PATH, {
  139. requires: {
  140. 'mongodb-legacy': { ObjectId },
  141. '@overleaf/settings': this.settings,
  142. '@overleaf/metrics': this.Metrics,
  143. '../SplitTests/SplitTestHandler': this.SplitTestHandler,
  144. '../SplitTests/SplitTestSessionHandler': this.SplitTestSessionHandler,
  145. '../User/UserController': this.UserController,
  146. './ProjectHelper': this.ProjectHelper,
  147. '../Subscription/LimitationsManager': this.LimitationsManager,
  148. '../Tags/TagsHandler': this.TagsHandler,
  149. '../Notifications/NotificationsHandler': this.NotificationsHandler,
  150. '../../models/User': { User: this.UserModel },
  151. './ProjectGetter': this.ProjectGetter,
  152. '../Authentication/SessionManager': this.SessionManager,
  153. '../../infrastructure/Features': this.Features,
  154. '../User/UserGetter': this.UserGetter,
  155. '../Subscription/SubscriptionViewModelBuilder':
  156. this.SubscriptionViewModelBuilder,
  157. '../../infrastructure/Modules': {
  158. promises: {
  159. hooks: { fire: sinon.stub().resolves([]) },
  160. },
  161. },
  162. '../Survey/SurveyHandler': this.SurveyHandler,
  163. '../User/UserPrimaryEmailCheckHandler':
  164. this.UserPrimaryEmailCheckHandler,
  165. '../Notifications/NotificationsBuilder': this.NotificationBuilder,
  166. '../Subscription/SubscriptionLocator': this.SubscriptionLocator,
  167. '../../infrastructure/GeoIpLookup': this.GeoIpLookup,
  168. },
  169. })
  170. this.req = {
  171. query: {},
  172. params: {
  173. Project_id: this.project_id,
  174. },
  175. headers: {},
  176. session: {
  177. user: this.user,
  178. },
  179. body: {},
  180. i18n: {
  181. translate() {},
  182. },
  183. }
  184. this.res = {}
  185. })
  186. describe('projectListPage', function () {
  187. beforeEach(function () {
  188. this.projects = [
  189. { _id: 1, lastUpdated: 1, owner_ref: 'user-1' },
  190. {
  191. _id: 2,
  192. lastUpdated: 2,
  193. owner_ref: 'user-2',
  194. lastUpdatedBy: 'user-1',
  195. },
  196. ]
  197. this.readAndWrite = [{ _id: 5, lastUpdated: 5, owner_ref: 'user-1' }]
  198. this.readOnly = [{ _id: 3, lastUpdated: 3, owner_ref: 'user-1' }]
  199. this.tokenReadAndWrite = [{ _id: 6, lastUpdated: 5, owner_ref: 'user-4' }]
  200. this.tokenReadOnly = [{ _id: 7, lastUpdated: 4, owner_ref: 'user-5' }]
  201. this.allProjects = {
  202. owned: this.projects,
  203. readAndWrite: this.readAndWrite,
  204. readOnly: this.readOnly,
  205. tokenReadAndWrite: this.tokenReadAndWrite,
  206. tokenReadOnly: this.tokenReadOnly,
  207. }
  208. this.ProjectGetter.promises.findAllUsersProjects.resolves(
  209. this.allProjects
  210. )
  211. })
  212. it('should render the project/list-react page', function (done) {
  213. this.res.render = (pageName, opts) => {
  214. pageName.should.equal('project/list-react')
  215. done()
  216. }
  217. this.ProjectListController.projectListPage(this.req, this.res)
  218. })
  219. it('should invoke the session maintenance', function (done) {
  220. this.Features.hasFeature.withArgs('saas').returns(true)
  221. this.res.render = () => {
  222. this.SplitTestSessionHandler.promises.sessionMaintenance.should.have.been.calledWith(
  223. this.req,
  224. this.user
  225. )
  226. done()
  227. }
  228. this.ProjectListController.projectListPage(this.req, this.res)
  229. })
  230. it('should send the tags', function (done) {
  231. this.res.render = (pageName, opts) => {
  232. opts.tags.length.should.equal(this.tags.length)
  233. done()
  234. }
  235. this.ProjectListController.projectListPage(this.req, this.res)
  236. })
  237. it('should create trigger ip matcher notifications', function (done) {
  238. this.settings.overleaf = true
  239. this.req.ip = '111.111.111.111'
  240. this.res.render = (pageName, opts) => {
  241. this.NotificationBuilder.promises.ipMatcherAffiliation.called.should.equal(
  242. true
  243. )
  244. done()
  245. }
  246. this.ProjectListController.projectListPage(this.req, this.res)
  247. })
  248. it('should send the projects', function (done) {
  249. this.res.render = (pageName, opts) => {
  250. opts.prefetchedProjectsBlob.projects.length.should.equal(
  251. this.projects.length +
  252. this.readAndWrite.length +
  253. this.readOnly.length +
  254. this.tokenReadAndWrite.length +
  255. this.tokenReadOnly.length
  256. )
  257. done()
  258. }
  259. this.ProjectListController.projectListPage(this.req, this.res)
  260. })
  261. it('should send the user', function (done) {
  262. this.res.render = (pageName, opts) => {
  263. opts.user.should.deep.equal(this.user)
  264. done()
  265. }
  266. this.ProjectListController.projectListPage(this.req, this.res)
  267. })
  268. it('should inject the users', function (done) {
  269. this.res.render = (pageName, opts) => {
  270. const projects = opts.prefetchedProjectsBlob.projects
  271. projects
  272. .filter(p => p.id === '1')[0]
  273. .owner.firstName.should.equal(
  274. this.users[this.projects.filter(p => p._id === 1)[0].owner_ref]
  275. .first_name
  276. )
  277. projects
  278. .filter(p => p.id === '2')[0]
  279. .owner.firstName.should.equal(
  280. this.users[this.projects.filter(p => p._id === 2)[0].owner_ref]
  281. .first_name
  282. )
  283. projects
  284. .filter(p => p.id === '2')[0]
  285. .lastUpdatedBy.firstName.should.equal(
  286. this.users[this.projects.filter(p => p._id === 2)[0].lastUpdatedBy]
  287. .first_name
  288. )
  289. done()
  290. }
  291. this.ProjectListController.projectListPage(this.req, this.res)
  292. })
  293. it("should send the user's best subscription when saas feature present", function (done) {
  294. this.Features.hasFeature.withArgs('saas').returns(true)
  295. this.res.render = (pageName, opts) => {
  296. expect(opts.usersBestSubscription).to.deep.include({ type: 'free' })
  297. done()
  298. }
  299. this.ProjectListController.projectListPage(this.req, this.res)
  300. })
  301. it('should not return a best subscription without saas feature', function (done) {
  302. this.Features.hasFeature.withArgs('saas').returns(false)
  303. this.res.render = (pageName, opts) => {
  304. expect(opts.usersBestSubscription).to.be.undefined
  305. done()
  306. }
  307. this.ProjectListController.projectListPage(this.req, this.res)
  308. })
  309. it('should show INR Banner for Indian users with free account', function (done) {
  310. // usersBestSubscription is only available when saas feature is present
  311. this.Features.hasFeature.withArgs('saas').returns(true)
  312. this.SubscriptionViewModelBuilder.promises.getBestSubscription.resolves({
  313. type: 'free',
  314. })
  315. this.GeoIpLookup.promises.getCurrencyCode.resolves({
  316. countryCode: 'IN',
  317. })
  318. this.res.render = (pageName, opts) => {
  319. expect(opts.showInrGeoBanner).to.be.true
  320. done()
  321. }
  322. this.ProjectListController.projectListPage(this.req, this.res)
  323. })
  324. it('should not show INR Banner for Indian users with premium account', function (done) {
  325. // usersBestSubscription is only available when saas feature is present
  326. this.Features.hasFeature.withArgs('saas').returns(true)
  327. this.SubscriptionViewModelBuilder.promises.getBestSubscription.resolves({
  328. type: 'individual',
  329. })
  330. this.GeoIpLookup.promises.getCurrencyCode.resolves({
  331. countryCode: 'IN',
  332. })
  333. this.res.render = (pageName, opts) => {
  334. expect(opts.showInrGeoBanner).to.be.false
  335. done()
  336. }
  337. this.ProjectListController.projectListPage(this.req, this.res)
  338. })
  339. describe('With Institution SSO feature', function () {
  340. beforeEach(function (done) {
  341. this.institutionEmail = 'test@overleaf.com'
  342. this.institutionName = 'Overleaf'
  343. this.Features.hasFeature.withArgs('saml').returns(true)
  344. this.Features.hasFeature.withArgs('affiliations').returns(true)
  345. this.Features.hasFeature.withArgs('overleaf-integration').returns(true)
  346. done()
  347. })
  348. it('should show institution SSO available notification for confirmed domains', function () {
  349. this.UserGetter.promises.getUserFullEmails.resolves([
  350. {
  351. email: 'test@overleaf.com',
  352. affiliation: {
  353. institution: {
  354. id: 1,
  355. confirmed: true,
  356. name: 'Overleaf',
  357. ssoBeta: false,
  358. ssoEnabled: true,
  359. },
  360. },
  361. },
  362. ])
  363. this.res.render = (pageName, opts) => {
  364. expect(opts.notificationsInstitution).to.deep.include({
  365. email: this.institutionEmail,
  366. institutionId: 1,
  367. institutionName: this.institutionName,
  368. templateKey: 'notification_institution_sso_available',
  369. })
  370. }
  371. this.ProjectListController.projectListPage(this.req, this.res)
  372. })
  373. it('should show a linked notification', function () {
  374. this.req.session.saml = {
  375. institutionEmail: this.institutionEmail,
  376. linked: {
  377. hasEntitlement: false,
  378. universityName: this.institutionName,
  379. },
  380. }
  381. this.res.render = (pageName, opts) => {
  382. expect(opts.notificationsInstitution).to.deep.include({
  383. email: this.institutionEmail,
  384. institutionName: this.institutionName,
  385. templateKey: 'notification_institution_sso_linked',
  386. })
  387. }
  388. this.ProjectListController.projectListPage(this.req, this.res)
  389. })
  390. it('should show a linked another email notification', function () {
  391. // when they request to link an email but the institution returns
  392. // a different email
  393. this.res.render = (pageName, opts) => {
  394. expect(opts.notificationsInstitution).to.deep.include({
  395. institutionEmail: this.institutionEmail,
  396. requestedEmail: 'requested@overleaf.com',
  397. templateKey: 'notification_institution_sso_non_canonical',
  398. })
  399. }
  400. this.req.session.saml = {
  401. emailNonCanonical: this.institutionEmail,
  402. institutionEmail: this.institutionEmail,
  403. requestedEmail: 'requested@overleaf.com',
  404. linked: {
  405. hasEntitlement: false,
  406. universityName: this.institutionName,
  407. },
  408. }
  409. this.ProjectListController.projectListPage(this.req, this.res)
  410. })
  411. it('should show a notification when intent was to register via SSO but account existed', function () {
  412. this.res.render = (pageName, opts) => {
  413. expect(opts.notificationsInstitution).to.deep.include({
  414. email: this.institutionEmail,
  415. templateKey: 'notification_institution_sso_already_registered',
  416. })
  417. }
  418. this.req.session.saml = {
  419. institutionEmail: this.institutionEmail,
  420. linked: {
  421. hasEntitlement: false,
  422. universityName: 'Overleaf',
  423. },
  424. registerIntercept: {
  425. id: 1,
  426. name: 'Example University',
  427. },
  428. }
  429. this.ProjectListController.projectListPage(this.req, this.res)
  430. })
  431. it('should not show a register notification if the flow was abandoned', function () {
  432. // could initially start to register with an SSO email and then
  433. // abandon flow and login with an existing non-institution SSO email
  434. this.res.render = (pageName, opts) => {
  435. expect(opts.notificationsInstitution).to.deep.not.include({
  436. email: 'test@overleaf.com',
  437. templateKey: 'notification_institution_sso_already_registered',
  438. })
  439. }
  440. this.req.session.saml = {
  441. registerIntercept: {
  442. id: 1,
  443. name: 'Example University',
  444. },
  445. }
  446. this.ProjectListController.projectListPage(this.req, this.res)
  447. })
  448. it('should show error notification', function () {
  449. this.res.render = (pageName, opts) => {
  450. expect(opts.notificationsInstitution.length).to.equal(1)
  451. expect(opts.notificationsInstitution[0].templateKey).to.equal(
  452. 'notification_institution_sso_error'
  453. )
  454. expect(opts.notificationsInstitution[0].error).to.be.instanceof(
  455. Errors.SAMLAlreadyLinkedError
  456. )
  457. }
  458. this.req.session.saml = {
  459. institutionEmail: this.institutionEmail,
  460. error: new Errors.SAMLAlreadyLinkedError(),
  461. }
  462. this.ProjectListController.projectListPage(this.req, this.res)
  463. })
  464. describe('for an unconfirmed domain for an SSO institution', function () {
  465. beforeEach(function (done) {
  466. this.UserGetter.promises.getUserFullEmails.resolves([
  467. {
  468. email: 'test@overleaf-uncofirmed.com',
  469. affiliation: {
  470. institution: {
  471. id: 1,
  472. confirmed: false,
  473. name: 'Overleaf',
  474. ssoBeta: false,
  475. ssoEnabled: true,
  476. },
  477. },
  478. },
  479. ])
  480. done()
  481. })
  482. it('should not show institution SSO available notification', function () {
  483. this.res.render = (pageName, opts) => {
  484. expect(opts.notificationsInstitution.length).to.equal(0)
  485. }
  486. this.ProjectListController.projectListPage(this.req, this.res)
  487. })
  488. })
  489. describe('when linking/logging in initiated on institution side', function () {
  490. it('should not show a linked another email notification', function () {
  491. // this is only used when initated on Overleaf,
  492. // because we keep track of the requested email they tried to link
  493. this.res.render = (pageName, opts) => {
  494. expect(opts.notificationsInstitution).to.not.deep.include({
  495. institutionEmail: this.institutionEmail,
  496. requestedEmail: undefined,
  497. templateKey: 'notification_institution_sso_non_canonical',
  498. })
  499. }
  500. this.req.session.saml = {
  501. emailNonCanonical: this.institutionEmail,
  502. institutionEmail: this.institutionEmail,
  503. linked: {
  504. hasEntitlement: false,
  505. universityName: this.institutionName,
  506. },
  507. }
  508. this.ProjectListController.projectListPage(this.req, this.res)
  509. })
  510. })
  511. describe('Institution with SSO beta testable', function () {
  512. beforeEach(function (done) {
  513. this.UserGetter.promises.getUserFullEmails.resolves([
  514. {
  515. email: 'beta@beta.com',
  516. affiliation: {
  517. institution: {
  518. id: 2,
  519. confirmed: true,
  520. name: 'Beta University',
  521. ssoBeta: true,
  522. ssoEnabled: false,
  523. },
  524. },
  525. },
  526. ])
  527. done()
  528. })
  529. it('should show institution SSO available notification when on a beta testing session', function () {
  530. this.req.session.samlBeta = true
  531. this.res.render = (pageName, opts) => {
  532. expect(opts.notificationsInstitution).to.deep.include({
  533. email: 'beta@beta.com',
  534. institutionId: 2,
  535. institutionName: 'Beta University',
  536. templateKey: 'notification_institution_sso_available',
  537. })
  538. }
  539. this.ProjectListController.projectListPage(this.req, this.res)
  540. })
  541. it('should not show institution SSO available notification when not on a beta testing session', function () {
  542. this.req.session.samlBeta = false
  543. this.res.render = (pageName, opts) => {
  544. expect(opts.notificationsInstitution).to.deep.not.include({
  545. email: 'test@overleaf.com',
  546. institutionId: 1,
  547. institutionName: 'Overleaf',
  548. templateKey: 'notification_institution_sso_available',
  549. })
  550. }
  551. this.ProjectListController.projectListPage(this.req, this.res)
  552. })
  553. })
  554. })
  555. describe('Without Institution SSO feature', function () {
  556. beforeEach(function (done) {
  557. this.Features.hasFeature.withArgs('saml').returns(false)
  558. done()
  559. })
  560. it('should not show institution sso available notification', function () {
  561. this.res.render = (pageName, opts) => {
  562. expect(opts.notificationsInstitution).to.deep.not.include({
  563. email: 'test@overleaf.com',
  564. institutionId: 1,
  565. institutionName: 'Overleaf',
  566. templateKey: 'notification_institution_sso_available',
  567. })
  568. }
  569. this.ProjectListController.projectListPage(this.req, this.res)
  570. })
  571. })
  572. })
  573. describe('projectListReactPage with duplicate projects', function () {
  574. beforeEach(function () {
  575. this.projects = [
  576. { _id: 1, lastUpdated: 1, owner_ref: 'user-1' },
  577. { _id: 2, lastUpdated: 2, owner_ref: 'user-2' },
  578. ]
  579. this.readAndWrite = [{ _id: 5, lastUpdated: 5, owner_ref: 'user-1' }]
  580. this.readOnly = [{ _id: 3, lastUpdated: 3, owner_ref: 'user-1' }]
  581. this.tokenReadAndWrite = [{ _id: 6, lastUpdated: 5, owner_ref: 'user-4' }]
  582. this.tokenReadOnly = [
  583. { _id: 6, lastUpdated: 5, owner_ref: 'user-4' }, // Also in tokenReadAndWrite
  584. { _id: 7, lastUpdated: 4, owner_ref: 'user-5' },
  585. ]
  586. this.allProjects = {
  587. owned: this.projects,
  588. readAndWrite: this.readAndWrite,
  589. readOnly: this.readOnly,
  590. tokenReadAndWrite: this.tokenReadAndWrite,
  591. tokenReadOnly: this.tokenReadOnly,
  592. }
  593. this.ProjectGetter.promises.findAllUsersProjects.resolves(
  594. this.allProjects
  595. )
  596. })
  597. it('should render the project/list-react page', function (done) {
  598. this.res.render = (pageName, opts) => {
  599. pageName.should.equal('project/list-react')
  600. done()
  601. }
  602. this.ProjectListController.projectListPage(this.req, this.res)
  603. })
  604. it('should omit one of the projects', function (done) {
  605. this.res.render = (pageName, opts) => {
  606. opts.prefetchedProjectsBlob.projects.length.should.equal(
  607. this.projects.length +
  608. this.readAndWrite.length +
  609. this.readOnly.length +
  610. this.tokenReadAndWrite.length +
  611. this.tokenReadOnly.length -
  612. 1
  613. )
  614. done()
  615. }
  616. this.ProjectListController.projectListPage(this.req, this.res)
  617. })
  618. })
  619. })