AuthorizationHelper.test.mjs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import { vi, expect } from 'vitest'
  2. const modulePath = '../../../../app/src/Features/Helpers/AuthorizationHelper'
  3. describe('AuthorizationHelper', function () {
  4. beforeEach(async function (ctx) {
  5. vi.doMock('../../../../app/src/models/User', () => ({
  6. UserSchema: {
  7. obj: {
  8. staffAccess: {
  9. publisherMetrics: {},
  10. publisherManagement: {},
  11. institutionMetrics: {},
  12. institutionManagement: {},
  13. groupMetrics: {},
  14. groupManagement: {},
  15. adminMetrics: {},
  16. },
  17. },
  18. },
  19. }))
  20. vi.doMock('../../../../app/src/Features/Project/ProjectGetter', () => ({
  21. default: (ctx.ProjectGetter = { promises: {} }),
  22. }))
  23. vi.doMock(
  24. '../../../../app/src/Features/SplitTests/SplitTestHandler',
  25. () => ({
  26. default: (ctx.SplitTestHandler = {
  27. promises: {},
  28. }),
  29. })
  30. )
  31. ctx.AuthorizationHelper = (await import(modulePath)).default
  32. })
  33. describe('hasAnyStaffAccess', function () {
  34. it('with empty user', function (ctx) {
  35. const user = {}
  36. expect(ctx.AuthorizationHelper.hasAnyStaffAccess(user)).to.be.false
  37. })
  38. it('with no access user', function (ctx) {
  39. const user = { isAdmin: false, staffAccess: { adminMetrics: false } }
  40. expect(ctx.AuthorizationHelper.hasAnyStaffAccess(user)).to.be.false
  41. })
  42. it('with admin user', function (ctx) {
  43. const user = { isAdmin: true }
  44. expect(ctx.AuthorizationHelper.hasAnyStaffAccess(user)).to.be.false
  45. })
  46. it('with staff user', function (ctx) {
  47. const user = { staffAccess: { adminMetrics: true, somethingElse: false } }
  48. expect(ctx.AuthorizationHelper.hasAnyStaffAccess(user)).to.be.true
  49. })
  50. it('with non-staff user with extra attributes', function (ctx) {
  51. // make sure that staffAccess attributes not declared on the model don't
  52. // give user access
  53. const user = { staffAccess: { adminMetrics: false, somethingElse: true } }
  54. expect(ctx.AuthorizationHelper.hasAnyStaffAccess(user)).to.be.false
  55. })
  56. })
  57. })