2018-10-24 09:50:34 -04:00
|
|
|
sinon = require('sinon')
|
|
|
|
chai = require('chai')
|
|
|
|
expect = require('chai').expect
|
|
|
|
modulePath = "../../../../app/js/Features/UserMembership/UserMembershipAuthorization.js"
|
|
|
|
SandboxedModule = require('sandboxed-module')
|
|
|
|
MockRequest = require "../helpers/MockRequest"
|
|
|
|
EntityConfigs = require("../../../../app/js/Features/UserMembership/UserMembershipEntityConfigs")
|
|
|
|
Errors = require("../../../../app/js/Features/Errors/Errors")
|
|
|
|
|
|
|
|
describe "UserMembershipAuthorization", ->
|
|
|
|
beforeEach ->
|
|
|
|
@req = new MockRequest()
|
|
|
|
@req.params.id = 'mock-entity-id'
|
|
|
|
@user = _id: 'mock-user-id'
|
|
|
|
@subscription = { _id: 'mock-subscription-id'}
|
|
|
|
|
|
|
|
@AuthenticationController =
|
|
|
|
getSessionUser: sinon.stub().returns(@user)
|
|
|
|
@UserMembershipHandler =
|
|
|
|
getEntity: sinon.stub().yields(null, @subscription)
|
2018-12-06 05:52:54 -05:00
|
|
|
getEntityWithoutAuthorizationCheck: sinon.stub().yields(null, @subscription)
|
2018-10-24 09:50:34 -04:00
|
|
|
@AuthorizationMiddlewear =
|
|
|
|
redirectToRestricted: sinon.stub().yields()
|
2018-12-06 08:52:41 -05:00
|
|
|
ensureUserIsSiteAdmin: sinon.stub().yields()
|
2018-10-24 09:50:34 -04:00
|
|
|
@UserMembershipAuthorization = SandboxedModule.require modulePath, requires:
|
|
|
|
'../Authentication/AuthenticationController': @AuthenticationController
|
|
|
|
'../Authorization/AuthorizationMiddlewear': @AuthorizationMiddlewear
|
|
|
|
'./UserMembershipHandler': @UserMembershipHandler
|
|
|
|
'./EntityConfigs': EntityConfigs
|
|
|
|
'../Errors/Errors': Errors
|
2018-12-05 09:54:00 -05:00
|
|
|
'request': @request = sinon.stub().yields(null, null, {})
|
2018-10-24 09:50:34 -04:00
|
|
|
"logger-sharelatex":
|
|
|
|
log: ->
|
|
|
|
err: ->
|
|
|
|
|
2018-11-30 08:03:35 -05:00
|
|
|
describe 'requireAccessToEntity', ->
|
2018-10-24 09:50:34 -04:00
|
|
|
it 'get entity', (done) ->
|
2019-01-11 09:17:27 -05:00
|
|
|
@UserMembershipAuthorization.requireGroupMetricsAccess @req, null, (error) =>
|
2018-10-24 09:50:34 -04:00
|
|
|
expect(error).to.not.extist
|
|
|
|
sinon.assert.calledWithMatch(
|
|
|
|
@UserMembershipHandler.getEntity,
|
|
|
|
@req.params.id,
|
|
|
|
modelName: 'Subscription',
|
|
|
|
@user
|
|
|
|
)
|
|
|
|
expect(@req.entity).to.equal @subscription
|
|
|
|
expect(@req.entityConfig).to.exist
|
|
|
|
done()
|
|
|
|
|
2018-12-06 05:52:54 -05:00
|
|
|
it 'handle entity not found as non-admin', (done) ->
|
2018-10-24 09:50:34 -04:00
|
|
|
@UserMembershipHandler.getEntity.yields(null, null)
|
2018-12-06 05:52:54 -05:00
|
|
|
@UserMembershipHandler.getEntityWithoutAuthorizationCheck.yields(null, null)
|
2019-01-11 09:17:27 -05:00
|
|
|
@UserMembershipAuthorization.requireGroupMetricsAccess @req, null, (error) =>
|
2018-10-24 09:50:34 -04:00
|
|
|
expect(error).to.extist
|
2018-12-06 05:52:54 -05:00
|
|
|
expect(error).to.be.instanceof(Error)
|
|
|
|
expect(error.constructor.name).to.equal('NotFoundError')
|
2018-10-24 09:50:34 -04:00
|
|
|
sinon.assert.called(@UserMembershipHandler.getEntity)
|
|
|
|
expect(@req.entity).to.not.exist
|
|
|
|
done()
|
|
|
|
|
2018-12-06 05:52:54 -05:00
|
|
|
it 'handle entity not found an admin can create', (done) ->
|
|
|
|
@user.isAdmin = true
|
|
|
|
@UserMembershipHandler.getEntity.yields(null, null)
|
|
|
|
@UserMembershipHandler.getEntityWithoutAuthorizationCheck.yields(null, null)
|
2019-01-11 09:17:27 -05:00
|
|
|
@UserMembershipAuthorization.requirePublisherMetricsAccess @req, redirect: (path) =>
|
2018-12-06 05:52:54 -05:00
|
|
|
expect(path).to.extist
|
|
|
|
expect(path).to.match /create/
|
|
|
|
done()
|
|
|
|
|
|
|
|
it 'handle entity not found an admin cannot create', (done) ->
|
|
|
|
@user.isAdmin = true
|
|
|
|
@UserMembershipHandler.getEntity.yields(null, null)
|
|
|
|
@UserMembershipHandler.getEntityWithoutAuthorizationCheck.yields(null, null)
|
2019-01-11 09:17:27 -05:00
|
|
|
@UserMembershipAuthorization.requireGroupMetricsAccess @req, null, (error) =>
|
2018-12-06 05:52:54 -05:00
|
|
|
expect(error).to.extist
|
|
|
|
expect(error).to.be.instanceof(Error)
|
|
|
|
expect(error.constructor.name).to.equal('NotFoundError')
|
|
|
|
done()
|
|
|
|
|
|
|
|
it 'handle entity no access', (done) ->
|
|
|
|
@UserMembershipHandler.getEntity.yields(null, null)
|
2019-01-11 09:17:27 -05:00
|
|
|
@UserMembershipAuthorization.requireGroupMetricsAccess @req, null, (error) =>
|
2018-12-06 05:52:54 -05:00
|
|
|
sinon.assert.called(@AuthorizationMiddlewear.redirectToRestricted)
|
|
|
|
done()
|
|
|
|
|
2018-10-24 09:50:34 -04:00
|
|
|
it 'handle anonymous user', (done) ->
|
|
|
|
@AuthenticationController.getSessionUser.returns(null)
|
2019-01-11 09:17:27 -05:00
|
|
|
@UserMembershipAuthorization.requireGroupMetricsAccess @req, null, (error) =>
|
2018-10-24 09:50:34 -04:00
|
|
|
expect(error).to.extist
|
|
|
|
sinon.assert.called(@AuthorizationMiddlewear.redirectToRestricted)
|
|
|
|
sinon.assert.notCalled(@UserMembershipHandler.getEntity)
|
|
|
|
expect(@req.entity).to.not.exist
|
|
|
|
done()
|
2018-10-30 06:02:30 -04:00
|
|
|
|
2018-11-30 08:03:35 -05:00
|
|
|
describe 'requireEntityAccess', ->
|
|
|
|
it 'handle team access', (done) ->
|
2019-01-11 09:17:27 -05:00
|
|
|
@UserMembershipAuthorization.requireTeamMetricsAccess @req, null, (error) =>
|
2018-11-30 08:03:35 -05:00
|
|
|
expect(error).to.not.extist
|
|
|
|
sinon.assert.calledWithMatch(
|
|
|
|
@UserMembershipHandler.getEntity,
|
|
|
|
@req.params.id,
|
|
|
|
fields: primaryKey: 'overleaf.id'
|
|
|
|
)
|
|
|
|
done()
|
|
|
|
|
|
|
|
it 'handle group access', (done) ->
|
2019-01-11 09:17:27 -05:00
|
|
|
@UserMembershipAuthorization.requireGroupMetricsAccess @req, null, (error) =>
|
2018-11-26 08:25:33 -05:00
|
|
|
expect(error).to.not.extist
|
|
|
|
sinon.assert.calledWithMatch(
|
|
|
|
@UserMembershipHandler.getEntity,
|
2018-11-30 08:03:35 -05:00
|
|
|
@req.params.id,
|
|
|
|
translations: title: 'group_account'
|
|
|
|
)
|
|
|
|
done()
|
|
|
|
|
|
|
|
it 'handle group managers access', (done) ->
|
2019-01-11 09:17:27 -05:00
|
|
|
@UserMembershipAuthorization.requireGroupManagersManagementAccess @req, null, (error) =>
|
2018-11-30 08:03:35 -05:00
|
|
|
expect(error).to.not.extist
|
|
|
|
sinon.assert.calledWithMatch(
|
|
|
|
@UserMembershipHandler.getEntity,
|
|
|
|
@req.params.id,
|
|
|
|
translations: subtitle: 'managers_management'
|
2018-11-26 08:25:33 -05:00
|
|
|
)
|
|
|
|
done()
|
|
|
|
|
2018-11-30 08:03:35 -05:00
|
|
|
it 'handle institution access', (done) ->
|
2019-01-11 09:17:27 -05:00
|
|
|
@UserMembershipAuthorization.requireInstitutionMetricsAccess @req, null, (error) =>
|
2018-11-30 08:03:35 -05:00
|
|
|
expect(error).to.not.extist
|
|
|
|
sinon.assert.calledWithMatch(
|
|
|
|
@UserMembershipHandler.getEntity,
|
|
|
|
@req.params.id,
|
|
|
|
modelName: 'Institution',
|
|
|
|
)
|
|
|
|
done()
|
|
|
|
|
2018-12-06 08:52:41 -05:00
|
|
|
it 'handle template with brand access', (done) ->
|
2018-12-05 09:54:00 -05:00
|
|
|
templateData =
|
|
|
|
id: 123
|
|
|
|
title: 'Template Title'
|
|
|
|
brand: { slug: 'brand-slug' }
|
|
|
|
@request.yields(null, { statusCode: 200 }, JSON.stringify(templateData))
|
2019-01-11 09:17:27 -05:00
|
|
|
@UserMembershipAuthorization.requireTemplateMetricsAccess @req, null, (error) =>
|
2018-12-05 09:54:00 -05:00
|
|
|
expect(error).to.not.extist
|
|
|
|
sinon.assert.calledWithMatch(
|
|
|
|
@UserMembershipHandler.getEntity,
|
|
|
|
'brand-slug',
|
|
|
|
modelName: 'Publisher',
|
|
|
|
)
|
|
|
|
done()
|
|
|
|
|
2018-12-06 08:52:41 -05:00
|
|
|
it 'handle template without brand access', (done) ->
|
|
|
|
templateData =
|
|
|
|
id: 123
|
|
|
|
title: 'Template Title'
|
|
|
|
brand: null
|
|
|
|
@request.yields(null, { statusCode: 200 }, JSON.stringify(templateData))
|
2019-01-11 09:17:27 -05:00
|
|
|
@UserMembershipAuthorization.requireTemplateMetricsAccess @req, null, (error) =>
|
2018-12-06 08:52:41 -05:00
|
|
|
expect(error).to.not.extist
|
|
|
|
sinon.assert.notCalled(@UserMembershipHandler.getEntity)
|
|
|
|
sinon.assert.calledOnce(@AuthorizationMiddlewear.ensureUserIsSiteAdmin)
|
|
|
|
done()
|
|
|
|
|
2018-11-30 08:03:35 -05:00
|
|
|
it 'handle graph access', (done) ->
|
|
|
|
@req.query.resource_id = 'mock-resource-id'
|
|
|
|
@req.query.resource_type = 'institution'
|
|
|
|
middlewear = @UserMembershipAuthorization.requireGraphAccess
|
2018-10-30 06:02:30 -04:00
|
|
|
middlewear @req, null, (error) =>
|
|
|
|
expect(error).to.not.extist
|
2018-11-30 08:03:35 -05:00
|
|
|
sinon.assert.calledWithMatch(
|
|
|
|
@UserMembershipHandler.getEntity,
|
|
|
|
@req.query.resource_id,
|
|
|
|
modelName: 'Institution',
|
|
|
|
)
|
|
|
|
done()
|