ProjectControllerTests.js 41 KB

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