scope-util.test.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import { SCOPE } from '@growi/core/dist/interfaces';
  2. import { describe, it, expect } from 'vitest';
  3. import { parseScopes, getDisabledScopes, extractScopes } from './scope-util';
  4. describe('scope-util', () => {
  5. const mockScopes = {
  6. READ: {
  7. USER: 'read:user',
  8. ADMIN: {
  9. SETTING: 'read:admin:setting',
  10. ALL: 'read:admin:all',
  11. },
  12. ALL: 'read:all',
  13. },
  14. WRITE: {
  15. USER: 'write:user',
  16. ADMIN: {
  17. SETTING: 'write:admin:setting',
  18. ALL: 'write:admin:all',
  19. },
  20. ALL: 'write:all',
  21. },
  22. };
  23. it('should parse scopes correctly for non-admin', () => {
  24. const result = parseScopes({ scopes: mockScopes, isAdmin: false });
  25. // Check that admin scopes are excluded
  26. expect(result.ADMIN).toBeUndefined();
  27. expect(result.ALL).toBeUndefined();
  28. // Check that user scopes are included
  29. expect(result.USER).toBeDefined();
  30. expect(result.USER['read:user']).toBe('read:user');
  31. expect(result.USER['write:user']).toBe('write:user');
  32. });
  33. it('should include admin scopes for admin users', () => {
  34. const result = parseScopes({ scopes: mockScopes, isAdmin: true });
  35. // Check that admin scopes are included
  36. expect(result.ADMIN).toBeDefined();
  37. expect(result.ALL).toBeDefined();
  38. // Check admin settings
  39. expect(result.ADMIN['admin:setting']['read:admin:setting']).toBe('read:admin:setting');
  40. expect(result.ADMIN['admin:setting']['write:admin:setting']).toBe('write:admin:setting');
  41. // Check ALL category
  42. expect(result.ALL['read:all']).toBe('read:all');
  43. expect(result.ALL['write:all']).toBe('write:all');
  44. });
  45. it('should return empty set when no scopes are selected', () => {
  46. const result = getDisabledScopes([], ['read:user', 'write:user']);
  47. expect(result.size).toBe(0);
  48. });
  49. it('should disable specific scopes when a wildcard is selected', () => {
  50. const selectedScopes = [SCOPE.READ.ALL];
  51. const availableScopes = [
  52. SCOPE.READ.FEATURES.PAGE,
  53. SCOPE.READ.FEATURES.ATTACHMENT,
  54. SCOPE.WRITE.FEATURES.PAGE,
  55. SCOPE.READ.ALL,
  56. ];
  57. const result = getDisabledScopes(selectedScopes, availableScopes);
  58. // Should disable all read: scopes except the wildcard itself
  59. expect(result.has(SCOPE.READ.FEATURES.PAGE)).toBe(true);
  60. expect(result.has(SCOPE.READ.FEATURES.ATTACHMENT)).toBe(true);
  61. expect(result.has(SCOPE.WRITE.FEATURES.PAGE)).toBe(false);
  62. expect(result.has(SCOPE.READ.ALL)).toBe(false);
  63. });
  64. it('should handle multiple wildcard selections', () => {
  65. const selectedScopes = [SCOPE.READ.ALL, SCOPE.WRITE.ALL];
  66. const availableScopes = [
  67. SCOPE.READ.FEATURES.PAGE, SCOPE.READ.FEATURES.ATTACHMENT, SCOPE.READ.ALL,
  68. SCOPE.WRITE.FEATURES.PAGE, SCOPE.WRITE.FEATURES.ATTACHMENT, SCOPE.WRITE.ALL,
  69. ];
  70. const result = getDisabledScopes(selectedScopes, availableScopes);
  71. // Should disable all specific scopes under both wildcards
  72. expect(result.has(SCOPE.READ.FEATURES.PAGE)).toBe(true);
  73. expect(result.has(SCOPE.READ.FEATURES.ATTACHMENT)).toBe(true);
  74. expect(result.has(SCOPE.WRITE.FEATURES.PAGE)).toBe(true);
  75. expect(result.has(SCOPE.WRITE.FEATURES.ATTACHMENT)).toBe(true);
  76. expect(result.has(SCOPE.READ.ALL)).toBe(false);
  77. expect(result.has(SCOPE.WRITE.ALL)).toBe(false);
  78. });
  79. it('should extract all scope strings from a nested object', () => {
  80. const scopeObj = {
  81. USER: {
  82. 'read:user': 'read:user',
  83. 'write:user': 'write:user',
  84. },
  85. ADMIN: {
  86. 'ADMIN:SETTING': {
  87. 'read:admin:setting': 'read:admin:setting',
  88. 'write:admin:setting': 'write:admin:setting',
  89. },
  90. },
  91. };
  92. const result = extractScopes(scopeObj);
  93. expect(result).toContain('read:user');
  94. expect(result).toContain('write:user');
  95. expect(result).toContain('read:admin:setting');
  96. expect(result).toContain('write:admin:setting');
  97. expect(result.length).toBe(4);
  98. });
  99. it('should return empty array for empty object', () => {
  100. const result = extractScopes({});
  101. expect(result).toEqual([]);
  102. });
  103. it('should handle objects with no string values', () => {
  104. const scopeObj = {
  105. level1: {
  106. level2: {
  107. level3: {},
  108. },
  109. },
  110. };
  111. const result = extractScopes(scopeObj);
  112. expect(result).toEqual([]);
  113. });
  114. });