index.spec.ts 6.4 KB

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