ProjectControllerTests.js 51 KB

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