index.spec.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. // edge cases
  48. expect(isCreatablePage('/me')).toBeFalsy();
  49. expect(isCreatablePage('/me/')).toBeFalsy();
  50. expect(isCreatablePage('/me/x')).toBeFalsy();
  51. expect(isCreatablePage('/meeting')).toBeTruthy();
  52. expect(isCreatablePage('/meeting/x')).toBeTruthy();
  53. // end with "edit"
  54. expect(isCreatablePage('/meeting/edit')).toBeFalsy();
  55. // under score
  56. expect(isCreatablePage('/_')).toBeTruthy();
  57. expect(isCreatablePage('/_template')).toBeTruthy();
  58. expect(isCreatablePage('/__template')).toBeTruthy();
  59. expect(isCreatablePage('/_r/x')).toBeFalsy();
  60. expect(isCreatablePage('/_api')).toBeFalsy();
  61. expect(isCreatablePage('/_apix')).toBeFalsy();
  62. expect(isCreatablePage('/_api/x')).toBeFalsy();
  63. expect(isCreatablePage('/hoge/xx.md')).toBeFalsy();
  64. // relative path
  65. expect(isCreatablePage('/..')).toBeFalsy();
  66. expect(isCreatablePage('/../page')).toBeFalsy();
  67. expect(isCreatablePage('/page/..')).toBeFalsy();
  68. expect(isCreatablePage('/page/../page')).toBeFalsy();
  69. // start with https?
  70. expect(isCreatablePage('/http://demo.growi.org/hoge')).toBeFalsy();
  71. expect(isCreatablePage('/https://demo.growi.org/hoge')).toBeFalsy();
  72. expect(isCreatablePage('http://demo.growi.org/hoge')).toBeFalsy();
  73. expect(isCreatablePage('https://demo.growi.org/hoge')).toBeFalsy();
  74. // include backslash
  75. expect(isCreatablePage('/foo\\/bar')).toBeFalsy();
  76. expect(isCreatablePage('/foo\\\\bar')).toBeFalsy();
  77. expect(isCreatablePage('/_search')).toBeFalsy();
  78. expect(isCreatablePage('/_search/foo')).toBeFalsy();
  79. expect(isCreatablePage('/_private-legacy-pages')).toBeFalsy();
  80. expect(isCreatablePage('/_private-legacy-pages/foo')).toBeFalsy();
  81. expect(isCreatablePage('/ the / path / with / space')).toBeFalsy();
  82. const forbidden = ['installer', 'register', 'login', 'logout',
  83. 'admin', 'files', 'trash', 'paste', 'comments'];
  84. for (let i = 0; i < forbidden.length; i++) {
  85. const pn = forbidden[i];
  86. expect(isCreatablePage(`/${pn}`)).toBeFalsy();
  87. expect(isCreatablePage(`/${pn}/`)).toBeFalsy();
  88. expect(isCreatablePage(`/${pn}/abc`)).toBeFalsy();
  89. }
  90. });
  91. describe.concurrent('Test omitDuplicateAreaPathFromPaths', () => {
  92. test.concurrent('Should not omit when all paths are at unique area', () => {
  93. const paths = ['/A', '/B/A', '/C/B/A', '/D'];
  94. const expectedPaths = paths;
  95. expect(omitDuplicateAreaPathFromPaths(paths)).toStrictEqual(expectedPaths);
  96. });
  97. test.concurrent('Should omit when some paths are at duplicated area', () => {
  98. const paths = ['/A', '/A/A', '/A/B/A', '/B', '/B/A', '/AA'];
  99. const expectedPaths = ['/A', '/B', '/AA'];
  100. expect(omitDuplicateAreaPathFromPaths(paths)).toStrictEqual(expectedPaths);
  101. });
  102. test.concurrent('Should omit when some long paths are at duplicated area', () => {
  103. const paths = ['/A/B/C', '/A/B/C/D', '/A/B/C/D/E'];
  104. const expectedPaths = ['/A/B/C'];
  105. expect(omitDuplicateAreaPathFromPaths(paths)).toStrictEqual(expectedPaths);
  106. });
  107. test.concurrent('Should omit when some long paths are at duplicated area [case insensitivity]', () => {
  108. const paths = ['/a/B/C', '/A/b/C/D', '/A/B/c/D/E'];
  109. const expectedPaths = ['/a/B/C'];
  110. expect(omitDuplicateAreaPathFromPaths(paths)).toStrictEqual(expectedPaths);
  111. });
  112. });
  113. describe.concurrent('Test getUsernameByPath', () => {
  114. test.concurrent('found', () => {
  115. const username = getUsernameByPath('/user/sotarok');
  116. expect(username).toBe('sotarok');
  117. });
  118. test.concurrent('found with slash', () => {
  119. const username = getUsernameByPath('/user/some.user.name12/');
  120. expect(username).toBe('some.user.name12');
  121. });
  122. test.concurrent('not found', () => {
  123. const username = getUsernameByPath('/the/page/is/not/related/to/user/page');
  124. expect(username).toBeNull();
  125. });
  126. });
  127. });