ProjectHelperTests.js 9.5 KB

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