| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- const SandboxedModule = require('sandboxed-module')
- const sinon = require('sinon')
- const { expect } = require('chai')
- const modulePath = '../../../../app/src/Features/Helpers/AuthorizationHelper'
- describe('AuthorizationHelper', function () {
- beforeEach(function () {
- this.AuthorizationHelper = SandboxedModule.require(modulePath, {
- requires: {
- './AdminAuthorizationHelper': (this.AdminAuthorizationHelper = {
- hasAdminAccess: sinon.stub().returns(false),
- }),
- '../../models/User': {
- UserSchema: {
- obj: {
- staffAccess: {
- publisherMetrics: {},
- publisherManagement: {},
- institutionMetrics: {},
- institutionManagement: {},
- groupMetrics: {},
- groupManagement: {},
- adminMetrics: {},
- },
- },
- },
- },
- '../Project/ProjectGetter': (this.ProjectGetter = { promises: {} }),
- '../SplitTests/SplitTestHandler': (this.SplitTestHandler = {
- promises: {},
- }),
- },
- })
- })
- describe('hasAnyStaffAccess', function () {
- it('with empty user', function () {
- const user = {}
- expect(this.AuthorizationHelper.hasAnyStaffAccess(user)).to.be.false
- })
- it('with no access user', function () {
- const user = { isAdmin: false, staffAccess: { adminMetrics: false } }
- expect(this.AuthorizationHelper.hasAnyStaffAccess(user)).to.be.false
- })
- it('with admin user', function () {
- const user = { isAdmin: true }
- this.AdminAuthorizationHelper.hasAdminAccess.returns(true)
- expect(this.AuthorizationHelper.hasAnyStaffAccess(user)).to.be.false
- })
- it('with staff user', function () {
- const user = { staffAccess: { adminMetrics: true, somethingElse: false } }
- this.AdminAuthorizationHelper.hasAdminAccess.returns(true)
- expect(this.AuthorizationHelper.hasAnyStaffAccess(user)).to.be.true
- })
- it('with non-staff user with extra attributes', function () {
- // make sure that staffAccess attributes not declared on the model don't
- // give user access
- const user = { staffAccess: { adminMetrics: false, somethingElse: true } }
- expect(this.AuthorizationHelper.hasAnyStaffAccess(user)).to.be.false
- })
- })
- describe('isReviewerRoleEnabled', function () {
- it('with no reviewers and no split test', async function () {
- this.ProjectGetter.promises.getProject = sinon.stub().resolves({
- reviewer_refs: {},
- owner_ref: 'ownerId',
- })
- this.SplitTestHandler.promises.getAssignmentForUser = sinon
- .stub()
- .resolves({
- variant: 'disabled',
- })
- expect(
- await this.AuthorizationHelper.promises.isReviewerRoleEnabled(
- 'userId',
- 'projectId'
- )
- ).to.be.false
- })
- it('with no reviewers and enabled split test', async function () {
- this.ProjectGetter.promises.getProject = sinon.stub().resolves({
- reviewer_refs: {},
- owner_ref: 'userId',
- })
- this.SplitTestHandler.promises.getAssignmentForUser = sinon
- .stub()
- .resolves({
- variant: 'enabled',
- })
- expect(
- await this.AuthorizationHelper.promises.isReviewerRoleEnabled(
- 'userId',
- 'projectId'
- )
- ).to.be.true
- })
- it('with reviewers and disabled split test', async function () {
- this.ProjectGetter.promises.getProject = sinon.stub().resolves({
- reviewer_refs: [{ $oid: 'userId' }],
- })
- this.SplitTestHandler.promises.getAssignmentForUser = sinon
- .stub()
- .resolves({
- variant: 'default',
- })
- expect(
- await this.AuthorizationHelper.promises.isReviewerRoleEnabled(
- 'userId',
- 'projectId'
- )
- ).to.be.true
- })
- it('with reviewers and enabled split test', async function () {
- this.ProjectGetter.promises.getProject = sinon.stub().resolves({
- reviewer_refs: [{ $oid: 'userId' }],
- })
- this.SplitTestHandler.promises.getAssignmentForUser = sinon
- .stub()
- .resolves({
- variant: 'enabled',
- })
- expect(
- await this.AuthorizationHelper.promises.isReviewerRoleEnabled(
- 'userId',
- 'projectId'
- )
- ).to.be.true
- })
- })
- })
|