ProjectCreationHandlerTests.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  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. requires: {
  86. '../../models/User': {
  87. User: this.User
  88. },
  89. '../../models/Project': { Project: this.ProjectModel },
  90. '../../models/Folder': { Folder: this.FolderModel },
  91. '../History/HistoryManager': this.HistoryManager,
  92. './ProjectEntityUpdateHandler': this.ProjectEntityUpdateHandler,
  93. './ProjectDetailsHandler': this.ProjectDetailsHandler,
  94. 'settings-sharelatex': this.Settings,
  95. '../Analytics/AnalyticsManager': this.AnalyticsManager,
  96. 'logger-sharelatex': { log() {} },
  97. 'metrics-sharelatex': {
  98. inc() {},
  99. timeAsyncMethod() {}
  100. }
  101. }
  102. }))
  103. })
  104. describe('Creating a Blank project', function() {
  105. beforeEach(function() {
  106. this.overleaf_id = 1234
  107. this.HistoryManager.initializeProject = sinon
  108. .stub()
  109. .callsArgWith(0, null, { overleaf_id: this.overleaf_id })
  110. return (this.ProjectModel.prototype.save = sinon.stub().callsArg(0))
  111. })
  112. describe('successfully', function() {
  113. it('should save the project', function(done) {
  114. return this.handler.createBlankProject(ownerId, projectName, () => {
  115. this.ProjectModel.prototype.save.called.should.equal(true)
  116. return done()
  117. })
  118. })
  119. it('should return the project in the callback', function(done) {
  120. return this.handler.createBlankProject(ownerId, projectName, function(
  121. err,
  122. project
  123. ) {
  124. project.name.should.equal(projectName)
  125. ;(project.owner_ref + '').should.equal(ownerId)
  126. return done()
  127. })
  128. })
  129. it('should initialize the project overleaf if history id not provided', function(done) {
  130. this.handler.createBlankProject(ownerId, projectName, done)
  131. return this.HistoryManager.initializeProject
  132. .calledWith()
  133. .should.equal(true)
  134. })
  135. it('should set the overleaf id if overleaf id not provided', function(done) {
  136. return this.handler.createBlankProject(
  137. ownerId,
  138. projectName,
  139. (err, project) => {
  140. project.overleaf.history.id.should.equal(this.overleaf_id)
  141. return done()
  142. }
  143. )
  144. })
  145. it('should set the overleaf id if overleaf id provided', function(done) {
  146. const overleaf_id = 2345
  147. const attributes = {
  148. overleaf: {
  149. history: {
  150. id: overleaf_id
  151. }
  152. }
  153. }
  154. return this.handler.createBlankProject(
  155. ownerId,
  156. projectName,
  157. attributes,
  158. function(err, project) {
  159. project.overleaf.history.id.should.equal(overleaf_id)
  160. return done()
  161. }
  162. )
  163. })
  164. it('should set the language from the user', function(done) {
  165. return this.handler.createBlankProject(ownerId, projectName, function(
  166. err,
  167. project
  168. ) {
  169. project.spellCheckLanguage.should.equal('de')
  170. return done()
  171. })
  172. })
  173. it('should set the imageName to currentImageName if set and no imageName attribute', function(done) {
  174. this.Settings.currentImageName = 'mock-image-name'
  175. return this.handler.createBlankProject(
  176. ownerId,
  177. projectName,
  178. (err, project) => {
  179. project.imageName.should.equal(this.Settings.currentImageName)
  180. return done()
  181. }
  182. )
  183. })
  184. it('should not set the imageName if no currentImageName', function(done) {
  185. this.Settings.currentImageName = null
  186. return this.handler.createBlankProject(
  187. ownerId,
  188. projectName,
  189. (err, project) => {
  190. expect(project.imageName).to.not.exist
  191. return done()
  192. }
  193. )
  194. })
  195. it('should set the imageName to the attribute value if set and not overwrite it with the currentImageName', function(done) {
  196. this.Settings.currentImageName = 'mock-image-name'
  197. const attributes = { imageName: 'attribute-image-name' }
  198. return this.handler.createBlankProject(
  199. ownerId,
  200. projectName,
  201. attributes,
  202. (err, project) => {
  203. project.imageName.should.equal(attributes.imageName)
  204. return done()
  205. }
  206. )
  207. })
  208. it('should not set the overleaf.history.display if not configured in settings', function(done) {
  209. this.Settings.apis.project_history.displayHistoryForNewProjects = false
  210. return this.handler.createBlankProject(
  211. ownerId,
  212. projectName,
  213. (err, project) => {
  214. expect(project.overleaf.history.display).to.not.exist
  215. return done()
  216. }
  217. )
  218. })
  219. it('should set the overleaf.history.display if configured in settings', function(done) {
  220. this.Settings.apis.project_history.displayHistoryForNewProjects = true
  221. return this.handler.createBlankProject(
  222. ownerId,
  223. projectName,
  224. (err, project) => {
  225. expect(project.overleaf.history.display).to.equal(true)
  226. return done()
  227. }
  228. )
  229. })
  230. it('should send a project-created event to analytics', function(done) {
  231. return this.handler.createBlankProject(
  232. ownerId,
  233. projectName,
  234. (err, project) => {
  235. expect(this.AnalyticsManager.recordEvent.callCount).to.equal(1)
  236. expect(
  237. this.AnalyticsManager.recordEvent.calledWith(
  238. ownerId,
  239. 'project-created'
  240. )
  241. ).to.equal(true)
  242. return done()
  243. }
  244. )
  245. })
  246. it('should send a project-created event with template information if provided', function(done) {
  247. const attributes = {
  248. fromV1TemplateId: 100
  249. }
  250. return this.handler.createBlankProject(
  251. ownerId,
  252. projectName,
  253. attributes,
  254. (err, project) => {
  255. expect(this.AnalyticsManager.recordEvent.callCount).to.equal(1)
  256. expect(
  257. this.AnalyticsManager.recordEvent.calledWith(
  258. ownerId,
  259. 'project-created',
  260. { projectId: project._id, attributes }
  261. )
  262. ).to.equal(true)
  263. return done()
  264. }
  265. )
  266. })
  267. it('should send a project-imported event when importing a project', function(done) {
  268. const attributes = {
  269. overleaf: {
  270. history: {
  271. id: 100
  272. }
  273. }
  274. }
  275. return this.handler.createBlankProject(
  276. ownerId,
  277. projectName,
  278. attributes,
  279. (err, project) => {
  280. expect(this.AnalyticsManager.recordEvent.callCount).to.equal(1)
  281. expect(
  282. this.AnalyticsManager.recordEvent.calledWith(
  283. ownerId,
  284. 'project-imported'
  285. )
  286. ).to.equal(true)
  287. return done()
  288. }
  289. )
  290. })
  291. })
  292. describe('with an error', function() {
  293. beforeEach(function() {
  294. this.ProjectModel.prototype.save = sinon
  295. .stub()
  296. .callsArgWith(0, new Error('something went wrong'))
  297. return this.handler.createBlankProject(
  298. ownerId,
  299. projectName,
  300. this.callback
  301. )
  302. })
  303. it('should return the error to the callback', function() {
  304. return should.exist(this.callback.args[0][0])
  305. })
  306. })
  307. describe('with an invalid name', function() {
  308. beforeEach(function() {
  309. this.ProjectDetailsHandler.validateProjectName = sinon
  310. .stub()
  311. .yields(new Error('bad name'))
  312. return this.handler.createBlankProject(
  313. ownerId,
  314. projectName,
  315. this.callback
  316. )
  317. })
  318. it('should return the error to the callback', function() {
  319. return should.exist(this.callback.args[0][0])
  320. })
  321. it('should not try to create the project', function() {
  322. return this.ProjectModel.prototype.save.called.should.equal(false)
  323. })
  324. })
  325. })
  326. describe('Creating a basic project', function() {
  327. beforeEach(function() {
  328. this.project = new this.ProjectModel()
  329. this.handler._buildTemplate = function(
  330. template_name,
  331. user,
  332. project_name,
  333. callback
  334. ) {
  335. if (template_name === 'mainbasic.tex') {
  336. return callback(null, ['mainbasic.tex', 'lines'])
  337. }
  338. throw new Error(`unknown template: ${template_name}`)
  339. }
  340. sinon.spy(this.handler, '_buildTemplate')
  341. this.handler.createBlankProject = sinon
  342. .stub()
  343. .callsArgWith(2, null, this.project)
  344. this.handler._createRootDoc = sinon
  345. .stub()
  346. .callsArgWith(3, null, this.project)
  347. return this.handler.createBasicProject(
  348. ownerId,
  349. projectName,
  350. this.callback
  351. )
  352. })
  353. it('should create a blank project first', function() {
  354. return this.handler.createBlankProject
  355. .calledWith(ownerId, projectName)
  356. .should.equal(true)
  357. })
  358. it('should create the root document', function() {
  359. return this.handler._createRootDoc
  360. .calledWith(this.project, ownerId, ['mainbasic.tex', 'lines'])
  361. .should.equal(true)
  362. })
  363. it('should build the mainbasic.tex template', function() {
  364. return this.handler._buildTemplate
  365. .calledWith('mainbasic.tex', ownerId, projectName)
  366. .should.equal(true)
  367. })
  368. })
  369. describe('Creating a project from a snippet', function() {
  370. beforeEach(function() {
  371. this.project = new this.ProjectModel()
  372. this.handler.createBlankProject = sinon
  373. .stub()
  374. .callsArgWith(2, null, this.project)
  375. this.handler._createRootDoc = sinon
  376. .stub()
  377. .callsArgWith(3, null, this.project)
  378. return this.handler.createProjectFromSnippet(
  379. ownerId,
  380. projectName,
  381. ['snippet line 1', 'snippet line 2'],
  382. this.callback
  383. )
  384. })
  385. it('should create a blank project first', function() {
  386. return this.handler.createBlankProject
  387. .calledWith(ownerId, projectName)
  388. .should.equal(true)
  389. })
  390. it('should create the root document', function() {
  391. return this.handler._createRootDoc
  392. .calledWith(this.project, ownerId, ['snippet line 1', 'snippet line 2'])
  393. .should.equal(true)
  394. })
  395. })
  396. describe('Creating an example project', function() {
  397. beforeEach(function() {
  398. this.project = new this.ProjectModel()
  399. this.handler._buildTemplate = function(
  400. template_name,
  401. user,
  402. project_name,
  403. callback
  404. ) {
  405. if (template_name === 'main.tex') {
  406. return callback(null, ['main.tex', 'lines'])
  407. }
  408. if (template_name === 'references.bib') {
  409. return callback(null, ['references.bib', 'lines'])
  410. }
  411. throw new Error(`unknown template: ${template_name}`)
  412. }
  413. sinon.spy(this.handler, '_buildTemplate')
  414. this.handler.createBlankProject = sinon
  415. .stub()
  416. .callsArgWith(2, null, this.project)
  417. this.handler._createRootDoc = sinon
  418. .stub()
  419. .callsArgWith(3, null, this.project)
  420. return this.handler.createExampleProject(
  421. ownerId,
  422. projectName,
  423. this.callback
  424. )
  425. })
  426. it('should create a blank project first', function() {
  427. return this.handler.createBlankProject
  428. .calledWith(ownerId, projectName)
  429. .should.equal(true)
  430. })
  431. it('should create the root document', function() {
  432. return this.handler._createRootDoc
  433. .calledWith(this.project, ownerId, ['main.tex', 'lines'])
  434. .should.equal(true)
  435. })
  436. it('should insert references.bib', function() {
  437. return this.ProjectEntityUpdateHandler.addDoc
  438. .calledWith(
  439. project_id,
  440. rootFolderId,
  441. 'references.bib',
  442. ['references.bib', 'lines'],
  443. ownerId
  444. )
  445. .should.equal(true)
  446. })
  447. it('should insert universe.jpg', function() {
  448. return this.ProjectEntityUpdateHandler.addFile
  449. .calledWith(
  450. project_id,
  451. rootFolderId,
  452. 'universe.jpg',
  453. Path.resolve(
  454. __dirname + '/../../../../app/templates/project_files/universe.jpg'
  455. ),
  456. null,
  457. ownerId
  458. )
  459. .should.equal(true)
  460. })
  461. it('should build the main.tex template', function() {
  462. return this.handler._buildTemplate
  463. .calledWith('main.tex', ownerId, projectName)
  464. .should.equal(true)
  465. })
  466. it('should build the references.bib template', function() {
  467. return this.handler._buildTemplate
  468. .calledWith('references.bib', ownerId, projectName)
  469. .should.equal(true)
  470. })
  471. })
  472. describe('_buildTemplate', function() {
  473. beforeEach(function(done) {
  474. return this.handler._buildTemplate(
  475. 'main.tex',
  476. this.user_id,
  477. projectName,
  478. (err, templateLines) => {
  479. this.template = templateLines.reduce(
  480. (singleLine, line) => `${singleLine}\n${line}`
  481. )
  482. return done()
  483. }
  484. )
  485. })
  486. it('should insert the project name into the template', function(done) {
  487. this.template.indexOf(projectName).should.not.equal(-1)
  488. return done()
  489. })
  490. it('should insert the users name into the template', function(done) {
  491. this.template.indexOf(this.user.first_name).should.not.equal(-1)
  492. this.template.indexOf(this.user.last_name).should.not.equal(-1)
  493. return done()
  494. })
  495. it('should not have undefined in the template', function(done) {
  496. this.template.indexOf('undefined').should.equal(-1)
  497. return done()
  498. })
  499. it('should not have any underscore brackets in the output', function(done) {
  500. this.template.indexOf('{{').should.equal(-1)
  501. this.template.indexOf('<%=').should.equal(-1)
  502. return done()
  503. })
  504. it('should put the year in', function(done) {
  505. this.template.indexOf(new Date().getUTCFullYear()).should.not.equal(-1)
  506. return done()
  507. })
  508. })
  509. describe('_createRootDoc', function() {
  510. beforeEach(function(done) {
  511. this.project = new this.ProjectModel()
  512. return this.handler._createRootDoc(
  513. this.project,
  514. ownerId,
  515. ['line 1', 'line 2'],
  516. done
  517. )
  518. })
  519. it('should insert main.tex', function() {
  520. return this.ProjectEntityUpdateHandler.addDoc
  521. .calledWith(
  522. project_id,
  523. rootFolderId,
  524. 'main.tex',
  525. ['line 1', 'line 2'],
  526. ownerId
  527. )
  528. .should.equal(true)
  529. })
  530. it('should set the main doc id', function() {
  531. return this.ProjectEntityUpdateHandler.setRootDoc
  532. .calledWith(project_id, docId)
  533. .should.equal(true)
  534. })
  535. })
  536. })