| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525 |
- sinon = require('sinon')
- chai = require('chai')
- should = chai.should()
- expect = chai.expect
- modulePath = "../../../../app/js/Features/Authorization/AuthorizationManager.js"
- SandboxedModule = require('sandboxed-module')
- Errors = require "../../../../app/js/Features/Errors/Errors.js"
- describe "AuthorizationManager", ->
- beforeEach ->
- @AuthorizationManager = SandboxedModule.require modulePath, requires:
- "../Collaborators/CollaboratorsHandler": @CollaboratorsHandler = {}
- "../../models/Project": Project: @Project = {}
- "../../models/User": User: @User = {}
- "../Errors/Errors": Errors
- "../TokenAccess/TokenAccessHandler": @TokenAccessHandler = {
- isValidToken: sinon.stub().callsArgWith(2, null, false, false)
- }
- @user_id = "user-id-1"
- @project_id = "project-id-1"
- @token = 'some-token'
- @callback = sinon.stub()
- describe "getPrivilegeLevelForProject", ->
- beforeEach ->
- @Project.findOne = sinon.stub()
- @AuthorizationManager.isUserSiteAdmin = sinon.stub()
- @CollaboratorsHandler.getMemberIdPrivilegeLevel = sinon.stub()
- describe 'with a token-based project', ->
- beforeEach ->
- @Project.findOne
- .withArgs({ _id: @project_id }, { publicAccesLevel: 1 })
- .yields(null, { publicAccesLevel: "tokenBased" })
- describe "with a user_id with a privilege level", ->
- beforeEach ->
- @AuthorizationManager.isUserSiteAdmin.withArgs(@user_id).yields(null, false)
- @CollaboratorsHandler.getMemberIdPrivilegeLevel
- .withArgs(@user_id, @project_id)
- .yields(null, "readOnly")
- @AuthorizationManager.getPrivilegeLevelForProject @user_id, @project_id, @token, @callback
- it "should return the user's privilege level", ->
- @callback.calledWith(null, "readOnly", false).should.equal true
- describe "with a user_id with no privilege level", ->
- beforeEach ->
- @AuthorizationManager.isUserSiteAdmin.withArgs(@user_id).yields(null, false)
- @CollaboratorsHandler.getMemberIdPrivilegeLevel
- .withArgs(@user_id, @project_id)
- .yields(null, false)
- @AuthorizationManager.getPrivilegeLevelForProject @user_id, @project_id, @token, @callback
- it "should return false", ->
- @callback.calledWith(null, false, false).should.equal true
- describe "with a user_id who is an admin", ->
- beforeEach ->
- @AuthorizationManager.isUserSiteAdmin.withArgs(@user_id).yields(null, true)
- @CollaboratorsHandler.getMemberIdPrivilegeLevel
- .withArgs(@user_id, @project_id)
- .yields(null, false)
- @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)", ->
- describe 'when the token is not valid', ->
- beforeEach ->
- @TokenAccessHandler.isValidToken = sinon.stub()
- .withArgs(@project_id, @token)
- .yields(null, false, false)
- @AuthorizationManager.getPrivilegeLevelForProject null, @project_id, @token, @callback
- it "should not call CollaboratorsHandler.getMemberIdPrivilegeLevel", ->
- @CollaboratorsHandler.getMemberIdPrivilegeLevel.called.should.equal false
- it "should not call AuthorizationManager.isUserSiteAdmin", ->
- @AuthorizationManager.isUserSiteAdmin.called.should.equal false
- it 'should check if the token is valid', ->
- @TokenAccessHandler.isValidToken.calledWith(@project_id, @token).should.equal true
- it "should return false", ->
- @callback.calledWith(null, false, false).should.equal true
- describe 'when the token is valid for read-and-write', ->
- describe 'when read-write-sharing is not enabled', ->
- beforeEach ->
- @TokenAccessHandler.ANONYMOUS_READ_AND_WRITE_ENABLED = false
- @TokenAccessHandler.isValidToken = sinon.stub()
- .withArgs(@project_id, @token)
- .yields(null, true, false)
- @AuthorizationManager.getPrivilegeLevelForProject null, @project_id, @token, @callback
- it "should not call CollaboratorsHandler.getMemberIdPrivilegeLevel", ->
- @CollaboratorsHandler.getMemberIdPrivilegeLevel.called.should.equal false
- it "should not call AuthorizationManager.isUserSiteAdmin", ->
- @AuthorizationManager.isUserSiteAdmin.called.should.equal false
- it 'should check if the token is valid', ->
- @TokenAccessHandler.isValidToken.calledWith(@project_id, @token).should.equal true
- it "should deny access", ->
- @callback.calledWith(null, false, false).should.equal true
- describe 'when read-write-sharing is enabled', ->
- beforeEach ->
- @TokenAccessHandler.ANONYMOUS_READ_AND_WRITE_ENABLED = true
- @TokenAccessHandler.isValidToken = sinon.stub()
- .withArgs(@project_id, @token)
- .yields(null, true, false)
- @AuthorizationManager.getPrivilegeLevelForProject null, @project_id, @token, @callback
- it "should not call CollaboratorsHandler.getMemberIdPrivilegeLevel", ->
- @CollaboratorsHandler.getMemberIdPrivilegeLevel.called.should.equal false
- it "should not call AuthorizationManager.isUserSiteAdmin", ->
- @AuthorizationManager.isUserSiteAdmin.called.should.equal false
- it 'should check if the token is valid', ->
- @TokenAccessHandler.isValidToken.calledWith(@project_id, @token).should.equal true
- it "should give read-write access", ->
- @callback.calledWith(null, "readAndWrite", false).should.equal true
- describe 'when the token is valid for read-only', ->
- beforeEach ->
- @TokenAccessHandler.isValidToken = sinon.stub()
- .withArgs(@project_id, @token)
- .yields(null, false, true)
- @AuthorizationManager.getPrivilegeLevelForProject null, @project_id, @token, @callback
- it "should not call CollaboratorsHandler.getMemberIdPrivilegeLevel", ->
- @CollaboratorsHandler.getMemberIdPrivilegeLevel.called.should.equal false
- it "should not call AuthorizationManager.isUserSiteAdmin", ->
- @AuthorizationManager.isUserSiteAdmin.called.should.equal false
- it 'should check if the token is valid', ->
- @TokenAccessHandler.isValidToken.calledWith(@project_id, @token).should.equal true
- it "should give read-only access", ->
- @callback.calledWith(null, "readOnly", false).should.equal true
- describe "with a private project", ->
- beforeEach ->
- @Project.findOne
- .withArgs({ _id: @project_id }, { publicAccesLevel: 1 })
- .yields(null, { publicAccesLevel: "private" })
- describe "with a user_id with a privilege level", ->
- beforeEach ->
- @AuthorizationManager.isUserSiteAdmin.withArgs(@user_id).yields(null, false)
- @CollaboratorsHandler.getMemberIdPrivilegeLevel
- .withArgs(@user_id, @project_id)
- .yields(null, "readOnly")
- @AuthorizationManager.getPrivilegeLevelForProject @user_id, @project_id, @token, @callback
- it "should return the user's privilege level", ->
- @callback.calledWith(null, "readOnly", false).should.equal true
- describe "with a user_id with no privilege level", ->
- beforeEach ->
- @AuthorizationManager.isUserSiteAdmin.withArgs(@user_id).yields(null, false)
- @CollaboratorsHandler.getMemberIdPrivilegeLevel
- .withArgs(@user_id, @project_id)
- .yields(null, false)
- @AuthorizationManager.getPrivilegeLevelForProject @user_id, @project_id, @token, @callback
- it "should return false", ->
- @callback.calledWith(null, false, false).should.equal true
- describe "with a user_id who is an admin", ->
- beforeEach ->
- @AuthorizationManager.isUserSiteAdmin.withArgs(@user_id).yields(null, true)
- @CollaboratorsHandler.getMemberIdPrivilegeLevel
- .withArgs(@user_id, @project_id)
- .yields(null, false)
- @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 null, @project_id, @token, @callback
- it "should not call CollaboratorsHandler.getMemberIdPrivilegeLevel", ->
- @CollaboratorsHandler.getMemberIdPrivilegeLevel.called.should.equal false
- it "should not call AuthorizationManager.isUserSiteAdmin", ->
- @AuthorizationManager.isUserSiteAdmin.called.should.equal false
- it "should return false", ->
- @callback.calledWith(null, false, false).should.equal true
- describe "with a public project", ->
- beforeEach ->
- @Project.findOne
- .withArgs({ _id: @project_id }, { publicAccesLevel: 1 })
- .yields(null, { publicAccesLevel: "readAndWrite" })
- describe "with a user_id with a privilege level", ->
- beforeEach ->
- @AuthorizationManager.isUserSiteAdmin.withArgs(@user_id).yields(null, false)
- @CollaboratorsHandler.getMemberIdPrivilegeLevel
- .withArgs(@user_id, @project_id)
- .yields(null, "readOnly")
- @AuthorizationManager.getPrivilegeLevelForProject @user_id, @project_id, @token, @callback
- it "should return the user's privilege level", ->
- @callback.calledWith(null, "readOnly", false).should.equal true
- describe "with a user_id with no privilege level", ->
- beforeEach ->
- @AuthorizationManager.isUserSiteAdmin.withArgs(@user_id).yields(null, false)
- @CollaboratorsHandler.getMemberIdPrivilegeLevel
- .withArgs(@user_id, @project_id)
- .yields(null, false)
- @AuthorizationManager.getPrivilegeLevelForProject @user_id, @project_id, @token, @callback
- it "should return the public privilege level", ->
- @callback.calledWith(null, "readAndWrite", true).should.equal true
- describe "with a user_id who is an admin", ->
- beforeEach ->
- @AuthorizationManager.isUserSiteAdmin.withArgs(@user_id).yields(null, true)
- @CollaboratorsHandler.getMemberIdPrivilegeLevel
- .withArgs(@user_id, @project_id)
- .yields(null, false)
- @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 null, @project_id, @token, @callback
- it "should not call CollaboratorsHandler.getMemberIdPrivilegeLevel", ->
- @CollaboratorsHandler.getMemberIdPrivilegeLevel.called.should.equal false
- it "should not call AuthorizationManager.isUserSiteAdmin", ->
- @AuthorizationManager.isUserSiteAdmin.called.should.equal false
- it "should return the public privilege level", ->
- @callback.calledWith(null, "readAndWrite", true).should.equal true
- describe "when the project doesn't exist", ->
- beforeEach ->
- @Project.findOne
- .withArgs({ _id: @project_id }, { publicAccesLevel: 1 })
- .yields(null, null)
- it "should return a NotFoundError", ->
- @AuthorizationManager.getPrivilegeLevelForProject @user_id, @project_id, @token, (error) ->
- error.should.be.instanceof Errors.NotFoundError
- describe "when the project id is not valid", ->
- beforeEach ->
- @AuthorizationManager.isUserSiteAdmin.withArgs(@user_id).yields(null, false)
- @CollaboratorsHandler.getMemberIdPrivilegeLevel
- .withArgs(@user_id, @project_id)
- .yields(null, "readOnly")
- it "should return a error", (done)->
- @AuthorizationManager.getPrivilegeLevelForProject undefined, "not project id", @token, (err) =>
- @Project.findOne.called.should.equal false
- expect(err).to.exist
- done()
- describe "canUserReadProject", ->
- beforeEach ->
- @AuthorizationManager.getPrivilegeLevelForProject = sinon.stub()
- describe "when user is owner", ->
- beforeEach ->
- @AuthorizationManager.getPrivilegeLevelForProject
- .withArgs(@user_id, @project_id, @token)
- .yields(null, "owner", false)
- it "should return true", (done) ->
- @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(@user_id, @project_id, @token)
- .yields(null, "readAndWrite", false)
- it "should return true", (done) ->
- @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(@user_id, @project_id, @token)
- .yields(null, "readOnly", false)
- it "should return true", (done) ->
- @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(@user_id, @project_id, @token)
- .yields(null, false, false)
- it "should return false", (done) ->
- @AuthorizationManager.canUserReadProject @user_id, @project_id, @token, (error, canRead) ->
- expect(canRead).to.equal false
- done()
- describe "canUserWriteProjectContent", ->
- beforeEach ->
- @AuthorizationManager.getPrivilegeLevelForProject = sinon.stub()
- describe "when user is owner", ->
- beforeEach ->
- @AuthorizationManager.getPrivilegeLevelForProject
- .withArgs(@user_id, @project_id, @token)
- .yields(null, "owner", false)
- it "should return true", (done) ->
- @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(@user_id, @project_id, @token)
- .yields(null, "readAndWrite", false)
- it "should return true", (done) ->
- @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(@user_id, @project_id, @token)
- .yields(null, "readOnly", false)
- it "should return false", (done) ->
- @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(@user_id, @project_id, @token)
- .yields(null, false, false)
- it "should return false", (done) ->
- @AuthorizationManager.canUserWriteProjectContent @user_id, @project_id, @token, (error, canWrite) ->
- expect(canWrite).to.equal false
- done()
- describe "canUserWriteProjectSettings", ->
- beforeEach ->
- @AuthorizationManager.getPrivilegeLevelForProject = sinon.stub()
- describe "when user is owner", ->
- beforeEach ->
- @AuthorizationManager.getPrivilegeLevelForProject
- .withArgs(@user_id, @project_id, @token)
- .yields(null, "owner", false)
- it "should return true", (done) ->
- @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(@user_id, @project_id, @token)
- .yields(null, "readAndWrite", false)
- it "should return true", (done) ->
- @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(@user_id, @project_id, @token)
- .yields(null, "readAndWrite", true)
- it "should return false", (done) ->
- @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(@user_id, @project_id, @token)
- .yields(null, "readOnly", false)
- it "should return false", (done) ->
- @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(@user_id, @project_id, @token)
- .yields(null, false, false)
- it "should return false", (done) ->
- @AuthorizationManager.canUserWriteProjectSettings @user_id, @project_id, @token, (error, canWrite) ->
- expect(canWrite).to.equal false
- done()
- describe "canUserAdminProject", ->
- beforeEach ->
- @AuthorizationManager.getPrivilegeLevelForProject = sinon.stub()
- describe "when user is owner", ->
- beforeEach ->
- @AuthorizationManager.getPrivilegeLevelForProject
- .withArgs(@user_id, @project_id, @token)
- .yields(null, "owner", false)
- it "should return true", (done) ->
- @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(@user_id, @project_id, @token)
- .yields(null, "readAndWrite", false)
- it "should return false", (done) ->
- @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(@user_id, @project_id, @token)
- .yields(null, "readOnly", false)
- it "should return false", (done) ->
- @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(@user_id, @project_id, @token)
- .yields(null, false, false)
- it "should return false", (done) ->
- @AuthorizationManager.canUserAdminProject @user_id, @project_id, @token, (error, canAdmin) ->
- expect(canAdmin).to.equal false
- done()
- describe "isUserSiteAdmin", ->
- beforeEach ->
- @User.findOne = sinon.stub()
- describe "when user is admin", ->
- beforeEach ->
- @User.findOne
- .withArgs({ _id: @user_id }, { isAdmin: 1 })
- .yields(null, { isAdmin: true })
- it "should return true", (done) ->
- @AuthorizationManager.isUserSiteAdmin @user_id, (error, isAdmin) ->
- expect(isAdmin).to.equal true
- done()
- describe "when user is not admin", ->
- beforeEach ->
- @User.findOne
- .withArgs({ _id: @user_id }, { isAdmin: 1 })
- .yields(null, { isAdmin: false })
- it "should return false", (done) ->
- @AuthorizationManager.isUserSiteAdmin @user_id, (error, isAdmin) ->
- expect(isAdmin).to.equal false
- done()
- describe "when user is not found", ->
- beforeEach ->
- @User.findOne
- .withArgs({ _id: @user_id }, { isAdmin: 1 })
- .yields(null, null)
- it "should return false", (done) ->
- @AuthorizationManager.isUserSiteAdmin @user_id, (error, isAdmin) ->
- expect(isAdmin).to.equal false
- done()
- describe "when no user is passed", ->
- it "should return false", (done) ->
- @AuthorizationManager.isUserSiteAdmin null, (error, isAdmin) =>
- @User.findOne.called.should.equal false
- expect(isAdmin).to.equal false
- done()
|