ProjectControllerTests.js 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841
  1. const SandboxedModule = require('sandboxed-module')
  2. const path = require('path')
  3. const sinon = require('sinon')
  4. const { expect } = require('chai')
  5. const { ObjectId } = require('mongodb')
  6. const Errors = require('../../../../app/src/Features/Errors/Errors')
  7. const MODULE_PATH = path.join(
  8. __dirname,
  9. '../../../../app/src/Features/Project/ProjectController'
  10. )
  11. describe('ProjectController', function () {
  12. beforeEach(function () {
  13. this.project_id = ObjectId('abcdefabcdefabcdefabcdef')
  14. this.user = {
  15. _id: ObjectId('123456123456123456123456'),
  16. email: 'test@overleaf.com',
  17. first_name: 'bjkdsjfk',
  18. features: {},
  19. emails: [{ email: 'test@overleaf.com' }],
  20. }
  21. this.settings = {
  22. apis: {
  23. chat: {
  24. url: 'chat.com',
  25. },
  26. },
  27. siteUrl: 'mysite.com',
  28. algolia: {},
  29. }
  30. this.brandVariationDetails = {
  31. id: '12',
  32. active: true,
  33. brand_name: 'The journal',
  34. home_url: 'http://www.thejournal.com/',
  35. publish_menu_link_html: 'Submit your paper to the <em>The Journal</em>',
  36. }
  37. this.token = 'some-token'
  38. this.ProjectDeleter = {
  39. deleteProject: sinon.stub().callsArg(2),
  40. restoreProject: sinon.stub().callsArg(1),
  41. findArchivedProjects: sinon.stub(),
  42. }
  43. this.ProjectDuplicator = {
  44. duplicate: sinon.stub().callsArgWith(3, null, { _id: this.project_id }),
  45. }
  46. this.ProjectCreationHandler = {
  47. createExampleProject: sinon
  48. .stub()
  49. .callsArgWith(2, null, { _id: this.project_id }),
  50. createBasicProject: sinon
  51. .stub()
  52. .callsArgWith(2, null, { _id: this.project_id }),
  53. }
  54. this.SubscriptionLocator = { getUsersSubscription: sinon.stub() }
  55. this.LimitationsManager = { hasPaidSubscription: sinon.stub() }
  56. this.TagsHandler = { getAllTags: sinon.stub() }
  57. this.NotificationsHandler = { getUserNotifications: sinon.stub() }
  58. this.UserModel = { findById: sinon.stub() }
  59. this.AuthorizationManager = {
  60. getPrivilegeLevelForProject: sinon.stub(),
  61. isRestrictedUser: sinon.stub().returns(false),
  62. }
  63. this.EditorController = { renameProject: sinon.stub() }
  64. this.InactiveProjectManager = { reactivateProjectIfRequired: sinon.stub() }
  65. this.ProjectUpdateHandler = { markAsOpened: sinon.stub() }
  66. this.ProjectGetter = {
  67. findAllUsersProjects: sinon.stub(),
  68. getProject: sinon.stub(),
  69. }
  70. this.ProjectHelper = {
  71. isArchived: sinon.stub(),
  72. isTrashed: sinon.stub(),
  73. isArchivedOrTrashed: sinon.stub(),
  74. getAllowedImagesForUser: sinon.stub().returns([]),
  75. }
  76. this.SessionManager = {
  77. getLoggedInUser: sinon.stub().callsArgWith(1, null, this.user),
  78. getLoggedInUserId: sinon.stub().returns(this.user._id),
  79. getSessionUser: sinon.stub().returns(this.user),
  80. isUserLoggedIn: sinon.stub().returns(true),
  81. }
  82. this.UserController = {
  83. logout: sinon.stub(),
  84. }
  85. this.TokenAccessHandler = {
  86. getRequestToken: sinon.stub().returns(this.token),
  87. protectTokens: sinon.stub(),
  88. }
  89. this.CollaboratorsGetter = {
  90. userIsTokenMember: sinon.stub().callsArgWith(2, null, false),
  91. }
  92. this.ProjectEntityHandler = {}
  93. this.NotificationBuilder = {
  94. ipMatcherAffiliation: sinon.stub().returns({ create: sinon.stub() }),
  95. }
  96. this.UserGetter = {
  97. getUserFullEmails: sinon.stub().yields(null, []),
  98. getUser: sinon
  99. .stub()
  100. .callsArgWith(2, null, { lastLoginIp: '192.170.18.2' }),
  101. }
  102. this.Features = {
  103. hasFeature: sinon.stub(),
  104. }
  105. this.BrandVariationsHandler = {
  106. getBrandVariationById: sinon
  107. .stub()
  108. .callsArgWith(1, null, this.brandVariationDetails),
  109. }
  110. this.TpdsProjectFlusher = {
  111. flushProjectToTpdsIfNeeded: sinon.stub().yields(),
  112. }
  113. this.Metrics = {
  114. Timer: class {
  115. done() {}
  116. },
  117. inc: sinon.stub(),
  118. }
  119. this.NewLogsUIHelper = {
  120. getNewLogsUIVariantForUser: sinon
  121. .stub()
  122. .returns({ newLogsUI: false, subvariant: null }),
  123. }
  124. this.SplitTestHandler = {
  125. promises: {
  126. getTestSegmentation: sinon.stub().resolves({ enabled: false }),
  127. },
  128. getTestSegmentation: sinon.stub().yields(null, { enabled: false }),
  129. }
  130. this.SplitTestV2Handler = {
  131. promises: {
  132. getAssignment: sinon.stub().resolves({ active: false }),
  133. assignInLocalsContext: sinon.stub().resolves(),
  134. },
  135. getAssignment: sinon.stub().yields(null, { active: false }),
  136. assignInLocalsContext: sinon.stub().yields(null),
  137. }
  138. this.ProjectController = SandboxedModule.require(MODULE_PATH, {
  139. requires: {
  140. mongodb: { ObjectId },
  141. '@overleaf/settings': this.settings,
  142. '@overleaf/metrics': this.Metrics,
  143. '../SplitTests/SplitTestHandler': this.SplitTestHandler,
  144. '../SplitTests/SplitTestV2Handler': this.SplitTestV2Handler,
  145. './ProjectDeleter': this.ProjectDeleter,
  146. './ProjectDuplicator': this.ProjectDuplicator,
  147. './ProjectCreationHandler': this.ProjectCreationHandler,
  148. '../Editor/EditorController': this.EditorController,
  149. '../User/UserController': this.UserController,
  150. './ProjectHelper': this.ProjectHelper,
  151. '../Subscription/SubscriptionLocator': this.SubscriptionLocator,
  152. '../Subscription/LimitationsManager': this.LimitationsManager,
  153. '../Tags/TagsHandler': this.TagsHandler,
  154. '../Notifications/NotificationsHandler': this.NotificationsHandler,
  155. '../../models/User': { User: this.UserModel },
  156. '../Authorization/AuthorizationManager': this.AuthorizationManager,
  157. '../InactiveData/InactiveProjectManager': this.InactiveProjectManager,
  158. './ProjectUpdateHandler': this.ProjectUpdateHandler,
  159. './ProjectGetter': this.ProjectGetter,
  160. './ProjectDetailsHandler': this.ProjectDetailsHandler,
  161. '../Authentication/SessionManager': this.SessionManager,
  162. '../TokenAccess/TokenAccessHandler': this.TokenAccessHandler,
  163. '../Collaborators/CollaboratorsGetter': this.CollaboratorsGetter,
  164. './ProjectEntityHandler': this.ProjectEntityHandler,
  165. '../../infrastructure/Features': this.Features,
  166. '../Notifications/NotificationsBuilder': this.NotificationBuilder,
  167. '../User/UserGetter': this.UserGetter,
  168. '../BrandVariations/BrandVariationsHandler': this
  169. .BrandVariationsHandler,
  170. '../ThirdPartyDataStore/TpdsProjectFlusher': this.TpdsProjectFlusher,
  171. '../../models/Project': {},
  172. '../Analytics/AnalyticsManager': { recordEvent: () => {} },
  173. '../../infrastructure/Modules': {
  174. hooks: { fire: sinon.stub().yields(null, []) },
  175. },
  176. '../Helpers/NewLogsUI': this.NewLogsUIHelper,
  177. },
  178. })
  179. this.projectName = '£12321jkj9ujkljds'
  180. this.req = {
  181. query: {},
  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. {
  361. _id: '1',
  362. user_id: '2',
  363. templateKey: '3',
  364. messageOpts: '4',
  365. key: '5',
  366. },
  367. ]
  368. this.projects = [
  369. { _id: 1, lastUpdated: 1, owner_ref: 'user-1' },
  370. {
  371. _id: 2,
  372. lastUpdated: 2,
  373. owner_ref: 'user-2',
  374. lastUpdatedBy: 'user-1',
  375. },
  376. ]
  377. this.collabertions = [{ _id: 5, lastUpdated: 5, owner_ref: 'user-1' }]
  378. this.readOnly = [{ _id: 3, lastUpdated: 3, owner_ref: 'user-1' }]
  379. this.tokenReadAndWrite = [{ _id: 6, lastUpdated: 5, owner_ref: 'user-4' }]
  380. this.tokenReadOnly = [{ _id: 7, lastUpdated: 4, owner_ref: 'user-5' }]
  381. this.allProjects = {
  382. owned: this.projects,
  383. readAndWrite: this.collabertions,
  384. readOnly: this.readOnly,
  385. tokenReadAndWrite: this.tokenReadAndWrite,
  386. tokenReadOnly: this.tokenReadOnly,
  387. }
  388. this.users = {
  389. 'user-1': {
  390. first_name: 'James',
  391. },
  392. 'user-2': {
  393. first_name: 'Henry',
  394. },
  395. }
  396. this.users[this.user._id] = this.user // Owner
  397. this.UserModel.findById = (id, fields, callback) => {
  398. callback(null, this.users[id])
  399. }
  400. this.UserGetter.getUser = (id, fields, callback) => {
  401. callback(null, this.users[id])
  402. }
  403. this.LimitationsManager.hasPaidSubscription.callsArgWith(1, null, false)
  404. this.TagsHandler.getAllTags.callsArgWith(1, null, this.tags)
  405. this.NotificationsHandler.getUserNotifications = sinon
  406. .stub()
  407. .callsArgWith(1, null, this.notifications, {})
  408. this.ProjectGetter.findAllUsersProjects.callsArgWith(
  409. 2,
  410. null,
  411. this.allProjects
  412. )
  413. })
  414. it('should render the project/list page', function (done) {
  415. this.res.render = (pageName, opts) => {
  416. pageName.should.equal('project/list')
  417. done()
  418. }
  419. this.ProjectController.projectListPage(this.req, this.res)
  420. })
  421. it('should send the tags', function (done) {
  422. this.res.render = (pageName, opts) => {
  423. opts.tags.length.should.equal(this.tags.length)
  424. done()
  425. }
  426. this.ProjectController.projectListPage(this.req, this.res)
  427. })
  428. it('should create trigger ip matcher notifications', function (done) {
  429. this.settings.overleaf = true
  430. this.res.render = (pageName, opts) => {
  431. this.NotificationBuilder.ipMatcherAffiliation.called.should.equal(true)
  432. done()
  433. }
  434. this.ProjectController.projectListPage(this.req, this.res)
  435. })
  436. it('should send the projects', function (done) {
  437. this.res.render = (pageName, opts) => {
  438. opts.projects.length.should.equal(
  439. this.projects.length +
  440. this.collabertions.length +
  441. this.readOnly.length +
  442. this.tokenReadAndWrite.length +
  443. this.tokenReadOnly.length
  444. )
  445. done()
  446. }
  447. this.ProjectController.projectListPage(this.req, this.res)
  448. })
  449. it('should send the user', function (done) {
  450. this.res.render = (pageName, opts) => {
  451. opts.user.should.deep.equal(this.user)
  452. done()
  453. }
  454. this.ProjectController.projectListPage(this.req, this.res)
  455. })
  456. it('should inject the users', function (done) {
  457. this.res.render = (pageName, opts) => {
  458. opts.projects[0].owner.should.equal(
  459. this.users[this.projects[0].owner_ref]
  460. )
  461. opts.projects[1].owner.should.equal(
  462. this.users[this.projects[1].owner_ref]
  463. )
  464. opts.projects[1].lastUpdatedBy.should.equal(
  465. this.users[this.projects[1].lastUpdatedBy]
  466. )
  467. done()
  468. }
  469. this.ProjectController.projectListPage(this.req, this.res)
  470. })
  471. it('should send hasSubscription == false when no subscription', function (done) {
  472. this.res.render = (pageName, opts) => {
  473. opts.hasSubscription.should.equal(false)
  474. done()
  475. }
  476. this.ProjectController.projectListPage(this.req, this.res)
  477. })
  478. it('should send hasSubscription == true when there is a subscription', function (done) {
  479. this.LimitationsManager.hasPaidSubscription = sinon
  480. .stub()
  481. .callsArgWith(1, null, true)
  482. this.res.render = (pageName, opts) => {
  483. opts.hasSubscription.should.equal(true)
  484. done()
  485. }
  486. this.ProjectController.projectListPage(this.req, this.res)
  487. })
  488. describe('front widget', function (done) {
  489. beforeEach(function () {
  490. this.settings.overleaf = {
  491. front_chat_widget_room_id: 'chat-room-id',
  492. }
  493. })
  494. it('should show for paid users', function (done) {
  495. this.user.features.github = true
  496. this.user.features.dropbox = true
  497. this.res.render = (pageName, opts) => {
  498. opts.frontChatWidgetRoomId.should.equal(
  499. this.settings.overleaf.front_chat_widget_room_id
  500. )
  501. done()
  502. }
  503. this.ProjectController.projectListPage(this.req, this.res)
  504. })
  505. it('should show for sample users', function (done) {
  506. this.user._id = ObjectId('588f3ddae8ebc1bac07c9f00') // last two digits
  507. this.res.render = (pageName, opts) => {
  508. opts.frontChatWidgetRoomId.should.equal(
  509. this.settings.overleaf.front_chat_widget_room_id
  510. )
  511. done()
  512. }
  513. this.ProjectController.projectListPage(this.req, this.res)
  514. })
  515. it('should not show for non sample users', function (done) {
  516. this.user._id = ObjectId('588f3ddae8ebc1bac07c9fff') // last two digits
  517. this.res.render = (pageName, opts) => {
  518. expect(opts.frontChatWidgetRoomId).to.equal(undefined)
  519. done()
  520. }
  521. this.ProjectController.projectListPage(this.req, this.res)
  522. })
  523. })
  524. describe('With Institution SSO feature', function () {
  525. beforeEach(function (done) {
  526. this.institutionEmail = 'test@overleaf.com'
  527. this.institutionName = 'Overleaf'
  528. this.Features.hasFeature.withArgs('saml').returns(true)
  529. this.Features.hasFeature.withArgs('affiliations').returns(true)
  530. this.Features.hasFeature.withArgs('overleaf-integration').returns(true)
  531. done()
  532. })
  533. it('should show institution SSO available notification for confirmed domains', function () {
  534. this.UserGetter.getUserFullEmails.yields(null, [
  535. {
  536. email: 'test@overleaf.com',
  537. affiliation: {
  538. institution: {
  539. id: 1,
  540. confirmed: true,
  541. name: 'Overleaf',
  542. ssoBeta: false,
  543. ssoEnabled: true,
  544. },
  545. },
  546. },
  547. ])
  548. this.res.render = (pageName, opts) => {
  549. expect(opts.notificationsInstitution).to.deep.include({
  550. email: this.institutionEmail,
  551. institutionId: 1,
  552. institutionName: this.institutionName,
  553. templateKey: 'notification_institution_sso_available',
  554. })
  555. }
  556. this.ProjectController.projectListPage(this.req, this.res)
  557. })
  558. it('should show a linked notification', function () {
  559. this.req.session.saml = {
  560. institutionEmail: this.institutionEmail,
  561. linked: {
  562. hasEntitlement: false,
  563. universityName: this.institutionName,
  564. },
  565. }
  566. this.res.render = (pageName, opts) => {
  567. expect(opts.notificationsInstitution).to.deep.include({
  568. email: this.institutionEmail,
  569. institutionName: this.institutionName,
  570. templateKey: 'notification_institution_sso_linked',
  571. })
  572. }
  573. this.ProjectController.projectListPage(this.req, this.res)
  574. })
  575. it('should show a linked another email notification', function () {
  576. // when they request to link an email but the institution returns
  577. // a different email
  578. this.res.render = (pageName, opts) => {
  579. expect(opts.notificationsInstitution).to.deep.include({
  580. institutionEmail: this.institutionEmail,
  581. requestedEmail: 'requested@overleaf.com',
  582. templateKey: 'notification_institution_sso_non_canonical',
  583. })
  584. }
  585. this.req.session.saml = {
  586. emailNonCanonical: this.institutionEmail,
  587. institutionEmail: this.institutionEmail,
  588. requestedEmail: 'requested@overleaf.com',
  589. linked: {
  590. hasEntitlement: false,
  591. universityName: this.institutionName,
  592. },
  593. }
  594. this.ProjectController.projectListPage(this.req, this.res)
  595. })
  596. it('should show a notification when intent was to register via SSO but account existed', function () {
  597. this.res.render = (pageName, opts) => {
  598. expect(opts.notificationsInstitution).to.deep.include({
  599. email: this.institutionEmail,
  600. templateKey: 'notification_institution_sso_already_registered',
  601. })
  602. }
  603. this.req.session.saml = {
  604. institutionEmail: this.institutionEmail,
  605. linked: {
  606. hasEntitlement: false,
  607. universityName: 'Overleaf',
  608. },
  609. registerIntercept: {
  610. id: 1,
  611. name: 'Example University',
  612. },
  613. }
  614. this.ProjectController.projectListPage(this.req, this.res)
  615. })
  616. it('should not show a register notification if the flow was abandoned', function () {
  617. // could initially start to register with an SSO email and then
  618. // abandon flow and login with an existing non-institution SSO email
  619. this.res.render = (pageName, opts) => {
  620. expect(opts.notificationsInstitution).to.deep.not.include({
  621. email: 'test@overleaf.com',
  622. templateKey: 'notification_institution_sso_already_registered',
  623. })
  624. }
  625. this.req.session.saml = {
  626. registerIntercept: {
  627. id: 1,
  628. name: 'Example University',
  629. },
  630. }
  631. this.ProjectController.projectListPage(this.req, this.res)
  632. })
  633. it('should show error notification', function () {
  634. this.res.render = (pageName, opts) => {
  635. expect(opts.notificationsInstitution.length).to.equal(1)
  636. expect(opts.notificationsInstitution[0].templateKey).to.equal(
  637. 'notification_institution_sso_error'
  638. )
  639. expect(opts.notificationsInstitution[0].error).to.be.instanceof(
  640. Errors.SAMLAlreadyLinkedError
  641. )
  642. }
  643. this.req.session.saml = {
  644. institutionEmail: this.institutionEmail,
  645. error: new Errors.SAMLAlreadyLinkedError(),
  646. }
  647. this.ProjectController.projectListPage(this.req, this.res)
  648. })
  649. describe('for an unconfirmed domain for an SSO institution', function () {
  650. beforeEach(function (done) {
  651. this.UserGetter.getUserFullEmails.yields(null, [
  652. {
  653. email: 'test@overleaf-uncofirmed.com',
  654. affiliation: {
  655. institution: {
  656. id: 1,
  657. confirmed: false,
  658. name: 'Overleaf',
  659. ssoBeta: false,
  660. ssoEnabled: true,
  661. },
  662. },
  663. },
  664. ])
  665. done()
  666. })
  667. it('should not show institution SSO available notification', function () {
  668. this.res.render = (pageName, opts) => {
  669. expect(opts.notificationsInstitution.length).to.equal(0)
  670. }
  671. this.ProjectController.projectListPage(this.req, this.res)
  672. })
  673. })
  674. describe('when linking/logging in initiated on institution side', function () {
  675. it('should not show a linked another email notification', function () {
  676. // this is only used when initated on Overleaf,
  677. // because we keep track of the requested email they tried to link
  678. this.res.render = (pageName, opts) => {
  679. expect(opts.notificationsInstitution).to.not.deep.include({
  680. institutionEmail: this.institutionEmail,
  681. requestedEmail: undefined,
  682. templateKey: 'notification_institution_sso_non_canonical',
  683. })
  684. }
  685. this.req.session.saml = {
  686. emailNonCanonical: this.institutionEmail,
  687. institutionEmail: this.institutionEmail,
  688. linked: {
  689. hasEntitlement: false,
  690. universityName: this.institutionName,
  691. },
  692. }
  693. this.ProjectController.projectListPage(this.req, this.res)
  694. })
  695. })
  696. describe('Institution with SSO beta testable', function () {
  697. beforeEach(function (done) {
  698. this.UserGetter.getUserFullEmails.yields(null, [
  699. {
  700. email: 'beta@beta.com',
  701. affiliation: {
  702. institution: {
  703. id: 2,
  704. confirmed: true,
  705. name: 'Beta University',
  706. ssoBeta: true,
  707. ssoEnabled: false,
  708. },
  709. },
  710. },
  711. ])
  712. done()
  713. })
  714. it('should show institution SSO available notification when on a beta testing session', function () {
  715. this.req.session.samlBeta = true
  716. this.res.render = (pageName, opts) => {
  717. expect(opts.notificationsInstitution).to.deep.include({
  718. email: 'beta@beta.com',
  719. institutionId: 2,
  720. institutionName: 'Beta University',
  721. templateKey: 'notification_institution_sso_available',
  722. })
  723. }
  724. this.ProjectController.projectListPage(this.req, this.res)
  725. })
  726. it('should not show institution SSO available notification when not on a beta testing session', function () {
  727. this.req.session.samlBeta = false
  728. this.res.render = (pageName, opts) => {
  729. expect(opts.notificationsInstitution).to.deep.not.include({
  730. email: 'test@overleaf.com',
  731. institutionId: 1,
  732. institutionName: 'Overleaf',
  733. templateKey: 'notification_institution_sso_available',
  734. })
  735. }
  736. this.ProjectController.projectListPage(this.req, this.res)
  737. })
  738. })
  739. })
  740. describe('Without Institution SSO feature', function () {
  741. beforeEach(function (done) {
  742. this.Features.hasFeature.withArgs('saml').returns(false)
  743. done()
  744. })
  745. it('should not show institution sso available notification', function () {
  746. this.res.render = (pageName, opts) => {
  747. expect(opts.notificationsInstitution).to.deep.not.include({
  748. email: 'test@overleaf.com',
  749. institutionId: 1,
  750. institutionName: 'Overleaf',
  751. templateKey: 'notification_institution_sso_available',
  752. })
  753. }
  754. this.ProjectController.projectListPage(this.req, this.res)
  755. })
  756. })
  757. })
  758. describe('projectListPage with duplicate projects', function () {
  759. beforeEach(function () {
  760. this.tags = [
  761. { name: 1, project_ids: ['1', '2', '3'] },
  762. { name: 2, project_ids: ['a', '1'] },
  763. { name: 3, project_ids: ['a', 'b', 'c', 'd'] },
  764. ]
  765. this.notifications = [
  766. {
  767. _id: '1',
  768. user_id: '2',
  769. templateKey: '3',
  770. messageOpts: '4',
  771. key: '5',
  772. },
  773. ]
  774. this.projects = [
  775. { _id: 1, lastUpdated: 1, owner_ref: 'user-1' },
  776. { _id: 2, lastUpdated: 2, owner_ref: 'user-2' },
  777. ]
  778. this.collabertions = [{ _id: 5, lastUpdated: 5, owner_ref: 'user-1' }]
  779. this.readOnly = [{ _id: 3, lastUpdated: 3, owner_ref: 'user-1' }]
  780. this.tokenReadAndWrite = [{ _id: 6, lastUpdated: 5, owner_ref: 'user-4' }]
  781. this.tokenReadOnly = [
  782. { _id: 6, lastUpdated: 5, owner_ref: 'user-4' }, // Also in tokenReadAndWrite
  783. { _id: 7, lastUpdated: 4, owner_ref: 'user-5' },
  784. ]
  785. this.allProjects = {
  786. owned: this.projects,
  787. readAndWrite: this.collabertions,
  788. readOnly: this.readOnly,
  789. tokenReadAndWrite: this.tokenReadAndWrite,
  790. tokenReadOnly: this.tokenReadOnly,
  791. }
  792. this.users = {
  793. 'user-1': {
  794. first_name: 'James',
  795. },
  796. 'user-2': {
  797. first_name: 'Henry',
  798. },
  799. }
  800. this.users[this.user._id] = this.user // Owner
  801. this.UserModel.findById = (id, fields, callback) => {
  802. callback(null, this.users[id])
  803. }
  804. this.LimitationsManager.hasPaidSubscription.callsArgWith(1, null, false)
  805. this.TagsHandler.getAllTags.callsArgWith(1, null, this.tags)
  806. this.NotificationsHandler.getUserNotifications = sinon
  807. .stub()
  808. .callsArgWith(1, null, this.notifications, {})
  809. this.ProjectGetter.findAllUsersProjects.callsArgWith(
  810. 2,
  811. null,
  812. this.allProjects
  813. )
  814. })
  815. it('should render the project/list page', function (done) {
  816. this.res.render = (pageName, opts) => {
  817. pageName.should.equal('project/list')
  818. done()
  819. }
  820. this.ProjectController.projectListPage(this.req, this.res)
  821. })
  822. it('should omit one of the projects', function (done) {
  823. this.res.render = (pageName, opts) => {
  824. opts.projects.length.should.equal(
  825. this.projects.length +
  826. this.collabertions.length +
  827. this.readOnly.length +
  828. this.tokenReadAndWrite.length +
  829. this.tokenReadOnly.length -
  830. 1
  831. )
  832. done()
  833. }
  834. this.ProjectController.projectListPage(this.req, this.res)
  835. })
  836. })
  837. describe('renameProject', function () {
  838. beforeEach(function () {
  839. this.newProjectName = 'my supper great new project'
  840. this.req.body.newProjectName = this.newProjectName
  841. })
  842. it('should call the editor controller', function (done) {
  843. this.EditorController.renameProject.callsArgWith(2)
  844. this.res.sendStatus = code => {
  845. code.should.equal(200)
  846. this.EditorController.renameProject
  847. .calledWith(this.project_id, this.newProjectName)
  848. .should.equal(true)
  849. done()
  850. }
  851. this.ProjectController.renameProject(this.req, this.res)
  852. })
  853. it('should send an error to next() if there is a problem', function (done) {
  854. let error
  855. this.EditorController.renameProject.callsArgWith(
  856. 2,
  857. (error = new Error('problem'))
  858. )
  859. const next = e => {
  860. e.should.equal(error)
  861. done()
  862. }
  863. this.ProjectController.renameProject(this.req, this.res, next)
  864. })
  865. })
  866. describe('loadEditor', function () {
  867. beforeEach(function () {
  868. this.settings.editorIsOpen = true
  869. this.project = {
  870. name: 'my proj',
  871. _id: '213123kjlkj',
  872. owner_ref: '59fc84d5fbea77482d436e1b',
  873. }
  874. this.brandedProject = {
  875. name: 'my branded proj',
  876. _id: '3252332',
  877. owner_ref: '59fc84d5fbea77482d436e1b',
  878. brandVariationId: '12',
  879. }
  880. this.user = {
  881. _id: '588f3ddae8ebc1bac07c9fa4',
  882. ace: {
  883. fontSize: 'massive',
  884. theme: 'sexy',
  885. },
  886. email: 'bob@bob.com',
  887. refProviders: {
  888. mendeley: { encrypted: 'aaaa' },
  889. zotero: { encrypted: 'bbbb' },
  890. },
  891. }
  892. this.ProjectGetter.getProject.callsArgWith(2, null, this.project)
  893. this.UserModel.findById.callsArgWith(2, null, this.user)
  894. this.SubscriptionLocator.getUsersSubscription.callsArgWith(1, null, {})
  895. this.AuthorizationManager.getPrivilegeLevelForProject.callsArgWith(
  896. 3,
  897. null,
  898. 'owner'
  899. )
  900. this.ProjectDeleter.unmarkAsDeletedByExternalSource = sinon.stub()
  901. this.InactiveProjectManager.reactivateProjectIfRequired.callsArgWith(1)
  902. this.ProjectUpdateHandler.markAsOpened.callsArgWith(1)
  903. })
  904. it('should render the project/editor page', function (done) {
  905. this.res.render = (pageName, opts) => {
  906. pageName.should.equal('project/editor')
  907. done()
  908. }
  909. this.ProjectController.loadEditor(this.req, this.res)
  910. })
  911. it('should add user', function (done) {
  912. this.res.render = (pageName, opts) => {
  913. opts.user.email.should.equal(this.user.email)
  914. done()
  915. }
  916. this.ProjectController.loadEditor(this.req, this.res)
  917. })
  918. it('should sanitize refProviders', function (done) {
  919. this.res.render = (_pageName, opts) => {
  920. expect(opts.user.refProviders).to.deep.equal({
  921. mendeley: true,
  922. zotero: true,
  923. })
  924. done()
  925. }
  926. this.ProjectController.loadEditor(this.req, this.res)
  927. })
  928. it('should add on userSettings', function (done) {
  929. this.res.render = (pageName, opts) => {
  930. opts.userSettings.fontSize.should.equal(this.user.ace.fontSize)
  931. opts.userSettings.editorTheme.should.equal(this.user.ace.theme)
  932. done()
  933. }
  934. this.ProjectController.loadEditor(this.req, this.res)
  935. })
  936. it('should add isRestrictedTokenMember', function (done) {
  937. this.res.render = (pageName, opts) => {
  938. opts.isRestrictedTokenMember.should.exist
  939. opts.isRestrictedTokenMember.should.equal(false)
  940. return done()
  941. }
  942. return this.ProjectController.loadEditor(this.req, this.res)
  943. })
  944. it('should set isRestrictedTokenMember when appropriate', function (done) {
  945. this.AuthorizationManager.isRestrictedUser.returns(true)
  946. this.res.render = (pageName, opts) => {
  947. opts.isRestrictedTokenMember.should.exist
  948. opts.isRestrictedTokenMember.should.equal(true)
  949. return done()
  950. }
  951. return this.ProjectController.loadEditor(this.req, this.res)
  952. })
  953. it('should render the closed page if the editor is closed', function (done) {
  954. this.settings.editorIsOpen = false
  955. this.res.render = (pageName, opts) => {
  956. pageName.should.equal('general/closed')
  957. done()
  958. }
  959. this.ProjectController.loadEditor(this.req, this.res)
  960. })
  961. it('should not render the page if the project can not be accessed', function (done) {
  962. this.AuthorizationManager.getPrivilegeLevelForProject = sinon
  963. .stub()
  964. .callsArgWith(3, null, null)
  965. this.res.sendStatus = (resCode, opts) => {
  966. resCode.should.equal(401)
  967. done()
  968. }
  969. this.ProjectController.loadEditor(this.req, this.res)
  970. })
  971. it('should reactivateProjectIfRequired', function (done) {
  972. this.res.render = (pageName, opts) => {
  973. this.InactiveProjectManager.reactivateProjectIfRequired
  974. .calledWith(this.project_id)
  975. .should.equal(true)
  976. done()
  977. }
  978. this.ProjectController.loadEditor(this.req, this.res)
  979. })
  980. it('should mark project as opened', function (done) {
  981. this.res.render = (pageName, opts) => {
  982. this.ProjectUpdateHandler.markAsOpened
  983. .calledWith(this.project_id)
  984. .should.equal(true)
  985. done()
  986. }
  987. this.ProjectController.loadEditor(this.req, this.res)
  988. })
  989. it('should call the brand variations handler for branded projects', function (done) {
  990. this.ProjectGetter.getProject.callsArgWith(2, null, this.brandedProject)
  991. this.res.render = (pageName, opts) => {
  992. this.BrandVariationsHandler.getBrandVariationById
  993. .calledWith()
  994. .should.equal(true)
  995. done()
  996. }
  997. this.ProjectController.loadEditor(this.req, this.res)
  998. })
  999. it('should not call the brand variations handler for unbranded projects', function (done) {
  1000. this.res.render = (pageName, opts) => {
  1001. this.BrandVariationsHandler.getBrandVariationById.called.should.equal(
  1002. false
  1003. )
  1004. done()
  1005. }
  1006. this.ProjectController.loadEditor(this.req, this.res)
  1007. })
  1008. it('should expose the brand variation details as locals for branded projects', function (done) {
  1009. this.ProjectGetter.getProject.callsArgWith(2, null, this.brandedProject)
  1010. this.res.render = (pageName, opts) => {
  1011. opts.brandVariation.should.deep.equal(this.brandVariationDetails)
  1012. done()
  1013. }
  1014. this.ProjectController.loadEditor(this.req, this.res)
  1015. })
  1016. it('flushes the project to TPDS if a flush is pending', function (done) {
  1017. this.res.render = () => {
  1018. this.TpdsProjectFlusher.flushProjectToTpdsIfNeeded.should.have.been.calledWith(
  1019. this.project_id
  1020. )
  1021. done()
  1022. }
  1023. this.ProjectController.loadEditor(this.req, this.res)
  1024. })
  1025. describe('pdf caching feature flags', function () {
  1026. /* eslint-disable mocha/no-identical-title */
  1027. function showNoVariant() {
  1028. beforeEach(function () {
  1029. this.SplitTestHandler.getTestSegmentation = sinon
  1030. .stub()
  1031. .yields(null, { enabled: false })
  1032. })
  1033. }
  1034. function showVariant(variant) {
  1035. beforeEach(function () {
  1036. this.SplitTestHandler.getTestSegmentation = sinon
  1037. .stub()
  1038. .yields(null, { enabled: true, variant })
  1039. })
  1040. }
  1041. function expectBandwidthTrackingEnabled() {
  1042. it('should track pdf bandwidth', function (done) {
  1043. this.res.render = (pageName, opts) => {
  1044. expect(opts.trackPdfDownload).to.equal(true)
  1045. done()
  1046. }
  1047. this.ProjectController.loadEditor(this.req, this.res)
  1048. })
  1049. }
  1050. function expectPDFCachingEnabled() {
  1051. it('should enable pdf caching', function (done) {
  1052. this.res.render = (pageName, opts) => {
  1053. expect(opts.enablePdfCaching).to.equal(true)
  1054. done()
  1055. }
  1056. this.ProjectController.loadEditor(this.req, this.res)
  1057. })
  1058. }
  1059. function expectBandwidthTrackingDisabled() {
  1060. it('should not track pdf bandwidth', function (done) {
  1061. this.res.render = (pageName, opts) => {
  1062. expect(opts.trackPdfDownload).to.equal(false)
  1063. done()
  1064. }
  1065. this.ProjectController.loadEditor(this.req, this.res)
  1066. })
  1067. }
  1068. function expectPDFCachingDisabled() {
  1069. it('should disable pdf caching', function (done) {
  1070. this.res.render = (pageName, opts) => {
  1071. expect(opts.enablePdfCaching).to.equal(false)
  1072. done()
  1073. }
  1074. this.ProjectController.loadEditor(this.req, this.res)
  1075. })
  1076. }
  1077. function expectToCollectMetricsAndCachePDF() {
  1078. describe('with no query', function () {
  1079. expectBandwidthTrackingEnabled()
  1080. expectPDFCachingEnabled()
  1081. })
  1082. describe('with enable_pdf_caching=false', function () {
  1083. beforeEach(function () {
  1084. this.req.query.enable_pdf_caching = 'false'
  1085. })
  1086. expectBandwidthTrackingDisabled()
  1087. expectPDFCachingDisabled()
  1088. })
  1089. describe('with enable_pdf_caching=true', function () {
  1090. beforeEach(function () {
  1091. this.req.query.enable_pdf_caching = 'true'
  1092. })
  1093. expectBandwidthTrackingEnabled()
  1094. expectPDFCachingEnabled()
  1095. })
  1096. }
  1097. function expectToCollectMetricsOnly() {
  1098. describe('with no query', function () {
  1099. expectBandwidthTrackingEnabled()
  1100. expectPDFCachingDisabled()
  1101. })
  1102. describe('with enable_pdf_caching=false', function () {
  1103. beforeEach(function () {
  1104. this.req.query.enable_pdf_caching = 'false'
  1105. })
  1106. expectBandwidthTrackingDisabled()
  1107. expectPDFCachingDisabled()
  1108. })
  1109. describe('with enable_pdf_caching=true', function () {
  1110. beforeEach(function () {
  1111. this.req.query.enable_pdf_caching = 'true'
  1112. })
  1113. expectBandwidthTrackingEnabled()
  1114. expectPDFCachingDisabled()
  1115. })
  1116. }
  1117. function expectToCachePDFOnly() {
  1118. describe('with no query', function () {
  1119. expectBandwidthTrackingDisabled()
  1120. expectPDFCachingEnabled()
  1121. })
  1122. describe('with enable_pdf_caching=false', function () {
  1123. beforeEach(function () {
  1124. this.req.query.enable_pdf_caching = 'false'
  1125. })
  1126. expectBandwidthTrackingDisabled()
  1127. expectPDFCachingDisabled()
  1128. })
  1129. describe('with enable_pdf_caching=true', function () {
  1130. beforeEach(function () {
  1131. this.req.query.enable_pdf_caching = 'true'
  1132. })
  1133. expectBandwidthTrackingDisabled()
  1134. expectPDFCachingEnabled()
  1135. })
  1136. }
  1137. function expectToNotBeEnrolledAtAll() {
  1138. describe('with no query', function () {
  1139. expectBandwidthTrackingDisabled()
  1140. expectPDFCachingDisabled()
  1141. })
  1142. describe('with enable_pdf_caching=false', function () {
  1143. beforeEach(function () {
  1144. this.req.query.enable_pdf_caching = 'false'
  1145. })
  1146. expectBandwidthTrackingDisabled()
  1147. expectPDFCachingDisabled()
  1148. })
  1149. describe('with enable_pdf_caching=true', function () {
  1150. beforeEach(function () {
  1151. this.req.query.enable_pdf_caching = 'true'
  1152. })
  1153. expectBandwidthTrackingDisabled()
  1154. expectPDFCachingDisabled()
  1155. })
  1156. }
  1157. function tagAnonymous() {
  1158. beforeEach(function () {
  1159. this.SessionManager.isUserLoggedIn = sinon.stub().returns(false)
  1160. })
  1161. }
  1162. beforeEach(function () {
  1163. this.settings.enablePdfCaching = true
  1164. })
  1165. describe('during regular roll-out', function () {
  1166. before(function () {
  1167. this.skip()
  1168. })
  1169. describe('disabled', function () {
  1170. showNoVariant()
  1171. describe('regular user', function () {
  1172. expectToNotBeEnrolledAtAll()
  1173. })
  1174. describe('anonymous user', function () {
  1175. tagAnonymous()
  1176. expectToCachePDFOnly()
  1177. })
  1178. })
  1179. describe('variant=collect-metrics', function () {
  1180. showVariant('collect-metrics')
  1181. describe('regular user', function () {
  1182. expectToCollectMetricsOnly()
  1183. })
  1184. describe('anonymous user', function () {
  1185. tagAnonymous()
  1186. expectToCachePDFOnly()
  1187. })
  1188. })
  1189. describe('variant=collect-metrics-and-enable-caching', function () {
  1190. showVariant('collect-metrics-and-enable-caching')
  1191. describe('regular user', function () {
  1192. expectToCollectMetricsAndCachePDF()
  1193. })
  1194. describe('anonymous user', function () {
  1195. tagAnonymous()
  1196. expectToCachePDFOnly()
  1197. })
  1198. })
  1199. describe('variant=enable-caching-only', function () {
  1200. showVariant('enable-caching-only')
  1201. describe('regular user', function () {
  1202. expectToCachePDFOnly()
  1203. })
  1204. describe('anonymous user', function () {
  1205. tagAnonymous()
  1206. expectToCachePDFOnly()
  1207. })
  1208. })
  1209. })
  1210. describe('during opt-in only', function () {
  1211. describe('with no query', function () {
  1212. expectBandwidthTrackingDisabled()
  1213. expectPDFCachingDisabled()
  1214. })
  1215. describe('with enable_pdf_caching=false', function () {
  1216. beforeEach(function () {
  1217. this.req.query.enable_pdf_caching = 'false'
  1218. })
  1219. expectBandwidthTrackingDisabled()
  1220. expectPDFCachingDisabled()
  1221. })
  1222. describe('with enable_pdf_caching=true', function () {
  1223. beforeEach(function () {
  1224. this.req.query.enable_pdf_caching = 'true'
  1225. })
  1226. expectBandwidthTrackingEnabled()
  1227. expectPDFCachingEnabled()
  1228. })
  1229. })
  1230. })
  1231. describe('wsUrl', function () {
  1232. function checkLoadEditorWsMetric(metric) {
  1233. it(`should inc metric ${metric}`, function (done) {
  1234. this.res.render = () => {
  1235. this.Metrics.inc.calledWith(metric).should.equal(true)
  1236. done()
  1237. }
  1238. this.ProjectController.loadEditor(this.req, this.res)
  1239. })
  1240. }
  1241. function checkWsFallback(isBeta, isV2) {
  1242. describe('with ws=fallback', function () {
  1243. beforeEach(function () {
  1244. this.req.query = {}
  1245. this.req.query.ws = 'fallback'
  1246. })
  1247. it('should unset the wsUrl', function (done) {
  1248. this.res.render = (pageName, opts) => {
  1249. ;(opts.wsUrl || '/socket.io').should.equal('/socket.io')
  1250. done()
  1251. }
  1252. this.ProjectController.loadEditor(this.req, this.res)
  1253. })
  1254. checkLoadEditorWsMetric(
  1255. `load-editor-ws${isBeta ? '-beta' : ''}${
  1256. isV2 ? '-v2' : ''
  1257. }-fallback`
  1258. )
  1259. })
  1260. }
  1261. beforeEach(function () {
  1262. this.settings.wsUrl = '/other.socket.io'
  1263. })
  1264. it('should set the custom wsUrl', function (done) {
  1265. this.res.render = (pageName, opts) => {
  1266. opts.wsUrl.should.equal('/other.socket.io')
  1267. done()
  1268. }
  1269. this.ProjectController.loadEditor(this.req, this.res)
  1270. })
  1271. checkLoadEditorWsMetric('load-editor-ws')
  1272. checkWsFallback(false)
  1273. describe('beta program', function () {
  1274. beforeEach(function () {
  1275. this.settings.wsUrlBeta = '/beta.socket.io'
  1276. })
  1277. describe('for a normal user', function () {
  1278. it('should set the normal custom wsUrl', function (done) {
  1279. this.res.render = (pageName, opts) => {
  1280. opts.wsUrl.should.equal('/other.socket.io')
  1281. done()
  1282. }
  1283. this.ProjectController.loadEditor(this.req, this.res)
  1284. })
  1285. checkLoadEditorWsMetric('load-editor-ws')
  1286. checkWsFallback(false)
  1287. })
  1288. describe('for a beta user', function () {
  1289. beforeEach(function () {
  1290. this.user.betaProgram = true
  1291. })
  1292. it('should set the beta wsUrl', function (done) {
  1293. this.res.render = (pageName, opts) => {
  1294. opts.wsUrl.should.equal('/beta.socket.io')
  1295. done()
  1296. }
  1297. this.ProjectController.loadEditor(this.req, this.res)
  1298. })
  1299. checkLoadEditorWsMetric('load-editor-ws-beta')
  1300. checkWsFallback(true)
  1301. })
  1302. })
  1303. describe('v2-rollout', function () {
  1304. beforeEach(function () {
  1305. this.settings.wsUrlBeta = '/beta.socket.io'
  1306. this.settings.wsUrlV2 = '/socket.io.v2'
  1307. })
  1308. function checkNonMatch() {
  1309. it('should set the normal custom wsUrl', function (done) {
  1310. this.res.render = (pageName, opts) => {
  1311. opts.wsUrl.should.equal('/other.socket.io')
  1312. done()
  1313. }
  1314. this.ProjectController.loadEditor(this.req, this.res)
  1315. })
  1316. checkLoadEditorWsMetric('load-editor-ws')
  1317. checkWsFallback(false)
  1318. }
  1319. function checkMatch() {
  1320. it('should set the v2 wsUrl', function (done) {
  1321. this.res.render = (pageName, opts) => {
  1322. opts.wsUrl.should.equal('/socket.io.v2')
  1323. done()
  1324. }
  1325. this.ProjectController.loadEditor(this.req, this.res)
  1326. })
  1327. checkLoadEditorWsMetric('load-editor-ws-v2')
  1328. checkWsFallback(false, true)
  1329. }
  1330. function checkForBetaUser() {
  1331. describe('for a beta user', function () {
  1332. beforeEach(function () {
  1333. this.user.betaProgram = true
  1334. })
  1335. it('should set the beta wsUrl', function (done) {
  1336. this.res.render = (pageName, opts) => {
  1337. opts.wsUrl.should.equal('/beta.socket.io')
  1338. done()
  1339. }
  1340. this.ProjectController.loadEditor(this.req, this.res)
  1341. })
  1342. checkLoadEditorWsMetric('load-editor-ws-beta')
  1343. checkWsFallback(true)
  1344. })
  1345. }
  1346. describe('when the roll out percentage is 0', function () {
  1347. beforeEach(function () {
  1348. this.settings.wsUrlV2Percentage = 0
  1349. })
  1350. describe('when the projectId does not match (0)', function () {
  1351. beforeEach(function () {
  1352. this.req.params.Project_id = ObjectId.createFromTime(0)
  1353. })
  1354. checkNonMatch()
  1355. })
  1356. describe('when the projectId does not match (42)', function () {
  1357. beforeEach(function () {
  1358. this.req.params.Project_id = ObjectId.createFromTime(42)
  1359. })
  1360. checkNonMatch()
  1361. })
  1362. checkForBetaUser()
  1363. })
  1364. describe('when the roll out percentage is 1', function () {
  1365. beforeEach(function () {
  1366. this.settings.wsUrlV2Percentage = 1
  1367. })
  1368. describe('when the projectId matches (0)', function () {
  1369. beforeEach(function () {
  1370. this.req.params.Project_id = ObjectId.createFromTime(0)
  1371. })
  1372. checkMatch()
  1373. checkForBetaUser()
  1374. })
  1375. describe('when the projectId does not match (1)', function () {
  1376. beforeEach(function () {
  1377. this.req.params.Project_id = ObjectId.createFromTime(1)
  1378. })
  1379. checkNonMatch()
  1380. checkForBetaUser()
  1381. })
  1382. describe('when the projectId does not match (42)', function () {
  1383. beforeEach(function () {
  1384. this.req.params.Project_id = ObjectId.createFromTime(42)
  1385. })
  1386. checkNonMatch()
  1387. })
  1388. })
  1389. describe('when the roll out percentage is 10', function () {
  1390. beforeEach(function () {
  1391. this.settings.wsUrlV2Percentage = 10
  1392. })
  1393. describe('when the projectId matches (0)', function () {
  1394. beforeEach(function () {
  1395. this.req.params.Project_id = ObjectId.createFromTime(0)
  1396. })
  1397. checkMatch()
  1398. })
  1399. describe('when the projectId matches (9)', function () {
  1400. beforeEach(function () {
  1401. this.req.params.Project_id = ObjectId.createFromTime(9)
  1402. })
  1403. checkMatch()
  1404. checkForBetaUser()
  1405. })
  1406. describe('when the projectId does not match (10)', function () {
  1407. beforeEach(function () {
  1408. this.req.params.Project_id = ObjectId.createFromTime(10)
  1409. })
  1410. checkNonMatch()
  1411. })
  1412. describe('when the projectId does not match (42)', function () {
  1413. beforeEach(function () {
  1414. this.req.params.Project_id = ObjectId.createFromTime(42)
  1415. })
  1416. checkNonMatch()
  1417. checkForBetaUser()
  1418. })
  1419. })
  1420. describe('when the roll out percentage is 100', function () {
  1421. beforeEach(function () {
  1422. this.settings.wsUrlV2Percentage = 100
  1423. })
  1424. describe('when the projectId matches (0)', function () {
  1425. beforeEach(function () {
  1426. this.req.params.Project_id = ObjectId.createFromTime(0)
  1427. })
  1428. checkMatch()
  1429. checkForBetaUser()
  1430. })
  1431. describe('when the projectId matches (10)', function () {
  1432. beforeEach(function () {
  1433. this.req.params.Project_id = ObjectId.createFromTime(10)
  1434. })
  1435. checkMatch()
  1436. })
  1437. describe('when the projectId matches (42)', function () {
  1438. beforeEach(function () {
  1439. this.req.params.Project_id = ObjectId.createFromTime(42)
  1440. })
  1441. checkMatch()
  1442. })
  1443. describe('when the projectId matches (99)', function () {
  1444. beforeEach(function () {
  1445. this.req.params.Project_id = ObjectId.createFromTime(99)
  1446. })
  1447. checkMatch()
  1448. })
  1449. })
  1450. })
  1451. })
  1452. })
  1453. describe('userProjectsJson', function () {
  1454. beforeEach(function (done) {
  1455. const projects = [
  1456. {
  1457. archived: true,
  1458. trashed: true,
  1459. id: 'a',
  1460. name: 'A',
  1461. accessLevel: 'a',
  1462. somethingElse: 1,
  1463. },
  1464. {
  1465. archived: false,
  1466. id: 'b',
  1467. name: 'B',
  1468. accessLevel: 'b',
  1469. somethingElse: 1,
  1470. },
  1471. {
  1472. archived: false,
  1473. trashed: true,
  1474. id: 'c',
  1475. name: 'C',
  1476. accessLevel: 'c',
  1477. somethingElse: 1,
  1478. },
  1479. {
  1480. archived: false,
  1481. trashed: false,
  1482. id: 'd',
  1483. name: 'D',
  1484. accessLevel: 'd',
  1485. somethingElse: 1,
  1486. },
  1487. ]
  1488. this.ProjectHelper.isArchivedOrTrashed
  1489. .withArgs(projects[0], this.user._id)
  1490. .returns(true)
  1491. this.ProjectHelper.isArchivedOrTrashed
  1492. .withArgs(projects[1], this.user._id)
  1493. .returns(false)
  1494. this.ProjectHelper.isArchivedOrTrashed
  1495. .withArgs(projects[2], this.user._id)
  1496. .returns(true)
  1497. this.ProjectHelper.isArchivedOrTrashed
  1498. .withArgs(projects[3], this.user._id)
  1499. .returns(false)
  1500. this.ProjectGetter.findAllUsersProjects = sinon
  1501. .stub()
  1502. .callsArgWith(2, null, [])
  1503. this.ProjectController._buildProjectList = sinon.stub().returns(projects)
  1504. this.SessionManager.getLoggedInUserId = sinon
  1505. .stub()
  1506. .returns(this.user._id)
  1507. done()
  1508. })
  1509. it('should produce a list of projects', function (done) {
  1510. this.res.json = data => {
  1511. expect(data).to.deep.equal({
  1512. projects: [
  1513. { _id: 'b', name: 'B', accessLevel: 'b' },
  1514. { _id: 'd', name: 'D', accessLevel: 'd' },
  1515. ],
  1516. })
  1517. done()
  1518. }
  1519. this.ProjectController.userProjectsJson(this.req, this.res, this.next)
  1520. })
  1521. })
  1522. describe('projectEntitiesJson', function () {
  1523. beforeEach(function () {
  1524. this.SessionManager.getLoggedInUserId = sinon.stub().returns('abc')
  1525. this.req.params = { Project_id: 'abcd' }
  1526. this.project = { _id: 'abcd' }
  1527. this.docs = [
  1528. { path: '/things/b.txt', doc: true },
  1529. { path: '/main.tex', doc: true },
  1530. ]
  1531. this.files = [{ path: '/things/a.txt' }]
  1532. this.ProjectGetter.getProject = sinon
  1533. .stub()
  1534. .callsArgWith(1, null, this.project)
  1535. this.ProjectEntityHandler.getAllEntitiesFromProject = sinon
  1536. .stub()
  1537. .callsArgWith(1, null, this.docs, this.files)
  1538. })
  1539. it('should produce a list of entities', function (done) {
  1540. this.res.json = data => {
  1541. expect(data).to.deep.equal({
  1542. project_id: 'abcd',
  1543. entities: [
  1544. { path: '/main.tex', type: 'doc' },
  1545. { path: '/things/a.txt', type: 'file' },
  1546. { path: '/things/b.txt', type: 'doc' },
  1547. ],
  1548. })
  1549. expect(this.ProjectGetter.getProject.callCount).to.equal(1)
  1550. expect(
  1551. this.ProjectEntityHandler.getAllEntitiesFromProject.callCount
  1552. ).to.equal(1)
  1553. done()
  1554. }
  1555. this.ProjectController.projectEntitiesJson(this.req, this.res, this.next)
  1556. })
  1557. })
  1558. describe('_buildProjectViewModel', function () {
  1559. beforeEach(function () {
  1560. this.ProjectHelper.isArchived.returns(false)
  1561. this.ProjectHelper.isTrashed.returns(false)
  1562. this.project = {
  1563. _id: 'abcd',
  1564. name: 'netsenits',
  1565. lastUpdated: 1,
  1566. lastUpdatedBy: 2,
  1567. publicAccesLevel: 'private',
  1568. archived: false,
  1569. owner_ref: 'defg',
  1570. tokens: {
  1571. readAndWrite: '1abcd',
  1572. readAndWritePrefix: '1',
  1573. readOnly: 'neiotsranteoia',
  1574. },
  1575. }
  1576. })
  1577. describe('project not being archived or trashed', function () {
  1578. it('should produce a model of the project', function () {
  1579. const result = this.ProjectController._buildProjectViewModel(
  1580. this.project,
  1581. 'readAndWrite',
  1582. 'owner',
  1583. this.user._id
  1584. )
  1585. expect(result).to.exist
  1586. expect(result).to.be.an('object')
  1587. expect(result).to.deep.equal({
  1588. id: 'abcd',
  1589. name: 'netsenits',
  1590. lastUpdated: 1,
  1591. lastUpdatedBy: 2,
  1592. publicAccessLevel: 'private',
  1593. accessLevel: 'readAndWrite',
  1594. source: 'owner',
  1595. archived: false,
  1596. trashed: false,
  1597. owner_ref: 'defg',
  1598. isV1Project: false,
  1599. })
  1600. })
  1601. })
  1602. describe('project being simultaneously archived and trashed', function () {
  1603. beforeEach(function () {
  1604. this.ProjectHelper.isArchived.returns(true)
  1605. this.ProjectHelper.isTrashed.returns(true)
  1606. })
  1607. it('should produce a model of the project', function () {
  1608. const result = this.ProjectController._buildProjectViewModel(
  1609. this.project,
  1610. 'readAndWrite',
  1611. 'owner',
  1612. this.user._id
  1613. )
  1614. expect(result).to.exist
  1615. expect(result).to.be.an('object')
  1616. expect(result).to.deep.equal({
  1617. id: 'abcd',
  1618. name: 'netsenits',
  1619. lastUpdated: 1,
  1620. lastUpdatedBy: 2,
  1621. publicAccessLevel: 'private',
  1622. accessLevel: 'readAndWrite',
  1623. source: 'owner',
  1624. archived: true,
  1625. trashed: false,
  1626. owner_ref: 'defg',
  1627. isV1Project: false,
  1628. })
  1629. })
  1630. })
  1631. describe('when token-read-only access', function () {
  1632. it('should redact the owner and last-updated data', function () {
  1633. const result = this.ProjectController._buildProjectViewModel(
  1634. this.project,
  1635. 'readOnly',
  1636. 'token',
  1637. this.user._id
  1638. )
  1639. expect(result).to.exist
  1640. expect(result).to.be.an('object')
  1641. expect(result).to.deep.equal({
  1642. id: 'abcd',
  1643. name: 'netsenits',
  1644. lastUpdated: 1,
  1645. lastUpdatedBy: null,
  1646. publicAccessLevel: 'private',
  1647. accessLevel: 'readOnly',
  1648. source: 'token',
  1649. archived: false,
  1650. trashed: false,
  1651. owner_ref: null,
  1652. isV1Project: false,
  1653. })
  1654. })
  1655. })
  1656. })
  1657. describe('_isInPercentageRollout', function () {
  1658. before(function () {
  1659. this.ids = [
  1660. '5a05cd7621f9fe22be131740',
  1661. '5a05cd7821f9fe22be131741',
  1662. '5a05cd7921f9fe22be131742',
  1663. '5a05cd7a21f9fe22be131743',
  1664. '5a05cd7b21f9fe22be131744',
  1665. '5a05cd7c21f9fe22be131745',
  1666. '5a05cd7d21f9fe22be131746',
  1667. '5a05cd7e21f9fe22be131747',
  1668. '5a05cd7f21f9fe22be131748',
  1669. '5a05cd8021f9fe22be131749',
  1670. '5a05cd8021f9fe22be13174a',
  1671. '5a05cd8121f9fe22be13174b',
  1672. '5a05cd8221f9fe22be13174c',
  1673. '5a05cd8221f9fe22be13174d',
  1674. '5a05cd8321f9fe22be13174e',
  1675. '5a05cd8321f9fe22be13174f',
  1676. '5a05cd8421f9fe22be131750',
  1677. '5a05cd8421f9fe22be131751',
  1678. '5a05cd8421f9fe22be131752',
  1679. '5a05cd8521f9fe22be131753',
  1680. ]
  1681. })
  1682. it('should produce the expected results', function () {
  1683. expect(
  1684. this.ids.map(i =>
  1685. this.ProjectController._isInPercentageRollout('abcd', i, 50)
  1686. )
  1687. ).to.deep.equal([
  1688. false,
  1689. false,
  1690. false,
  1691. false,
  1692. false,
  1693. false,
  1694. true,
  1695. false,
  1696. true,
  1697. true,
  1698. true,
  1699. true,
  1700. true,
  1701. true,
  1702. false,
  1703. false,
  1704. false,
  1705. true,
  1706. false,
  1707. true,
  1708. ])
  1709. expect(
  1710. this.ids.map(i =>
  1711. this.ProjectController._isInPercentageRollout('efgh', i, 50)
  1712. )
  1713. ).to.deep.equal([
  1714. false,
  1715. false,
  1716. false,
  1717. false,
  1718. true,
  1719. false,
  1720. false,
  1721. true,
  1722. false,
  1723. false,
  1724. true,
  1725. true,
  1726. true,
  1727. false,
  1728. true,
  1729. false,
  1730. true,
  1731. true,
  1732. false,
  1733. false,
  1734. ])
  1735. })
  1736. })
  1737. })