|
|
@@ -19,9 +19,11 @@ describe "TokenAccessHandler", ->
|
|
|
@req = {}
|
|
|
@TokenAccessHandler = SandboxedModule.require modulePath, requires:
|
|
|
'../../models/Project': {Project: @Project = {}}
|
|
|
- 'settings-sharelatex': {}
|
|
|
+ 'settings-sharelatex': @settings = {}
|
|
|
'../Collaborators/CollaboratorsHandler': @CollaboratorsHandler = {}
|
|
|
-
|
|
|
+ '../V1/V1Api': @V1Api = {
|
|
|
+ request: sinon.stub()
|
|
|
+ }
|
|
|
|
|
|
describe 'findProjectWithReadOnlyToken', ->
|
|
|
beforeEach ->
|
|
|
@@ -434,3 +436,46 @@ describe "TokenAccessHandler", ->
|
|
|
@TokenAccessHandler.protectTokens(@project, 'owner')
|
|
|
expect(@project.tokens.readAndWrite).to.equal 'rw'
|
|
|
expect(@project.tokens.readOnly).to.equal 'ro'
|
|
|
+
|
|
|
+ describe 'checkV1Access', ->
|
|
|
+ beforeEach ->
|
|
|
+ @callback = sinon.stub()
|
|
|
+
|
|
|
+ describe 'when v1 api not set', ->
|
|
|
+ beforeEach ->
|
|
|
+ @TokenAccessHandler.checkV1Access @token, @callback
|
|
|
+
|
|
|
+ it 'should not check access and return true', ->
|
|
|
+ expect(@V1Api.request.called).to.equal false
|
|
|
+ expect(@callback.calledWith null, true).to.equal true
|
|
|
+
|
|
|
+ describe 'when v1 api is set', ->
|
|
|
+ beforeEach ->
|
|
|
+ @settings.apis = { v1: 'v1' }
|
|
|
+
|
|
|
+ describe 'when access allowed', ->
|
|
|
+ beforeEach ->
|
|
|
+ @V1Api.request = sinon.stub().callsArgWith(1, null, {}, { allow: true} )
|
|
|
+ @TokenAccessHandler.checkV1Access @token, @callback
|
|
|
+
|
|
|
+ it 'should check api', ->
|
|
|
+ expect(@V1Api.request.calledWith { url: "/api/v1/sharelatex/docs/#{@token}/read" }).to.equal true
|
|
|
+
|
|
|
+ it 'should callback with true', ->
|
|
|
+ expect(@callback.calledWith null, true).to.equal true
|
|
|
+
|
|
|
+ describe 'when access denied', ->
|
|
|
+ beforeEach ->
|
|
|
+ @V1Api.request = sinon.stub().callsArgWith(1, null, {}, { allow: false, published_path: 'doc-url'} )
|
|
|
+ @TokenAccessHandler.checkV1Access @token, @callback
|
|
|
+
|
|
|
+ it 'should callback with false and redirect', ->
|
|
|
+ expect(@callback.calledWith null, false, 'doc-url').to.equal true
|
|
|
+
|
|
|
+ describe 'on error', ->
|
|
|
+ beforeEach ->
|
|
|
+ @V1Api.request = sinon.stub().callsArgWith(1, 'error')
|
|
|
+ @TokenAccessHandler.checkV1Access @token, @callback
|
|
|
+
|
|
|
+ it 'should callback with error', ->
|
|
|
+ expect(@callback.calledWith 'error').to.equal true
|