AuthorizationHelperTests.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. 'userId',
  74. 'projectId'
  75. )
  76. ).to.be.false
  77. })
  78. it('with no reviewers and enabled split test', async function () {
  79. this.ProjectGetter.promises.getProject = sinon.stub().resolves({
  80. reviewer_refs: {},
  81. owner_ref: 'userId',
  82. })
  83. this.SplitTestHandler.promises.getAssignmentForUser = sinon
  84. .stub()
  85. .resolves({
  86. variant: 'enabled',
  87. })
  88. expect(
  89. await this.AuthorizationHelper.promises.isReviewerRoleEnabled(
  90. 'userId',
  91. 'projectId'
  92. )
  93. ).to.be.true
  94. })
  95. it('with reviewers and disabled split test', async function () {
  96. this.ProjectGetter.promises.getProject = sinon.stub().resolves({
  97. reviewer_refs: [{ $oid: 'userId' }],
  98. })
  99. this.SplitTestHandler.promises.getAssignmentForUser = sinon
  100. .stub()
  101. .resolves({
  102. variant: 'default',
  103. })
  104. expect(
  105. await this.AuthorizationHelper.promises.isReviewerRoleEnabled(
  106. 'userId',
  107. 'projectId'
  108. )
  109. ).to.be.true
  110. })
  111. it('with reviewers and enabled split test', async function () {
  112. this.ProjectGetter.promises.getProject = sinon.stub().resolves({
  113. reviewer_refs: [{ $oid: 'userId' }],
  114. })
  115. this.SplitTestHandler.promises.getAssignmentForUser = sinon
  116. .stub()
  117. .resolves({
  118. variant: 'enabled',
  119. })
  120. expect(
  121. await this.AuthorizationHelper.promises.isReviewerRoleEnabled(
  122. 'userId',
  123. 'projectId'
  124. )
  125. ).to.be.true
  126. })
  127. })
  128. })