ProjectControllerTests.js 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080
  1. /* eslint-disable
  2. max-len,
  3. no-return-assign,
  4. no-unused-vars,
  5. */
  6. // TODO: This file was created by bulk-decaffeinate.
  7. // Fix any style issues and re-enable lint.
  8. /*
  9. * decaffeinate suggestions:
  10. * DS102: Remove unnecessary code created because of implicit returns
  11. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  12. */
  13. const should = require('chai').should()
  14. const SandboxedModule = require('sandboxed-module')
  15. const assert = require('assert')
  16. const path = require('path')
  17. const sinon = require('sinon')
  18. const modulePath = path.join(
  19. __dirname,
  20. '../../../../app/src/Features/Project/ProjectController'
  21. )
  22. const { expect } = require('chai')
  23. const Errors = require('../../../../app/src/Features/Errors/Errors')
  24. describe('ProjectController', function() {
  25. beforeEach(function() {
  26. this.project_id = '123213jlkj9kdlsaj'
  27. this.user = {
  28. _id: '588f3ddae8ebc1bac07c9fa4',
  29. first_name: 'bjkdsjfk',
  30. features: {}
  31. }
  32. this.settings = {
  33. apis: {
  34. chat: {
  35. url: 'chat.com'
  36. }
  37. },
  38. siteUrl: 'mysite.com',
  39. algolia: {}
  40. }
  41. this.brandVariationDetails = {
  42. id: '12',
  43. active: true,
  44. brand_name: 'The journal',
  45. home_url: 'http://www.thejournal.com/',
  46. publish_menu_link_html: 'Submit your paper to the <em>The Journal</em>'
  47. }
  48. this.token = 'some-token'
  49. this.ProjectDeleter = {
  50. archiveProject: sinon.stub().callsArg(1),
  51. deleteProject: sinon.stub().callsArg(2),
  52. restoreProject: sinon.stub().callsArg(1),
  53. findArchivedProjects: sinon.stub()
  54. }
  55. this.ProjectDuplicator = {
  56. duplicate: sinon.stub().callsArgWith(3, null, { _id: this.project_id })
  57. }
  58. this.ProjectCreationHandler = {
  59. createExampleProject: sinon
  60. .stub()
  61. .callsArgWith(2, null, { _id: this.project_id }),
  62. createBasicProject: sinon
  63. .stub()
  64. .callsArgWith(2, null, { _id: this.project_id })
  65. }
  66. this.SubscriptionLocator = { getUsersSubscription: sinon.stub() }
  67. this.LimitationsManager = { hasPaidSubscription: sinon.stub() }
  68. this.TagsHandler = { getAllTags: sinon.stub() }
  69. this.NotificationsHandler = { getUserNotifications: sinon.stub() }
  70. this.UserModel = { findById: sinon.stub() }
  71. this.AuthorizationManager = { getPrivilegeLevelForProject: sinon.stub() }
  72. this.EditorController = { renameProject: sinon.stub() }
  73. this.InactiveProjectManager = { reactivateProjectIfRequired: sinon.stub() }
  74. this.ProjectUpdateHandler = { markAsOpened: sinon.stub() }
  75. this.ReferencesSearchHandler = { indexProjectReferences: sinon.stub() }
  76. this.ProjectGetter = {
  77. findAllUsersProjects: sinon.stub(),
  78. getProject: sinon.stub()
  79. }
  80. this.ProjectHelper = {
  81. isArchived: sinon.stub(),
  82. isTrashed: sinon.stub(),
  83. isArchivedOrTrashed: sinon.stub()
  84. }
  85. this.AuthenticationController = {
  86. getLoggedInUser: sinon.stub().callsArgWith(1, null, this.user),
  87. getLoggedInUserId: sinon.stub().returns(this.user._id),
  88. getSessionUser: sinon.stub().returns(this.user),
  89. isUserLoggedIn: sinon.stub().returns(true)
  90. }
  91. this.AnalyticsManager = { getLastOccurrence: sinon.stub() }
  92. this.TokenAccessHandler = {
  93. getRequestToken: sinon.stub().returns(this.token),
  94. protectTokens: sinon.stub()
  95. }
  96. this.CollaboratorsHandler = {
  97. userIsTokenMember: sinon.stub().callsArgWith(2, null, false)
  98. }
  99. this.ProjectEntityHandler = {}
  100. this.NotificationBuilder = {
  101. ipMatcherAffiliation: sinon.stub().returns({ create: sinon.stub() })
  102. }
  103. this.UserGetter = {
  104. getUser: sinon
  105. .stub()
  106. .callsArgWith(2, null, { lastLoginIp: '192.170.18.2' }),
  107. getUserOrUserStubById: sinon.stub().callsArgWith(2, null, {})
  108. }
  109. this.Modules = {
  110. hooks: {
  111. fire: sinon.stub()
  112. }
  113. }
  114. this.Features = { hasFeature: sinon.stub() }
  115. this.BrandVariationsHandler = {
  116. getBrandVariationById: sinon
  117. .stub()
  118. .callsArgWith(1, null, this.brandVariationDetails)
  119. }
  120. this.getUserAffiliations = sinon.stub().callsArgWith(1, null, [])
  121. this.ProjectController = SandboxedModule.require(modulePath, {
  122. globals: {
  123. console: console
  124. },
  125. requires: {
  126. 'settings-sharelatex': this.settings,
  127. 'logger-sharelatex': {
  128. log() {},
  129. err() {}
  130. },
  131. 'metrics-sharelatex': {
  132. Timer: class {
  133. done() {}
  134. },
  135. inc() {}
  136. },
  137. './ProjectDeleter': this.ProjectDeleter,
  138. './ProjectDuplicator': this.ProjectDuplicator,
  139. './ProjectCreationHandler': this.ProjectCreationHandler,
  140. '../Editor/EditorController': this.EditorController,
  141. './ProjectHelper': this.ProjectHelper,
  142. '../Subscription/SubscriptionLocator': this.SubscriptionLocator,
  143. '../Subscription/LimitationsManager': this.LimitationsManager,
  144. '../Tags/TagsHandler': this.TagsHandler,
  145. '../Notifications/NotificationsHandler': this.NotificationsHandler,
  146. '../../models/User': {
  147. User: this.UserModel
  148. },
  149. '../Authorization/AuthorizationManager': this.AuthorizationManager,
  150. '../InactiveData/InactiveProjectManager': this.InactiveProjectManager,
  151. './ProjectUpdateHandler': this.ProjectUpdateHandler,
  152. '../ReferencesSearch/ReferencesSearchHandler': this
  153. .ReferencesSearchHandler,
  154. './ProjectGetter': this.ProjectGetter,
  155. '../Authentication/AuthenticationController': this
  156. .AuthenticationController,
  157. '../Analytics/AnalyticsManager': this.AnalyticsManager,
  158. '../TokenAccess/TokenAccessHandler': this.TokenAccessHandler,
  159. '../Collaborators/CollaboratorsHandler': this.CollaboratorsHandler,
  160. '../../infrastructure/Modules': this.Modules,
  161. './ProjectEntityHandler': this.ProjectEntityHandler,
  162. '../Errors/Errors': Errors,
  163. '../../infrastructure/Features': this.Features,
  164. '../Notifications/NotificationsBuilder': this.NotificationBuilder,
  165. '../User/UserGetter': this.UserGetter,
  166. '../BrandVariations/BrandVariationsHandler': this
  167. .BrandVariationsHandler,
  168. '../Institutions/InstitutionsAPI': {
  169. getUserAffiliations: this.getUserAffiliations
  170. },
  171. '../V1/V1Handler': {}
  172. }
  173. })
  174. this.projectName = '£12321jkj9ujkljds'
  175. this.req = {
  176. params: {
  177. Project_id: this.project_id
  178. },
  179. headers: {},
  180. connection: {
  181. remoteAddress: '192.170.18.1'
  182. },
  183. session: {
  184. user: this.user
  185. },
  186. body: {
  187. projectName: this.projectName
  188. },
  189. i18n: {
  190. translate() {}
  191. },
  192. ip: '192.170.18.1'
  193. }
  194. return (this.res = {
  195. locals: {
  196. jsPath: 'js path here'
  197. },
  198. setTimeout: sinon.stub()
  199. })
  200. })
  201. describe('updateProjectSettings', function() {
  202. it('should update the name', function(done) {
  203. this.EditorController.renameProject = sinon.stub().callsArg(2)
  204. this.req.body = { name: (this.name = 'New name') }
  205. this.res.sendStatus = code => {
  206. this.EditorController.renameProject
  207. .calledWith(this.project_id, this.name)
  208. .should.equal(true)
  209. code.should.equal(204)
  210. return done()
  211. }
  212. return this.ProjectController.updateProjectSettings(this.req, this.res)
  213. })
  214. it('should update the compiler', function(done) {
  215. this.EditorController.setCompiler = sinon.stub().callsArg(2)
  216. this.req.body = { compiler: (this.compiler = 'pdflatex') }
  217. this.res.sendStatus = code => {
  218. this.EditorController.setCompiler
  219. .calledWith(this.project_id, this.compiler)
  220. .should.equal(true)
  221. code.should.equal(204)
  222. return done()
  223. }
  224. return this.ProjectController.updateProjectSettings(this.req, this.res)
  225. })
  226. it('should update the imageName', function(done) {
  227. this.EditorController.setImageName = sinon.stub().callsArg(2)
  228. this.req.body = { imageName: (this.imageName = 'texlive-1234.5') }
  229. this.res.sendStatus = code => {
  230. this.EditorController.setImageName
  231. .calledWith(this.project_id, this.imageName)
  232. .should.equal(true)
  233. code.should.equal(204)
  234. return done()
  235. }
  236. return this.ProjectController.updateProjectSettings(this.req, this.res)
  237. })
  238. it('should update the spell check language', function(done) {
  239. this.EditorController.setSpellCheckLanguage = sinon.stub().callsArg(2)
  240. this.req.body = { spellCheckLanguage: (this.languageCode = 'fr') }
  241. this.res.sendStatus = code => {
  242. this.EditorController.setSpellCheckLanguage
  243. .calledWith(this.project_id, this.languageCode)
  244. .should.equal(true)
  245. code.should.equal(204)
  246. return done()
  247. }
  248. return this.ProjectController.updateProjectSettings(this.req, this.res)
  249. })
  250. it('should update the root doc', function(done) {
  251. this.EditorController.setRootDoc = sinon.stub().callsArg(2)
  252. this.req.body = { rootDocId: (this.rootDocId = 'root-doc-id') }
  253. this.res.sendStatus = code => {
  254. this.EditorController.setRootDoc
  255. .calledWith(this.project_id, this.rootDocId)
  256. .should.equal(true)
  257. code.should.equal(204)
  258. return done()
  259. }
  260. return this.ProjectController.updateProjectSettings(this.req, this.res)
  261. })
  262. })
  263. describe('updateProjectAdminSettings', function() {
  264. it('should update the public access level', function(done) {
  265. this.EditorController.setPublicAccessLevel = sinon.stub().callsArg(2)
  266. this.req.body = {
  267. publicAccessLevel: (this.publicAccessLevel = 'readonly')
  268. }
  269. this.res.sendStatus = code => {
  270. this.EditorController.setPublicAccessLevel
  271. .calledWith(this.project_id, this.publicAccessLevel)
  272. .should.equal(true)
  273. code.should.equal(204)
  274. return done()
  275. }
  276. return this.ProjectController.updateProjectAdminSettings(
  277. this.req,
  278. this.res
  279. )
  280. })
  281. })
  282. describe('deleteProject', function() {
  283. it('should tell the project deleter to archive when forever=false', function(done) {
  284. this.res.sendStatus = code => {
  285. this.ProjectDeleter.archiveProject
  286. .calledWith(this.project_id)
  287. .should.equal(true)
  288. code.should.equal(200)
  289. return done()
  290. }
  291. return this.ProjectController.deleteProject(this.req, this.res)
  292. })
  293. it('should tell the project deleter to delete when forever=true', function(done) {
  294. this.req.query = { forever: 'true' }
  295. this.res.sendStatus = code => {
  296. this.ProjectDeleter.deleteProject
  297. .calledWith(this.project_id, {
  298. deleterUser: this.user,
  299. ipAddress: this.req.ip
  300. })
  301. .should.equal(true)
  302. code.should.equal(200)
  303. return done()
  304. }
  305. return this.ProjectController.deleteProject(this.req, this.res)
  306. })
  307. })
  308. describe('restoreProject', function() {
  309. it('should tell the project deleter', function(done) {
  310. this.res.sendStatus = code => {
  311. this.ProjectDeleter.restoreProject
  312. .calledWith(this.project_id)
  313. .should.equal(true)
  314. code.should.equal(200)
  315. return done()
  316. }
  317. return this.ProjectController.restoreProject(this.req, this.res)
  318. })
  319. })
  320. describe('cloneProject', function() {
  321. it('should call the project duplicator', function(done) {
  322. this.res.send = json => {
  323. this.ProjectDuplicator.duplicate
  324. .calledWith(this.user, this.project_id, this.projectName)
  325. .should.equal(true)
  326. json.project_id.should.equal(this.project_id)
  327. return done()
  328. }
  329. return this.ProjectController.cloneProject(this.req, this.res)
  330. })
  331. })
  332. describe('newProject', function() {
  333. it('should call the projectCreationHandler with createExampleProject', function(done) {
  334. this.req.body.template = 'example'
  335. this.res.send = json => {
  336. this.ProjectCreationHandler.createExampleProject
  337. .calledWith(this.user._id, this.projectName)
  338. .should.equal(true)
  339. this.ProjectCreationHandler.createBasicProject.called.should.equal(
  340. false
  341. )
  342. return done()
  343. }
  344. return this.ProjectController.newProject(this.req, this.res)
  345. })
  346. it('should call the projectCreationHandler with createBasicProject', function(done) {
  347. this.req.body.template = 'basic'
  348. this.res.send = json => {
  349. this.ProjectCreationHandler.createExampleProject.called.should.equal(
  350. false
  351. )
  352. this.ProjectCreationHandler.createBasicProject
  353. .calledWith(this.user._id, this.projectName)
  354. .should.equal(true)
  355. return done()
  356. }
  357. return this.ProjectController.newProject(this.req, this.res)
  358. })
  359. })
  360. describe('projectListPage', function() {
  361. beforeEach(function() {
  362. this.tags = [
  363. { name: 1, project_ids: ['1', '2', '3'] },
  364. { name: 2, project_ids: ['a', '1'] },
  365. { name: 3, project_ids: ['a', 'b', 'c', 'd'] }
  366. ]
  367. this.notifications = [
  368. { _id: '1', user_id: '2', templateKey: '3', messageOpts: '4', key: '5' }
  369. ]
  370. this.projects = [
  371. { _id: 1, lastUpdated: 1, owner_ref: 'user-1' },
  372. { _id: 2, lastUpdated: 2, owner_ref: 'user-2', lastUpdatedBy: 'user-1' }
  373. ]
  374. this.collabertions = [{ _id: 5, lastUpdated: 5, owner_ref: 'user-1' }]
  375. this.readOnly = [{ _id: 3, lastUpdated: 3, owner_ref: 'user-1' }]
  376. this.tokenReadAndWrite = [{ _id: 6, lastUpdated: 5, owner_ref: 'user-4' }]
  377. this.tokenReadOnly = [{ _id: 7, lastUpdated: 4, owner_ref: 'user-5' }]
  378. this.allProjects = {
  379. owned: this.projects,
  380. readAndWrite: this.collabertions,
  381. readOnly: this.readOnly,
  382. tokenReadAndWrite: this.tokenReadAndWrite,
  383. tokenReadOnly: this.tokenReadOnly
  384. }
  385. this.users = {
  386. 'user-1': {
  387. first_name: 'James'
  388. },
  389. 'user-2': {
  390. first_name: 'Henry'
  391. }
  392. }
  393. this.users[this.user._id] = this.user // Owner
  394. this.UserModel.findById = (id, fields, callback) => {
  395. return callback(null, this.users[id])
  396. }
  397. this.UserGetter.getUserOrUserStubById = (id, fields, callback) => {
  398. return callback(null, this.users[id])
  399. }
  400. this.LimitationsManager.hasPaidSubscription.callsArgWith(1, null, false)
  401. this.TagsHandler.getAllTags.callsArgWith(1, null, this.tags)
  402. this.NotificationsHandler.getUserNotifications = sinon
  403. .stub()
  404. .callsArgWith(1, null, this.notifications, {})
  405. this.ProjectGetter.findAllUsersProjects.callsArgWith(
  406. 2,
  407. null,
  408. this.allProjects
  409. )
  410. return this.Modules.hooks.fire
  411. .withArgs('findAllV1Projects', this.user._id)
  412. .yields(undefined)
  413. }) // Without integration module hook, cb returns undefined
  414. it('should render the project/list page', function(done) {
  415. this.res.render = (pageName, opts) => {
  416. pageName.should.equal('project/list')
  417. return done()
  418. }
  419. return this.ProjectController.projectListPage(this.req, this.res)
  420. })
  421. it('should send the tags', function(done) {
  422. this.res.render = (pageName, opts) => {
  423. opts.tags.length.should.equal(this.tags.length)
  424. return done()
  425. }
  426. return this.ProjectController.projectListPage(this.req, this.res)
  427. })
  428. it('should create trigger ip matcher notifications', function(done) {
  429. this.settings.overleaf = true
  430. this.res.render = (pageName, opts) => {
  431. this.NotificationBuilder.ipMatcherAffiliation.called.should.equal(true)
  432. return done()
  433. }
  434. return this.ProjectController.projectListPage(this.req, this.res)
  435. })
  436. it('should send the projects', function(done) {
  437. this.res.render = (pageName, opts) => {
  438. opts.projects.length.should.equal(
  439. this.projects.length +
  440. this.collabertions.length +
  441. this.readOnly.length +
  442. this.tokenReadAndWrite.length +
  443. this.tokenReadOnly.length
  444. )
  445. return done()
  446. }
  447. return this.ProjectController.projectListPage(this.req, this.res)
  448. })
  449. it('should send the user', function(done) {
  450. this.res.render = (pageName, opts) => {
  451. opts.user.should.deep.equal(this.user)
  452. return done()
  453. }
  454. return this.ProjectController.projectListPage(this.req, this.res)
  455. })
  456. it('should inject the users', function(done) {
  457. this.res.render = (pageName, opts) => {
  458. opts.projects[0].owner.should.equal(
  459. this.users[this.projects[0].owner_ref]
  460. )
  461. opts.projects[1].owner.should.equal(
  462. this.users[this.projects[1].owner_ref]
  463. )
  464. opts.projects[1].lastUpdatedBy.should.equal(
  465. this.users[this.projects[1].lastUpdatedBy]
  466. )
  467. return done()
  468. }
  469. return this.ProjectController.projectListPage(this.req, this.res)
  470. })
  471. it('should send hasSubscription == false when no subscription', function(done) {
  472. this.res.render = (pageName, opts) => {
  473. opts.hasSubscription.should.equal(false)
  474. return done()
  475. }
  476. return this.ProjectController.projectListPage(this.req, this.res)
  477. })
  478. it('should send hasSubscription == true when there is a subscription', function(done) {
  479. this.LimitationsManager.hasPaidSubscription = sinon
  480. .stub()
  481. .callsArgWith(1, null, true)
  482. this.res.render = (pageName, opts) => {
  483. opts.hasSubscription.should.equal(true)
  484. return done()
  485. }
  486. return this.ProjectController.projectListPage(this.req, this.res)
  487. })
  488. describe('front widget', function(done) {
  489. beforeEach(function() {
  490. return (this.settings.overleaf = {
  491. front_chat_widget_room_id: 'chat-room-id'
  492. })
  493. })
  494. it('should show for paid users', function(done) {
  495. this.user.features.github = true
  496. this.user.features.dropbox = true
  497. this.res.render = (pageName, opts) => {
  498. opts.frontChatWidgetRoomId.should.equal(
  499. this.settings.overleaf.front_chat_widget_room_id
  500. )
  501. return done()
  502. }
  503. return this.ProjectController.projectListPage(this.req, this.res)
  504. })
  505. it('should show for sample users', function(done) {
  506. this.user._id = '588f3ddae8ebc1bac07c9f00' // last two digits
  507. this.res.render = (pageName, opts) => {
  508. opts.frontChatWidgetRoomId.should.equal(
  509. this.settings.overleaf.front_chat_widget_room_id
  510. )
  511. return done()
  512. }
  513. return this.ProjectController.projectListPage(this.req, this.res)
  514. })
  515. it('should not show for non sample users', function(done) {
  516. this.user._id = '588f3ddae8ebc1bac07c9fff' // last two digits
  517. this.res.render = (pageName, opts) => {
  518. expect(opts.frontChatWidgetRoomId).to.equal(undefined)
  519. return done()
  520. }
  521. return this.ProjectController.projectListPage(this.req, this.res)
  522. })
  523. })
  524. describe('with overleaf-integration-web-module hook', function() {
  525. beforeEach(function() {
  526. this.Features.hasFeature = sinon
  527. .stub()
  528. .withArgs('overleaf-integration')
  529. .returns(true)
  530. this.V1Response = {
  531. projects: [
  532. {
  533. id: '123mockV1Id',
  534. title: 'mock title',
  535. updated_at: 1509616411,
  536. removed: false,
  537. archived: false
  538. },
  539. {
  540. id: '456mockV1Id',
  541. title: 'mock title 2',
  542. updated_at: 1509616411,
  543. removed: true,
  544. archived: false
  545. }
  546. ],
  547. tags: [{ name: 'mock tag', project_ids: ['123mockV1Id'] }]
  548. }
  549. return this.Modules.hooks.fire
  550. .withArgs('findAllV1Projects', this.user._id)
  551. .yields(null, [this.V1Response])
  552. }) // Need to wrap response in array, as multiple hooks could fire
  553. it('should include V1 projects', function(done) {
  554. this.res.render = (pageName, opts) => {
  555. opts.projects.length.should.equal(
  556. this.projects.length +
  557. this.collabertions.length +
  558. this.readOnly.length +
  559. this.tokenReadAndWrite.length +
  560. this.tokenReadOnly.length +
  561. this.V1Response.projects.length
  562. )
  563. opts.projects.forEach(p => {
  564. // Check properties correctly mapped from V1
  565. expect(p).to.have.property('id')
  566. expect(p).to.have.property('name')
  567. expect(p).to.have.property('lastUpdated')
  568. expect(p).to.have.property('accessLevel')
  569. return expect(p).to.have.property('archived')
  570. })
  571. return done()
  572. }
  573. return this.ProjectController.projectListPage(this.req, this.res)
  574. })
  575. it('should include V1 tags', function(done) {
  576. this.res.render = (pageName, opts) => {
  577. opts.tags.length.should.equal(
  578. this.tags.length + this.V1Response.tags.length
  579. )
  580. opts.tags.forEach(t => {
  581. expect(t).to.have.property('name')
  582. return expect(t).to.have.property('project_ids')
  583. })
  584. return done()
  585. }
  586. return this.ProjectController.projectListPage(this.req, this.res)
  587. })
  588. it('should have isShowingV1Projects flag', function(done) {
  589. this.res.render = (pageName, opts) => {
  590. opts.isShowingV1Projects.should.equal(true)
  591. return done()
  592. }
  593. return this.ProjectController.projectListPage(this.req, this.res)
  594. })
  595. })
  596. })
  597. describe('projectListPage with duplicate projects', function() {
  598. beforeEach(function() {
  599. this.tags = [
  600. { name: 1, project_ids: ['1', '2', '3'] },
  601. { name: 2, project_ids: ['a', '1'] },
  602. { name: 3, project_ids: ['a', 'b', 'c', 'd'] }
  603. ]
  604. this.notifications = [
  605. { _id: '1', user_id: '2', templateKey: '3', messageOpts: '4', key: '5' }
  606. ]
  607. this.projects = [
  608. { _id: 1, lastUpdated: 1, owner_ref: 'user-1' },
  609. { _id: 2, lastUpdated: 2, owner_ref: 'user-2' }
  610. ]
  611. this.collabertions = [{ _id: 5, lastUpdated: 5, owner_ref: 'user-1' }]
  612. this.readOnly = [{ _id: 3, lastUpdated: 3, owner_ref: 'user-1' }]
  613. this.tokenReadAndWrite = [{ _id: 6, lastUpdated: 5, owner_ref: 'user-4' }]
  614. this.tokenReadOnly = [
  615. { _id: 6, lastUpdated: 5, owner_ref: 'user-4' }, // Also in tokenReadAndWrite
  616. { _id: 7, lastUpdated: 4, owner_ref: 'user-5' }
  617. ]
  618. this.allProjects = {
  619. owned: this.projects,
  620. readAndWrite: this.collabertions,
  621. readOnly: this.readOnly,
  622. tokenReadAndWrite: this.tokenReadAndWrite,
  623. tokenReadOnly: this.tokenReadOnly
  624. }
  625. this.users = {
  626. 'user-1': {
  627. first_name: 'James'
  628. },
  629. 'user-2': {
  630. first_name: 'Henry'
  631. }
  632. }
  633. this.users[this.user._id] = this.user // Owner
  634. this.UserModel.findById = (id, fields, callback) => {
  635. return callback(null, this.users[id])
  636. }
  637. this.LimitationsManager.hasPaidSubscription.callsArgWith(1, null, false)
  638. this.TagsHandler.getAllTags.callsArgWith(1, null, this.tags)
  639. this.NotificationsHandler.getUserNotifications = sinon
  640. .stub()
  641. .callsArgWith(1, null, this.notifications, {})
  642. this.ProjectGetter.findAllUsersProjects.callsArgWith(
  643. 2,
  644. null,
  645. this.allProjects
  646. )
  647. return this.Modules.hooks.fire
  648. .withArgs('findAllV1Projects', this.user._id)
  649. .yields(undefined)
  650. }) // Without integration module hook, cb returns undefined
  651. it('should render the project/list page', function(done) {
  652. this.res.render = (pageName, opts) => {
  653. pageName.should.equal('project/list')
  654. return done()
  655. }
  656. return this.ProjectController.projectListPage(this.req, this.res)
  657. })
  658. it('should omit one of the projects', function(done) {
  659. this.res.render = (pageName, opts) => {
  660. opts.projects.length.should.equal(
  661. this.projects.length +
  662. this.collabertions.length +
  663. this.readOnly.length +
  664. this.tokenReadAndWrite.length +
  665. this.tokenReadOnly.length -
  666. 1
  667. )
  668. return done()
  669. }
  670. return this.ProjectController.projectListPage(this.req, this.res)
  671. })
  672. })
  673. describe('renameProject', function() {
  674. beforeEach(function() {
  675. this.newProjectName = 'my supper great new project'
  676. return (this.req.body.newProjectName = this.newProjectName)
  677. })
  678. it('should call the editor controller', function(done) {
  679. this.EditorController.renameProject.callsArgWith(2)
  680. this.res.sendStatus = code => {
  681. code.should.equal(200)
  682. this.EditorController.renameProject
  683. .calledWith(this.project_id, this.newProjectName)
  684. .should.equal(true)
  685. return done()
  686. }
  687. return this.ProjectController.renameProject(this.req, this.res)
  688. })
  689. it('should send an error to next() if there is a problem', function(done) {
  690. let error
  691. this.EditorController.renameProject.callsArgWith(
  692. 2,
  693. (error = new Error('problem'))
  694. )
  695. const next = e => {
  696. e.should.equal(error)
  697. return done()
  698. }
  699. return this.ProjectController.renameProject(this.req, this.res, next)
  700. })
  701. })
  702. describe('loadEditor', function() {
  703. beforeEach(function() {
  704. this.settings.editorIsOpen = true
  705. this.project = {
  706. name: 'my proj',
  707. _id: '213123kjlkj',
  708. owner_ref: '59fc84d5fbea77482d436e1b'
  709. }
  710. this.brandedProject = {
  711. name: 'my branded proj',
  712. _id: '3252332',
  713. owner_ref: '59fc84d5fbea77482d436e1b',
  714. brandVariationId: '12'
  715. }
  716. this.user = {
  717. _id: '588f3ddae8ebc1bac07c9fa4',
  718. ace: {
  719. fontSize: 'massive',
  720. theme: 'sexy'
  721. },
  722. email: 'bob@bob.com'
  723. }
  724. this.ProjectGetter.getProject.callsArgWith(2, null, this.project)
  725. this.UserModel.findById.callsArgWith(1, null, this.user)
  726. this.SubscriptionLocator.getUsersSubscription.callsArgWith(1, null, {})
  727. this.AuthorizationManager.getPrivilegeLevelForProject.callsArgWith(
  728. 3,
  729. null,
  730. 'owner'
  731. )
  732. this.ProjectDeleter.unmarkAsDeletedByExternalSource = sinon.stub()
  733. this.InactiveProjectManager.reactivateProjectIfRequired.callsArgWith(1)
  734. this.AnalyticsManager.getLastOccurrence.yields(null, { mock: 'event' })
  735. return this.ProjectUpdateHandler.markAsOpened.callsArgWith(1)
  736. })
  737. it('should render the project/editor page', function(done) {
  738. this.res.render = (pageName, opts) => {
  739. pageName.should.equal('project/editor')
  740. return done()
  741. }
  742. return this.ProjectController.loadEditor(this.req, this.res)
  743. })
  744. it('should add user', function(done) {
  745. this.res.render = (pageName, opts) => {
  746. opts.user.email.should.equal(this.user.email)
  747. return done()
  748. }
  749. return this.ProjectController.loadEditor(this.req, this.res)
  750. })
  751. it('should add on userSettings', function(done) {
  752. this.res.render = (pageName, opts) => {
  753. opts.userSettings.fontSize.should.equal(this.user.ace.fontSize)
  754. opts.userSettings.editorTheme.should.equal(this.user.ace.theme)
  755. return done()
  756. }
  757. return this.ProjectController.loadEditor(this.req, this.res)
  758. })
  759. it('should render the closed page if the editor is closed', function(done) {
  760. this.settings.editorIsOpen = false
  761. this.res.render = (pageName, opts) => {
  762. pageName.should.equal('general/closed')
  763. return done()
  764. }
  765. return this.ProjectController.loadEditor(this.req, this.res)
  766. })
  767. it('should not render the page if the project can not be accessed', function(done) {
  768. this.AuthorizationManager.getPrivilegeLevelForProject = sinon
  769. .stub()
  770. .callsArgWith(3, null, null)
  771. this.res.sendStatus = (resCode, opts) => {
  772. resCode.should.equal(401)
  773. return done()
  774. }
  775. return this.ProjectController.loadEditor(this.req, this.res)
  776. })
  777. it('should reactivateProjectIfRequired', function(done) {
  778. this.res.render = (pageName, opts) => {
  779. this.InactiveProjectManager.reactivateProjectIfRequired
  780. .calledWith(this.project_id)
  781. .should.equal(true)
  782. return done()
  783. }
  784. return this.ProjectController.loadEditor(this.req, this.res)
  785. })
  786. it('should mark project as opened', function(done) {
  787. this.res.render = (pageName, opts) => {
  788. this.ProjectUpdateHandler.markAsOpened
  789. .calledWith(this.project_id)
  790. .should.equal(true)
  791. return done()
  792. }
  793. return this.ProjectController.loadEditor(this.req, this.res)
  794. })
  795. it('should call the brand variations handler for branded projects', function(done) {
  796. this.ProjectGetter.getProject.callsArgWith(2, null, this.brandedProject)
  797. this.res.render = (pageName, opts) => {
  798. this.BrandVariationsHandler.getBrandVariationById
  799. .calledWith()
  800. .should.equal(true)
  801. return done()
  802. }
  803. return this.ProjectController.loadEditor(this.req, this.res)
  804. })
  805. it('should not call the brand variations handler for unbranded projects', function(done) {
  806. this.res.render = (pageName, opts) => {
  807. this.BrandVariationsHandler.getBrandVariationById.called.should.equal(
  808. false
  809. )
  810. return done()
  811. }
  812. return this.ProjectController.loadEditor(this.req, this.res)
  813. })
  814. it('should expose the brand variation details as locals for branded projects', function(done) {
  815. this.ProjectGetter.getProject.callsArgWith(2, null, this.brandedProject)
  816. this.res.render = (pageName, opts) => {
  817. opts.brandVariation.should.deep.equal(this.brandVariationDetails)
  818. return done()
  819. }
  820. return this.ProjectController.loadEditor(this.req, this.res)
  821. })
  822. })
  823. describe('userProjectsJson', function() {
  824. beforeEach(function(done) {
  825. const projects = [
  826. {
  827. archived: true,
  828. trashed: true,
  829. id: 'a',
  830. name: 'A',
  831. accessLevel: 'a',
  832. somethingElse: 1
  833. },
  834. {
  835. archived: false,
  836. id: 'b',
  837. name: 'B',
  838. accessLevel: 'b',
  839. somethingElse: 1
  840. },
  841. {
  842. archived: false,
  843. trashed: true,
  844. id: 'c',
  845. name: 'C',
  846. accessLevel: 'c',
  847. somethingElse: 1
  848. },
  849. {
  850. archived: false,
  851. trashed: false,
  852. id: 'd',
  853. name: 'D',
  854. accessLevel: 'd',
  855. somethingElse: 1
  856. }
  857. ]
  858. this.ProjectHelper.isArchivedOrTrashed
  859. .withArgs(projects[0], this.user._id)
  860. .returns(true)
  861. this.ProjectHelper.isArchivedOrTrashed
  862. .withArgs(projects[1], this.user._id)
  863. .returns(false)
  864. this.ProjectHelper.isArchivedOrTrashed
  865. .withArgs(projects[2], this.user._id)
  866. .returns(true)
  867. this.ProjectHelper.isArchivedOrTrashed
  868. .withArgs(projects[3], this.user._id)
  869. .returns(false)
  870. this.ProjectGetter.findAllUsersProjects = sinon
  871. .stub()
  872. .callsArgWith(2, null, [])
  873. this.ProjectController._buildProjectList = sinon.stub().returns(projects)
  874. this.AuthenticationController.getLoggedInUserId = sinon
  875. .stub()
  876. .returns(this.user._id)
  877. return done()
  878. })
  879. it('should produce a list of projects', function(done) {
  880. this.res.json = data => {
  881. expect(data).to.deep.equal({
  882. projects: [
  883. { _id: 'b', name: 'B', accessLevel: 'b' },
  884. { _id: 'd', name: 'D', accessLevel: 'd' }
  885. ]
  886. })
  887. return done()
  888. }
  889. return this.ProjectController.userProjectsJson(
  890. this.req,
  891. this.res,
  892. this.next
  893. )
  894. })
  895. })
  896. describe('projectEntitiesJson', function() {
  897. beforeEach(function() {
  898. this.AuthenticationController.getLoggedInUserId = sinon
  899. .stub()
  900. .returns('abc')
  901. this.req.params = { Project_id: 'abcd' }
  902. this.project = { _id: 'abcd' }
  903. this.docs = [
  904. { path: '/things/b.txt', doc: true },
  905. { path: '/main.tex', doc: true }
  906. ]
  907. this.files = [{ path: '/things/a.txt' }]
  908. this.ProjectGetter.getProject = sinon
  909. .stub()
  910. .callsArgWith(1, null, this.project)
  911. return (this.ProjectEntityHandler.getAllEntitiesFromProject = sinon
  912. .stub()
  913. .callsArgWith(1, null, this.docs, this.files))
  914. })
  915. it('should produce a list of entities', function(done) {
  916. this.res.json = data => {
  917. expect(data).to.deep.equal({
  918. project_id: 'abcd',
  919. entities: [
  920. { path: '/main.tex', type: 'doc' },
  921. { path: '/things/a.txt', type: 'file' },
  922. { path: '/things/b.txt', type: 'doc' }
  923. ]
  924. })
  925. expect(this.ProjectGetter.getProject.callCount).to.equal(1)
  926. expect(
  927. this.ProjectEntityHandler.getAllEntitiesFromProject.callCount
  928. ).to.equal(1)
  929. return done()
  930. }
  931. return this.ProjectController.projectEntitiesJson(
  932. this.req,
  933. this.res,
  934. this.next
  935. )
  936. })
  937. })
  938. describe('_isInPercentageRollout', function() {
  939. before(function() {
  940. return (this.ids = [
  941. '5a05cd7621f9fe22be131740',
  942. '5a05cd7821f9fe22be131741',
  943. '5a05cd7921f9fe22be131742',
  944. '5a05cd7a21f9fe22be131743',
  945. '5a05cd7b21f9fe22be131744',
  946. '5a05cd7c21f9fe22be131745',
  947. '5a05cd7d21f9fe22be131746',
  948. '5a05cd7e21f9fe22be131747',
  949. '5a05cd7f21f9fe22be131748',
  950. '5a05cd8021f9fe22be131749',
  951. '5a05cd8021f9fe22be13174a',
  952. '5a05cd8121f9fe22be13174b',
  953. '5a05cd8221f9fe22be13174c',
  954. '5a05cd8221f9fe22be13174d',
  955. '5a05cd8321f9fe22be13174e',
  956. '5a05cd8321f9fe22be13174f',
  957. '5a05cd8421f9fe22be131750',
  958. '5a05cd8421f9fe22be131751',
  959. '5a05cd8421f9fe22be131752',
  960. '5a05cd8521f9fe22be131753'
  961. ])
  962. })
  963. it('should produce the expected results', function() {
  964. expect(
  965. this.ids.map(i => {
  966. return this.ProjectController._isInPercentageRollout('abcd', i, 50)
  967. })
  968. ).to.deep.equal([
  969. false,
  970. false,
  971. false,
  972. false,
  973. false,
  974. false,
  975. true,
  976. false,
  977. true,
  978. true,
  979. true,
  980. true,
  981. true,
  982. true,
  983. false,
  984. false,
  985. false,
  986. true,
  987. false,
  988. true
  989. ])
  990. return expect(
  991. this.ids.map(i => {
  992. return this.ProjectController._isInPercentageRollout('efgh', i, 50)
  993. })
  994. ).to.deep.equal([
  995. false,
  996. false,
  997. false,
  998. false,
  999. true,
  1000. false,
  1001. false,
  1002. true,
  1003. false,
  1004. false,
  1005. true,
  1006. true,
  1007. true,
  1008. false,
  1009. true,
  1010. false,
  1011. true,
  1012. true,
  1013. false,
  1014. false
  1015. ])
  1016. })
  1017. })
  1018. })