AuthorizationHelperTests.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. },
  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. this.AdminAuthorizationHelper.hasAdminAccess.returns(true)
  42. expect(this.AuthorizationHelper.hasAnyStaffAccess(user)).to.be.false
  43. })
  44. it('with staff user', function () {
  45. const user = { staffAccess: { adminMetrics: true, somethingElse: false } }
  46. this.AdminAuthorizationHelper.hasAdminAccess.returns(true)
  47. expect(this.AuthorizationHelper.hasAnyStaffAccess(user)).to.be.true
  48. })
  49. it('with non-staff user with extra attributes', function () {
  50. // make sure that staffAccess attributes not declared on the model don't
  51. // give user access
  52. const user = { staffAccess: { adminMetrics: false, somethingElse: true } }
  53. expect(this.AuthorizationHelper.hasAnyStaffAccess(user)).to.be.false
  54. })
  55. })
  56. })