v5.migration.test.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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 UserGroup;
  8. let UserGroupRelation;
  9. let testUser1;
  10. let rootPage;
  11. beforeAll(async() => {
  12. jest.restoreAllMocks();
  13. crowi = await getInstance();
  14. Page = mongoose.model('Page');
  15. User = mongoose.model('User');
  16. UserGroup = mongoose.model('UserGroup');
  17. UserGroupRelation = mongoose.model('UserGroupRelation');
  18. await crowi.configManager.updateConfigsInTheSameNamespace('crowi', { 'app:isV5Compatible': true });
  19. await User.insertMany([{ name: 'testUser1', username: 'testUser1', email: 'testUser1@example.com' }]);
  20. testUser1 = await User.findOne({ username: 'testUser1' });
  21. rootPage = await Page.findOne({ path: '/' });
  22. });
  23. describe('normalizeParentRecursivelyByPages()', () => {
  24. test('should migrate all pages specified by pageIds', async() => {
  25. jest.restoreAllMocks();
  26. // initialize pages for test
  27. await Page.insertMany([
  28. {
  29. path: '/private1',
  30. grant: Page.GRANT_OWNER,
  31. creator: testUser1,
  32. lastUpdateUser: testUser1,
  33. grantedUsers: [testUser1._id],
  34. },
  35. {
  36. path: '/dummyParent/private1',
  37. grant: Page.GRANT_OWNER,
  38. creator: testUser1,
  39. lastUpdateUser: testUser1,
  40. grantedUsers: [testUser1._id],
  41. },
  42. {
  43. path: '/dummyParent/private1/private2',
  44. grant: Page.GRANT_OWNER,
  45. creator: testUser1,
  46. lastUpdateUser: testUser1,
  47. grantedUsers: [testUser1._id],
  48. },
  49. {
  50. path: '/dummyParent/private1/private3',
  51. grant: Page.GRANT_OWNER,
  52. creator: testUser1,
  53. lastUpdateUser: testUser1,
  54. grantedUsers: [testUser1._id],
  55. },
  56. ]);
  57. const pagesToRun = await Page.find({ path: { $in: ['/private1', '/dummyParent/private1'] } });
  58. // migrate
  59. await crowi.pageService.normalizeParentRecursivelyByPages(pagesToRun, testUser1);
  60. const migratedPages = await Page.find({
  61. path: {
  62. $in: ['/private1', '/dummyParent', '/dummyParent/private1', '/dummyParent/private1/private2', '/dummyParent/private1/private3'],
  63. },
  64. });
  65. const migratedPagePaths = migratedPages.filter(doc => doc.parent != null).map(doc => doc.path);
  66. const expected = ['/private1', '/dummyParent', '/dummyParent/private1', '/dummyParent/private1/private2', '/dummyParent/private1/private3'];
  67. expect(migratedPagePaths.sort()).toStrictEqual(expected.sort());
  68. });
  69. });
  70. describe('normalizeAllPublicPages()', () => {
  71. jest.setTimeout(60000);
  72. let createPagePaths;
  73. let allPossiblePagePaths;
  74. beforeAll(async() => {
  75. createPagePaths = [
  76. '/publicA', '/publicA/privateB', '/publicA/privateB/publicC', '/parenthesis/(a)[b]{c}d', '/parenthesis/(a)[b]{c}d/public', '/migratedD',
  77. ];
  78. allPossiblePagePaths = [...createPagePaths, '/parenthesis', '/'];
  79. // initialize pages for test
  80. await Page.insertMany([
  81. {
  82. path: '/publicA',
  83. grant: Page.GRANT_PUBLIC,
  84. creator: testUser1,
  85. lastUpdateUser: testUser1,
  86. },
  87. {
  88. path: '/publicA/privateB',
  89. grant: Page.GRANT_OWNER,
  90. creator: testUser1,
  91. lastUpdateUser: testUser1,
  92. grantedUsers: [testUser1._id],
  93. },
  94. {
  95. path: '/publicA/privateB/publicC',
  96. grant: Page.GRANT_PUBLIC,
  97. creator: testUser1,
  98. lastUpdateUser: testUser1,
  99. },
  100. {
  101. path: '/parenthesis/(a)[b]{c}d',
  102. grant: Page.GRANT_PUBLIC,
  103. creator: testUser1,
  104. lastUpdateUser: testUser1,
  105. },
  106. {
  107. path: '/parenthesis/(a)[b]{c}d/public',
  108. grant: Page.GRANT_PUBLIC,
  109. creator: testUser1,
  110. lastUpdateUser: testUser1,
  111. },
  112. ]);
  113. const parent = await Page.find({ path: '/' });
  114. await Page.insertMany([
  115. {
  116. path: '/migratedD',
  117. grant: Page.GRANT_PUBLIC,
  118. creator: testUser1,
  119. lastUpdateUser: testUser1,
  120. parent: parent._id,
  121. },
  122. ]);
  123. // migrate
  124. await crowi.pageService.normalizeAllPublicPages(Page.GRANT_PUBLIC);
  125. jest.setTimeout(30000);
  126. });
  127. test('should migrate all public pages', async() => {
  128. const migratedPages = await Page.find({
  129. path: {
  130. $in: allPossiblePagePaths,
  131. },
  132. parent: { $ne: null },
  133. });
  134. const migratedEmptyPages = await Page.find({
  135. path: {
  136. $in: allPossiblePagePaths,
  137. },
  138. isEmpty: true,
  139. parent: { $ne: null },
  140. });
  141. const nonMigratedPages = await Page.find({
  142. path: {
  143. $in: allPossiblePagePaths,
  144. },
  145. parent: null,
  146. });
  147. const migratedPaths = migratedPages.map(page => page.path).sort();
  148. const migratedEmptyPaths = migratedEmptyPages.map(page => page.path).sort();
  149. const nonMigratedPaths = nonMigratedPages.map(page => page.path).sort();
  150. const expectedMigratedPaths = allPossiblePagePaths.filter(path => path !== '/').sort();
  151. const expectedMigratedEmptyPaths = ['/publicA/privateB', '/parenthesis'].sort();
  152. const expectedNonMigratedPaths = ['/publicA/privateB', '/'].sort();
  153. expect(migratedPaths).toStrictEqual(expectedMigratedPaths);
  154. expect(migratedEmptyPaths).toStrictEqual(expectedMigratedEmptyPaths);
  155. expect(nonMigratedPaths).toStrictEqual(expectedNonMigratedPaths);
  156. });
  157. });
  158. describe('normalizeParentByPageId()', () => {
  159. const groupIdIsolate = new mongoose.Types.ObjectId();
  160. const groupIdA = new mongoose.Types.ObjectId();
  161. const groupIdB = new mongoose.Types.ObjectId();
  162. const pageId1 = new mongoose.Types.ObjectId();
  163. const pageId2 = new mongoose.Types.ObjectId();
  164. const pageId3 = new mongoose.Types.ObjectId();
  165. beforeAll(async() => {
  166. await UserGroup.insertMany([
  167. {
  168. _id: groupIdIsolate,
  169. name: 'groupIsolate',
  170. },
  171. {
  172. _id: groupIdA,
  173. name: 'groupA',
  174. },
  175. {
  176. _id: groupIdB,
  177. name: 'groupB',
  178. parent: groupIdA,
  179. },
  180. ]);
  181. await UserGroupRelation.insertMany([
  182. {
  183. relatedGroup: groupIdIsolate,
  184. relatedUser: testUser1._id,
  185. },
  186. ]);
  187. await Page.insertMany([
  188. {
  189. _id: pageId1,
  190. path: '/a',
  191. parent: rootPage._id,
  192. grant: Page.GRANT_PUBLIC,
  193. isEmpty: true,
  194. },
  195. {
  196. _id: pageId2,
  197. path: '/a/groupB',
  198. parent: pageId1,
  199. grant: Page.GRANT_USER_GROUP,
  200. grantedGroup: groupIdB,
  201. grantedUsers: [testUser1._id],
  202. creator: testUser1,
  203. lastUpdateUser: testUser1._id,
  204. },
  205. {
  206. _id: pageId3,
  207. path: '/a',
  208. grant: Page.GRANT_USER_GROUP,
  209. grantedGroup: groupIdA,
  210. grantedUsers: [testUser1._id],
  211. creator: testUser1,
  212. lastUpdateUser: testUser1._id,
  213. },
  214. ]);
  215. });
  216. });
  217. test('replace private parents with empty pages', async() => {
  218. const replacedPathPages = await Page.find({ path: '/publicA/privateB' }); // ex-private page
  219. const _newEmptyPage = replacedPathPages.filter(page => page.parent != null)[0];
  220. const newEmptyPage = {
  221. path: _newEmptyPage.path,
  222. grant: _newEmptyPage.grant,
  223. isEmpty: _newEmptyPage.isEmpty,
  224. };
  225. const expectedNewEmptyPage = {
  226. path: '/publicA/privateB',
  227. grant: Page.GRANT_PUBLIC,
  228. isEmpty: true,
  229. };
  230. const _privatePage = replacedPathPages.filter(page => page.parent == null)[0];
  231. const privatePage = {
  232. path: _privatePage.path,
  233. grant: _privatePage.grant,
  234. isEmpty: _privatePage.isEmpty,
  235. };
  236. const expectedPrivatePage = {
  237. path: '/publicA/privateB',
  238. grant: Page.GRANT_OWNER,
  239. isEmpty: false,
  240. };
  241. expect(replacedPathPages.length).toBe(2);
  242. expect(newEmptyPage).toStrictEqual(expectedNewEmptyPage);
  243. expect(privatePage).toStrictEqual(expectedPrivatePage);
  244. });
  245. });