scope-util.test.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import { describe, it, expect } from 'vitest';
  2. import { ALL_SIGN } from '../../interfaces/scope';
  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 = [`read:${ALL_SIGN}`];
  51. const availableScopes = ['read:user', 'read:admin', 'write:user', `read:${ALL_SIGN}`];
  52. const result = getDisabledScopes(selectedScopes, availableScopes);
  53. // Should disable all read: scopes except the wildcard itself
  54. expect(result.has('read:user')).toBe(true);
  55. expect(result.has('read:admin')).toBe(true);
  56. expect(result.has(`read:${ALL_SIGN}`)).toBe(false);
  57. expect(result.has('write:user')).toBe(false);
  58. });
  59. it('should handle multiple wildcard selections', () => {
  60. const selectedScopes = [`read:${ALL_SIGN}`, `write:${ALL_SIGN}`];
  61. const availableScopes = [
  62. 'read:user', 'read:admin', `read:${ALL_SIGN}`,
  63. 'write:user', 'write:admin', `write:${ALL_SIGN}`,
  64. ];
  65. const result = getDisabledScopes(selectedScopes, availableScopes);
  66. // Should disable all specific scopes under both wildcards
  67. expect(result.has('read:user')).toBe(true);
  68. expect(result.has('read:admin')).toBe(true);
  69. expect(result.has('write:user')).toBe(true);
  70. expect(result.has('write:admin')).toBe(true);
  71. expect(result.has(`read:${ALL_SIGN}`)).toBe(false);
  72. expect(result.has(`write:${ALL_SIGN}`)).toBe(false);
  73. });
  74. it('should extract all scope strings from a nested object', () => {
  75. const scopeObj = {
  76. USER: {
  77. 'read:user': 'read:user',
  78. 'write:user': 'write:user',
  79. },
  80. ADMIN: {
  81. 'ADMIN:SETTING': {
  82. 'read:admin:setting': 'read:admin:setting',
  83. 'write:admin:setting': 'write:admin:setting',
  84. },
  85. },
  86. };
  87. const result = extractScopes(scopeObj);
  88. expect(result).toContain('read:user');
  89. expect(result).toContain('write:user');
  90. expect(result).toContain('read:admin:setting');
  91. expect(result).toContain('write:admin:setting');
  92. expect(result.length).toBe(4);
  93. });
  94. it('should return empty array for empty object', () => {
  95. const result = extractScopes({});
  96. expect(result).toEqual([]);
  97. });
  98. it('should handle objects with no string values', () => {
  99. const scopeObj = {
  100. level1: {
  101. level2: {
  102. level3: {},
  103. },
  104. },
  105. };
  106. const result = extractScopes(scopeObj);
  107. expect(result).toEqual([]);
  108. });
  109. });