ProjectControllerTests.js 60 KB

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