ProjectControllerTests.js 47 KB

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