v5-migration.test.js 6.5 KB

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