AuthorizationHelperTests.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. })