ProjectControllerTests.js 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255
  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 MODULE_PATH = path.join(
  7. __dirname,
  8. '../../../../app/src/Features/Project/ProjectController'
  9. )
  10. describe('ProjectController', function () {
  11. beforeEach(function () {
  12. this.project_id = new ObjectId('abcdefabcdefabcdefabcdef')
  13. this.user = {
  14. _id: new ObjectId('123456123456123456123456'),
  15. email: 'test@overleaf.com',
  16. first_name: 'bjkdsjfk',
  17. features: {},
  18. emails: [{ email: 'test@overleaf.com' }],
  19. }
  20. this.settings = {
  21. apis: {
  22. chat: {
  23. url: 'chat.com',
  24. },
  25. },
  26. siteUrl: 'https://overleaf.com',
  27. algolia: {},
  28. }
  29. this.brandVariationDetails = {
  30. id: '12',
  31. active: true,
  32. brand_name: 'The journal',
  33. home_url: 'http://www.thejournal.com/',
  34. publish_menu_link_html: 'Submit your paper to the <em>The Journal</em>',
  35. }
  36. this.token = 'some-token'
  37. this.ProjectDeleter = {
  38. deleteProject: sinon.stub().callsArg(2),
  39. restoreProject: sinon.stub().callsArg(1),
  40. findArchivedProjects: sinon.stub(),
  41. }
  42. this.ProjectDuplicator = {
  43. duplicate: sinon.stub().callsArgWith(4, null, { _id: this.project_id }),
  44. }
  45. this.ProjectCreationHandler = {
  46. createExampleProject: sinon
  47. .stub()
  48. .callsArgWith(2, null, { _id: this.project_id }),
  49. createBasicProject: sinon
  50. .stub()
  51. .callsArgWith(2, null, { _id: this.project_id }),
  52. }
  53. this.SubscriptionLocator = { getUsersSubscription: sinon.stub() }
  54. this.LimitationsManager = {
  55. hasPaidSubscription: sinon.stub(),
  56. userIsMemberOfGroupSubscription: sinon
  57. .stub()
  58. .callsArgWith(1, null, false),
  59. }
  60. this.TagsHandler = {
  61. getTagsForProject: sinon.stub().callsArgWith(2, null, [
  62. {
  63. name: 'test',
  64. project_ids: [this.project_id],
  65. },
  66. ]),
  67. addProjectToTags: sinon.stub(),
  68. }
  69. this.UserModel = { findById: sinon.stub(), updateOne: sinon.stub() }
  70. this.AuthorizationManager = {
  71. getPrivilegeLevelForProject: sinon.stub(),
  72. isRestrictedUser: sinon.stub().returns(false),
  73. }
  74. this.EditorController = { renameProject: sinon.stub() }
  75. this.InactiveProjectManager = { reactivateProjectIfRequired: sinon.stub() }
  76. this.ProjectUpdateHandler = { markAsOpened: sinon.stub() }
  77. this.ProjectGetter = {
  78. findAllUsersProjects: sinon.stub(),
  79. getProject: sinon.stub(),
  80. }
  81. this.ProjectHelper = {
  82. isArchived: sinon.stub(),
  83. isTrashed: sinon.stub(),
  84. isArchivedOrTrashed: sinon.stub(),
  85. getAllowedImagesForUser: sinon.stub().returns([]),
  86. }
  87. this.SessionManager = {
  88. getLoggedInUser: sinon.stub().callsArgWith(1, null, this.user),
  89. getLoggedInUserId: sinon.stub().returns(this.user._id),
  90. getSessionUser: sinon.stub().returns(this.user),
  91. isUserLoggedIn: sinon.stub().returns(true),
  92. }
  93. this.UserController = {
  94. logout: sinon.stub(),
  95. }
  96. this.TokenAccessHandler = {
  97. getRequestToken: sinon.stub().returns(this.token),
  98. }
  99. this.CollaboratorsGetter = {
  100. userIsTokenMember: sinon.stub().callsArgWith(2, null, false),
  101. isUserInvitedMemberOfProject: sinon.stub().callsArgWith(2, null, true),
  102. }
  103. this.ProjectEntityHandler = {}
  104. this.UserGetter = {
  105. getUserFullEmails: sinon.stub().yields(null, []),
  106. getUser: sinon
  107. .stub()
  108. .callsArgWith(2, null, { lastLoginIp: '192.170.18.2' }),
  109. }
  110. this.Features = {
  111. hasFeature: sinon.stub(),
  112. }
  113. this.FeaturesUpdater = {
  114. featuresEpochIsCurrent: sinon.stub().returns(true),
  115. refreshFeatures: sinon.stub().yields(null, this.user),
  116. }
  117. this.BrandVariationsHandler = {
  118. getBrandVariationById: sinon
  119. .stub()
  120. .callsArgWith(1, null, this.brandVariationDetails),
  121. }
  122. this.TpdsProjectFlusher = {
  123. flushProjectToTpdsIfNeeded: sinon.stub().yields(),
  124. }
  125. this.Metrics = {
  126. Timer: class {
  127. done() {}
  128. },
  129. inc: sinon.stub(),
  130. }
  131. this.SplitTestHandler = {
  132. promises: {
  133. getAssignment: sinon.stub().resolves({ variant: 'default' }),
  134. },
  135. getAssignment: sinon.stub().yields(null, { variant: 'default' }),
  136. sessionMaintenance: sinon.stub().yields(),
  137. }
  138. this.InstitutionsFeatures = {
  139. hasLicence: sinon.stub().callsArgWith(1, null, false),
  140. }
  141. this.SubscriptionViewModelBuilder = {
  142. getBestSubscription: sinon.stub().yields(null, { type: 'free' }),
  143. }
  144. this.SurveyHandler = {
  145. getSurvey: sinon.stub().yields(null, {}),
  146. }
  147. this.ProjectAuditLogHandler = {
  148. addEntry: sinon.stub().yields(),
  149. }
  150. this.TutorialHandler = {
  151. getInactiveTutorials: sinon.stub().returns([]),
  152. }
  153. this.ProjectController = SandboxedModule.require(MODULE_PATH, {
  154. requires: {
  155. mongodb: { ObjectId },
  156. '@overleaf/settings': this.settings,
  157. '@overleaf/metrics': this.Metrics,
  158. '../SplitTests/SplitTestHandler': this.SplitTestHandler,
  159. './ProjectDeleter': this.ProjectDeleter,
  160. './ProjectDuplicator': this.ProjectDuplicator,
  161. './ProjectCreationHandler': this.ProjectCreationHandler,
  162. '../Editor/EditorController': this.EditorController,
  163. '../User/UserController': this.UserController,
  164. './ProjectHelper': this.ProjectHelper,
  165. '../Subscription/SubscriptionLocator': this.SubscriptionLocator,
  166. '../Subscription/LimitationsManager': this.LimitationsManager,
  167. '../Tags/TagsHandler': this.TagsHandler,
  168. '../../models/User': { User: this.UserModel },
  169. '../Authorization/AuthorizationManager': this.AuthorizationManager,
  170. '../InactiveData/InactiveProjectManager': this.InactiveProjectManager,
  171. './ProjectUpdateHandler': this.ProjectUpdateHandler,
  172. './ProjectGetter': this.ProjectGetter,
  173. './ProjectDetailsHandler': this.ProjectDetailsHandler,
  174. '../Authentication/SessionManager': this.SessionManager,
  175. '../TokenAccess/TokenAccessHandler': this.TokenAccessHandler,
  176. '../Collaborators/CollaboratorsGetter': this.CollaboratorsGetter,
  177. './ProjectEntityHandler': this.ProjectEntityHandler,
  178. '../../infrastructure/Features': this.Features,
  179. '../Subscription/FeaturesUpdater': this.FeaturesUpdater,
  180. '../User/UserGetter': this.UserGetter,
  181. '../BrandVariations/BrandVariationsHandler':
  182. this.BrandVariationsHandler,
  183. '../ThirdPartyDataStore/TpdsProjectFlusher': this.TpdsProjectFlusher,
  184. '../../models/Project': {},
  185. '../Analytics/AnalyticsManager': { recordEventForUser: () => {} },
  186. '../Subscription/SubscriptionViewModelBuilder':
  187. this.SubscriptionViewModelBuilder,
  188. '../Spelling/SpellingHandler': {
  189. getUserDictionary: sinon.stub().yields(null, []),
  190. },
  191. '../Institutions/InstitutionsFeatures': this.InstitutionsFeatures,
  192. '../Survey/SurveyHandler': this.SurveyHandler,
  193. './ProjectAuditLogHandler': this.ProjectAuditLogHandler,
  194. '../Tutorial/TutorialHandler': this.TutorialHandler,
  195. },
  196. })
  197. this.projectName = '£12321jkj9ujkljds'
  198. this.req = {
  199. query: {},
  200. params: {
  201. Project_id: this.project_id,
  202. },
  203. headers: {},
  204. connection: {
  205. remoteAddress: '192.170.18.1',
  206. },
  207. session: {
  208. user: this.user,
  209. },
  210. body: {
  211. projectName: this.projectName,
  212. },
  213. i18n: {
  214. translate() {},
  215. },
  216. ip: '192.170.18.1',
  217. }
  218. this.res = {
  219. locals: {
  220. jsPath: 'js path here',
  221. },
  222. setTimeout: sinon.stub(),
  223. }
  224. })
  225. describe('updateProjectSettings', function () {
  226. it('should update the name', function (done) {
  227. this.EditorController.renameProject = sinon.stub().callsArg(2)
  228. this.req.body = { name: (this.name = 'New name') }
  229. this.res.sendStatus = code => {
  230. this.EditorController.renameProject
  231. .calledWith(this.project_id, this.name)
  232. .should.equal(true)
  233. code.should.equal(204)
  234. done()
  235. }
  236. this.ProjectController.updateProjectSettings(this.req, this.res)
  237. })
  238. it('should update the compiler', function (done) {
  239. this.EditorController.setCompiler = sinon.stub().callsArg(2)
  240. this.req.body = { compiler: (this.compiler = 'pdflatex') }
  241. this.res.sendStatus = code => {
  242. this.EditorController.setCompiler
  243. .calledWith(this.project_id, this.compiler)
  244. .should.equal(true)
  245. code.should.equal(204)
  246. done()
  247. }
  248. this.ProjectController.updateProjectSettings(this.req, this.res)
  249. })
  250. it('should update the imageName', function (done) {
  251. this.EditorController.setImageName = sinon.stub().callsArg(2)
  252. this.req.body = { imageName: (this.imageName = 'texlive-1234.5') }
  253. this.res.sendStatus = code => {
  254. this.EditorController.setImageName
  255. .calledWith(this.project_id, this.imageName)
  256. .should.equal(true)
  257. code.should.equal(204)
  258. done()
  259. }
  260. this.ProjectController.updateProjectSettings(this.req, this.res)
  261. })
  262. it('should update the spell check language', function (done) {
  263. this.EditorController.setSpellCheckLanguage = sinon.stub().callsArg(2)
  264. this.req.body = { spellCheckLanguage: (this.languageCode = 'fr') }
  265. this.res.sendStatus = code => {
  266. this.EditorController.setSpellCheckLanguage
  267. .calledWith(this.project_id, this.languageCode)
  268. .should.equal(true)
  269. code.should.equal(204)
  270. done()
  271. }
  272. this.ProjectController.updateProjectSettings(this.req, this.res)
  273. })
  274. it('should update the root doc', function (done) {
  275. this.EditorController.setRootDoc = sinon.stub().callsArg(2)
  276. this.req.body = { rootDocId: (this.rootDocId = 'root-doc-id') }
  277. this.res.sendStatus = code => {
  278. this.EditorController.setRootDoc
  279. .calledWith(this.project_id, this.rootDocId)
  280. .should.equal(true)
  281. code.should.equal(204)
  282. done()
  283. }
  284. this.ProjectController.updateProjectSettings(this.req, this.res)
  285. })
  286. })
  287. describe('updateProjectAdminSettings', function () {
  288. it('should update the public access level', function (done) {
  289. this.EditorController.setPublicAccessLevel = sinon.stub().callsArg(2)
  290. this.req.body = {
  291. publicAccessLevel: 'readOnly',
  292. }
  293. this.res.sendStatus = code => {
  294. this.EditorController.setPublicAccessLevel
  295. .calledWith(this.project_id, 'readOnly')
  296. .should.equal(true)
  297. code.should.equal(204)
  298. done()
  299. }
  300. this.ProjectController.updateProjectAdminSettings(this.req, this.res)
  301. })
  302. it('should record the change in the project audit log', function (done) {
  303. this.EditorController.setPublicAccessLevel = sinon.stub().callsArg(2)
  304. this.req.body = {
  305. publicAccessLevel: 'readOnly',
  306. }
  307. this.res.sendStatus = code => {
  308. this.ProjectAuditLogHandler.addEntry
  309. .calledWith(
  310. this.project_id,
  311. 'toggle-access-level',
  312. this.user._id,
  313. this.req.ip,
  314. {
  315. publicAccessLevel: 'readOnly',
  316. status: 'OK',
  317. }
  318. )
  319. .should.equal(true)
  320. done()
  321. }
  322. this.ProjectController.updateProjectAdminSettings(this.req, this.res)
  323. })
  324. })
  325. describe('deleteProject', function () {
  326. it('should call the project deleter', function (done) {
  327. this.res.sendStatus = code => {
  328. this.ProjectDeleter.deleteProject
  329. .calledWith(this.project_id, {
  330. deleterUser: this.user,
  331. ipAddress: this.req.ip,
  332. })
  333. .should.equal(true)
  334. code.should.equal(200)
  335. done()
  336. }
  337. this.ProjectController.deleteProject(this.req, this.res)
  338. })
  339. })
  340. describe('restoreProject', function () {
  341. it('should tell the project deleter', function (done) {
  342. this.res.sendStatus = code => {
  343. this.ProjectDeleter.restoreProject
  344. .calledWith(this.project_id)
  345. .should.equal(true)
  346. code.should.equal(200)
  347. done()
  348. }
  349. this.ProjectController.restoreProject(this.req, this.res)
  350. })
  351. })
  352. describe('cloneProject', function () {
  353. it('should call the project duplicator', function (done) {
  354. this.res.json = json => {
  355. this.ProjectDuplicator.duplicate
  356. .calledWith(this.user, this.project_id, this.projectName)
  357. .should.equal(true)
  358. json.project_id.should.equal(this.project_id)
  359. done()
  360. }
  361. this.ProjectController.cloneProject(this.req, this.res)
  362. })
  363. })
  364. describe('newProject', function () {
  365. it('should call the projectCreationHandler with createExampleProject', function (done) {
  366. this.req.body.template = 'example'
  367. this.res.json = json => {
  368. this.ProjectCreationHandler.createExampleProject
  369. .calledWith(this.user._id, this.projectName)
  370. .should.equal(true)
  371. this.ProjectCreationHandler.createBasicProject.called.should.equal(
  372. false
  373. )
  374. done()
  375. }
  376. this.ProjectController.newProject(this.req, this.res)
  377. })
  378. it('should call the projectCreationHandler with createBasicProject', function (done) {
  379. this.req.body.template = 'basic'
  380. this.res.json = json => {
  381. this.ProjectCreationHandler.createExampleProject.called.should.equal(
  382. false
  383. )
  384. this.ProjectCreationHandler.createBasicProject
  385. .calledWith(this.user._id, this.projectName)
  386. .should.equal(true)
  387. done()
  388. }
  389. this.ProjectController.newProject(this.req, this.res)
  390. })
  391. })
  392. describe('renameProject', function () {
  393. beforeEach(function () {
  394. this.newProjectName = 'my supper great new project'
  395. this.req.body.newProjectName = this.newProjectName
  396. })
  397. it('should call the editor controller', function (done) {
  398. this.EditorController.renameProject.callsArgWith(2)
  399. this.res.sendStatus = code => {
  400. code.should.equal(200)
  401. this.EditorController.renameProject
  402. .calledWith(this.project_id, this.newProjectName)
  403. .should.equal(true)
  404. done()
  405. }
  406. this.ProjectController.renameProject(this.req, this.res)
  407. })
  408. it('should send an error to next() if there is a problem', function (done) {
  409. let error
  410. this.EditorController.renameProject.callsArgWith(
  411. 2,
  412. (error = new Error('problem'))
  413. )
  414. const next = e => {
  415. e.should.equal(error)
  416. done()
  417. }
  418. this.ProjectController.renameProject(this.req, this.res, next)
  419. })
  420. })
  421. describe('loadEditor', function () {
  422. beforeEach(function () {
  423. this.settings.editorIsOpen = true
  424. this.project = {
  425. name: 'my proj',
  426. _id: '213123kjlkj',
  427. owner_ref: '59fc84d5fbea77482d436e1b',
  428. }
  429. this.brandedProject = {
  430. name: 'my branded proj',
  431. _id: '3252332',
  432. owner_ref: '59fc84d5fbea77482d436e1b',
  433. brandVariationId: '12',
  434. }
  435. this.user = {
  436. _id: this.user._id,
  437. ace: {
  438. fontSize: 'massive',
  439. theme: 'sexy',
  440. },
  441. email: 'bob@bob.com',
  442. refProviders: {
  443. mendeley: { encrypted: 'aaaa' },
  444. zotero: { encrypted: 'bbbb' },
  445. },
  446. }
  447. this.ProjectGetter.getProject.callsArgWith(2, null, this.project)
  448. this.UserModel.findById.callsArgWith(2, null, this.user)
  449. this.SubscriptionLocator.getUsersSubscription.callsArgWith(1, null, {})
  450. this.AuthorizationManager.getPrivilegeLevelForProject.callsArgWith(
  451. 3,
  452. null,
  453. 'owner'
  454. )
  455. this.ProjectDeleter.unmarkAsDeletedByExternalSource = sinon.stub()
  456. this.InactiveProjectManager.reactivateProjectIfRequired.callsArgWith(1)
  457. this.ProjectUpdateHandler.markAsOpened.callsArgWith(1)
  458. })
  459. it('should render the project/editor page', function (done) {
  460. this.res.render = (pageName, opts) => {
  461. pageName.should.equal('project/editor')
  462. done()
  463. }
  464. this.ProjectController.loadEditor(this.req, this.res)
  465. })
  466. it('should add user', function (done) {
  467. this.res.render = (pageName, opts) => {
  468. opts.user.email.should.equal(this.user.email)
  469. done()
  470. }
  471. this.ProjectController.loadEditor(this.req, this.res)
  472. })
  473. it('should sanitize refProviders', function (done) {
  474. this.res.render = (_pageName, opts) => {
  475. expect(opts.user.refProviders).to.deep.equal({
  476. mendeley: true,
  477. zotero: true,
  478. })
  479. done()
  480. }
  481. this.ProjectController.loadEditor(this.req, this.res)
  482. })
  483. it('should add on userSettings', function (done) {
  484. this.res.render = (pageName, opts) => {
  485. opts.userSettings.fontSize.should.equal(this.user.ace.fontSize)
  486. opts.userSettings.editorTheme.should.equal(this.user.ace.theme)
  487. done()
  488. }
  489. this.ProjectController.loadEditor(this.req, this.res)
  490. })
  491. it('should add isRestrictedTokenMember', function (done) {
  492. this.AuthorizationManager.isRestrictedUser.returns(false)
  493. this.res.render = (pageName, opts) => {
  494. opts.isRestrictedTokenMember.should.exist
  495. opts.isRestrictedTokenMember.should.equal(false)
  496. return done()
  497. }
  498. return this.ProjectController.loadEditor(this.req, this.res)
  499. })
  500. it('should set isRestrictedTokenMember when appropriate', function (done) {
  501. this.AuthorizationManager.isRestrictedUser.returns(true)
  502. this.res.render = (pageName, opts) => {
  503. opts.isRestrictedTokenMember.should.exist
  504. opts.isRestrictedTokenMember.should.equal(true)
  505. return done()
  506. }
  507. return this.ProjectController.loadEditor(this.req, this.res)
  508. })
  509. it('should invoke the session maintenance for logged in user', function (done) {
  510. this.res.render = () => {
  511. this.SplitTestHandler.sessionMaintenance.should.have.been.calledWith(
  512. this.req,
  513. this.user
  514. )
  515. done()
  516. }
  517. this.ProjectController.loadEditor(this.req, this.res)
  518. })
  519. it('should invoke the session maintenance for anonymous user', function (done) {
  520. this.SessionManager.getLoggedInUserId.returns(null)
  521. this.res.render = () => {
  522. this.SplitTestHandler.sessionMaintenance.should.have.been.calledWith(
  523. this.req
  524. )
  525. done()
  526. }
  527. this.ProjectController.loadEditor(this.req, this.res)
  528. })
  529. it('should render the closed page if the editor is closed', function (done) {
  530. this.settings.editorIsOpen = false
  531. this.res.render = (pageName, opts) => {
  532. pageName.should.equal('general/closed')
  533. done()
  534. }
  535. this.ProjectController.loadEditor(this.req, this.res)
  536. })
  537. it('should not render the page if the project can not be accessed', function (done) {
  538. this.AuthorizationManager.getPrivilegeLevelForProject = sinon
  539. .stub()
  540. .callsArgWith(3, null, null)
  541. this.res.sendStatus = (resCode, opts) => {
  542. resCode.should.equal(401)
  543. done()
  544. }
  545. this.ProjectController.loadEditor(this.req, this.res)
  546. })
  547. it('should reactivateProjectIfRequired', function (done) {
  548. this.res.render = (pageName, opts) => {
  549. this.InactiveProjectManager.reactivateProjectIfRequired
  550. .calledWith(this.project_id)
  551. .should.equal(true)
  552. done()
  553. }
  554. this.ProjectController.loadEditor(this.req, this.res)
  555. })
  556. it('should mark user as active', function (done) {
  557. this.res.render = (pageName, opts) => {
  558. expect(this.UserModel.updateOne).to.have.been.calledOnce
  559. expect(this.UserModel.updateOne.args[0][0]).to.deep.equal({
  560. _id: new ObjectId(this.user._id),
  561. })
  562. expect(this.UserModel.updateOne.args[0][1].$set.lastActive).to.exist
  563. done()
  564. }
  565. this.ProjectController.loadEditor(this.req, this.res)
  566. })
  567. it('should mark project as opened', function (done) {
  568. this.res.render = (pageName, opts) => {
  569. this.ProjectUpdateHandler.markAsOpened
  570. .calledWith(this.project_id)
  571. .should.equal(true)
  572. done()
  573. }
  574. this.ProjectController.loadEditor(this.req, this.res)
  575. })
  576. it('should call the brand variations handler for branded projects', function (done) {
  577. this.ProjectGetter.getProject.callsArgWith(2, null, this.brandedProject)
  578. this.res.render = (pageName, opts) => {
  579. this.BrandVariationsHandler.getBrandVariationById
  580. .calledWith()
  581. .should.equal(true)
  582. done()
  583. }
  584. this.ProjectController.loadEditor(this.req, this.res)
  585. })
  586. it('should not call the brand variations handler for unbranded projects', function (done) {
  587. this.res.render = (pageName, opts) => {
  588. this.BrandVariationsHandler.getBrandVariationById.called.should.equal(
  589. false
  590. )
  591. done()
  592. }
  593. this.ProjectController.loadEditor(this.req, this.res)
  594. })
  595. it('should expose the brand variation details as locals for branded projects', function (done) {
  596. this.ProjectGetter.getProject.callsArgWith(2, null, this.brandedProject)
  597. this.res.render = (pageName, opts) => {
  598. opts.brandVariation.should.deep.equal(this.brandVariationDetails)
  599. done()
  600. }
  601. this.ProjectController.loadEditor(this.req, this.res)
  602. })
  603. it('flushes the project to TPDS if a flush is pending', function (done) {
  604. this.res.render = () => {
  605. this.TpdsProjectFlusher.flushProjectToTpdsIfNeeded.should.have.been.calledWith(
  606. this.project_id
  607. )
  608. done()
  609. }
  610. this.ProjectController.loadEditor(this.req, this.res)
  611. })
  612. it('should refresh the user features if the epoch is outdated', function (done) {
  613. this.FeaturesUpdater.featuresEpochIsCurrent = sinon.stub().returns(false)
  614. this.res.render = () => {
  615. this.FeaturesUpdater.refreshFeatures.should.have.been.calledWith(
  616. this.user._id,
  617. 'load-editor'
  618. )
  619. done()
  620. }
  621. this.ProjectController.loadEditor(this.req, this.res)
  622. })
  623. describe('wsUrl', function () {
  624. function checkLoadEditorWsMetric(metric) {
  625. it(`should inc metric ${metric}`, function (done) {
  626. this.res.render = () => {
  627. this.Metrics.inc.calledWith(metric).should.equal(true)
  628. done()
  629. }
  630. this.ProjectController.loadEditor(this.req, this.res)
  631. })
  632. }
  633. function checkWsFallback(isBeta, isV2) {
  634. describe('with ws=fallback', function () {
  635. beforeEach(function () {
  636. this.req.query = {}
  637. this.req.query.ws = 'fallback'
  638. })
  639. it('should unset the wsUrl', function (done) {
  640. this.res.render = (pageName, opts) => {
  641. ;(opts.wsUrl || '/socket.io').should.equal('/socket.io')
  642. done()
  643. }
  644. this.ProjectController.loadEditor(this.req, this.res)
  645. })
  646. checkLoadEditorWsMetric(
  647. `load-editor-ws${isBeta ? '-beta' : ''}${
  648. isV2 ? '-v2' : ''
  649. }-fallback`
  650. )
  651. })
  652. }
  653. beforeEach(function () {
  654. this.settings.wsUrl = '/other.socket.io'
  655. })
  656. it('should set the custom wsUrl', function (done) {
  657. this.res.render = (pageName, opts) => {
  658. opts.wsUrl.should.equal('/other.socket.io')
  659. done()
  660. }
  661. this.ProjectController.loadEditor(this.req, this.res)
  662. })
  663. checkLoadEditorWsMetric('load-editor-ws')
  664. checkWsFallback(false)
  665. describe('beta program', function () {
  666. beforeEach(function () {
  667. this.settings.wsUrlBeta = '/beta.socket.io'
  668. })
  669. describe('for a normal user', function () {
  670. it('should set the normal custom wsUrl', function (done) {
  671. this.res.render = (pageName, opts) => {
  672. opts.wsUrl.should.equal('/other.socket.io')
  673. done()
  674. }
  675. this.ProjectController.loadEditor(this.req, this.res)
  676. })
  677. checkLoadEditorWsMetric('load-editor-ws')
  678. checkWsFallback(false)
  679. })
  680. describe('for a beta user', function () {
  681. beforeEach(function () {
  682. this.user.betaProgram = true
  683. })
  684. it('should set the beta wsUrl', function (done) {
  685. this.res.render = (pageName, opts) => {
  686. opts.wsUrl.should.equal('/beta.socket.io')
  687. done()
  688. }
  689. this.ProjectController.loadEditor(this.req, this.res)
  690. })
  691. checkLoadEditorWsMetric('load-editor-ws-beta')
  692. checkWsFallback(true)
  693. })
  694. })
  695. describe('v2-rollout', function () {
  696. beforeEach(function () {
  697. this.settings.wsUrlBeta = '/beta.socket.io'
  698. this.settings.wsUrlV2 = '/socket.io.v2'
  699. })
  700. function checkNonMatch() {
  701. it('should set the normal custom wsUrl', function (done) {
  702. this.res.render = (pageName, opts) => {
  703. opts.wsUrl.should.equal('/other.socket.io')
  704. done()
  705. }
  706. this.ProjectController.loadEditor(this.req, this.res)
  707. })
  708. checkLoadEditorWsMetric('load-editor-ws')
  709. checkWsFallback(false)
  710. }
  711. function checkMatch() {
  712. it('should set the v2 wsUrl', function (done) {
  713. this.res.render = (pageName, opts) => {
  714. opts.wsUrl.should.equal('/socket.io.v2')
  715. done()
  716. }
  717. this.ProjectController.loadEditor(this.req, this.res)
  718. })
  719. checkLoadEditorWsMetric('load-editor-ws-v2')
  720. checkWsFallback(false, true)
  721. }
  722. function checkForBetaUser() {
  723. describe('for a beta user', function () {
  724. beforeEach(function () {
  725. this.user.betaProgram = true
  726. })
  727. it('should set the beta wsUrl', function (done) {
  728. this.res.render = (pageName, opts) => {
  729. opts.wsUrl.should.equal('/beta.socket.io')
  730. done()
  731. }
  732. this.ProjectController.loadEditor(this.req, this.res)
  733. })
  734. checkLoadEditorWsMetric('load-editor-ws-beta')
  735. checkWsFallback(true)
  736. })
  737. }
  738. describe('when the roll out percentage is 0', function () {
  739. beforeEach(function () {
  740. this.settings.wsUrlV2Percentage = 0
  741. })
  742. describe('when the projectId does not match (0)', function () {
  743. beforeEach(function () {
  744. this.req.params.Project_id = ObjectId.createFromTime(0)
  745. })
  746. checkNonMatch()
  747. })
  748. describe('when the projectId does not match (42)', function () {
  749. beforeEach(function () {
  750. this.req.params.Project_id = ObjectId.createFromTime(42)
  751. })
  752. checkNonMatch()
  753. })
  754. checkForBetaUser()
  755. })
  756. describe('when the roll out percentage is 1', function () {
  757. beforeEach(function () {
  758. this.settings.wsUrlV2Percentage = 1
  759. })
  760. describe('when the projectId matches (0)', function () {
  761. beforeEach(function () {
  762. this.req.params.Project_id = ObjectId.createFromTime(0)
  763. })
  764. checkMatch()
  765. checkForBetaUser()
  766. })
  767. describe('when the projectId does not match (1)', function () {
  768. beforeEach(function () {
  769. this.req.params.Project_id = ObjectId.createFromTime(1)
  770. })
  771. checkNonMatch()
  772. checkForBetaUser()
  773. })
  774. describe('when the projectId does not match (42)', function () {
  775. beforeEach(function () {
  776. this.req.params.Project_id = ObjectId.createFromTime(42)
  777. })
  778. checkNonMatch()
  779. })
  780. })
  781. describe('when the roll out percentage is 10', function () {
  782. beforeEach(function () {
  783. this.settings.wsUrlV2Percentage = 10
  784. })
  785. describe('when the projectId matches (0)', function () {
  786. beforeEach(function () {
  787. this.req.params.Project_id = ObjectId.createFromTime(0)
  788. })
  789. checkMatch()
  790. })
  791. describe('when the projectId matches (9)', function () {
  792. beforeEach(function () {
  793. this.req.params.Project_id = ObjectId.createFromTime(9)
  794. })
  795. checkMatch()
  796. checkForBetaUser()
  797. })
  798. describe('when the projectId does not match (10)', function () {
  799. beforeEach(function () {
  800. this.req.params.Project_id = ObjectId.createFromTime(10)
  801. })
  802. checkNonMatch()
  803. })
  804. describe('when the projectId does not match (42)', function () {
  805. beforeEach(function () {
  806. this.req.params.Project_id = ObjectId.createFromTime(42)
  807. })
  808. checkNonMatch()
  809. checkForBetaUser()
  810. })
  811. })
  812. describe('when the roll out percentage is 100', function () {
  813. beforeEach(function () {
  814. this.settings.wsUrlV2Percentage = 100
  815. })
  816. describe('when the projectId matches (0)', function () {
  817. beforeEach(function () {
  818. this.req.params.Project_id = ObjectId.createFromTime(0)
  819. })
  820. checkMatch()
  821. checkForBetaUser()
  822. })
  823. describe('when the projectId matches (10)', function () {
  824. beforeEach(function () {
  825. this.req.params.Project_id = ObjectId.createFromTime(10)
  826. })
  827. checkMatch()
  828. })
  829. describe('when the projectId matches (42)', function () {
  830. beforeEach(function () {
  831. this.req.params.Project_id = ObjectId.createFromTime(42)
  832. })
  833. checkMatch()
  834. })
  835. describe('when the projectId matches (99)', function () {
  836. beforeEach(function () {
  837. this.req.params.Project_id = ObjectId.createFromTime(99)
  838. })
  839. checkMatch()
  840. })
  841. })
  842. })
  843. })
  844. describe('upgrade prompt (on header and share project modal)', function () {
  845. beforeEach(function () {
  846. // default to saas enabled
  847. this.Features.hasFeature.withArgs('saas').returns(true)
  848. // default to without a subscription
  849. this.SubscriptionLocator.getUsersSubscription = sinon
  850. .stub()
  851. .callsArgWith(1, null, null)
  852. })
  853. it('should not show without the saas feature', function (done) {
  854. this.Features.hasFeature.withArgs('saas').returns(false)
  855. this.res.render = (pageName, opts) => {
  856. expect(opts.showUpgradePrompt).to.equal(false)
  857. done()
  858. }
  859. this.ProjectController.loadEditor(this.req, this.res)
  860. })
  861. it('should show for a user without a subscription or only non-paid affiliations', function (done) {
  862. this.res.render = (pageName, opts) => {
  863. expect(opts.showUpgradePrompt).to.equal(true)
  864. done()
  865. }
  866. this.ProjectController.loadEditor(this.req, this.res)
  867. })
  868. it('should not show for a user with a personal subscription', function (done) {
  869. this.SubscriptionLocator.getUsersSubscription = sinon
  870. .stub()
  871. .callsArgWith(1, null, {})
  872. this.res.render = (pageName, opts) => {
  873. expect(opts.showUpgradePrompt).to.equal(false)
  874. done()
  875. }
  876. this.ProjectController.loadEditor(this.req, this.res)
  877. })
  878. it('should not show for a user who is a member of a group subscription', function (done) {
  879. this.LimitationsManager.userIsMemberOfGroupSubscription = sinon
  880. .stub()
  881. .callsArgWith(1, null, true)
  882. this.res.render = (pageName, opts) => {
  883. expect(opts.showUpgradePrompt).to.equal(false)
  884. done()
  885. }
  886. this.ProjectController.loadEditor(this.req, this.res)
  887. })
  888. it('should not show for a user with an affiliated paid university', function (done) {
  889. this.InstitutionsFeatures.hasLicence = sinon
  890. .stub()
  891. .callsArgWith(1, null, true)
  892. this.res.render = (pageName, opts) => {
  893. expect(opts.showUpgradePrompt).to.equal(false)
  894. done()
  895. }
  896. this.ProjectController.loadEditor(this.req, this.res)
  897. })
  898. })
  899. })
  900. describe('userProjectsJson', function () {
  901. beforeEach(function (done) {
  902. const projects = [
  903. {
  904. archived: true,
  905. trashed: true,
  906. id: 'a',
  907. name: 'A',
  908. accessLevel: 'a',
  909. somethingElse: 1,
  910. },
  911. {
  912. archived: false,
  913. id: 'b',
  914. name: 'B',
  915. accessLevel: 'b',
  916. somethingElse: 1,
  917. },
  918. {
  919. archived: false,
  920. trashed: true,
  921. id: 'c',
  922. name: 'C',
  923. accessLevel: 'c',
  924. somethingElse: 1,
  925. },
  926. {
  927. archived: false,
  928. trashed: false,
  929. id: 'd',
  930. name: 'D',
  931. accessLevel: 'd',
  932. somethingElse: 1,
  933. },
  934. ]
  935. this.ProjectHelper.isArchivedOrTrashed
  936. .withArgs(projects[0], this.user._id)
  937. .returns(true)
  938. this.ProjectHelper.isArchivedOrTrashed
  939. .withArgs(projects[1], this.user._id)
  940. .returns(false)
  941. this.ProjectHelper.isArchivedOrTrashed
  942. .withArgs(projects[2], this.user._id)
  943. .returns(true)
  944. this.ProjectHelper.isArchivedOrTrashed
  945. .withArgs(projects[3], this.user._id)
  946. .returns(false)
  947. this.ProjectGetter.findAllUsersProjects = sinon
  948. .stub()
  949. .callsArgWith(2, null, [])
  950. this.ProjectController._buildProjectList = sinon.stub().returns(projects)
  951. this.SessionManager.getLoggedInUserId = sinon
  952. .stub()
  953. .returns(this.user._id)
  954. done()
  955. })
  956. it('should produce a list of projects', function (done) {
  957. this.res.json = data => {
  958. expect(data).to.deep.equal({
  959. projects: [
  960. { _id: 'b', name: 'B', accessLevel: 'b' },
  961. { _id: 'd', name: 'D', accessLevel: 'd' },
  962. ],
  963. })
  964. done()
  965. }
  966. this.ProjectController.userProjectsJson(this.req, this.res, this.next)
  967. })
  968. })
  969. describe('projectEntitiesJson', function () {
  970. beforeEach(function () {
  971. this.SessionManager.getLoggedInUserId = sinon.stub().returns('abc')
  972. this.req.params = { Project_id: 'abcd' }
  973. this.project = { _id: 'abcd' }
  974. this.docs = [
  975. { path: '/things/b.txt', doc: true },
  976. { path: '/main.tex', doc: true },
  977. ]
  978. this.files = [{ path: '/things/a.txt' }]
  979. this.ProjectGetter.getProject = sinon
  980. .stub()
  981. .callsArgWith(1, null, this.project)
  982. this.ProjectEntityHandler.getAllEntitiesFromProject = sinon
  983. .stub()
  984. .returns({ docs: this.docs, files: this.files })
  985. })
  986. it('should produce a list of entities', function (done) {
  987. this.res.json = data => {
  988. expect(data).to.deep.equal({
  989. project_id: 'abcd',
  990. entities: [
  991. { path: '/main.tex', type: 'doc' },
  992. { path: '/things/a.txt', type: 'file' },
  993. { path: '/things/b.txt', type: 'doc' },
  994. ],
  995. })
  996. expect(this.ProjectGetter.getProject.callCount).to.equal(1)
  997. expect(
  998. this.ProjectEntityHandler.getAllEntitiesFromProject.callCount
  999. ).to.equal(1)
  1000. done()
  1001. }
  1002. this.ProjectController.projectEntitiesJson(this.req, this.res, this.next)
  1003. })
  1004. it('should call next with an error if the project file tree is invalid', function (done) {
  1005. this.ProjectEntityHandler.getAllEntitiesFromProject = sinon
  1006. .stub()
  1007. .throws()
  1008. this.next = err => {
  1009. expect(err).to.be.an.instanceof(Error)
  1010. done()
  1011. }
  1012. this.ProjectController.projectEntitiesJson(this.req, this.res, this.next)
  1013. })
  1014. })
  1015. describe('_buildProjectViewModel', function () {
  1016. beforeEach(function () {
  1017. this.ProjectHelper.isArchived.returns(false)
  1018. this.ProjectHelper.isTrashed.returns(false)
  1019. this.project = {
  1020. _id: 'abcd',
  1021. name: 'netsenits',
  1022. lastUpdated: 1,
  1023. lastUpdatedBy: 2,
  1024. publicAccesLevel: 'private',
  1025. archived: false,
  1026. owner_ref: 'defg',
  1027. tokens: {
  1028. readAndWrite: '1abcd',
  1029. readAndWritePrefix: '1',
  1030. readOnly: 'neiotsranteoia',
  1031. },
  1032. }
  1033. })
  1034. describe('project not being archived or trashed', function () {
  1035. it('should produce a model of the project', function () {
  1036. const result = this.ProjectController._buildProjectViewModel(
  1037. this.project,
  1038. 'readAndWrite',
  1039. 'owner',
  1040. this.user._id
  1041. )
  1042. expect(result).to.exist
  1043. expect(result).to.be.an('object')
  1044. expect(result).to.deep.equal({
  1045. id: 'abcd',
  1046. name: 'netsenits',
  1047. lastUpdated: 1,
  1048. lastUpdatedBy: 2,
  1049. publicAccessLevel: 'private',
  1050. accessLevel: 'readAndWrite',
  1051. source: 'owner',
  1052. archived: false,
  1053. trashed: false,
  1054. owner_ref: 'defg',
  1055. isV1Project: false,
  1056. })
  1057. })
  1058. })
  1059. describe('project being simultaneously archived and trashed', function () {
  1060. beforeEach(function () {
  1061. this.ProjectHelper.isArchived.returns(true)
  1062. this.ProjectHelper.isTrashed.returns(true)
  1063. })
  1064. it('should produce a model of the project', function () {
  1065. const result = this.ProjectController._buildProjectViewModel(
  1066. this.project,
  1067. 'readAndWrite',
  1068. 'owner',
  1069. this.user._id
  1070. )
  1071. expect(result).to.exist
  1072. expect(result).to.be.an('object')
  1073. expect(result).to.deep.equal({
  1074. id: 'abcd',
  1075. name: 'netsenits',
  1076. lastUpdated: 1,
  1077. lastUpdatedBy: 2,
  1078. publicAccessLevel: 'private',
  1079. accessLevel: 'readAndWrite',
  1080. source: 'owner',
  1081. archived: true,
  1082. trashed: false,
  1083. owner_ref: 'defg',
  1084. isV1Project: false,
  1085. })
  1086. })
  1087. })
  1088. describe('when token-read-only access', function () {
  1089. it('should redact the owner and last-updated data', function () {
  1090. const result = this.ProjectController._buildProjectViewModel(
  1091. this.project,
  1092. 'readOnly',
  1093. 'token',
  1094. this.user._id
  1095. )
  1096. expect(result).to.exist
  1097. expect(result).to.be.an('object')
  1098. expect(result).to.deep.equal({
  1099. id: 'abcd',
  1100. name: 'netsenits',
  1101. lastUpdated: 1,
  1102. lastUpdatedBy: null,
  1103. publicAccessLevel: 'private',
  1104. accessLevel: 'readOnly',
  1105. source: 'token',
  1106. archived: false,
  1107. trashed: false,
  1108. owner_ref: null,
  1109. isV1Project: false,
  1110. })
  1111. })
  1112. })
  1113. })
  1114. describe('_isInPercentageRollout', function () {
  1115. before(function () {
  1116. this.ids = [
  1117. '5a05cd7621f9fe22be131740',
  1118. '5a05cd7821f9fe22be131741',
  1119. '5a05cd7921f9fe22be131742',
  1120. '5a05cd7a21f9fe22be131743',
  1121. '5a05cd7b21f9fe22be131744',
  1122. '5a05cd7c21f9fe22be131745',
  1123. '5a05cd7d21f9fe22be131746',
  1124. '5a05cd7e21f9fe22be131747',
  1125. '5a05cd7f21f9fe22be131748',
  1126. '5a05cd8021f9fe22be131749',
  1127. '5a05cd8021f9fe22be13174a',
  1128. '5a05cd8121f9fe22be13174b',
  1129. '5a05cd8221f9fe22be13174c',
  1130. '5a05cd8221f9fe22be13174d',
  1131. '5a05cd8321f9fe22be13174e',
  1132. '5a05cd8321f9fe22be13174f',
  1133. '5a05cd8421f9fe22be131750',
  1134. '5a05cd8421f9fe22be131751',
  1135. '5a05cd8421f9fe22be131752',
  1136. '5a05cd8521f9fe22be131753',
  1137. ]
  1138. })
  1139. it('should produce the expected results', function () {
  1140. expect(
  1141. this.ids.map(i =>
  1142. this.ProjectController._isInPercentageRollout('abcd', i, 50)
  1143. )
  1144. ).to.deep.equal([
  1145. false,
  1146. false,
  1147. false,
  1148. false,
  1149. false,
  1150. false,
  1151. true,
  1152. false,
  1153. true,
  1154. true,
  1155. true,
  1156. true,
  1157. true,
  1158. true,
  1159. false,
  1160. false,
  1161. false,
  1162. true,
  1163. false,
  1164. true,
  1165. ])
  1166. expect(
  1167. this.ids.map(i =>
  1168. this.ProjectController._isInPercentageRollout('efgh', i, 50)
  1169. )
  1170. ).to.deep.equal([
  1171. false,
  1172. false,
  1173. false,
  1174. false,
  1175. true,
  1176. false,
  1177. false,
  1178. true,
  1179. false,
  1180. false,
  1181. true,
  1182. true,
  1183. true,
  1184. false,
  1185. true,
  1186. false,
  1187. true,
  1188. true,
  1189. false,
  1190. false,
  1191. ])
  1192. })
  1193. })
  1194. })