ProjectListControllerTests.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  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')
  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. sessionMaintenance: sinon.stub().resolves(),
  103. getAssignment: sinon.stub().resolves({ variant: 'default' }),
  104. },
  105. }
  106. this.SubscriptionViewModelBuilder = {
  107. promises: {
  108. getBestSubscription: sinon.stub().resolves({ type: 'free' }),
  109. },
  110. }
  111. this.SurveyHandler = {
  112. promises: {
  113. getSurvey: sinon.stub().resolves({}),
  114. },
  115. }
  116. this.NotificationBuilder = {
  117. promises: {
  118. ipMatcherAffiliation: sinon.stub().returns({ create: sinon.stub() }),
  119. },
  120. }
  121. this.SubscriptionLocator = {
  122. promises: {
  123. getUserSubscription: sinon.stub().resolves({}),
  124. },
  125. }
  126. this.ProjectListController = SandboxedModule.require(MODULE_PATH, {
  127. requires: {
  128. mongodb: { ObjectId },
  129. '@overleaf/settings': this.settings,
  130. '@overleaf/metrics': this.Metrics,
  131. '../SplitTests/SplitTestHandler': this.SplitTestHandler,
  132. '../User/UserController': this.UserController,
  133. './ProjectHelper': this.ProjectHelper,
  134. '../Subscription/LimitationsManager': this.LimitationsManager,
  135. '../Tags/TagsHandler': this.TagsHandler,
  136. '../Notifications/NotificationsHandler': this.NotificationsHandler,
  137. '../../models/User': { User: this.UserModel },
  138. './ProjectGetter': this.ProjectGetter,
  139. '../Authentication/SessionManager': this.SessionManager,
  140. '../../infrastructure/Features': this.Features,
  141. '../User/UserGetter': this.UserGetter,
  142. '../Subscription/SubscriptionViewModelBuilder':
  143. this.SubscriptionViewModelBuilder,
  144. '../../infrastructure/Modules': {
  145. promises: {
  146. hooks: { fire: sinon.stub().resolves([]) },
  147. },
  148. },
  149. '../Survey/SurveyHandler': this.SurveyHandler,
  150. '../User/UserPrimaryEmailCheckHandler':
  151. this.UserPrimaryEmailCheckHandler,
  152. '../Notifications/NotificationsBuilder': this.NotificationBuilder,
  153. '../Subscription/SubscriptionLocator': this.SubscriptionLocator,
  154. },
  155. })
  156. this.req = {
  157. query: {},
  158. params: {
  159. Project_id: this.project_id,
  160. },
  161. headers: {},
  162. session: {
  163. user: this.user,
  164. },
  165. body: {},
  166. i18n: {
  167. translate() {},
  168. },
  169. }
  170. this.res = {}
  171. })
  172. describe('projectListPage', function () {
  173. beforeEach(function () {
  174. this.projects = [
  175. { _id: 1, lastUpdated: 1, owner_ref: 'user-1' },
  176. {
  177. _id: 2,
  178. lastUpdated: 2,
  179. owner_ref: 'user-2',
  180. lastUpdatedBy: 'user-1',
  181. },
  182. ]
  183. this.readAndWrite = [{ _id: 5, lastUpdated: 5, owner_ref: 'user-1' }]
  184. this.readOnly = [{ _id: 3, lastUpdated: 3, owner_ref: 'user-1' }]
  185. this.tokenReadAndWrite = [{ _id: 6, lastUpdated: 5, owner_ref: 'user-4' }]
  186. this.tokenReadOnly = [{ _id: 7, lastUpdated: 4, owner_ref: 'user-5' }]
  187. this.allProjects = {
  188. owned: this.projects,
  189. readAndWrite: this.readAndWrite,
  190. readOnly: this.readOnly,
  191. tokenReadAndWrite: this.tokenReadAndWrite,
  192. tokenReadOnly: this.tokenReadOnly,
  193. }
  194. this.ProjectGetter.promises.findAllUsersProjects.resolves(
  195. this.allProjects
  196. )
  197. })
  198. it('should render the project/list-react page', function (done) {
  199. this.res.render = (pageName, opts) => {
  200. pageName.should.equal('project/list-react')
  201. done()
  202. }
  203. this.ProjectListController.projectListPage(this.req, this.res)
  204. })
  205. it('should invoke the session maintenance', function (done) {
  206. this.Features.hasFeature.withArgs('saas').returns(true)
  207. this.res.render = () => {
  208. this.SplitTestHandler.promises.sessionMaintenance.should.have.been.calledWith(
  209. this.req,
  210. this.user
  211. )
  212. done()
  213. }
  214. this.ProjectListController.projectListPage(this.req, this.res)
  215. })
  216. it('should send the tags', function (done) {
  217. this.res.render = (pageName, opts) => {
  218. opts.tags.length.should.equal(this.tags.length)
  219. done()
  220. }
  221. this.ProjectListController.projectListPage(this.req, this.res)
  222. })
  223. it('should create trigger ip matcher notifications', function (done) {
  224. this.settings.overleaf = true
  225. this.req.ip = '111.111.111.111'
  226. this.res.render = (pageName, opts) => {
  227. this.NotificationBuilder.promises.ipMatcherAffiliation.called.should.equal(
  228. true
  229. )
  230. done()
  231. }
  232. this.ProjectListController.projectListPage(this.req, this.res)
  233. })
  234. it('should send the projects', function (done) {
  235. this.res.render = (pageName, opts) => {
  236. opts.prefetchedProjectsBlob.projects.length.should.equal(
  237. this.projects.length +
  238. this.readAndWrite.length +
  239. this.readOnly.length +
  240. this.tokenReadAndWrite.length +
  241. this.tokenReadOnly.length
  242. )
  243. done()
  244. }
  245. this.ProjectListController.projectListPage(this.req, this.res)
  246. })
  247. it('should send the user', function (done) {
  248. this.res.render = (pageName, opts) => {
  249. opts.user.should.deep.equal(this.user)
  250. done()
  251. }
  252. this.ProjectListController.projectListPage(this.req, this.res)
  253. })
  254. it('should inject the users', function (done) {
  255. this.res.render = (pageName, opts) => {
  256. const projects = opts.prefetchedProjectsBlob.projects
  257. projects
  258. .filter(p => p.id === '1')[0]
  259. .owner.firstName.should.equal(
  260. this.users[this.projects.filter(p => p._id === 1)[0].owner_ref]
  261. .first_name
  262. )
  263. projects
  264. .filter(p => p.id === '2')[0]
  265. .owner.firstName.should.equal(
  266. this.users[this.projects.filter(p => p._id === 2)[0].owner_ref]
  267. .first_name
  268. )
  269. projects
  270. .filter(p => p.id === '2')[0]
  271. .lastUpdatedBy.firstName.should.equal(
  272. this.users[this.projects.filter(p => p._id === 2)[0].lastUpdatedBy]
  273. .first_name
  274. )
  275. done()
  276. }
  277. this.ProjectListController.projectListPage(this.req, this.res)
  278. })
  279. it("should send the user's best subscription when saas feature present", function (done) {
  280. this.Features.hasFeature.withArgs('saas').returns(true)
  281. this.res.render = (pageName, opts) => {
  282. expect(opts.usersBestSubscription).to.deep.include({ type: 'free' })
  283. done()
  284. }
  285. this.ProjectListController.projectListPage(this.req, this.res)
  286. })
  287. it('should not return a best subscription without saas feature', function (done) {
  288. this.Features.hasFeature.withArgs('saas').returns(false)
  289. this.res.render = (pageName, opts) => {
  290. expect(opts.usersBestSubscription).to.be.undefined
  291. done()
  292. }
  293. this.ProjectListController.projectListPage(this.req, this.res)
  294. })
  295. describe('With Institution SSO feature', function () {
  296. beforeEach(function (done) {
  297. this.institutionEmail = 'test@overleaf.com'
  298. this.institutionName = 'Overleaf'
  299. this.Features.hasFeature.withArgs('saml').returns(true)
  300. this.Features.hasFeature.withArgs('affiliations').returns(true)
  301. this.Features.hasFeature.withArgs('overleaf-integration').returns(true)
  302. done()
  303. })
  304. it('should show institution SSO available notification for confirmed domains', function () {
  305. this.UserGetter.promises.getUserFullEmails.resolves([
  306. {
  307. email: 'test@overleaf.com',
  308. affiliation: {
  309. institution: {
  310. id: 1,
  311. confirmed: true,
  312. name: 'Overleaf',
  313. ssoBeta: false,
  314. ssoEnabled: true,
  315. },
  316. },
  317. },
  318. ])
  319. this.res.render = (pageName, opts) => {
  320. expect(opts.notificationsInstitution).to.deep.include({
  321. email: this.institutionEmail,
  322. institutionId: 1,
  323. institutionName: this.institutionName,
  324. templateKey: 'notification_institution_sso_available',
  325. })
  326. }
  327. this.ProjectListController.projectListPage(this.req, this.res)
  328. })
  329. it('should show a linked notification', function () {
  330. this.req.session.saml = {
  331. institutionEmail: this.institutionEmail,
  332. linked: {
  333. hasEntitlement: false,
  334. universityName: this.institutionName,
  335. },
  336. }
  337. this.res.render = (pageName, opts) => {
  338. expect(opts.notificationsInstitution).to.deep.include({
  339. email: this.institutionEmail,
  340. institutionName: this.institutionName,
  341. templateKey: 'notification_institution_sso_linked',
  342. })
  343. }
  344. this.ProjectListController.projectListPage(this.req, this.res)
  345. })
  346. it('should show a linked another email notification', function () {
  347. // when they request to link an email but the institution returns
  348. // a different email
  349. this.res.render = (pageName, opts) => {
  350. expect(opts.notificationsInstitution).to.deep.include({
  351. institutionEmail: this.institutionEmail,
  352. requestedEmail: 'requested@overleaf.com',
  353. templateKey: 'notification_institution_sso_non_canonical',
  354. })
  355. }
  356. this.req.session.saml = {
  357. emailNonCanonical: this.institutionEmail,
  358. institutionEmail: this.institutionEmail,
  359. requestedEmail: 'requested@overleaf.com',
  360. linked: {
  361. hasEntitlement: false,
  362. universityName: this.institutionName,
  363. },
  364. }
  365. this.ProjectListController.projectListPage(this.req, this.res)
  366. })
  367. it('should show a notification when intent was to register via SSO but account existed', function () {
  368. this.res.render = (pageName, opts) => {
  369. expect(opts.notificationsInstitution).to.deep.include({
  370. email: this.institutionEmail,
  371. templateKey: 'notification_institution_sso_already_registered',
  372. })
  373. }
  374. this.req.session.saml = {
  375. institutionEmail: this.institutionEmail,
  376. linked: {
  377. hasEntitlement: false,
  378. universityName: 'Overleaf',
  379. },
  380. registerIntercept: {
  381. id: 1,
  382. name: 'Example University',
  383. },
  384. }
  385. this.ProjectListController.projectListPage(this.req, this.res)
  386. })
  387. it('should not show a register notification if the flow was abandoned', function () {
  388. // could initially start to register with an SSO email and then
  389. // abandon flow and login with an existing non-institution SSO email
  390. this.res.render = (pageName, opts) => {
  391. expect(opts.notificationsInstitution).to.deep.not.include({
  392. email: 'test@overleaf.com',
  393. templateKey: 'notification_institution_sso_already_registered',
  394. })
  395. }
  396. this.req.session.saml = {
  397. registerIntercept: {
  398. id: 1,
  399. name: 'Example University',
  400. },
  401. }
  402. this.ProjectListController.projectListPage(this.req, this.res)
  403. })
  404. it('should show error notification', function () {
  405. this.res.render = (pageName, opts) => {
  406. expect(opts.notificationsInstitution.length).to.equal(1)
  407. expect(opts.notificationsInstitution[0].templateKey).to.equal(
  408. 'notification_institution_sso_error'
  409. )
  410. expect(opts.notificationsInstitution[0].error).to.be.instanceof(
  411. Errors.SAMLAlreadyLinkedError
  412. )
  413. }
  414. this.req.session.saml = {
  415. institutionEmail: this.institutionEmail,
  416. error: new Errors.SAMLAlreadyLinkedError(),
  417. }
  418. this.ProjectListController.projectListPage(this.req, this.res)
  419. })
  420. describe('for an unconfirmed domain for an SSO institution', function () {
  421. beforeEach(function (done) {
  422. this.UserGetter.promises.getUserFullEmails.resolves([
  423. {
  424. email: 'test@overleaf-uncofirmed.com',
  425. affiliation: {
  426. institution: {
  427. id: 1,
  428. confirmed: false,
  429. name: 'Overleaf',
  430. ssoBeta: false,
  431. ssoEnabled: true,
  432. },
  433. },
  434. },
  435. ])
  436. done()
  437. })
  438. it('should not show institution SSO available notification', function () {
  439. this.res.render = (pageName, opts) => {
  440. expect(opts.notificationsInstitution.length).to.equal(0)
  441. }
  442. this.ProjectListController.projectListPage(this.req, this.res)
  443. })
  444. })
  445. describe('when linking/logging in initiated on institution side', function () {
  446. it('should not show a linked another email notification', function () {
  447. // this is only used when initated on Overleaf,
  448. // because we keep track of the requested email they tried to link
  449. this.res.render = (pageName, opts) => {
  450. expect(opts.notificationsInstitution).to.not.deep.include({
  451. institutionEmail: this.institutionEmail,
  452. requestedEmail: undefined,
  453. templateKey: 'notification_institution_sso_non_canonical',
  454. })
  455. }
  456. this.req.session.saml = {
  457. emailNonCanonical: this.institutionEmail,
  458. institutionEmail: this.institutionEmail,
  459. linked: {
  460. hasEntitlement: false,
  461. universityName: this.institutionName,
  462. },
  463. }
  464. this.ProjectListController.projectListPage(this.req, this.res)
  465. })
  466. })
  467. describe('Institution with SSO beta testable', function () {
  468. beforeEach(function (done) {
  469. this.UserGetter.promises.getUserFullEmails.resolves([
  470. {
  471. email: 'beta@beta.com',
  472. affiliation: {
  473. institution: {
  474. id: 2,
  475. confirmed: true,
  476. name: 'Beta University',
  477. ssoBeta: true,
  478. ssoEnabled: false,
  479. },
  480. },
  481. },
  482. ])
  483. done()
  484. })
  485. it('should show institution SSO available notification when on a beta testing session', function () {
  486. this.req.session.samlBeta = true
  487. this.res.render = (pageName, opts) => {
  488. expect(opts.notificationsInstitution).to.deep.include({
  489. email: 'beta@beta.com',
  490. institutionId: 2,
  491. institutionName: 'Beta University',
  492. templateKey: 'notification_institution_sso_available',
  493. })
  494. }
  495. this.ProjectListController.projectListPage(this.req, this.res)
  496. })
  497. it('should not show institution SSO available notification when not on a beta testing session', function () {
  498. this.req.session.samlBeta = false
  499. this.res.render = (pageName, opts) => {
  500. expect(opts.notificationsInstitution).to.deep.not.include({
  501. email: 'test@overleaf.com',
  502. institutionId: 1,
  503. institutionName: 'Overleaf',
  504. templateKey: 'notification_institution_sso_available',
  505. })
  506. }
  507. this.ProjectListController.projectListPage(this.req, this.res)
  508. })
  509. })
  510. })
  511. describe('Without Institution SSO feature', function () {
  512. beforeEach(function (done) {
  513. this.Features.hasFeature.withArgs('saml').returns(false)
  514. done()
  515. })
  516. it('should not show institution sso available notification', function () {
  517. this.res.render = (pageName, opts) => {
  518. expect(opts.notificationsInstitution).to.deep.not.include({
  519. email: 'test@overleaf.com',
  520. institutionId: 1,
  521. institutionName: 'Overleaf',
  522. templateKey: 'notification_institution_sso_available',
  523. })
  524. }
  525. this.ProjectListController.projectListPage(this.req, this.res)
  526. })
  527. })
  528. })
  529. describe('projectListReactPage with duplicate projects', function () {
  530. beforeEach(function () {
  531. this.projects = [
  532. { _id: 1, lastUpdated: 1, owner_ref: 'user-1' },
  533. { _id: 2, lastUpdated: 2, owner_ref: 'user-2' },
  534. ]
  535. this.readAndWrite = [{ _id: 5, lastUpdated: 5, owner_ref: 'user-1' }]
  536. this.readOnly = [{ _id: 3, lastUpdated: 3, owner_ref: 'user-1' }]
  537. this.tokenReadAndWrite = [{ _id: 6, lastUpdated: 5, owner_ref: 'user-4' }]
  538. this.tokenReadOnly = [
  539. { _id: 6, lastUpdated: 5, owner_ref: 'user-4' }, // Also in tokenReadAndWrite
  540. { _id: 7, lastUpdated: 4, owner_ref: 'user-5' },
  541. ]
  542. this.allProjects = {
  543. owned: this.projects,
  544. readAndWrite: this.readAndWrite,
  545. readOnly: this.readOnly,
  546. tokenReadAndWrite: this.tokenReadAndWrite,
  547. tokenReadOnly: this.tokenReadOnly,
  548. }
  549. this.ProjectGetter.promises.findAllUsersProjects.resolves(
  550. this.allProjects
  551. )
  552. })
  553. it('should render the project/list-react page', function (done) {
  554. this.res.render = (pageName, opts) => {
  555. pageName.should.equal('project/list-react')
  556. done()
  557. }
  558. this.ProjectListController.projectListPage(this.req, this.res)
  559. })
  560. it('should omit one of the projects', function (done) {
  561. this.res.render = (pageName, opts) => {
  562. opts.prefetchedProjectsBlob.projects.length.should.equal(
  563. this.projects.length +
  564. this.readAndWrite.length +
  565. this.readOnly.length +
  566. this.tokenReadAndWrite.length +
  567. this.tokenReadOnly.length -
  568. 1
  569. )
  570. done()
  571. }
  572. this.ProjectListController.projectListPage(this.req, this.res)
  573. })
  574. })
  575. })