v5.migration.test.js 6.6 KB

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