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')
|
|
|
|
const PrivilegeLevels = require('../../../../app/src/Features/Authorization/PrivilegeLevels')
|
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 = {
|
|
|
|
_id: ObjectId('abcdefabcdefabcdefabcdef'),
|
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-17 11:28:47 -04:00
|
|
|
_id: ObjectId('123456123456123456123456'),
|
|
|
|
email: 'collaborator@example.com'
|
2019-09-30 10:46:15 -04:00
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
this.project = {
|
2019-09-30 10:46:15 -04:00
|
|
|
_id: ObjectId('5d5dabdbb351de090cdff0b2'),
|
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
|
|
|
}
|
|
|
|
this.ProjectModel = {
|
2019-09-09 07:52:25 -04:00
|
|
|
update: sinon.stub().returns({
|
|
|
|
exec: sinon.stub().resolves()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
this.UserGetter = {
|
|
|
|
promises: {
|
|
|
|
getUser: sinon.stub().resolves(this.user)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.TpdsUpdateSender = {
|
|
|
|
promises: {
|
|
|
|
moveEntity: sinon.stub().resolves()
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
this.ProjectEntityHandler = {
|
2019-09-09 07:52:25 -04:00
|
|
|
promises: {
|
|
|
|
flushProjectToThirdPartyDataStore: sinon.stub().resolves()
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
this.CollaboratorsHandler = {
|
2019-09-09 07:52:25 -04:00
|
|
|
promises: {
|
2019-09-30 10:46:15 -04:00
|
|
|
removeUserFromProject: sinon.stub().resolves(),
|
|
|
|
addUserIdToProject: sinon.stub().resolves()
|
2019-09-09 07:52:25 -04:00
|
|
|
}
|
|
|
|
}
|
2019-10-17 11:28:47 -04:00
|
|
|
this.EmailHandler = {
|
|
|
|
promises: {
|
|
|
|
sendEmail: sinon.stub().resolves()
|
|
|
|
}
|
|
|
|
}
|
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: {
|
|
|
|
'./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
|
|
|
'./ProjectEntityHandler': this.ProjectEntityHandler,
|
|
|
|
'../Collaborators/CollaboratorsHandler': this.CollaboratorsHandler,
|
2019-10-17 11:28:47 -04:00
|
|
|
'../Email/EmailHandler': this.EmailHandler,
|
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,
|
|
|
|
'../Errors/Errors': Errors,
|
|
|
|
'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('transferOwnership', function() {
|
|
|
|
beforeEach(function() {
|
2019-10-17 11:28:47 -04:00
|
|
|
this.UserGetter.promises.getUser
|
|
|
|
.withArgs(this.user._id)
|
|
|
|
.resolves(this.user)
|
|
|
|
this.UserGetter.promises.getUser
|
|
|
|
.withArgs(this.collaborator._id)
|
|
|
|
.resolves(this.collaborator)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2019-09-09 07:52:25 -04:00
|
|
|
it("should return a not found error if the project can't be found", async function() {
|
|
|
|
this.ProjectGetter.promises.getProject.resolves(null)
|
|
|
|
await expect(
|
2019-09-30 10:46:15 -04:00
|
|
|
this.handler.promises.transferOwnership('abc', this.collaborator._id)
|
|
|
|
).to.be.rejectedWith(Errors.ProjectNotFoundError)
|
2019-09-09 07:52:25 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
it("should return a not found error if the user can't be found", async function() {
|
2019-10-17 11:28:47 -04:00
|
|
|
this.UserGetter.promises.getUser
|
|
|
|
.withArgs(this.collaborator._id)
|
|
|
|
.resolves(null)
|
2019-09-09 07:52:25 -04:00
|
|
|
await expect(
|
2019-09-30 10:46:15 -04:00
|
|
|
this.handler.promises.transferOwnership(
|
|
|
|
this.project._id,
|
|
|
|
this.collaborator._id
|
|
|
|
)
|
|
|
|
).to.be.rejectedWith(Errors.UserNotFoundError)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2019-09-09 07:52:25 -04:00
|
|
|
it('should return an error if user cannot be removed as collaborator ', async function() {
|
|
|
|
this.CollaboratorsHandler.promises.removeUserFromProject.rejects(
|
|
|
|
new Error('user-cannot-be-removed')
|
2019-05-29 05:21:06 -04:00
|
|
|
)
|
2019-09-30 10:46:15 -04:00
|
|
|
await expect(
|
|
|
|
this.handler.promises.transferOwnership(
|
|
|
|
this.project._id,
|
|
|
|
this.collaborator._id
|
|
|
|
)
|
|
|
|
).to.be.rejected
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2019-09-09 07:52:25 -04:00
|
|
|
it('should transfer ownership of the project', async function() {
|
2019-09-30 10:46:15 -04:00
|
|
|
await this.handler.promises.transferOwnership(
|
|
|
|
this.project._id,
|
|
|
|
this.collaborator._id
|
|
|
|
)
|
2019-09-09 07:52:25 -04:00
|
|
|
expect(this.ProjectModel.update).to.have.been.calledWith(
|
2019-09-30 10:46:15 -04:00
|
|
|
{ _id: this.project._id },
|
|
|
|
sinon.match({ $set: { owner_ref: this.collaborator._id } })
|
2019-09-09 07:52:25 -04:00
|
|
|
)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2019-09-30 10:46:15 -04:00
|
|
|
it('should do nothing if transferring back to the owner', async function() {
|
|
|
|
await this.handler.promises.transferOwnership(
|
|
|
|
this.project._id,
|
|
|
|
this.user._id
|
|
|
|
)
|
|
|
|
expect(this.ProjectModel.update).not.to.have.been.called
|
|
|
|
})
|
|
|
|
|
2019-09-09 07:52:25 -04:00
|
|
|
it("should remove the user from the project's collaborators", async function() {
|
2019-09-30 10:46:15 -04:00
|
|
|
await this.handler.promises.transferOwnership(
|
|
|
|
this.project._id,
|
|
|
|
this.collaborator._id
|
|
|
|
)
|
2019-09-09 07:52:25 -04:00
|
|
|
expect(
|
|
|
|
this.CollaboratorsHandler.promises.removeUserFromProject
|
2019-09-30 10:46:15 -04:00
|
|
|
).to.have.been.calledWith(this.project._id, this.collaborator._id)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2019-09-30 10:46:15 -04:00
|
|
|
it('should add the former project owner as a read/write collaborator', async function() {
|
|
|
|
await this.handler.promises.transferOwnership(
|
|
|
|
this.project._id,
|
|
|
|
this.collaborator._id
|
|
|
|
)
|
2019-09-09 07:52:25 -04:00
|
|
|
expect(
|
2019-09-30 10:46:15 -04:00
|
|
|
this.CollaboratorsHandler.promises.addUserIdToProject
|
|
|
|
).to.have.been.calledWith(
|
|
|
|
this.project._id,
|
|
|
|
this.collaborator._id,
|
|
|
|
this.user._id,
|
|
|
|
PrivilegeLevels.READ_AND_WRITE
|
|
|
|
)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2019-09-30 10:46:15 -04:00
|
|
|
it('should flush the project to tpds', async function() {
|
|
|
|
await this.handler.promises.transferOwnership(
|
|
|
|
this.project._id,
|
|
|
|
this.collaborator._id
|
2019-09-09 07:52:25 -04:00
|
|
|
)
|
2019-09-30 10:46:15 -04:00
|
|
|
expect(
|
|
|
|
this.ProjectEntityHandler.promises.flushProjectToThirdPartyDataStore
|
|
|
|
).to.have.been.calledWith(this.project._id)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2019-10-17 11:28:47 -04:00
|
|
|
it('should send an email notification', async function() {
|
|
|
|
await this.handler.promises.transferOwnership(
|
|
|
|
this.project._id,
|
|
|
|
this.collaborator._id
|
|
|
|
)
|
|
|
|
expect(this.EmailHandler.promises.sendEmail).to.have.been.calledWith(
|
|
|
|
'ownershipTransferConfirmationPreviousOwner',
|
|
|
|
{
|
|
|
|
to: this.user.email,
|
|
|
|
project: this.project,
|
|
|
|
newOwner: this.collaborator
|
|
|
|
}
|
|
|
|
)
|
|
|
|
expect(this.EmailHandler.promises.sendEmail).to.have.been.calledWith(
|
|
|
|
'ownershipTransferConfirmationNewOwner',
|
|
|
|
{
|
|
|
|
to: this.collaborator.email,
|
|
|
|
project: this.project,
|
|
|
|
previousOwner: this.user
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2019-09-30 10:46:15 -04:00
|
|
|
it('should decline to transfer ownership to a non-collaborator', async function() {
|
|
|
|
this.project.collaberator_refs = []
|
|
|
|
await expect(
|
|
|
|
this.handler.promises.transferOwnership(
|
|
|
|
this.project._id,
|
|
|
|
this.collaborator._id
|
|
|
|
)
|
|
|
|
).to.be.rejectedWith(Errors.UserNotCollaboratorError)
|
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)
|
2019-09-09 07:52:25 -04:00
|
|
|
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
|
|
|
{ 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
|
|
|
|
)
|
|
|
|
expect(this.ProjectModel.update).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)
|
2019-09-09 07:52:25 -04:00
|
|
|
expect(this.ProjectModel.update).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
|
|
|
|
expect(this.ProjectModel.update).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
|
|
|
|
.resolved
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-09-09 07:52:25 -04:00
|
|
|
describe('generateUniqueName', function() {
|
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
|
|
|
],
|
|
|
|
readAndWrite: [{ _id: 4, name: 'name2' }, { _id: 5, name: 'name22' }],
|
|
|
|
readOnly: [{ _id: 6, name: 'name3' }, { _id: 7, name: 'name33' }],
|
|
|
|
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
|
|
|
|
)
|
|
|
|
expect(this.ProjectModel.update).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
|
|
|
|
)
|
|
|
|
).to.be.resolved
|
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-09-09 07:52:25 -04:00
|
|
|
this.ProjectModel.update.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)
|
2019-09-09 07:52:25 -04:00
|
|
|
expect(this.ProjectModel.update).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
|
|
|
|
expect(this.ProjectModel.update).to.have.been.calledOnce
|
|
|
|
expect(this.ProjectModel.update).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)
|
|
|
|
expect(this.ProjectModel.update).to.have.been.calledWith(
|
|
|
|
{ _id: this.project._id },
|
|
|
|
{ $unset: { tokens: 1 }, $set: { publicAccesLevel: 'private' } }
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|