ProjectControllerTests.js 47 KB

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