|
|
@@ -5,7 +5,6 @@ expect = chai.expect
|
|
|
modulePath = "../../../../app/js/Features/Authorization/AuthorizationManager.js"
|
|
|
SandboxedModule = require('sandboxed-module')
|
|
|
Errors = require "../../../../app/js/Features/Errors/Errors.js"
|
|
|
-MockRequest = require '../helpers/MockRequest'
|
|
|
|
|
|
describe "AuthorizationManager", ->
|
|
|
beforeEach ->
|
|
|
@@ -15,10 +14,11 @@ describe "AuthorizationManager", ->
|
|
|
"../../models/User": User: @User = {}
|
|
|
"../Errors/Errors": Errors
|
|
|
"../TokenAccess/TokenAccessHandler": @TokenAccessHandler = {
|
|
|
- requestHasReadOnlyTokenAccess: sinon.stub().callsArgWith(2, null, false)
|
|
|
+ isValidReadOnlyToken: sinon.stub().callsArgWith(2, null, false)
|
|
|
}
|
|
|
@user_id = "user-id-1"
|
|
|
@project_id = "project-id-1"
|
|
|
+ @token = 'some-token'
|
|
|
@callback = sinon.stub()
|
|
|
|
|
|
describe "getPrivilegeLevelForProject", ->
|
|
|
@@ -29,7 +29,6 @@ describe "AuthorizationManager", ->
|
|
|
|
|
|
describe "with a private project", ->
|
|
|
beforeEach ->
|
|
|
- @req = new MockRequest()
|
|
|
@Project.findOne
|
|
|
.withArgs({ _id: @project_id }, { publicAccesLevel: 1 })
|
|
|
.yields(null, { publicAccesLevel: "private" })
|
|
|
@@ -40,7 +39,7 @@ describe "AuthorizationManager", ->
|
|
|
@CollaboratorsHandler.getMemberIdPrivilegeLevel
|
|
|
.withArgs(@user_id, @project_id)
|
|
|
.yields(null, "readOnly")
|
|
|
- @AuthorizationManager.getPrivilegeLevelForProject @req, @user_id, @project_id, @callback
|
|
|
+ @AuthorizationManager.getPrivilegeLevelForProject @user_id, @project_id, @token, @callback
|
|
|
|
|
|
it "should return the user's privilege level", ->
|
|
|
@callback.calledWith(null, "readOnly", false).should.equal true
|
|
|
@@ -51,7 +50,7 @@ describe "AuthorizationManager", ->
|
|
|
@CollaboratorsHandler.getMemberIdPrivilegeLevel
|
|
|
.withArgs(@user_id, @project_id)
|
|
|
.yields(null, false)
|
|
|
- @AuthorizationManager.getPrivilegeLevelForProject @req, @user_id, @project_id, @callback
|
|
|
+ @AuthorizationManager.getPrivilegeLevelForProject @user_id, @project_id, @token, @callback
|
|
|
|
|
|
it "should return false", ->
|
|
|
@callback.calledWith(null, false, false).should.equal true
|
|
|
@@ -62,14 +61,14 @@ describe "AuthorizationManager", ->
|
|
|
@CollaboratorsHandler.getMemberIdPrivilegeLevel
|
|
|
.withArgs(@user_id, @project_id)
|
|
|
.yields(null, false)
|
|
|
- @AuthorizationManager.getPrivilegeLevelForProject @req, @user_id, @project_id, @callback
|
|
|
+ @AuthorizationManager.getPrivilegeLevelForProject @user_id, @project_id, @token, @callback
|
|
|
|
|
|
it "should return the user as an owner", ->
|
|
|
@callback.calledWith(null, "owner", false).should.equal true
|
|
|
|
|
|
describe "with no user (anonymous)", ->
|
|
|
beforeEach ->
|
|
|
- @AuthorizationManager.getPrivilegeLevelForProject @req, null, @project_id, @callback
|
|
|
+ @AuthorizationManager.getPrivilegeLevelForProject null, @project_id, @token, @callback
|
|
|
|
|
|
it "should not call CollaboratorsHandler.getMemberIdPrivilegeLevel", ->
|
|
|
@CollaboratorsHandler.getMemberIdPrivilegeLevel.called.should.equal false
|
|
|
@@ -92,7 +91,7 @@ describe "AuthorizationManager", ->
|
|
|
@CollaboratorsHandler.getMemberIdPrivilegeLevel
|
|
|
.withArgs(@user_id, @project_id)
|
|
|
.yields(null, "readOnly")
|
|
|
- @AuthorizationManager.getPrivilegeLevelForProject @req, @user_id, @project_id, @callback
|
|
|
+ @AuthorizationManager.getPrivilegeLevelForProject @user_id, @project_id, @token, @callback
|
|
|
|
|
|
it "should return the user's privilege level", ->
|
|
|
@callback.calledWith(null, "readOnly", false).should.equal true
|
|
|
@@ -103,7 +102,7 @@ describe "AuthorizationManager", ->
|
|
|
@CollaboratorsHandler.getMemberIdPrivilegeLevel
|
|
|
.withArgs(@user_id, @project_id)
|
|
|
.yields(null, false)
|
|
|
- @AuthorizationManager.getPrivilegeLevelForProject @req, @user_id, @project_id, @callback
|
|
|
+ @AuthorizationManager.getPrivilegeLevelForProject @user_id, @project_id, @token, @callback
|
|
|
|
|
|
it "should return the public privilege level", ->
|
|
|
@callback.calledWith(null, "readAndWrite", true).should.equal true
|
|
|
@@ -114,14 +113,14 @@ describe "AuthorizationManager", ->
|
|
|
@CollaboratorsHandler.getMemberIdPrivilegeLevel
|
|
|
.withArgs(@user_id, @project_id)
|
|
|
.yields(null, false)
|
|
|
- @AuthorizationManager.getPrivilegeLevelForProject @req, @user_id, @project_id, @callback
|
|
|
+ @AuthorizationManager.getPrivilegeLevelForProject @user_id, @project_id, @token, @callback
|
|
|
|
|
|
it "should return the user as an owner", ->
|
|
|
@callback.calledWith(null, "owner", false).should.equal true
|
|
|
|
|
|
describe "with no user (anonymous)", ->
|
|
|
beforeEach ->
|
|
|
- @AuthorizationManager.getPrivilegeLevelForProject @req, null, @project_id, @callback
|
|
|
+ @AuthorizationManager.getPrivilegeLevelForProject null, @project_id, @token, @callback
|
|
|
|
|
|
it "should not call CollaboratorsHandler.getMemberIdPrivilegeLevel", ->
|
|
|
@CollaboratorsHandler.getMemberIdPrivilegeLevel.called.should.equal false
|
|
|
@@ -139,7 +138,7 @@ describe "AuthorizationManager", ->
|
|
|
.yields(null, null)
|
|
|
|
|
|
it "should return a NotFoundError", ->
|
|
|
- @AuthorizationManager.getPrivilegeLevelForProject @req, @user_id, @project_id, (error) ->
|
|
|
+ @AuthorizationManager.getPrivilegeLevelForProject @user_id, @project_id, @token, (error) ->
|
|
|
error.should.be.instanceof Errors.NotFoundError
|
|
|
|
|
|
describe "when the project id is not valid", ->
|
|
|
@@ -150,215 +149,211 @@ describe "AuthorizationManager", ->
|
|
|
.yields(null, "readOnly")
|
|
|
|
|
|
it "should return a error", (done)->
|
|
|
- @AuthorizationManager.getPrivilegeLevelForProject @req, undefined, "not project id", (err) =>
|
|
|
+ @AuthorizationManager.getPrivilegeLevelForProject undefined, "not project id", @token, (err) =>
|
|
|
@Project.findOne.called.should.equal false
|
|
|
expect(err).to.exist
|
|
|
done()
|
|
|
|
|
|
describe "canUserReadProject", ->
|
|
|
beforeEach ->
|
|
|
- @req = new MockRequest()
|
|
|
@AuthorizationManager.getPrivilegeLevelForProject = sinon.stub()
|
|
|
|
|
|
describe "when user is owner", ->
|
|
|
beforeEach ->
|
|
|
@AuthorizationManager.getPrivilegeLevelForProject
|
|
|
- .withArgs(@req, @user_id, @project_id)
|
|
|
+ .withArgs(@user_id, @project_id, @token)
|
|
|
.yields(null, "owner", false)
|
|
|
|
|
|
it "should return true", (done) ->
|
|
|
- @AuthorizationManager.canUserReadProject @req, @user_id, @project_id, (error, canRead) ->
|
|
|
+ @AuthorizationManager.canUserReadProject @user_id, @project_id, @token, (error, canRead) ->
|
|
|
expect(canRead).to.equal true
|
|
|
done()
|
|
|
|
|
|
describe "when user has read-write access", ->
|
|
|
beforeEach ->
|
|
|
@AuthorizationManager.getPrivilegeLevelForProject
|
|
|
- .withArgs(@req, @user_id, @project_id)
|
|
|
+ .withArgs(@user_id, @project_id, @token)
|
|
|
.yields(null, "readAndWrite", false)
|
|
|
|
|
|
it "should return true", (done) ->
|
|
|
- @AuthorizationManager.canUserReadProject @req, @user_id, @project_id, (error, canRead) ->
|
|
|
+ @AuthorizationManager.canUserReadProject @user_id, @project_id, @token, (error, canRead) ->
|
|
|
expect(canRead).to.equal true
|
|
|
done()
|
|
|
|
|
|
describe "when user has read-only access", ->
|
|
|
beforeEach ->
|
|
|
@AuthorizationManager.getPrivilegeLevelForProject
|
|
|
- .withArgs(@req, @user_id, @project_id)
|
|
|
+ .withArgs(@user_id, @project_id, @token)
|
|
|
.yields(null, "readOnly", false)
|
|
|
|
|
|
it "should return true", (done) ->
|
|
|
- @AuthorizationManager.canUserReadProject @req, @user_id, @project_id, (error, canRead) ->
|
|
|
+ @AuthorizationManager.canUserReadProject @user_id, @project_id, @token, (error, canRead) ->
|
|
|
expect(canRead).to.equal true
|
|
|
done()
|
|
|
|
|
|
describe "when user has no access", ->
|
|
|
beforeEach ->
|
|
|
@AuthorizationManager.getPrivilegeLevelForProject
|
|
|
- .withArgs(@req, @user_id, @project_id)
|
|
|
+ .withArgs(@user_id, @project_id, @token)
|
|
|
.yields(null, false, false)
|
|
|
|
|
|
it "should return false", (done) ->
|
|
|
- @AuthorizationManager.canUserReadProject @req, @user_id, @project_id, (error, canRead) ->
|
|
|
+ @AuthorizationManager.canUserReadProject @user_id, @project_id, @token, (error, canRead) ->
|
|
|
expect(canRead).to.equal false
|
|
|
done()
|
|
|
|
|
|
describe "canUserWriteProjectContent", ->
|
|
|
beforeEach ->
|
|
|
- @req = new MockRequest()
|
|
|
@AuthorizationManager.getPrivilegeLevelForProject = sinon.stub()
|
|
|
|
|
|
describe "when user is owner", ->
|
|
|
beforeEach ->
|
|
|
@AuthorizationManager.getPrivilegeLevelForProject
|
|
|
- .withArgs(@req, @user_id, @project_id)
|
|
|
+ .withArgs(@user_id, @project_id, @token)
|
|
|
.yields(null, "owner", false)
|
|
|
|
|
|
it "should return true", (done) ->
|
|
|
- @AuthorizationManager.canUserWriteProjectContent @req, @user_id, @project_id, (error, canWrite) ->
|
|
|
+ @AuthorizationManager.canUserWriteProjectContent @user_id, @project_id, @token, (error, canWrite) ->
|
|
|
expect(canWrite).to.equal true
|
|
|
done()
|
|
|
|
|
|
describe "when user has read-write access", ->
|
|
|
beforeEach ->
|
|
|
@AuthorizationManager.getPrivilegeLevelForProject
|
|
|
- .withArgs(@req, @user_id, @project_id)
|
|
|
+ .withArgs(@user_id, @project_id, @token)
|
|
|
.yields(null, "readAndWrite", false)
|
|
|
|
|
|
it "should return true", (done) ->
|
|
|
- @AuthorizationManager.canUserWriteProjectContent @req, @user_id, @project_id, (error, canWrite) ->
|
|
|
+ @AuthorizationManager.canUserWriteProjectContent @user_id, @project_id, @token, (error, canWrite) ->
|
|
|
expect(canWrite).to.equal true
|
|
|
done()
|
|
|
|
|
|
describe "when user has read-only access", ->
|
|
|
beforeEach ->
|
|
|
@AuthorizationManager.getPrivilegeLevelForProject
|
|
|
- .withArgs(@req, @user_id, @project_id)
|
|
|
+ .withArgs(@user_id, @project_id, @token)
|
|
|
.yields(null, "readOnly", false)
|
|
|
|
|
|
it "should return false", (done) ->
|
|
|
- @AuthorizationManager.canUserWriteProjectContent @req, @user_id, @project_id, (error, canWrite) ->
|
|
|
+ @AuthorizationManager.canUserWriteProjectContent @user_id, @project_id, @token, (error, canWrite) ->
|
|
|
expect(canWrite).to.equal false
|
|
|
done()
|
|
|
|
|
|
describe "when user has no access", ->
|
|
|
beforeEach ->
|
|
|
@AuthorizationManager.getPrivilegeLevelForProject
|
|
|
- .withArgs(@req, @user_id, @project_id)
|
|
|
+ .withArgs(@user_id, @project_id, @token)
|
|
|
.yields(null, false, false)
|
|
|
|
|
|
it "should return false", (done) ->
|
|
|
- @AuthorizationManager.canUserWriteProjectContent @req, @user_id, @project_id, (error, canWrite) ->
|
|
|
+ @AuthorizationManager.canUserWriteProjectContent @user_id, @project_id, @token, (error, canWrite) ->
|
|
|
expect(canWrite).to.equal false
|
|
|
done()
|
|
|
|
|
|
describe "canUserWriteProjectSettings", ->
|
|
|
beforeEach ->
|
|
|
- @req = new MockRequest()
|
|
|
@AuthorizationManager.getPrivilegeLevelForProject = sinon.stub()
|
|
|
|
|
|
describe "when user is owner", ->
|
|
|
beforeEach ->
|
|
|
@AuthorizationManager.getPrivilegeLevelForProject
|
|
|
- .withArgs(@req, @user_id, @project_id)
|
|
|
+ .withArgs(@user_id, @project_id, @token)
|
|
|
.yields(null, "owner", false)
|
|
|
|
|
|
it "should return true", (done) ->
|
|
|
- @AuthorizationManager.canUserWriteProjectSettings @req, @user_id, @project_id, (error, canWrite) ->
|
|
|
+ @AuthorizationManager.canUserWriteProjectSettings @user_id, @project_id, @token, (error, canWrite) ->
|
|
|
expect(canWrite).to.equal true
|
|
|
done()
|
|
|
|
|
|
describe "when user has read-write access as a collaborator", ->
|
|
|
beforeEach ->
|
|
|
@AuthorizationManager.getPrivilegeLevelForProject
|
|
|
- .withArgs(@req, @user_id, @project_id)
|
|
|
+ .withArgs(@user_id, @project_id, @token)
|
|
|
.yields(null, "readAndWrite", false)
|
|
|
|
|
|
it "should return true", (done) ->
|
|
|
- @AuthorizationManager.canUserWriteProjectSettings @req, @user_id, @project_id, (error, canWrite) ->
|
|
|
+ @AuthorizationManager.canUserWriteProjectSettings @user_id, @project_id, @token, (error, canWrite) ->
|
|
|
expect(canWrite).to.equal true
|
|
|
done()
|
|
|
|
|
|
describe "when user has read-write access as the public", ->
|
|
|
beforeEach ->
|
|
|
@AuthorizationManager.getPrivilegeLevelForProject
|
|
|
- .withArgs(@req, @user_id, @project_id)
|
|
|
+ .withArgs(@user_id, @project_id, @token)
|
|
|
.yields(null, "readAndWrite", true)
|
|
|
|
|
|
it "should return false", (done) ->
|
|
|
- @AuthorizationManager.canUserWriteProjectSettings @req, @user_id, @project_id, (error, canWrite) ->
|
|
|
+ @AuthorizationManager.canUserWriteProjectSettings @user_id, @project_id, @token, (error, canWrite) ->
|
|
|
expect(canWrite).to.equal false
|
|
|
done()
|
|
|
|
|
|
describe "when user has read-only access", ->
|
|
|
beforeEach ->
|
|
|
@AuthorizationManager.getPrivilegeLevelForProject
|
|
|
- .withArgs(@req, @user_id, @project_id)
|
|
|
+ .withArgs(@user_id, @project_id, @token)
|
|
|
.yields(null, "readOnly", false)
|
|
|
|
|
|
it "should return false", (done) ->
|
|
|
- @AuthorizationManager.canUserWriteProjectSettings @req, @user_id, @project_id, (error, canWrite) ->
|
|
|
+ @AuthorizationManager.canUserWriteProjectSettings @user_id, @project_id, @token, (error, canWrite) ->
|
|
|
expect(canWrite).to.equal false
|
|
|
done()
|
|
|
|
|
|
describe "when user has no access", ->
|
|
|
beforeEach ->
|
|
|
@AuthorizationManager.getPrivilegeLevelForProject
|
|
|
- .withArgs(@req, @user_id, @project_id)
|
|
|
+ .withArgs(@user_id, @project_id, @token)
|
|
|
.yields(null, false, false)
|
|
|
|
|
|
it "should return false", (done) ->
|
|
|
- @AuthorizationManager.canUserWriteProjectSettings @req, @user_id, @project_id, (error, canWrite) ->
|
|
|
+ @AuthorizationManager.canUserWriteProjectSettings @user_id, @project_id, @token, (error, canWrite) ->
|
|
|
expect(canWrite).to.equal false
|
|
|
done()
|
|
|
|
|
|
describe "canUserAdminProject", ->
|
|
|
beforeEach ->
|
|
|
- @req = new MockRequest()
|
|
|
@AuthorizationManager.getPrivilegeLevelForProject = sinon.stub()
|
|
|
|
|
|
describe "when user is owner", ->
|
|
|
beforeEach ->
|
|
|
@AuthorizationManager.getPrivilegeLevelForProject
|
|
|
- .withArgs(@req, @user_id, @project_id)
|
|
|
+ .withArgs(@user_id, @project_id, @token)
|
|
|
.yields(null, "owner", false)
|
|
|
|
|
|
it "should return true", (done) ->
|
|
|
- @AuthorizationManager.canUserAdminProject @req, @user_id, @project_id, (error, canAdmin) ->
|
|
|
+ @AuthorizationManager.canUserAdminProject @user_id, @project_id, @token, (error, canAdmin) ->
|
|
|
expect(canAdmin).to.equal true
|
|
|
done()
|
|
|
|
|
|
describe "when user has read-write access", ->
|
|
|
beforeEach ->
|
|
|
@AuthorizationManager.getPrivilegeLevelForProject
|
|
|
- .withArgs(@req, @user_id, @project_id)
|
|
|
+ .withArgs(@user_id, @project_id, @token)
|
|
|
.yields(null, "readAndWrite", false)
|
|
|
|
|
|
it "should return false", (done) ->
|
|
|
- @AuthorizationManager.canUserAdminProject @req, @user_id, @project_id, (error, canAdmin) ->
|
|
|
+ @AuthorizationManager.canUserAdminProject @user_id, @project_id, @token, (error, canAdmin) ->
|
|
|
expect(canAdmin).to.equal false
|
|
|
done()
|
|
|
|
|
|
describe "when user has read-only access", ->
|
|
|
beforeEach ->
|
|
|
@AuthorizationManager.getPrivilegeLevelForProject
|
|
|
- .withArgs(@req, @user_id, @project_id)
|
|
|
+ .withArgs(@user_id, @project_id, @token)
|
|
|
.yields(null, "readOnly", false)
|
|
|
|
|
|
it "should return false", (done) ->
|
|
|
- @AuthorizationManager.canUserAdminProject @req, @user_id, @project_id, (error, canAdmin) ->
|
|
|
+ @AuthorizationManager.canUserAdminProject @user_id, @project_id, @token, (error, canAdmin) ->
|
|
|
expect(canAdmin).to.equal false
|
|
|
done()
|
|
|
|
|
|
describe "when user has no access", ->
|
|
|
beforeEach ->
|
|
|
@AuthorizationManager.getPrivilegeLevelForProject
|
|
|
- .withArgs(@req, @user_id, @project_id)
|
|
|
+ .withArgs(@user_id, @project_id, @token)
|
|
|
.yields(null, false, false)
|
|
|
|
|
|
it "should return false", (done) ->
|
|
|
- @AuthorizationManager.canUserAdminProject @req, @user_id, @project_id, (error, canAdmin) ->
|
|
|
+ @AuthorizationManager.canUserAdminProject @user_id, @project_id, @token, (error, canAdmin) ->
|
|
|
expect(canAdmin).to.equal false
|
|
|
done()
|
|
|
|