page-path-utils.test.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. import {
  2. isTopPage, isMovablePage, convertToNewAffiliationPath, isCreatablePage, omitDuplicateAreaPathFromPaths,
  3. } from '~/utils/page-path-utils';
  4. describe('TopPage Path test', () => {
  5. test('Path is only "/"', () => {
  6. const result = isTopPage('/');
  7. expect(result).toBe(true);
  8. });
  9. test('Path is not match string', () => {
  10. const result = isTopPage('/test');
  11. expect(result).toBe(false);
  12. });
  13. test('Path is integer', () => {
  14. const result = isTopPage(1);
  15. expect(result).toBe(false);
  16. });
  17. test('Path is null', () => {
  18. const result = isTopPage(null);
  19. expect(result).toBe(false);
  20. });
  21. });
  22. describe('isMovablePage test', () => {
  23. test('should decide deletable or not', () => {
  24. expect(isMovablePage('/')).toBeFalsy();
  25. expect(isMovablePage('/hoge')).toBeTruthy();
  26. expect(isMovablePage('/user')).toBeFalsy();
  27. expect(isMovablePage('/user/xxx')).toBeFalsy();
  28. expect(isMovablePage('/user/xxx123')).toBeFalsy();
  29. expect(isMovablePage('/user/xxx/hoge')).toBeTruthy();
  30. });
  31. });
  32. describe('convertToNewAffiliationPath test', () => {
  33. test('Child path is not converted normally', () => {
  34. const result = convertToNewAffiliationPath('parent/', 'parent2/', 'parent/child');
  35. expect(result).toBe('parent2/child');
  36. });
  37. test('Parent path is not converted normally', () => {
  38. const result = convertToNewAffiliationPath('parent/', 'parent3/', 'parent/child');
  39. expect(result === 'parent/child').toBe(false);
  40. });
  41. test('Parent and Child path names are switched unexpectedly', () => {
  42. const result = convertToNewAffiliationPath('parent/', 'parent4/', 'parent/child');
  43. expect(result === 'child/parent4').toBe(false);
  44. });
  45. test('Child path is null', () => {
  46. expect(() => {
  47. convertToNewAffiliationPath('parent/', 'parent5/', null);
  48. }).toThrow();
  49. });
  50. test('Old parent path is null', () => {
  51. expect(() => {
  52. convertToNewAffiliationPath(null, 'parent5/', 'child');
  53. }).toThrow();
  54. });
  55. test('New parent path is null', () => {
  56. expect(() => {
  57. convertToNewAffiliationPath('parent/', null, 'child');
  58. }).toThrow();
  59. });
  60. });
  61. describe('isCreatablePage test', () => {
  62. test('should decide creatable or not', () => {
  63. expect(isCreatablePage('/hoge')).toBeTruthy();
  64. // edge cases
  65. expect(isCreatablePage('/me')).toBeFalsy();
  66. expect(isCreatablePage('/me/')).toBeFalsy();
  67. expect(isCreatablePage('/me/x')).toBeFalsy();
  68. expect(isCreatablePage('/meeting')).toBeTruthy();
  69. expect(isCreatablePage('/meeting/x')).toBeTruthy();
  70. // end with "edit"
  71. expect(isCreatablePage('/meeting/edit')).toBeFalsy();
  72. // under score
  73. expect(isCreatablePage('/_')).toBeTruthy();
  74. expect(isCreatablePage('/_template')).toBeTruthy();
  75. expect(isCreatablePage('/__template')).toBeTruthy();
  76. expect(isCreatablePage('/_r/x')).toBeFalsy();
  77. expect(isCreatablePage('/_api')).toBeFalsy();
  78. expect(isCreatablePage('/_apix')).toBeFalsy();
  79. expect(isCreatablePage('/_api/x')).toBeFalsy();
  80. expect(isCreatablePage('/hoge/xx.md')).toBeFalsy();
  81. // relative path
  82. expect(isCreatablePage('/..')).toBeFalsy();
  83. expect(isCreatablePage('/../page')).toBeFalsy();
  84. expect(isCreatablePage('/page/..')).toBeFalsy();
  85. expect(isCreatablePage('/page/../page')).toBeFalsy();
  86. // start with https?
  87. expect(isCreatablePage('/http://demo.growi.org/hoge')).toBeFalsy();
  88. expect(isCreatablePage('/https://demo.growi.org/hoge')).toBeFalsy();
  89. expect(isCreatablePage('http://demo.growi.org/hoge')).toBeFalsy();
  90. expect(isCreatablePage('https://demo.growi.org/hoge')).toBeFalsy();
  91. // include backslash
  92. expect(isCreatablePage('/foo\\/bar')).toBeFalsy();
  93. expect(isCreatablePage('/foo\\\\bar')).toBeFalsy();
  94. expect(isCreatablePage('/_search')).toBeFalsy();
  95. expect(isCreatablePage('/_search/foo')).toBeFalsy();
  96. expect(isCreatablePage('/_private-legacy-pages')).toBeFalsy();
  97. expect(isCreatablePage('/_private-legacy-pages/foo')).toBeFalsy();
  98. expect(isCreatablePage('/ the / path / with / space')).toBeFalsy();
  99. const forbidden = ['installer', 'register', 'login', 'logout',
  100. 'admin', 'files', 'trash', 'paste', 'comments'];
  101. for (let i = 0; i < forbidden.length; i++) {
  102. const pn = forbidden[i];
  103. expect(isCreatablePage(`/${pn}`)).toBeFalsy();
  104. expect(isCreatablePage(`/${pn}/`)).toBeFalsy();
  105. expect(isCreatablePage(`/${pn}/abc`)).toBeFalsy();
  106. }
  107. });
  108. describe('Test omitDuplicateAreaPathFromPaths', () => {
  109. test('Should not omit when all paths are at unique area', () => {
  110. const paths = ['/A', '/B/A', '/C/B/A', '/D'];
  111. const expectedPaths = paths;
  112. expect(omitDuplicateAreaPathFromPaths(paths)).toStrictEqual(paths);
  113. });
  114. test('Should omit when some paths are at duplicated area', () => {
  115. const paths = ['/A', '/A/A', '/A/B/A', '/B', '/B/A', '/AA'];
  116. const expectedPaths = ['/A', '/B', '/AA'];
  117. expect(omitDuplicateAreaPathFromPaths(paths)).toStrictEqual(expectedPaths);
  118. });
  119. test('Should omit when some long paths are at duplicated area', () => {
  120. const paths = ['/A/B/C', '/A/B/C/D', '/A/B/C/D/E'];
  121. const expectedPaths = ['/A/B/C'];
  122. expect(omitDuplicateAreaPathFromPaths(paths)).toStrictEqual(expectedPaths);
  123. });
  124. test('Should omit when some long paths are at duplicated area [case insensitivity]', () => {
  125. const paths = ['/a/B/C', '/A/b/C/D', '/A/B/c/D/E'];
  126. const expectedPaths = ['/a/B/C'];
  127. expect(omitDuplicateAreaPathFromPaths(paths)).toStrictEqual(expectedPaths);
  128. });
  129. });
  130. });