AuthorizationHelperTests.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. const SandboxedModule = require('sandboxed-module')
  2. const sinon = require('sinon')
  3. const { expect } = require('chai')
  4. const modulePath = '../../../../app/src/Features/Helpers/AuthorizationHelper'
  5. describe('AuthorizationHelper', function () {
  6. beforeEach(function () {
  7. this.AuthorizationHelper = SandboxedModule.require(modulePath, {
  8. requires: {
  9. './AdminAuthorizationHelper': (this.AdminAuthorizationHelper = {
  10. hasAdminAccess: sinon.stub().returns(false),
  11. }),
  12. '../../models/User': {
  13. UserSchema: {
  14. obj: {
  15. staffAccess: {
  16. publisherMetrics: {},
  17. publisherManagement: {},
  18. institutionMetrics: {},
  19. institutionManagement: {},
  20. groupMetrics: {},
  21. groupManagement: {},
  22. adminMetrics: {},
  23. },
  24. },
  25. },
  26. },
  27. '../Project/ProjectGetter': (this.ProjectGetter = { promises: {} }),
  28. '../SplitTests/SplitTestHandler': (this.SplitTestHandler = {
  29. promises: {},
  30. }),
  31. },
  32. })
  33. })
  34. describe('hasAnyStaffAccess', function () {
  35. it('with empty user', function () {
  36. const user = {}
  37. expect(this.AuthorizationHelper.hasAnyStaffAccess(user)).to.be.false
  38. })
  39. it('with no access user', function () {
  40. const user = { isAdmin: false, staffAccess: { adminMetrics: false } }
  41. expect(this.AuthorizationHelper.hasAnyStaffAccess(user)).to.be.false
  42. })
  43. it('with admin user', function () {
  44. const user = { isAdmin: true }
  45. this.AdminAuthorizationHelper.hasAdminAccess.returns(true)
  46. expect(this.AuthorizationHelper.hasAnyStaffAccess(user)).to.be.false
  47. })
  48. it('with staff user', function () {
  49. const user = { staffAccess: { adminMetrics: true, somethingElse: false } }
  50. this.AdminAuthorizationHelper.hasAdminAccess.returns(true)
  51. expect(this.AuthorizationHelper.hasAnyStaffAccess(user)).to.be.true
  52. })
  53. it('with non-staff user with extra attributes', function () {
  54. // make sure that staffAccess attributes not declared on the model don't
  55. // give user access
  56. const user = { staffAccess: { adminMetrics: false, somethingElse: true } }
  57. expect(this.AuthorizationHelper.hasAnyStaffAccess(user)).to.be.false
  58. })
  59. })
  60. describe('isReviewerRoleEnabled', function () {
  61. it('with no reviewers and no split test', async function () {
  62. this.ProjectGetter.promises.getProject = sinon.stub().resolves({
  63. reviewer_refs: {},
  64. owner_ref: 'ownerId',
  65. })
  66. this.SplitTestHandler.promises.getAssignmentForUser = sinon
  67. .stub()
  68. .resolves({
  69. variant: 'disabled',
  70. })
  71. expect(
  72. await this.AuthorizationHelper.promises.isReviewerRoleEnabled(
  73. 'projectId'
  74. )
  75. ).to.be.false
  76. })
  77. it('with no reviewers and enabled split test', async function () {
  78. this.ProjectGetter.promises.getProject = sinon.stub().resolves({
  79. reviewer_refs: {},
  80. owner_ref: 'userId',
  81. })
  82. this.SplitTestHandler.promises.getAssignmentForUser = sinon
  83. .stub()
  84. .resolves({
  85. variant: 'enabled',
  86. })
  87. expect(
  88. await this.AuthorizationHelper.promises.isReviewerRoleEnabled(
  89. 'projectId'
  90. )
  91. ).to.be.true
  92. })
  93. it('with reviewers and disabled split test', async function () {
  94. this.ProjectGetter.promises.getProject = sinon.stub().resolves({
  95. reviewer_refs: [{ $oid: 'userId' }],
  96. })
  97. this.SplitTestHandler.promises.getAssignmentForUser = sinon
  98. .stub()
  99. .resolves({
  100. variant: 'default',
  101. })
  102. expect(
  103. await this.AuthorizationHelper.promises.isReviewerRoleEnabled(
  104. 'projectId'
  105. )
  106. ).to.be.true
  107. })
  108. it('with reviewers and enabled split test', async function () {
  109. this.ProjectGetter.promises.getProject = sinon.stub().resolves({
  110. reviewer_refs: [{ $oid: 'userId' }],
  111. })
  112. this.SplitTestHandler.promises.getAssignmentForUser = sinon
  113. .stub()
  114. .resolves({
  115. variant: 'enabled',
  116. })
  117. expect(
  118. await this.AuthorizationHelper.promises.isReviewerRoleEnabled(
  119. 'projectId'
  120. )
  121. ).to.be.true
  122. })
  123. })
  124. })