ProjectHelperTests.js 9.2 KB

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