Generate tokens on old projects if they're not present
This commit is contained in:
@@ -564,20 +564,99 @@ describe "EditorController", ->
|
||||
|
||||
describe "setPublicAccessLevel", ->
|
||||
|
||||
beforeEach ->
|
||||
@newAccessLevel = "public"
|
||||
@ProjectDetailsHandler.setPublicAccessLevel = sinon.stub().callsArgWith(2, null)
|
||||
@EditorRealTimeController.emitToRoom = sinon.stub()
|
||||
describe 'when setting to private', ->
|
||||
beforeEach ->
|
||||
@newAccessLevel = 'private'
|
||||
@ProjectDetailsHandler.setPublicAccessLevel = sinon.stub().callsArgWith(2, null)
|
||||
@ProjectDetailsHandler.ensureTokensArePresent = sinon.stub()
|
||||
.callsArgWith(1, null, @tokens)
|
||||
@EditorRealTimeController.emitToRoom = sinon.stub()
|
||||
|
||||
it "should call the EditorController", (done)->
|
||||
@EditorController.setPublicAccessLevel @project_id, @newAccessLevel, =>
|
||||
@ProjectDetailsHandler.setPublicAccessLevel.calledWith(@project_id, @newAccessLevel).should.equal true
|
||||
done()
|
||||
it 'should set the access level', (done) ->
|
||||
@EditorController.setPublicAccessLevel @project_id, @newAccessLevel, =>
|
||||
@ProjectDetailsHandler.setPublicAccessLevel
|
||||
.calledWith(@project_id, @newAccessLevel).should.equal true
|
||||
done()
|
||||
|
||||
it "should emit the update to the room", (done)->
|
||||
@EditorController.setPublicAccessLevel @project_id, @newAccessLevel, =>
|
||||
@EditorRealTimeController.emitToRoom.calledWith(@project_id, 'publicAccessLevelUpdated', @newAccessLevel).should.equal true
|
||||
done()
|
||||
it 'should broadcast the access level change', (done) ->
|
||||
@EditorController.setPublicAccessLevel @project_id, @newAccessLevel, =>
|
||||
@EditorRealTimeController.emitToRoom
|
||||
.calledWith(@project_id, 'project:publicAccessLevel:changed').should.equal true
|
||||
done()
|
||||
|
||||
it 'should not ensure tokens are present for project', (done) ->
|
||||
@EditorController.setPublicAccessLevel @project_id, @newAccessLevel, =>
|
||||
@ProjectDetailsHandler.ensureTokensArePresent
|
||||
.calledWith(@project_id).should.equal false
|
||||
done()
|
||||
|
||||
it 'should not broadcast a token change', (done) ->
|
||||
@EditorController.setPublicAccessLevel @project_id, @newAccessLevel, =>
|
||||
@EditorRealTimeController.emitToRoom
|
||||
.calledWith(@project_id, 'project:tokens:changed', {tokens: @tokens})
|
||||
.should.equal false
|
||||
done()
|
||||
|
||||
it 'should not produce an error', (done) ->
|
||||
@EditorController.setPublicAccessLevel @project_id, @newAccessLevel, (err) =>
|
||||
expect(err).to.not.exist
|
||||
done()
|
||||
|
||||
describe 'when setting to tokenBased', ->
|
||||
beforeEach ->
|
||||
@newAccessLevel = 'tokenBased'
|
||||
@tokens = {readOnly: 'aaa', readAndWrite: '42bbb'}
|
||||
@ProjectDetailsHandler.setPublicAccessLevel = sinon.stub()
|
||||
.callsArgWith(2, null)
|
||||
@ProjectDetailsHandler.ensureTokensArePresent = sinon.stub()
|
||||
.callsArgWith(1, null, @tokens)
|
||||
@EditorRealTimeController.emitToRoom = sinon.stub()
|
||||
|
||||
it 'should set the access level', (done) ->
|
||||
@EditorController.setPublicAccessLevel @project_id, @newAccessLevel, =>
|
||||
@ProjectDetailsHandler.setPublicAccessLevel
|
||||
.calledWith(@project_id, @newAccessLevel).should.equal true
|
||||
done()
|
||||
|
||||
it 'should broadcast the access level change', (done) ->
|
||||
@EditorController.setPublicAccessLevel @project_id, @newAccessLevel, =>
|
||||
@EditorRealTimeController.emitToRoom
|
||||
.calledWith(@project_id, 'project:publicAccessLevel:changed')
|
||||
.should.equal true
|
||||
done()
|
||||
|
||||
it 'should ensure tokens are present for project', (done) ->
|
||||
@EditorController.setPublicAccessLevel @project_id, @newAccessLevel, =>
|
||||
@ProjectDetailsHandler.ensureTokensArePresent
|
||||
.calledWith(@project_id).should.equal true
|
||||
done()
|
||||
|
||||
it 'should broadcast the token change too', (done) ->
|
||||
@EditorController.setPublicAccessLevel @project_id, @newAccessLevel, =>
|
||||
@EditorRealTimeController.emitToRoom
|
||||
.calledWith(@project_id, 'project:tokens:changed', {tokens: @tokens})
|
||||
.should.equal true
|
||||
done()
|
||||
|
||||
it 'should not produce an error', (done) ->
|
||||
@EditorController.setPublicAccessLevel @project_id, @newAccessLevel, (err) =>
|
||||
expect(err).to.not.exist
|
||||
done()
|
||||
|
||||
# beforeEach ->
|
||||
# @newAccessLevel = "public"
|
||||
# @ProjectDetailsHandler.setPublicAccessLevel = sinon.stub().callsArgWith(2, null)
|
||||
# @EditorRealTimeController.emitToRoom = sinon.stub()
|
||||
|
||||
# it "should call the EditorController", (done)->
|
||||
# @EditorController.setPublicAccessLevel @project_id, @newAccessLevel, =>
|
||||
# @ProjectDetailsHandler.setPublicAccessLevel.calledWith(@project_id, @newAccessLevel).should.equal true
|
||||
# done()
|
||||
|
||||
# it "should emit the update to the room", (done)->
|
||||
# @EditorController.setPublicAccessLevel @project_id, @newAccessLevel, =>
|
||||
# @EditorRealTimeController.emitToRoom.calledWith(@project_id, 'publicAccessLevelUpdated', @newAccessLevel).should.equal true
|
||||
# done()
|
||||
|
||||
describe "setRootDoc", ->
|
||||
|
||||
@@ -594,4 +673,4 @@ describe "EditorController", ->
|
||||
it "should emit the update to the room", (done)->
|
||||
@EditorController.setRootDoc @project_id, @newRootDocID, =>
|
||||
@EditorRealTimeController.emitToRoom.calledWith(@project_id, 'rootDocUpdated', @newRootDocID).should.equal true
|
||||
done()
|
||||
done()
|
||||
|
||||
@@ -38,6 +38,7 @@ describe 'ProjectDetailsHandler', ->
|
||||
'logger-sharelatex':
|
||||
log:->
|
||||
err:->
|
||||
'./ProjectTokenGenerator': @ProjectTokenGenerator = {}
|
||||
|
||||
describe "getDetails", ->
|
||||
|
||||
@@ -149,3 +150,76 @@ describe 'ProjectDetailsHandler', ->
|
||||
@ProjectModel.update.calledWith({_id: @project_id}, {publicAccesLevel: @accessLevel}).should.equal true
|
||||
done()
|
||||
|
||||
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()
|
||||
|
||||
Reference in New Issue
Block a user