ProjectController.test.mjs 54 KB

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