ProjectControllerTests.js 40 KB

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