ProjectCreationHandlerTests.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. /* eslint-disable
  2. camelcase,
  3. handle-callback-err,
  4. max-len,
  5. no-path-concat,
  6. no-return-assign,
  7. no-unused-vars,
  8. */
  9. // TODO: This file was created by bulk-decaffeinate.
  10. // Fix any style issues and re-enable lint.
  11. /*
  12. * decaffeinate suggestions:
  13. * DS102: Remove unnecessary code created because of implicit returns
  14. * DS206: Consider reworking classes to avoid initClass
  15. * DS207: Consider shorter variations of null checks
  16. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  17. */
  18. const spies = require('chai-spies')
  19. const chai = require('chai').use(spies)
  20. const sinon = require('sinon')
  21. const should = chai.should()
  22. const { expect } = chai
  23. const modulePath =
  24. '../../../../app/src/Features/Project/ProjectCreationHandler.js'
  25. const SandboxedModule = require('sandboxed-module')
  26. const Settings = require('settings-sharelatex')
  27. const Path = require('path')
  28. const _ = require('underscore')
  29. describe('ProjectCreationHandler', function() {
  30. const ownerId = '4eecb1c1bffa66588e0000a1'
  31. const projectName = 'project name goes here'
  32. const project_id = '4eecaffcbffa66588e000008'
  33. const docId = '4eecb17ebffa66588e00003f'
  34. const rootFolderId = '234adfa3r2afe'
  35. beforeEach(function() {
  36. let Folder, Project
  37. this.ProjectModel = Project = (function() {
  38. Project = class Project {
  39. static initClass() {
  40. this.prototype.save = sinon.stub().callsArg(0)
  41. this.prototype.rootFolder = [
  42. {
  43. _id: rootFolderId,
  44. docs: []
  45. }
  46. ]
  47. }
  48. constructor(options) {
  49. if (options == null) {
  50. options = {}
  51. }
  52. this._id = project_id
  53. this.owner_ref = options.owner_ref
  54. this.name = options.name
  55. this.overleaf = { history: {} }
  56. }
  57. }
  58. Project.initClass()
  59. return Project
  60. })()
  61. this.FolderModel = Folder = class Folder {
  62. constructor(options) {
  63. ;({ name: this.name } = options)
  64. }
  65. }
  66. this.ProjectEntityUpdateHandler = {
  67. addDoc: sinon.stub().callsArgWith(5, null, { _id: docId }),
  68. addFile: sinon.stub().callsArg(6),
  69. setRootDoc: sinon.stub().callsArg(2)
  70. }
  71. this.ProjectDetailsHandler = { validateProjectName: sinon.stub().yields() }
  72. this.HistoryManager = { initializeProject: sinon.stub().callsArg(0) }
  73. this.user = {
  74. first_name: 'first name here',
  75. last_name: 'last name here',
  76. ace: {
  77. spellCheckLanguage: 'de'
  78. }
  79. }
  80. this.User = { findById: sinon.stub().callsArgWith(2, null, this.user) }
  81. this.callback = sinon.stub()
  82. this.Settings = { apis: { project_history: {} } }
  83. this.AnalyticsManager = { recordEvent: sinon.stub() }
  84. return (this.handler = SandboxedModule.require(modulePath, {
  85. globals: {
  86. console: console
  87. },
  88. requires: {
  89. '../../models/User': {
  90. User: this.User
  91. },
  92. '../../models/Project': { Project: this.ProjectModel },
  93. '../../models/Folder': { Folder: this.FolderModel },
  94. '../History/HistoryManager': this.HistoryManager,
  95. './ProjectEntityUpdateHandler': this.ProjectEntityUpdateHandler,
  96. './ProjectDetailsHandler': this.ProjectDetailsHandler,
  97. 'settings-sharelatex': this.Settings,
  98. '../Analytics/AnalyticsManager': this.AnalyticsManager,
  99. 'logger-sharelatex': { log() {} },
  100. 'metrics-sharelatex': {
  101. inc() {},
  102. timeAsyncMethod() {}
  103. }
  104. }
  105. }))
  106. })
  107. describe('Creating a Blank project', function() {
  108. beforeEach(function() {
  109. this.overleaf_id = 1234
  110. this.HistoryManager.initializeProject = sinon
  111. .stub()
  112. .callsArgWith(0, null, { overleaf_id: this.overleaf_id })
  113. return (this.ProjectModel.prototype.save = sinon.stub().callsArg(0))
  114. })
  115. describe('successfully', function() {
  116. it('should save the project', function(done) {
  117. return this.handler.createBlankProject(ownerId, projectName, () => {
  118. this.ProjectModel.prototype.save.called.should.equal(true)
  119. return done()
  120. })
  121. })
  122. it('should return the project in the callback', function(done) {
  123. return this.handler.createBlankProject(ownerId, projectName, function(
  124. err,
  125. project
  126. ) {
  127. project.name.should.equal(projectName)
  128. ;(project.owner_ref + '').should.equal(ownerId)
  129. return done()
  130. })
  131. })
  132. it('should initialize the project overleaf if history id not provided', function(done) {
  133. this.handler.createBlankProject(ownerId, projectName, done)
  134. return this.HistoryManager.initializeProject
  135. .calledWith()
  136. .should.equal(true)
  137. })
  138. it('should set the overleaf id if overleaf id not provided', function(done) {
  139. return this.handler.createBlankProject(
  140. ownerId,
  141. projectName,
  142. (err, project) => {
  143. project.overleaf.history.id.should.equal(this.overleaf_id)
  144. return done()
  145. }
  146. )
  147. })
  148. it('should set the overleaf id if overleaf id provided', function(done) {
  149. const overleaf_id = 2345
  150. const attributes = {
  151. overleaf: {
  152. history: {
  153. id: overleaf_id
  154. }
  155. }
  156. }
  157. return this.handler.createBlankProject(
  158. ownerId,
  159. projectName,
  160. attributes,
  161. function(err, project) {
  162. project.overleaf.history.id.should.equal(overleaf_id)
  163. return done()
  164. }
  165. )
  166. })
  167. it('should set the language from the user', function(done) {
  168. return this.handler.createBlankProject(ownerId, projectName, function(
  169. err,
  170. project
  171. ) {
  172. project.spellCheckLanguage.should.equal('de')
  173. return done()
  174. })
  175. })
  176. it('should set the imageName to currentImageName if set and no imageName attribute', function(done) {
  177. this.Settings.currentImageName = 'mock-image-name'
  178. return this.handler.createBlankProject(
  179. ownerId,
  180. projectName,
  181. (err, project) => {
  182. project.imageName.should.equal(this.Settings.currentImageName)
  183. return done()
  184. }
  185. )
  186. })
  187. it('should not set the imageName if no currentImageName', function(done) {
  188. this.Settings.currentImageName = null
  189. return this.handler.createBlankProject(
  190. ownerId,
  191. projectName,
  192. (err, project) => {
  193. expect(project.imageName).to.not.exist
  194. return done()
  195. }
  196. )
  197. })
  198. it('should set the imageName to the attribute value if set and not overwrite it with the currentImageName', function(done) {
  199. this.Settings.currentImageName = 'mock-image-name'
  200. const attributes = { imageName: 'attribute-image-name' }
  201. return this.handler.createBlankProject(
  202. ownerId,
  203. projectName,
  204. attributes,
  205. (err, project) => {
  206. project.imageName.should.equal(attributes.imageName)
  207. return done()
  208. }
  209. )
  210. })
  211. it('should not set the overleaf.history.display if not configured in settings', function(done) {
  212. this.Settings.apis.project_history.displayHistoryForNewProjects = false
  213. return this.handler.createBlankProject(
  214. ownerId,
  215. projectName,
  216. (err, project) => {
  217. expect(project.overleaf.history.display).to.not.exist
  218. return done()
  219. }
  220. )
  221. })
  222. it('should set the overleaf.history.display if configured in settings', function(done) {
  223. this.Settings.apis.project_history.displayHistoryForNewProjects = true
  224. return this.handler.createBlankProject(
  225. ownerId,
  226. projectName,
  227. (err, project) => {
  228. expect(project.overleaf.history.display).to.equal(true)
  229. return done()
  230. }
  231. )
  232. })
  233. it('should send a project-created event to analytics', function(done) {
  234. return this.handler.createBlankProject(
  235. ownerId,
  236. projectName,
  237. (err, project) => {
  238. expect(this.AnalyticsManager.recordEvent.callCount).to.equal(1)
  239. expect(
  240. this.AnalyticsManager.recordEvent.calledWith(
  241. ownerId,
  242. 'project-created'
  243. )
  244. ).to.equal(true)
  245. return done()
  246. }
  247. )
  248. })
  249. it('should send a project-created event with template information if provided', function(done) {
  250. const attributes = {
  251. fromV1TemplateId: 100
  252. }
  253. return this.handler.createBlankProject(
  254. ownerId,
  255. projectName,
  256. attributes,
  257. (err, project) => {
  258. expect(this.AnalyticsManager.recordEvent.callCount).to.equal(1)
  259. expect(
  260. this.AnalyticsManager.recordEvent.calledWith(
  261. ownerId,
  262. 'project-created',
  263. { projectId: project._id, attributes }
  264. )
  265. ).to.equal(true)
  266. return done()
  267. }
  268. )
  269. })
  270. it('should send a project-imported event when importing a project', function(done) {
  271. const attributes = {
  272. overleaf: {
  273. history: {
  274. id: 100
  275. }
  276. }
  277. }
  278. return this.handler.createBlankProject(
  279. ownerId,
  280. projectName,
  281. attributes,
  282. (err, project) => {
  283. expect(this.AnalyticsManager.recordEvent.callCount).to.equal(1)
  284. expect(
  285. this.AnalyticsManager.recordEvent.calledWith(
  286. ownerId,
  287. 'project-imported'
  288. )
  289. ).to.equal(true)
  290. return done()
  291. }
  292. )
  293. })
  294. })
  295. describe('with an error', function() {
  296. beforeEach(function() {
  297. this.ProjectModel.prototype.save = sinon
  298. .stub()
  299. .callsArgWith(0, new Error('something went wrong'))
  300. return this.handler.createBlankProject(
  301. ownerId,
  302. projectName,
  303. this.callback
  304. )
  305. })
  306. it('should return the error to the callback', function() {
  307. return should.exist(this.callback.args[0][0])
  308. })
  309. })
  310. describe('with an invalid name', function() {
  311. beforeEach(function() {
  312. this.ProjectDetailsHandler.validateProjectName = sinon
  313. .stub()
  314. .yields(new Error('bad name'))
  315. return this.handler.createBlankProject(
  316. ownerId,
  317. projectName,
  318. this.callback
  319. )
  320. })
  321. it('should return the error to the callback', function() {
  322. return should.exist(this.callback.args[0][0])
  323. })
  324. it('should not try to create the project', function() {
  325. return this.ProjectModel.prototype.save.called.should.equal(false)
  326. })
  327. })
  328. })
  329. describe('Creating a basic project', function() {
  330. beforeEach(function() {
  331. this.project = new this.ProjectModel()
  332. this.handler._buildTemplate = function(
  333. template_name,
  334. user,
  335. project_name,
  336. callback
  337. ) {
  338. if (template_name === 'mainbasic.tex') {
  339. return callback(null, ['mainbasic.tex', 'lines'])
  340. }
  341. throw new Error(`unknown template: ${template_name}`)
  342. }
  343. sinon.spy(this.handler, '_buildTemplate')
  344. this.handler.createBlankProject = sinon
  345. .stub()
  346. .callsArgWith(2, null, this.project)
  347. this.handler._createRootDoc = sinon
  348. .stub()
  349. .callsArgWith(3, null, this.project)
  350. return this.handler.createBasicProject(
  351. ownerId,
  352. projectName,
  353. this.callback
  354. )
  355. })
  356. it('should create a blank project first', function() {
  357. return this.handler.createBlankProject
  358. .calledWith(ownerId, projectName)
  359. .should.equal(true)
  360. })
  361. it('should create the root document', function() {
  362. return this.handler._createRootDoc
  363. .calledWith(this.project, ownerId, ['mainbasic.tex', 'lines'])
  364. .should.equal(true)
  365. })
  366. it('should build the mainbasic.tex template', function() {
  367. return this.handler._buildTemplate
  368. .calledWith('mainbasic.tex', ownerId, projectName)
  369. .should.equal(true)
  370. })
  371. })
  372. describe('Creating a project from a snippet', function() {
  373. beforeEach(function() {
  374. this.project = new this.ProjectModel()
  375. this.handler.createBlankProject = sinon
  376. .stub()
  377. .callsArgWith(2, null, this.project)
  378. this.handler._createRootDoc = sinon
  379. .stub()
  380. .callsArgWith(3, null, this.project)
  381. return this.handler.createProjectFromSnippet(
  382. ownerId,
  383. projectName,
  384. ['snippet line 1', 'snippet line 2'],
  385. this.callback
  386. )
  387. })
  388. it('should create a blank project first', function() {
  389. return this.handler.createBlankProject
  390. .calledWith(ownerId, projectName)
  391. .should.equal(true)
  392. })
  393. it('should create the root document', function() {
  394. return this.handler._createRootDoc
  395. .calledWith(this.project, ownerId, ['snippet line 1', 'snippet line 2'])
  396. .should.equal(true)
  397. })
  398. })
  399. describe('Creating an example project', function() {
  400. beforeEach(function() {
  401. this.project = new this.ProjectModel()
  402. this.handler._buildTemplate = function(
  403. template_name,
  404. user,
  405. project_name,
  406. callback
  407. ) {
  408. if (template_name === 'main.tex') {
  409. return callback(null, ['main.tex', 'lines'])
  410. }
  411. if (template_name === 'references.bib') {
  412. return callback(null, ['references.bib', 'lines'])
  413. }
  414. throw new Error(`unknown template: ${template_name}`)
  415. }
  416. sinon.spy(this.handler, '_buildTemplate')
  417. this.handler.createBlankProject = sinon
  418. .stub()
  419. .callsArgWith(2, null, this.project)
  420. this.handler._createRootDoc = sinon
  421. .stub()
  422. .callsArgWith(3, null, this.project)
  423. return this.handler.createExampleProject(
  424. ownerId,
  425. projectName,
  426. this.callback
  427. )
  428. })
  429. it('should create a blank project first', function() {
  430. return this.handler.createBlankProject
  431. .calledWith(ownerId, projectName)
  432. .should.equal(true)
  433. })
  434. it('should create the root document', function() {
  435. return this.handler._createRootDoc
  436. .calledWith(this.project, ownerId, ['main.tex', 'lines'])
  437. .should.equal(true)
  438. })
  439. it('should insert references.bib', function() {
  440. return this.ProjectEntityUpdateHandler.addDoc
  441. .calledWith(
  442. project_id,
  443. rootFolderId,
  444. 'references.bib',
  445. ['references.bib', 'lines'],
  446. ownerId
  447. )
  448. .should.equal(true)
  449. })
  450. it('should insert universe.jpg', function() {
  451. return this.ProjectEntityUpdateHandler.addFile
  452. .calledWith(
  453. project_id,
  454. rootFolderId,
  455. 'universe.jpg',
  456. Path.resolve(
  457. __dirname + '/../../../../app/templates/project_files/universe.jpg'
  458. ),
  459. null,
  460. ownerId
  461. )
  462. .should.equal(true)
  463. })
  464. it('should build the main.tex template', function() {
  465. return this.handler._buildTemplate
  466. .calledWith('main.tex', ownerId, projectName)
  467. .should.equal(true)
  468. })
  469. it('should build the references.bib template', function() {
  470. return this.handler._buildTemplate
  471. .calledWith('references.bib', ownerId, projectName)
  472. .should.equal(true)
  473. })
  474. })
  475. describe('_buildTemplate', function() {
  476. beforeEach(function(done) {
  477. return this.handler._buildTemplate(
  478. 'main.tex',
  479. this.user_id,
  480. projectName,
  481. (err, templateLines) => {
  482. this.template = templateLines.reduce(
  483. (singleLine, line) => `${singleLine}\n${line}`
  484. )
  485. return done()
  486. }
  487. )
  488. })
  489. it('should insert the project name into the template', function(done) {
  490. this.template.indexOf(projectName).should.not.equal(-1)
  491. return done()
  492. })
  493. it('should insert the users name into the template', function(done) {
  494. this.template.indexOf(this.user.first_name).should.not.equal(-1)
  495. this.template.indexOf(this.user.last_name).should.not.equal(-1)
  496. return done()
  497. })
  498. it('should not have undefined in the template', function(done) {
  499. this.template.indexOf('undefined').should.equal(-1)
  500. return done()
  501. })
  502. it('should not have any underscore brackets in the output', function(done) {
  503. this.template.indexOf('{{').should.equal(-1)
  504. this.template.indexOf('<%=').should.equal(-1)
  505. return done()
  506. })
  507. it('should put the year in', function(done) {
  508. this.template.indexOf(new Date().getUTCFullYear()).should.not.equal(-1)
  509. return done()
  510. })
  511. })
  512. describe('_createRootDoc', function() {
  513. beforeEach(function(done) {
  514. this.project = new this.ProjectModel()
  515. return this.handler._createRootDoc(
  516. this.project,
  517. ownerId,
  518. ['line 1', 'line 2'],
  519. done
  520. )
  521. })
  522. it('should insert main.tex', function() {
  523. return this.ProjectEntityUpdateHandler.addDoc
  524. .calledWith(
  525. project_id,
  526. rootFolderId,
  527. 'main.tex',
  528. ['line 1', 'line 2'],
  529. ownerId
  530. )
  531. .should.equal(true)
  532. })
  533. it('should set the main doc id', function() {
  534. return this.ProjectEntityUpdateHandler.setRootDoc
  535. .calledWith(project_id, docId)
  536. .should.equal(true)
  537. })
  538. })
  539. })