ProjectControllerTests.js 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197
  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. '../../models/Project': {}
  173. }
  174. })
  175. this.projectName = '£12321jkj9ujkljds'
  176. this.req = {
  177. params: {
  178. Project_id: this.project_id
  179. },
  180. headers: {},
  181. connection: {
  182. remoteAddress: '192.170.18.1'
  183. },
  184. session: {
  185. user: this.user
  186. },
  187. body: {
  188. projectName: this.projectName
  189. },
  190. i18n: {
  191. translate() {}
  192. },
  193. ip: '192.170.18.1'
  194. }
  195. return (this.res = {
  196. locals: {
  197. jsPath: 'js path here'
  198. },
  199. setTimeout: sinon.stub()
  200. })
  201. })
  202. describe('updateProjectSettings', function() {
  203. it('should update the name', function(done) {
  204. this.EditorController.renameProject = sinon.stub().callsArg(2)
  205. this.req.body = { name: (this.name = 'New name') }
  206. this.res.sendStatus = code => {
  207. this.EditorController.renameProject
  208. .calledWith(this.project_id, this.name)
  209. .should.equal(true)
  210. code.should.equal(204)
  211. return done()
  212. }
  213. return this.ProjectController.updateProjectSettings(this.req, this.res)
  214. })
  215. it('should update the compiler', function(done) {
  216. this.EditorController.setCompiler = sinon.stub().callsArg(2)
  217. this.req.body = { compiler: (this.compiler = 'pdflatex') }
  218. this.res.sendStatus = code => {
  219. this.EditorController.setCompiler
  220. .calledWith(this.project_id, this.compiler)
  221. .should.equal(true)
  222. code.should.equal(204)
  223. return done()
  224. }
  225. return this.ProjectController.updateProjectSettings(this.req, this.res)
  226. })
  227. it('should update the imageName', function(done) {
  228. this.EditorController.setImageName = sinon.stub().callsArg(2)
  229. this.req.body = { imageName: (this.imageName = 'texlive-1234.5') }
  230. this.res.sendStatus = code => {
  231. this.EditorController.setImageName
  232. .calledWith(this.project_id, this.imageName)
  233. .should.equal(true)
  234. code.should.equal(204)
  235. return done()
  236. }
  237. return this.ProjectController.updateProjectSettings(this.req, this.res)
  238. })
  239. it('should update the spell check language', function(done) {
  240. this.EditorController.setSpellCheckLanguage = sinon.stub().callsArg(2)
  241. this.req.body = { spellCheckLanguage: (this.languageCode = 'fr') }
  242. this.res.sendStatus = code => {
  243. this.EditorController.setSpellCheckLanguage
  244. .calledWith(this.project_id, this.languageCode)
  245. .should.equal(true)
  246. code.should.equal(204)
  247. return done()
  248. }
  249. return this.ProjectController.updateProjectSettings(this.req, this.res)
  250. })
  251. it('should update the root doc', function(done) {
  252. this.EditorController.setRootDoc = sinon.stub().callsArg(2)
  253. this.req.body = { rootDocId: (this.rootDocId = 'root-doc-id') }
  254. this.res.sendStatus = code => {
  255. this.EditorController.setRootDoc
  256. .calledWith(this.project_id, this.rootDocId)
  257. .should.equal(true)
  258. code.should.equal(204)
  259. return done()
  260. }
  261. return this.ProjectController.updateProjectSettings(this.req, this.res)
  262. })
  263. })
  264. describe('updateProjectAdminSettings', function() {
  265. it('should update the public access level', function(done) {
  266. this.EditorController.setPublicAccessLevel = sinon.stub().callsArg(2)
  267. this.req.body = {
  268. publicAccessLevel: (this.publicAccessLevel = 'readonly')
  269. }
  270. this.res.sendStatus = code => {
  271. this.EditorController.setPublicAccessLevel
  272. .calledWith(this.project_id, this.publicAccessLevel)
  273. .should.equal(true)
  274. code.should.equal(204)
  275. return done()
  276. }
  277. return this.ProjectController.updateProjectAdminSettings(
  278. this.req,
  279. this.res
  280. )
  281. })
  282. })
  283. describe('deleteProject', function() {
  284. it('should tell the project deleter to archive when forever=false', function(done) {
  285. this.res.sendStatus = code => {
  286. this.ProjectDeleter.archiveProject
  287. .calledWith(this.project_id)
  288. .should.equal(true)
  289. code.should.equal(200)
  290. return done()
  291. }
  292. return this.ProjectController.deleteProject(this.req, this.res)
  293. })
  294. it('should tell the project deleter to delete when forever=true', function(done) {
  295. this.req.query = { forever: 'true' }
  296. this.res.sendStatus = code => {
  297. this.ProjectDeleter.deleteProject
  298. .calledWith(this.project_id, {
  299. deleterUser: this.user,
  300. ipAddress: this.req.ip
  301. })
  302. .should.equal(true)
  303. code.should.equal(200)
  304. return done()
  305. }
  306. return this.ProjectController.deleteProject(this.req, this.res)
  307. })
  308. })
  309. describe('restoreProject', function() {
  310. it('should tell the project deleter', function(done) {
  311. this.res.sendStatus = code => {
  312. this.ProjectDeleter.restoreProject
  313. .calledWith(this.project_id)
  314. .should.equal(true)
  315. code.should.equal(200)
  316. return done()
  317. }
  318. return this.ProjectController.restoreProject(this.req, this.res)
  319. })
  320. })
  321. describe('cloneProject', function() {
  322. it('should call the project duplicator', function(done) {
  323. this.res.send = json => {
  324. this.ProjectDuplicator.duplicate
  325. .calledWith(this.user, this.project_id, this.projectName)
  326. .should.equal(true)
  327. json.project_id.should.equal(this.project_id)
  328. return done()
  329. }
  330. return this.ProjectController.cloneProject(this.req, this.res)
  331. })
  332. })
  333. describe('newProject', function() {
  334. it('should call the projectCreationHandler with createExampleProject', function(done) {
  335. this.req.body.template = 'example'
  336. this.res.send = json => {
  337. this.ProjectCreationHandler.createExampleProject
  338. .calledWith(this.user._id, this.projectName)
  339. .should.equal(true)
  340. this.ProjectCreationHandler.createBasicProject.called.should.equal(
  341. false
  342. )
  343. return done()
  344. }
  345. return this.ProjectController.newProject(this.req, this.res)
  346. })
  347. it('should call the projectCreationHandler with createBasicProject', function(done) {
  348. this.req.body.template = 'basic'
  349. this.res.send = json => {
  350. this.ProjectCreationHandler.createExampleProject.called.should.equal(
  351. false
  352. )
  353. this.ProjectCreationHandler.createBasicProject
  354. .calledWith(this.user._id, this.projectName)
  355. .should.equal(true)
  356. return done()
  357. }
  358. return this.ProjectController.newProject(this.req, this.res)
  359. })
  360. })
  361. describe('projectListPage', function() {
  362. beforeEach(function() {
  363. this.tags = [
  364. { name: 1, project_ids: ['1', '2', '3'] },
  365. { name: 2, project_ids: ['a', '1'] },
  366. { name: 3, project_ids: ['a', 'b', 'c', 'd'] }
  367. ]
  368. this.notifications = [
  369. { _id: '1', user_id: '2', templateKey: '3', messageOpts: '4', key: '5' }
  370. ]
  371. this.projects = [
  372. { _id: 1, lastUpdated: 1, owner_ref: 'user-1' },
  373. { _id: 2, lastUpdated: 2, owner_ref: 'user-2', lastUpdatedBy: 'user-1' }
  374. ]
  375. this.collabertions = [{ _id: 5, lastUpdated: 5, owner_ref: 'user-1' }]
  376. this.readOnly = [{ _id: 3, lastUpdated: 3, owner_ref: 'user-1' }]
  377. this.tokenReadAndWrite = [{ _id: 6, lastUpdated: 5, owner_ref: 'user-4' }]
  378. this.tokenReadOnly = [{ _id: 7, lastUpdated: 4, owner_ref: 'user-5' }]
  379. this.allProjects = {
  380. owned: this.projects,
  381. readAndWrite: this.collabertions,
  382. readOnly: this.readOnly,
  383. tokenReadAndWrite: this.tokenReadAndWrite,
  384. tokenReadOnly: this.tokenReadOnly
  385. }
  386. this.users = {
  387. 'user-1': {
  388. first_name: 'James'
  389. },
  390. 'user-2': {
  391. first_name: 'Henry'
  392. }
  393. }
  394. this.users[this.user._id] = this.user // Owner
  395. this.UserModel.findById = (id, fields, callback) => {
  396. return callback(null, this.users[id])
  397. }
  398. this.UserGetter.getUserOrUserStubById = (id, fields, callback) => {
  399. return callback(null, this.users[id])
  400. }
  401. this.LimitationsManager.hasPaidSubscription.callsArgWith(1, null, false)
  402. this.TagsHandler.getAllTags.callsArgWith(1, null, this.tags)
  403. this.NotificationsHandler.getUserNotifications = sinon
  404. .stub()
  405. .callsArgWith(1, null, this.notifications, {})
  406. this.ProjectGetter.findAllUsersProjects.callsArgWith(
  407. 2,
  408. null,
  409. this.allProjects
  410. )
  411. return this.Modules.hooks.fire
  412. .withArgs('findAllV1Projects', this.user._id)
  413. .yields(undefined)
  414. }) // Without integration module hook, cb returns undefined
  415. it('should render the project/list page', function(done) {
  416. this.res.render = (pageName, opts) => {
  417. pageName.should.equal('project/list')
  418. return done()
  419. }
  420. return this.ProjectController.projectListPage(this.req, this.res)
  421. })
  422. it('should send the tags', function(done) {
  423. this.res.render = (pageName, opts) => {
  424. opts.tags.length.should.equal(this.tags.length)
  425. return done()
  426. }
  427. return this.ProjectController.projectListPage(this.req, this.res)
  428. })
  429. it('should create trigger ip matcher notifications', function(done) {
  430. this.settings.overleaf = true
  431. this.res.render = (pageName, opts) => {
  432. this.NotificationBuilder.ipMatcherAffiliation.called.should.equal(true)
  433. return done()
  434. }
  435. return this.ProjectController.projectListPage(this.req, this.res)
  436. })
  437. it('should send the projects', function(done) {
  438. this.res.render = (pageName, opts) => {
  439. opts.projects.length.should.equal(
  440. this.projects.length +
  441. this.collabertions.length +
  442. this.readOnly.length +
  443. this.tokenReadAndWrite.length +
  444. this.tokenReadOnly.length
  445. )
  446. return done()
  447. }
  448. return this.ProjectController.projectListPage(this.req, this.res)
  449. })
  450. it('should send the user', function(done) {
  451. this.res.render = (pageName, opts) => {
  452. opts.user.should.deep.equal(this.user)
  453. return done()
  454. }
  455. return this.ProjectController.projectListPage(this.req, this.res)
  456. })
  457. it('should inject the users', function(done) {
  458. this.res.render = (pageName, opts) => {
  459. opts.projects[0].owner.should.equal(
  460. this.users[this.projects[0].owner_ref]
  461. )
  462. opts.projects[1].owner.should.equal(
  463. this.users[this.projects[1].owner_ref]
  464. )
  465. opts.projects[1].lastUpdatedBy.should.equal(
  466. this.users[this.projects[1].lastUpdatedBy]
  467. )
  468. return done()
  469. }
  470. return this.ProjectController.projectListPage(this.req, this.res)
  471. })
  472. it('should send hasSubscription == false when no subscription', function(done) {
  473. this.res.render = (pageName, opts) => {
  474. opts.hasSubscription.should.equal(false)
  475. return done()
  476. }
  477. return this.ProjectController.projectListPage(this.req, this.res)
  478. })
  479. it('should send hasSubscription == true when there is a subscription', function(done) {
  480. this.LimitationsManager.hasPaidSubscription = sinon
  481. .stub()
  482. .callsArgWith(1, null, true)
  483. this.res.render = (pageName, opts) => {
  484. opts.hasSubscription.should.equal(true)
  485. return done()
  486. }
  487. return this.ProjectController.projectListPage(this.req, this.res)
  488. })
  489. describe('front widget', function(done) {
  490. beforeEach(function() {
  491. return (this.settings.overleaf = {
  492. front_chat_widget_room_id: 'chat-room-id'
  493. })
  494. })
  495. it('should show for paid users', function(done) {
  496. this.user.features.github = true
  497. this.user.features.dropbox = true
  498. this.res.render = (pageName, opts) => {
  499. opts.frontChatWidgetRoomId.should.equal(
  500. this.settings.overleaf.front_chat_widget_room_id
  501. )
  502. return done()
  503. }
  504. return this.ProjectController.projectListPage(this.req, this.res)
  505. })
  506. it('should show for sample users', function(done) {
  507. this.user._id = '588f3ddae8ebc1bac07c9f00' // last two digits
  508. this.res.render = (pageName, opts) => {
  509. opts.frontChatWidgetRoomId.should.equal(
  510. this.settings.overleaf.front_chat_widget_room_id
  511. )
  512. return done()
  513. }
  514. return this.ProjectController.projectListPage(this.req, this.res)
  515. })
  516. it('should not show for non sample users', function(done) {
  517. this.user._id = '588f3ddae8ebc1bac07c9fff' // last two digits
  518. this.res.render = (pageName, opts) => {
  519. expect(opts.frontChatWidgetRoomId).to.equal(undefined)
  520. return done()
  521. }
  522. return this.ProjectController.projectListPage(this.req, this.res)
  523. })
  524. })
  525. describe('with overleaf-integration-web-module hook', function() {
  526. beforeEach(function() {
  527. this.Features.hasFeature = sinon
  528. .stub()
  529. .withArgs('overleaf-integration')
  530. .returns(true)
  531. this.V1Response = {
  532. projects: [
  533. {
  534. id: '123mockV1Id',
  535. title: 'mock title',
  536. updated_at: 1509616411,
  537. removed: false,
  538. archived: false
  539. },
  540. {
  541. id: '456mockV1Id',
  542. title: 'mock title 2',
  543. updated_at: 1509616411,
  544. removed: true,
  545. archived: false
  546. }
  547. ],
  548. tags: [{ name: 'mock tag', project_ids: ['123mockV1Id'] }]
  549. }
  550. return this.Modules.hooks.fire
  551. .withArgs('findAllV1Projects', this.user._id)
  552. .yields(null, [this.V1Response])
  553. }) // Need to wrap response in array, as multiple hooks could fire
  554. it('should include V1 projects', function(done) {
  555. this.res.render = (pageName, opts) => {
  556. opts.projects.length.should.equal(
  557. this.projects.length +
  558. this.collabertions.length +
  559. this.readOnly.length +
  560. this.tokenReadAndWrite.length +
  561. this.tokenReadOnly.length +
  562. this.V1Response.projects.length
  563. )
  564. opts.projects.forEach(p => {
  565. // Check properties correctly mapped from V1
  566. expect(p).to.have.property('id')
  567. expect(p).to.have.property('name')
  568. expect(p).to.have.property('lastUpdated')
  569. expect(p).to.have.property('accessLevel')
  570. return expect(p).to.have.property('archived')
  571. })
  572. return done()
  573. }
  574. return this.ProjectController.projectListPage(this.req, this.res)
  575. })
  576. it('should include V1 tags', function(done) {
  577. this.res.render = (pageName, opts) => {
  578. opts.tags.length.should.equal(
  579. this.tags.length + this.V1Response.tags.length
  580. )
  581. opts.tags.forEach(t => {
  582. expect(t).to.have.property('name')
  583. return expect(t).to.have.property('project_ids')
  584. })
  585. return done()
  586. }
  587. return this.ProjectController.projectListPage(this.req, this.res)
  588. })
  589. it('should have isShowingV1Projects flag', function(done) {
  590. this.res.render = (pageName, opts) => {
  591. opts.isShowingV1Projects.should.equal(true)
  592. return done()
  593. }
  594. return this.ProjectController.projectListPage(this.req, this.res)
  595. })
  596. })
  597. })
  598. describe('projectListPage with duplicate projects', function() {
  599. beforeEach(function() {
  600. this.tags = [
  601. { name: 1, project_ids: ['1', '2', '3'] },
  602. { name: 2, project_ids: ['a', '1'] },
  603. { name: 3, project_ids: ['a', 'b', 'c', 'd'] }
  604. ]
  605. this.notifications = [
  606. { _id: '1', user_id: '2', templateKey: '3', messageOpts: '4', key: '5' }
  607. ]
  608. this.projects = [
  609. { _id: 1, lastUpdated: 1, owner_ref: 'user-1' },
  610. { _id: 2, lastUpdated: 2, owner_ref: 'user-2' }
  611. ]
  612. this.collabertions = [{ _id: 5, lastUpdated: 5, owner_ref: 'user-1' }]
  613. this.readOnly = [{ _id: 3, lastUpdated: 3, owner_ref: 'user-1' }]
  614. this.tokenReadAndWrite = [{ _id: 6, lastUpdated: 5, owner_ref: 'user-4' }]
  615. this.tokenReadOnly = [
  616. { _id: 6, lastUpdated: 5, owner_ref: 'user-4' }, // Also in tokenReadAndWrite
  617. { _id: 7, lastUpdated: 4, owner_ref: 'user-5' }
  618. ]
  619. this.allProjects = {
  620. owned: this.projects,
  621. readAndWrite: this.collabertions,
  622. readOnly: this.readOnly,
  623. tokenReadAndWrite: this.tokenReadAndWrite,
  624. tokenReadOnly: this.tokenReadOnly
  625. }
  626. this.users = {
  627. 'user-1': {
  628. first_name: 'James'
  629. },
  630. 'user-2': {
  631. first_name: 'Henry'
  632. }
  633. }
  634. this.users[this.user._id] = this.user // Owner
  635. this.UserModel.findById = (id, fields, callback) => {
  636. return callback(null, this.users[id])
  637. }
  638. this.LimitationsManager.hasPaidSubscription.callsArgWith(1, null, false)
  639. this.TagsHandler.getAllTags.callsArgWith(1, null, this.tags)
  640. this.NotificationsHandler.getUserNotifications = sinon
  641. .stub()
  642. .callsArgWith(1, null, this.notifications, {})
  643. this.ProjectGetter.findAllUsersProjects.callsArgWith(
  644. 2,
  645. null,
  646. this.allProjects
  647. )
  648. return this.Modules.hooks.fire
  649. .withArgs('findAllV1Projects', this.user._id)
  650. .yields(undefined)
  651. }) // Without integration module hook, cb returns undefined
  652. it('should render the project/list page', function(done) {
  653. this.res.render = (pageName, opts) => {
  654. pageName.should.equal('project/list')
  655. return done()
  656. }
  657. return this.ProjectController.projectListPage(this.req, this.res)
  658. })
  659. it('should omit one of the projects', function(done) {
  660. this.res.render = (pageName, opts) => {
  661. opts.projects.length.should.equal(
  662. this.projects.length +
  663. this.collabertions.length +
  664. this.readOnly.length +
  665. this.tokenReadAndWrite.length +
  666. this.tokenReadOnly.length -
  667. 1
  668. )
  669. return done()
  670. }
  671. return this.ProjectController.projectListPage(this.req, this.res)
  672. })
  673. })
  674. describe('renameProject', function() {
  675. beforeEach(function() {
  676. this.newProjectName = 'my supper great new project'
  677. return (this.req.body.newProjectName = this.newProjectName)
  678. })
  679. it('should call the editor controller', function(done) {
  680. this.EditorController.renameProject.callsArgWith(2)
  681. this.res.sendStatus = code => {
  682. code.should.equal(200)
  683. this.EditorController.renameProject
  684. .calledWith(this.project_id, this.newProjectName)
  685. .should.equal(true)
  686. return done()
  687. }
  688. return this.ProjectController.renameProject(this.req, this.res)
  689. })
  690. it('should send an error to next() if there is a problem', function(done) {
  691. let error
  692. this.EditorController.renameProject.callsArgWith(
  693. 2,
  694. (error = new Error('problem'))
  695. )
  696. const next = e => {
  697. e.should.equal(error)
  698. return done()
  699. }
  700. return this.ProjectController.renameProject(this.req, this.res, next)
  701. })
  702. })
  703. describe('loadEditor', function() {
  704. beforeEach(function() {
  705. this.settings.editorIsOpen = true
  706. this.project = {
  707. name: 'my proj',
  708. _id: '213123kjlkj',
  709. owner_ref: '59fc84d5fbea77482d436e1b'
  710. }
  711. this.brandedProject = {
  712. name: 'my branded proj',
  713. _id: '3252332',
  714. owner_ref: '59fc84d5fbea77482d436e1b',
  715. brandVariationId: '12'
  716. }
  717. this.user = {
  718. _id: '588f3ddae8ebc1bac07c9fa4',
  719. ace: {
  720. fontSize: 'massive',
  721. theme: 'sexy'
  722. },
  723. email: 'bob@bob.com'
  724. }
  725. this.ProjectGetter.getProject.callsArgWith(2, null, this.project)
  726. this.UserModel.findById.callsArgWith(1, null, this.user)
  727. this.SubscriptionLocator.getUsersSubscription.callsArgWith(1, null, {})
  728. this.AuthorizationManager.getPrivilegeLevelForProject.callsArgWith(
  729. 3,
  730. null,
  731. 'owner'
  732. )
  733. this.ProjectDeleter.unmarkAsDeletedByExternalSource = sinon.stub()
  734. this.InactiveProjectManager.reactivateProjectIfRequired.callsArgWith(1)
  735. this.AnalyticsManager.getLastOccurrence.yields(null, { mock: 'event' })
  736. return this.ProjectUpdateHandler.markAsOpened.callsArgWith(1)
  737. })
  738. it('should render the project/editor page', function(done) {
  739. this.res.render = (pageName, opts) => {
  740. pageName.should.equal('project/editor')
  741. return done()
  742. }
  743. return this.ProjectController.loadEditor(this.req, this.res)
  744. })
  745. it('should add user', function(done) {
  746. this.res.render = (pageName, opts) => {
  747. opts.user.email.should.equal(this.user.email)
  748. return done()
  749. }
  750. return this.ProjectController.loadEditor(this.req, this.res)
  751. })
  752. it('should add on userSettings', function(done) {
  753. this.res.render = (pageName, opts) => {
  754. opts.userSettings.fontSize.should.equal(this.user.ace.fontSize)
  755. opts.userSettings.editorTheme.should.equal(this.user.ace.theme)
  756. return done()
  757. }
  758. return this.ProjectController.loadEditor(this.req, this.res)
  759. })
  760. it('should add isRestrictedTokenMember', function(done) {
  761. this.res.render = (pageName, opts) => {
  762. opts.isRestrictedTokenMember.should.exist
  763. opts.isRestrictedTokenMember.should.equal(false)
  764. return done()
  765. }
  766. return this.ProjectController.loadEditor(this.req, this.res)
  767. })
  768. it('should set isRestrictedTokenMember to true when the user is accessing project via read-only token', function(done) {
  769. this.CollaboratorsHandler.userIsTokenMember.callsArgWith(2, null, true)
  770. this.AuthorizationManager.getPrivilegeLevelForProject.callsArgWith(
  771. 3,
  772. null,
  773. 'readOnly'
  774. )
  775. this.res.render = (pageName, opts) => {
  776. opts.isRestrictedTokenMember.should.exist
  777. opts.isRestrictedTokenMember.should.equal(true)
  778. return done()
  779. }
  780. return this.ProjectController.loadEditor(this.req, this.res)
  781. })
  782. it('should set isRestrictedTokenMember to true when anonymous read-only token access', function(done) {
  783. this.CollaboratorsHandler.userIsTokenMember.callsArgWith(2, null, null)
  784. this.AuthenticationController.isUserLoggedIn = sinon.stub().returns(false)
  785. this.AuthorizationManager.getPrivilegeLevelForProject.callsArgWith(
  786. 3,
  787. null,
  788. 'readOnly'
  789. )
  790. this.res.render = (pageName, opts) => {
  791. opts.isRestrictedTokenMember.should.exist
  792. opts.isRestrictedTokenMember.should.equal(true)
  793. return done()
  794. }
  795. return this.ProjectController.loadEditor(this.req, this.res)
  796. })
  797. it('should render the closed page if the editor is closed', function(done) {
  798. this.settings.editorIsOpen = false
  799. this.res.render = (pageName, opts) => {
  800. pageName.should.equal('general/closed')
  801. return done()
  802. }
  803. return this.ProjectController.loadEditor(this.req, this.res)
  804. })
  805. it('should not render the page if the project can not be accessed', function(done) {
  806. this.AuthorizationManager.getPrivilegeLevelForProject = sinon
  807. .stub()
  808. .callsArgWith(3, null, null)
  809. this.res.sendStatus = (resCode, opts) => {
  810. resCode.should.equal(401)
  811. return done()
  812. }
  813. return this.ProjectController.loadEditor(this.req, this.res)
  814. })
  815. it('should reactivateProjectIfRequired', function(done) {
  816. this.res.render = (pageName, opts) => {
  817. this.InactiveProjectManager.reactivateProjectIfRequired
  818. .calledWith(this.project_id)
  819. .should.equal(true)
  820. return done()
  821. }
  822. return this.ProjectController.loadEditor(this.req, this.res)
  823. })
  824. it('should mark project as opened', function(done) {
  825. this.res.render = (pageName, opts) => {
  826. this.ProjectUpdateHandler.markAsOpened
  827. .calledWith(this.project_id)
  828. .should.equal(true)
  829. return done()
  830. }
  831. return this.ProjectController.loadEditor(this.req, this.res)
  832. })
  833. it('should call the brand variations handler for branded projects', function(done) {
  834. this.ProjectGetter.getProject.callsArgWith(2, null, this.brandedProject)
  835. this.res.render = (pageName, opts) => {
  836. this.BrandVariationsHandler.getBrandVariationById
  837. .calledWith()
  838. .should.equal(true)
  839. return done()
  840. }
  841. return this.ProjectController.loadEditor(this.req, this.res)
  842. })
  843. it('should not call the brand variations handler for unbranded projects', function(done) {
  844. this.res.render = (pageName, opts) => {
  845. this.BrandVariationsHandler.getBrandVariationById.called.should.equal(
  846. false
  847. )
  848. return done()
  849. }
  850. return this.ProjectController.loadEditor(this.req, this.res)
  851. })
  852. it('should expose the brand variation details as locals for branded projects', function(done) {
  853. this.ProjectGetter.getProject.callsArgWith(2, null, this.brandedProject)
  854. this.res.render = (pageName, opts) => {
  855. opts.brandVariation.should.deep.equal(this.brandVariationDetails)
  856. return done()
  857. }
  858. return this.ProjectController.loadEditor(this.req, this.res)
  859. })
  860. })
  861. describe('userProjectsJson', function() {
  862. beforeEach(function(done) {
  863. const projects = [
  864. {
  865. archived: true,
  866. trashed: true,
  867. id: 'a',
  868. name: 'A',
  869. accessLevel: 'a',
  870. somethingElse: 1
  871. },
  872. {
  873. archived: false,
  874. id: 'b',
  875. name: 'B',
  876. accessLevel: 'b',
  877. somethingElse: 1
  878. },
  879. {
  880. archived: false,
  881. trashed: true,
  882. id: 'c',
  883. name: 'C',
  884. accessLevel: 'c',
  885. somethingElse: 1
  886. },
  887. {
  888. archived: false,
  889. trashed: false,
  890. id: 'd',
  891. name: 'D',
  892. accessLevel: 'd',
  893. somethingElse: 1
  894. }
  895. ]
  896. this.ProjectHelper.isArchivedOrTrashed
  897. .withArgs(projects[0], this.user._id)
  898. .returns(true)
  899. this.ProjectHelper.isArchivedOrTrashed
  900. .withArgs(projects[1], this.user._id)
  901. .returns(false)
  902. this.ProjectHelper.isArchivedOrTrashed
  903. .withArgs(projects[2], this.user._id)
  904. .returns(true)
  905. this.ProjectHelper.isArchivedOrTrashed
  906. .withArgs(projects[3], this.user._id)
  907. .returns(false)
  908. this.ProjectGetter.findAllUsersProjects = sinon
  909. .stub()
  910. .callsArgWith(2, null, [])
  911. this.ProjectController._buildProjectList = sinon.stub().returns(projects)
  912. this.AuthenticationController.getLoggedInUserId = sinon
  913. .stub()
  914. .returns(this.user._id)
  915. return done()
  916. })
  917. it('should produce a list of projects', function(done) {
  918. this.res.json = data => {
  919. expect(data).to.deep.equal({
  920. projects: [
  921. { _id: 'b', name: 'B', accessLevel: 'b' },
  922. { _id: 'd', name: 'D', accessLevel: 'd' }
  923. ]
  924. })
  925. return done()
  926. }
  927. return this.ProjectController.userProjectsJson(
  928. this.req,
  929. this.res,
  930. this.next
  931. )
  932. })
  933. })
  934. describe('projectEntitiesJson', function() {
  935. beforeEach(function() {
  936. this.AuthenticationController.getLoggedInUserId = sinon
  937. .stub()
  938. .returns('abc')
  939. this.req.params = { Project_id: 'abcd' }
  940. this.project = { _id: 'abcd' }
  941. this.docs = [
  942. { path: '/things/b.txt', doc: true },
  943. { path: '/main.tex', doc: true }
  944. ]
  945. this.files = [{ path: '/things/a.txt' }]
  946. this.ProjectGetter.getProject = sinon
  947. .stub()
  948. .callsArgWith(1, null, this.project)
  949. return (this.ProjectEntityHandler.getAllEntitiesFromProject = sinon
  950. .stub()
  951. .callsArgWith(1, null, this.docs, this.files))
  952. })
  953. it('should produce a list of entities', function(done) {
  954. this.res.json = data => {
  955. expect(data).to.deep.equal({
  956. project_id: 'abcd',
  957. entities: [
  958. { path: '/main.tex', type: 'doc' },
  959. { path: '/things/a.txt', type: 'file' },
  960. { path: '/things/b.txt', type: 'doc' }
  961. ]
  962. })
  963. expect(this.ProjectGetter.getProject.callCount).to.equal(1)
  964. expect(
  965. this.ProjectEntityHandler.getAllEntitiesFromProject.callCount
  966. ).to.equal(1)
  967. return done()
  968. }
  969. return this.ProjectController.projectEntitiesJson(
  970. this.req,
  971. this.res,
  972. this.next
  973. )
  974. })
  975. })
  976. describe('_buildProjectViewModel', function() {
  977. beforeEach(function() {
  978. this.ProjectHelper.isArchived.returns(false)
  979. this.project = {
  980. _id: 'abcd',
  981. name: 'netsenits',
  982. lastUpdated: 1,
  983. lastUpdatedBy: 2,
  984. publicAccesLevel: 'private',
  985. archived: false,
  986. owner_ref: 'defg',
  987. tokens: {
  988. readAndWrite: '1abcd',
  989. readAndWritePrefix: '1',
  990. readOnly: 'neiotsranteoia'
  991. }
  992. }
  993. })
  994. it('should produce a model of the project', function() {
  995. const result = this.ProjectController._buildProjectViewModel(
  996. this.project,
  997. 'readAndWrite',
  998. 'owner',
  999. this.user._id
  1000. )
  1001. expect(result).to.exist
  1002. expect(result).to.be.object
  1003. expect(result).to.deep.equal({
  1004. id: 'abcd',
  1005. name: 'netsenits',
  1006. lastUpdated: 1,
  1007. lastUpdatedBy: 2,
  1008. publicAccessLevel: 'private',
  1009. accessLevel: 'readAndWrite',
  1010. source: 'owner',
  1011. archived: false,
  1012. owner_ref: 'defg',
  1013. tokens: {
  1014. readAndWrite: '1abcd',
  1015. readAndWritePrefix: '1',
  1016. readOnly: 'neiotsranteoia'
  1017. },
  1018. isV1Project: false
  1019. })
  1020. })
  1021. describe('when token-read-only access', function() {
  1022. it('should redact the owner and last-updated data', function() {
  1023. const result = this.ProjectController._buildProjectViewModel(
  1024. this.project,
  1025. 'readOnly',
  1026. 'token',
  1027. this.user._id
  1028. )
  1029. expect(result).to.exist
  1030. expect(result).to.be.object
  1031. expect(result).to.deep.equal({
  1032. id: 'abcd',
  1033. name: 'netsenits',
  1034. lastUpdated: 1,
  1035. lastUpdatedBy: null,
  1036. publicAccessLevel: 'private',
  1037. accessLevel: 'readOnly',
  1038. source: 'token',
  1039. archived: false,
  1040. owner_ref: null,
  1041. tokens: {
  1042. readAndWrite: '1abcd',
  1043. readAndWritePrefix: '1',
  1044. readOnly: 'neiotsranteoia'
  1045. },
  1046. isV1Project: false
  1047. })
  1048. })
  1049. })
  1050. })
  1051. describe('_isInPercentageRollout', function() {
  1052. before(function() {
  1053. return (this.ids = [
  1054. '5a05cd7621f9fe22be131740',
  1055. '5a05cd7821f9fe22be131741',
  1056. '5a05cd7921f9fe22be131742',
  1057. '5a05cd7a21f9fe22be131743',
  1058. '5a05cd7b21f9fe22be131744',
  1059. '5a05cd7c21f9fe22be131745',
  1060. '5a05cd7d21f9fe22be131746',
  1061. '5a05cd7e21f9fe22be131747',
  1062. '5a05cd7f21f9fe22be131748',
  1063. '5a05cd8021f9fe22be131749',
  1064. '5a05cd8021f9fe22be13174a',
  1065. '5a05cd8121f9fe22be13174b',
  1066. '5a05cd8221f9fe22be13174c',
  1067. '5a05cd8221f9fe22be13174d',
  1068. '5a05cd8321f9fe22be13174e',
  1069. '5a05cd8321f9fe22be13174f',
  1070. '5a05cd8421f9fe22be131750',
  1071. '5a05cd8421f9fe22be131751',
  1072. '5a05cd8421f9fe22be131752',
  1073. '5a05cd8521f9fe22be131753'
  1074. ])
  1075. })
  1076. it('should produce the expected results', function() {
  1077. expect(
  1078. this.ids.map(i => {
  1079. return this.ProjectController._isInPercentageRollout('abcd', i, 50)
  1080. })
  1081. ).to.deep.equal([
  1082. false,
  1083. false,
  1084. false,
  1085. false,
  1086. false,
  1087. false,
  1088. true,
  1089. false,
  1090. true,
  1091. true,
  1092. true,
  1093. true,
  1094. true,
  1095. true,
  1096. false,
  1097. false,
  1098. false,
  1099. true,
  1100. false,
  1101. true
  1102. ])
  1103. return expect(
  1104. this.ids.map(i => {
  1105. return this.ProjectController._isInPercentageRollout('efgh', i, 50)
  1106. })
  1107. ).to.deep.equal([
  1108. false,
  1109. false,
  1110. false,
  1111. false,
  1112. true,
  1113. false,
  1114. false,
  1115. true,
  1116. false,
  1117. false,
  1118. true,
  1119. true,
  1120. true,
  1121. false,
  1122. true,
  1123. false,
  1124. true,
  1125. true,
  1126. false,
  1127. false
  1128. ])
  1129. })
  1130. })
  1131. })