ProjectControllerTests.js 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822
  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/ProjectController'
  10. )
  11. describe('ProjectController', 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. }
  21. this.settings = {
  22. apis: {
  23. chat: {
  24. url: 'chat.com',
  25. },
  26. },
  27. siteUrl: 'mysite.com',
  28. algolia: {},
  29. }
  30. this.brandVariationDetails = {
  31. id: '12',
  32. active: true,
  33. brand_name: 'The journal',
  34. home_url: 'http://www.thejournal.com/',
  35. publish_menu_link_html: 'Submit your paper to the <em>The Journal</em>',
  36. }
  37. this.token = 'some-token'
  38. this.ProjectDeleter = {
  39. deleteProject: sinon.stub().callsArg(2),
  40. restoreProject: sinon.stub().callsArg(1),
  41. findArchivedProjects: sinon.stub(),
  42. }
  43. this.ProjectDuplicator = {
  44. duplicate: sinon.stub().callsArgWith(3, null, { _id: this.project_id }),
  45. }
  46. this.ProjectCreationHandler = {
  47. createExampleProject: sinon
  48. .stub()
  49. .callsArgWith(2, null, { _id: this.project_id }),
  50. createBasicProject: sinon
  51. .stub()
  52. .callsArgWith(2, null, { _id: this.project_id }),
  53. }
  54. this.ProjectHistoryHandler = {
  55. ensureHistoryExistsForProject: sinon.stub().callsArg(1),
  56. }
  57. this.SubscriptionLocator = { getUsersSubscription: sinon.stub() }
  58. this.LimitationsManager = { hasPaidSubscription: sinon.stub() }
  59. this.TagsHandler = { getAllTags: sinon.stub() }
  60. this.NotificationsHandler = { getUserNotifications: sinon.stub() }
  61. this.UserModel = { findById: sinon.stub() }
  62. this.AuthorizationManager = {
  63. getPrivilegeLevelForProject: sinon.stub(),
  64. isRestrictedUser: sinon.stub().returns(false),
  65. }
  66. this.EditorController = { renameProject: sinon.stub() }
  67. this.InactiveProjectManager = { reactivateProjectIfRequired: sinon.stub() }
  68. this.ProjectUpdateHandler = { markAsOpened: sinon.stub() }
  69. this.ProjectGetter = {
  70. findAllUsersProjects: sinon.stub(),
  71. getProject: sinon.stub(),
  72. }
  73. this.ProjectHelper = {
  74. isArchived: sinon.stub(),
  75. isTrashed: sinon.stub(),
  76. isArchivedOrTrashed: sinon.stub(),
  77. getAllowedImagesForUser: sinon.stub().returns([]),
  78. }
  79. this.SessionManager = {
  80. getLoggedInUser: sinon.stub().callsArgWith(1, null, this.user),
  81. getLoggedInUserId: sinon.stub().returns(this.user._id),
  82. getSessionUser: sinon.stub().returns(this.user),
  83. isUserLoggedIn: sinon.stub().returns(true),
  84. }
  85. this.UserController = {
  86. logout: sinon.stub(),
  87. }
  88. this.TokenAccessHandler = {
  89. getRequestToken: sinon.stub().returns(this.token),
  90. protectTokens: sinon.stub(),
  91. }
  92. this.CollaboratorsGetter = {
  93. userIsTokenMember: sinon.stub().callsArgWith(2, null, false),
  94. }
  95. this.ProjectEntityHandler = {}
  96. this.NotificationBuilder = {
  97. ipMatcherAffiliation: sinon.stub().returns({ create: sinon.stub() }),
  98. }
  99. this.UserGetter = {
  100. getUserFullEmails: sinon.stub().yields(null, []),
  101. getUser: sinon
  102. .stub()
  103. .callsArgWith(2, null, { lastLoginIp: '192.170.18.2' }),
  104. }
  105. this.Features = {
  106. hasFeature: sinon.stub(),
  107. }
  108. this.BrandVariationsHandler = {
  109. getBrandVariationById: sinon
  110. .stub()
  111. .callsArgWith(1, null, this.brandVariationDetails),
  112. }
  113. this.TpdsProjectFlusher = {
  114. flushProjectToTpdsIfNeeded: sinon.stub().yields(),
  115. }
  116. this.Metrics = {
  117. Timer: class {
  118. done() {}
  119. },
  120. inc: sinon.stub(),
  121. }
  122. this.NewLogsUIHelper = {
  123. getNewLogsUIVariantForUser: sinon
  124. .stub()
  125. .returns({ newLogsUI: false, subvariant: null }),
  126. }
  127. this.SplitTestHandler = {
  128. promises: {
  129. getTestSegmentation: sinon.stub().resolves({ enabled: false }),
  130. },
  131. getTestSegmentation: sinon.stub().yields(null, { enabled: false }),
  132. }
  133. this.ProjectController = SandboxedModule.require(MODULE_PATH, {
  134. requires: {
  135. mongodb: { ObjectId },
  136. '@overleaf/settings': this.settings,
  137. '@overleaf/metrics': this.Metrics,
  138. '../SplitTests/SplitTestHandler': this.SplitTestHandler,
  139. './ProjectDeleter': this.ProjectDeleter,
  140. './ProjectDuplicator': this.ProjectDuplicator,
  141. './ProjectCreationHandler': this.ProjectCreationHandler,
  142. './ProjectHistoryHandler': this.ProjectHistoryHandler,
  143. '../Editor/EditorController': this.EditorController,
  144. '../User/UserController': this.UserController,
  145. './ProjectHelper': this.ProjectHelper,
  146. '../Subscription/SubscriptionLocator': this.SubscriptionLocator,
  147. '../Subscription/LimitationsManager': this.LimitationsManager,
  148. '../Tags/TagsHandler': this.TagsHandler,
  149. '../Notifications/NotificationsHandler': this.NotificationsHandler,
  150. '../../models/User': { User: this.UserModel },
  151. '../Authorization/AuthorizationManager': this.AuthorizationManager,
  152. '../InactiveData/InactiveProjectManager': this.InactiveProjectManager,
  153. './ProjectUpdateHandler': this.ProjectUpdateHandler,
  154. './ProjectGetter': this.ProjectGetter,
  155. './ProjectDetailsHandler': this.ProjectDetailsHandler,
  156. '../Authentication/SessionManager': this.SessionManager,
  157. '../TokenAccess/TokenAccessHandler': this.TokenAccessHandler,
  158. '../Collaborators/CollaboratorsGetter': this.CollaboratorsGetter,
  159. './ProjectEntityHandler': this.ProjectEntityHandler,
  160. '../../infrastructure/Features': this.Features,
  161. '../Notifications/NotificationsBuilder': this.NotificationBuilder,
  162. '../User/UserGetter': this.UserGetter,
  163. '../BrandVariations/BrandVariationsHandler': this
  164. .BrandVariationsHandler,
  165. '../ThirdPartyDataStore/TpdsProjectFlusher': this.TpdsProjectFlusher,
  166. '../../models/Project': {},
  167. '../Analytics/AnalyticsManager': { recordEvent: () => {} },
  168. '../../infrastructure/Modules': {
  169. hooks: { fire: sinon.stub().yields(null, []) },
  170. },
  171. '../Helpers/NewLogsUI': this.NewLogsUIHelper,
  172. },
  173. })
  174. this.projectName = '£12321jkj9ujkljds'
  175. this.req = {
  176. query: {},
  177. params: {
  178. Project_id: this.project_id,
  179. },
  180. headers: {},
  181. connection: {
  182. remoteAddress: '192.170.18.1',
  183. },
  184. session: {
  185. user: this.user,
  186. },
  187. body: {
  188. projectName: this.projectName,
  189. },
  190. i18n: {
  191. translate() {},
  192. },
  193. ip: '192.170.18.1',
  194. }
  195. this.res = {
  196. locals: {
  197. jsPath: 'js path here',
  198. },
  199. setTimeout: sinon.stub(),
  200. }
  201. })
  202. describe('updateProjectSettings', function () {
  203. it('should update the name', function (done) {
  204. this.EditorController.renameProject = sinon.stub().callsArg(2)
  205. this.req.body = { name: (this.name = 'New name') }
  206. this.res.sendStatus = code => {
  207. this.EditorController.renameProject
  208. .calledWith(this.project_id, this.name)
  209. .should.equal(true)
  210. code.should.equal(204)
  211. done()
  212. }
  213. this.ProjectController.updateProjectSettings(this.req, this.res)
  214. })
  215. it('should update the compiler', function (done) {
  216. this.EditorController.setCompiler = sinon.stub().callsArg(2)
  217. this.req.body = { compiler: (this.compiler = 'pdflatex') }
  218. this.res.sendStatus = code => {
  219. this.EditorController.setCompiler
  220. .calledWith(this.project_id, this.compiler)
  221. .should.equal(true)
  222. code.should.equal(204)
  223. done()
  224. }
  225. this.ProjectController.updateProjectSettings(this.req, this.res)
  226. })
  227. it('should update the imageName', function (done) {
  228. this.EditorController.setImageName = sinon.stub().callsArg(2)
  229. this.req.body = { imageName: (this.imageName = 'texlive-1234.5') }
  230. this.res.sendStatus = code => {
  231. this.EditorController.setImageName
  232. .calledWith(this.project_id, this.imageName)
  233. .should.equal(true)
  234. code.should.equal(204)
  235. done()
  236. }
  237. this.ProjectController.updateProjectSettings(this.req, this.res)
  238. })
  239. it('should update the spell check language', function (done) {
  240. this.EditorController.setSpellCheckLanguage = sinon.stub().callsArg(2)
  241. this.req.body = { spellCheckLanguage: (this.languageCode = 'fr') }
  242. this.res.sendStatus = code => {
  243. this.EditorController.setSpellCheckLanguage
  244. .calledWith(this.project_id, this.languageCode)
  245. .should.equal(true)
  246. code.should.equal(204)
  247. done()
  248. }
  249. this.ProjectController.updateProjectSettings(this.req, this.res)
  250. })
  251. it('should update the root doc', function (done) {
  252. this.EditorController.setRootDoc = sinon.stub().callsArg(2)
  253. this.req.body = { rootDocId: (this.rootDocId = 'root-doc-id') }
  254. this.res.sendStatus = code => {
  255. this.EditorController.setRootDoc
  256. .calledWith(this.project_id, this.rootDocId)
  257. .should.equal(true)
  258. code.should.equal(204)
  259. done()
  260. }
  261. this.ProjectController.updateProjectSettings(this.req, this.res)
  262. })
  263. })
  264. describe('updateProjectAdminSettings', function () {
  265. it('should update the public access level', function (done) {
  266. this.EditorController.setPublicAccessLevel = sinon.stub().callsArg(2)
  267. this.req.body = {
  268. publicAccessLevel: (this.publicAccessLevel = 'readonly'),
  269. }
  270. this.res.sendStatus = code => {
  271. this.EditorController.setPublicAccessLevel
  272. .calledWith(this.project_id, this.publicAccessLevel)
  273. .should.equal(true)
  274. code.should.equal(204)
  275. done()
  276. }
  277. this.ProjectController.updateProjectAdminSettings(this.req, this.res)
  278. })
  279. })
  280. describe('deleteProject', function () {
  281. it('should call the project deleter', function (done) {
  282. this.res.sendStatus = code => {
  283. this.ProjectDeleter.deleteProject
  284. .calledWith(this.project_id, {
  285. deleterUser: this.user,
  286. ipAddress: this.req.ip,
  287. })
  288. .should.equal(true)
  289. code.should.equal(200)
  290. done()
  291. }
  292. this.ProjectController.deleteProject(this.req, this.res)
  293. })
  294. })
  295. describe('restoreProject', function () {
  296. it('should tell the project deleter', function (done) {
  297. this.res.sendStatus = code => {
  298. this.ProjectDeleter.restoreProject
  299. .calledWith(this.project_id)
  300. .should.equal(true)
  301. code.should.equal(200)
  302. done()
  303. }
  304. this.ProjectController.restoreProject(this.req, this.res)
  305. })
  306. })
  307. describe('cloneProject', function () {
  308. it('should call the project duplicator', function (done) {
  309. this.res.send = json => {
  310. this.ProjectDuplicator.duplicate
  311. .calledWith(this.user, this.project_id, this.projectName)
  312. .should.equal(true)
  313. json.project_id.should.equal(this.project_id)
  314. done()
  315. }
  316. this.ProjectController.cloneProject(this.req, this.res)
  317. })
  318. })
  319. describe('newProject', function () {
  320. it('should call the projectCreationHandler with createExampleProject', function (done) {
  321. this.req.body.template = 'example'
  322. this.res.send = json => {
  323. this.ProjectCreationHandler.createExampleProject
  324. .calledWith(this.user._id, this.projectName)
  325. .should.equal(true)
  326. this.ProjectCreationHandler.createBasicProject.called.should.equal(
  327. false
  328. )
  329. done()
  330. }
  331. this.ProjectController.newProject(this.req, this.res)
  332. })
  333. it('should call the projectCreationHandler with createBasicProject', function (done) {
  334. this.req.body.template = 'basic'
  335. this.res.send = json => {
  336. this.ProjectCreationHandler.createExampleProject.called.should.equal(
  337. false
  338. )
  339. this.ProjectCreationHandler.createBasicProject
  340. .calledWith(this.user._id, this.projectName)
  341. .should.equal(true)
  342. done()
  343. }
  344. this.ProjectController.newProject(this.req, this.res)
  345. })
  346. })
  347. describe('projectListPage', function () {
  348. beforeEach(function () {
  349. this.tags = [
  350. { name: 1, project_ids: ['1', '2', '3'] },
  351. { name: 2, project_ids: ['a', '1'] },
  352. { name: 3, project_ids: ['a', 'b', 'c', 'd'] },
  353. ]
  354. this.notifications = [
  355. {
  356. _id: '1',
  357. user_id: '2',
  358. templateKey: '3',
  359. messageOpts: '4',
  360. key: '5',
  361. },
  362. ]
  363. this.projects = [
  364. { _id: 1, lastUpdated: 1, owner_ref: 'user-1' },
  365. {
  366. _id: 2,
  367. lastUpdated: 2,
  368. owner_ref: 'user-2',
  369. lastUpdatedBy: 'user-1',
  370. },
  371. ]
  372. this.collabertions = [{ _id: 5, lastUpdated: 5, owner_ref: 'user-1' }]
  373. this.readOnly = [{ _id: 3, lastUpdated: 3, owner_ref: 'user-1' }]
  374. this.tokenReadAndWrite = [{ _id: 6, lastUpdated: 5, owner_ref: 'user-4' }]
  375. this.tokenReadOnly = [{ _id: 7, lastUpdated: 4, owner_ref: 'user-5' }]
  376. this.allProjects = {
  377. owned: this.projects,
  378. readAndWrite: this.collabertions,
  379. readOnly: this.readOnly,
  380. tokenReadAndWrite: this.tokenReadAndWrite,
  381. tokenReadOnly: this.tokenReadOnly,
  382. }
  383. this.users = {
  384. 'user-1': {
  385. first_name: 'James',
  386. },
  387. 'user-2': {
  388. first_name: 'Henry',
  389. },
  390. }
  391. this.users[this.user._id] = this.user // Owner
  392. this.UserModel.findById = (id, fields, callback) => {
  393. callback(null, this.users[id])
  394. }
  395. this.UserGetter.getUser = (id, fields, callback) => {
  396. callback(null, this.users[id])
  397. }
  398. this.LimitationsManager.hasPaidSubscription.callsArgWith(1, null, false)
  399. this.TagsHandler.getAllTags.callsArgWith(1, null, this.tags)
  400. this.NotificationsHandler.getUserNotifications = sinon
  401. .stub()
  402. .callsArgWith(1, null, this.notifications, {})
  403. this.ProjectGetter.findAllUsersProjects.callsArgWith(
  404. 2,
  405. null,
  406. this.allProjects
  407. )
  408. })
  409. it('should render the project/list page', function (done) {
  410. this.res.render = (pageName, opts) => {
  411. pageName.should.equal('project/list')
  412. done()
  413. }
  414. this.ProjectController.projectListPage(this.req, this.res)
  415. })
  416. it('should send the tags', function (done) {
  417. this.res.render = (pageName, opts) => {
  418. opts.tags.length.should.equal(this.tags.length)
  419. done()
  420. }
  421. this.ProjectController.projectListPage(this.req, this.res)
  422. })
  423. it('should create trigger ip matcher notifications', function (done) {
  424. this.settings.overleaf = true
  425. this.res.render = (pageName, opts) => {
  426. this.NotificationBuilder.ipMatcherAffiliation.called.should.equal(true)
  427. done()
  428. }
  429. this.ProjectController.projectListPage(this.req, this.res)
  430. })
  431. it('should send the projects', function (done) {
  432. this.res.render = (pageName, opts) => {
  433. opts.projects.length.should.equal(
  434. this.projects.length +
  435. this.collabertions.length +
  436. this.readOnly.length +
  437. this.tokenReadAndWrite.length +
  438. this.tokenReadOnly.length
  439. )
  440. done()
  441. }
  442. this.ProjectController.projectListPage(this.req, this.res)
  443. })
  444. it('should send the user', function (done) {
  445. this.res.render = (pageName, opts) => {
  446. opts.user.should.deep.equal(this.user)
  447. done()
  448. }
  449. this.ProjectController.projectListPage(this.req, this.res)
  450. })
  451. it('should inject the users', function (done) {
  452. this.res.render = (pageName, opts) => {
  453. opts.projects[0].owner.should.equal(
  454. this.users[this.projects[0].owner_ref]
  455. )
  456. opts.projects[1].owner.should.equal(
  457. this.users[this.projects[1].owner_ref]
  458. )
  459. opts.projects[1].lastUpdatedBy.should.equal(
  460. this.users[this.projects[1].lastUpdatedBy]
  461. )
  462. done()
  463. }
  464. this.ProjectController.projectListPage(this.req, this.res)
  465. })
  466. it('should send hasSubscription == false when no subscription', function (done) {
  467. this.res.render = (pageName, opts) => {
  468. opts.hasSubscription.should.equal(false)
  469. done()
  470. }
  471. this.ProjectController.projectListPage(this.req, this.res)
  472. })
  473. it('should send hasSubscription == true when there is a subscription', function (done) {
  474. this.LimitationsManager.hasPaidSubscription = sinon
  475. .stub()
  476. .callsArgWith(1, null, true)
  477. this.res.render = (pageName, opts) => {
  478. opts.hasSubscription.should.equal(true)
  479. done()
  480. }
  481. this.ProjectController.projectListPage(this.req, this.res)
  482. })
  483. describe('front widget', function (done) {
  484. beforeEach(function () {
  485. this.settings.overleaf = {
  486. front_chat_widget_room_id: 'chat-room-id',
  487. }
  488. })
  489. it('should show for paid users', function (done) {
  490. this.user.features.github = true
  491. this.user.features.dropbox = true
  492. this.res.render = (pageName, opts) => {
  493. opts.frontChatWidgetRoomId.should.equal(
  494. this.settings.overleaf.front_chat_widget_room_id
  495. )
  496. done()
  497. }
  498. this.ProjectController.projectListPage(this.req, this.res)
  499. })
  500. it('should show for sample users', function (done) {
  501. this.user._id = ObjectId('588f3ddae8ebc1bac07c9f00') // last two digits
  502. this.res.render = (pageName, opts) => {
  503. opts.frontChatWidgetRoomId.should.equal(
  504. this.settings.overleaf.front_chat_widget_room_id
  505. )
  506. done()
  507. }
  508. this.ProjectController.projectListPage(this.req, this.res)
  509. })
  510. it('should not show for non sample users', function (done) {
  511. this.user._id = ObjectId('588f3ddae8ebc1bac07c9fff') // last two digits
  512. this.res.render = (pageName, opts) => {
  513. expect(opts.frontChatWidgetRoomId).to.equal(undefined)
  514. done()
  515. }
  516. this.ProjectController.projectListPage(this.req, this.res)
  517. })
  518. })
  519. describe('With Institution SSO feature', function () {
  520. beforeEach(function (done) {
  521. this.institutionEmail = 'test@overleaf.com'
  522. this.institutionName = 'Overleaf'
  523. this.Features.hasFeature.withArgs('saml').returns(true)
  524. this.Features.hasFeature.withArgs('affiliations').returns(true)
  525. this.Features.hasFeature.withArgs('overleaf-integration').returns(true)
  526. done()
  527. })
  528. it('should show institution SSO available notification for confirmed domains', function () {
  529. this.UserGetter.getUserFullEmails.yields(null, [
  530. {
  531. email: 'test@overleaf.com',
  532. affiliation: {
  533. institution: {
  534. id: 1,
  535. confirmed: true,
  536. name: 'Overleaf',
  537. ssoBeta: false,
  538. ssoEnabled: true,
  539. },
  540. },
  541. },
  542. ])
  543. this.res.render = (pageName, opts) => {
  544. expect(opts.notificationsInstitution).to.deep.include({
  545. email: this.institutionEmail,
  546. institutionId: 1,
  547. institutionName: this.institutionName,
  548. templateKey: 'notification_institution_sso_available',
  549. })
  550. }
  551. this.ProjectController.projectListPage(this.req, this.res)
  552. })
  553. it('should show a linked notification', function () {
  554. this.req.session.saml = {
  555. institutionEmail: this.institutionEmail,
  556. linked: {
  557. hasEntitlement: false,
  558. universityName: this.institutionName,
  559. },
  560. }
  561. this.res.render = (pageName, opts) => {
  562. expect(opts.notificationsInstitution).to.deep.include({
  563. email: this.institutionEmail,
  564. institutionName: this.institutionName,
  565. templateKey: 'notification_institution_sso_linked',
  566. })
  567. }
  568. this.ProjectController.projectListPage(this.req, this.res)
  569. })
  570. it('should show a linked another email notification', function () {
  571. // when they request to link an email but the institution returns
  572. // a different email
  573. this.res.render = (pageName, opts) => {
  574. expect(opts.notificationsInstitution).to.deep.include({
  575. institutionEmail: this.institutionEmail,
  576. requestedEmail: 'requested@overleaf.com',
  577. templateKey: 'notification_institution_sso_non_canonical',
  578. })
  579. }
  580. this.req.session.saml = {
  581. emailNonCanonical: this.institutionEmail,
  582. institutionEmail: this.institutionEmail,
  583. requestedEmail: 'requested@overleaf.com',
  584. linked: {
  585. hasEntitlement: false,
  586. universityName: this.institutionName,
  587. },
  588. }
  589. this.ProjectController.projectListPage(this.req, this.res)
  590. })
  591. it('should show a notification when intent was to register via SSO but account existed', function () {
  592. this.res.render = (pageName, opts) => {
  593. expect(opts.notificationsInstitution).to.deep.include({
  594. email: this.institutionEmail,
  595. templateKey: 'notification_institution_sso_already_registered',
  596. })
  597. }
  598. this.req.session.saml = {
  599. institutionEmail: this.institutionEmail,
  600. linked: {
  601. hasEntitlement: false,
  602. universityName: 'Overleaf',
  603. },
  604. registerIntercept: {
  605. id: 1,
  606. name: 'Example University',
  607. },
  608. }
  609. this.ProjectController.projectListPage(this.req, this.res)
  610. })
  611. it('should not show a register notification if the flow was abandoned', function () {
  612. // could initially start to register with an SSO email and then
  613. // abandon flow and login with an existing non-institution SSO email
  614. this.res.render = (pageName, opts) => {
  615. expect(opts.notificationsInstitution).to.deep.not.include({
  616. email: 'test@overleaf.com',
  617. templateKey: 'notification_institution_sso_already_registered',
  618. })
  619. }
  620. this.req.session.saml = {
  621. registerIntercept: {
  622. id: 1,
  623. name: 'Example University',
  624. },
  625. }
  626. this.ProjectController.projectListPage(this.req, this.res)
  627. })
  628. it('should show error notification', function () {
  629. this.res.render = (pageName, opts) => {
  630. expect(opts.notificationsInstitution.length).to.equal(1)
  631. expect(opts.notificationsInstitution[0].templateKey).to.equal(
  632. 'notification_institution_sso_error'
  633. )
  634. expect(opts.notificationsInstitution[0].error).to.be.instanceof(
  635. Errors.SAMLAlreadyLinkedError
  636. )
  637. }
  638. this.req.session.saml = {
  639. institutionEmail: this.institutionEmail,
  640. error: new Errors.SAMLAlreadyLinkedError(),
  641. }
  642. this.ProjectController.projectListPage(this.req, this.res)
  643. })
  644. describe('for an unconfirmed domain for an SSO institution', function () {
  645. beforeEach(function (done) {
  646. this.UserGetter.getUserFullEmails.yields(null, [
  647. {
  648. email: 'test@overleaf-uncofirmed.com',
  649. affiliation: {
  650. institution: {
  651. id: 1,
  652. confirmed: false,
  653. name: 'Overleaf',
  654. ssoBeta: false,
  655. ssoEnabled: true,
  656. },
  657. },
  658. },
  659. ])
  660. done()
  661. })
  662. it('should not show institution SSO available notification', function () {
  663. this.res.render = (pageName, opts) => {
  664. expect(opts.notificationsInstitution.length).to.equal(0)
  665. }
  666. this.ProjectController.projectListPage(this.req, this.res)
  667. })
  668. })
  669. describe('when linking/logging in initiated on institution side', function () {
  670. it('should not show a linked another email notification', function () {
  671. // this is only used when initated on Overleaf,
  672. // because we keep track of the requested email they tried to link
  673. this.res.render = (pageName, opts) => {
  674. expect(opts.notificationsInstitution).to.not.deep.include({
  675. institutionEmail: this.institutionEmail,
  676. requestedEmail: undefined,
  677. templateKey: 'notification_institution_sso_non_canonical',
  678. })
  679. }
  680. this.req.session.saml = {
  681. emailNonCanonical: this.institutionEmail,
  682. institutionEmail: this.institutionEmail,
  683. linked: {
  684. hasEntitlement: false,
  685. universityName: this.institutionName,
  686. },
  687. }
  688. this.ProjectController.projectListPage(this.req, this.res)
  689. })
  690. })
  691. describe('Institution with SSO beta testable', function () {
  692. beforeEach(function (done) {
  693. this.UserGetter.getUserFullEmails.yields(null, [
  694. {
  695. email: 'beta@beta.com',
  696. affiliation: {
  697. institution: {
  698. id: 2,
  699. confirmed: true,
  700. name: 'Beta University',
  701. ssoBeta: true,
  702. ssoEnabled: false,
  703. },
  704. },
  705. },
  706. ])
  707. done()
  708. })
  709. it('should show institution SSO available notification when on a beta testing session', function () {
  710. this.req.session.samlBeta = true
  711. this.res.render = (pageName, opts) => {
  712. expect(opts.notificationsInstitution).to.deep.include({
  713. email: 'beta@beta.com',
  714. institutionId: 2,
  715. institutionName: 'Beta University',
  716. templateKey: 'notification_institution_sso_available',
  717. })
  718. }
  719. this.ProjectController.projectListPage(this.req, this.res)
  720. })
  721. it('should not show institution SSO available notification when not on a beta testing session', function () {
  722. this.req.session.samlBeta = false
  723. this.res.render = (pageName, opts) => {
  724. expect(opts.notificationsInstitution).to.deep.not.include({
  725. email: 'test@overleaf.com',
  726. institutionId: 1,
  727. institutionName: 'Overleaf',
  728. templateKey: 'notification_institution_sso_available',
  729. })
  730. }
  731. this.ProjectController.projectListPage(this.req, this.res)
  732. })
  733. })
  734. })
  735. describe('Without Institution SSO feature', function () {
  736. beforeEach(function (done) {
  737. this.Features.hasFeature.withArgs('saml').returns(false)
  738. done()
  739. })
  740. it('should not show institution sso available notification', function () {
  741. this.res.render = (pageName, opts) => {
  742. expect(opts.notificationsInstitution).to.deep.not.include({
  743. email: 'test@overleaf.com',
  744. institutionId: 1,
  745. institutionName: 'Overleaf',
  746. templateKey: 'notification_institution_sso_available',
  747. })
  748. }
  749. this.ProjectController.projectListPage(this.req, this.res)
  750. })
  751. })
  752. })
  753. describe('projectListPage with duplicate projects', function () {
  754. beforeEach(function () {
  755. this.tags = [
  756. { name: 1, project_ids: ['1', '2', '3'] },
  757. { name: 2, project_ids: ['a', '1'] },
  758. { name: 3, project_ids: ['a', 'b', 'c', 'd'] },
  759. ]
  760. this.notifications = [
  761. {
  762. _id: '1',
  763. user_id: '2',
  764. templateKey: '3',
  765. messageOpts: '4',
  766. key: '5',
  767. },
  768. ]
  769. this.projects = [
  770. { _id: 1, lastUpdated: 1, owner_ref: 'user-1' },
  771. { _id: 2, lastUpdated: 2, owner_ref: 'user-2' },
  772. ]
  773. this.collabertions = [{ _id: 5, lastUpdated: 5, owner_ref: 'user-1' }]
  774. this.readOnly = [{ _id: 3, lastUpdated: 3, owner_ref: 'user-1' }]
  775. this.tokenReadAndWrite = [{ _id: 6, lastUpdated: 5, owner_ref: 'user-4' }]
  776. this.tokenReadOnly = [
  777. { _id: 6, lastUpdated: 5, owner_ref: 'user-4' }, // Also in tokenReadAndWrite
  778. { _id: 7, lastUpdated: 4, owner_ref: 'user-5' },
  779. ]
  780. this.allProjects = {
  781. owned: this.projects,
  782. readAndWrite: this.collabertions,
  783. readOnly: this.readOnly,
  784. tokenReadAndWrite: this.tokenReadAndWrite,
  785. tokenReadOnly: this.tokenReadOnly,
  786. }
  787. this.users = {
  788. 'user-1': {
  789. first_name: 'James',
  790. },
  791. 'user-2': {
  792. first_name: 'Henry',
  793. },
  794. }
  795. this.users[this.user._id] = this.user // Owner
  796. this.UserModel.findById = (id, fields, callback) => {
  797. callback(null, this.users[id])
  798. }
  799. this.LimitationsManager.hasPaidSubscription.callsArgWith(1, null, false)
  800. this.TagsHandler.getAllTags.callsArgWith(1, null, this.tags)
  801. this.NotificationsHandler.getUserNotifications = sinon
  802. .stub()
  803. .callsArgWith(1, null, this.notifications, {})
  804. this.ProjectGetter.findAllUsersProjects.callsArgWith(
  805. 2,
  806. null,
  807. this.allProjects
  808. )
  809. })
  810. it('should render the project/list page', function (done) {
  811. this.res.render = (pageName, opts) => {
  812. pageName.should.equal('project/list')
  813. done()
  814. }
  815. this.ProjectController.projectListPage(this.req, this.res)
  816. })
  817. it('should omit one of the projects', function (done) {
  818. this.res.render = (pageName, opts) => {
  819. opts.projects.length.should.equal(
  820. this.projects.length +
  821. this.collabertions.length +
  822. this.readOnly.length +
  823. this.tokenReadAndWrite.length +
  824. this.tokenReadOnly.length -
  825. 1
  826. )
  827. done()
  828. }
  829. this.ProjectController.projectListPage(this.req, this.res)
  830. })
  831. })
  832. describe('renameProject', function () {
  833. beforeEach(function () {
  834. this.newProjectName = 'my supper great new project'
  835. this.req.body.newProjectName = this.newProjectName
  836. })
  837. it('should call the editor controller', function (done) {
  838. this.EditorController.renameProject.callsArgWith(2)
  839. this.res.sendStatus = code => {
  840. code.should.equal(200)
  841. this.EditorController.renameProject
  842. .calledWith(this.project_id, this.newProjectName)
  843. .should.equal(true)
  844. done()
  845. }
  846. this.ProjectController.renameProject(this.req, this.res)
  847. })
  848. it('should send an error to next() if there is a problem', function (done) {
  849. let error
  850. this.EditorController.renameProject.callsArgWith(
  851. 2,
  852. (error = new Error('problem'))
  853. )
  854. const next = e => {
  855. e.should.equal(error)
  856. done()
  857. }
  858. this.ProjectController.renameProject(this.req, this.res, next)
  859. })
  860. })
  861. describe('loadEditor', function () {
  862. beforeEach(function () {
  863. this.settings.editorIsOpen = true
  864. this.project = {
  865. name: 'my proj',
  866. _id: '213123kjlkj',
  867. owner_ref: '59fc84d5fbea77482d436e1b',
  868. }
  869. this.brandedProject = {
  870. name: 'my branded proj',
  871. _id: '3252332',
  872. owner_ref: '59fc84d5fbea77482d436e1b',
  873. brandVariationId: '12',
  874. }
  875. this.user = {
  876. _id: '588f3ddae8ebc1bac07c9fa4',
  877. ace: {
  878. fontSize: 'massive',
  879. theme: 'sexy',
  880. },
  881. email: 'bob@bob.com',
  882. refProviders: {
  883. mendeley: { encrypted: 'aaaa' },
  884. zotero: { encrypted: 'bbbb' },
  885. },
  886. }
  887. this.ProjectGetter.getProject.callsArgWith(2, null, this.project)
  888. this.UserModel.findById.callsArgWith(2, null, this.user)
  889. this.SubscriptionLocator.getUsersSubscription.callsArgWith(1, null, {})
  890. this.AuthorizationManager.getPrivilegeLevelForProject.callsArgWith(
  891. 3,
  892. null,
  893. 'owner'
  894. )
  895. this.ProjectDeleter.unmarkAsDeletedByExternalSource = sinon.stub()
  896. this.InactiveProjectManager.reactivateProjectIfRequired.callsArgWith(1)
  897. this.ProjectUpdateHandler.markAsOpened.callsArgWith(1)
  898. })
  899. it('should render the project/editor page', function (done) {
  900. this.res.render = (pageName, opts) => {
  901. pageName.should.equal('project/editor')
  902. done()
  903. }
  904. this.ProjectController.loadEditor(this.req, this.res)
  905. })
  906. it('should add user', function (done) {
  907. this.res.render = (pageName, opts) => {
  908. opts.user.email.should.equal(this.user.email)
  909. done()
  910. }
  911. this.ProjectController.loadEditor(this.req, this.res)
  912. })
  913. it('should sanitize refProviders', function (done) {
  914. this.res.render = (_pageName, opts) => {
  915. expect(opts.user.refProviders).to.deep.equal({
  916. mendeley: true,
  917. zotero: true,
  918. })
  919. done()
  920. }
  921. this.ProjectController.loadEditor(this.req, this.res)
  922. })
  923. it('should add on userSettings', function (done) {
  924. this.res.render = (pageName, opts) => {
  925. opts.userSettings.fontSize.should.equal(this.user.ace.fontSize)
  926. opts.userSettings.editorTheme.should.equal(this.user.ace.theme)
  927. done()
  928. }
  929. this.ProjectController.loadEditor(this.req, this.res)
  930. })
  931. it('should add isRestrictedTokenMember', function (done) {
  932. this.res.render = (pageName, opts) => {
  933. opts.isRestrictedTokenMember.should.exist
  934. opts.isRestrictedTokenMember.should.equal(false)
  935. return done()
  936. }
  937. return this.ProjectController.loadEditor(this.req, this.res)
  938. })
  939. it('should set isRestrictedTokenMember when appropriate', function (done) {
  940. this.AuthorizationManager.isRestrictedUser.returns(true)
  941. this.res.render = (pageName, opts) => {
  942. opts.isRestrictedTokenMember.should.exist
  943. opts.isRestrictedTokenMember.should.equal(true)
  944. return done()
  945. }
  946. return this.ProjectController.loadEditor(this.req, this.res)
  947. })
  948. it('should render the closed page if the editor is closed', function (done) {
  949. this.settings.editorIsOpen = false
  950. this.res.render = (pageName, opts) => {
  951. pageName.should.equal('general/closed')
  952. done()
  953. }
  954. this.ProjectController.loadEditor(this.req, this.res)
  955. })
  956. it('should not render the page if the project can not be accessed', function (done) {
  957. this.AuthorizationManager.getPrivilegeLevelForProject = sinon
  958. .stub()
  959. .callsArgWith(3, null, null)
  960. this.res.sendStatus = (resCode, opts) => {
  961. resCode.should.equal(401)
  962. done()
  963. }
  964. this.ProjectController.loadEditor(this.req, this.res)
  965. })
  966. it('should reactivateProjectIfRequired', function (done) {
  967. this.res.render = (pageName, opts) => {
  968. this.InactiveProjectManager.reactivateProjectIfRequired
  969. .calledWith(this.project_id)
  970. .should.equal(true)
  971. done()
  972. }
  973. this.ProjectController.loadEditor(this.req, this.res)
  974. })
  975. it('should ensureHistoryExistsForProject if saas and project_history enabled', function (done) {
  976. this.Features.hasFeature.withArgs('saas').returns(true)
  977. this.settings.apis.project_history = 'enabled'
  978. this.res.render = (pageName, opts) => {
  979. this.ProjectHistoryHandler.ensureHistoryExistsForProject
  980. .calledWith(this.project_id)
  981. .should.equal(true)
  982. done()
  983. }
  984. this.ProjectController.loadEditor(this.req, this.res)
  985. })
  986. it('should mark project as opened', function (done) {
  987. this.res.render = (pageName, opts) => {
  988. this.ProjectUpdateHandler.markAsOpened
  989. .calledWith(this.project_id)
  990. .should.equal(true)
  991. done()
  992. }
  993. this.ProjectController.loadEditor(this.req, this.res)
  994. })
  995. it('should call the brand variations handler for branded projects', function (done) {
  996. this.ProjectGetter.getProject.callsArgWith(2, null, this.brandedProject)
  997. this.res.render = (pageName, opts) => {
  998. this.BrandVariationsHandler.getBrandVariationById
  999. .calledWith()
  1000. .should.equal(true)
  1001. done()
  1002. }
  1003. this.ProjectController.loadEditor(this.req, this.res)
  1004. })
  1005. it('should not call the brand variations handler for unbranded projects', function (done) {
  1006. this.res.render = (pageName, opts) => {
  1007. this.BrandVariationsHandler.getBrandVariationById.called.should.equal(
  1008. false
  1009. )
  1010. done()
  1011. }
  1012. this.ProjectController.loadEditor(this.req, this.res)
  1013. })
  1014. it('should expose the brand variation details as locals for branded projects', function (done) {
  1015. this.ProjectGetter.getProject.callsArgWith(2, null, this.brandedProject)
  1016. this.res.render = (pageName, opts) => {
  1017. opts.brandVariation.should.deep.equal(this.brandVariationDetails)
  1018. done()
  1019. }
  1020. this.ProjectController.loadEditor(this.req, this.res)
  1021. })
  1022. it('flushes the project to TPDS if a flush is pending', function (done) {
  1023. this.res.render = () => {
  1024. this.TpdsProjectFlusher.flushProjectToTpdsIfNeeded.should.have.been.calledWith(
  1025. this.project_id
  1026. )
  1027. done()
  1028. }
  1029. this.ProjectController.loadEditor(this.req, this.res)
  1030. })
  1031. describe('pdf caching feature flags', function () {
  1032. /* eslint-disable mocha/no-identical-title */
  1033. function showNoVariant() {
  1034. beforeEach(function () {
  1035. this.SplitTestHandler.getTestSegmentation = sinon
  1036. .stub()
  1037. .yields(null, { enabled: false })
  1038. })
  1039. }
  1040. function showVariant(variant) {
  1041. beforeEach(function () {
  1042. this.SplitTestHandler.getTestSegmentation = sinon
  1043. .stub()
  1044. .yields(null, { enabled: true, variant })
  1045. })
  1046. }
  1047. function expectBandwidthTrackingEnabled() {
  1048. it('should track pdf bandwidth', function (done) {
  1049. this.res.render = (pageName, opts) => {
  1050. expect(opts.trackPdfDownload).to.equal(true)
  1051. done()
  1052. }
  1053. this.ProjectController.loadEditor(this.req, this.res)
  1054. })
  1055. }
  1056. function expectPDFCachingEnabled() {
  1057. it('should enable pdf caching', function (done) {
  1058. this.res.render = (pageName, opts) => {
  1059. expect(opts.enablePdfCaching).to.equal(true)
  1060. done()
  1061. }
  1062. this.ProjectController.loadEditor(this.req, this.res)
  1063. })
  1064. }
  1065. function expectBandwidthTrackingDisabled() {
  1066. it('should not track pdf bandwidth', function (done) {
  1067. this.res.render = (pageName, opts) => {
  1068. expect(opts.trackPdfDownload).to.equal(false)
  1069. done()
  1070. }
  1071. this.ProjectController.loadEditor(this.req, this.res)
  1072. })
  1073. }
  1074. function expectPDFCachingDisabled() {
  1075. it('should disable pdf caching', function (done) {
  1076. this.res.render = (pageName, opts) => {
  1077. expect(opts.enablePdfCaching).to.equal(false)
  1078. done()
  1079. }
  1080. this.ProjectController.loadEditor(this.req, this.res)
  1081. })
  1082. }
  1083. function expectToCollectMetricsAndCachePDF() {
  1084. describe('with no query', function () {
  1085. expectBandwidthTrackingEnabled()
  1086. expectPDFCachingEnabled()
  1087. })
  1088. describe('with enable_pdf_caching=false', function () {
  1089. beforeEach(function () {
  1090. this.req.query.enable_pdf_caching = 'false'
  1091. })
  1092. expectBandwidthTrackingDisabled()
  1093. expectPDFCachingDisabled()
  1094. })
  1095. describe('with enable_pdf_caching=true', function () {
  1096. beforeEach(function () {
  1097. this.req.query.enable_pdf_caching = 'true'
  1098. })
  1099. expectBandwidthTrackingEnabled()
  1100. expectPDFCachingEnabled()
  1101. })
  1102. }
  1103. function expectToCollectMetricsOnly() {
  1104. describe('with no query', function () {
  1105. expectBandwidthTrackingEnabled()
  1106. expectPDFCachingDisabled()
  1107. })
  1108. describe('with enable_pdf_caching=false', function () {
  1109. beforeEach(function () {
  1110. this.req.query.enable_pdf_caching = 'false'
  1111. })
  1112. expectBandwidthTrackingDisabled()
  1113. expectPDFCachingDisabled()
  1114. })
  1115. describe('with enable_pdf_caching=true', function () {
  1116. beforeEach(function () {
  1117. this.req.query.enable_pdf_caching = 'true'
  1118. })
  1119. expectBandwidthTrackingEnabled()
  1120. expectPDFCachingDisabled()
  1121. })
  1122. }
  1123. function expectToCachePDFOnly() {
  1124. describe('with no query', function () {
  1125. expectBandwidthTrackingDisabled()
  1126. expectPDFCachingEnabled()
  1127. })
  1128. describe('with enable_pdf_caching=false', function () {
  1129. beforeEach(function () {
  1130. this.req.query.enable_pdf_caching = 'false'
  1131. })
  1132. expectBandwidthTrackingDisabled()
  1133. expectPDFCachingDisabled()
  1134. })
  1135. describe('with enable_pdf_caching=true', function () {
  1136. beforeEach(function () {
  1137. this.req.query.enable_pdf_caching = 'true'
  1138. })
  1139. expectBandwidthTrackingDisabled()
  1140. expectPDFCachingEnabled()
  1141. })
  1142. }
  1143. function expectToNotBeEnrolledAtAll() {
  1144. describe('with no query', function () {
  1145. expectBandwidthTrackingDisabled()
  1146. expectPDFCachingDisabled()
  1147. })
  1148. describe('with enable_pdf_caching=false', function () {
  1149. beforeEach(function () {
  1150. this.req.query.enable_pdf_caching = 'false'
  1151. })
  1152. expectBandwidthTrackingDisabled()
  1153. expectPDFCachingDisabled()
  1154. })
  1155. describe('with enable_pdf_caching=true', function () {
  1156. beforeEach(function () {
  1157. this.req.query.enable_pdf_caching = 'true'
  1158. })
  1159. expectBandwidthTrackingDisabled()
  1160. expectPDFCachingDisabled()
  1161. })
  1162. }
  1163. function tagAnonymous() {
  1164. beforeEach(function () {
  1165. this.SessionManager.isUserLoggedIn = sinon.stub().returns(false)
  1166. })
  1167. }
  1168. beforeEach(function () {
  1169. this.settings.enablePdfCaching = true
  1170. })
  1171. describe('during regular roll-out', function () {
  1172. describe('disabled', function () {
  1173. showNoVariant()
  1174. describe('regular user', function () {
  1175. expectToNotBeEnrolledAtAll()
  1176. })
  1177. describe('anonymous user', function () {
  1178. tagAnonymous()
  1179. expectToCachePDFOnly()
  1180. })
  1181. })
  1182. describe('variant=collect-metrics', function () {
  1183. showVariant('collect-metrics')
  1184. describe('regular user', function () {
  1185. expectToCollectMetricsOnly()
  1186. })
  1187. describe('anonymous user', function () {
  1188. tagAnonymous()
  1189. expectToCachePDFOnly()
  1190. })
  1191. })
  1192. describe('variant=collect-metrics-and-enable-caching', function () {
  1193. showVariant('collect-metrics-and-enable-caching')
  1194. describe('regular user', function () {
  1195. expectToCollectMetricsAndCachePDF()
  1196. })
  1197. describe('anonymous user', function () {
  1198. tagAnonymous()
  1199. expectToCachePDFOnly()
  1200. })
  1201. })
  1202. describe('variant=enable-caching-only', function () {
  1203. showVariant('enable-caching-only')
  1204. describe('regular user', function () {
  1205. expectToCachePDFOnly()
  1206. })
  1207. describe('anonymous user', function () {
  1208. tagAnonymous()
  1209. expectToCachePDFOnly()
  1210. })
  1211. })
  1212. })
  1213. })
  1214. describe('wsUrl', function () {
  1215. function checkLoadEditorWsMetric(metric) {
  1216. it(`should inc metric ${metric}`, function (done) {
  1217. this.res.render = () => {
  1218. this.Metrics.inc.calledWith(metric).should.equal(true)
  1219. done()
  1220. }
  1221. this.ProjectController.loadEditor(this.req, this.res)
  1222. })
  1223. }
  1224. function checkWsFallback(isBeta, isV2) {
  1225. describe('with ws=fallback', function () {
  1226. beforeEach(function () {
  1227. this.req.query = {}
  1228. this.req.query.ws = 'fallback'
  1229. })
  1230. it('should unset the wsUrl', function (done) {
  1231. this.res.render = (pageName, opts) => {
  1232. ;(opts.wsUrl || '/socket.io').should.equal('/socket.io')
  1233. done()
  1234. }
  1235. this.ProjectController.loadEditor(this.req, this.res)
  1236. })
  1237. checkLoadEditorWsMetric(
  1238. `load-editor-ws${isBeta ? '-beta' : ''}${
  1239. isV2 ? '-v2' : ''
  1240. }-fallback`
  1241. )
  1242. })
  1243. }
  1244. beforeEach(function () {
  1245. this.settings.wsUrl = '/other.socket.io'
  1246. })
  1247. it('should set the custom wsUrl', function (done) {
  1248. this.res.render = (pageName, opts) => {
  1249. opts.wsUrl.should.equal('/other.socket.io')
  1250. done()
  1251. }
  1252. this.ProjectController.loadEditor(this.req, this.res)
  1253. })
  1254. checkLoadEditorWsMetric('load-editor-ws')
  1255. checkWsFallback(false)
  1256. describe('beta program', function () {
  1257. beforeEach(function () {
  1258. this.settings.wsUrlBeta = '/beta.socket.io'
  1259. })
  1260. describe('for a normal user', function () {
  1261. it('should set the normal custom wsUrl', function (done) {
  1262. this.res.render = (pageName, opts) => {
  1263. opts.wsUrl.should.equal('/other.socket.io')
  1264. done()
  1265. }
  1266. this.ProjectController.loadEditor(this.req, this.res)
  1267. })
  1268. checkLoadEditorWsMetric('load-editor-ws')
  1269. checkWsFallback(false)
  1270. })
  1271. describe('for a beta user', function () {
  1272. beforeEach(function () {
  1273. this.user.betaProgram = true
  1274. })
  1275. it('should set the beta wsUrl', function (done) {
  1276. this.res.render = (pageName, opts) => {
  1277. opts.wsUrl.should.equal('/beta.socket.io')
  1278. done()
  1279. }
  1280. this.ProjectController.loadEditor(this.req, this.res)
  1281. })
  1282. checkLoadEditorWsMetric('load-editor-ws-beta')
  1283. checkWsFallback(true)
  1284. })
  1285. })
  1286. describe('v2-rollout', function () {
  1287. beforeEach(function () {
  1288. this.settings.wsUrlBeta = '/beta.socket.io'
  1289. this.settings.wsUrlV2 = '/socket.io.v2'
  1290. })
  1291. function checkNonMatch() {
  1292. it('should set the normal custom wsUrl', function (done) {
  1293. this.res.render = (pageName, opts) => {
  1294. opts.wsUrl.should.equal('/other.socket.io')
  1295. done()
  1296. }
  1297. this.ProjectController.loadEditor(this.req, this.res)
  1298. })
  1299. checkLoadEditorWsMetric('load-editor-ws')
  1300. checkWsFallback(false)
  1301. }
  1302. function checkMatch() {
  1303. it('should set the v2 wsUrl', function (done) {
  1304. this.res.render = (pageName, opts) => {
  1305. opts.wsUrl.should.equal('/socket.io.v2')
  1306. done()
  1307. }
  1308. this.ProjectController.loadEditor(this.req, this.res)
  1309. })
  1310. checkLoadEditorWsMetric('load-editor-ws-v2')
  1311. checkWsFallback(false, true)
  1312. }
  1313. function checkForBetaUser() {
  1314. describe('for a beta user', function () {
  1315. beforeEach(function () {
  1316. this.user.betaProgram = true
  1317. })
  1318. it('should set the beta wsUrl', function (done) {
  1319. this.res.render = (pageName, opts) => {
  1320. opts.wsUrl.should.equal('/beta.socket.io')
  1321. done()
  1322. }
  1323. this.ProjectController.loadEditor(this.req, this.res)
  1324. })
  1325. checkLoadEditorWsMetric('load-editor-ws-beta')
  1326. checkWsFallback(true)
  1327. })
  1328. }
  1329. describe('when the roll out percentage is 0', function () {
  1330. beforeEach(function () {
  1331. this.settings.wsUrlV2Percentage = 0
  1332. })
  1333. describe('when the projectId does not match (0)', function () {
  1334. beforeEach(function () {
  1335. this.req.params.Project_id = ObjectId.createFromTime(0)
  1336. })
  1337. checkNonMatch()
  1338. })
  1339. describe('when the projectId does not match (42)', function () {
  1340. beforeEach(function () {
  1341. this.req.params.Project_id = ObjectId.createFromTime(42)
  1342. })
  1343. checkNonMatch()
  1344. })
  1345. checkForBetaUser()
  1346. })
  1347. describe('when the roll out percentage is 1', function () {
  1348. beforeEach(function () {
  1349. this.settings.wsUrlV2Percentage = 1
  1350. })
  1351. describe('when the projectId matches (0)', function () {
  1352. beforeEach(function () {
  1353. this.req.params.Project_id = ObjectId.createFromTime(0)
  1354. })
  1355. checkMatch()
  1356. checkForBetaUser()
  1357. })
  1358. describe('when the projectId does not match (1)', function () {
  1359. beforeEach(function () {
  1360. this.req.params.Project_id = ObjectId.createFromTime(1)
  1361. })
  1362. checkNonMatch()
  1363. checkForBetaUser()
  1364. })
  1365. describe('when the projectId does not match (42)', function () {
  1366. beforeEach(function () {
  1367. this.req.params.Project_id = ObjectId.createFromTime(42)
  1368. })
  1369. checkNonMatch()
  1370. })
  1371. })
  1372. describe('when the roll out percentage is 10', function () {
  1373. beforeEach(function () {
  1374. this.settings.wsUrlV2Percentage = 10
  1375. })
  1376. describe('when the projectId matches (0)', function () {
  1377. beforeEach(function () {
  1378. this.req.params.Project_id = ObjectId.createFromTime(0)
  1379. })
  1380. checkMatch()
  1381. })
  1382. describe('when the projectId matches (9)', function () {
  1383. beforeEach(function () {
  1384. this.req.params.Project_id = ObjectId.createFromTime(9)
  1385. })
  1386. checkMatch()
  1387. checkForBetaUser()
  1388. })
  1389. describe('when the projectId does not match (10)', function () {
  1390. beforeEach(function () {
  1391. this.req.params.Project_id = ObjectId.createFromTime(10)
  1392. })
  1393. checkNonMatch()
  1394. })
  1395. describe('when the projectId does not match (42)', function () {
  1396. beforeEach(function () {
  1397. this.req.params.Project_id = ObjectId.createFromTime(42)
  1398. })
  1399. checkNonMatch()
  1400. checkForBetaUser()
  1401. })
  1402. })
  1403. describe('when the roll out percentage is 100', function () {
  1404. beforeEach(function () {
  1405. this.settings.wsUrlV2Percentage = 100
  1406. })
  1407. describe('when the projectId matches (0)', function () {
  1408. beforeEach(function () {
  1409. this.req.params.Project_id = ObjectId.createFromTime(0)
  1410. })
  1411. checkMatch()
  1412. checkForBetaUser()
  1413. })
  1414. describe('when the projectId matches (10)', function () {
  1415. beforeEach(function () {
  1416. this.req.params.Project_id = ObjectId.createFromTime(10)
  1417. })
  1418. checkMatch()
  1419. })
  1420. describe('when the projectId matches (42)', function () {
  1421. beforeEach(function () {
  1422. this.req.params.Project_id = ObjectId.createFromTime(42)
  1423. })
  1424. checkMatch()
  1425. })
  1426. describe('when the projectId matches (99)', function () {
  1427. beforeEach(function () {
  1428. this.req.params.Project_id = ObjectId.createFromTime(99)
  1429. })
  1430. checkMatch()
  1431. })
  1432. })
  1433. })
  1434. })
  1435. })
  1436. describe('userProjectsJson', function () {
  1437. beforeEach(function (done) {
  1438. const projects = [
  1439. {
  1440. archived: true,
  1441. trashed: true,
  1442. id: 'a',
  1443. name: 'A',
  1444. accessLevel: 'a',
  1445. somethingElse: 1,
  1446. },
  1447. {
  1448. archived: false,
  1449. id: 'b',
  1450. name: 'B',
  1451. accessLevel: 'b',
  1452. somethingElse: 1,
  1453. },
  1454. {
  1455. archived: false,
  1456. trashed: true,
  1457. id: 'c',
  1458. name: 'C',
  1459. accessLevel: 'c',
  1460. somethingElse: 1,
  1461. },
  1462. {
  1463. archived: false,
  1464. trashed: false,
  1465. id: 'd',
  1466. name: 'D',
  1467. accessLevel: 'd',
  1468. somethingElse: 1,
  1469. },
  1470. ]
  1471. this.ProjectHelper.isArchivedOrTrashed
  1472. .withArgs(projects[0], this.user._id)
  1473. .returns(true)
  1474. this.ProjectHelper.isArchivedOrTrashed
  1475. .withArgs(projects[1], this.user._id)
  1476. .returns(false)
  1477. this.ProjectHelper.isArchivedOrTrashed
  1478. .withArgs(projects[2], this.user._id)
  1479. .returns(true)
  1480. this.ProjectHelper.isArchivedOrTrashed
  1481. .withArgs(projects[3], this.user._id)
  1482. .returns(false)
  1483. this.ProjectGetter.findAllUsersProjects = sinon
  1484. .stub()
  1485. .callsArgWith(2, null, [])
  1486. this.ProjectController._buildProjectList = sinon.stub().returns(projects)
  1487. this.SessionManager.getLoggedInUserId = sinon
  1488. .stub()
  1489. .returns(this.user._id)
  1490. done()
  1491. })
  1492. it('should produce a list of projects', function (done) {
  1493. this.res.json = data => {
  1494. expect(data).to.deep.equal({
  1495. projects: [
  1496. { _id: 'b', name: 'B', accessLevel: 'b' },
  1497. { _id: 'd', name: 'D', accessLevel: 'd' },
  1498. ],
  1499. })
  1500. done()
  1501. }
  1502. this.ProjectController.userProjectsJson(this.req, this.res, this.next)
  1503. })
  1504. })
  1505. describe('projectEntitiesJson', function () {
  1506. beforeEach(function () {
  1507. this.SessionManager.getLoggedInUserId = sinon.stub().returns('abc')
  1508. this.req.params = { Project_id: 'abcd' }
  1509. this.project = { _id: 'abcd' }
  1510. this.docs = [
  1511. { path: '/things/b.txt', doc: true },
  1512. { path: '/main.tex', doc: true },
  1513. ]
  1514. this.files = [{ path: '/things/a.txt' }]
  1515. this.ProjectGetter.getProject = sinon
  1516. .stub()
  1517. .callsArgWith(1, null, this.project)
  1518. this.ProjectEntityHandler.getAllEntitiesFromProject = sinon
  1519. .stub()
  1520. .callsArgWith(1, null, this.docs, this.files)
  1521. })
  1522. it('should produce a list of entities', function (done) {
  1523. this.res.json = data => {
  1524. expect(data).to.deep.equal({
  1525. project_id: 'abcd',
  1526. entities: [
  1527. { path: '/main.tex', type: 'doc' },
  1528. { path: '/things/a.txt', type: 'file' },
  1529. { path: '/things/b.txt', type: 'doc' },
  1530. ],
  1531. })
  1532. expect(this.ProjectGetter.getProject.callCount).to.equal(1)
  1533. expect(
  1534. this.ProjectEntityHandler.getAllEntitiesFromProject.callCount
  1535. ).to.equal(1)
  1536. done()
  1537. }
  1538. this.ProjectController.projectEntitiesJson(this.req, this.res, this.next)
  1539. })
  1540. })
  1541. describe('_buildProjectViewModel', function () {
  1542. beforeEach(function () {
  1543. this.ProjectHelper.isArchived.returns(false)
  1544. this.ProjectHelper.isTrashed.returns(false)
  1545. this.project = {
  1546. _id: 'abcd',
  1547. name: 'netsenits',
  1548. lastUpdated: 1,
  1549. lastUpdatedBy: 2,
  1550. publicAccesLevel: 'private',
  1551. archived: false,
  1552. owner_ref: 'defg',
  1553. tokens: {
  1554. readAndWrite: '1abcd',
  1555. readAndWritePrefix: '1',
  1556. readOnly: 'neiotsranteoia',
  1557. },
  1558. }
  1559. })
  1560. describe('project not being archived or trashed', function () {
  1561. it('should produce a model of the project', function () {
  1562. const result = this.ProjectController._buildProjectViewModel(
  1563. this.project,
  1564. 'readAndWrite',
  1565. 'owner',
  1566. this.user._id
  1567. )
  1568. expect(result).to.exist
  1569. expect(result).to.be.an('object')
  1570. expect(result).to.deep.equal({
  1571. id: 'abcd',
  1572. name: 'netsenits',
  1573. lastUpdated: 1,
  1574. lastUpdatedBy: 2,
  1575. publicAccessLevel: 'private',
  1576. accessLevel: 'readAndWrite',
  1577. source: 'owner',
  1578. archived: false,
  1579. trashed: false,
  1580. owner_ref: 'defg',
  1581. isV1Project: false,
  1582. })
  1583. })
  1584. })
  1585. describe('project being simultaneously archived and trashed', function () {
  1586. beforeEach(function () {
  1587. this.ProjectHelper.isArchived.returns(true)
  1588. this.ProjectHelper.isTrashed.returns(true)
  1589. })
  1590. it('should produce a model of the project', function () {
  1591. const result = this.ProjectController._buildProjectViewModel(
  1592. this.project,
  1593. 'readAndWrite',
  1594. 'owner',
  1595. this.user._id
  1596. )
  1597. expect(result).to.exist
  1598. expect(result).to.be.an('object')
  1599. expect(result).to.deep.equal({
  1600. id: 'abcd',
  1601. name: 'netsenits',
  1602. lastUpdated: 1,
  1603. lastUpdatedBy: 2,
  1604. publicAccessLevel: 'private',
  1605. accessLevel: 'readAndWrite',
  1606. source: 'owner',
  1607. archived: true,
  1608. trashed: false,
  1609. owner_ref: 'defg',
  1610. isV1Project: false,
  1611. })
  1612. })
  1613. })
  1614. describe('when token-read-only access', function () {
  1615. it('should redact the owner and last-updated data', function () {
  1616. const result = this.ProjectController._buildProjectViewModel(
  1617. this.project,
  1618. 'readOnly',
  1619. 'token',
  1620. this.user._id
  1621. )
  1622. expect(result).to.exist
  1623. expect(result).to.be.an('object')
  1624. expect(result).to.deep.equal({
  1625. id: 'abcd',
  1626. name: 'netsenits',
  1627. lastUpdated: 1,
  1628. lastUpdatedBy: null,
  1629. publicAccessLevel: 'private',
  1630. accessLevel: 'readOnly',
  1631. source: 'token',
  1632. archived: false,
  1633. trashed: false,
  1634. owner_ref: null,
  1635. isV1Project: false,
  1636. })
  1637. })
  1638. })
  1639. })
  1640. describe('_isInPercentageRollout', function () {
  1641. before(function () {
  1642. this.ids = [
  1643. '5a05cd7621f9fe22be131740',
  1644. '5a05cd7821f9fe22be131741',
  1645. '5a05cd7921f9fe22be131742',
  1646. '5a05cd7a21f9fe22be131743',
  1647. '5a05cd7b21f9fe22be131744',
  1648. '5a05cd7c21f9fe22be131745',
  1649. '5a05cd7d21f9fe22be131746',
  1650. '5a05cd7e21f9fe22be131747',
  1651. '5a05cd7f21f9fe22be131748',
  1652. '5a05cd8021f9fe22be131749',
  1653. '5a05cd8021f9fe22be13174a',
  1654. '5a05cd8121f9fe22be13174b',
  1655. '5a05cd8221f9fe22be13174c',
  1656. '5a05cd8221f9fe22be13174d',
  1657. '5a05cd8321f9fe22be13174e',
  1658. '5a05cd8321f9fe22be13174f',
  1659. '5a05cd8421f9fe22be131750',
  1660. '5a05cd8421f9fe22be131751',
  1661. '5a05cd8421f9fe22be131752',
  1662. '5a05cd8521f9fe22be131753',
  1663. ]
  1664. })
  1665. it('should produce the expected results', function () {
  1666. expect(
  1667. this.ids.map(i =>
  1668. this.ProjectController._isInPercentageRollout('abcd', i, 50)
  1669. )
  1670. ).to.deep.equal([
  1671. false,
  1672. false,
  1673. false,
  1674. false,
  1675. false,
  1676. false,
  1677. true,
  1678. false,
  1679. true,
  1680. true,
  1681. true,
  1682. true,
  1683. true,
  1684. true,
  1685. false,
  1686. false,
  1687. false,
  1688. true,
  1689. false,
  1690. true,
  1691. ])
  1692. expect(
  1693. this.ids.map(i =>
  1694. this.ProjectController._isInPercentageRollout('efgh', i, 50)
  1695. )
  1696. ).to.deep.equal([
  1697. false,
  1698. false,
  1699. false,
  1700. false,
  1701. true,
  1702. false,
  1703. false,
  1704. true,
  1705. false,
  1706. false,
  1707. true,
  1708. true,
  1709. true,
  1710. false,
  1711. true,
  1712. false,
  1713. true,
  1714. true,
  1715. false,
  1716. false,
  1717. ])
  1718. })
  1719. })
  1720. })