ProjectHelper.test.mjs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. import { vi, expect } from 'vitest'
  2. import mongodb from 'mongodb-legacy'
  3. const { ObjectId } = mongodb
  4. const MODULE_PATH = '../../../../app/src/Features/Project/ProjectHelper.mjs'
  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(async function (ctx) {
  12. ctx.project = {
  13. _id: '123213jlkj9kdlsaj',
  14. }
  15. ctx.user = {
  16. _id: '588f3ddae8ebc1bac07c9fa4',
  17. first_name: 'bjkdsjfk',
  18. features: {},
  19. labsProgram: true,
  20. labsExperiments: ['monthly-texlive'],
  21. }
  22. ctx.adminUser = {
  23. _id: 'admin-user-id',
  24. isAdmin: true,
  25. alphaProgram: true,
  26. }
  27. ctx.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. ctx.SplitTestHandler = {
  45. promises: {
  46. getAssignmentForUser: vi.fn().mockResolvedValue({ variant: 'default' }),
  47. },
  48. }
  49. vi.doMock('mongodb-legacy', () => ({
  50. default: { ObjectId },
  51. }))
  52. vi.doMock('@overleaf/settings', () => ({
  53. default: ctx.Settings,
  54. }))
  55. vi.doMock(
  56. '../../../../app/src/Features/SplitTests/SplitTestHandler.mjs',
  57. () => ({
  58. default: ctx.SplitTestHandler,
  59. })
  60. )
  61. ctx.ProjectHelper = (await import(MODULE_PATH)).default
  62. })
  63. describe('isArchived', function () {
  64. describe('project.archived being an array', function () {
  65. it('returns true if user id is found', function (ctx) {
  66. ctx.project.archived = [
  67. new ObjectId('588f3ddae8ebc1bac07c9fa4'),
  68. new ObjectId('5c41deb2b4ca500153340809'),
  69. ]
  70. expect(
  71. ctx.ProjectHelper.isArchived(ctx.project, ctx.user._id)
  72. ).to.equal(true)
  73. })
  74. it('returns false if user id is not found', function (ctx) {
  75. ctx.project.archived = []
  76. expect(
  77. ctx.ProjectHelper.isArchived(ctx.project, ctx.user._id)
  78. ).to.equal(false)
  79. })
  80. })
  81. describe('project.archived being undefined', function () {
  82. it('returns false if archived is undefined', function (ctx) {
  83. ctx.project.archived = undefined
  84. expect(
  85. ctx.ProjectHelper.isArchived(ctx.project, ctx.user._id)
  86. ).to.equal(false)
  87. })
  88. })
  89. })
  90. describe('isTrashed', function () {
  91. it('returns true if user id is found', function (ctx) {
  92. ctx.project.trashed = [
  93. new ObjectId('588f3ddae8ebc1bac07c9fa4'),
  94. new ObjectId('5c41deb2b4ca500153340809'),
  95. ]
  96. expect(ctx.ProjectHelper.isTrashed(ctx.project, ctx.user._id)).to.equal(
  97. true
  98. )
  99. })
  100. it('returns false if user id is not found', function (ctx) {
  101. ctx.project.trashed = []
  102. expect(ctx.ProjectHelper.isTrashed(ctx.project, ctx.user._id)).to.equal(
  103. false
  104. )
  105. })
  106. describe('project.trashed being undefined', function () {
  107. it('returns false if trashed is undefined', function (ctx) {
  108. ctx.project.trashed = undefined
  109. expect(ctx.ProjectHelper.isTrashed(ctx.project, ctx.user._id)).to.equal(
  110. false
  111. )
  112. })
  113. })
  114. })
  115. describe('compilerFromV1Engine', function () {
  116. it('returns the correct engine for latex_dvipdf', function (ctx) {
  117. expect(ctx.ProjectHelper.compilerFromV1Engine('latex_dvipdf')).to.equal(
  118. 'latex'
  119. )
  120. })
  121. it('returns the correct engine for pdflatex', function (ctx) {
  122. expect(ctx.ProjectHelper.compilerFromV1Engine('pdflatex')).to.equal(
  123. 'pdflatex'
  124. )
  125. })
  126. it('returns the correct engine for xelatex', function (ctx) {
  127. expect(ctx.ProjectHelper.compilerFromV1Engine('xelatex')).to.equal(
  128. 'xelatex'
  129. )
  130. })
  131. it('returns the correct engine for lualatex', function (ctx) {
  132. expect(ctx.ProjectHelper.compilerFromV1Engine('lualatex')).to.equal(
  133. 'lualatex'
  134. )
  135. })
  136. })
  137. describe('getAllowedImagesForUser', function () {
  138. it('marks alpha only images as not allowed when the user is anonymous', async function (ctx) {
  139. const images = await ctx.ProjectHelper.getAllowedImagesForUser(null)
  140. const imageNames = _mapToAllowed(images)
  141. expect(imageNames).to.deep.equal([
  142. { imageName: 'texlive-full:2018.1', allowed: true },
  143. { imageName: 'texlive-full:2019.1', allowed: true },
  144. { imageName: 'texlive-full:2020.1', allowed: false },
  145. { imageName: 'texlive-full:2021.1', allowed: false },
  146. ])
  147. })
  148. it('marks monthly labs images as not allowed when the user is anonymous', async function (ctx) {
  149. const images = await ctx.ProjectHelper.getAllowedImagesForUser(null)
  150. const imageNames = _mapToAllowed(images)
  151. expect(imageNames).to.deep.equal([
  152. { imageName: 'texlive-full:2018.1', allowed: true },
  153. { imageName: 'texlive-full:2019.1', allowed: true },
  154. { imageName: 'texlive-full:2020.1', allowed: false },
  155. { imageName: 'texlive-full:2021.1', allowed: false },
  156. ])
  157. })
  158. it('marks monthly labs images as allowed when the user is enrolled', async function (ctx) {
  159. ctx.SplitTestHandler.promises.getAssignmentForUser.mockResolvedValue({
  160. variant: 'enabled',
  161. })
  162. const images = await ctx.ProjectHelper.getAllowedImagesForUser(ctx.user)
  163. const imageNames = _mapToAllowed(images)
  164. expect(imageNames).to.deep.equal([
  165. { imageName: 'texlive-full:2018.1', allowed: true },
  166. { imageName: 'texlive-full:2019.1', allowed: true },
  167. { imageName: 'texlive-full:2020.1', allowed: false },
  168. { imageName: 'texlive-full:2021.1', allowed: true },
  169. ])
  170. })
  171. it('marks alpha only images as not allowed when when the user is not admin', async function (ctx) {
  172. const images = await ctx.ProjectHelper.getAllowedImagesForUser(ctx.user)
  173. const imageNames = _mapToAllowed(images)
  174. expect(imageNames).to.deep.equal([
  175. { imageName: 'texlive-full:2018.1', allowed: true },
  176. { imageName: 'texlive-full:2019.1', allowed: true },
  177. { imageName: 'texlive-full:2020.1', allowed: false },
  178. { imageName: 'texlive-full:2021.1', allowed: false },
  179. ])
  180. })
  181. it('returns all images when the user is admin', async function (ctx) {
  182. const images = await ctx.ProjectHelper.getAllowedImagesForUser(
  183. ctx.adminUser
  184. )
  185. const imageNames = _mapToAllowed(images)
  186. expect(imageNames).to.deep.equal([
  187. { imageName: 'texlive-full:2018.1', allowed: true },
  188. { imageName: 'texlive-full:2019.1', allowed: true },
  189. { imageName: 'texlive-full:2020.1', allowed: true },
  190. { imageName: 'texlive-full:2021.1', allowed: false },
  191. ])
  192. })
  193. })
  194. })