2014-02-12 05:23:40 -05:00
|
|
|
should = require('chai').should()
|
|
|
|
modulePath = "../../../../app/js/Features/Project/ProjectDetailsHandler"
|
2017-03-28 05:12:52 -04:00
|
|
|
Errors = require "../../../../app/js/Features/Errors/Errors"
|
2014-02-12 05:23:40 -05:00
|
|
|
SandboxedModule = require('sandboxed-module')
|
|
|
|
sinon = require('sinon')
|
|
|
|
assert = require("chai").assert
|
2017-05-19 11:21:02 -04:00
|
|
|
expect = require("chai").expect
|
2014-02-12 05:23:40 -05:00
|
|
|
require('chai').should()
|
|
|
|
|
2016-02-26 02:56:26 -05:00
|
|
|
describe 'ProjectDetailsHandler', ->
|
2014-02-12 05:23:40 -05:00
|
|
|
|
|
|
|
beforeEach ->
|
2014-03-28 08:47:15 -04:00
|
|
|
@project_id = "321l3j1kjkjl"
|
|
|
|
@user_id = "user-id-123"
|
2017-09-26 05:19:30 -04:00
|
|
|
@project =
|
2014-03-28 08:47:15 -04:00
|
|
|
name: "project"
|
|
|
|
description: "this is a great project"
|
|
|
|
something:"should not exist"
|
|
|
|
compiler: "latexxxxxx"
|
|
|
|
owner_ref: @user_id
|
|
|
|
@user =
|
|
|
|
features: "mock-features"
|
2017-09-26 05:19:30 -04:00
|
|
|
@ProjectGetter =
|
2014-03-28 08:47:15 -04:00
|
|
|
getProjectWithoutDocLines: sinon.stub().callsArgWith(1, null, @project)
|
2014-04-07 10:37:40 -04:00
|
|
|
getProject: sinon.stub().callsArgWith(2, null, @project)
|
2014-02-12 05:23:40 -05:00
|
|
|
@ProjectModel =
|
|
|
|
update: sinon.stub()
|
2014-07-16 06:56:22 -04:00
|
|
|
findOne: sinon.stub()
|
2014-03-28 08:47:15 -04:00
|
|
|
@UserGetter =
|
|
|
|
getUser: sinon.stub().callsArgWith(1, null, @user)
|
2014-04-07 10:37:40 -04:00
|
|
|
@tpdsUpdateSender =
|
|
|
|
moveEntity:sinon.stub().callsArgWith 1
|
2014-02-12 05:23:40 -05:00
|
|
|
@handler = SandboxedModule.require modulePath, requires:
|
|
|
|
"./ProjectGetter":@ProjectGetter
|
|
|
|
'../../models/Project': Project:@ProjectModel
|
2014-03-28 08:47:15 -04:00
|
|
|
"../User/UserGetter": @UserGetter
|
2014-04-07 10:37:40 -04:00
|
|
|
'../ThirdPartyDataStore/TpdsUpdateSender':@tpdsUpdateSender
|
2014-02-12 05:23:40 -05:00
|
|
|
'logger-sharelatex':
|
|
|
|
log:->
|
|
|
|
err:->
|
2017-10-04 11:31:24 -04:00
|
|
|
'./ProjectTokenGenerator': @ProjectTokenGenerator = {}
|
2014-02-12 05:23:40 -05:00
|
|
|
|
|
|
|
describe "getDetails", ->
|
|
|
|
|
2014-03-28 08:47:15 -04:00
|
|
|
it "should find the project and owner", (done)->
|
2017-09-26 05:19:30 -04:00
|
|
|
@handler.getDetails @project_id, (err, details)=>
|
2014-02-12 05:23:40 -05:00
|
|
|
details.name.should.equal @project.name
|
|
|
|
details.description.should.equal @project.description
|
|
|
|
details.compiler.should.equal @project.compiler
|
2014-03-28 08:47:15 -04:00
|
|
|
details.features.should.equal @user.features
|
2014-02-12 05:23:40 -05:00
|
|
|
assert.equal(details.something, undefined)
|
|
|
|
done()
|
|
|
|
|
2017-09-26 05:19:30 -04:00
|
|
|
it "should find overleaf metadata if it exists", (done)->
|
|
|
|
@project.overleaf = { id: 'id' }
|
|
|
|
@handler.getDetails @project_id, (err, details)=>
|
|
|
|
details.overleaf.should.equal @project.overleaf
|
|
|
|
assert.equal(details.something, undefined)
|
|
|
|
done()
|
|
|
|
|
2017-03-28 04:44:50 -04:00
|
|
|
it "should return an error for a non-existent project", (done)->
|
|
|
|
@ProjectGetter.getProject.callsArg(2, null, null)
|
2017-03-28 05:12:52 -04:00
|
|
|
err = new Errors.NotFoundError("project not found")
|
|
|
|
@handler.getDetails "0123456789012345678901234", (error, details) =>
|
|
|
|
err.should.eql error
|
2017-03-28 04:44:50 -04:00
|
|
|
done()
|
|
|
|
|
2014-02-12 05:23:40 -05:00
|
|
|
it "should return the error", (done)->
|
|
|
|
error = "some error"
|
2016-02-26 02:56:26 -05:00
|
|
|
@ProjectGetter.getProject.callsArgWith(2, error)
|
2014-02-12 05:23:40 -05:00
|
|
|
@handler.getDetails @project_id, (err)=>
|
|
|
|
err.should.equal error
|
|
|
|
done()
|
|
|
|
|
2014-07-16 06:27:47 -04:00
|
|
|
describe "getProjectDescription", ->
|
|
|
|
|
|
|
|
it "should make a call to mongo just for the description", (done)->
|
2018-02-15 07:18:43 -05:00
|
|
|
@ProjectGetter.getProject.callsArgWith(2)
|
2014-07-16 06:27:47 -04:00
|
|
|
@handler.getProjectDescription @project_id, (err, description)=>
|
2018-02-15 07:18:43 -05:00
|
|
|
@ProjectGetter.getProject
|
|
|
|
.calledWith(@project_id, description: true)
|
|
|
|
.should.equal true
|
2014-07-16 06:27:47 -04:00
|
|
|
done()
|
|
|
|
|
|
|
|
it "should return what the mongo call returns", (done)->
|
|
|
|
err = "error"
|
|
|
|
description = "cool project"
|
2018-02-15 07:18:43 -05:00
|
|
|
@ProjectGetter.getProject.callsArgWith(2, err, {description:description})
|
2014-07-16 06:27:47 -04:00
|
|
|
@handler.getProjectDescription @project_id, (returnedErr, returnedDescription)=>
|
|
|
|
err.should.equal returnedErr
|
|
|
|
description.should.equal returnedDescription
|
2017-09-26 05:19:30 -04:00
|
|
|
done()
|
2014-07-16 06:27:47 -04:00
|
|
|
|
2014-02-12 05:23:40 -05:00
|
|
|
describe "setProjectDescription", ->
|
|
|
|
|
|
|
|
beforeEach ->
|
|
|
|
@description = "updated teh description"
|
|
|
|
|
|
|
|
it "should update the project detials", (done)->
|
|
|
|
@ProjectModel.update.callsArgWith(2)
|
|
|
|
@handler.setProjectDescription @project_id, @description, =>
|
|
|
|
@ProjectModel.update.calledWith({_id:@project_id}, {description:@description}).should.equal true
|
|
|
|
done()
|
|
|
|
|
2014-07-16 06:27:47 -04:00
|
|
|
|
|
|
|
|
2014-04-07 10:37:40 -04:00
|
|
|
describe "renameProject", ->
|
|
|
|
beforeEach ->
|
2017-05-19 11:21:02 -04:00
|
|
|
@handler.validateProjectName = sinon.stub().yields()
|
2014-04-07 10:37:40 -04:00
|
|
|
@ProjectModel.update.callsArgWith(2)
|
|
|
|
@newName = "new name here"
|
|
|
|
|
|
|
|
it "should update the project with the new name", (done)->
|
|
|
|
newName = "new name here"
|
|
|
|
@handler.renameProject @project_id, @newName, =>
|
|
|
|
@ProjectModel.update.calledWith({_id: @project_id}, {name: @newName}).should.equal true
|
|
|
|
done()
|
|
|
|
|
|
|
|
it "should tell the tpdsUpdateSender", (done)->
|
|
|
|
@handler.renameProject @project_id, @newName, =>
|
|
|
|
@tpdsUpdateSender.moveEntity.calledWith({project_id:@project_id, project_name:@project.name, newProjectName:@newName}).should.equal true
|
|
|
|
done()
|
2017-09-26 05:19:30 -04:00
|
|
|
|
2017-05-19 11:21:02 -04:00
|
|
|
it "should not do anything with an invalid name", (done) ->
|
|
|
|
@handler.validateProjectName = sinon.stub().yields(new Error("invalid name"))
|
|
|
|
@handler.renameProject @project_id, @newName, =>
|
|
|
|
@tpdsUpdateSender.moveEntity.called.should.equal false
|
|
|
|
@ProjectModel.update.called.should.equal false
|
|
|
|
done()
|
2014-02-12 05:23:40 -05:00
|
|
|
|
2017-05-19 11:21:02 -04:00
|
|
|
describe "validateProjectName", ->
|
2017-10-12 11:03:12 -04:00
|
|
|
|
|
|
|
it "should reject undefined names", (done) ->
|
|
|
|
@handler.validateProjectName undefined, (error) ->
|
|
|
|
expect(error).to.exist
|
|
|
|
done()
|
|
|
|
|
2017-05-19 11:21:02 -04:00
|
|
|
it "should reject empty names", (done) ->
|
|
|
|
@handler.validateProjectName "", (error) ->
|
|
|
|
expect(error).to.exist
|
|
|
|
done()
|
|
|
|
|
2018-11-06 03:20:19 -05:00
|
|
|
it "should reject names with /s", (done) ->
|
2017-05-19 11:21:02 -04:00
|
|
|
@handler.validateProjectName "foo/bar", (error) ->
|
|
|
|
expect(error).to.exist
|
|
|
|
done()
|
|
|
|
|
2018-11-06 03:20:19 -05:00
|
|
|
it "should reject names with \\s", (done) ->
|
|
|
|
@handler.validateProjectName "foo\\bar", (error) ->
|
|
|
|
expect(error).to.exist
|
|
|
|
done()
|
|
|
|
|
2017-05-19 11:21:02 -04:00
|
|
|
it "should reject long names", (done) ->
|
|
|
|
@handler.validateProjectName new Array(1000).join("a"), (error) ->
|
|
|
|
expect(error).to.exist
|
|
|
|
done()
|
|
|
|
|
|
|
|
it "should accept normal names", (done) ->
|
|
|
|
@handler.validateProjectName "foobar", (error) ->
|
|
|
|
expect(error).to.not.exist
|
|
|
|
done()
|
2014-04-07 11:07:44 -04:00
|
|
|
|
2018-09-14 06:08:03 -04:00
|
|
|
describe "ensureProjectNameIsUnique", ->
|
|
|
|
beforeEach ->
|
|
|
|
@result = {
|
2018-11-26 11:14:49 -05:00
|
|
|
owned: [{_id: 1, name:"name"}, {_id: 2, name: "name1"}, {_id: 3, name: "name11"}, {_id: 100, name: "numeric"}]
|
2018-10-12 07:10:35 -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"}, {_id:12, name:"x".repeat(15)}]
|
2018-09-14 06:08:03 -04:00
|
|
|
}
|
2018-11-26 11:14:49 -05:00
|
|
|
for i in [1..20].concat([30..40])
|
|
|
|
@result.owned.push {_id: 100 + i, name: "numeric (#{i})"}
|
2018-09-14 06:08:03 -04:00
|
|
|
@ProjectGetter.findAllUsersProjects = sinon.stub().callsArgWith(2, null, @result)
|
|
|
|
|
|
|
|
it "should leave a unique name unchanged", (done) ->
|
|
|
|
@handler.ensureProjectNameIsUnique @user_id, "unique-name", ["-test-suffix"], (error, name, changed) ->
|
|
|
|
expect(name).to.equal "unique-name"
|
|
|
|
expect(changed).to.equal false
|
|
|
|
done()
|
|
|
|
|
|
|
|
it "should append a suffix to an existing name", (done) ->
|
|
|
|
@handler.ensureProjectNameIsUnique @user_id, "name1", ["-test-suffix"], (error, name, changed) ->
|
|
|
|
expect(name).to.equal "name1-test-suffix"
|
|
|
|
expect(changed).to.equal true
|
|
|
|
done()
|
|
|
|
|
|
|
|
it "should fallback to a second suffix when needed", (done) ->
|
|
|
|
@handler.ensureProjectNameIsUnique @user_id, "name1", ["1", "-test-suffix"], (error, name, changed) ->
|
|
|
|
expect(name).to.equal "name1-test-suffix"
|
|
|
|
expect(changed).to.equal true
|
|
|
|
done()
|
|
|
|
|
|
|
|
it "should truncate the name when append a suffix if the result is too long", (done) ->
|
|
|
|
@handler.MAX_PROJECT_NAME_LENGTH = 20
|
|
|
|
@handler.ensureProjectNameIsUnique @user_id, "x".repeat(15), ["-test-suffix"], (error, name, changed) ->
|
|
|
|
expect(name).to.equal "x".repeat(8) + "-test-suffix"
|
|
|
|
expect(changed).to.equal true
|
|
|
|
done()
|
|
|
|
|
2018-11-26 11:14:49 -05:00
|
|
|
it "should use a numeric index if no suffix is supplied", (done) ->
|
|
|
|
@handler.ensureProjectNameIsUnique @user_id, "name1", [], (error, name, changed) ->
|
|
|
|
expect(name).to.equal "name1 (1)"
|
|
|
|
expect(changed).to.equal true
|
|
|
|
done()
|
|
|
|
|
|
|
|
it "should use a numeric index if all suffixes are exhausted", (done) ->
|
|
|
|
@handler.ensureProjectNameIsUnique @user_id, "name", ["1", "11"], (error, name, changed) ->
|
|
|
|
expect(name).to.equal "name (1)"
|
|
|
|
expect(changed).to.equal true
|
|
|
|
done()
|
|
|
|
|
|
|
|
it "should find the next lowest available numeric index for the base name", (done) ->
|
|
|
|
@handler.ensureProjectNameIsUnique @user_id, "numeric", [], (error, name, changed) ->
|
|
|
|
expect(name).to.equal "numeric (21)"
|
|
|
|
expect(changed).to.equal true
|
|
|
|
done()
|
|
|
|
|
|
|
|
it "should find the next available numeric index when a numeric index is already present", (done) ->
|
|
|
|
@handler.ensureProjectNameIsUnique @user_id, "numeric (5)", [], (error, name, changed) ->
|
|
|
|
expect(name).to.equal "numeric (21)"
|
|
|
|
expect(changed).to.equal true
|
|
|
|
done()
|
|
|
|
|
|
|
|
it "should not find a numeric index lower than the one already present", (done) ->
|
|
|
|
@handler.ensureProjectNameIsUnique @user_id, "numeric (31)", [], (error, name, changed) ->
|
|
|
|
expect(name).to.equal "numeric (41)"
|
|
|
|
expect(changed).to.equal true
|
2018-09-14 06:08:03 -04:00
|
|
|
done()
|
|
|
|
|
2018-09-25 06:15:32 -04:00
|
|
|
describe "fixProjectName", ->
|
|
|
|
|
|
|
|
it "should change empty names to Untitled", () ->
|
|
|
|
expect(@handler.fixProjectName "").to.equal "Untitled"
|
|
|
|
|
|
|
|
it "should replace / with -", () ->
|
|
|
|
expect(@handler.fixProjectName "foo/bar").to.equal "foo-bar"
|
|
|
|
|
2018-11-06 03:20:19 -05:00
|
|
|
it "should replace \\ with ''", () ->
|
|
|
|
expect(@handler.fixProjectName "foo \\ bar").to.equal "foo bar"
|
|
|
|
|
2018-09-25 06:15:32 -04:00
|
|
|
it "should truncate long names", () ->
|
|
|
|
expect(@handler.fixProjectName new Array(1000).join("a")).to.equal "a".repeat(150)
|
|
|
|
|
|
|
|
it "should accept normal names", () ->
|
|
|
|
expect(@handler.fixProjectName "foobar").to.equal "foobar"
|
|
|
|
|
2018-09-14 06:08:03 -04:00
|
|
|
|
2014-04-07 11:07:44 -04:00
|
|
|
describe "setPublicAccessLevel", ->
|
|
|
|
beforeEach ->
|
|
|
|
@ProjectModel.update.callsArgWith(2)
|
|
|
|
@accessLevel = "readOnly"
|
|
|
|
|
|
|
|
it "should update the project with the new level", (done)->
|
|
|
|
@handler.setPublicAccessLevel @project_id, @accessLevel, =>
|
|
|
|
@ProjectModel.update.calledWith({_id: @project_id}, {publicAccesLevel: @accessLevel}).should.equal true
|
|
|
|
done()
|
|
|
|
|
2017-10-26 11:39:24 -04:00
|
|
|
it 'should not produce an error', (done) ->
|
|
|
|
@handler.setPublicAccessLevel @project_id, @accessLevel, (err) =>
|
|
|
|
expect(err).to.not.exist
|
|
|
|
done()
|
|
|
|
|
|
|
|
describe 'when update produces an error', ->
|
|
|
|
beforeEach ->
|
|
|
|
@ProjectModel.update.callsArgWith(2, new Error('woops'))
|
|
|
|
|
|
|
|
it 'should produce an error', (done) ->
|
|
|
|
@handler.setPublicAccessLevel @project_id, @accessLevel, (err) =>
|
|
|
|
expect(err).to.exist
|
|
|
|
expect(err).to.be.instanceof Error
|
|
|
|
done()
|
|
|
|
|
2017-10-04 11:31:24 -04:00
|
|
|
describe "ensureTokensArePresent", ->
|
|
|
|
beforeEach ->
|
|
|
|
|
|
|
|
describe 'when the project has tokens', ->
|
|
|
|
beforeEach ->
|
|
|
|
@project =
|
|
|
|
_id: @project_id
|
|
|
|
tokens:
|
|
|
|
readOnly: 'aaa'
|
|
|
|
readAndWrite: '42bbb'
|
|
|
|
@ProjectGetter.getProject = sinon.stub()
|
|
|
|
.callsArgWith(2, null, @project)
|
|
|
|
@ProjectModel.update = sinon.stub()
|
|
|
|
|
|
|
|
it 'should get the project', (done) ->
|
|
|
|
@handler.ensureTokensArePresent @project_id, (err, tokens) =>
|
|
|
|
expect(@ProjectGetter.getProject.callCount).to.equal 1
|
|
|
|
expect(@ProjectGetter.getProject.calledWith(@project_id, {tokens: 1}))
|
|
|
|
.to.equal true
|
|
|
|
done()
|
|
|
|
|
|
|
|
it 'should not update the project with new tokens', (done) ->
|
|
|
|
@handler.ensureTokensArePresent @project_id, (err, tokens) =>
|
|
|
|
expect(@ProjectModel.update.callCount).to.equal 0
|
|
|
|
done()
|
|
|
|
|
|
|
|
it 'should produce the tokens without error', (done) ->
|
|
|
|
@handler.ensureTokensArePresent @project_id, (err, tokens) =>
|
|
|
|
expect(err).to.not.exist
|
|
|
|
expect(tokens).to.deep.equal @project.tokens
|
|
|
|
done()
|
|
|
|
|
|
|
|
describe 'when tokens are missing', ->
|
|
|
|
beforeEach ->
|
|
|
|
@project =
|
|
|
|
_id: @project_id
|
|
|
|
@ProjectGetter.getProject = sinon.stub()
|
|
|
|
.callsArgWith(2, null, @project)
|
|
|
|
@readOnlyToken = 'abc'
|
|
|
|
@readAndWriteToken = '42def'
|
|
|
|
@ProjectTokenGenerator.readOnlyToken = sinon.stub().returns(@readOnlyToken)
|
|
|
|
@ProjectTokenGenerator.readAndWriteToken = sinon.stub().returns(@readAndWriteToken)
|
|
|
|
@ProjectModel.update = sinon.stub()
|
|
|
|
.callsArgWith(2, null)
|
|
|
|
|
|
|
|
it 'should get the project', (done) ->
|
|
|
|
@handler.ensureTokensArePresent @project_id, (err, tokens) =>
|
|
|
|
expect(@ProjectGetter.getProject.callCount).to.equal 1
|
|
|
|
expect(@ProjectGetter.getProject.calledWith(@project_id, {tokens: 1}))
|
|
|
|
.to.equal true
|
|
|
|
done()
|
|
|
|
|
|
|
|
it 'should update the project with new tokens', (done) ->
|
|
|
|
@handler.ensureTokensArePresent @project_id, (err, tokens) =>
|
|
|
|
expect(@ProjectTokenGenerator.readOnlyToken.callCount)
|
|
|
|
.to.equal 1
|
|
|
|
expect(@ProjectTokenGenerator.readAndWriteToken.callCount)
|
|
|
|
.to.equal 1
|
|
|
|
expect(@ProjectModel.update.callCount).to.equal 1
|
|
|
|
expect(@ProjectModel.update.calledWith(
|
|
|
|
{_id: @project_id},
|
|
|
|
{$set: {tokens: {readOnly: @readOnlyToken, readAndWrite: @readAndWriteToken}}}
|
|
|
|
)).to.equal true
|
|
|
|
done()
|
|
|
|
|
|
|
|
it 'should produce the tokens without error', (done) ->
|
|
|
|
@handler.ensureTokensArePresent @project_id, (err, tokens) =>
|
|
|
|
expect(err).to.not.exist
|
|
|
|
expect(tokens).to.deep.equal {
|
|
|
|
readOnly: @readOnlyToken,
|
|
|
|
readAndWrite: @readAndWriteToken
|
|
|
|
}
|
|
|
|
done()
|