v5-migration.test.js 5.7 KB

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