ProjectControllerTests.js 39 KB

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