ProjectControllerTests.js 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381
  1. const SandboxedModule = require('sandboxed-module')
  2. const path = require('path')
  3. const sinon = require('sinon')
  4. const { expect } = require('chai')
  5. const HttpErrors = require('@overleaf/o-error/http')
  6. const { ObjectId } = require('mongodb')
  7. const Errors = require('../../../../app/src/Features/Errors/Errors')
  8. const MODULE_PATH = path.join(
  9. __dirname,
  10. '../../../../app/src/Features/Project/ProjectController'
  11. )
  12. describe('ProjectController', function() {
  13. beforeEach(function() {
  14. this.project_id = ObjectId('abcdefabcdefabcdefabcdef')
  15. this.user = {
  16. _id: ObjectId('123456123456123456123456'),
  17. email: 'test@overleaf.com',
  18. first_name: 'bjkdsjfk',
  19. features: {},
  20. emails: [{ email: 'test@overleaf.com' }]
  21. }
  22. this.settings = {
  23. apis: {
  24. chat: {
  25. url: 'chat.com'
  26. }
  27. },
  28. siteUrl: 'mysite.com',
  29. algolia: {}
  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. legacyArchiveProject: sinon.stub().callsArg(1),
  41. deleteProject: sinon.stub().callsArg(2),
  42. restoreProject: sinon.stub().callsArg(1),
  43. findArchivedProjects: sinon.stub()
  44. }
  45. this.ProjectDuplicator = {
  46. duplicate: sinon.stub().callsArgWith(3, null, { _id: this.project_id })
  47. }
  48. this.ProjectCreationHandler = {
  49. createExampleProject: sinon
  50. .stub()
  51. .callsArgWith(2, null, { _id: this.project_id }),
  52. createBasicProject: sinon
  53. .stub()
  54. .callsArgWith(2, null, { _id: this.project_id })
  55. }
  56. this.SubscriptionLocator = { getUsersSubscription: sinon.stub() }
  57. this.LimitationsManager = { hasPaidSubscription: sinon.stub() }
  58. this.TagsHandler = { getAllTags: sinon.stub() }
  59. this.NotificationsHandler = { getUserNotifications: sinon.stub() }
  60. this.UserModel = { findById: sinon.stub() }
  61. this.AuthorizationManager = {
  62. getPrivilegeLevelForProject: sinon.stub(),
  63. isRestrictedUser: sinon.stub().returns(false)
  64. }
  65. this.EditorController = { renameProject: sinon.stub() }
  66. this.InactiveProjectManager = { reactivateProjectIfRequired: sinon.stub() }
  67. this.ProjectUpdateHandler = { markAsOpened: sinon.stub() }
  68. this.ReferencesSearchHandler = { indexProjectReferences: sinon.stub() }
  69. this.ProjectGetter = {
  70. findAllUsersProjects: sinon.stub(),
  71. getProject: sinon.stub()
  72. }
  73. this.ProjectHelper = {
  74. isArchived: sinon.stub(),
  75. isTrashed: sinon.stub(),
  76. isArchivedOrTrashed: sinon.stub()
  77. }
  78. this.AuthenticationController = {
  79. getLoggedInUser: sinon.stub().callsArgWith(1, null, this.user),
  80. getLoggedInUserId: sinon.stub().returns(this.user._id),
  81. getSessionUser: sinon.stub().returns(this.user),
  82. isUserLoggedIn: sinon.stub().returns(true)
  83. }
  84. this.AnalyticsManager = { getLastOccurrence: sinon.stub() }
  85. this.TokenAccessHandler = {
  86. getRequestToken: sinon.stub().returns(this.token),
  87. protectTokens: sinon.stub()
  88. }
  89. this.CollaboratorsGetter = {
  90. userIsTokenMember: sinon.stub().callsArgWith(2, null, false)
  91. }
  92. this.ProjectEntityHandler = {}
  93. this.NotificationBuilder = {
  94. ipMatcherAffiliation: sinon.stub().returns({ create: sinon.stub() })
  95. }
  96. this.UserGetter = {
  97. getUser: sinon
  98. .stub()
  99. .callsArgWith(2, null, { lastLoginIp: '192.170.18.2' })
  100. }
  101. this.Modules = {
  102. hooks: {
  103. fire: sinon.stub()
  104. }
  105. }
  106. this.Features = {
  107. hasFeature: sinon
  108. .stub()
  109. .withArgs('saml')
  110. .returns(false)
  111. }
  112. this.BrandVariationsHandler = {
  113. getBrandVariationById: sinon
  114. .stub()
  115. .callsArgWith(1, null, this.brandVariationDetails)
  116. }
  117. this.getUserAffiliations = sinon.stub().callsArgWith(1, null, [
  118. {
  119. email: 'test@overleaf.com',
  120. institution: {
  121. id: 1,
  122. name: 'Overleaf',
  123. ssoEnabled: true
  124. }
  125. }
  126. ])
  127. this.ProjectController = SandboxedModule.require(MODULE_PATH, {
  128. globals: {
  129. console: console
  130. },
  131. requires: {
  132. 'settings-sharelatex': this.settings,
  133. 'logger-sharelatex': {
  134. log() {},
  135. err() {}
  136. },
  137. 'metrics-sharelatex': {
  138. Timer: class {
  139. done() {}
  140. },
  141. inc() {}
  142. },
  143. '@overleaf/o-error/http': HttpErrors,
  144. './ProjectDeleter': this.ProjectDeleter,
  145. './ProjectDuplicator': this.ProjectDuplicator,
  146. './ProjectCreationHandler': this.ProjectCreationHandler,
  147. '../Editor/EditorController': this.EditorController,
  148. './ProjectHelper': this.ProjectHelper,
  149. '../Subscription/SubscriptionLocator': this.SubscriptionLocator,
  150. '../Subscription/LimitationsManager': this.LimitationsManager,
  151. '../Tags/TagsHandler': this.TagsHandler,
  152. '../Notifications/NotificationsHandler': this.NotificationsHandler,
  153. '../../models/User': {
  154. User: this.UserModel
  155. },
  156. '../Authorization/AuthorizationManager': this.AuthorizationManager,
  157. '../InactiveData/InactiveProjectManager': this.InactiveProjectManager,
  158. './ProjectUpdateHandler': this.ProjectUpdateHandler,
  159. '../ReferencesSearch/ReferencesSearchHandler': this
  160. .ReferencesSearchHandler,
  161. './ProjectGetter': this.ProjectGetter,
  162. './ProjectDetailsHandler': this.ProjectDetailsHandler,
  163. '../Authentication/AuthenticationController': this
  164. .AuthenticationController,
  165. '../Analytics/AnalyticsManager': this.AnalyticsManager,
  166. '../TokenAccess/TokenAccessHandler': this.TokenAccessHandler,
  167. '../Collaborators/CollaboratorsGetter': this.CollaboratorsGetter,
  168. '../../infrastructure/Modules': this.Modules,
  169. './ProjectEntityHandler': this.ProjectEntityHandler,
  170. '../Errors/Errors': Errors,
  171. '../../infrastructure/Features': this.Features,
  172. '../Notifications/NotificationsBuilder': this.NotificationBuilder,
  173. '../User/UserGetter': this.UserGetter,
  174. '../BrandVariations/BrandVariationsHandler': this
  175. .BrandVariationsHandler,
  176. '../Institutions/InstitutionsAPI': {
  177. getUserAffiliations: this.getUserAffiliations
  178. },
  179. '../V1/V1Handler': {},
  180. '../../models/Project': {}
  181. }
  182. })
  183. this.projectName = '£12321jkj9ujkljds'
  184. this.req = {
  185. params: {
  186. Project_id: this.project_id
  187. },
  188. headers: {},
  189. connection: {
  190. remoteAddress: '192.170.18.1'
  191. },
  192. session: {
  193. user: this.user
  194. },
  195. body: {
  196. projectName: this.projectName
  197. },
  198. i18n: {
  199. translate() {}
  200. },
  201. ip: '192.170.18.1'
  202. }
  203. this.res = {
  204. locals: {
  205. jsPath: 'js path here'
  206. },
  207. setTimeout: sinon.stub()
  208. }
  209. })
  210. describe('updateProjectSettings', function() {
  211. it('should update the name', function(done) {
  212. this.EditorController.renameProject = sinon.stub().callsArg(2)
  213. this.req.body = { name: (this.name = 'New name') }
  214. this.res.sendStatus = code => {
  215. this.EditorController.renameProject
  216. .calledWith(this.project_id, this.name)
  217. .should.equal(true)
  218. code.should.equal(204)
  219. done()
  220. }
  221. this.ProjectController.updateProjectSettings(this.req, this.res)
  222. })
  223. it('should update the compiler', function(done) {
  224. this.EditorController.setCompiler = sinon.stub().callsArg(2)
  225. this.req.body = { compiler: (this.compiler = 'pdflatex') }
  226. this.res.sendStatus = code => {
  227. this.EditorController.setCompiler
  228. .calledWith(this.project_id, this.compiler)
  229. .should.equal(true)
  230. code.should.equal(204)
  231. done()
  232. }
  233. this.ProjectController.updateProjectSettings(this.req, this.res)
  234. })
  235. it('should update the imageName', function(done) {
  236. this.EditorController.setImageName = sinon.stub().callsArg(2)
  237. this.req.body = { imageName: (this.imageName = 'texlive-1234.5') }
  238. this.res.sendStatus = code => {
  239. this.EditorController.setImageName
  240. .calledWith(this.project_id, this.imageName)
  241. .should.equal(true)
  242. code.should.equal(204)
  243. done()
  244. }
  245. this.ProjectController.updateProjectSettings(this.req, this.res)
  246. })
  247. it('should update the spell check language', function(done) {
  248. this.EditorController.setSpellCheckLanguage = sinon.stub().callsArg(2)
  249. this.req.body = { spellCheckLanguage: (this.languageCode = 'fr') }
  250. this.res.sendStatus = code => {
  251. this.EditorController.setSpellCheckLanguage
  252. .calledWith(this.project_id, this.languageCode)
  253. .should.equal(true)
  254. code.should.equal(204)
  255. done()
  256. }
  257. this.ProjectController.updateProjectSettings(this.req, this.res)
  258. })
  259. it('should update the root doc', function(done) {
  260. this.EditorController.setRootDoc = sinon.stub().callsArg(2)
  261. this.req.body = { rootDocId: (this.rootDocId = 'root-doc-id') }
  262. this.res.sendStatus = code => {
  263. this.EditorController.setRootDoc
  264. .calledWith(this.project_id, this.rootDocId)
  265. .should.equal(true)
  266. code.should.equal(204)
  267. done()
  268. }
  269. this.ProjectController.updateProjectSettings(this.req, this.res)
  270. })
  271. })
  272. describe('updateProjectAdminSettings', function() {
  273. it('should update the public access level', function(done) {
  274. this.EditorController.setPublicAccessLevel = sinon.stub().callsArg(2)
  275. this.req.body = {
  276. publicAccessLevel: (this.publicAccessLevel = 'readonly')
  277. }
  278. this.res.sendStatus = code => {
  279. this.EditorController.setPublicAccessLevel
  280. .calledWith(this.project_id, this.publicAccessLevel)
  281. .should.equal(true)
  282. code.should.equal(204)
  283. done()
  284. }
  285. this.ProjectController.updateProjectAdminSettings(this.req, this.res)
  286. })
  287. })
  288. describe('deleteProject', function() {
  289. it('should tell the project deleter to archive when forever=false', function(done) {
  290. this.res.sendStatus = code => {
  291. this.ProjectDeleter.legacyArchiveProject
  292. .calledWith(this.project_id)
  293. .should.equal(true)
  294. code.should.equal(200)
  295. done()
  296. }
  297. this.ProjectController.deleteProject(this.req, this.res)
  298. })
  299. it('should tell the project deleter to delete when forever=true', function(done) {
  300. this.req.query = { forever: 'true' }
  301. this.res.sendStatus = code => {
  302. this.ProjectDeleter.deleteProject
  303. .calledWith(this.project_id, {
  304. deleterUser: this.user,
  305. ipAddress: this.req.ip
  306. })
  307. .should.equal(true)
  308. code.should.equal(200)
  309. done()
  310. }
  311. this.ProjectController.deleteProject(this.req, this.res)
  312. })
  313. })
  314. describe('restoreProject', function() {
  315. it('should tell the project deleter', function(done) {
  316. this.res.sendStatus = code => {
  317. this.ProjectDeleter.restoreProject
  318. .calledWith(this.project_id)
  319. .should.equal(true)
  320. code.should.equal(200)
  321. done()
  322. }
  323. this.ProjectController.restoreProject(this.req, this.res)
  324. })
  325. })
  326. describe('cloneProject', function() {
  327. it('should call the project duplicator', function(done) {
  328. this.res.send = json => {
  329. this.ProjectDuplicator.duplicate
  330. .calledWith(this.user, this.project_id, this.projectName)
  331. .should.equal(true)
  332. json.project_id.should.equal(this.project_id)
  333. done()
  334. }
  335. this.ProjectController.cloneProject(this.req, this.res)
  336. })
  337. })
  338. describe('newProject', function() {
  339. it('should call the projectCreationHandler with createExampleProject', function(done) {
  340. this.req.body.template = 'example'
  341. this.res.send = json => {
  342. this.ProjectCreationHandler.createExampleProject
  343. .calledWith(this.user._id, this.projectName)
  344. .should.equal(true)
  345. this.ProjectCreationHandler.createBasicProject.called.should.equal(
  346. false
  347. )
  348. done()
  349. }
  350. this.ProjectController.newProject(this.req, this.res)
  351. })
  352. it('should call the projectCreationHandler with createBasicProject', function(done) {
  353. this.req.body.template = 'basic'
  354. this.res.send = json => {
  355. this.ProjectCreationHandler.createExampleProject.called.should.equal(
  356. false
  357. )
  358. this.ProjectCreationHandler.createBasicProject
  359. .calledWith(this.user._id, this.projectName)
  360. .should.equal(true)
  361. done()
  362. }
  363. this.ProjectController.newProject(this.req, this.res)
  364. })
  365. })
  366. describe('projectListPage', function() {
  367. beforeEach(function() {
  368. this.tags = [
  369. { name: 1, project_ids: ['1', '2', '3'] },
  370. { name: 2, project_ids: ['a', '1'] },
  371. { name: 3, project_ids: ['a', 'b', 'c', 'd'] }
  372. ]
  373. this.notifications = [
  374. { _id: '1', user_id: '2', templateKey: '3', messageOpts: '4', key: '5' }
  375. ]
  376. this.projects = [
  377. { _id: 1, lastUpdated: 1, owner_ref: 'user-1' },
  378. { _id: 2, lastUpdated: 2, owner_ref: 'user-2', lastUpdatedBy: 'user-1' }
  379. ]
  380. this.collabertions = [{ _id: 5, lastUpdated: 5, owner_ref: 'user-1' }]
  381. this.readOnly = [{ _id: 3, lastUpdated: 3, owner_ref: 'user-1' }]
  382. this.tokenReadAndWrite = [{ _id: 6, lastUpdated: 5, owner_ref: 'user-4' }]
  383. this.tokenReadOnly = [{ _id: 7, lastUpdated: 4, owner_ref: 'user-5' }]
  384. this.allProjects = {
  385. owned: this.projects,
  386. readAndWrite: this.collabertions,
  387. readOnly: this.readOnly,
  388. tokenReadAndWrite: this.tokenReadAndWrite,
  389. tokenReadOnly: this.tokenReadOnly
  390. }
  391. this.users = {
  392. 'user-1': {
  393. first_name: 'James'
  394. },
  395. 'user-2': {
  396. first_name: 'Henry'
  397. }
  398. }
  399. this.users[this.user._id] = this.user // Owner
  400. this.UserModel.findById = (id, fields, callback) => {
  401. callback(null, this.users[id])
  402. }
  403. this.UserGetter.getUser = (id, fields, callback) => {
  404. callback(null, this.users[id])
  405. }
  406. this.LimitationsManager.hasPaidSubscription.callsArgWith(1, null, false)
  407. this.TagsHandler.getAllTags.callsArgWith(1, null, this.tags)
  408. this.NotificationsHandler.getUserNotifications = sinon
  409. .stub()
  410. .callsArgWith(1, null, this.notifications, {})
  411. this.ProjectGetter.findAllUsersProjects.callsArgWith(
  412. 2,
  413. null,
  414. this.allProjects
  415. )
  416. this.Modules.hooks.fire
  417. .withArgs('findAllV1Projects', this.user._id)
  418. .yields(undefined)
  419. }) // Without integration module hook, cb returns undefined
  420. it('should render the project/list page', function(done) {
  421. this.res.render = (pageName, opts) => {
  422. pageName.should.equal('project/list')
  423. done()
  424. }
  425. this.ProjectController.projectListPage(this.req, this.res)
  426. })
  427. it('should send the tags', function(done) {
  428. this.res.render = (pageName, opts) => {
  429. opts.tags.length.should.equal(this.tags.length)
  430. done()
  431. }
  432. this.ProjectController.projectListPage(this.req, this.res)
  433. })
  434. it('should create trigger ip matcher notifications', function(done) {
  435. this.settings.overleaf = true
  436. this.res.render = (pageName, opts) => {
  437. this.NotificationBuilder.ipMatcherAffiliation.called.should.equal(true)
  438. done()
  439. }
  440. this.ProjectController.projectListPage(this.req, this.res)
  441. })
  442. it('should send the projects', function(done) {
  443. this.res.render = (pageName, opts) => {
  444. opts.projects.length.should.equal(
  445. this.projects.length +
  446. this.collabertions.length +
  447. this.readOnly.length +
  448. this.tokenReadAndWrite.length +
  449. this.tokenReadOnly.length
  450. )
  451. done()
  452. }
  453. this.ProjectController.projectListPage(this.req, this.res)
  454. })
  455. it('should send the user', function(done) {
  456. this.res.render = (pageName, opts) => {
  457. opts.user.should.deep.equal(this.user)
  458. done()
  459. }
  460. this.ProjectController.projectListPage(this.req, this.res)
  461. })
  462. it('should inject the users', function(done) {
  463. this.res.render = (pageName, opts) => {
  464. opts.projects[0].owner.should.equal(
  465. this.users[this.projects[0].owner_ref]
  466. )
  467. opts.projects[1].owner.should.equal(
  468. this.users[this.projects[1].owner_ref]
  469. )
  470. opts.projects[1].lastUpdatedBy.should.equal(
  471. this.users[this.projects[1].lastUpdatedBy]
  472. )
  473. done()
  474. }
  475. this.ProjectController.projectListPage(this.req, this.res)
  476. })
  477. it('should send hasSubscription == false when no subscription', function(done) {
  478. this.res.render = (pageName, opts) => {
  479. opts.hasSubscription.should.equal(false)
  480. done()
  481. }
  482. this.ProjectController.projectListPage(this.req, this.res)
  483. })
  484. it('should send hasSubscription == true when there is a subscription', function(done) {
  485. this.LimitationsManager.hasPaidSubscription = sinon
  486. .stub()
  487. .callsArgWith(1, null, true)
  488. this.res.render = (pageName, opts) => {
  489. opts.hasSubscription.should.equal(true)
  490. done()
  491. }
  492. this.ProjectController.projectListPage(this.req, this.res)
  493. })
  494. describe('when there is a v1 connection error', function() {
  495. beforeEach(function() {
  496. this.Features.hasFeature = sinon
  497. .stub()
  498. .withArgs('overleaf-integration')
  499. .returns(true)
  500. this.connectionWarning =
  501. 'Error accessing Overleaf V1. Some of your projects or features may be missing.'
  502. })
  503. it('should show a warning when there is an error getting v1 projects', function(done) {
  504. this.Modules.hooks.fire
  505. .withArgs('findAllV1Projects', this.user._id)
  506. .yields(new Errors.V1ConnectionError('error'))
  507. this.res.render = (pageName, opts) => {
  508. expect(opts.warnings).to.contain(this.connectionWarning)
  509. done()
  510. }
  511. this.ProjectController.projectListPage(this.req, this.res)
  512. })
  513. it('should show a warning when there is an error getting subscriptions from v1', function(done) {
  514. this.LimitationsManager.hasPaidSubscription.yields(
  515. new Errors.V1ConnectionError('error')
  516. )
  517. this.res.render = (pageName, opts) => {
  518. expect(opts.warnings).to.contain(this.connectionWarning)
  519. done()
  520. }
  521. this.ProjectController.projectListPage(this.req, this.res)
  522. })
  523. it('should show a warning when there is an error getting affiliations from v1', function(done) {
  524. this.getUserAffiliations.yields(new Errors.V1ConnectionError('error'))
  525. this.res.render = (pageName, opts) => {
  526. expect(opts.warnings).to.contain(this.connectionWarning)
  527. done()
  528. }
  529. this.ProjectController.projectListPage(this.req, this.res)
  530. })
  531. })
  532. describe('front widget', function(done) {
  533. beforeEach(function() {
  534. this.settings.overleaf = {
  535. front_chat_widget_room_id: 'chat-room-id'
  536. }
  537. })
  538. it('should show for paid users', function(done) {
  539. this.user.features.github = true
  540. this.user.features.dropbox = true
  541. this.res.render = (pageName, opts) => {
  542. opts.frontChatWidgetRoomId.should.equal(
  543. this.settings.overleaf.front_chat_widget_room_id
  544. )
  545. done()
  546. }
  547. this.ProjectController.projectListPage(this.req, this.res)
  548. })
  549. it('should show for sample users', function(done) {
  550. this.user._id = ObjectId('588f3ddae8ebc1bac07c9f00') // last two digits
  551. this.res.render = (pageName, opts) => {
  552. opts.frontChatWidgetRoomId.should.equal(
  553. this.settings.overleaf.front_chat_widget_room_id
  554. )
  555. done()
  556. }
  557. this.ProjectController.projectListPage(this.req, this.res)
  558. })
  559. it('should not show for non sample users', function(done) {
  560. this.user._id = ObjectId('588f3ddae8ebc1bac07c9fff') // last two digits
  561. this.res.render = (pageName, opts) => {
  562. expect(opts.frontChatWidgetRoomId).to.equal(undefined)
  563. done()
  564. }
  565. this.ProjectController.projectListPage(this.req, this.res)
  566. })
  567. })
  568. describe('with overleaf-integration-web-module hook', function() {
  569. beforeEach(function() {
  570. this.Features.hasFeature = sinon
  571. .stub()
  572. .withArgs('overleaf-integration')
  573. .returns(true)
  574. this.V1Response = {
  575. projects: [
  576. {
  577. id: '123mockV1Id',
  578. title: 'mock title',
  579. updated_at: 1509616411,
  580. removed: false,
  581. archived: false
  582. },
  583. {
  584. id: '456mockV1Id',
  585. title: 'mock title 2',
  586. updated_at: 1509616411,
  587. removed: true,
  588. archived: false
  589. }
  590. ],
  591. tags: [{ name: 'mock tag', project_ids: ['123mockV1Id'] }]
  592. }
  593. this.Modules.hooks.fire
  594. .withArgs('findAllV1Projects', this.user._id)
  595. .yields(null, [this.V1Response])
  596. }) // Need to wrap response in array, as multiple hooks could fire
  597. it('should include V1 projects', function(done) {
  598. this.res.render = (pageName, opts) => {
  599. opts.projects.length.should.equal(
  600. this.projects.length +
  601. this.collabertions.length +
  602. this.readOnly.length +
  603. this.tokenReadAndWrite.length +
  604. this.tokenReadOnly.length +
  605. this.V1Response.projects.length
  606. )
  607. opts.projects.forEach(p => {
  608. // Check properties correctly mapped from V1
  609. expect(p).to.have.property('id')
  610. expect(p).to.have.property('name')
  611. expect(p).to.have.property('lastUpdated')
  612. expect(p).to.have.property('accessLevel')
  613. expect(p).to.have.property('archived')
  614. })
  615. done()
  616. }
  617. this.ProjectController.projectListPage(this.req, this.res)
  618. })
  619. it('should include V1 tags', function(done) {
  620. this.res.render = (pageName, opts) => {
  621. opts.tags.length.should.equal(
  622. this.tags.length + this.V1Response.tags.length
  623. )
  624. opts.tags.forEach(t => {
  625. expect(t).to.have.property('name')
  626. expect(t).to.have.property('project_ids')
  627. })
  628. done()
  629. }
  630. this.ProjectController.projectListPage(this.req, this.res)
  631. })
  632. it('should have isShowingV1Projects flag', function(done) {
  633. this.res.render = (pageName, opts) => {
  634. opts.isShowingV1Projects.should.equal(true)
  635. done()
  636. }
  637. this.ProjectController.projectListPage(this.req, this.res)
  638. })
  639. })
  640. describe('When Institution SSO is released', function() {
  641. beforeEach(function(done) {
  642. this.institutionEmail = 'test@overleaf.com'
  643. this.institutionName = 'Overleaf'
  644. this.Features.hasFeature.withArgs('saml').returns(true)
  645. done()
  646. })
  647. it('should show institution SSO available notification', function() {
  648. this.res.render = (pageName, opts) => {
  649. expect(opts.notificationsInstitution).to.deep.include({
  650. email: 'test@overleaf.com',
  651. institutionId: 1,
  652. institutionName: 'Overleaf',
  653. templateKey: 'notification_institution_sso_available'
  654. })
  655. }
  656. this.ProjectController.projectListPage(this.req, this.res)
  657. })
  658. it('should show a linked notification', function() {
  659. this.req.session.saml = {
  660. institutionEmail: this.institutionEmail,
  661. linked: {
  662. hasEntitlement: false,
  663. universityName: this.institutionName
  664. }
  665. }
  666. this.res.render = (pageName, opts) => {
  667. expect(opts.notificationsInstitution).to.deep.include({
  668. email: this.institutionEmail,
  669. institutionName: this.institutionName,
  670. templateKey: 'notification_institution_sso_linked'
  671. })
  672. }
  673. this.ProjectController.projectListPage(this.req, this.res)
  674. })
  675. it('should show a linked another email notification', function() {
  676. // when they request to link an email but the institution returns
  677. // a different email
  678. this.res.render = (pageName, opts) => {
  679. expect(opts.notificationsInstitution).to.deep.include({
  680. institutionEmail: this.institutionEmail,
  681. requestedEmail: 'requested@overleaf.com',
  682. templateKey: 'notification_institution_sso_non_canonical'
  683. })
  684. }
  685. this.req.session.saml = {
  686. emailNonCanonical: this.institutionEmail,
  687. institutionEmail: this.institutionEmail,
  688. requestedEmail: 'requested@overleaf.com',
  689. linked: {
  690. hasEntitlement: false,
  691. universityName: this.institutionName
  692. }
  693. }
  694. this.ProjectController.projectListPage(this.req, this.res)
  695. })
  696. it('should show a notification when intent was to register via SSO but account existed', function() {
  697. this.res.render = (pageName, opts) => {
  698. expect(opts.notificationsInstitution).to.deep.include({
  699. email: this.institutionEmail,
  700. templateKey: 'notification_institution_sso_already_registered'
  701. })
  702. }
  703. this.req.session.saml = {
  704. institutionEmail: this.institutionEmail,
  705. linked: {
  706. hasEntitlement: false,
  707. universityName: 'Overleaf'
  708. },
  709. registerIntercept: {
  710. id: 1,
  711. name: 'Example University'
  712. }
  713. }
  714. this.ProjectController.projectListPage(this.req, this.res)
  715. })
  716. it('should not show a register notification if the flow was abandoned', function() {
  717. // could initially start to register with an SSO email and then
  718. // abandon flow and login with an existing non-institution SSO email
  719. this.res.render = (pageName, opts) => {
  720. expect(opts.notificationsInstitution).to.deep.not.include({
  721. email: 'test@overleaf.com',
  722. templateKey: 'notification_institution_sso_already_registered'
  723. })
  724. }
  725. this.req.session.saml = {
  726. registerIntercept: {
  727. id: 1,
  728. name: 'Example University'
  729. }
  730. }
  731. this.ProjectController.projectListPage(this.req, this.res)
  732. })
  733. it('should show institution account linked to another account', function() {
  734. this.res.render = (pageName, opts) => {
  735. expect(opts.notificationsInstitution).to.deep.include({
  736. templateKey: 'notification_institution_sso_linked_by_another'
  737. })
  738. // Also check other notifications are not shown
  739. expect(opts.notificationsInstitution).to.not.deep.include({
  740. email: this.institutionEmail,
  741. templateKey: 'notification_institution_sso_already_registered'
  742. })
  743. expect(opts.notificationsInstitution).to.not.deep.include({
  744. institutionEmail: this.institutionEmail,
  745. requestedEmail: 'requested@overleaf.com',
  746. templateKey: 'notification_institution_sso_non_canonical'
  747. })
  748. expect(opts.notificationsInstitution).to.not.deep.include({
  749. email: this.institutionEmail,
  750. institutionName: this.institutionName,
  751. templateKey: 'notification_institution_sso_linked'
  752. })
  753. }
  754. this.req.session.saml = {
  755. emailNonCanonical: this.institutionEmail,
  756. institutionEmail: this.institutionEmail,
  757. requestedEmail: 'requested@overleaf.com',
  758. linkedToAnother: true
  759. }
  760. this.ProjectController.projectListPage(this.req, this.res)
  761. })
  762. describe('when linking/logging in initiated on institution side', function() {
  763. it('should not show a linked another email notification', function() {
  764. // this is only used when initated on Overleaf,
  765. // because we keep track of the requested email they tried to link
  766. this.res.render = (pageName, opts) => {
  767. expect(opts.notificationsInstitution).to.not.deep.include({
  768. institutionEmail: this.institutionEmail,
  769. requestedEmail: undefined,
  770. templateKey: 'notification_institution_sso_non_canonical'
  771. })
  772. }
  773. this.req.session.saml = {
  774. emailNonCanonical: this.institutionEmail,
  775. institutionEmail: this.institutionEmail,
  776. linked: {
  777. hasEntitlement: false,
  778. universityName: this.institutionName
  779. }
  780. }
  781. this.ProjectController.projectListPage(this.req, this.res)
  782. })
  783. })
  784. })
  785. describe('When Institution SSO is not released', function() {
  786. beforeEach(function(done) {
  787. this.Features.hasFeature.withArgs('saml').returns(false)
  788. done()
  789. })
  790. it('should not show institution sso available notification', function() {
  791. this.res.render = (pageName, opts) => {
  792. expect(opts.notificationsInstitution).to.deep.not.include({
  793. email: 'test@overleaf.com',
  794. institutionId: 1,
  795. institutionName: 'Overleaf',
  796. templateKey: 'notification_institution_sso_available'
  797. })
  798. }
  799. this.ProjectController.projectListPage(this.req, this.res)
  800. })
  801. })
  802. })
  803. describe('projectListPage with duplicate projects', function() {
  804. beforeEach(function() {
  805. this.tags = [
  806. { name: 1, project_ids: ['1', '2', '3'] },
  807. { name: 2, project_ids: ['a', '1'] },
  808. { name: 3, project_ids: ['a', 'b', 'c', 'd'] }
  809. ]
  810. this.notifications = [
  811. { _id: '1', user_id: '2', templateKey: '3', messageOpts: '4', key: '5' }
  812. ]
  813. this.projects = [
  814. { _id: 1, lastUpdated: 1, owner_ref: 'user-1' },
  815. { _id: 2, lastUpdated: 2, owner_ref: 'user-2' }
  816. ]
  817. this.collabertions = [{ _id: 5, lastUpdated: 5, owner_ref: 'user-1' }]
  818. this.readOnly = [{ _id: 3, lastUpdated: 3, owner_ref: 'user-1' }]
  819. this.tokenReadAndWrite = [{ _id: 6, lastUpdated: 5, owner_ref: 'user-4' }]
  820. this.tokenReadOnly = [
  821. { _id: 6, lastUpdated: 5, owner_ref: 'user-4' }, // Also in tokenReadAndWrite
  822. { _id: 7, lastUpdated: 4, owner_ref: 'user-5' }
  823. ]
  824. this.allProjects = {
  825. owned: this.projects,
  826. readAndWrite: this.collabertions,
  827. readOnly: this.readOnly,
  828. tokenReadAndWrite: this.tokenReadAndWrite,
  829. tokenReadOnly: this.tokenReadOnly
  830. }
  831. this.users = {
  832. 'user-1': {
  833. first_name: 'James'
  834. },
  835. 'user-2': {
  836. first_name: 'Henry'
  837. }
  838. }
  839. this.users[this.user._id] = this.user // Owner
  840. this.UserModel.findById = (id, fields, callback) => {
  841. callback(null, this.users[id])
  842. }
  843. this.LimitationsManager.hasPaidSubscription.callsArgWith(1, null, false)
  844. this.TagsHandler.getAllTags.callsArgWith(1, null, this.tags)
  845. this.NotificationsHandler.getUserNotifications = sinon
  846. .stub()
  847. .callsArgWith(1, null, this.notifications, {})
  848. this.ProjectGetter.findAllUsersProjects.callsArgWith(
  849. 2,
  850. null,
  851. this.allProjects
  852. )
  853. this.Modules.hooks.fire
  854. .withArgs('findAllV1Projects', this.user._id)
  855. .yields(undefined)
  856. }) // Without integration module hook, cb returns undefined
  857. it('should render the project/list page', function(done) {
  858. this.res.render = (pageName, opts) => {
  859. pageName.should.equal('project/list')
  860. done()
  861. }
  862. this.ProjectController.projectListPage(this.req, this.res)
  863. })
  864. it('should omit one of the projects', function(done) {
  865. this.res.render = (pageName, opts) => {
  866. opts.projects.length.should.equal(
  867. this.projects.length +
  868. this.collabertions.length +
  869. this.readOnly.length +
  870. this.tokenReadAndWrite.length +
  871. this.tokenReadOnly.length -
  872. 1
  873. )
  874. done()
  875. }
  876. this.ProjectController.projectListPage(this.req, this.res)
  877. })
  878. })
  879. describe('renameProject', function() {
  880. beforeEach(function() {
  881. this.newProjectName = 'my supper great new project'
  882. this.req.body.newProjectName = this.newProjectName
  883. })
  884. it('should call the editor controller', function(done) {
  885. this.EditorController.renameProject.callsArgWith(2)
  886. this.res.sendStatus = code => {
  887. code.should.equal(200)
  888. this.EditorController.renameProject
  889. .calledWith(this.project_id, this.newProjectName)
  890. .should.equal(true)
  891. done()
  892. }
  893. this.ProjectController.renameProject(this.req, this.res)
  894. })
  895. it('should send an error to next() if there is a problem', function(done) {
  896. let error
  897. this.EditorController.renameProject.callsArgWith(
  898. 2,
  899. (error = new Error('problem'))
  900. )
  901. const next = e => {
  902. e.should.equal(error)
  903. done()
  904. }
  905. this.ProjectController.renameProject(this.req, this.res, next)
  906. })
  907. })
  908. describe('loadEditor', function() {
  909. beforeEach(function() {
  910. this.settings.editorIsOpen = true
  911. this.project = {
  912. name: 'my proj',
  913. _id: '213123kjlkj',
  914. owner_ref: '59fc84d5fbea77482d436e1b'
  915. }
  916. this.brandedProject = {
  917. name: 'my branded proj',
  918. _id: '3252332',
  919. owner_ref: '59fc84d5fbea77482d436e1b',
  920. brandVariationId: '12'
  921. }
  922. this.user = {
  923. _id: '588f3ddae8ebc1bac07c9fa4',
  924. ace: {
  925. fontSize: 'massive',
  926. theme: 'sexy'
  927. },
  928. email: 'bob@bob.com'
  929. }
  930. this.ProjectGetter.getProject.callsArgWith(2, null, this.project)
  931. this.UserModel.findById.callsArgWith(1, null, this.user)
  932. this.SubscriptionLocator.getUsersSubscription.callsArgWith(1, null, {})
  933. this.AuthorizationManager.getPrivilegeLevelForProject.callsArgWith(
  934. 3,
  935. null,
  936. 'owner'
  937. )
  938. this.ProjectDeleter.unmarkAsDeletedByExternalSource = sinon.stub()
  939. this.InactiveProjectManager.reactivateProjectIfRequired.callsArgWith(1)
  940. this.AnalyticsManager.getLastOccurrence.yields(null, { mock: 'event' })
  941. this.ProjectUpdateHandler.markAsOpened.callsArgWith(1)
  942. })
  943. it('should render the project/editor page', function(done) {
  944. this.res.render = (pageName, opts) => {
  945. pageName.should.equal('project/editor')
  946. done()
  947. }
  948. this.ProjectController.loadEditor(this.req, this.res)
  949. })
  950. it('should add user', function(done) {
  951. this.res.render = (pageName, opts) => {
  952. opts.user.email.should.equal(this.user.email)
  953. done()
  954. }
  955. this.ProjectController.loadEditor(this.req, this.res)
  956. })
  957. it('should add on userSettings', function(done) {
  958. this.res.render = (pageName, opts) => {
  959. opts.userSettings.fontSize.should.equal(this.user.ace.fontSize)
  960. opts.userSettings.editorTheme.should.equal(this.user.ace.theme)
  961. done()
  962. }
  963. this.ProjectController.loadEditor(this.req, this.res)
  964. })
  965. it('should add isRestrictedTokenMember', function(done) {
  966. this.res.render = (pageName, opts) => {
  967. opts.isRestrictedTokenMember.should.exist
  968. opts.isRestrictedTokenMember.should.equal(false)
  969. return done()
  970. }
  971. return this.ProjectController.loadEditor(this.req, this.res)
  972. })
  973. it('should set isRestrictedTokenMember when appropriate', function(done) {
  974. this.AuthorizationManager.isRestrictedUser.returns(true)
  975. this.res.render = (pageName, opts) => {
  976. opts.isRestrictedTokenMember.should.exist
  977. opts.isRestrictedTokenMember.should.equal(true)
  978. return done()
  979. }
  980. return this.ProjectController.loadEditor(this.req, this.res)
  981. })
  982. it('should render the closed page if the editor is closed', function(done) {
  983. this.settings.editorIsOpen = false
  984. this.res.render = (pageName, opts) => {
  985. pageName.should.equal('general/closed')
  986. done()
  987. }
  988. this.ProjectController.loadEditor(this.req, this.res)
  989. })
  990. it('should not render the page if the project can not be accessed', function(done) {
  991. this.AuthorizationManager.getPrivilegeLevelForProject = sinon
  992. .stub()
  993. .callsArgWith(3, null, null)
  994. this.res.sendStatus = (resCode, opts) => {
  995. resCode.should.equal(401)
  996. done()
  997. }
  998. this.ProjectController.loadEditor(this.req, this.res)
  999. })
  1000. it('should reactivateProjectIfRequired', function(done) {
  1001. this.res.render = (pageName, opts) => {
  1002. this.InactiveProjectManager.reactivateProjectIfRequired
  1003. .calledWith(this.project_id)
  1004. .should.equal(true)
  1005. done()
  1006. }
  1007. this.ProjectController.loadEditor(this.req, this.res)
  1008. })
  1009. it('should mark project as opened', function(done) {
  1010. this.res.render = (pageName, opts) => {
  1011. this.ProjectUpdateHandler.markAsOpened
  1012. .calledWith(this.project_id)
  1013. .should.equal(true)
  1014. done()
  1015. }
  1016. this.ProjectController.loadEditor(this.req, this.res)
  1017. })
  1018. it('should call the brand variations handler for branded projects', function(done) {
  1019. this.ProjectGetter.getProject.callsArgWith(2, null, this.brandedProject)
  1020. this.res.render = (pageName, opts) => {
  1021. this.BrandVariationsHandler.getBrandVariationById
  1022. .calledWith()
  1023. .should.equal(true)
  1024. done()
  1025. }
  1026. this.ProjectController.loadEditor(this.req, this.res)
  1027. })
  1028. it('should not call the brand variations handler for unbranded projects', function(done) {
  1029. this.res.render = (pageName, opts) => {
  1030. this.BrandVariationsHandler.getBrandVariationById.called.should.equal(
  1031. false
  1032. )
  1033. done()
  1034. }
  1035. this.ProjectController.loadEditor(this.req, this.res)
  1036. })
  1037. it('should expose the brand variation details as locals for branded projects', function(done) {
  1038. this.ProjectGetter.getProject.callsArgWith(2, null, this.brandedProject)
  1039. this.res.render = (pageName, opts) => {
  1040. opts.brandVariation.should.deep.equal(this.brandVariationDetails)
  1041. done()
  1042. }
  1043. this.ProjectController.loadEditor(this.req, this.res)
  1044. })
  1045. })
  1046. describe('userProjectsJson', function() {
  1047. beforeEach(function(done) {
  1048. const projects = [
  1049. {
  1050. archived: true,
  1051. trashed: true,
  1052. id: 'a',
  1053. name: 'A',
  1054. accessLevel: 'a',
  1055. somethingElse: 1
  1056. },
  1057. {
  1058. archived: false,
  1059. id: 'b',
  1060. name: 'B',
  1061. accessLevel: 'b',
  1062. somethingElse: 1
  1063. },
  1064. {
  1065. archived: false,
  1066. trashed: true,
  1067. id: 'c',
  1068. name: 'C',
  1069. accessLevel: 'c',
  1070. somethingElse: 1
  1071. },
  1072. {
  1073. archived: false,
  1074. trashed: false,
  1075. id: 'd',
  1076. name: 'D',
  1077. accessLevel: 'd',
  1078. somethingElse: 1
  1079. }
  1080. ]
  1081. this.ProjectHelper.isArchivedOrTrashed
  1082. .withArgs(projects[0], this.user._id)
  1083. .returns(true)
  1084. this.ProjectHelper.isArchivedOrTrashed
  1085. .withArgs(projects[1], this.user._id)
  1086. .returns(false)
  1087. this.ProjectHelper.isArchivedOrTrashed
  1088. .withArgs(projects[2], this.user._id)
  1089. .returns(true)
  1090. this.ProjectHelper.isArchivedOrTrashed
  1091. .withArgs(projects[3], this.user._id)
  1092. .returns(false)
  1093. this.ProjectGetter.findAllUsersProjects = sinon
  1094. .stub()
  1095. .callsArgWith(2, null, [])
  1096. this.ProjectController._buildProjectList = sinon.stub().returns(projects)
  1097. this.AuthenticationController.getLoggedInUserId = sinon
  1098. .stub()
  1099. .returns(this.user._id)
  1100. done()
  1101. })
  1102. it('should produce a list of projects', function(done) {
  1103. this.res.json = data => {
  1104. expect(data).to.deep.equal({
  1105. projects: [
  1106. { _id: 'b', name: 'B', accessLevel: 'b' },
  1107. { _id: 'd', name: 'D', accessLevel: 'd' }
  1108. ]
  1109. })
  1110. done()
  1111. }
  1112. this.ProjectController.userProjectsJson(this.req, this.res, this.next)
  1113. })
  1114. })
  1115. describe('projectEntitiesJson', function() {
  1116. beforeEach(function() {
  1117. this.AuthenticationController.getLoggedInUserId = sinon
  1118. .stub()
  1119. .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.getProject = sinon
  1128. .stub()
  1129. .callsArgWith(1, null, this.project)
  1130. this.ProjectEntityHandler.getAllEntitiesFromProject = sinon
  1131. .stub()
  1132. .callsArgWith(1, null, this.docs, 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.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. })
  1153. describe('_buildProjectViewModel', function() {
  1154. beforeEach(function() {
  1155. this.ProjectHelper.isArchived.returns(false)
  1156. this.project = {
  1157. _id: 'abcd',
  1158. name: 'netsenits',
  1159. lastUpdated: 1,
  1160. lastUpdatedBy: 2,
  1161. publicAccesLevel: 'private',
  1162. archived: false,
  1163. owner_ref: 'defg',
  1164. tokens: {
  1165. readAndWrite: '1abcd',
  1166. readAndWritePrefix: '1',
  1167. readOnly: 'neiotsranteoia'
  1168. }
  1169. }
  1170. })
  1171. it('should produce a model of the project', function() {
  1172. const result = this.ProjectController._buildProjectViewModel(
  1173. this.project,
  1174. 'readAndWrite',
  1175. 'owner',
  1176. this.user._id
  1177. )
  1178. expect(result).to.exist
  1179. expect(result).to.be.object
  1180. expect(result).to.deep.equal({
  1181. id: 'abcd',
  1182. name: 'netsenits',
  1183. lastUpdated: 1,
  1184. lastUpdatedBy: 2,
  1185. publicAccessLevel: 'private',
  1186. accessLevel: 'readAndWrite',
  1187. source: 'owner',
  1188. archived: false,
  1189. owner_ref: 'defg',
  1190. tokens: {
  1191. readAndWrite: '1abcd',
  1192. readAndWritePrefix: '1',
  1193. readOnly: 'neiotsranteoia'
  1194. },
  1195. isV1Project: false
  1196. })
  1197. })
  1198. describe('when token-read-only access', function() {
  1199. it('should redact the owner and last-updated data', function() {
  1200. const result = this.ProjectController._buildProjectViewModel(
  1201. this.project,
  1202. 'readOnly',
  1203. 'token',
  1204. this.user._id
  1205. )
  1206. expect(result).to.exist
  1207. expect(result).to.be.object
  1208. expect(result).to.deep.equal({
  1209. id: 'abcd',
  1210. name: 'netsenits',
  1211. lastUpdated: 1,
  1212. lastUpdatedBy: null,
  1213. publicAccessLevel: 'private',
  1214. accessLevel: 'readOnly',
  1215. source: 'token',
  1216. archived: false,
  1217. owner_ref: null,
  1218. tokens: {
  1219. readAndWrite: '1abcd',
  1220. readAndWritePrefix: '1',
  1221. readOnly: 'neiotsranteoia'
  1222. },
  1223. isV1Project: false
  1224. })
  1225. })
  1226. })
  1227. })
  1228. describe('_isInPercentageRollout', function() {
  1229. before(function() {
  1230. this.ids = [
  1231. '5a05cd7621f9fe22be131740',
  1232. '5a05cd7821f9fe22be131741',
  1233. '5a05cd7921f9fe22be131742',
  1234. '5a05cd7a21f9fe22be131743',
  1235. '5a05cd7b21f9fe22be131744',
  1236. '5a05cd7c21f9fe22be131745',
  1237. '5a05cd7d21f9fe22be131746',
  1238. '5a05cd7e21f9fe22be131747',
  1239. '5a05cd7f21f9fe22be131748',
  1240. '5a05cd8021f9fe22be131749',
  1241. '5a05cd8021f9fe22be13174a',
  1242. '5a05cd8121f9fe22be13174b',
  1243. '5a05cd8221f9fe22be13174c',
  1244. '5a05cd8221f9fe22be13174d',
  1245. '5a05cd8321f9fe22be13174e',
  1246. '5a05cd8321f9fe22be13174f',
  1247. '5a05cd8421f9fe22be131750',
  1248. '5a05cd8421f9fe22be131751',
  1249. '5a05cd8421f9fe22be131752',
  1250. '5a05cd8521f9fe22be131753'
  1251. ]
  1252. })
  1253. it('should produce the expected results', function() {
  1254. expect(
  1255. this.ids.map(i =>
  1256. this.ProjectController._isInPercentageRollout('abcd', i, 50)
  1257. )
  1258. ).to.deep.equal([
  1259. false,
  1260. false,
  1261. false,
  1262. false,
  1263. false,
  1264. false,
  1265. true,
  1266. false,
  1267. true,
  1268. true,
  1269. true,
  1270. true,
  1271. true,
  1272. true,
  1273. false,
  1274. false,
  1275. false,
  1276. true,
  1277. false,
  1278. true
  1279. ])
  1280. expect(
  1281. this.ids.map(i =>
  1282. this.ProjectController._isInPercentageRollout('efgh', i, 50)
  1283. )
  1284. ).to.deep.equal([
  1285. false,
  1286. false,
  1287. false,
  1288. false,
  1289. true,
  1290. false,
  1291. false,
  1292. true,
  1293. false,
  1294. false,
  1295. true,
  1296. true,
  1297. true,
  1298. false,
  1299. true,
  1300. false,
  1301. true,
  1302. true,
  1303. false,
  1304. false
  1305. ])
  1306. })
  1307. })
  1308. })