ProjectHelperTests.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. }
  19. this.Settings = {
  20. allowedImageNames: [
  21. { imageName: 'texlive-full:2018.1', imageDesc: 'TeX Live 2018' },
  22. { imageName: 'texlive-full:2019.1', imageDesc: 'TeX Live 2019' },
  23. {
  24. imageName: 'texlive-full:2020.1',
  25. imageDesc: 'TeX Live 2020',
  26. adminOnly: true,
  27. },
  28. ],
  29. }
  30. this.ProjectHelper = SandboxedModule.require(MODULE_PATH, {
  31. requires: {
  32. mongodb: { ObjectId },
  33. '@overleaf/settings': this.Settings,
  34. },
  35. })
  36. })
  37. describe('isArchived', function () {
  38. describe('project.archived being an array', function () {
  39. it('returns true if user id is found', function () {
  40. this.project.archived = [
  41. ObjectId('588f3ddae8ebc1bac07c9fa4'),
  42. ObjectId('5c41deb2b4ca500153340809'),
  43. ]
  44. expect(
  45. this.ProjectHelper.isArchived(this.project, this.user._id)
  46. ).to.equal(true)
  47. })
  48. it('returns false if user id is not found', function () {
  49. this.project.archived = []
  50. expect(
  51. this.ProjectHelper.isArchived(this.project, this.user._id)
  52. ).to.equal(false)
  53. })
  54. })
  55. describe('project.archived being a boolean', function () {
  56. it('returns true if archived is true', function () {
  57. this.project.archived = true
  58. expect(
  59. this.ProjectHelper.isArchived(this.project, this.user._id)
  60. ).to.equal(true)
  61. })
  62. it('returns false if archived is false', function () {
  63. this.project.archived = false
  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. ObjectId('588f3ddae8ebc1bac07c9fa4'),
  82. 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('calculateArchivedArray', function () {
  104. describe('project.archived being an array', function () {
  105. it('returns an array adding the current user id when archiving', function () {
  106. const project = { archived: [] }
  107. const result = this.ProjectHelper.calculateArchivedArray(
  108. project,
  109. ObjectId('5c922599cdb09e014aa7d499'),
  110. 'ARCHIVE'
  111. )
  112. expect(result).to.deep.equal([ObjectId('5c922599cdb09e014aa7d499')])
  113. })
  114. it('returns an array without the current user id when unarchiving', function () {
  115. const project = { archived: [ObjectId('5c922599cdb09e014aa7d499')] }
  116. const result = this.ProjectHelper.calculateArchivedArray(
  117. project,
  118. ObjectId('5c922599cdb09e014aa7d499'),
  119. 'UNARCHIVE'
  120. )
  121. expect(result).to.deep.equal([])
  122. })
  123. })
  124. describe('project.archived being a boolean and being true', function () {
  125. it('returns an array of all associated user ids when archiving', function () {
  126. const project = {
  127. archived: true,
  128. owner_ref: this.user._id,
  129. collaberator_refs: [
  130. ObjectId('4f2cfb341eb5855a5b000f8b'),
  131. ObjectId('5c45f3bd425ead01488675aa'),
  132. ],
  133. readOnly_refs: [ObjectId('5c92243fcdb09e014aa7d487')],
  134. tokenAccessReadAndWrite_refs: [ObjectId('5c922599cdb09e014aa7d499')],
  135. tokenAccessReadOnly_refs: [],
  136. }
  137. const result = this.ProjectHelper.calculateArchivedArray(
  138. project,
  139. this.user._id,
  140. 'ARCHIVE'
  141. )
  142. expect(result).to.deep.equal([
  143. this.user._id,
  144. ObjectId('4f2cfb341eb5855a5b000f8b'),
  145. ObjectId('5c45f3bd425ead01488675aa'),
  146. ObjectId('5c92243fcdb09e014aa7d487'),
  147. ObjectId('5c922599cdb09e014aa7d499'),
  148. ])
  149. })
  150. it('returns an array of all associated users without the current user id when unarchived', function () {
  151. const project = {
  152. archived: true,
  153. owner_ref: this.user._id,
  154. collaberator_refs: [
  155. ObjectId('4f2cfb341eb5855a5b000f8b'),
  156. ObjectId('5c45f3bd425ead01488675aa'),
  157. ObjectId('5c922599cdb09e014aa7d499'),
  158. ],
  159. readOnly_refs: [ObjectId('5c92243fcdb09e014aa7d487')],
  160. tokenAccessReadAndWrite_refs: [ObjectId('5c922599cdb09e014aa7d499')],
  161. tokenAccessReadOnly_refs: [],
  162. }
  163. const result = this.ProjectHelper.calculateArchivedArray(
  164. project,
  165. this.user._id,
  166. 'UNARCHIVE'
  167. )
  168. expect(result).to.deep.equal([
  169. ObjectId('4f2cfb341eb5855a5b000f8b'),
  170. ObjectId('5c45f3bd425ead01488675aa'),
  171. ObjectId('5c922599cdb09e014aa7d499'),
  172. ObjectId('5c92243fcdb09e014aa7d487'),
  173. ])
  174. })
  175. })
  176. describe('project.archived being a boolean and being false', function () {
  177. it('returns an array adding the current user id when archiving', function () {
  178. const project = { archived: false }
  179. const result = this.ProjectHelper.calculateArchivedArray(
  180. project,
  181. ObjectId('5c922599cdb09e014aa7d499'),
  182. 'ARCHIVE'
  183. )
  184. expect(result).to.deep.equal([ObjectId('5c922599cdb09e014aa7d499')])
  185. })
  186. it('returns an empty array when unarchiving', function () {
  187. const project = { archived: false }
  188. const result = this.ProjectHelper.calculateArchivedArray(
  189. project,
  190. ObjectId('5c922599cdb09e014aa7d499'),
  191. 'UNARCHIVE'
  192. )
  193. expect(result).to.deep.equal([])
  194. })
  195. })
  196. describe('project.archived not being set', function () {
  197. it('returns an array adding the current user id when archiving', function () {
  198. const project = { archived: undefined }
  199. const result = this.ProjectHelper.calculateArchivedArray(
  200. project,
  201. ObjectId('5c922599cdb09e014aa7d499'),
  202. 'ARCHIVE'
  203. )
  204. expect(result).to.deep.equal([ObjectId('5c922599cdb09e014aa7d499')])
  205. })
  206. it('returns an empty array when unarchiving', function () {
  207. const project = { archived: undefined }
  208. const result = this.ProjectHelper.calculateArchivedArray(
  209. project,
  210. ObjectId('5c922599cdb09e014aa7d499'),
  211. 'UNARCHIVE'
  212. )
  213. expect(result).to.deep.equal([])
  214. })
  215. })
  216. })
  217. describe('compilerFromV1Engine', function () {
  218. it('returns the correct engine for latex_dvipdf', function () {
  219. expect(this.ProjectHelper.compilerFromV1Engine('latex_dvipdf')).to.equal(
  220. 'latex'
  221. )
  222. })
  223. it('returns the correct engine for pdflatex', function () {
  224. expect(this.ProjectHelper.compilerFromV1Engine('pdflatex')).to.equal(
  225. 'pdflatex'
  226. )
  227. })
  228. it('returns the correct engine for xelatex', function () {
  229. expect(this.ProjectHelper.compilerFromV1Engine('xelatex')).to.equal(
  230. 'xelatex'
  231. )
  232. })
  233. it('returns the correct engine for lualatex', function () {
  234. expect(this.ProjectHelper.compilerFromV1Engine('lualatex')).to.equal(
  235. 'lualatex'
  236. )
  237. })
  238. })
  239. describe('getAllowedImagesForUser', function () {
  240. it('filters out admin-only images when the user is anonymous', function () {
  241. const images = this.ProjectHelper.getAllowedImagesForUser(null)
  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('filters out admin-only images when the user is not admin', function () {
  249. const images = this.ProjectHelper.getAllowedImagesForUser(this.user)
  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. ])
  255. })
  256. it('returns all images when the user is admin', function () {
  257. const images = this.ProjectHelper.getAllowedImagesForUser(this.adminUser)
  258. const imageNames = images.map(image => image.imageName)
  259. expect(imageNames).to.deep.equal([
  260. 'texlive-full:2018.1',
  261. 'texlive-full:2019.1',
  262. 'texlive-full:2020.1',
  263. ])
  264. })
  265. })
  266. })