AuthorizationHelperTests.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. const chai = require('chai')
  2. const SandboxedModule = require('sandboxed-module')
  3. const { expect } = chai
  4. const modulePath = '../../../../app/src/Features/Helpers/AuthorizationHelper'
  5. describe('AuthorizationHelper', function() {
  6. beforeEach(function() {
  7. this.AuthorizationHelper = SandboxedModule.require(modulePath, {
  8. globals: {
  9. console: console
  10. },
  11. requires: {
  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. }
  28. })
  29. })
  30. describe('hasAnyStaffAccess', function() {
  31. it('with empty user', function() {
  32. const user = {}
  33. expect(this.AuthorizationHelper.hasAnyStaffAccess(user)).to.be.false
  34. })
  35. it('with no access user', function() {
  36. const user = { isAdmin: false, staffAccess: { adminMetrics: false } }
  37. expect(this.AuthorizationHelper.hasAnyStaffAccess(user)).to.be.false
  38. })
  39. it('with admin user', function() {
  40. const user = { isAdmin: true }
  41. expect(this.AuthorizationHelper.hasAnyStaffAccess(user)).to.be.true
  42. })
  43. it('with staff user', function() {
  44. const user = { staffAccess: { adminMetrics: true, somethingElse: false } }
  45. expect(this.AuthorizationHelper.hasAnyStaffAccess(user)).to.be.true
  46. })
  47. it('with non-staff user with extra attributes', function() {
  48. // make sure that staffAccess attributes not declared on the model don't
  49. // give user access
  50. const user = { staffAccess: { adminMetrics: false, somethingElse: true } }
  51. expect(this.AuthorizationHelper.hasAnyStaffAccess(user)).to.be.false
  52. })
  53. })
  54. })