ProjectControllerTests.js 53 KB

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