ProjectController.test.mjs 65 KB

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