ProjectControllerTests.js 46 KB

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