ProjectHelperTests.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. const { expect } = require('chai')
  2. const SandboxedModule = require('sandboxed-module')
  3. const { ObjectId } = require('mongodb')
  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: { 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. ObjectId('588f3ddae8ebc1bac07c9fa4'),
  44. 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. ObjectId('588f3ddae8ebc1bac07c9fa4'),
  70. 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('calculateArchivedArray', function () {
  92. describe('project.archived being an array', function () {
  93. it('returns an array adding the current user id when archiving', function () {
  94. const project = { archived: [] }
  95. const result = this.ProjectHelper.calculateArchivedArray(
  96. project,
  97. ObjectId('5c922599cdb09e014aa7d499'),
  98. 'ARCHIVE'
  99. )
  100. expect(result).to.deep.equal([ObjectId('5c922599cdb09e014aa7d499')])
  101. })
  102. it('returns an array without the current user id when unarchiving', function () {
  103. const project = { archived: [ObjectId('5c922599cdb09e014aa7d499')] }
  104. const result = this.ProjectHelper.calculateArchivedArray(
  105. project,
  106. ObjectId('5c922599cdb09e014aa7d499'),
  107. 'UNARCHIVE'
  108. )
  109. expect(result).to.deep.equal([])
  110. })
  111. })
  112. describe('project.archived being a boolean and being true', function () {
  113. it('returns an array of all associated user ids when archiving', function () {
  114. const project = {
  115. archived: true,
  116. owner_ref: this.user._id,
  117. collaberator_refs: [
  118. ObjectId('4f2cfb341eb5855a5b000f8b'),
  119. ObjectId('5c45f3bd425ead01488675aa'),
  120. ],
  121. readOnly_refs: [ObjectId('5c92243fcdb09e014aa7d487')],
  122. tokenAccessReadAndWrite_refs: [ObjectId('5c922599cdb09e014aa7d499')],
  123. tokenAccessReadOnly_refs: [],
  124. }
  125. const result = this.ProjectHelper.calculateArchivedArray(
  126. project,
  127. this.user._id,
  128. 'ARCHIVE'
  129. )
  130. expect(result).to.deep.equal([
  131. this.user._id,
  132. ObjectId('4f2cfb341eb5855a5b000f8b'),
  133. ObjectId('5c45f3bd425ead01488675aa'),
  134. ObjectId('5c92243fcdb09e014aa7d487'),
  135. ObjectId('5c922599cdb09e014aa7d499'),
  136. ])
  137. })
  138. it('returns an array of all associated users without the current user id when unarchived', function () {
  139. const project = {
  140. archived: true,
  141. owner_ref: this.user._id,
  142. collaberator_refs: [
  143. ObjectId('4f2cfb341eb5855a5b000f8b'),
  144. ObjectId('5c45f3bd425ead01488675aa'),
  145. ObjectId('5c922599cdb09e014aa7d499'),
  146. ],
  147. readOnly_refs: [ObjectId('5c92243fcdb09e014aa7d487')],
  148. tokenAccessReadAndWrite_refs: [ObjectId('5c922599cdb09e014aa7d499')],
  149. tokenAccessReadOnly_refs: [],
  150. }
  151. const result = this.ProjectHelper.calculateArchivedArray(
  152. project,
  153. this.user._id,
  154. 'UNARCHIVE'
  155. )
  156. expect(result).to.deep.equal([
  157. ObjectId('4f2cfb341eb5855a5b000f8b'),
  158. ObjectId('5c45f3bd425ead01488675aa'),
  159. ObjectId('5c922599cdb09e014aa7d499'),
  160. ObjectId('5c92243fcdb09e014aa7d487'),
  161. ])
  162. })
  163. })
  164. describe('project.archived being a boolean and being false', function () {
  165. it('returns an array adding the current user id when archiving', function () {
  166. const project = { archived: false }
  167. const result = this.ProjectHelper.calculateArchivedArray(
  168. project,
  169. ObjectId('5c922599cdb09e014aa7d499'),
  170. 'ARCHIVE'
  171. )
  172. expect(result).to.deep.equal([ObjectId('5c922599cdb09e014aa7d499')])
  173. })
  174. it('returns an empty array when unarchiving', function () {
  175. const project = { archived: false }
  176. const result = this.ProjectHelper.calculateArchivedArray(
  177. project,
  178. ObjectId('5c922599cdb09e014aa7d499'),
  179. 'UNARCHIVE'
  180. )
  181. expect(result).to.deep.equal([])
  182. })
  183. })
  184. describe('project.archived not being set', function () {
  185. it('returns an array adding the current user id when archiving', function () {
  186. const project = { archived: undefined }
  187. const result = this.ProjectHelper.calculateArchivedArray(
  188. project,
  189. ObjectId('5c922599cdb09e014aa7d499'),
  190. 'ARCHIVE'
  191. )
  192. expect(result).to.deep.equal([ObjectId('5c922599cdb09e014aa7d499')])
  193. })
  194. it('returns an empty array when unarchiving', function () {
  195. const project = { archived: undefined }
  196. const result = this.ProjectHelper.calculateArchivedArray(
  197. project,
  198. ObjectId('5c922599cdb09e014aa7d499'),
  199. 'UNARCHIVE'
  200. )
  201. expect(result).to.deep.equal([])
  202. })
  203. })
  204. })
  205. describe('compilerFromV1Engine', function () {
  206. it('returns the correct engine for latex_dvipdf', function () {
  207. expect(this.ProjectHelper.compilerFromV1Engine('latex_dvipdf')).to.equal(
  208. 'latex'
  209. )
  210. })
  211. it('returns the correct engine for pdflatex', function () {
  212. expect(this.ProjectHelper.compilerFromV1Engine('pdflatex')).to.equal(
  213. 'pdflatex'
  214. )
  215. })
  216. it('returns the correct engine for xelatex', function () {
  217. expect(this.ProjectHelper.compilerFromV1Engine('xelatex')).to.equal(
  218. 'xelatex'
  219. )
  220. })
  221. it('returns the correct engine for lualatex', function () {
  222. expect(this.ProjectHelper.compilerFromV1Engine('lualatex')).to.equal(
  223. 'lualatex'
  224. )
  225. })
  226. })
  227. describe('getAllowedImagesForUser', function () {
  228. it('filters out alpha-only images when the user is anonymous', function () {
  229. const images = this.ProjectHelper.getAllowedImagesForUser(null)
  230. const imageNames = images.map(image => image.imageName)
  231. expect(imageNames).to.deep.equal([
  232. 'texlive-full:2018.1',
  233. 'texlive-full:2019.1',
  234. ])
  235. })
  236. it('filters out alpha-only images when the user is not admin', function () {
  237. const images = this.ProjectHelper.getAllowedImagesForUser(this.user)
  238. const imageNames = images.map(image => image.imageName)
  239. expect(imageNames).to.deep.equal([
  240. 'texlive-full:2018.1',
  241. 'texlive-full:2019.1',
  242. ])
  243. })
  244. it('returns all images when the user is admin', function () {
  245. const images = this.ProjectHelper.getAllowedImagesForUser(this.adminUser)
  246. const imageNames = images.map(image => image.imageName)
  247. expect(imageNames).to.deep.equal([
  248. 'texlive-full:2018.1',
  249. 'texlive-full:2019.1',
  250. 'texlive-full:2020.1',
  251. ])
  252. })
  253. })
  254. })