2014-11-12 10:54:55 -05:00
|
|
|
chai = require "chai"
|
|
|
|
chai.should()
|
|
|
|
expect = chai.expect
|
|
|
|
sinon = require("sinon")
|
|
|
|
SandboxedModule = require('sandboxed-module')
|
|
|
|
path = require "path"
|
|
|
|
modulePath = '../../../app/js/AuthorizationManager'
|
|
|
|
|
|
|
|
describe 'AuthorizationManager', ->
|
|
|
|
beforeEach ->
|
|
|
|
@client =
|
2020-02-24 08:32:20 -05:00
|
|
|
ol_context: {}
|
2016-09-02 11:35:00 -04:00
|
|
|
|
2014-11-12 10:54:55 -05:00
|
|
|
@AuthorizationManager = SandboxedModule.require modulePath, requires: {}
|
|
|
|
|
|
|
|
describe "assertClientCanViewProject", ->
|
|
|
|
it "should allow the readOnly privilegeLevel", (done) ->
|
2020-02-24 08:32:20 -05:00
|
|
|
@client.ol_context.privilege_level = "readOnly"
|
2014-11-12 10:54:55 -05:00
|
|
|
@AuthorizationManager.assertClientCanViewProject @client, (error) ->
|
|
|
|
expect(error).to.be.null
|
|
|
|
done()
|
2020-05-01 06:43:18 -04:00
|
|
|
|
2014-11-12 10:54:55 -05:00
|
|
|
it "should allow the readAndWrite privilegeLevel", (done) ->
|
2020-02-24 08:32:20 -05:00
|
|
|
@client.ol_context.privilege_level = "readAndWrite"
|
2014-11-12 10:54:55 -05:00
|
|
|
@AuthorizationManager.assertClientCanViewProject @client, (error) ->
|
|
|
|
expect(error).to.be.null
|
|
|
|
done()
|
2020-05-01 06:43:18 -04:00
|
|
|
|
2014-11-12 10:54:55 -05:00
|
|
|
it "should allow the owner privilegeLevel", (done) ->
|
2020-02-24 08:32:20 -05:00
|
|
|
@client.ol_context.privilege_level = "owner"
|
2014-11-12 10:54:55 -05:00
|
|
|
@AuthorizationManager.assertClientCanViewProject @client, (error) ->
|
|
|
|
expect(error).to.be.null
|
|
|
|
done()
|
2020-05-01 06:43:18 -04:00
|
|
|
|
2014-11-12 10:54:55 -05:00
|
|
|
it "should return an error with any other privilegeLevel", (done) ->
|
2020-02-24 08:32:20 -05:00
|
|
|
@client.ol_context.privilege_level = "unknown"
|
2014-11-12 10:54:55 -05:00
|
|
|
@AuthorizationManager.assertClientCanViewProject @client, (error) ->
|
2014-11-13 12:07:05 -05:00
|
|
|
error.message.should.equal "not authorized"
|
|
|
|
done()
|
|
|
|
|
|
|
|
describe "assertClientCanEditProject", ->
|
|
|
|
it "should not allow the readOnly privilegeLevel", (done) ->
|
2020-02-24 08:32:20 -05:00
|
|
|
@client.ol_context.privilege_level = "readOnly"
|
2014-11-13 12:07:05 -05:00
|
|
|
@AuthorizationManager.assertClientCanEditProject @client, (error) ->
|
|
|
|
error.message.should.equal "not authorized"
|
|
|
|
done()
|
2020-05-01 06:43:18 -04:00
|
|
|
|
2014-11-13 12:07:05 -05:00
|
|
|
it "should allow the readAndWrite privilegeLevel", (done) ->
|
2020-02-24 08:32:20 -05:00
|
|
|
@client.ol_context.privilege_level = "readAndWrite"
|
2014-11-13 12:07:05 -05:00
|
|
|
@AuthorizationManager.assertClientCanEditProject @client, (error) ->
|
|
|
|
expect(error).to.be.null
|
|
|
|
done()
|
2020-05-01 06:43:18 -04:00
|
|
|
|
2014-11-13 12:07:05 -05:00
|
|
|
it "should allow the owner privilegeLevel", (done) ->
|
2020-02-24 08:32:20 -05:00
|
|
|
@client.ol_context.privilege_level = "owner"
|
2014-11-13 12:07:05 -05:00
|
|
|
@AuthorizationManager.assertClientCanEditProject @client, (error) ->
|
|
|
|
expect(error).to.be.null
|
|
|
|
done()
|
2020-05-01 06:43:18 -04:00
|
|
|
|
2014-11-13 12:07:05 -05:00
|
|
|
it "should return an error with any other privilegeLevel", (done) ->
|
2020-02-24 08:32:20 -05:00
|
|
|
@client.ol_context.privilege_level = "unknown"
|
2014-11-13 12:07:05 -05:00
|
|
|
@AuthorizationManager.assertClientCanEditProject @client, (error) ->
|
2014-11-12 10:54:55 -05:00
|
|
|
error.message.should.equal "not authorized"
|
2016-09-02 11:35:00 -04:00
|
|
|
done()
|
|
|
|
|
|
|
|
# check doc access for project
|
|
|
|
|
|
|
|
describe "assertClientCanViewProjectAndDoc", ->
|
|
|
|
beforeEach () ->
|
|
|
|
@doc_id = "12345"
|
|
|
|
@callback = sinon.stub()
|
2020-02-24 08:32:20 -05:00
|
|
|
@client.ol_context = {}
|
2016-09-02 11:35:00 -04:00
|
|
|
|
|
|
|
describe "when not authorised at the project level", ->
|
|
|
|
beforeEach () ->
|
2020-02-24 08:32:20 -05:00
|
|
|
@client.ol_context.privilege_level = "unknown"
|
2016-09-02 11:35:00 -04:00
|
|
|
|
|
|
|
it "should not allow access", () ->
|
2020-05-01 06:43:18 -04:00
|
|
|
@AuthorizationManager.assertClientCanViewProjectAndDoc @client, @doc_id, (err) ->
|
|
|
|
err.message.should.equal "not authorized"
|
2016-09-02 11:35:00 -04:00
|
|
|
|
|
|
|
describe "even when authorised at the doc level", ->
|
|
|
|
beforeEach (done) ->
|
|
|
|
@AuthorizationManager.addAccessToDoc @client, @doc_id, done
|
|
|
|
|
|
|
|
it "should not allow access", () ->
|
2020-05-01 06:43:18 -04:00
|
|
|
@AuthorizationManager.assertClientCanViewProjectAndDoc @client, @doc_id, (err) ->
|
|
|
|
err.message.should.equal "not authorized"
|
2016-09-02 11:35:00 -04:00
|
|
|
|
|
|
|
describe "when authorised at the project level", ->
|
|
|
|
beforeEach () ->
|
2020-02-24 08:32:20 -05:00
|
|
|
@client.ol_context.privilege_level = "readOnly"
|
2016-09-02 11:35:00 -04:00
|
|
|
|
|
|
|
describe "and not authorised at the document level", ->
|
|
|
|
it "should not allow access", () ->
|
2020-05-01 06:43:18 -04:00
|
|
|
@AuthorizationManager.assertClientCanViewProjectAndDoc @client, @doc_id, (err) ->
|
|
|
|
err.message.should.equal "not authorized"
|
2016-09-02 11:35:00 -04:00
|
|
|
|
|
|
|
describe "and authorised at the document level", ->
|
|
|
|
beforeEach (done) ->
|
|
|
|
@AuthorizationManager.addAccessToDoc @client, @doc_id, done
|
|
|
|
|
|
|
|
it "should allow access", () ->
|
|
|
|
@AuthorizationManager.assertClientCanViewProjectAndDoc @client, @doc_id, @callback
|
|
|
|
@callback
|
|
|
|
.calledWith(null)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
describe "when document authorisation is added and then removed", ->
|
|
|
|
beforeEach (done) ->
|
|
|
|
@AuthorizationManager.addAccessToDoc @client, @doc_id, () =>
|
|
|
|
@AuthorizationManager.removeAccessToDoc @client, @doc_id, done
|
|
|
|
|
|
|
|
it "should deny access", () ->
|
2020-05-01 06:43:18 -04:00
|
|
|
@AuthorizationManager.assertClientCanViewProjectAndDoc @client, @doc_id, (err) ->
|
|
|
|
err.message.should.equal "not authorized"
|
2016-09-02 11:35:00 -04:00
|
|
|
|
|
|
|
describe "assertClientCanEditProjectAndDoc", ->
|
|
|
|
beforeEach () ->
|
|
|
|
@doc_id = "12345"
|
|
|
|
@callback = sinon.stub()
|
2020-02-24 08:32:20 -05:00
|
|
|
@client.ol_context = {}
|
2016-09-02 11:35:00 -04:00
|
|
|
|
|
|
|
describe "when not authorised at the project level", ->
|
|
|
|
beforeEach () ->
|
2020-02-24 08:32:20 -05:00
|
|
|
@client.ol_context.privilege_level = "readOnly"
|
2016-09-02 11:35:00 -04:00
|
|
|
|
|
|
|
it "should not allow access", () ->
|
2020-05-01 06:43:18 -04:00
|
|
|
@AuthorizationManager.assertClientCanEditProjectAndDoc @client, @doc_id, (err) ->
|
|
|
|
err.message.should.equal "not authorized"
|
2016-09-02 11:35:00 -04:00
|
|
|
|
|
|
|
describe "even when authorised at the doc level", ->
|
|
|
|
beforeEach (done) ->
|
|
|
|
@AuthorizationManager.addAccessToDoc @client, @doc_id, done
|
|
|
|
|
|
|
|
it "should not allow access", () ->
|
2020-05-01 06:43:18 -04:00
|
|
|
@AuthorizationManager.assertClientCanEditProjectAndDoc @client, @doc_id, (err) ->
|
|
|
|
err.message.should.equal "not authorized"
|
2016-09-02 11:35:00 -04:00
|
|
|
|
|
|
|
describe "when authorised at the project level", ->
|
|
|
|
beforeEach () ->
|
2020-02-24 08:32:20 -05:00
|
|
|
@client.ol_context.privilege_level = "readAndWrite"
|
2016-09-02 11:35:00 -04:00
|
|
|
|
|
|
|
describe "and not authorised at the document level", ->
|
|
|
|
it "should not allow access", () ->
|
2020-05-01 06:43:18 -04:00
|
|
|
@AuthorizationManager.assertClientCanEditProjectAndDoc @client, @doc_id, (err) ->
|
|
|
|
err.message.should.equal "not authorized"
|
2016-09-02 11:35:00 -04:00
|
|
|
|
|
|
|
describe "and authorised at the document level", ->
|
|
|
|
beforeEach (done) ->
|
|
|
|
@AuthorizationManager.addAccessToDoc @client, @doc_id, done
|
|
|
|
|
|
|
|
it "should allow access", () ->
|
|
|
|
@AuthorizationManager.assertClientCanEditProjectAndDoc @client, @doc_id, @callback
|
|
|
|
@callback
|
|
|
|
.calledWith(null)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
describe "when document authorisation is added and then removed", ->
|
|
|
|
beforeEach (done) ->
|
|
|
|
@AuthorizationManager.addAccessToDoc @client, @doc_id, () =>
|
|
|
|
@AuthorizationManager.removeAccessToDoc @client, @doc_id, done
|
|
|
|
|
|
|
|
it "should deny access", () ->
|
2020-05-01 06:43:18 -04:00
|
|
|
@AuthorizationManager.assertClientCanEditProjectAndDoc @client, @doc_id, (err) ->
|
|
|
|
err.message.should.equal "not authorized"
|