ProjectControllerTests.js 48 KB

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