AuthorizationHelperTests.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. const SandboxedModule = require('sandboxed-module')
  2. const { expect } = require('chai')
  3. const modulePath = '../../../../app/src/Features/Helpers/AuthorizationHelper'
  4. describe('AuthorizationHelper', function () {
  5. beforeEach(function () {
  6. this.AuthorizationHelper = SandboxedModule.require(modulePath, {
  7. requires: {
  8. '../../models/User': {
  9. UserSchema: {
  10. obj: {
  11. staffAccess: {
  12. publisherMetrics: {},
  13. publisherManagement: {},
  14. institutionMetrics: {},
  15. institutionManagement: {},
  16. groupMetrics: {},
  17. groupManagement: {},
  18. adminMetrics: {},
  19. },
  20. },
  21. },
  22. },
  23. },
  24. })
  25. })
  26. describe('hasAnyStaffAccess', function () {
  27. it('with empty user', function () {
  28. const user = {}
  29. expect(this.AuthorizationHelper.hasAnyStaffAccess(user)).to.be.false
  30. })
  31. it('with no access user', function () {
  32. const user = { isAdmin: false, staffAccess: { adminMetrics: false } }
  33. expect(this.AuthorizationHelper.hasAnyStaffAccess(user)).to.be.false
  34. })
  35. it('with admin user', function () {
  36. const user = { isAdmin: true }
  37. expect(this.AuthorizationHelper.hasAnyStaffAccess(user)).to.be.true
  38. })
  39. it('with staff user', function () {
  40. const user = { staffAccess: { adminMetrics: true, somethingElse: false } }
  41. expect(this.AuthorizationHelper.hasAnyStaffAccess(user)).to.be.true
  42. })
  43. it('with non-staff user with extra attributes', function () {
  44. // make sure that staffAccess attributes not declared on the model don't
  45. // give user access
  46. const user = { staffAccess: { adminMetrics: false, somethingElse: true } }
  47. expect(this.AuthorizationHelper.hasAnyStaffAccess(user)).to.be.false
  48. })
  49. })
  50. })