ProjectListControllerTests.js 20 KB

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