ProjectControllerTests.js 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410
  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. hasUserBeenAssignedToVariant: sinon.stub().resolves(false),
  183. },
  184. getAssignment: sinon.stub().yields(null, { variant: 'default' }),
  185. }
  186. this.SplitTestSessionHandler = {
  187. promises: {
  188. sessionMaintenance: sinon.stub().resolves(),
  189. },
  190. }
  191. this.InstitutionsFeatures = {
  192. promises: {
  193. hasLicence: sinon.stub().resolves(false),
  194. },
  195. }
  196. this.InstitutionsGetter = {
  197. promises: {
  198. getCurrentAffiliations: sinon.stub().resolves([]),
  199. },
  200. }
  201. this.SurveyHandler = {
  202. getSurvey: sinon.stub().yields(null, {}),
  203. }
  204. this.ProjectAuditLogHandler = {
  205. promises: {
  206. addEntry: sinon.stub().resolves(),
  207. },
  208. }
  209. this.TutorialHandler = {
  210. getInactiveTutorials: sinon.stub().returns([]),
  211. }
  212. this.OnboardingDataCollectionManager = {
  213. getOnboardingDataValue: sinon.stub().resolves(null),
  214. }
  215. this.Modules = {
  216. promises: { hooks: { fire: sinon.stub().resolves() } },
  217. }
  218. this.ProjectController = SandboxedModule.require(MODULE_PATH, {
  219. requires: {
  220. 'mongodb-legacy': { ObjectId },
  221. '@overleaf/settings': this.settings,
  222. '@overleaf/metrics': this.Metrics,
  223. '../Collaborators/CollaboratorsHandler': this.CollaboratorsHandler,
  224. '../SplitTests/SplitTestHandler': this.SplitTestHandler,
  225. '../SplitTests/SplitTestSessionHandler': this.SplitTestSessionHandler,
  226. './ProjectDeleter': this.ProjectDeleter,
  227. './ProjectDuplicator': this.ProjectDuplicator,
  228. './ProjectCreationHandler': this.ProjectCreationHandler,
  229. '../Editor/EditorController': this.EditorController,
  230. '../User/UserController': this.UserController,
  231. './ProjectHelper': this.ProjectHelper,
  232. '../Subscription/SubscriptionLocator': this.SubscriptionLocator,
  233. '../Subscription/SubscriptionController': this.SubscriptionController,
  234. '../Subscription/LimitationsManager': this.LimitationsManager,
  235. '../Tags/TagsHandler': this.TagsHandler,
  236. '../../models/User': { User: this.UserModel },
  237. '../../models/Subscription': {},
  238. '../Authorization/AuthorizationManager': this.AuthorizationManager,
  239. '../InactiveData/InactiveProjectManager': this.InactiveProjectManager,
  240. './ProjectUpdateHandler': this.ProjectUpdateHandler,
  241. './ProjectGetter': this.ProjectGetter,
  242. './ProjectDetailsHandler': this.ProjectDetailsHandler,
  243. '../Authentication/SessionManager': this.SessionManager,
  244. '../TokenAccess/TokenAccessHandler': this.TokenAccessHandler,
  245. '../Collaborators/CollaboratorsGetter': this.CollaboratorsGetter,
  246. './ProjectEntityHandler': this.ProjectEntityHandler,
  247. '../../infrastructure/Features': this.Features,
  248. '../Subscription/FeaturesUpdater': this.FeaturesUpdater,
  249. '../User/UserGetter': this.UserGetter,
  250. '../BrandVariations/BrandVariationsHandler':
  251. this.BrandVariationsHandler,
  252. '../ThirdPartyDataStore/TpdsProjectFlusher': this.TpdsProjectFlusher,
  253. '../../models/Project': {},
  254. '../Analytics/AnalyticsManager': {
  255. recordEventForUserInBackground: () => {},
  256. },
  257. '../Subscription/SubscriptionViewModelBuilder':
  258. this.SubscriptionViewModelBuilder,
  259. '../Spelling/SpellingHandler': {
  260. promises: {
  261. getUserDictionary: sinon.stub().resolves([]),
  262. },
  263. },
  264. '../Institutions/InstitutionsFeatures': this.InstitutionsFeatures,
  265. '../Institutions/InstitutionsGetter': this.InstitutionsGetter,
  266. '../Survey/SurveyHandler': this.SurveyHandler,
  267. './ProjectAuditLogHandler': this.ProjectAuditLogHandler,
  268. '../Tutorial/TutorialHandler': this.TutorialHandler,
  269. '../OnboardingDataCollection/OnboardingDataCollectionManager':
  270. this.OnboardingDataCollectionManager,
  271. '../User/UserUpdater': {
  272. promises: {
  273. updateUser: sinon.stub().resolves(),
  274. },
  275. },
  276. '../../infrastructure/Modules': this.Modules,
  277. },
  278. })
  279. this.projectName = '£12321jkj9ujkljds'
  280. this.req = {
  281. query: {},
  282. params: {
  283. Project_id: this.project_id,
  284. },
  285. headers: {},
  286. connection: {
  287. remoteAddress: '192.170.18.1',
  288. },
  289. session: {
  290. user: this.user,
  291. },
  292. body: {
  293. projectName: this.projectName,
  294. },
  295. i18n: {
  296. translate() {},
  297. },
  298. ip: '192.170.18.1',
  299. capabilitySet: new Set(['chat']),
  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('capabilitySet', function () {
  1038. it('should be passed as an array when loading the editor', function (done) {
  1039. this.Features.hasFeature = sinon.stub().withArgs('chat').returns(false)
  1040. this.res.render = (pageName, opts) => {
  1041. expect(opts.capabilities).to.deep.equal(['chat'])
  1042. done()
  1043. }
  1044. this.ProjectController.loadEditor(this.req, this.res)
  1045. })
  1046. })
  1047. })
  1048. describe('userProjectsJson', function () {
  1049. beforeEach(function (done) {
  1050. const projects = [
  1051. {
  1052. archived: true,
  1053. trashed: true,
  1054. id: 'a',
  1055. name: 'A',
  1056. accessLevel: 'a',
  1057. somethingElse: 1,
  1058. },
  1059. {
  1060. archived: false,
  1061. id: 'b',
  1062. name: 'B',
  1063. accessLevel: 'b',
  1064. somethingElse: 1,
  1065. },
  1066. {
  1067. archived: false,
  1068. trashed: true,
  1069. id: 'c',
  1070. name: 'C',
  1071. accessLevel: 'c',
  1072. somethingElse: 1,
  1073. },
  1074. {
  1075. archived: false,
  1076. trashed: false,
  1077. id: 'd',
  1078. name: 'D',
  1079. accessLevel: 'd',
  1080. somethingElse: 1,
  1081. },
  1082. ]
  1083. this.ProjectHelper.isArchivedOrTrashed
  1084. .withArgs(projects[0], this.user._id)
  1085. .returns(true)
  1086. this.ProjectHelper.isArchivedOrTrashed
  1087. .withArgs(projects[1], this.user._id)
  1088. .returns(false)
  1089. this.ProjectHelper.isArchivedOrTrashed
  1090. .withArgs(projects[2], this.user._id)
  1091. .returns(true)
  1092. this.ProjectHelper.isArchivedOrTrashed
  1093. .withArgs(projects[3], this.user._id)
  1094. .returns(false)
  1095. this.ProjectGetter.promises.findAllUsersProjects = sinon
  1096. .stub()
  1097. .resolves([])
  1098. this.ProjectController._buildProjectList = sinon.stub().returns(projects)
  1099. this.SessionManager.getLoggedInUserId = sinon
  1100. .stub()
  1101. .returns(this.user._id)
  1102. done()
  1103. })
  1104. it('should produce a list of projects', function (done) {
  1105. this.res.json = data => {
  1106. expect(data).to.deep.equal({
  1107. projects: [
  1108. { _id: 'b', name: 'B', accessLevel: 'b' },
  1109. { _id: 'd', name: 'D', accessLevel: 'd' },
  1110. ],
  1111. })
  1112. done()
  1113. }
  1114. this.ProjectController.userProjectsJson(this.req, this.res, this.next)
  1115. })
  1116. })
  1117. describe('projectEntitiesJson', function () {
  1118. beforeEach(function () {
  1119. this.SessionManager.getLoggedInUserId = sinon.stub().returns('abc')
  1120. this.req.params = { Project_id: 'abcd' }
  1121. this.project = { _id: 'abcd' }
  1122. this.docs = [
  1123. { path: '/things/b.txt', doc: true },
  1124. { path: '/main.tex', doc: true },
  1125. ]
  1126. this.files = [{ path: '/things/a.txt' }]
  1127. this.ProjectGetter.promises.getProject = sinon
  1128. .stub()
  1129. .resolves(this.project)
  1130. this.ProjectEntityHandler.getAllEntitiesFromProject = sinon
  1131. .stub()
  1132. .returns({ docs: this.docs, files: this.files })
  1133. })
  1134. it('should produce a list of entities', function (done) {
  1135. this.res.json = data => {
  1136. expect(data).to.deep.equal({
  1137. project_id: 'abcd',
  1138. entities: [
  1139. { path: '/main.tex', type: 'doc' },
  1140. { path: '/things/a.txt', type: 'file' },
  1141. { path: '/things/b.txt', type: 'doc' },
  1142. ],
  1143. })
  1144. expect(this.ProjectGetter.promises.getProject.callCount).to.equal(1)
  1145. expect(
  1146. this.ProjectEntityHandler.getAllEntitiesFromProject.callCount
  1147. ).to.equal(1)
  1148. done()
  1149. }
  1150. this.ProjectController.projectEntitiesJson(this.req, this.res, this.next)
  1151. })
  1152. it('should call next with an error if the project file tree is invalid', function (done) {
  1153. this.ProjectEntityHandler.getAllEntitiesFromProject = sinon
  1154. .stub()
  1155. .throws()
  1156. this.next = err => {
  1157. expect(err).to.be.an.instanceof(Error)
  1158. done()
  1159. }
  1160. this.ProjectController.projectEntitiesJson(this.req, this.res, this.next)
  1161. })
  1162. })
  1163. describe('_buildProjectViewModel', function () {
  1164. beforeEach(function () {
  1165. this.ProjectHelper.isArchived.returns(false)
  1166. this.ProjectHelper.isTrashed.returns(false)
  1167. this.project = {
  1168. _id: 'abcd',
  1169. name: 'netsenits',
  1170. lastUpdated: 1,
  1171. lastUpdatedBy: 2,
  1172. publicAccesLevel: 'private',
  1173. archived: false,
  1174. owner_ref: 'defg',
  1175. tokens: {
  1176. readAndWrite: '1abcd',
  1177. readAndWritePrefix: '1',
  1178. readOnly: 'neiotsranteoia',
  1179. },
  1180. }
  1181. })
  1182. describe('project not being archived or trashed', function () {
  1183. it('should produce a model of the project', function () {
  1184. const result = this.ProjectController._buildProjectViewModel(
  1185. this.project,
  1186. 'readAndWrite',
  1187. 'owner',
  1188. this.user._id
  1189. )
  1190. expect(result).to.exist
  1191. expect(result).to.be.an('object')
  1192. expect(result).to.deep.equal({
  1193. id: 'abcd',
  1194. name: 'netsenits',
  1195. lastUpdated: 1,
  1196. lastUpdatedBy: 2,
  1197. publicAccessLevel: 'private',
  1198. accessLevel: 'readAndWrite',
  1199. source: 'owner',
  1200. archived: false,
  1201. trashed: false,
  1202. owner_ref: 'defg',
  1203. isV1Project: false,
  1204. })
  1205. })
  1206. })
  1207. describe('project being simultaneously archived and trashed', function () {
  1208. beforeEach(function () {
  1209. this.ProjectHelper.isArchived.returns(true)
  1210. this.ProjectHelper.isTrashed.returns(true)
  1211. })
  1212. it('should produce a model of the project', function () {
  1213. const result = this.ProjectController._buildProjectViewModel(
  1214. this.project,
  1215. 'readAndWrite',
  1216. 'owner',
  1217. this.user._id
  1218. )
  1219. expect(result).to.exist
  1220. expect(result).to.be.an('object')
  1221. expect(result).to.deep.equal({
  1222. id: 'abcd',
  1223. name: 'netsenits',
  1224. lastUpdated: 1,
  1225. lastUpdatedBy: 2,
  1226. publicAccessLevel: 'private',
  1227. accessLevel: 'readAndWrite',
  1228. source: 'owner',
  1229. archived: true,
  1230. trashed: false,
  1231. owner_ref: 'defg',
  1232. isV1Project: false,
  1233. })
  1234. })
  1235. })
  1236. describe('when token-read-only access', function () {
  1237. it('should redact the owner and last-updated data', function () {
  1238. const result = this.ProjectController._buildProjectViewModel(
  1239. this.project,
  1240. 'readOnly',
  1241. 'token',
  1242. this.user._id
  1243. )
  1244. expect(result).to.exist
  1245. expect(result).to.be.an('object')
  1246. expect(result).to.deep.equal({
  1247. id: 'abcd',
  1248. name: 'netsenits',
  1249. lastUpdated: 1,
  1250. lastUpdatedBy: null,
  1251. publicAccessLevel: 'private',
  1252. accessLevel: 'readOnly',
  1253. source: 'token',
  1254. archived: false,
  1255. trashed: false,
  1256. owner_ref: null,
  1257. isV1Project: false,
  1258. })
  1259. })
  1260. })
  1261. })
  1262. describe('_isInPercentageRollout', function () {
  1263. before(function () {
  1264. this.ids = [
  1265. '5a05cd7621f9fe22be131740',
  1266. '5a05cd7821f9fe22be131741',
  1267. '5a05cd7921f9fe22be131742',
  1268. '5a05cd7a21f9fe22be131743',
  1269. '5a05cd7b21f9fe22be131744',
  1270. '5a05cd7c21f9fe22be131745',
  1271. '5a05cd7d21f9fe22be131746',
  1272. '5a05cd7e21f9fe22be131747',
  1273. '5a05cd7f21f9fe22be131748',
  1274. '5a05cd8021f9fe22be131749',
  1275. '5a05cd8021f9fe22be13174a',
  1276. '5a05cd8121f9fe22be13174b',
  1277. '5a05cd8221f9fe22be13174c',
  1278. '5a05cd8221f9fe22be13174d',
  1279. '5a05cd8321f9fe22be13174e',
  1280. '5a05cd8321f9fe22be13174f',
  1281. '5a05cd8421f9fe22be131750',
  1282. '5a05cd8421f9fe22be131751',
  1283. '5a05cd8421f9fe22be131752',
  1284. '5a05cd8521f9fe22be131753',
  1285. ]
  1286. })
  1287. it('should produce the expected results', function () {
  1288. expect(
  1289. this.ids.map(i =>
  1290. this.ProjectController._isInPercentageRollout('abcd', i, 50)
  1291. )
  1292. ).to.deep.equal([
  1293. false,
  1294. false,
  1295. false,
  1296. false,
  1297. false,
  1298. false,
  1299. true,
  1300. false,
  1301. true,
  1302. true,
  1303. true,
  1304. true,
  1305. true,
  1306. true,
  1307. false,
  1308. false,
  1309. false,
  1310. true,
  1311. false,
  1312. true,
  1313. ])
  1314. expect(
  1315. this.ids.map(i =>
  1316. this.ProjectController._isInPercentageRollout('efgh', i, 50)
  1317. )
  1318. ).to.deep.equal([
  1319. false,
  1320. false,
  1321. false,
  1322. false,
  1323. true,
  1324. false,
  1325. false,
  1326. true,
  1327. false,
  1328. false,
  1329. true,
  1330. true,
  1331. true,
  1332. false,
  1333. true,
  1334. false,
  1335. true,
  1336. true,
  1337. false,
  1338. false,
  1339. ])
  1340. })
  1341. })
  1342. })