index.spec.ts 6.0 KB

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