ProjectHelperTests.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. function _mapToAllowed(images) {
  6. return images.map(image => {
  7. return { imageName: image.imageName, allowed: image.allowed }
  8. })
  9. }
  10. describe('ProjectHelper', function () {
  11. beforeEach(function () {
  12. this.project = {
  13. _id: '123213jlkj9kdlsaj',
  14. }
  15. this.user = {
  16. _id: '588f3ddae8ebc1bac07c9fa4',
  17. first_name: 'bjkdsjfk',
  18. features: {},
  19. labsProgram: true,
  20. labsExperiments: ['monthly-texlive'],
  21. }
  22. this.adminUser = {
  23. _id: 'admin-user-id',
  24. isAdmin: true,
  25. alphaProgram: true,
  26. }
  27. this.Settings = {
  28. adminPrivilegeAvailable: true,
  29. allowedImageNames: [
  30. { imageName: 'texlive-full:2018.1', imageDesc: 'TeX Live 2018' },
  31. { imageName: 'texlive-full:2019.1', imageDesc: 'TeX Live 2019' },
  32. {
  33. imageName: 'texlive-full:2020.1',
  34. imageDesc: 'TeX Live 2020',
  35. alphaOnly: true,
  36. },
  37. {
  38. imageName: 'texlive-full:2021.1',
  39. imageDesc: 'TeX Live 2021',
  40. monthlyExperimental: true,
  41. },
  42. ],
  43. }
  44. this.ProjectHelper = SandboxedModule.require(MODULE_PATH, {
  45. requires: {
  46. 'mongodb-legacy': { ObjectId },
  47. '@overleaf/settings': this.Settings,
  48. },
  49. })
  50. })
  51. describe('isArchived', function () {
  52. describe('project.archived being an array', function () {
  53. it('returns true if user id is found', function () {
  54. this.project.archived = [
  55. new ObjectId('588f3ddae8ebc1bac07c9fa4'),
  56. new ObjectId('5c41deb2b4ca500153340809'),
  57. ]
  58. expect(
  59. this.ProjectHelper.isArchived(this.project, this.user._id)
  60. ).to.equal(true)
  61. })
  62. it('returns false if user id is not found', function () {
  63. this.project.archived = []
  64. expect(
  65. this.ProjectHelper.isArchived(this.project, this.user._id)
  66. ).to.equal(false)
  67. })
  68. })
  69. describe('project.archived being undefined', function () {
  70. it('returns false if archived is undefined', function () {
  71. this.project.archived = undefined
  72. expect(
  73. this.ProjectHelper.isArchived(this.project, this.user._id)
  74. ).to.equal(false)
  75. })
  76. })
  77. })
  78. describe('isTrashed', function () {
  79. it('returns true if user id is found', function () {
  80. this.project.trashed = [
  81. new ObjectId('588f3ddae8ebc1bac07c9fa4'),
  82. new ObjectId('5c41deb2b4ca500153340809'),
  83. ]
  84. expect(
  85. this.ProjectHelper.isTrashed(this.project, this.user._id)
  86. ).to.equal(true)
  87. })
  88. it('returns false if user id is not found', function () {
  89. this.project.trashed = []
  90. expect(
  91. this.ProjectHelper.isTrashed(this.project, this.user._id)
  92. ).to.equal(false)
  93. })
  94. describe('project.trashed being undefined', function () {
  95. it('returns false if trashed is undefined', function () {
  96. this.project.trashed = undefined
  97. expect(
  98. this.ProjectHelper.isTrashed(this.project, this.user._id)
  99. ).to.equal(false)
  100. })
  101. })
  102. })
  103. describe('compilerFromV1Engine', function () {
  104. it('returns the correct engine for latex_dvipdf', function () {
  105. expect(this.ProjectHelper.compilerFromV1Engine('latex_dvipdf')).to.equal(
  106. 'latex'
  107. )
  108. })
  109. it('returns the correct engine for pdflatex', function () {
  110. expect(this.ProjectHelper.compilerFromV1Engine('pdflatex')).to.equal(
  111. 'pdflatex'
  112. )
  113. })
  114. it('returns the correct engine for xelatex', function () {
  115. expect(this.ProjectHelper.compilerFromV1Engine('xelatex')).to.equal(
  116. 'xelatex'
  117. )
  118. })
  119. it('returns the correct engine for lualatex', function () {
  120. expect(this.ProjectHelper.compilerFromV1Engine('lualatex')).to.equal(
  121. 'lualatex'
  122. )
  123. })
  124. })
  125. describe('getAllowedImagesForUser', function () {
  126. it('marks alpha only images as not allowed when the user is anonymous', function () {
  127. const images = this.ProjectHelper.getAllowedImagesForUser(null)
  128. const imageNames = _mapToAllowed(images)
  129. expect(imageNames).to.deep.equal([
  130. { imageName: 'texlive-full:2018.1', allowed: true },
  131. { imageName: 'texlive-full:2019.1', allowed: true },
  132. { imageName: 'texlive-full:2020.1', allowed: false },
  133. { imageName: 'texlive-full:2021.1', allowed: false },
  134. ])
  135. })
  136. it('marks monthly labs images as not allowed when the user is anonymous', function () {
  137. const images = this.ProjectHelper.getAllowedImagesForUser(null)
  138. const imageNames = _mapToAllowed(images)
  139. expect(imageNames).to.deep.equal([
  140. { imageName: 'texlive-full:2018.1', allowed: true },
  141. { imageName: 'texlive-full:2019.1', allowed: true },
  142. { imageName: 'texlive-full:2020.1', allowed: false },
  143. { imageName: 'texlive-full:2021.1', allowed: false },
  144. ])
  145. })
  146. it('marks monthly labs images as allowed when the user is enrolled', function () {
  147. const images = this.ProjectHelper.getAllowedImagesForUser(this.user)
  148. const imageNames = _mapToAllowed(images)
  149. expect(imageNames).to.deep.equal([
  150. { imageName: 'texlive-full:2018.1', allowed: true },
  151. { imageName: 'texlive-full:2019.1', allowed: true },
  152. { imageName: 'texlive-full:2020.1', allowed: false },
  153. { imageName: 'texlive-full:2021.1', allowed: true },
  154. ])
  155. })
  156. it('marks alpha only images as not allowed when when the user is not admin', function () {
  157. const images = this.ProjectHelper.getAllowedImagesForUser(this.user)
  158. const imageNames = _mapToAllowed(images)
  159. expect(imageNames).to.deep.equal([
  160. { imageName: 'texlive-full:2018.1', allowed: true },
  161. { imageName: 'texlive-full:2019.1', allowed: true },
  162. { imageName: 'texlive-full:2020.1', allowed: false },
  163. { imageName: 'texlive-full:2021.1', allowed: true },
  164. ])
  165. })
  166. it('returns all images when the user is admin', function () {
  167. const images = this.ProjectHelper.getAllowedImagesForUser(this.adminUser)
  168. const imageNames = _mapToAllowed(images)
  169. expect(imageNames).to.deep.equal([
  170. { imageName: 'texlive-full:2018.1', allowed: true },
  171. { imageName: 'texlive-full:2019.1', allowed: true },
  172. { imageName: 'texlive-full:2020.1', allowed: true },
  173. { imageName: 'texlive-full:2021.1', allowed: false },
  174. ])
  175. })
  176. })
  177. })