ProjectHelperTests.js 9.5 KB

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