ProjectControllerTests.js 60 KB

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