2021-03-31 08:20:55 -04:00
|
|
|
const { expect } = require('chai')
|
2019-05-29 05:21:06 -04:00
|
|
|
const SandboxedModule = require('sandboxed-module')
|
2020-09-23 04:49:26 -04:00
|
|
|
const { ObjectId } = require('mongodb')
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2020-06-30 08:04:59 -04:00
|
|
|
const MODULE_PATH = '../../../../app/src/Features/Project/ProjectHelper.js'
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('ProjectHelper', function () {
|
|
|
|
beforeEach(function () {
|
2019-08-09 05:40:11 -04:00
|
|
|
this.project = {
|
2021-04-27 03:52:58 -04:00
|
|
|
_id: '123213jlkj9kdlsaj',
|
2019-08-09 05:40:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
this.user = {
|
|
|
|
_id: '588f3ddae8ebc1bac07c9fa4',
|
|
|
|
first_name: 'bjkdsjfk',
|
2021-04-27 03:52:58 -04:00
|
|
|
features: {},
|
2019-08-09 05:40:11 -04:00
|
|
|
}
|
|
|
|
|
2020-06-30 08:04:59 -04:00
|
|
|
this.adminUser = {
|
|
|
|
_id: 'admin-user-id',
|
2021-04-27 03:52:58 -04:00
|
|
|
isAdmin: true,
|
2022-06-22 15:19:16 -04:00
|
|
|
alphaProgram: true,
|
2020-06-30 08:04:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
this.Settings = {
|
2022-03-31 06:34:49 -04:00
|
|
|
adminPrivilegeAvailable: true,
|
2020-06-30 08:04:59 -04:00
|
|
|
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',
|
2022-06-22 15:19:16 -04:00
|
|
|
alphaOnly: true,
|
2021-04-27 03:52:58 -04:00
|
|
|
},
|
|
|
|
],
|
2020-06-30 08:04:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
this.ProjectHelper = SandboxedModule.require(MODULE_PATH, {
|
|
|
|
requires: {
|
2020-10-05 04:23:21 -04:00
|
|
|
mongodb: { ObjectId },
|
2021-07-07 05:38:56 -04:00
|
|
|
'@overleaf/settings': this.Settings,
|
2021-04-27 03:52:58 -04:00
|
|
|
},
|
2020-06-30 08:04:59 -04:00
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('isArchived', function () {
|
|
|
|
describe('project.archived being an array', function () {
|
|
|
|
it('returns true if user id is found', function () {
|
2019-08-09 05:40:11 -04:00
|
|
|
this.project.archived = [
|
|
|
|
ObjectId('588f3ddae8ebc1bac07c9fa4'),
|
2021-04-27 03:52:58 -04:00
|
|
|
ObjectId('5c41deb2b4ca500153340809'),
|
2019-08-09 05:40:11 -04:00
|
|
|
]
|
|
|
|
expect(
|
|
|
|
this.ProjectHelper.isArchived(this.project, this.user._id)
|
|
|
|
).to.equal(true)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('returns false if user id is not found', function () {
|
2019-08-09 05:40:11 -04:00
|
|
|
this.project.archived = []
|
|
|
|
expect(
|
|
|
|
this.ProjectHelper.isArchived(this.project, this.user._id)
|
|
|
|
).to.equal(false)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('project.archived being undefined', function () {
|
|
|
|
it('returns false if archived is undefined', function () {
|
2019-08-27 06:38:17 -04:00
|
|
|
this.project.archived = undefined
|
|
|
|
expect(
|
|
|
|
this.ProjectHelper.isArchived(this.project, this.user._id)
|
|
|
|
).to.equal(false)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('isTrashed', function () {
|
|
|
|
it('returns true if user id is found', function () {
|
2019-08-27 06:38:17 -04:00
|
|
|
this.project.trashed = [
|
|
|
|
ObjectId('588f3ddae8ebc1bac07c9fa4'),
|
2021-04-27 03:52:58 -04:00
|
|
|
ObjectId('5c41deb2b4ca500153340809'),
|
2019-08-27 06:38:17 -04:00
|
|
|
]
|
|
|
|
expect(
|
|
|
|
this.ProjectHelper.isTrashed(this.project, this.user._id)
|
|
|
|
).to.equal(true)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('returns false if user id is not found', function () {
|
2019-08-27 06:38:17 -04:00
|
|
|
this.project.trashed = []
|
|
|
|
expect(
|
|
|
|
this.ProjectHelper.isTrashed(this.project, this.user._id)
|
|
|
|
).to.equal(false)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('project.trashed being undefined', function () {
|
|
|
|
it('returns false if trashed is undefined', function () {
|
2019-08-27 06:38:17 -04:00
|
|
|
this.project.trashed = undefined
|
|
|
|
expect(
|
|
|
|
this.ProjectHelper.isTrashed(this.project, this.user._id)
|
|
|
|
).to.equal(false)
|
|
|
|
})
|
|
|
|
})
|
2019-08-09 05:40:11 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('calculateArchivedArray', function () {
|
|
|
|
describe('project.archived being an array', function () {
|
|
|
|
it('returns an array adding the current user id when archiving', function () {
|
2019-10-02 10:06:57 -04:00
|
|
|
const project = { archived: [] }
|
|
|
|
const result = this.ProjectHelper.calculateArchivedArray(
|
|
|
|
project,
|
|
|
|
ObjectId('5c922599cdb09e014aa7d499'),
|
|
|
|
'ARCHIVE'
|
|
|
|
)
|
|
|
|
expect(result).to.deep.equal([ObjectId('5c922599cdb09e014aa7d499')])
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('returns an array without the current user id when unarchiving', function () {
|
2019-10-02 10:06:57 -04:00
|
|
|
const project = { archived: [ObjectId('5c922599cdb09e014aa7d499')] }
|
|
|
|
const result = this.ProjectHelper.calculateArchivedArray(
|
|
|
|
project,
|
|
|
|
ObjectId('5c922599cdb09e014aa7d499'),
|
|
|
|
'UNARCHIVE'
|
|
|
|
)
|
|
|
|
expect(result).to.deep.equal([])
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('project.archived being a boolean and being true', function () {
|
|
|
|
it('returns an array of all associated user ids when archiving', function () {
|
2019-10-02 10:06:57 -04:00
|
|
|
const project = {
|
|
|
|
archived: true,
|
|
|
|
owner_ref: this.user._id,
|
|
|
|
collaberator_refs: [
|
|
|
|
ObjectId('4f2cfb341eb5855a5b000f8b'),
|
2021-04-27 03:52:58 -04:00
|
|
|
ObjectId('5c45f3bd425ead01488675aa'),
|
2019-10-02 10:06:57 -04:00
|
|
|
],
|
|
|
|
readOnly_refs: [ObjectId('5c92243fcdb09e014aa7d487')],
|
|
|
|
tokenAccessReadAndWrite_refs: [ObjectId('5c922599cdb09e014aa7d499')],
|
2021-04-27 03:52:58 -04:00
|
|
|
tokenAccessReadOnly_refs: [],
|
2019-10-02 10:06:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const result = this.ProjectHelper.calculateArchivedArray(
|
|
|
|
project,
|
|
|
|
this.user._id,
|
|
|
|
'ARCHIVE'
|
|
|
|
)
|
|
|
|
expect(result).to.deep.equal([
|
|
|
|
this.user._id,
|
|
|
|
ObjectId('4f2cfb341eb5855a5b000f8b'),
|
|
|
|
ObjectId('5c45f3bd425ead01488675aa'),
|
|
|
|
ObjectId('5c92243fcdb09e014aa7d487'),
|
2021-04-27 03:52:58 -04:00
|
|
|
ObjectId('5c922599cdb09e014aa7d499'),
|
2019-10-02 10:06:57 -04:00
|
|
|
])
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('returns an array of all associated users without the current user id when unarchived', function () {
|
2019-10-02 10:06:57 -04:00
|
|
|
const project = {
|
|
|
|
archived: true,
|
|
|
|
owner_ref: this.user._id,
|
|
|
|
collaberator_refs: [
|
|
|
|
ObjectId('4f2cfb341eb5855a5b000f8b'),
|
|
|
|
ObjectId('5c45f3bd425ead01488675aa'),
|
2021-04-27 03:52:58 -04:00
|
|
|
ObjectId('5c922599cdb09e014aa7d499'),
|
2019-10-02 10:06:57 -04:00
|
|
|
],
|
|
|
|
readOnly_refs: [ObjectId('5c92243fcdb09e014aa7d487')],
|
|
|
|
tokenAccessReadAndWrite_refs: [ObjectId('5c922599cdb09e014aa7d499')],
|
2021-04-27 03:52:58 -04:00
|
|
|
tokenAccessReadOnly_refs: [],
|
2019-10-02 10:06:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const result = this.ProjectHelper.calculateArchivedArray(
|
|
|
|
project,
|
|
|
|
this.user._id,
|
|
|
|
'UNARCHIVE'
|
|
|
|
)
|
|
|
|
expect(result).to.deep.equal([
|
|
|
|
ObjectId('4f2cfb341eb5855a5b000f8b'),
|
|
|
|
ObjectId('5c45f3bd425ead01488675aa'),
|
|
|
|
ObjectId('5c922599cdb09e014aa7d499'),
|
2021-04-27 03:52:58 -04:00
|
|
|
ObjectId('5c92243fcdb09e014aa7d487'),
|
2019-10-02 10:06:57 -04:00
|
|
|
])
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('project.archived being a boolean and being false', function () {
|
|
|
|
it('returns an array adding the current user id when archiving', function () {
|
2019-10-02 10:06:57 -04:00
|
|
|
const project = { archived: false }
|
|
|
|
const result = this.ProjectHelper.calculateArchivedArray(
|
|
|
|
project,
|
|
|
|
ObjectId('5c922599cdb09e014aa7d499'),
|
|
|
|
'ARCHIVE'
|
|
|
|
)
|
|
|
|
expect(result).to.deep.equal([ObjectId('5c922599cdb09e014aa7d499')])
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('returns an empty array when unarchiving', function () {
|
2019-10-02 10:06:57 -04:00
|
|
|
const project = { archived: false }
|
|
|
|
const result = this.ProjectHelper.calculateArchivedArray(
|
|
|
|
project,
|
|
|
|
ObjectId('5c922599cdb09e014aa7d499'),
|
|
|
|
'UNARCHIVE'
|
|
|
|
)
|
|
|
|
expect(result).to.deep.equal([])
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('project.archived not being set', function () {
|
|
|
|
it('returns an array adding the current user id when archiving', function () {
|
2019-10-02 10:06:57 -04:00
|
|
|
const project = { archived: undefined }
|
|
|
|
const result = this.ProjectHelper.calculateArchivedArray(
|
|
|
|
project,
|
|
|
|
ObjectId('5c922599cdb09e014aa7d499'),
|
|
|
|
'ARCHIVE'
|
|
|
|
)
|
|
|
|
expect(result).to.deep.equal([ObjectId('5c922599cdb09e014aa7d499')])
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('returns an empty array when unarchiving', function () {
|
2019-10-02 10:06:57 -04:00
|
|
|
const project = { archived: undefined }
|
|
|
|
const result = this.ProjectHelper.calculateArchivedArray(
|
|
|
|
project,
|
|
|
|
ObjectId('5c922599cdb09e014aa7d499'),
|
|
|
|
'UNARCHIVE'
|
|
|
|
)
|
|
|
|
expect(result).to.deep.equal([])
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('compilerFromV1Engine', function () {
|
|
|
|
it('returns the correct engine for latex_dvipdf', function () {
|
2020-06-30 08:04:59 -04:00
|
|
|
expect(this.ProjectHelper.compilerFromV1Engine('latex_dvipdf')).to.equal(
|
|
|
|
'latex'
|
|
|
|
)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('returns the correct engine for pdflatex', function () {
|
2020-06-30 08:04:59 -04:00
|
|
|
expect(this.ProjectHelper.compilerFromV1Engine('pdflatex')).to.equal(
|
|
|
|
'pdflatex'
|
|
|
|
)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('returns the correct engine for xelatex', function () {
|
2020-06-30 08:04:59 -04:00
|
|
|
expect(this.ProjectHelper.compilerFromV1Engine('xelatex')).to.equal(
|
|
|
|
'xelatex'
|
|
|
|
)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('returns the correct engine for lualatex', function () {
|
2020-06-30 08:04:59 -04:00
|
|
|
expect(this.ProjectHelper.compilerFromV1Engine('lualatex')).to.equal(
|
|
|
|
'lualatex'
|
|
|
|
)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('getAllowedImagesForUser', function () {
|
2022-06-22 15:19:16 -04:00
|
|
|
it('filters out alpha-only images when the user is anonymous', function () {
|
2020-06-30 08:04:59 -04:00
|
|
|
const images = this.ProjectHelper.getAllowedImagesForUser(null)
|
|
|
|
const imageNames = images.map(image => image.imageName)
|
|
|
|
expect(imageNames).to.deep.equal([
|
|
|
|
'texlive-full:2018.1',
|
2021-04-27 03:52:58 -04:00
|
|
|
'texlive-full:2019.1',
|
2020-06-30 08:04:59 -04:00
|
|
|
])
|
|
|
|
})
|
|
|
|
|
2022-06-22 15:19:16 -04:00
|
|
|
it('filters out alpha-only images when the user is not admin', function () {
|
2020-06-30 08:04:59 -04:00
|
|
|
const images = this.ProjectHelper.getAllowedImagesForUser(this.user)
|
|
|
|
const imageNames = images.map(image => image.imageName)
|
|
|
|
expect(imageNames).to.deep.equal([
|
|
|
|
'texlive-full:2018.1',
|
2021-04-27 03:52:58 -04:00
|
|
|
'texlive-full:2019.1',
|
2020-06-30 08:04:59 -04:00
|
|
|
])
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('returns all images when the user is admin', function () {
|
2020-06-30 08:04:59 -04:00
|
|
|
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',
|
2021-04-27 03:52:58 -04:00
|
|
|
'texlive-full:2020.1',
|
2020-06-30 08:04:59 -04:00
|
|
|
])
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|