v5-migration.test.js 6.2 KB

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