index.spec.ts 5.3 KB

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