| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298 |
- const { expect } = require('chai')
- const SandboxedModule = require('sandboxed-module')
- const { ObjectId } = require('mongodb')
- const MODULE_PATH = '../../../../app/src/Features/Project/ProjectHelper.js'
- describe('ProjectHelper', function () {
- beforeEach(function () {
- this.project = {
- _id: '123213jlkj9kdlsaj',
- }
- this.user = {
- _id: '588f3ddae8ebc1bac07c9fa4',
- first_name: 'bjkdsjfk',
- features: {},
- }
- this.adminUser = {
- _id: 'admin-user-id',
- isAdmin: true,
- }
- this.Settings = {
- adminPrivilegeAvailable: true,
- allowedImageNames: [
- { imageName: 'texlive-full:2018.1', imageDesc: 'TeX Live 2018' },
- { imageName: 'texlive-full:2019.1', imageDesc: 'TeX Live 2019' },
- {
- imageName: 'texlive-full:2020.1',
- imageDesc: 'TeX Live 2020',
- adminOnly: true,
- },
- ],
- }
- this.ProjectHelper = SandboxedModule.require(MODULE_PATH, {
- requires: {
- mongodb: { ObjectId },
- '@overleaf/settings': this.Settings,
- },
- })
- })
- describe('isArchived', function () {
- describe('project.archived being an array', function () {
- it('returns true if user id is found', function () {
- this.project.archived = [
- ObjectId('588f3ddae8ebc1bac07c9fa4'),
- ObjectId('5c41deb2b4ca500153340809'),
- ]
- expect(
- this.ProjectHelper.isArchived(this.project, this.user._id)
- ).to.equal(true)
- })
- it('returns false if user id is not found', function () {
- this.project.archived = []
- expect(
- this.ProjectHelper.isArchived(this.project, this.user._id)
- ).to.equal(false)
- })
- })
- describe('project.archived being a boolean', function () {
- it('returns true if archived is true', function () {
- this.project.archived = true
- expect(
- this.ProjectHelper.isArchived(this.project, this.user._id)
- ).to.equal(true)
- })
- it('returns false if archived is false', function () {
- this.project.archived = false
- expect(
- this.ProjectHelper.isArchived(this.project, this.user._id)
- ).to.equal(false)
- })
- })
- describe('project.archived being undefined', function () {
- it('returns false if archived is undefined', function () {
- this.project.archived = undefined
- expect(
- this.ProjectHelper.isArchived(this.project, this.user._id)
- ).to.equal(false)
- })
- })
- })
- describe('isTrashed', function () {
- it('returns true if user id is found', function () {
- this.project.trashed = [
- ObjectId('588f3ddae8ebc1bac07c9fa4'),
- ObjectId('5c41deb2b4ca500153340809'),
- ]
- expect(
- this.ProjectHelper.isTrashed(this.project, this.user._id)
- ).to.equal(true)
- })
- it('returns false if user id is not found', function () {
- this.project.trashed = []
- expect(
- this.ProjectHelper.isTrashed(this.project, this.user._id)
- ).to.equal(false)
- })
- describe('project.trashed being undefined', function () {
- it('returns false if trashed is undefined', function () {
- this.project.trashed = undefined
- expect(
- this.ProjectHelper.isTrashed(this.project, this.user._id)
- ).to.equal(false)
- })
- })
- })
- describe('calculateArchivedArray', function () {
- describe('project.archived being an array', function () {
- it('returns an array adding the current user id when archiving', function () {
- const project = { archived: [] }
- const result = this.ProjectHelper.calculateArchivedArray(
- project,
- ObjectId('5c922599cdb09e014aa7d499'),
- 'ARCHIVE'
- )
- expect(result).to.deep.equal([ObjectId('5c922599cdb09e014aa7d499')])
- })
- it('returns an array without the current user id when unarchiving', function () {
- const project = { archived: [ObjectId('5c922599cdb09e014aa7d499')] }
- const result = this.ProjectHelper.calculateArchivedArray(
- project,
- ObjectId('5c922599cdb09e014aa7d499'),
- 'UNARCHIVE'
- )
- expect(result).to.deep.equal([])
- })
- })
- describe('project.archived being a boolean and being true', function () {
- it('returns an array of all associated user ids when archiving', function () {
- const project = {
- archived: true,
- owner_ref: this.user._id,
- collaberator_refs: [
- ObjectId('4f2cfb341eb5855a5b000f8b'),
- ObjectId('5c45f3bd425ead01488675aa'),
- ],
- readOnly_refs: [ObjectId('5c92243fcdb09e014aa7d487')],
- tokenAccessReadAndWrite_refs: [ObjectId('5c922599cdb09e014aa7d499')],
- tokenAccessReadOnly_refs: [],
- }
- const result = this.ProjectHelper.calculateArchivedArray(
- project,
- this.user._id,
- 'ARCHIVE'
- )
- expect(result).to.deep.equal([
- this.user._id,
- ObjectId('4f2cfb341eb5855a5b000f8b'),
- ObjectId('5c45f3bd425ead01488675aa'),
- ObjectId('5c92243fcdb09e014aa7d487'),
- ObjectId('5c922599cdb09e014aa7d499'),
- ])
- })
- it('returns an array of all associated users without the current user id when unarchived', function () {
- const project = {
- archived: true,
- owner_ref: this.user._id,
- collaberator_refs: [
- ObjectId('4f2cfb341eb5855a5b000f8b'),
- ObjectId('5c45f3bd425ead01488675aa'),
- ObjectId('5c922599cdb09e014aa7d499'),
- ],
- readOnly_refs: [ObjectId('5c92243fcdb09e014aa7d487')],
- tokenAccessReadAndWrite_refs: [ObjectId('5c922599cdb09e014aa7d499')],
- tokenAccessReadOnly_refs: [],
- }
- const result = this.ProjectHelper.calculateArchivedArray(
- project,
- this.user._id,
- 'UNARCHIVE'
- )
- expect(result).to.deep.equal([
- ObjectId('4f2cfb341eb5855a5b000f8b'),
- ObjectId('5c45f3bd425ead01488675aa'),
- ObjectId('5c922599cdb09e014aa7d499'),
- ObjectId('5c92243fcdb09e014aa7d487'),
- ])
- })
- })
- describe('project.archived being a boolean and being false', function () {
- it('returns an array adding the current user id when archiving', function () {
- const project = { archived: false }
- const result = this.ProjectHelper.calculateArchivedArray(
- project,
- ObjectId('5c922599cdb09e014aa7d499'),
- 'ARCHIVE'
- )
- expect(result).to.deep.equal([ObjectId('5c922599cdb09e014aa7d499')])
- })
- it('returns an empty array when unarchiving', function () {
- const project = { archived: false }
- const result = this.ProjectHelper.calculateArchivedArray(
- project,
- ObjectId('5c922599cdb09e014aa7d499'),
- 'UNARCHIVE'
- )
- expect(result).to.deep.equal([])
- })
- })
- describe('project.archived not being set', function () {
- it('returns an array adding the current user id when archiving', function () {
- const project = { archived: undefined }
- const result = this.ProjectHelper.calculateArchivedArray(
- project,
- ObjectId('5c922599cdb09e014aa7d499'),
- 'ARCHIVE'
- )
- expect(result).to.deep.equal([ObjectId('5c922599cdb09e014aa7d499')])
- })
- it('returns an empty array when unarchiving', function () {
- const project = { archived: undefined }
- const result = this.ProjectHelper.calculateArchivedArray(
- project,
- ObjectId('5c922599cdb09e014aa7d499'),
- 'UNARCHIVE'
- )
- expect(result).to.deep.equal([])
- })
- })
- })
- describe('compilerFromV1Engine', function () {
- it('returns the correct engine for latex_dvipdf', function () {
- expect(this.ProjectHelper.compilerFromV1Engine('latex_dvipdf')).to.equal(
- 'latex'
- )
- })
- it('returns the correct engine for pdflatex', function () {
- expect(this.ProjectHelper.compilerFromV1Engine('pdflatex')).to.equal(
- 'pdflatex'
- )
- })
- it('returns the correct engine for xelatex', function () {
- expect(this.ProjectHelper.compilerFromV1Engine('xelatex')).to.equal(
- 'xelatex'
- )
- })
- it('returns the correct engine for lualatex', function () {
- expect(this.ProjectHelper.compilerFromV1Engine('lualatex')).to.equal(
- 'lualatex'
- )
- })
- })
- describe('getAllowedImagesForUser', function () {
- it('filters out admin-only images when the user is anonymous', function () {
- const images = this.ProjectHelper.getAllowedImagesForUser(null)
- const imageNames = images.map(image => image.imageName)
- expect(imageNames).to.deep.equal([
- 'texlive-full:2018.1',
- 'texlive-full:2019.1',
- ])
- })
- it('filters out admin-only images when the user is not admin', function () {
- const images = this.ProjectHelper.getAllowedImagesForUser(this.user)
- const imageNames = images.map(image => image.imageName)
- expect(imageNames).to.deep.equal([
- 'texlive-full:2018.1',
- 'texlive-full:2019.1',
- ])
- })
- it('returns all images when the user is admin', function () {
- const images = this.ProjectHelper.getAllowedImagesForUser(this.adminUser)
- const imageNames = images.map(image => image.imageName)
- expect(imageNames).to.deep.equal([
- 'texlive-full:2018.1',
- 'texlive-full:2019.1',
- 'texlive-full:2020.1',
- ])
- })
- })
- })
|