v5.migration.test.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. const mongoose = require('mongoose');
  2. const { getInstance } = require('../setup-crowi');
  3. describe('V5 page migration', () => {
  4. let crowi;
  5. let Page;
  6. let User;
  7. let testUser1;
  8. beforeAll(async() => {
  9. jest.restoreAllMocks();
  10. crowi = await getInstance();
  11. Page = mongoose.model('Page');
  12. User = mongoose.model('User');
  13. await crowi.configManager.updateConfigsInTheSameNamespace('crowi', { 'app:isV5Compatible': true });
  14. await User.insertMany([{ name: 'testUser1', username: 'testUser1', email: 'testUser1@example.com' }]);
  15. testUser1 = await User.findOne({ username: 'testUser1' });
  16. });
  17. describe('normalizeParentRecursivelyByPageIds()', () => {
  18. test('should migrate all pages specified by pageIds', async() => {
  19. jest.restoreAllMocks();
  20. // initialize pages for test
  21. const pages = await Page.insertMany([
  22. {
  23. path: '/private1',
  24. grant: Page.GRANT_OWNER,
  25. creator: testUser1,
  26. lastUpdateUser: testUser1,
  27. grantedUsers: [testUser1._id],
  28. },
  29. {
  30. path: '/dummyParent/private1',
  31. grant: Page.GRANT_OWNER,
  32. creator: testUser1,
  33. lastUpdateUser: testUser1,
  34. grantedUsers: [testUser1._id],
  35. },
  36. {
  37. path: '/dummyParent/private1/private2',
  38. grant: Page.GRANT_OWNER,
  39. creator: testUser1,
  40. lastUpdateUser: testUser1,
  41. grantedUsers: [testUser1._id],
  42. },
  43. {
  44. path: '/dummyParent/private1/private3',
  45. grant: Page.GRANT_OWNER,
  46. creator: testUser1,
  47. lastUpdateUser: testUser1,
  48. grantedUsers: [testUser1._id],
  49. },
  50. ]);
  51. const pageIds = pages.map(page => page._id);
  52. // migrate
  53. await crowi.pageService.normalizeParentRecursivelyByPageIds(pageIds, testUser1);
  54. const migratedPages = await Page.find({
  55. path: {
  56. $in: ['/private1', '/dummyParent', '/dummyParent/private1', '/dummyParent/private1/private2', '/dummyParent/private1/private3'],
  57. },
  58. });
  59. const migratedPagePaths = migratedPages.filter(doc => doc.parent != null).map(doc => doc.path);
  60. const expected = ['/private1', '/dummyParent', '/dummyParent/private1', '/dummyParent/private1/private2', '/dummyParent/private1/private3'];
  61. expect(migratedPagePaths.sort()).toStrictEqual(expected.sort());
  62. });
  63. });
  64. describe('normalizeAllPublicPages()', () => {
  65. jest.setTimeout(60000);
  66. let createPagePaths;
  67. let allPossiblePagePaths;
  68. beforeAll(async() => {
  69. createPagePaths = [
  70. '/publicA', '/publicA/privateB', '/publicA/privateB/publicC', '/parenthesis/(a)[b]{c}d', '/parenthesis/(a)[b]{c}d/public', '/migratedD',
  71. ];
  72. allPossiblePagePaths = [...createPagePaths, '/parenthesis', '/'];
  73. // initialize pages for test
  74. await Page.insertMany([
  75. {
  76. path: '/publicA',
  77. grant: Page.GRANT_PUBLIC,
  78. creator: testUser1,
  79. lastUpdateUser: testUser1,
  80. },
  81. {
  82. path: '/publicA/privateB',
  83. grant: Page.GRANT_OWNER,
  84. creator: testUser1,
  85. lastUpdateUser: testUser1,
  86. grantedUsers: [testUser1._id],
  87. },
  88. {
  89. path: '/publicA/privateB/publicC',
  90. grant: Page.GRANT_PUBLIC,
  91. creator: testUser1,
  92. lastUpdateUser: testUser1,
  93. },
  94. {
  95. path: '/parenthesis/(a)[b]{c}d',
  96. grant: Page.GRANT_PUBLIC,
  97. creator: testUser1,
  98. lastUpdateUser: testUser1,
  99. },
  100. {
  101. path: '/parenthesis/(a)[b]{c}d/public',
  102. grant: Page.GRANT_PUBLIC,
  103. creator: testUser1,
  104. lastUpdateUser: testUser1,
  105. },
  106. ]);
  107. const parent = await Page.find({ path: '/' });
  108. await Page.insertMany([
  109. {
  110. path: '/migratedD',
  111. grant: Page.GRANT_PUBLIC,
  112. creator: testUser1,
  113. lastUpdateUser: testUser1,
  114. parent: parent._id,
  115. },
  116. ]);
  117. // migrate
  118. await crowi.pageService.normalizeAllPublicPages(Page.GRANT_PUBLIC);
  119. jest.setTimeout(30000);
  120. });
  121. test('should migrate all public pages', async() => {
  122. const migratedPages = await Page.find({
  123. path: {
  124. $in: allPossiblePagePaths,
  125. },
  126. parent: { $ne: null },
  127. });
  128. const migratedEmptyPages = await Page.find({
  129. path: {
  130. $in: allPossiblePagePaths,
  131. },
  132. isEmpty: true,
  133. parent: { $ne: null },
  134. });
  135. const nonMigratedPages = await Page.find({
  136. path: {
  137. $in: allPossiblePagePaths,
  138. },
  139. parent: null,
  140. });
  141. const migratedPaths = migratedPages.map(page => page.path).sort();
  142. const migratedEmptyPaths = migratedEmptyPages.map(page => page.path).sort();
  143. const nonMigratedPaths = nonMigratedPages.map(page => page.path).sort();
  144. const expectedMigratedPaths = allPossiblePagePaths.filter(path => path !== '/').sort();
  145. const expectedMigratedEmptyPaths = ['/publicA/privateB', '/parenthesis'].sort();
  146. const expectedNonMigratedPaths = ['/publicA/privateB', '/'].sort();
  147. expect(migratedPaths).toStrictEqual(expectedMigratedPaths);
  148. expect(migratedEmptyPaths).toStrictEqual(expectedMigratedEmptyPaths);
  149. expect(nonMigratedPaths).toStrictEqual(expectedNonMigratedPaths);
  150. });
  151. });
  152. test('replace private parents with empty pages', async() => {
  153. const replacedPathPages = await Page.find({ path: '/publicA/privateB' }); // ex-private page
  154. const _newEmptyPage = replacedPathPages.filter(page => page.parent != null)[0];
  155. const newEmptyPage = {
  156. path: _newEmptyPage.path,
  157. grant: _newEmptyPage.grant,
  158. isEmpty: _newEmptyPage.isEmpty,
  159. };
  160. const expectedNewEmptyPage = {
  161. path: '/publicA/privateB',
  162. grant: Page.GRANT_PUBLIC,
  163. isEmpty: true,
  164. };
  165. const _privatePage = replacedPathPages.filter(page => page.parent == null)[0];
  166. const privatePage = {
  167. path: _privatePage.path,
  168. grant: _privatePage.grant,
  169. isEmpty: _privatePage.isEmpty,
  170. };
  171. const expectedPrivatePage = {
  172. path: '/publicA/privateB',
  173. grant: Page.GRANT_OWNER,
  174. isEmpty: false,
  175. };
  176. expect(replacedPathPages.length).toBe(2);
  177. expect(newEmptyPage).toStrictEqual(expectedNewEmptyPage);
  178. expect(privatePage).toStrictEqual(expectedPrivatePage);
  179. });
  180. });