ProjectControllerTests.js 46 KB

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