ProjectController.test.mjs 54 KB

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