ProjectHelperTests.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. const { expect } = require('chai')
  2. const SandboxedModule = require('sandboxed-module')
  3. const { ObjectId } = require('mongodb-legacy')
  4. const MODULE_PATH = '../../../../app/src/Features/Project/ProjectHelper.js'
  5. describe('ProjectHelper', function () {
  6. beforeEach(function () {
  7. this.project = {
  8. _id: '123213jlkj9kdlsaj',
  9. }
  10. this.user = {
  11. _id: '588f3ddae8ebc1bac07c9fa4',
  12. first_name: 'bjkdsjfk',
  13. features: {},
  14. }
  15. this.adminUser = {
  16. _id: 'admin-user-id',
  17. isAdmin: true,
  18. alphaProgram: true,
  19. }
  20. this.Settings = {
  21. adminPrivilegeAvailable: true,
  22. allowedImageNames: [
  23. { imageName: 'texlive-full:2018.1', imageDesc: 'TeX Live 2018' },
  24. { imageName: 'texlive-full:2019.1', imageDesc: 'TeX Live 2019' },
  25. {
  26. imageName: 'texlive-full:2020.1',
  27. imageDesc: 'TeX Live 2020',
  28. alphaOnly: true,
  29. },
  30. ],
  31. }
  32. this.ProjectHelper = SandboxedModule.require(MODULE_PATH, {
  33. requires: {
  34. 'mongodb-legacy': { ObjectId },
  35. '@overleaf/settings': this.Settings,
  36. },
  37. })
  38. })
  39. describe('isArchived', function () {
  40. describe('project.archived being an array', function () {
  41. it('returns true if user id is found', function () {
  42. this.project.archived = [
  43. new ObjectId('588f3ddae8ebc1bac07c9fa4'),
  44. new ObjectId('5c41deb2b4ca500153340809'),
  45. ]
  46. expect(
  47. this.ProjectHelper.isArchived(this.project, this.user._id)
  48. ).to.equal(true)
  49. })
  50. it('returns false if user id is not found', function () {
  51. this.project.archived = []
  52. expect(
  53. this.ProjectHelper.isArchived(this.project, this.user._id)
  54. ).to.equal(false)
  55. })
  56. })
  57. describe('project.archived being undefined', function () {
  58. it('returns false if archived is undefined', function () {
  59. this.project.archived = undefined
  60. expect(
  61. this.ProjectHelper.isArchived(this.project, this.user._id)
  62. ).to.equal(false)
  63. })
  64. })
  65. })
  66. describe('isTrashed', function () {
  67. it('returns true if user id is found', function () {
  68. this.project.trashed = [
  69. new ObjectId('588f3ddae8ebc1bac07c9fa4'),
  70. new ObjectId('5c41deb2b4ca500153340809'),
  71. ]
  72. expect(
  73. this.ProjectHelper.isTrashed(this.project, this.user._id)
  74. ).to.equal(true)
  75. })
  76. it('returns false if user id is not found', function () {
  77. this.project.trashed = []
  78. expect(
  79. this.ProjectHelper.isTrashed(this.project, this.user._id)
  80. ).to.equal(false)
  81. })
  82. describe('project.trashed being undefined', function () {
  83. it('returns false if trashed is undefined', function () {
  84. this.project.trashed = undefined
  85. expect(
  86. this.ProjectHelper.isTrashed(this.project, this.user._id)
  87. ).to.equal(false)
  88. })
  89. })
  90. })
  91. describe('compilerFromV1Engine', function () {
  92. it('returns the correct engine for latex_dvipdf', function () {
  93. expect(this.ProjectHelper.compilerFromV1Engine('latex_dvipdf')).to.equal(
  94. 'latex'
  95. )
  96. })
  97. it('returns the correct engine for pdflatex', function () {
  98. expect(this.ProjectHelper.compilerFromV1Engine('pdflatex')).to.equal(
  99. 'pdflatex'
  100. )
  101. })
  102. it('returns the correct engine for xelatex', function () {
  103. expect(this.ProjectHelper.compilerFromV1Engine('xelatex')).to.equal(
  104. 'xelatex'
  105. )
  106. })
  107. it('returns the correct engine for lualatex', function () {
  108. expect(this.ProjectHelper.compilerFromV1Engine('lualatex')).to.equal(
  109. 'lualatex'
  110. )
  111. })
  112. })
  113. describe('getAllowedImagesForUser', function () {
  114. it('filters out alpha-only images when the user is anonymous', function () {
  115. const images = this.ProjectHelper.getAllowedImagesForUser(null)
  116. const imageNames = images.map(image => image.imageName)
  117. expect(imageNames).to.deep.equal([
  118. 'texlive-full:2018.1',
  119. 'texlive-full:2019.1',
  120. ])
  121. })
  122. it('filters out alpha-only images when the user is not admin', function () {
  123. const images = this.ProjectHelper.getAllowedImagesForUser(this.user)
  124. const imageNames = images.map(image => image.imageName)
  125. expect(imageNames).to.deep.equal([
  126. 'texlive-full:2018.1',
  127. 'texlive-full:2019.1',
  128. ])
  129. })
  130. it('returns all images when the user is admin', function () {
  131. const images = this.ProjectHelper.getAllowedImagesForUser(this.adminUser)
  132. const imageNames = images.map(image => image.imageName)
  133. expect(imageNames).to.deep.equal([
  134. 'texlive-full:2018.1',
  135. 'texlive-full:2019.1',
  136. 'texlive-full:2020.1',
  137. ])
  138. })
  139. })
  140. })