2019-05-29 05:21:06 -04:00
|
|
|
const SandboxedModule = require('sandboxed-module')
|
|
|
|
const sinon = require('sinon')
|
|
|
|
const { expect } = require('chai')
|
2019-09-30 10:46:15 -04:00
|
|
|
const { ObjectId } = require('mongodb')
|
|
|
|
const Errors = require('../../../../app/src/Features/Errors/Errors')
|
2020-11-27 08:11:12 -05:00
|
|
|
const ProjectHelper = require('../../../../app/src/Features/Project/ProjectHelper')
|
2019-09-09 07:52:25 -04:00
|
|
|
|
|
|
|
const MODULE_PATH = '../../../../app/src/Features/Project/ProjectDetailsHandler'
|
2019-05-29 05:21:06 -04:00
|
|
|
|
|
|
|
describe('ProjectDetailsHandler', function() {
|
|
|
|
beforeEach(function() {
|
2019-09-30 10:46:15 -04:00
|
|
|
this.user = {
|
2019-10-23 08:24:09 -04:00
|
|
|
_id: ObjectId(),
|
2019-10-17 11:28:47 -04:00
|
|
|
email: 'user@example.com',
|
2019-09-30 10:46:15 -04:00
|
|
|
features: 'mock-features'
|
|
|
|
}
|
|
|
|
this.collaborator = {
|
2019-10-23 08:24:09 -04:00
|
|
|
_id: ObjectId(),
|
2019-10-17 11:28:47 -04:00
|
|
|
email: 'collaborator@example.com'
|
2019-09-30 10:46:15 -04:00
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
this.project = {
|
2019-10-23 08:24:09 -04:00
|
|
|
_id: ObjectId(),
|
2019-05-29 05:21:06 -04:00
|
|
|
name: 'project',
|
|
|
|
description: 'this is a great project',
|
|
|
|
something: 'should not exist',
|
|
|
|
compiler: 'latexxxxxx',
|
2019-09-30 10:46:15 -04:00
|
|
|
owner_ref: this.user._id,
|
|
|
|
collaberator_refs: [this.collaborator._id]
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
this.ProjectGetter = {
|
2019-09-09 07:52:25 -04:00
|
|
|
promises: {
|
|
|
|
getProjectWithoutDocLines: sinon.stub().resolves(this.project),
|
|
|
|
getProject: sinon.stub().resolves(this.project),
|
|
|
|
findAllUsersProjects: sinon.stub().resolves({
|
|
|
|
owned: [],
|
|
|
|
readAndWrite: [],
|
|
|
|
readOnly: [],
|
|
|
|
tokenReadAndWrite: [],
|
|
|
|
tokenReadOnly: []
|
|
|
|
})
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2019-11-18 09:37:05 -05:00
|
|
|
this.ProjectModelUpdateQuery = {
|
|
|
|
exec: sinon.stub().resolves()
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
this.ProjectModel = {
|
2020-11-03 04:19:05 -05:00
|
|
|
updateOne: sinon.stub().returns(this.ProjectModelUpdateQuery)
|
2019-09-09 07:52:25 -04:00
|
|
|
}
|
|
|
|
this.UserGetter = {
|
|
|
|
promises: {
|
|
|
|
getUser: sinon.stub().resolves(this.user)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.TpdsUpdateSender = {
|
|
|
|
promises: {
|
|
|
|
moveEntity: sinon.stub().resolves()
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2019-09-09 07:52:25 -04:00
|
|
|
this.ProjectTokenGenerator = {
|
|
|
|
readAndWriteToken: sinon.stub(),
|
|
|
|
promises: {
|
|
|
|
generateUniqueReadOnlyToken: sinon.stub()
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2019-09-09 07:52:25 -04:00
|
|
|
this.settings = {
|
|
|
|
defaultFeatures: 'default-features'
|
|
|
|
}
|
|
|
|
this.handler = SandboxedModule.require(MODULE_PATH, {
|
2019-07-15 06:33:47 -04:00
|
|
|
globals: {
|
|
|
|
console: console
|
|
|
|
},
|
2019-05-29 05:21:06 -04:00
|
|
|
requires: {
|
2020-11-27 08:11:12 -05:00
|
|
|
'./ProjectHelper': ProjectHelper,
|
2019-05-29 05:21:06 -04:00
|
|
|
'./ProjectGetter': this.ProjectGetter,
|
|
|
|
'../../models/Project': {
|
|
|
|
Project: this.ProjectModel
|
|
|
|
},
|
|
|
|
'../User/UserGetter': this.UserGetter,
|
2019-09-09 07:52:25 -04:00
|
|
|
'../ThirdPartyDataStore/TpdsUpdateSender': this.TpdsUpdateSender,
|
2019-05-29 05:21:06 -04:00
|
|
|
'logger-sharelatex': {
|
|
|
|
log() {},
|
2019-07-01 09:48:09 -04:00
|
|
|
warn() {},
|
2019-05-29 05:21:06 -04:00
|
|
|
err() {}
|
|
|
|
},
|
2019-09-09 07:52:25 -04:00
|
|
|
'./ProjectTokenGenerator': this.ProjectTokenGenerator,
|
|
|
|
'settings-sharelatex': this.settings
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2019-09-09 07:52:25 -04:00
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
describe('getDetails', function() {
|
2019-09-09 07:52:25 -04:00
|
|
|
it('should find the project and owner', async function() {
|
2019-09-30 10:46:15 -04:00
|
|
|
const details = await this.handler.promises.getDetails(this.project._id)
|
2019-09-09 07:52:25 -04:00
|
|
|
details.name.should.equal(this.project.name)
|
|
|
|
details.description.should.equal(this.project.description)
|
|
|
|
details.compiler.should.equal(this.project.compiler)
|
|
|
|
details.features.should.equal(this.user.features)
|
|
|
|
expect(details.something).to.be.undefined
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2019-09-09 07:52:25 -04:00
|
|
|
it('should find overleaf metadata if it exists', async function() {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.project.overleaf = { id: 'id' }
|
2019-09-30 10:46:15 -04:00
|
|
|
const details = await this.handler.promises.getDetails(this.project._id)
|
2019-09-09 07:52:25 -04:00
|
|
|
details.overleaf.should.equal(this.project.overleaf)
|
|
|
|
expect(details.something).to.be.undefined
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2019-09-09 07:52:25 -04:00
|
|
|
it('should return an error for a non-existent project', async function() {
|
|
|
|
this.ProjectGetter.promises.getProject.resolves(null)
|
|
|
|
await expect(
|
|
|
|
this.handler.promises.getDetails('0123456789012345678901234')
|
|
|
|
).to.be.rejectedWith(Errors.NotFoundError)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2019-09-09 07:52:25 -04:00
|
|
|
it('should return the default features if no owner found', async function() {
|
|
|
|
this.UserGetter.promises.getUser.resolves(null)
|
2019-09-30 10:46:15 -04:00
|
|
|
const details = await this.handler.promises.getDetails(this.project._id)
|
2019-09-09 07:52:25 -04:00
|
|
|
details.features.should.equal(this.settings.defaultFeatures)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2019-09-09 07:52:25 -04:00
|
|
|
it('should rethrow any error', async function() {
|
|
|
|
this.ProjectGetter.promises.getProject.rejects(new Error('boom'))
|
2019-09-30 10:46:15 -04:00
|
|
|
await expect(this.handler.promises.getDetails(this.project._id)).to.be
|
2019-09-09 07:52:25 -04:00
|
|
|
.rejected
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('getProjectDescription', function() {
|
2019-09-09 07:52:25 -04:00
|
|
|
it('should make a call to mongo just for the description', async function() {
|
|
|
|
this.ProjectGetter.promises.getProject.resolves()
|
2019-09-30 10:46:15 -04:00
|
|
|
await this.handler.promises.getProjectDescription(this.project._id)
|
2020-12-15 05:23:54 -05:00
|
|
|
expect(
|
|
|
|
this.ProjectGetter.promises.getProject
|
|
|
|
).to.have.been.calledWith(this.project._id, { description: true })
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2019-09-09 07:52:25 -04:00
|
|
|
it('should return what the mongo call returns', async function() {
|
|
|
|
const expectedDescription = 'cool project'
|
|
|
|
this.ProjectGetter.promises.getProject.resolves({
|
|
|
|
description: expectedDescription
|
|
|
|
})
|
|
|
|
const description = await this.handler.promises.getProjectDescription(
|
2019-09-30 10:46:15 -04:00
|
|
|
this.project._id
|
2019-05-29 05:21:06 -04:00
|
|
|
)
|
2019-09-09 07:52:25 -04:00
|
|
|
expect(description).to.equal(expectedDescription)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('setProjectDescription', function() {
|
|
|
|
beforeEach(function() {
|
2019-09-09 07:52:25 -04:00
|
|
|
this.description = 'updated teh description'
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2019-09-09 07:52:25 -04:00
|
|
|
it('should update the project detials', async function() {
|
|
|
|
await this.handler.promises.setProjectDescription(
|
2019-09-30 10:46:15 -04:00
|
|
|
this.project._id,
|
2019-09-09 07:52:25 -04:00
|
|
|
this.description
|
|
|
|
)
|
2020-11-03 04:19:05 -05:00
|
|
|
expect(this.ProjectModel.updateOne).to.have.been.calledWith(
|
2019-09-30 10:46:15 -04:00
|
|
|
{ _id: this.project._id },
|
2019-09-09 07:52:25 -04:00
|
|
|
{ description: this.description }
|
2019-05-29 05:21:06 -04:00
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('renameProject', function() {
|
|
|
|
beforeEach(function() {
|
2019-09-09 07:52:25 -04:00
|
|
|
this.newName = 'new name here'
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2019-09-09 07:52:25 -04:00
|
|
|
it('should update the project with the new name', async function() {
|
2019-09-30 10:46:15 -04:00
|
|
|
await this.handler.promises.renameProject(this.project._id, this.newName)
|
2020-11-03 04:19:05 -05:00
|
|
|
expect(this.ProjectModel.updateOne).to.have.been.calledWith(
|
2019-09-30 10:46:15 -04:00
|
|
|
{ _id: this.project._id },
|
2019-09-09 07:52:25 -04:00
|
|
|
{ name: this.newName }
|
|
|
|
)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2019-09-09 07:52:25 -04:00
|
|
|
it('should tell the TpdsUpdateSender', async function() {
|
2019-09-30 10:46:15 -04:00
|
|
|
await this.handler.promises.renameProject(this.project._id, this.newName)
|
2019-09-09 07:52:25 -04:00
|
|
|
expect(this.TpdsUpdateSender.promises.moveEntity).to.have.been.calledWith(
|
|
|
|
{
|
2019-09-30 10:46:15 -04:00
|
|
|
project_id: this.project._id,
|
2019-09-09 07:52:25 -04:00
|
|
|
project_name: this.project.name,
|
|
|
|
newProjectName: this.newName
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should not do anything with an invalid name', async function() {
|
2019-09-30 10:46:15 -04:00
|
|
|
await expect(this.handler.promises.renameProject(this.project._id)).to.be
|
2019-09-09 07:52:25 -04:00
|
|
|
.rejected
|
|
|
|
expect(this.TpdsUpdateSender.promises.moveEntity).not.to.have.been.called
|
2020-11-03 04:19:05 -05:00
|
|
|
expect(this.ProjectModel.updateOne).not.to.have.been.called
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('validateProjectName', function() {
|
2019-09-09 07:52:25 -04:00
|
|
|
it('should reject undefined names', async function() {
|
|
|
|
await expect(this.handler.promises.validateProjectName(undefined)).to.be
|
|
|
|
.rejected
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2019-09-09 07:52:25 -04:00
|
|
|
it('should reject empty names', async function() {
|
|
|
|
await expect(this.handler.promises.validateProjectName('')).to.be.rejected
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2019-09-09 07:52:25 -04:00
|
|
|
it('should reject names with /s', async function() {
|
|
|
|
await expect(this.handler.promises.validateProjectName('foo/bar')).to.be
|
|
|
|
.rejected
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2019-09-09 07:52:25 -04:00
|
|
|
it('should reject names with \\s', async function() {
|
|
|
|
await expect(this.handler.promises.validateProjectName('foo\\bar')).to.be
|
|
|
|
.rejected
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2019-09-09 07:52:25 -04:00
|
|
|
it('should reject long names', async function() {
|
|
|
|
await expect(this.handler.promises.validateProjectName('a'.repeat(1000)))
|
|
|
|
.to.be.rejected
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2019-09-09 07:52:25 -04:00
|
|
|
it('should accept normal names', async function() {
|
|
|
|
await expect(this.handler.promises.validateProjectName('foobar')).to.be
|
2019-11-18 09:37:05 -05:00
|
|
|
.fulfilled
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-09-09 07:52:25 -04:00
|
|
|
describe('generateUniqueName', function() {
|
2020-11-27 08:11:12 -05:00
|
|
|
// actually testing `ProjectHelper.promises.ensureNameIsUnique()`
|
2019-05-29 05:21:06 -04:00
|
|
|
beforeEach(function() {
|
2019-09-09 07:52:25 -04:00
|
|
|
this.longName = 'x'.repeat(this.handler.MAX_PROJECT_NAME_LENGTH - 5)
|
|
|
|
const usersProjects = {
|
2019-05-29 05:21:06 -04:00
|
|
|
owned: [
|
|
|
|
{ _id: 1, name: 'name' },
|
|
|
|
{ _id: 2, name: 'name1' },
|
|
|
|
{ _id: 3, name: 'name11' },
|
2019-09-09 07:52:25 -04:00
|
|
|
{ _id: 100, name: 'numeric' },
|
|
|
|
{ _id: 101, name: 'numeric (1)' },
|
|
|
|
{ _id: 102, name: 'numeric (2)' },
|
|
|
|
{ _id: 103, name: 'numeric (3)' },
|
|
|
|
{ _id: 104, name: 'numeric (4)' },
|
|
|
|
{ _id: 105, name: 'numeric (5)' },
|
|
|
|
{ _id: 106, name: 'numeric (6)' },
|
|
|
|
{ _id: 107, name: 'numeric (7)' },
|
|
|
|
{ _id: 108, name: 'numeric (8)' },
|
|
|
|
{ _id: 109, name: 'numeric (9)' },
|
|
|
|
{ _id: 110, name: 'numeric (10)' },
|
|
|
|
{ _id: 111, name: 'numeric (11)' },
|
|
|
|
{ _id: 112, name: 'numeric (12)' },
|
|
|
|
{ _id: 113, name: 'numeric (13)' },
|
|
|
|
{ _id: 114, name: 'numeric (14)' },
|
|
|
|
{ _id: 115, name: 'numeric (15)' },
|
|
|
|
{ _id: 116, name: 'numeric (16)' },
|
|
|
|
{ _id: 117, name: 'numeric (17)' },
|
|
|
|
{ _id: 118, name: 'numeric (18)' },
|
|
|
|
{ _id: 119, name: 'numeric (19)' },
|
|
|
|
{ _id: 120, name: 'numeric (20)' },
|
|
|
|
{ _id: 130, name: 'numeric (30)' },
|
|
|
|
{ _id: 131, name: 'numeric (31)' },
|
|
|
|
{ _id: 132, name: 'numeric (32)' },
|
|
|
|
{ _id: 133, name: 'numeric (33)' },
|
|
|
|
{ _id: 134, name: 'numeric (34)' },
|
|
|
|
{ _id: 135, name: 'numeric (35)' },
|
|
|
|
{ _id: 136, name: 'numeric (36)' },
|
|
|
|
{ _id: 137, name: 'numeric (37)' },
|
|
|
|
{ _id: 138, name: 'numeric (38)' },
|
|
|
|
{ _id: 139, name: 'numeric (39)' },
|
|
|
|
{ _id: 140, name: 'numeric (40)' }
|
2019-05-29 05:21:06 -04:00
|
|
|
],
|
2020-12-15 05:23:54 -05:00
|
|
|
readAndWrite: [
|
|
|
|
{ _id: 4, name: 'name2' },
|
|
|
|
{ _id: 5, name: 'name22' }
|
|
|
|
],
|
|
|
|
readOnly: [
|
|
|
|
{ _id: 6, name: 'name3' },
|
|
|
|
{ _id: 7, name: 'name33' }
|
|
|
|
],
|
2019-05-29 05:21:06 -04:00
|
|
|
tokenReadAndWrite: [
|
|
|
|
{ _id: 8, name: 'name4' },
|
|
|
|
{ _id: 9, name: 'name44' }
|
|
|
|
],
|
|
|
|
tokenReadOnly: [
|
|
|
|
{ _id: 10, name: 'name5' },
|
|
|
|
{ _id: 11, name: 'name55' },
|
2019-09-09 07:52:25 -04:00
|
|
|
{ _id: 12, name: this.longName }
|
2019-05-29 05:21:06 -04:00
|
|
|
]
|
|
|
|
}
|
2019-09-09 07:52:25 -04:00
|
|
|
this.ProjectGetter.promises.findAllUsersProjects.resolves(usersProjects)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2019-09-09 07:52:25 -04:00
|
|
|
it('should leave a unique name unchanged', async function() {
|
|
|
|
const name = await this.handler.promises.generateUniqueName(
|
2019-09-30 10:46:15 -04:00
|
|
|
this.user._id,
|
2019-05-29 05:21:06 -04:00
|
|
|
'unique-name',
|
2019-09-09 07:52:25 -04:00
|
|
|
['-test-suffix']
|
2019-05-29 05:21:06 -04:00
|
|
|
)
|
2019-09-09 07:52:25 -04:00
|
|
|
expect(name).to.equal('unique-name')
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2019-09-09 07:52:25 -04:00
|
|
|
it('should append a suffix to an existing name', async function() {
|
|
|
|
const name = await this.handler.promises.generateUniqueName(
|
2019-09-30 10:46:15 -04:00
|
|
|
this.user._id,
|
2019-05-29 05:21:06 -04:00
|
|
|
'name1',
|
2019-09-09 07:52:25 -04:00
|
|
|
['-test-suffix']
|
2019-05-29 05:21:06 -04:00
|
|
|
)
|
2019-09-09 07:52:25 -04:00
|
|
|
expect(name).to.equal('name1-test-suffix')
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2019-09-09 07:52:25 -04:00
|
|
|
it('should fallback to a second suffix when needed', async function() {
|
|
|
|
const name = await this.handler.promises.generateUniqueName(
|
2019-09-30 10:46:15 -04:00
|
|
|
this.user._id,
|
2019-05-29 05:21:06 -04:00
|
|
|
'name1',
|
2019-09-09 07:52:25 -04:00
|
|
|
['1', '-test-suffix']
|
2019-05-29 05:21:06 -04:00
|
|
|
)
|
2019-09-09 07:52:25 -04:00
|
|
|
expect(name).to.equal('name1-test-suffix')
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2019-09-09 07:52:25 -04:00
|
|
|
it('should truncate the name when append a suffix if the result is too long', async function() {
|
|
|
|
const name = await this.handler.promises.generateUniqueName(
|
2019-09-30 10:46:15 -04:00
|
|
|
this.user._id,
|
2019-09-09 07:52:25 -04:00
|
|
|
this.longName,
|
|
|
|
['-test-suffix']
|
|
|
|
)
|
|
|
|
expect(name).to.equal(
|
|
|
|
this.longName.substr(0, this.handler.MAX_PROJECT_NAME_LENGTH - 12) +
|
|
|
|
'-test-suffix'
|
2019-05-29 05:21:06 -04:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2019-09-09 07:52:25 -04:00
|
|
|
it('should use a numeric index if no suffix is supplied', async function() {
|
|
|
|
const name = await this.handler.promises.generateUniqueName(
|
2019-09-30 10:46:15 -04:00
|
|
|
this.user._id,
|
2019-05-29 05:21:06 -04:00
|
|
|
'name1',
|
2019-09-09 07:52:25 -04:00
|
|
|
[]
|
2019-05-29 05:21:06 -04:00
|
|
|
)
|
2019-09-09 07:52:25 -04:00
|
|
|
expect(name).to.equal('name1 (1)')
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2019-09-09 07:52:25 -04:00
|
|
|
it('should use a numeric index if all suffixes are exhausted', async function() {
|
|
|
|
const name = await this.handler.promises.generateUniqueName(
|
2019-09-30 10:46:15 -04:00
|
|
|
this.user._id,
|
2019-05-29 05:21:06 -04:00
|
|
|
'name',
|
2019-09-09 07:52:25 -04:00
|
|
|
['1', '11']
|
2019-05-29 05:21:06 -04:00
|
|
|
)
|
2019-09-09 07:52:25 -04:00
|
|
|
expect(name).to.equal('name (1)')
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2019-09-09 07:52:25 -04:00
|
|
|
it('should find the next lowest available numeric index for the base name', async function() {
|
|
|
|
const name = await this.handler.promises.generateUniqueName(
|
2019-09-30 10:46:15 -04:00
|
|
|
this.user._id,
|
2019-05-29 05:21:06 -04:00
|
|
|
'numeric',
|
2019-09-09 07:52:25 -04:00
|
|
|
[]
|
2019-05-29 05:21:06 -04:00
|
|
|
)
|
2019-09-09 07:52:25 -04:00
|
|
|
expect(name).to.equal('numeric (21)')
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2019-09-09 07:52:25 -04:00
|
|
|
it('should find the next available numeric index when a numeric index is already present', async function() {
|
|
|
|
const name = await this.handler.promises.generateUniqueName(
|
2019-09-30 10:46:15 -04:00
|
|
|
this.user._id,
|
2019-05-29 05:21:06 -04:00
|
|
|
'numeric (5)',
|
2019-09-09 07:52:25 -04:00
|
|
|
[]
|
2019-05-29 05:21:06 -04:00
|
|
|
)
|
2019-09-09 07:52:25 -04:00
|
|
|
expect(name).to.equal('numeric (21)')
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2019-09-09 07:52:25 -04:00
|
|
|
it('should not find a numeric index lower than the one already present', async function() {
|
|
|
|
const name = await this.handler.promises.generateUniqueName(
|
2019-09-30 10:46:15 -04:00
|
|
|
this.user._id,
|
2019-05-29 05:21:06 -04:00
|
|
|
'numeric (31)',
|
2019-09-09 07:52:25 -04:00
|
|
|
[]
|
2019-05-29 05:21:06 -04:00
|
|
|
)
|
2019-09-09 07:52:25 -04:00
|
|
|
expect(name).to.equal('numeric (41)')
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('fixProjectName', function() {
|
|
|
|
it('should change empty names to Untitled', function() {
|
2019-09-09 07:52:25 -04:00
|
|
|
expect(this.handler.fixProjectName('')).to.equal('Untitled')
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should replace / with -', function() {
|
2019-09-09 07:52:25 -04:00
|
|
|
expect(this.handler.fixProjectName('foo/bar')).to.equal('foo-bar')
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
it("should replace \\ with ''", function() {
|
2019-09-09 07:52:25 -04:00
|
|
|
expect(this.handler.fixProjectName('foo \\ bar')).to.equal('foo bar')
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should truncate long names', function() {
|
2019-09-09 07:52:25 -04:00
|
|
|
expect(this.handler.fixProjectName('a'.repeat(1000))).to.equal(
|
|
|
|
'a'.repeat(150)
|
|
|
|
)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2019-06-21 09:46:09 -04:00
|
|
|
it('should accept normal names', function() {
|
2019-09-09 07:52:25 -04:00
|
|
|
expect(this.handler.fixProjectName('foobar')).to.equal('foobar')
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('setPublicAccessLevel', function() {
|
|
|
|
beforeEach(function() {
|
2019-09-09 07:52:25 -04:00
|
|
|
this.accessLevel = 'readOnly'
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2019-09-09 07:52:25 -04:00
|
|
|
it('should update the project with the new level', async function() {
|
|
|
|
await this.handler.promises.setPublicAccessLevel(
|
2019-09-30 10:46:15 -04:00
|
|
|
this.project._id,
|
2019-09-09 07:52:25 -04:00
|
|
|
this.accessLevel
|
|
|
|
)
|
2020-11-03 04:19:05 -05:00
|
|
|
expect(this.ProjectModel.updateOne).to.have.been.calledWith(
|
2019-09-30 10:46:15 -04:00
|
|
|
{ _id: this.project._id },
|
2019-09-09 07:52:25 -04:00
|
|
|
{ publicAccesLevel: this.accessLevel }
|
2019-05-29 05:21:06 -04:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2019-09-09 07:52:25 -04:00
|
|
|
it('should not produce an error', async function() {
|
|
|
|
await expect(
|
|
|
|
this.handler.promises.setPublicAccessLevel(
|
2019-09-30 10:46:15 -04:00
|
|
|
this.project._id,
|
2019-09-09 07:52:25 -04:00
|
|
|
this.accessLevel
|
|
|
|
)
|
2019-11-18 09:37:05 -05:00
|
|
|
).to.be.fulfilled
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2019-06-21 09:46:09 -04:00
|
|
|
describe('when update produces an error', function() {
|
2019-05-29 05:21:06 -04:00
|
|
|
beforeEach(function() {
|
2019-11-18 09:37:05 -05:00
|
|
|
this.ProjectModelUpdateQuery.exec.rejects(new Error('woops'))
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2019-09-09 07:52:25 -04:00
|
|
|
it('should produce an error', async function() {
|
|
|
|
await expect(
|
|
|
|
this.handler.promises.setPublicAccessLevel(
|
2019-09-30 10:46:15 -04:00
|
|
|
this.project._id,
|
2019-09-09 07:52:25 -04:00
|
|
|
this.accessLevel
|
|
|
|
)
|
|
|
|
).to.be.rejected
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-06-21 09:46:09 -04:00
|
|
|
describe('ensureTokensArePresent', function() {
|
2019-05-29 05:21:06 -04:00
|
|
|
describe('when the project has tokens', function() {
|
|
|
|
beforeEach(function() {
|
|
|
|
this.project = {
|
2019-09-30 10:46:15 -04:00
|
|
|
_id: this.project._id,
|
2019-05-29 05:21:06 -04:00
|
|
|
tokens: {
|
|
|
|
readOnly: 'aaa',
|
|
|
|
readAndWrite: '42bbb',
|
|
|
|
readAndWritePrefix: '42'
|
|
|
|
}
|
|
|
|
}
|
2019-09-09 07:52:25 -04:00
|
|
|
this.ProjectGetter.promises.getProject.resolves(this.project)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2019-09-09 07:52:25 -04:00
|
|
|
it('should get the project', async function() {
|
2019-09-30 10:46:15 -04:00
|
|
|
await this.handler.promises.ensureTokensArePresent(this.project._id)
|
2019-09-09 07:52:25 -04:00
|
|
|
expect(this.ProjectGetter.promises.getProject).to.have.been.calledOnce
|
|
|
|
expect(this.ProjectGetter.promises.getProject).to.have.been.calledWith(
|
2019-09-30 10:46:15 -04:00
|
|
|
this.project._id,
|
2019-09-09 07:52:25 -04:00
|
|
|
{
|
|
|
|
tokens: 1
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2019-09-09 07:52:25 -04:00
|
|
|
it('should not update the project with new tokens', async function() {
|
2019-09-30 10:46:15 -04:00
|
|
|
await this.handler.promises.ensureTokensArePresent(this.project._id)
|
2020-11-03 04:19:05 -05:00
|
|
|
expect(this.ProjectModel.updateOne).not.to.have.been.called
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2019-09-09 07:52:25 -04:00
|
|
|
it('should produce the tokens without error', async function() {
|
|
|
|
const tokens = await this.handler.promises.ensureTokensArePresent(
|
2019-09-30 10:46:15 -04:00
|
|
|
this.project._id
|
2019-05-29 05:21:06 -04:00
|
|
|
)
|
2019-09-09 07:52:25 -04:00
|
|
|
expect(tokens).to.deep.equal(this.project.tokens)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-06-21 09:46:09 -04:00
|
|
|
describe('when tokens are missing', function() {
|
2019-05-29 05:21:06 -04:00
|
|
|
beforeEach(function() {
|
2019-09-30 10:46:15 -04:00
|
|
|
this.project = { _id: this.project._id }
|
2019-09-09 07:52:25 -04:00
|
|
|
this.ProjectGetter.promises.getProject.resolves(this.project)
|
2019-05-29 05:21:06 -04:00
|
|
|
this.readOnlyToken = 'abc'
|
|
|
|
this.readAndWriteToken = '42def'
|
|
|
|
this.readAndWriteTokenPrefix = '42'
|
2019-09-09 07:52:25 -04:00
|
|
|
this.ProjectTokenGenerator.promises.generateUniqueReadOnlyToken.resolves(
|
|
|
|
this.readOnlyToken
|
|
|
|
)
|
|
|
|
this.ProjectTokenGenerator.readAndWriteToken.returns({
|
2019-05-29 05:21:06 -04:00
|
|
|
token: this.readAndWriteToken,
|
|
|
|
numericPrefix: this.readAndWriteTokenPrefix
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-09-09 07:52:25 -04:00
|
|
|
it('should get the project', async function() {
|
2019-09-30 10:46:15 -04:00
|
|
|
await this.handler.promises.ensureTokensArePresent(this.project._id)
|
2019-09-09 07:52:25 -04:00
|
|
|
expect(this.ProjectGetter.promises.getProject).to.have.been.calledOnce
|
|
|
|
expect(this.ProjectGetter.promises.getProject).to.have.been.calledWith(
|
2019-09-30 10:46:15 -04:00
|
|
|
this.project._id,
|
2019-09-09 07:52:25 -04:00
|
|
|
{
|
|
|
|
tokens: 1
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2019-09-09 07:52:25 -04:00
|
|
|
it('should update the project with new tokens', async function() {
|
2019-09-30 10:46:15 -04:00
|
|
|
await this.handler.promises.ensureTokensArePresent(this.project._id)
|
2019-09-09 07:52:25 -04:00
|
|
|
expect(this.ProjectTokenGenerator.promises.generateUniqueReadOnlyToken)
|
|
|
|
.to.have.been.calledOnce
|
|
|
|
expect(this.ProjectTokenGenerator.readAndWriteToken).to.have.been
|
|
|
|
.calledOnce
|
2020-11-03 04:19:05 -05:00
|
|
|
expect(this.ProjectModel.updateOne).to.have.been.calledOnce
|
|
|
|
expect(this.ProjectModel.updateOne).to.have.been.calledWith(
|
2019-09-30 10:46:15 -04:00
|
|
|
{ _id: this.project._id },
|
2019-09-09 07:52:25 -04:00
|
|
|
{
|
|
|
|
$set: {
|
|
|
|
tokens: {
|
|
|
|
readOnly: this.readOnlyToken,
|
|
|
|
readAndWrite: this.readAndWriteToken,
|
|
|
|
readAndWritePrefix: this.readAndWriteTokenPrefix
|
|
|
|
}
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2019-09-09 07:52:25 -04:00
|
|
|
it('should produce the tokens without error', async function() {
|
|
|
|
const tokens = await this.handler.promises.ensureTokensArePresent(
|
2019-09-30 10:46:15 -04:00
|
|
|
this.project._id
|
2019-05-29 05:21:06 -04:00
|
|
|
)
|
2019-09-09 07:52:25 -04:00
|
|
|
expect(tokens).to.deep.equal({
|
|
|
|
readOnly: this.readOnlyToken,
|
|
|
|
readAndWrite: this.readAndWriteToken,
|
|
|
|
readAndWritePrefix: this.readAndWriteTokenPrefix
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2019-10-14 09:22:45 -04:00
|
|
|
|
|
|
|
describe('clearTokens', function() {
|
|
|
|
it('clears the tokens from the project', async function() {
|
|
|
|
await this.handler.promises.clearTokens(this.project._id)
|
2020-11-03 04:19:05 -05:00
|
|
|
expect(this.ProjectModel.updateOne).to.have.been.calledWith(
|
2019-10-14 09:22:45 -04:00
|
|
|
{ _id: this.project._id },
|
|
|
|
{ $unset: { tokens: 1 }, $set: { publicAccesLevel: 'private' } }
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|