ProjectController.test.mjs 56 KB

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