ProjectController.test.mjs 55 KB

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