v5.non-public-page.test.ts 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722
  1. /* eslint-disable no-unused-vars */
  2. import { GroupType, type IGrantedGroup } from '@growi/core';
  3. import mongoose from 'mongoose';
  4. import { ExternalGroupProviderType } from '../../../src/features/external-user-group/interfaces/external-user-group';
  5. import ExternalUserGroup from '../../../src/features/external-user-group/server/models/external-user-group';
  6. import ExternalUserGroupRelation from '../../../src/features/external-user-group/server/models/external-user-group-relation';
  7. import type { IPageTagRelation } from '../../../src/interfaces/page-tag-relation';
  8. import PageTagRelation from '../../../src/server/models/page-tag-relation';
  9. import Tag from '../../../src/server/models/tag';
  10. import UserGroup from '../../../src/server/models/user-group';
  11. import UserGroupRelation from '../../../src/server/models/user-group-relation';
  12. import { getInstance } from '../setup-crowi';
  13. describe('PageService page operations with non-public pages', () => {
  14. let dummyUser1;
  15. let dummyUser2;
  16. let npDummyUser1;
  17. let npDummyUser2;
  18. let npDummyUser3;
  19. let groupIdIsolate;
  20. let groupIdA;
  21. let groupIdB;
  22. let groupIdC;
  23. let externalGroupIdIsolate;
  24. let externalGroupIdA;
  25. let externalGroupIdB;
  26. let externalGroupIdC;
  27. let crowi;
  28. let Page;
  29. let Revision;
  30. let User;
  31. let xssSpy;
  32. let rootPage;
  33. /**
  34. * Rename
  35. */
  36. const pageIdRename1 = new mongoose.Types.ObjectId();
  37. const pageIdRename2 = new mongoose.Types.ObjectId();
  38. const pageIdRename3 = new mongoose.Types.ObjectId();
  39. const pageIdRename4 = new mongoose.Types.ObjectId();
  40. const pageIdRename5 = new mongoose.Types.ObjectId();
  41. const pageIdRename6 = new mongoose.Types.ObjectId();
  42. const pageIdRename7 = new mongoose.Types.ObjectId();
  43. const pageIdRename8 = new mongoose.Types.ObjectId();
  44. const pageIdRename9 = new mongoose.Types.ObjectId();
  45. /**
  46. * Duplicate
  47. */
  48. // page id
  49. const pageIdDuplicate1 = new mongoose.Types.ObjectId();
  50. const pageIdDuplicate2 = new mongoose.Types.ObjectId();
  51. const pageIdDuplicate3 = new mongoose.Types.ObjectId();
  52. const pageIdDuplicate4 = new mongoose.Types.ObjectId();
  53. const pageIdDuplicate5 = new mongoose.Types.ObjectId();
  54. const pageIdDuplicate6 = new mongoose.Types.ObjectId();
  55. const pageIdDuplicate7 = new mongoose.Types.ObjectId();
  56. const pageIdDuplicate8 = new mongoose.Types.ObjectId();
  57. const pageIdDuplicate9 = new mongoose.Types.ObjectId();
  58. // revision id
  59. const revisionIdDuplicate1 = new mongoose.Types.ObjectId();
  60. const revisionIdDuplicate2 = new mongoose.Types.ObjectId();
  61. const revisionIdDuplicate3 = new mongoose.Types.ObjectId();
  62. const revisionIdDuplicate4 = new mongoose.Types.ObjectId();
  63. const revisionIdDuplicate5 = new mongoose.Types.ObjectId();
  64. const revisionIdDuplicate6 = new mongoose.Types.ObjectId();
  65. const revisionIdDuplicate7 = new mongoose.Types.ObjectId();
  66. const revisionIdDuplicate8 = new mongoose.Types.ObjectId();
  67. const revisionIdDuplicate9 = new mongoose.Types.ObjectId();
  68. /**
  69. * Revert
  70. */
  71. // page id
  72. const pageIdRevert1 = new mongoose.Types.ObjectId();
  73. const pageIdRevert2 = new mongoose.Types.ObjectId();
  74. const pageIdRevert3 = new mongoose.Types.ObjectId();
  75. const pageIdRevert4 = new mongoose.Types.ObjectId();
  76. const pageIdRevert5 = new mongoose.Types.ObjectId();
  77. const pageIdRevert6 = new mongoose.Types.ObjectId();
  78. // revision id
  79. const revisionIdRevert1 = new mongoose.Types.ObjectId();
  80. const revisionIdRevert2 = new mongoose.Types.ObjectId();
  81. const revisionIdRevert3 = new mongoose.Types.ObjectId();
  82. const revisionIdRevert4 = new mongoose.Types.ObjectId();
  83. const revisionIdRevert5 = new mongoose.Types.ObjectId();
  84. const revisionIdRevert6 = new mongoose.Types.ObjectId();
  85. // tag id
  86. const tagIdRevert1 = new mongoose.Types.ObjectId();
  87. const tagIdRevert2 = new mongoose.Types.ObjectId();
  88. const create = async(path, body, user, options = {}) => {
  89. const mockedCreateSubOperation = jest.spyOn(crowi.pageService, 'createSubOperation').mockReturnValue(null);
  90. const createdPage = await crowi.pageService.create(path, body, user, options);
  91. const argsForCreateSubOperation = mockedCreateSubOperation.mock.calls[0];
  92. mockedCreateSubOperation.mockRestore();
  93. await crowi.pageService.createSubOperation(...argsForCreateSubOperation);
  94. return createdPage;
  95. };
  96. // normalize for result comparison
  97. const normalizeGrantedGroups = (grantedGroups: IGrantedGroup[]) => {
  98. return grantedGroups.map((group) => {
  99. const itemId = typeof group.item === 'string' ? group.item : group.item._id;
  100. return { item: itemId, type: group.type };
  101. });
  102. };
  103. beforeAll(async() => {
  104. crowi = await getInstance();
  105. await crowi.configManager.updateConfigsInTheSameNamespace('crowi', { 'app:isV5Compatible': true });
  106. User = mongoose.model('User');
  107. Page = mongoose.model('Page');
  108. Revision = mongoose.model('Revision');
  109. /*
  110. * Common
  111. */
  112. const npUserId1 = new mongoose.Types.ObjectId();
  113. const npUserId2 = new mongoose.Types.ObjectId();
  114. const npUserId3 = new mongoose.Types.ObjectId();
  115. await User.insertMany([
  116. {
  117. _id: npUserId1, name: 'npUser1', username: 'npUser1', email: 'npUser1@example.com',
  118. },
  119. {
  120. _id: npUserId2, name: 'npUser2', username: 'npUser2', email: 'npUser2@example.com',
  121. },
  122. {
  123. _id: npUserId3, name: 'npUser3', username: 'npUser3', email: 'npUser3@example.com',
  124. },
  125. ]);
  126. groupIdIsolate = new mongoose.Types.ObjectId();
  127. groupIdA = new mongoose.Types.ObjectId();
  128. groupIdB = new mongoose.Types.ObjectId();
  129. groupIdC = new mongoose.Types.ObjectId();
  130. await UserGroup.insertMany([
  131. {
  132. _id: groupIdIsolate,
  133. name: 'np_groupIsolate',
  134. },
  135. {
  136. _id: groupIdA,
  137. name: 'np_groupA',
  138. },
  139. {
  140. _id: groupIdB,
  141. name: 'np_groupB',
  142. parent: groupIdA,
  143. },
  144. {
  145. _id: groupIdC,
  146. name: 'np_groupC',
  147. parent: groupIdB,
  148. },
  149. ]);
  150. await UserGroupRelation.insertMany([
  151. {
  152. relatedGroup: groupIdIsolate,
  153. relatedUser: npUserId1,
  154. createdAt: new Date(),
  155. },
  156. {
  157. relatedGroup: groupIdIsolate,
  158. relatedUser: npUserId2,
  159. createdAt: new Date(),
  160. },
  161. {
  162. relatedGroup: groupIdA,
  163. relatedUser: npUserId1,
  164. createdAt: new Date(),
  165. },
  166. {
  167. relatedGroup: groupIdA,
  168. relatedUser: npUserId2,
  169. createdAt: new Date(),
  170. },
  171. {
  172. relatedGroup: groupIdA,
  173. relatedUser: npUserId3,
  174. createdAt: new Date(),
  175. },
  176. {
  177. relatedGroup: groupIdB,
  178. relatedUser: npUserId2,
  179. createdAt: new Date(),
  180. },
  181. {
  182. relatedGroup: groupIdB,
  183. relatedUser: npUserId3,
  184. createdAt: new Date(),
  185. },
  186. {
  187. relatedGroup: groupIdC,
  188. relatedUser: npUserId3,
  189. createdAt: new Date(),
  190. },
  191. ]);
  192. // Insert ExternalUserGroups with the same group structure as UserGroups
  193. // Use to test
  194. // - ExternalUserGroup
  195. // - Case of multiple grantedGroups for Page
  196. externalGroupIdIsolate = new mongoose.Types.ObjectId();
  197. externalGroupIdA = new mongoose.Types.ObjectId();
  198. externalGroupIdB = new mongoose.Types.ObjectId();
  199. externalGroupIdC = new mongoose.Types.ObjectId();
  200. await ExternalUserGroup.insertMany([
  201. {
  202. _id: externalGroupIdIsolate,
  203. name: 'np_externalGroupIsolate',
  204. externalId: 'np_externalGroupIsolate',
  205. provider: ExternalGroupProviderType.ldap,
  206. },
  207. {
  208. _id: externalGroupIdA,
  209. name: 'np_externalGroupA',
  210. externalId: 'np_externalGroupA',
  211. provider: ExternalGroupProviderType.ldap,
  212. },
  213. {
  214. _id: externalGroupIdB,
  215. name: 'np_externalGroupB',
  216. externalId: 'np_externalGroupB',
  217. parent: externalGroupIdA,
  218. provider: ExternalGroupProviderType.ldap,
  219. },
  220. {
  221. _id: externalGroupIdC,
  222. name: 'np_externalGroupC',
  223. externalId: 'np_externalGroupC',
  224. parent: externalGroupIdB,
  225. provider: ExternalGroupProviderType.ldap,
  226. },
  227. ]);
  228. await ExternalUserGroupRelation.insertMany([
  229. {
  230. relatedGroup: externalGroupIdIsolate,
  231. relatedUser: npUserId1,
  232. createdAt: new Date(),
  233. },
  234. {
  235. relatedGroup: externalGroupIdIsolate,
  236. relatedUser: npUserId2,
  237. createdAt: new Date(),
  238. },
  239. {
  240. relatedGroup: externalGroupIdA,
  241. relatedUser: npUserId1,
  242. createdAt: new Date(),
  243. },
  244. {
  245. relatedGroup: externalGroupIdA,
  246. relatedUser: npUserId2,
  247. createdAt: new Date(),
  248. },
  249. {
  250. relatedGroup: externalGroupIdA,
  251. relatedUser: npUserId3,
  252. createdAt: new Date(),
  253. },
  254. {
  255. relatedGroup: externalGroupIdB,
  256. relatedUser: npUserId2,
  257. createdAt: new Date(),
  258. },
  259. {
  260. relatedGroup: externalGroupIdB,
  261. relatedUser: npUserId3,
  262. createdAt: new Date(),
  263. },
  264. {
  265. relatedGroup: externalGroupIdC,
  266. relatedUser: npUserId3,
  267. createdAt: new Date(),
  268. },
  269. ]);
  270. xssSpy = jest.spyOn(crowi.xss, 'process').mockImplementation(path => path);
  271. dummyUser1 = await User.findOne({ username: 'v5DummyUser1' });
  272. dummyUser2 = await User.findOne({ username: 'v5DummyUser2' });
  273. npDummyUser1 = await User.findOne({ username: 'npUser1' });
  274. npDummyUser2 = await User.findOne({ username: 'npUser2' });
  275. npDummyUser3 = await User.findOne({ username: 'npUser3' });
  276. rootPage = await Page.findOne({ path: '/' });
  277. if (rootPage == null) {
  278. const pages = await Page.insertMany([{ path: '/', grant: Page.GRANT_PUBLIC }]);
  279. rootPage = pages[0];
  280. }
  281. /**
  282. * create
  283. * mc_ => model create
  284. * emp => empty => page with isEmpty: true
  285. * pub => public => GRANT_PUBLIC
  286. */
  287. const pageIdCreate1 = new mongoose.Types.ObjectId();
  288. const pageIdCreate2 = new mongoose.Types.ObjectId();
  289. const pageIdCreate3 = new mongoose.Types.ObjectId();
  290. await Page.insertMany([
  291. {
  292. _id: pageIdCreate1,
  293. path: '/mc4_top/mc1_emp',
  294. grant: Page.GRANT_PUBLIC,
  295. creator: dummyUser1,
  296. lastUpdateUser: dummyUser1._id,
  297. parent: rootPage._id,
  298. isEmpty: true,
  299. },
  300. {
  301. path: '/mc4_top/mc1_emp/mc2_pub',
  302. grant: Page.GRANT_PUBLIC,
  303. creator: dummyUser1,
  304. lastUpdateUser: dummyUser1._id,
  305. parent: pageIdCreate1,
  306. isEmpty: false,
  307. },
  308. {
  309. path: '/mc5_top/mc3_awl',
  310. grant: Page.GRANT_RESTRICTED,
  311. creator: dummyUser1,
  312. lastUpdateUser: dummyUser1._id,
  313. isEmpty: false,
  314. },
  315. {
  316. _id: pageIdCreate2,
  317. path: '/mc4_top',
  318. grant: Page.GRANT_PUBLIC,
  319. creator: dummyUser1,
  320. lastUpdateUser: dummyUser1._id,
  321. isEmpty: false,
  322. parent: rootPage._id,
  323. descendantCount: 1,
  324. },
  325. {
  326. _id: pageIdCreate3,
  327. path: '/mc5_top',
  328. grant: Page.GRANT_PUBLIC,
  329. creator: dummyUser1,
  330. lastUpdateUser: dummyUser1._id,
  331. isEmpty: false,
  332. parent: rootPage._id,
  333. descendantCount: 0,
  334. },
  335. ]);
  336. /**
  337. * create
  338. * mc_ => model create
  339. * emp => empty => page with isEmpty: true
  340. * pub => public => GRANT_PUBLIC
  341. */
  342. const pageIdCreateBySystem1 = new mongoose.Types.ObjectId();
  343. const pageIdCreateBySystem2 = new mongoose.Types.ObjectId();
  344. const pageIdCreateBySystem3 = new mongoose.Types.ObjectId();
  345. await Page.insertMany([
  346. {
  347. _id: pageIdCreateBySystem1,
  348. path: '/mc4_top_by_system/mc1_emp_by_system',
  349. grant: Page.GRANT_PUBLIC,
  350. creator: dummyUser1,
  351. lastUpdateUser: dummyUser1._id,
  352. parent: rootPage._id,
  353. isEmpty: true,
  354. },
  355. {
  356. path: '/mc4_top_by_system/mc1_emp_by_system/mc2_pub_by_system',
  357. grant: Page.GRANT_PUBLIC,
  358. creator: dummyUser1,
  359. lastUpdateUser: dummyUser1._id,
  360. parent: pageIdCreateBySystem1,
  361. isEmpty: false,
  362. },
  363. {
  364. path: '/mc5_top_by_system/mc3_awl_by_system',
  365. grant: Page.GRANT_RESTRICTED,
  366. creator: dummyUser1,
  367. lastUpdateUser: dummyUser1._id,
  368. isEmpty: false,
  369. },
  370. {
  371. _id: pageIdCreateBySystem2,
  372. path: '/mc4_top_by_system',
  373. grant: Page.GRANT_PUBLIC,
  374. creator: dummyUser1,
  375. lastUpdateUser: dummyUser1._id,
  376. isEmpty: false,
  377. parent: rootPage._id,
  378. descendantCount: 1,
  379. },
  380. {
  381. _id: pageIdCreateBySystem3,
  382. path: '/mc5_top_by_system',
  383. grant: Page.GRANT_PUBLIC,
  384. creator: dummyUser1,
  385. lastUpdateUser: dummyUser1._id,
  386. isEmpty: false,
  387. parent: rootPage._id,
  388. descendantCount: 0,
  389. },
  390. ]);
  391. /*
  392. * Rename
  393. */
  394. await Page.insertMany([
  395. {
  396. _id: pageIdRename1,
  397. path: '/np_rename1_destination',
  398. grant: Page.GRANT_PUBLIC,
  399. creator: dummyUser1._id,
  400. lastUpdateUser: dummyUser1._id,
  401. parent: rootPage._id,
  402. },
  403. {
  404. _id: pageIdRename2,
  405. path: '/np_rename2',
  406. grant: Page.GRANT_USER_GROUP,
  407. grantedGroups: [{ item: groupIdB, type: GroupType.userGroup }, { item: externalGroupIdB, type: GroupType.externalUserGroup }],
  408. creator: npDummyUser2._id,
  409. lastUpdateUser: npDummyUser2._id,
  410. parent: rootPage._id,
  411. },
  412. {
  413. _id: pageIdRename3,
  414. path: '/np_rename2/np_rename3',
  415. grant: Page.GRANT_USER_GROUP,
  416. grantedGroups: [{ item: groupIdC, type: GroupType.userGroup }, { item: externalGroupIdC, type: GroupType.externalUserGroup }],
  417. creator: npDummyUser3._id,
  418. lastUpdateUser: npDummyUser3._id,
  419. parent: pageIdRename2._id,
  420. },
  421. {
  422. _id: pageIdRename4,
  423. path: '/np_rename4_destination',
  424. grant: Page.GRANT_USER_GROUP,
  425. grantedGroups: [{ item: groupIdIsolate, type: GroupType.userGroup }, { item: externalGroupIdIsolate, type: GroupType.externalUserGroup }],
  426. creator: npDummyUser3._id,
  427. lastUpdateUser: npDummyUser3._id,
  428. parent: rootPage._id,
  429. },
  430. {
  431. _id: pageIdRename5,
  432. path: '/np_rename5',
  433. grant: Page.GRANT_USER_GROUP,
  434. grantedGroups: [{ item: groupIdB, type: GroupType.userGroup }, { item: externalGroupIdB, type: GroupType.externalUserGroup }],
  435. creator: npDummyUser2._id,
  436. lastUpdateUser: npDummyUser2._id,
  437. parent: rootPage._id,
  438. },
  439. {
  440. _id: pageIdRename6,
  441. path: '/np_rename5/np_rename6',
  442. grant: Page.GRANT_USER_GROUP,
  443. grantedGroups: [{ item: groupIdB, type: GroupType.userGroup }, { item: externalGroupIdB, type: GroupType.externalUserGroup }],
  444. creator: npDummyUser2._id,
  445. lastUpdateUser: npDummyUser2._id,
  446. parent: pageIdRename5,
  447. },
  448. {
  449. _id: pageIdRename7,
  450. path: '/np_rename7_destination',
  451. grant: Page.GRANT_USER_GROUP,
  452. grantedGroups: [{ item: groupIdIsolate, type: GroupType.userGroup }, { item: externalGroupIdIsolate, type: GroupType.externalUserGroup }],
  453. creator: npDummyUser2._id,
  454. lastUpdateUser: npDummyUser2._id,
  455. parent: pageIdRename5,
  456. },
  457. {
  458. _id: pageIdRename8,
  459. path: '/np_rename8',
  460. grant: Page.GRANT_RESTRICTED,
  461. creator: dummyUser1._id,
  462. lastUpdateUser: dummyUser1._id,
  463. },
  464. {
  465. _id: pageIdRename9,
  466. path: '/np_rename8/np_rename9',
  467. grant: Page.GRANT_RESTRICTED,
  468. creator: dummyUser2._id,
  469. lastUpdateUser: dummyUser2._id,
  470. },
  471. ]);
  472. /*
  473. * Duplicate
  474. */
  475. await Page.insertMany([
  476. {
  477. _id: pageIdDuplicate1,
  478. path: '/np_duplicate1',
  479. grant: Page.GRANT_RESTRICTED,
  480. creator: dummyUser1._id,
  481. lastUpdateUser: dummyUser1._id,
  482. revision: revisionIdDuplicate1,
  483. },
  484. {
  485. _id: pageIdDuplicate2,
  486. path: '/np_duplicate2',
  487. grant: Page.GRANT_USER_GROUP,
  488. grantedGroups: [
  489. { item: groupIdA, type: GroupType.userGroup },
  490. { item: externalGroupIdA, type: GroupType.externalUserGroup },
  491. ],
  492. creator: npDummyUser1._id,
  493. lastUpdateUser: npDummyUser1._id,
  494. revision: revisionIdDuplicate2,
  495. parent: rootPage._id,
  496. },
  497. {
  498. _id: pageIdDuplicate3,
  499. path: '/np_duplicate2/np_duplicate3',
  500. grant: Page.GRANT_USER_GROUP,
  501. grantedGroups: [
  502. { item: groupIdB, type: GroupType.userGroup },
  503. { item: externalGroupIdB, type: GroupType.externalUserGroup },
  504. ],
  505. creator: npDummyUser2._id,
  506. lastUpdateUser: npDummyUser2._id,
  507. revision: revisionIdDuplicate3,
  508. parent: pageIdDuplicate2,
  509. },
  510. {
  511. _id: pageIdDuplicate4,
  512. path: '/np_duplicate4',
  513. grant: Page.GRANT_PUBLIC,
  514. creator: npDummyUser1._id,
  515. lastUpdateUser: npDummyUser1._id,
  516. revision: revisionIdDuplicate4,
  517. parent: rootPage._id,
  518. },
  519. {
  520. _id: pageIdDuplicate5,
  521. path: '/np_duplicate4/np_duplicate5',
  522. grant: Page.GRANT_RESTRICTED,
  523. creator: npDummyUser1._id,
  524. lastUpdateUser: npDummyUser1._id,
  525. revision: revisionIdDuplicate5,
  526. },
  527. {
  528. _id: pageIdDuplicate6,
  529. path: '/np_duplicate4/np_duplicate6',
  530. grant: Page.GRANT_PUBLIC,
  531. creator: npDummyUser1._id,
  532. lastUpdateUser: npDummyUser1._id,
  533. parent: pageIdDuplicate4,
  534. revision: revisionIdDuplicate6,
  535. },
  536. {
  537. _id: pageIdDuplicate7,
  538. path: '/np_duplicate7',
  539. grant: Page.GRANT_USER_GROUP,
  540. creator: npDummyUser1._id,
  541. lastUpdateUser: npDummyUser1._id,
  542. parent: rootPage._id,
  543. revision: revisionIdDuplicate7,
  544. grantedGroups: [
  545. { item: groupIdA, type: GroupType.userGroup },
  546. { item: externalGroupIdA, type: GroupType.externalUserGroup },
  547. { item: groupIdB, type: GroupType.userGroup },
  548. { item: externalGroupIdB, type: GroupType.externalUserGroup },
  549. ],
  550. },
  551. {
  552. _id: pageIdDuplicate8,
  553. path: '/np_duplicate7/np_duplicate8',
  554. grant: Page.GRANT_USER_GROUP,
  555. creator: npDummyUser3._id,
  556. lastUpdateUser: npDummyUser3._id,
  557. parent: pageIdDuplicate7,
  558. revision: revisionIdDuplicate8,
  559. grantedGroups: [
  560. { item: groupIdC, type: GroupType.userGroup },
  561. { item: externalGroupIdC, type: GroupType.externalUserGroup },
  562. ],
  563. },
  564. {
  565. _id: pageIdDuplicate9,
  566. path: '/np_duplicate7/np_duplicate9',
  567. grant: Page.GRANT_OWNER,
  568. creator: npDummyUser2._id,
  569. lastUpdateUser: npDummyUser2._id,
  570. parent: pageIdDuplicate7,
  571. revision: revisionIdDuplicate9,
  572. grantedUsers: [npDummyUser2._id],
  573. },
  574. ]);
  575. await Revision.insertMany([
  576. {
  577. _id: revisionIdDuplicate1,
  578. body: 'np_duplicate1',
  579. format: 'markdown',
  580. pageId: pageIdDuplicate1,
  581. author: npDummyUser1._id,
  582. },
  583. {
  584. _id: revisionIdDuplicate2,
  585. body: 'np_duplicate2',
  586. format: 'markdown',
  587. pageId: pageIdDuplicate2,
  588. author: npDummyUser2._id,
  589. },
  590. {
  591. _id: revisionIdDuplicate3,
  592. body: 'np_duplicate3',
  593. format: 'markdown',
  594. pageId: pageIdDuplicate3,
  595. author: npDummyUser2._id,
  596. },
  597. {
  598. _id: revisionIdDuplicate4,
  599. body: 'np_duplicate4',
  600. format: 'markdown',
  601. pageId: pageIdDuplicate4,
  602. author: npDummyUser2._id,
  603. },
  604. {
  605. _id: revisionIdDuplicate5,
  606. body: 'np_duplicate5',
  607. format: 'markdown',
  608. pageId: pageIdDuplicate5,
  609. author: npDummyUser2._id,
  610. },
  611. {
  612. _id: revisionIdDuplicate6,
  613. body: 'np_duplicate6',
  614. format: 'markdown',
  615. pageId: pageIdDuplicate6,
  616. author: npDummyUser1._id,
  617. },
  618. {
  619. _id: revisionIdDuplicate7,
  620. body: 'np_duplicate7',
  621. format: 'markdown',
  622. pageId: pageIdDuplicate7,
  623. author: npDummyUser1._id,
  624. },
  625. {
  626. _id: revisionIdDuplicate8,
  627. body: 'np_duplicate8',
  628. format: 'markdown',
  629. pageId: pageIdDuplicate8,
  630. author: npDummyUser3._id,
  631. },
  632. {
  633. _id: revisionIdDuplicate9,
  634. body: 'np_duplicate9',
  635. format: 'markdown',
  636. pageId: pageIdDuplicate9,
  637. author: npDummyUser2._id,
  638. },
  639. ]);
  640. /**
  641. * Delete
  642. */
  643. const pageIdDelete1 = new mongoose.Types.ObjectId();
  644. const pageIdDelete2 = new mongoose.Types.ObjectId();
  645. const pageIdDelete3 = new mongoose.Types.ObjectId();
  646. const pageIdDelete4 = new mongoose.Types.ObjectId();
  647. await Page.insertMany([
  648. {
  649. _id: pageIdDelete1,
  650. path: '/npdel1_awl',
  651. grant: Page.GRANT_RESTRICTED,
  652. status: Page.STATUS_PUBLISHED,
  653. isEmpty: false,
  654. },
  655. {
  656. _id: pageIdDelete2,
  657. path: '/npdel2_ug',
  658. grant: Page.GRANT_USER_GROUP,
  659. grantedGroups: [{ item: groupIdA, type: GroupType.userGroup }, { item: externalGroupIdA, type: GroupType.externalUserGroup }],
  660. status: Page.STATUS_PUBLISHED,
  661. isEmpty: false,
  662. parent: rootPage._id,
  663. descendantCount: 0,
  664. },
  665. {
  666. _id: pageIdDelete3,
  667. path: '/npdel3_top',
  668. grant: Page.GRANT_USER_GROUP,
  669. grantedGroups: [{ item: groupIdA, type: GroupType.userGroup }, { item: externalGroupIdA, type: GroupType.externalUserGroup }],
  670. status: Page.STATUS_PUBLISHED,
  671. isEmpty: false,
  672. parent: rootPage._id,
  673. descendantCount: 2,
  674. },
  675. {
  676. _id: pageIdDelete4,
  677. path: '/npdel3_top/npdel4_ug',
  678. grant: Page.GRANT_USER_GROUP,
  679. grantedGroups: [{ item: groupIdB, type: GroupType.userGroup }, { item: externalGroupIdB, type: GroupType.externalUserGroup }],
  680. status: Page.STATUS_PUBLISHED,
  681. isEmpty: false,
  682. parent: pageIdDelete3._id,
  683. descendantCount: 1,
  684. },
  685. {
  686. path: '/npdel3_top/npdel4_ug',
  687. grant: Page.GRANT_RESTRICTED,
  688. status: Page.STATUS_PUBLISHED,
  689. isEmpty: false,
  690. },
  691. {
  692. path: '/npdel3_top/npdel4_ug/npdel5_ug',
  693. grant: Page.GRANT_USER_GROUP,
  694. grantedGroups: [{ item: groupIdC, type: GroupType.userGroup }, { item: externalGroupIdC, type: GroupType.externalUserGroup }],
  695. status: Page.STATUS_PUBLISHED,
  696. isEmpty: false,
  697. parent: pageIdDelete4._id,
  698. descendantCount: 0,
  699. },
  700. ]);
  701. /**
  702. * Delete completely
  703. */
  704. const pageIdDeleteComp1 = new mongoose.Types.ObjectId();
  705. const pageIdDeleteComp2 = new mongoose.Types.ObjectId();
  706. await Page.insertMany([
  707. {
  708. path: '/npdc1_awl',
  709. grant: Page.GRANT_RESTRICTED,
  710. status: Page.STATUS_PUBLISHED,
  711. isEmpty: false,
  712. },
  713. {
  714. path: '/npdc2_ug',
  715. grant: Page.GRANT_USER_GROUP,
  716. grantedGroups: [{ item: groupIdA, type: GroupType.userGroup }, { item: externalGroupIdA, type: GroupType.externalUserGroup }],
  717. status: Page.STATUS_PUBLISHED,
  718. isEmpty: false,
  719. parent: rootPage._id,
  720. },
  721. {
  722. _id: pageIdDeleteComp1,
  723. path: '/npdc3_ug',
  724. grant: Page.GRANT_USER_GROUP,
  725. grantedGroups: [{ item: groupIdA, type: GroupType.userGroup }, { item: externalGroupIdA, type: GroupType.externalUserGroup }],
  726. status: Page.STATUS_PUBLISHED,
  727. isEmpty: false,
  728. parent: rootPage._id,
  729. },
  730. {
  731. _id: pageIdDeleteComp2,
  732. path: '/npdc3_ug/npdc4_ug',
  733. grant: Page.GRANT_USER_GROUP,
  734. grantedGroups: [{ item: groupIdB, type: GroupType.userGroup }, { item: externalGroupIdB, type: GroupType.externalUserGroup }],
  735. status: Page.STATUS_PUBLISHED,
  736. isEmpty: false,
  737. parent: pageIdDeleteComp1,
  738. },
  739. {
  740. path: '/npdc3_ug/npdc4_ug/npdc5_ug',
  741. grant: Page.GRANT_USER_GROUP,
  742. grantedGroups: [{ item: groupIdC, type: GroupType.userGroup }, { item: externalGroupIdC, type: GroupType.externalUserGroup }],
  743. status: Page.STATUS_PUBLISHED,
  744. isEmpty: false,
  745. parent: pageIdDeleteComp2,
  746. },
  747. {
  748. path: '/npdc3_ug/npdc4_ug',
  749. grant: Page.GRANT_RESTRICTED,
  750. status: Page.STATUS_PUBLISHED,
  751. isEmpty: false,
  752. },
  753. ]);
  754. /**
  755. * Revert
  756. */
  757. await Page.insertMany([
  758. {
  759. _id: pageIdRevert1,
  760. path: '/trash/np_revert1',
  761. grant: Page.GRANT_RESTRICTED,
  762. revision: revisionIdRevert1,
  763. status: Page.STATUS_DELETED,
  764. },
  765. {
  766. _id: pageIdRevert2,
  767. path: '/trash/np_revert2',
  768. grant: Page.GRANT_USER_GROUP,
  769. grantedGroups: [{ item: groupIdA, type: GroupType.userGroup }, { item: externalGroupIdA, type: GroupType.externalUserGroup }],
  770. revision: revisionIdRevert2,
  771. status: Page.STATUS_DELETED,
  772. },
  773. {
  774. _id: pageIdRevert3,
  775. path: '/trash/np_revert3',
  776. revision: revisionIdRevert3,
  777. status: Page.STATUS_DELETED,
  778. parent: rootPage._id,
  779. },
  780. {
  781. _id: pageIdRevert4,
  782. path: '/trash/np_revert3/middle/np_revert4',
  783. grant: Page.GRANT_RESTRICTED,
  784. revision: revisionIdRevert4,
  785. status: Page.STATUS_DELETED,
  786. },
  787. {
  788. _id: pageIdRevert5,
  789. path: '/trash/np_revert5',
  790. grant: Page.GRANT_USER_GROUP,
  791. grantedGroups: [{ item: groupIdA, type: GroupType.userGroup }, { item: externalGroupIdA, type: GroupType.externalUserGroup }],
  792. revision: revisionIdRevert5,
  793. status: Page.STATUS_DELETED,
  794. },
  795. {
  796. _id: pageIdRevert6,
  797. path: '/trash/np_revert5/middle/np_revert6',
  798. grant: Page.GRANT_USER_GROUP,
  799. grantedGroups: [{ item: groupIdB, type: GroupType.userGroup }, { item: externalGroupIdB, type: GroupType.externalUserGroup }],
  800. revision: revisionIdRevert6,
  801. status: Page.STATUS_DELETED,
  802. },
  803. ]);
  804. await Revision.insertMany([
  805. {
  806. _id: revisionIdRevert1,
  807. pageId: pageIdRevert1,
  808. body: 'np_revert1',
  809. format: 'markdown',
  810. author: dummyUser1._id,
  811. },
  812. {
  813. _id: revisionIdRevert2,
  814. pageId: pageIdRevert2,
  815. body: 'np_revert2',
  816. format: 'markdown',
  817. author: npDummyUser1,
  818. },
  819. {
  820. _id: revisionIdRevert3,
  821. pageId: pageIdRevert3,
  822. body: 'np_revert3',
  823. format: 'markdown',
  824. author: npDummyUser1,
  825. },
  826. {
  827. _id: revisionIdRevert4,
  828. pageId: pageIdRevert4,
  829. body: 'np_revert4',
  830. format: 'markdown',
  831. author: npDummyUser1,
  832. },
  833. {
  834. _id: revisionIdRevert5,
  835. pageId: pageIdRevert5,
  836. body: 'np_revert5',
  837. format: 'markdown',
  838. author: npDummyUser1,
  839. },
  840. {
  841. _id: revisionIdRevert6,
  842. pageId: pageIdRevert6,
  843. body: 'np_revert6',
  844. format: 'markdown',
  845. author: npDummyUser1,
  846. },
  847. ]);
  848. await Tag.insertMany([
  849. { _id: tagIdRevert1, name: 'np_revertTag1' },
  850. { _id: tagIdRevert2, name: 'np_revertTag2' },
  851. ]);
  852. await PageTagRelation.insertMany([
  853. {
  854. relatedPage: pageIdRevert1,
  855. relatedTag: tagIdRevert1,
  856. isPageTrashed: true,
  857. },
  858. {
  859. relatedPage: pageIdRevert2,
  860. relatedTag: tagIdRevert2,
  861. isPageTrashed: true,
  862. },
  863. ]);
  864. });
  865. describe('create', () => {
  866. describe('Creating a page using existing path', () => {
  867. test('with grant RESTRICTED should only create the page and change nothing else', async() => {
  868. const isGrantNormalizedSpy = jest.spyOn(crowi.pageGrantService, 'isGrantNormalized');
  869. const pathT = '/mc4_top';
  870. const path1 = '/mc4_top/mc1_emp';
  871. const path2 = '/mc4_top/mc1_emp/mc2_pub';
  872. const pageT = await Page.findOne({ path: pathT, descendantCount: 1 });
  873. const page1 = await Page.findOne({ path: path1, grant: Page.GRANT_PUBLIC });
  874. const page2 = await Page.findOne({ path: path2 });
  875. const page3 = await Page.findOne({ path: path1, grant: Page.GRANT_RESTRICTED });
  876. expect(pageT).toBeTruthy();
  877. expect(page1).toBeTruthy();
  878. expect(page2).toBeTruthy();
  879. expect(page3).toBeNull();
  880. // use existing path
  881. await create(path1, 'new body', dummyUser1, { grant: Page.GRANT_RESTRICTED });
  882. const _pageT = await Page.findOne({ path: pathT });
  883. const _page1 = await Page.findOne({ path: path1, grant: Page.GRANT_PUBLIC });
  884. const _page2 = await Page.findOne({ path: path2 });
  885. const _page3 = await Page.findOne({ path: path1, grant: Page.GRANT_RESTRICTED });
  886. expect(_pageT).toBeTruthy();
  887. expect(_page1).toBeTruthy();
  888. expect(_page2).toBeTruthy();
  889. expect(_page3).toBeTruthy();
  890. expect(_pageT.descendantCount).toBe(1);
  891. // isGrantNormalized is not called when GRANT RESTRICTED
  892. expect(isGrantNormalizedSpy).toBeCalledTimes(0);
  893. });
  894. });
  895. describe('Creating a page under a page with grant RESTRICTED', () => {
  896. test('will create a new empty page with the same path as the grant RESTRECTED page and become a parent', async() => {
  897. const isGrantNormalizedSpy = jest.spyOn(crowi.pageGrantService, 'isGrantNormalized');
  898. const pathT = '/mc5_top';
  899. const path1 = '/mc5_top/mc3_awl';
  900. const pathN = '/mc5_top/mc3_awl/mc4_pub'; // used to create
  901. const pageT = await Page.findOne({ path: pathT });
  902. const page1 = await Page.findOne({ path: path1, grant: Page.GRANT_RESTRICTED });
  903. const page2 = await Page.findOne({ path: path1, grant: Page.GRANT_PUBLIC });
  904. expect(pageT).toBeTruthy();
  905. expect(page1).toBeTruthy();
  906. expect(page2).toBeNull();
  907. await create(pathN, 'new body', dummyUser1, { grant: Page.GRANT_PUBLIC });
  908. const _pageT = await Page.findOne({ path: pathT });
  909. const _page1 = await Page.findOne({ path: path1, grant: Page.GRANT_RESTRICTED });
  910. const _page2 = await Page.findOne({ path: path1, grant: Page.GRANT_PUBLIC, isEmpty: true });
  911. const _pageN = await Page.findOne({ path: pathN, grant: Page.GRANT_PUBLIC }); // newly crated
  912. expect(_pageT).toBeTruthy();
  913. expect(_page1).toBeTruthy();
  914. expect(_page2).toBeTruthy();
  915. expect(_pageN).toBeTruthy();
  916. expect(_pageN.parent).toStrictEqual(_page2._id);
  917. expect(_pageT.descendantCount).toStrictEqual(1);
  918. // isGrantNormalized is called when GRANT PUBLIC
  919. expect(isGrantNormalizedSpy).toBeCalledTimes(1);
  920. });
  921. });
  922. });
  923. describe('create by system', () => {
  924. describe('Creating a page using existing path', () => {
  925. test('with grant RESTRICTED should only create the page and change nothing else', async() => {
  926. const isGrantNormalizedSpy = jest.spyOn(crowi.pageGrantService, 'isGrantNormalized');
  927. const pathT = '/mc4_top_by_system';
  928. const path1 = '/mc4_top_by_system/mc1_emp_by_system';
  929. const path2 = '/mc4_top_by_system/mc1_emp_by_system/mc2_pub_by_system';
  930. const pageT = await Page.findOne({ path: pathT, descendantCount: 1 });
  931. const page1 = await Page.findOne({ path: path1, grant: Page.GRANT_PUBLIC });
  932. const page2 = await Page.findOne({ path: path2 });
  933. const page3 = await Page.findOne({ path: path1, grant: Page.GRANT_RESTRICTED });
  934. expect(pageT).toBeTruthy();
  935. expect(page1).toBeTruthy();
  936. expect(page2).toBeTruthy();
  937. expect(page3).toBeNull();
  938. // use existing path
  939. await crowi.pageService.forceCreateBySystem(path1, 'new body', { grant: Page.GRANT_RESTRICTED });
  940. const _pageT = await Page.findOne({ path: pathT });
  941. const _page1 = await Page.findOne({ path: path1, grant: Page.GRANT_PUBLIC });
  942. const _page2 = await Page.findOne({ path: path2 });
  943. const _page3 = await Page.findOne({ path: path1, grant: Page.GRANT_RESTRICTED });
  944. expect(_pageT).toBeTruthy();
  945. expect(_page1).toBeTruthy();
  946. expect(_page2).toBeTruthy();
  947. expect(_page3).toBeTruthy();
  948. expect(_pageT.descendantCount).toBe(1);
  949. // isGrantNormalized is not called when create by ststem
  950. expect(isGrantNormalizedSpy).toBeCalledTimes(0);
  951. });
  952. });
  953. describe('Creating a page under a page with grant RESTRICTED', () => {
  954. test('will create a new empty page with the same path as the grant RESTRECTED page and become a parent', async() => {
  955. const isGrantNormalizedSpy = jest.spyOn(crowi.pageGrantService, 'isGrantNormalized');
  956. const pathT = '/mc5_top_by_system';
  957. const path1 = '/mc5_top_by_system/mc3_awl_by_system';
  958. const pathN = '/mc5_top_by_system/mc3_awl_by_system/mc4_pub_by_system'; // used to create
  959. const pageT = await Page.findOne({ path: pathT });
  960. const page1 = await Page.findOne({ path: path1, grant: Page.GRANT_RESTRICTED });
  961. const page2 = await Page.findOne({ path: path1, grant: Page.GRANT_PUBLIC });
  962. expect(pageT).toBeTruthy();
  963. expect(page1).toBeTruthy();
  964. expect(page2).toBeNull();
  965. await crowi.pageService.forceCreateBySystem(pathN, 'new body', { grant: Page.GRANT_PUBLIC });
  966. const _pageT = await Page.findOne({ path: pathT });
  967. const _page1 = await Page.findOne({ path: path1, grant: Page.GRANT_RESTRICTED });
  968. const _page2 = await Page.findOne({ path: path1, grant: Page.GRANT_PUBLIC, isEmpty: true });
  969. const _pageN = await Page.findOne({ path: pathN, grant: Page.GRANT_PUBLIC }); // newly crated
  970. expect(_pageT).toBeTruthy();
  971. expect(_page1).toBeTruthy();
  972. expect(_page2).toBeTruthy();
  973. expect(_pageN).toBeTruthy();
  974. expect(_pageN.parent).toStrictEqual(_page2._id);
  975. expect(_pageT.descendantCount).toStrictEqual(1);
  976. // isGrantNormalized is not called when create by ststem
  977. expect(isGrantNormalizedSpy).toBeCalledTimes(0);
  978. });
  979. });
  980. });
  981. describe('Rename', () => {
  982. const renamePage = async(page, newPagePath, user, options, activityParameters?) => {
  983. // mock return value
  984. const mockedRenameSubOperation = jest.spyOn(crowi.pageService, 'renameSubOperation').mockReturnValue(null);
  985. const renamedPage = await crowi.pageService.renamePage(page, newPagePath, user, options, activityParameters);
  986. // retrieve the arguments passed when calling method renameSubOperation inside renamePage method
  987. const argsForRenameSubOperation = mockedRenameSubOperation.mock.calls[0];
  988. // restores the original implementation
  989. mockedRenameSubOperation.mockRestore();
  990. // rename descendants
  991. if (page.grant !== Page.GRANT_RESTRICTED) {
  992. await crowi.pageService.renameSubOperation(...argsForRenameSubOperation);
  993. }
  994. return renamedPage;
  995. };
  996. test('Should rename/move with descendants with grant normalized pages', async() => {
  997. const _pathD = '/np_rename1_destination';
  998. const _path2 = '/np_rename2';
  999. const _path3 = '/np_rename2/np_rename3';
  1000. const _propertiesD = { grant: Page.GRANT_PUBLIC };
  1001. const _properties2 = { grant: Page.GRANT_USER_GROUP, grantedGroups: { $elemMatch: { item: groupIdB } } };
  1002. const _properties3 = { grant: Page.GRANT_USER_GROUP, grantedGroups: { $elemMatch: { item: groupIdC } } };
  1003. const _pageD = await Page.findOne({ path: _pathD, ..._propertiesD });
  1004. const _page2 = await Page.findOne({ path: _path2, ..._properties2 });
  1005. const _page3 = await Page.findOne({ path: _path3, ..._properties3, parent: _page2._id });
  1006. expect(_pageD).toBeTruthy();
  1007. expect(_page2).toBeTruthy();
  1008. expect(_page3).toBeTruthy();
  1009. const newPathForPage2 = '/np_rename1_destination/np_rename2';
  1010. const newPathForPage3 = '/np_rename1_destination/np_rename2/np_rename3';
  1011. await renamePage(_page2, newPathForPage2, npDummyUser2, {}, {
  1012. ip: '::ffff:127.0.0.1',
  1013. endpoint: '/_api/v3/pages/rename',
  1014. activityId: '62e291bc10e0ab61bd691794',
  1015. });
  1016. const pageD = await Page.findOne({ path: _pathD, ..._propertiesD });
  1017. const page2 = await Page.findOne({ path: _path2, ..._properties2 }); // not exist
  1018. const page3 = await Page.findOne({ path: _path3, ..._properties3, parent: _page2._id }); // not exist
  1019. const page2Renamed = await Page.findOne({ path: newPathForPage2 }); // renamed
  1020. const page3Renamed = await Page.findOne({ path: newPathForPage3 }); // renamed
  1021. expect(pageD).toBeTruthy();
  1022. expect(page2).toBeNull();
  1023. expect(page3).toBeNull();
  1024. expect(page2Renamed).toBeTruthy();
  1025. expect(page3Renamed).toBeTruthy();
  1026. expect(page2Renamed.parent).toStrictEqual(_pageD._id);
  1027. expect(page3Renamed.parent).toStrictEqual(page2Renamed._id);
  1028. expect(normalizeGrantedGroups(page2Renamed.grantedGroups)).toStrictEqual(normalizeGrantedGroups(_page2.grantedGroups));
  1029. expect(normalizeGrantedGroups(page3Renamed.grantedGroups)).toStrictEqual(normalizeGrantedGroups(_page3.grantedGroups));
  1030. expect(xssSpy).toHaveBeenCalled();
  1031. });
  1032. test('Should throw with NOT grant normalized pages', async() => {
  1033. const _pathD = '/np_rename4_destination';
  1034. const _path2 = '/np_rename5';
  1035. const _path3 = '/np_rename5/np_rename6';
  1036. const _propertiesD = { grant: Page.GRANT_USER_GROUP, grantedGroups: { $elemMatch: { item: groupIdIsolate } } };
  1037. const _properties2 = { grant: Page.GRANT_USER_GROUP, grantedGroups: { $elemMatch: { item: groupIdB } } };
  1038. const _properties3 = { grant: Page.GRANT_USER_GROUP, grantedGroups: { $elemMatch: { item: groupIdB } } };
  1039. const _pageD = await Page.findOne({ path: _pathD, ..._propertiesD });// isolate
  1040. const _page2 = await Page.findOne({ path: _path2, ..._properties2 });// groupIdB
  1041. const _page3 = await Page.findOne({ path: _path3, ..._properties3, parent: _page2 });// groupIdB
  1042. expect(_pageD).toBeTruthy();
  1043. expect(_page2).toBeTruthy();
  1044. expect(_page3).toBeTruthy();
  1045. const newPathForPage2 = '/np_rename4_destination/np_rename5';
  1046. const newPathForPage3 = '/np_rename4_destination/np_rename5/np_rename6';
  1047. let isThrown = false;
  1048. try {
  1049. await renamePage(_page2, newPathForPage2, dummyUser1, {}, {
  1050. ip: '::ffff:127.0.0.1',
  1051. endpoint: '/_api/v3/pages/rename',
  1052. activityId: '62e291bc10e0ab61bd691794',
  1053. });
  1054. }
  1055. catch (err) {
  1056. isThrown = true;
  1057. }
  1058. expect(isThrown).toBe(true);
  1059. const page2 = await Page.findOne({ path: _path2 }); // not renamed thus exist
  1060. const page3 = await Page.findOne({ path: _path3 }); // not renamed thus exist
  1061. const page2Renamed = await Page.findOne({ path: newPathForPage2 }); // not exist
  1062. const page3Renamed = await Page.findOne({ path: newPathForPage3 }); // not exist
  1063. expect(page2).toBeTruthy();
  1064. expect(page3).toBeTruthy();
  1065. expect(page2Renamed).toBeNull();
  1066. expect(page3Renamed).toBeNull();
  1067. });
  1068. test('Should rename/move multiple pages: child page with GRANT_RESTRICTED should NOT be renamed.', async() => {
  1069. const _pathD = '/np_rename7_destination';
  1070. const _path2 = '/np_rename8';
  1071. const _path3 = '/np_rename8/np_rename9';
  1072. const _pageD = await Page.findOne({ path: _pathD, grant: Page.GRANT_USER_GROUP, grantedGroups: { $elemMatch: { item: groupIdIsolate } } });
  1073. const _page2 = await Page.findOne({ path: _path2, grant: Page.GRANT_RESTRICTED });
  1074. const _page3 = await Page.findOne({ path: _path3, grant: Page.GRANT_RESTRICTED });
  1075. expect(_pageD).toBeTruthy();
  1076. expect(_page2).toBeTruthy();
  1077. expect(_page3).toBeTruthy();
  1078. const newPathForPage2 = '/np_rename7_destination/np_rename8';
  1079. const newpathForPage3 = '/np_rename7_destination/np_rename8/np_rename9';
  1080. await renamePage(_page2, newPathForPage2, npDummyUser1, { isRecursively: true }, {
  1081. ip: '::ffff:127.0.0.1',
  1082. endpoint: '/_api/v3/pages/rename',
  1083. activityId: '62e291bc10e0ab61bd691794',
  1084. });
  1085. const page2 = await Page.findOne({ path: _path2 }); // not exist
  1086. const page3 = await Page.findOne({ path: _path3 }); // not renamed thus exist
  1087. const page2Renamed = await Page.findOne({ path: newPathForPage2 }); // exist
  1088. const page3Renamed = await Page.findOne({ path: newpathForPage3 }); // not exist
  1089. expect(page2).toBeNull();
  1090. expect(page3).toBeTruthy();
  1091. expect(page2Renamed).toBeTruthy();
  1092. expect(page3Renamed).toBeNull();
  1093. expect(page2Renamed.parent).toBeNull();
  1094. expect(xssSpy).toHaveBeenCalled();
  1095. });
  1096. });
  1097. describe('Duplicate', () => {
  1098. const duplicate = async(page, newPagePath: string, user, isRecursively: boolean, onlyDuplicateUserRelatedResources: boolean) => {
  1099. // mock return value
  1100. const mockedDuplicateRecursivelyMainOperation = jest.spyOn(crowi.pageService, 'duplicateRecursivelyMainOperation').mockReturnValue(null);
  1101. const duplicatedPage = await crowi.pageService.duplicate(page, newPagePath, user, isRecursively, onlyDuplicateUserRelatedResources);
  1102. // retrieve the arguments passed when calling method duplicateRecursivelyMainOperation inside duplicate method
  1103. const argsForDuplicateRecursivelyMainOperation = mockedDuplicateRecursivelyMainOperation.mock.calls[0];
  1104. // restores the original implementation
  1105. mockedDuplicateRecursivelyMainOperation.mockRestore();
  1106. // duplicate descendants
  1107. if (page.grant !== Page.GRANT_RESTRICTED && isRecursively) {
  1108. await crowi.pageService.duplicateRecursivelyMainOperation(...argsForDuplicateRecursivelyMainOperation);
  1109. }
  1110. return duplicatedPage;
  1111. };
  1112. test('Duplicate single page with GRANT_RESTRICTED', async() => {
  1113. const _page = await Page.findOne({ path: '/np_duplicate1', grant: Page.GRANT_RESTRICTED }).populate({ path: 'revision', model: 'Revision' });
  1114. const _revision = _page.revision;
  1115. expect(_page).toBeTruthy();
  1116. expect(_revision).toBeTruthy();
  1117. const newPagePath = '/dup_np_duplicate1';
  1118. await duplicate(_page, newPagePath, npDummyUser1, false, false);
  1119. const duplicatedPage = await Page.findOne({ path: newPagePath });
  1120. const duplicatedRevision = await Revision.findOne({ pageId: duplicatedPage._id });
  1121. expect(xssSpy).toHaveBeenCalled();
  1122. expect(duplicatedPage).toBeTruthy();
  1123. expect(duplicatedPage._id).not.toStrictEqual(_page._id);
  1124. expect(duplicatedPage.grant).toBe(_page.grant);
  1125. expect(duplicatedPage.parent).toBeNull();
  1126. expect(duplicatedPage.parent).toStrictEqual(_page.parent);
  1127. expect(duplicatedPage.revision).toStrictEqual(duplicatedRevision._id);
  1128. expect(duplicatedRevision.body).toBe(_revision.body);
  1129. });
  1130. test('Should duplicate multiple pages with GRANT_USER_GROUP', async() => {
  1131. const _path1 = '/np_duplicate2';
  1132. const _path2 = '/np_duplicate2/np_duplicate3';
  1133. const _page1 = await Page.findOne({ path: _path1, parent: rootPage._id, grantedGroups: { $elemMatch: { item: groupIdA } } })
  1134. .populate({ path: 'revision', model: 'Revision' });
  1135. const _page2 = await Page.findOne({ path: _path2, parent: _page1._id, grantedGroups: { $elemMatch: { item: groupIdB } } })
  1136. .populate({ path: 'revision', model: 'Revision' });
  1137. const _revision1 = _page1.revision;
  1138. const _revision2 = _page2.revision;
  1139. expect(_page1).toBeTruthy();
  1140. expect(_page2).toBeTruthy();
  1141. expect(_revision1).toBeTruthy();
  1142. expect(_revision2).toBeTruthy();
  1143. const newPagePath = '/dup_np_duplicate2';
  1144. await duplicate(_page1, newPagePath, npDummyUser2, true, false);
  1145. const duplicatedPage1 = await Page.findOne({ path: newPagePath }).populate({ path: 'revision', model: 'Revision' });
  1146. const duplicatedPage2 = await Page.findOne({ path: '/dup_np_duplicate2/np_duplicate3' }).populate({ path: 'revision', model: 'Revision' });
  1147. const duplicatedRevision1 = duplicatedPage1.revision;
  1148. const duplicatedRevision2 = duplicatedPage2.revision;
  1149. expect(xssSpy).toHaveBeenCalled();
  1150. expect(duplicatedPage1).toBeTruthy();
  1151. expect(duplicatedPage2).toBeTruthy();
  1152. expect(duplicatedRevision1).toBeTruthy();
  1153. expect(duplicatedRevision2).toBeTruthy();
  1154. expect(normalizeGrantedGroups(duplicatedPage1.grantedGroups)).toStrictEqual([
  1155. { item: groupIdA, type: GroupType.userGroup },
  1156. { item: externalGroupIdA, type: GroupType.externalUserGroup },
  1157. ]);
  1158. expect(normalizeGrantedGroups(duplicatedPage2.grantedGroups)).toStrictEqual([
  1159. { item: groupIdB, type: GroupType.userGroup },
  1160. { item: externalGroupIdB, type: GroupType.externalUserGroup },
  1161. ]);
  1162. expect(duplicatedPage1.parent).toStrictEqual(_page1.parent);
  1163. expect(duplicatedPage2.parent).toStrictEqual(duplicatedPage1._id);
  1164. expect(duplicatedRevision1.body).toBe(_revision1.body);
  1165. expect(duplicatedRevision2.body).toBe(_revision2.body);
  1166. expect(duplicatedRevision1.pageId).toStrictEqual(duplicatedPage1._id);
  1167. expect(duplicatedRevision2.pageId).toStrictEqual(duplicatedPage2._id);
  1168. });
  1169. test('Should duplicate multiple pages. Page with GRANT_RESTRICTED should NOT be duplicated', async() => {
  1170. const _path1 = '/np_duplicate4';
  1171. const _path2 = '/np_duplicate4/np_duplicate5';
  1172. const _path3 = '/np_duplicate4/np_duplicate6';
  1173. const _page1 = await Page.findOne({ path: _path1, parent: rootPage._id, grant: Page.GRANT_PUBLIC })
  1174. .populate({ path: 'revision', model: 'Revision' });
  1175. const _page2 = await Page.findOne({ path: _path2, grant: Page.GRANT_RESTRICTED }).populate({ path: 'revision', model: 'Revision' });
  1176. const _page3 = await Page.findOne({ path: _path3, grant: Page.GRANT_PUBLIC }).populate({ path: 'revision', model: 'Revision' });
  1177. const baseRevision1 = _page1.revision;
  1178. const baseRevision2 = _page2.revision;
  1179. const baseRevision3 = _page3.revision;
  1180. expect(_page1).toBeTruthy();
  1181. expect(_page2).toBeTruthy();
  1182. expect(_page3).toBeTruthy();
  1183. expect(baseRevision1).toBeTruthy();
  1184. expect(baseRevision2).toBeTruthy();
  1185. const newPagePath = '/dup_np_duplicate4';
  1186. await duplicate(_page1, newPagePath, npDummyUser1, true, false);
  1187. const duplicatedPage1 = await Page.findOne({ path: newPagePath }).populate({ path: 'revision', model: 'Revision' });
  1188. const duplicatedPage2 = await Page.findOne({ path: '/dup_np_duplicate4/np_duplicate5' }).populate({ path: 'revision', model: 'Revision' });
  1189. const duplicatedPage3 = await Page.findOne({ path: '/dup_np_duplicate4/np_duplicate6' }).populate({ path: 'revision', model: 'Revision' });
  1190. const duplicatedRevision1 = duplicatedPage1.revision;
  1191. const duplicatedRevision3 = duplicatedPage3.revision;
  1192. expect(xssSpy).toHaveBeenCalled();
  1193. expect(duplicatedPage1).toBeTruthy();
  1194. expect(duplicatedPage2).toBeNull();
  1195. expect(duplicatedPage3).toBeTruthy();
  1196. expect(duplicatedRevision1).toBeTruthy();
  1197. expect(duplicatedRevision3).toBeTruthy();
  1198. expect(duplicatedPage1.grant).toStrictEqual(Page.GRANT_PUBLIC);
  1199. expect(duplicatedPage3.grant).toStrictEqual(Page.GRANT_PUBLIC);
  1200. expect(duplicatedPage1.parent).toStrictEqual(_page1.parent);
  1201. expect(duplicatedPage3.parent).toStrictEqual(duplicatedPage1._id);
  1202. expect(duplicatedRevision1.body).toBe(baseRevision1.body);
  1203. expect(duplicatedRevision3.body).toBe(baseRevision3.body);
  1204. expect(duplicatedRevision1.pageId).toStrictEqual(duplicatedPage1._id);
  1205. expect(duplicatedRevision3.pageId).toStrictEqual(duplicatedPage3._id);
  1206. });
  1207. test('Should duplicate only user related pages and granted groups when onlyDuplicateUserRelatedResources is true', async() => {
  1208. const _path1 = '/np_duplicate7';
  1209. const _path2 = '/np_duplicate7/np_duplicate8';
  1210. const _path3 = '/np_duplicate7/np_duplicate9';
  1211. const _page1 = await Page.findOne({ path: _path1, parent: rootPage._id })
  1212. .populate({ path: 'revision', model: 'Revision' });
  1213. const _page2 = await Page.findOne({ path: _path2, parent: _page1._id });
  1214. const _page3 = await Page.findOne({ path: _path3, parent: _page1._id });
  1215. const _revision1 = _page1.revision;
  1216. expect(_page1).toBeTruthy();
  1217. expect(_page2).toBeTruthy();
  1218. expect(_page3).toBeTruthy();
  1219. expect(_revision1).toBeTruthy();
  1220. const newPagePath = '/dup_np_duplicate7';
  1221. await duplicate(_page1, newPagePath, npDummyUser1, true, true);
  1222. const duplicatedPage1 = await Page.findOne({ path: newPagePath }).populate({ path: 'revision', model: 'Revision' });
  1223. const duplicatedPage2 = await Page.findOne({ path: '/dup_np_duplicate7/np_duplicate8' }).populate({ path: 'revision', model: 'Revision' });
  1224. const duplicatedPage3 = await Page.findOne({ path: '/dup_np_duplicate7/np_duplicate9' }).populate({ path: 'revision', model: 'Revision' });
  1225. const duplicatedRevision1 = duplicatedPage1.revision;
  1226. expect(xssSpy).toHaveBeenCalled();
  1227. expect(duplicatedPage1).toBeTruthy();
  1228. expect(duplicatedPage2).toBeFalsy();
  1229. expect(duplicatedPage3).toBeFalsy();
  1230. expect(duplicatedRevision1).toBeTruthy();
  1231. expect(normalizeGrantedGroups(duplicatedPage1.grantedGroups)).toStrictEqual([
  1232. { item: groupIdA, type: GroupType.userGroup },
  1233. { item: externalGroupIdA, type: GroupType.externalUserGroup },
  1234. ]);
  1235. expect(duplicatedPage1.parent).toStrictEqual(_page1.parent);
  1236. expect(duplicatedRevision1.body).toBe(_revision1.body);
  1237. expect(duplicatedRevision1.pageId).toStrictEqual(duplicatedPage1._id);
  1238. });
  1239. test('Should duplicate all pages and granted groups when onlyDuplicateUserRelatedResources is false', async() => {
  1240. const _path1 = '/np_duplicate7';
  1241. const _path2 = '/np_duplicate7/np_duplicate8';
  1242. const _path3 = '/np_duplicate7/np_duplicate9';
  1243. const _page1 = await Page.findOne({ path: _path1, parent: rootPage._id })
  1244. .populate({ path: 'revision', model: 'Revision' });
  1245. const _page2 = await Page.findOne({ path: _path2, parent: _page1._id })
  1246. .populate({ path: 'revision', model: 'Revision' });
  1247. const _page3 = await Page.findOne({ path: _path3, parent: _page1._id })
  1248. .populate({ path: 'revision', model: 'Revision' });
  1249. const _revision1 = _page1.revision;
  1250. const _revision2 = _page2.revision;
  1251. const _revision3 = _page3.revision;
  1252. expect(_page1).toBeTruthy();
  1253. expect(_page2).toBeTruthy();
  1254. expect(_page3).toBeTruthy();
  1255. expect(_revision1).toBeTruthy();
  1256. expect(_revision2).toBeTruthy();
  1257. expect(_revision3).toBeTruthy();
  1258. const newPagePath = '/dup2_np_duplicate7';
  1259. await duplicate(_page1, newPagePath, npDummyUser1, true, false);
  1260. const duplicatedPage1 = await Page.findOne({ path: newPagePath }).populate({ path: 'revision', model: 'Revision' });
  1261. const duplicatedPage2 = await Page.findOne({ path: '/dup2_np_duplicate7/np_duplicate8' }).populate({ path: 'revision', model: 'Revision' });
  1262. const duplicatedPage3 = await Page.findOne({ path: '/dup2_np_duplicate7/np_duplicate9' }).populate({ path: 'revision', model: 'Revision' });
  1263. const duplicatedRevision1 = duplicatedPage1.revision;
  1264. const duplicatedRevision2 = duplicatedPage2.revision;
  1265. const duplicatedRevision3 = duplicatedPage3.revision;
  1266. expect(xssSpy).toHaveBeenCalled();
  1267. expect(duplicatedPage1).toBeTruthy();
  1268. expect(duplicatedPage2).toBeTruthy();
  1269. expect(duplicatedPage3).toBeTruthy();
  1270. expect(duplicatedRevision1).toBeTruthy();
  1271. expect(duplicatedRevision2).toBeTruthy();
  1272. expect(duplicatedRevision3).toBeTruthy();
  1273. expect(normalizeGrantedGroups(duplicatedPage1.grantedGroups)).toStrictEqual([
  1274. { item: groupIdA, type: GroupType.userGroup },
  1275. { item: externalGroupIdA, type: GroupType.externalUserGroup },
  1276. { item: groupIdB, type: GroupType.userGroup },
  1277. { item: externalGroupIdB, type: GroupType.externalUserGroup },
  1278. ]);
  1279. expect(duplicatedPage1.parent).toStrictEqual(_page1.parent);
  1280. expect(duplicatedRevision1.body).toBe(_revision1.body);
  1281. expect(duplicatedRevision1.pageId).toStrictEqual(duplicatedPage1._id);
  1282. expect(normalizeGrantedGroups(duplicatedPage2.grantedGroups)).toStrictEqual([
  1283. { item: groupIdC, type: GroupType.userGroup },
  1284. { item: externalGroupIdC, type: GroupType.externalUserGroup },
  1285. ]);
  1286. expect(duplicatedPage2.parent).toStrictEqual(duplicatedPage1._id);
  1287. expect(duplicatedRevision2.body).toBe(_revision2.body);
  1288. expect(duplicatedRevision2.pageId).toStrictEqual(duplicatedPage2._id);
  1289. expect(duplicatedPage3.grantedUsers).toStrictEqual([npDummyUser2._id]);
  1290. expect(duplicatedPage3.parent).toStrictEqual(duplicatedPage1._id);
  1291. expect(duplicatedRevision3.body).toBe(_revision3.body);
  1292. expect(duplicatedRevision3.pageId).toStrictEqual(duplicatedPage3._id);
  1293. });
  1294. });
  1295. describe('Delete', () => {
  1296. const deletePage = async(page, user, options, isRecursively, activityParameters?) => {
  1297. const mockedDeleteRecursivelyMainOperation = jest.spyOn(crowi.pageService, 'deleteRecursivelyMainOperation').mockReturnValue(null);
  1298. const deletedPage = await crowi.pageService.deletePage(page, user, options, isRecursively, activityParameters);
  1299. const argsForDeleteRecursivelyMainOperation = mockedDeleteRecursivelyMainOperation.mock.calls[0];
  1300. mockedDeleteRecursivelyMainOperation.mockRestore();
  1301. if (isRecursively) {
  1302. await crowi.pageService.deleteRecursivelyMainOperation(...argsForDeleteRecursivelyMainOperation);
  1303. }
  1304. return deletedPage;
  1305. };
  1306. describe('Delete single page with grant RESTRICTED', () => {
  1307. test('should be able to delete', async() => {
  1308. const _pathT = '/npdel1_awl';
  1309. const _pageT = await Page.findOne({ path: _pathT, grant: Page.GRANT_RESTRICTED });
  1310. expect(_pageT).toBeTruthy();
  1311. const isRecursively = false;
  1312. await deletePage(_pageT, dummyUser1, {}, isRecursively, {
  1313. ip: '::ffff:127.0.0.1',
  1314. endpoint: '/_api/v3/pages/rename',
  1315. });
  1316. const pageT = await Page.findOne({ path: `/trash${_pathT}` });
  1317. const pageN = await Page.findOne({ path: _pathT }); // should not exist
  1318. expect(pageT).toBeTruthy();
  1319. expect(pageN).toBeNull();
  1320. expect(pageT.grant).toBe(Page.GRANT_RESTRICTED);
  1321. expect(pageT.status).toBe(Page.STATUS_DELETED);
  1322. });
  1323. });
  1324. describe('Delete single page with grant USER_GROUP', () => {
  1325. test('should be able to delete', async() => {
  1326. const _path = '/npdel2_ug';
  1327. const _page1 = await Page.findOne({ path: _path, grantedGroups: { $elemMatch: { item: groupIdA } } });
  1328. expect(_page1).toBeTruthy();
  1329. const isRecursively = false;
  1330. await deletePage(_page1, npDummyUser1, {}, isRecursively, {
  1331. ip: '::ffff:127.0.0.1',
  1332. endpoint: '/_api/v3/pages/rename',
  1333. });
  1334. const pageN = await Page.findOne({ path: _path, grantedGroups: { $elemMatch: { item: groupIdA } } });
  1335. const page1 = await Page.findOne({ path: `/trash${_path}`, grantedGroups: { $elemMatch: { item: groupIdA } } });
  1336. expect(pageN).toBeNull();
  1337. expect(page1).toBeTruthy();
  1338. expect(page1.status).toBe(Page.STATUS_DELETED);
  1339. expect(page1.descendantCount).toBe(0);
  1340. expect(page1.parent).toBeNull();
  1341. });
  1342. });
  1343. describe('Delete multiple pages with grant USER_GROUP', () => {
  1344. test('should be able to delete all descendants except page with GRANT_RESTRICTED', async() => {
  1345. const _pathT = '/npdel3_top';
  1346. const _path1 = '/npdel3_top/npdel4_ug';
  1347. const _path2 = '/npdel3_top/npdel4_ug/npdel5_ug';
  1348. const _pageT = await Page.findOne({ path: _pathT, grant: Page.GRANT_USER_GROUP, grantedGroups: { $elemMatch: { item: groupIdA } } }); // A
  1349. const _page1 = await Page.findOne({ path: _path1, grant: Page.GRANT_USER_GROUP, grantedGroups: { $elemMatch: { item: groupIdB } } }); // B
  1350. const _page2 = await Page.findOne({ path: _path2, grant: Page.GRANT_USER_GROUP, grantedGroups: { $elemMatch: { item: groupIdC } } }); // C
  1351. const _pageR = await Page.findOne({ path: _path1, grant: Page.GRANT_RESTRICTED }); // Restricted
  1352. expect(_pageT).toBeTruthy();
  1353. expect(_page1).toBeTruthy();
  1354. expect(_page2).toBeTruthy();
  1355. expect(_pageR).toBeTruthy();
  1356. const isRecursively = true;
  1357. await deletePage(_pageT, npDummyUser1, {}, isRecursively, {
  1358. ip: '::ffff:127.0.0.1',
  1359. endpoint: '/_api/v3/pages/rename',
  1360. });
  1361. const pageTNotExist = await Page.findOne({ path: _pathT, grant: Page.GRANT_USER_GROUP, grantedGroups: { $elemMatch: { item: groupIdA } } }); // A should not exist
  1362. const page1NotExist = await Page.findOne({ path: _path1, grant: Page.GRANT_USER_GROUP, grantedGroups: { $elemMatch: { item: groupIdB } } }); // B should not exist
  1363. const page2NotExist = await Page.findOne({ path: _path2, grant: Page.GRANT_USER_GROUP, grantedGroups: { $elemMatch: { item: groupIdC } } }); // C should not exist
  1364. const pageT = await Page.findOne({ path: `/trash${_pathT}`, grant: Page.GRANT_USER_GROUP, grantedGroups: { $elemMatch: { item: groupIdA } } }); // A
  1365. const page1 = await Page.findOne({ path: `/trash${_path1}`, grant: Page.GRANT_USER_GROUP, grantedGroups: { $elemMatch: { item: groupIdB } } }); // B
  1366. const page2 = await Page.findOne({ path: `/trash${_path2}`, grant: Page.GRANT_USER_GROUP, grantedGroups: { $elemMatch: { item: groupIdC } } }); // C
  1367. const pageR = await Page.findOne({ path: _path1, grant: Page.GRANT_RESTRICTED }); // Restricted
  1368. expect(page1NotExist).toBeNull();
  1369. expect(pageTNotExist).toBeNull();
  1370. expect(page2NotExist).toBeNull();
  1371. expect(pageT).toBeTruthy();
  1372. expect(page1).toBeTruthy();
  1373. expect(page2).toBeTruthy();
  1374. expect(pageR).toBeTruthy();
  1375. expect(pageT.status).toBe(Page.STATUS_DELETED);
  1376. expect(pageT.status).toBe(Page.STATUS_DELETED);
  1377. expect(page1.status).toBe(Page.STATUS_DELETED);
  1378. expect(page1.descendantCount).toBe(0);
  1379. expect(page2.descendantCount).toBe(0);
  1380. expect(page2.descendantCount).toBe(0);
  1381. expect(pageT.parent).toBeNull();
  1382. expect(page1.parent).toBeNull();
  1383. expect(page2.parent).toBeNull();
  1384. });
  1385. });
  1386. });
  1387. describe('Delete completely', () => {
  1388. const deleteCompletely = async(page, user, options = {}, isRecursively = false, preventEmitting = false, activityParameters?) => {
  1389. const mockedDeleteCompletelyRecursivelyMainOperation = jest.spyOn(crowi.pageService, 'deleteCompletelyRecursivelyMainOperation').mockReturnValue(null);
  1390. await crowi.pageService.deleteCompletely(page, user, options, isRecursively, preventEmitting, activityParameters);
  1391. const argsForDeleteCompletelyRecursivelyMainOperation = mockedDeleteCompletelyRecursivelyMainOperation.mock.calls[0];
  1392. mockedDeleteCompletelyRecursivelyMainOperation.mockRestore();
  1393. if (isRecursively) {
  1394. await crowi.pageService.deleteCompletelyRecursivelyMainOperation(...argsForDeleteCompletelyRecursivelyMainOperation);
  1395. }
  1396. return;
  1397. };
  1398. describe('Delete single page with grant RESTRICTED', () => {
  1399. test('should be able to delete completely', async() => {
  1400. const _path = '/npdc1_awl';
  1401. const _page = await Page.findOne({ path: _path, grant: Page.GRANT_RESTRICTED });
  1402. expect(_page).toBeTruthy();
  1403. await deleteCompletely(_page, dummyUser1, {}, false, false, {
  1404. ip: '::ffff:127.0.0.1',
  1405. endpoint: '/_api/v3/pages/rename',
  1406. });
  1407. const page = await Page.findOne({ path: _path, grant: Page.GRANT_RESTRICTED });
  1408. expect(page).toBeNull();
  1409. });
  1410. });
  1411. describe('Delete single page with grant USER_GROUP', () => {
  1412. test('should be able to delete completely', async() => {
  1413. const _path = '/npdc2_ug';
  1414. const _page = await Page.findOne({ path: _path, grant: Page.GRANT_USER_GROUP, grantedGroups: { $elemMatch: { item: groupIdA } } });
  1415. expect(_page).toBeTruthy();
  1416. await deleteCompletely(_page, npDummyUser1, {}, false, false, {
  1417. ip: '::ffff:127.0.0.1',
  1418. endpoint: '/_api/v3/pages/rename',
  1419. });
  1420. const page = await Page.findOne({ path: _path, grant: Page.GRANT_USER_GROUP, grantedGroups: { $elemMatch: { item: groupIdA } } });
  1421. expect(page).toBeNull();
  1422. });
  1423. });
  1424. describe('Delete multiple pages with grant USER_GROUP', () => {
  1425. test('should be able to delete all descendants completely except page with GRANT_RESTRICTED', async() => {
  1426. const _path1 = '/npdc3_ug';
  1427. const _path2 = '/npdc3_ug/npdc4_ug';
  1428. const _path3 = '/npdc3_ug/npdc4_ug/npdc5_ug';
  1429. const _page1 = await Page.findOne({ path: _path1, grant: Page.GRANT_USER_GROUP, grantedGroups: { $elemMatch: { item: groupIdA } } });
  1430. const _page2 = await Page.findOne({ path: _path2, grant: Page.GRANT_USER_GROUP, grantedGroups: { $elemMatch: { item: groupIdB } } });
  1431. const _page3 = await Page.findOne({ path: _path3, grant: Page.GRANT_USER_GROUP, grantedGroups: { $elemMatch: { item: groupIdC } } });
  1432. const _page4 = await Page.findOne({ path: _path2, grant: Page.GRANT_RESTRICTED });
  1433. expect(_page1).toBeTruthy();
  1434. expect(_page2).toBeTruthy();
  1435. expect(_page3).toBeTruthy();
  1436. expect(_page4).toBeTruthy();
  1437. await deleteCompletely(_page1, npDummyUser1, {}, true, false, {
  1438. ip: '::ffff:127.0.0.1',
  1439. endpoint: '/_api/v3/pages/rename',
  1440. });
  1441. const page1 = await Page.findOne({ path: _path1, grant: Page.GRANT_USER_GROUP, grantedGroups: { $elemMatch: { item: groupIdA } } });
  1442. const page2 = await Page.findOne({ path: _path2, grant: Page.GRANT_USER_GROUP, grantedGroups: { $elemMatch: { item: groupIdB } } });
  1443. const page3 = await Page.findOne({ path: _path3, grant: Page.GRANT_USER_GROUP, grantedGroups: { $elemMatch: { item: groupIdC } } });
  1444. const page4 = await Page.findOne({ path: _path2, grant: Page.GRANT_RESTRICTED });
  1445. expect(page1).toBeNull();
  1446. expect(page2).toBeNull();
  1447. expect(page3).toBeNull();
  1448. expect(page4).toBeTruthy();
  1449. });
  1450. });
  1451. });
  1452. describe('revert', () => {
  1453. const revertDeletedPage = async(page, user, options = {}, isRecursively = false, activityParameters?) => {
  1454. // mock return value
  1455. const mockedRevertRecursivelyMainOperation = jest.spyOn(crowi.pageService, 'revertRecursivelyMainOperation').mockReturnValue(null);
  1456. const revertedPage = await crowi.pageService.revertDeletedPage(page, user, options, isRecursively, activityParameters);
  1457. const argsForRecursivelyMainOperation = mockedRevertRecursivelyMainOperation.mock.calls[0];
  1458. // restores the original implementation
  1459. mockedRevertRecursivelyMainOperation.mockRestore();
  1460. if (isRecursively) {
  1461. await crowi.pageService.revertRecursivelyMainOperation(...argsForRecursivelyMainOperation);
  1462. }
  1463. return revertedPage;
  1464. };
  1465. test('should revert single deleted page with GRANT_RESTRICTED', async() => {
  1466. const trashedPage = await Page.findOne({ path: '/trash/np_revert1', status: Page.STATUS_DELETED, grant: Page.GRANT_RESTRICTED });
  1467. const revision = await Revision.findOne({ pageId: trashedPage._id });
  1468. const tag = await Tag.findOne({ name: 'np_revertTag1' });
  1469. const deletedPageTagRelation = await PageTagRelation.findOne({ relatedPage: trashedPage._id, relatedTag: tag?._id, isPageTrashed: true });
  1470. expect(trashedPage).toBeTruthy();
  1471. expect(revision).toBeTruthy();
  1472. expect(tag).toBeTruthy();
  1473. expect(deletedPageTagRelation).toBeTruthy();
  1474. await revertDeletedPage(trashedPage, dummyUser1, {}, false, {
  1475. ip: '::ffff:127.0.0.1',
  1476. endpoint: '/_api/v3/pages/rename',
  1477. });
  1478. const revertedPage = await Page.findOne({ path: '/np_revert1' });
  1479. const deltedPageBeforeRevert = await Page.findOne({ path: '/trash/np_revert1' });
  1480. const pageTagRelation = await PageTagRelation.findOne<IPageTagRelation>({ relatedPage: revertedPage._id, relatedTag: tag?._id });
  1481. expect(revertedPage).toBeTruthy();
  1482. expect(pageTagRelation).toBeTruthy();
  1483. expect(deltedPageBeforeRevert).toBeNull();
  1484. // page with GRANT_RESTRICTED does not have parent
  1485. expect(revertedPage.parent).toBeNull();
  1486. expect(revertedPage.status).toBe(Page.STATUS_PUBLISHED);
  1487. expect(revertedPage.grant).toBe(Page.GRANT_RESTRICTED);
  1488. expect(pageTagRelation?.isPageTrashed).toBe(false);
  1489. });
  1490. test('should revert single deleted page with GRANT_USER_GROUP', async() => {
  1491. const beforeRevertPath = '/trash/np_revert2';
  1492. const user1 = await User.findOne({ name: 'npUser1' });
  1493. const trashedPage = await Page.findOne({ path: beforeRevertPath, status: Page.STATUS_DELETED, grant: Page.GRANT_USER_GROUP });
  1494. const revision = await Revision.findOne({ pageId: trashedPage._id });
  1495. const tag = await Tag.findOne({ name: 'np_revertTag2' });
  1496. const deletedPageTagRelation = await PageTagRelation.findOne({ relatedPage: trashedPage._id, relatedTag: tag?._id, isPageTrashed: true });
  1497. expect(trashedPage).toBeTruthy();
  1498. expect(revision).toBeTruthy();
  1499. expect(tag).toBeTruthy();
  1500. expect(deletedPageTagRelation).toBeTruthy();
  1501. await revertDeletedPage(trashedPage, user1, {}, false, {
  1502. ip: '::ffff:127.0.0.1',
  1503. endpoint: '/_api/v3/pages/revert',
  1504. });
  1505. const revertedPage = await Page.findOne({ path: '/np_revert2' });
  1506. const trashedPageBR = await Page.findOne({ path: beforeRevertPath });
  1507. const pageTagRelation = await PageTagRelation.findOne<IPageTagRelation>({ relatedPage: revertedPage._id, relatedTag: tag?._id });
  1508. expect(revertedPage).toBeTruthy();
  1509. expect(pageTagRelation).toBeTruthy();
  1510. expect(trashedPageBR).toBeNull();
  1511. expect(revertedPage.parent).toStrictEqual(rootPage._id);
  1512. expect(revertedPage.status).toBe(Page.STATUS_PUBLISHED);
  1513. expect(revertedPage.grant).toBe(Page.GRANT_USER_GROUP);
  1514. expect(normalizeGrantedGroups(revertedPage.grantedGroups)).toStrictEqual([
  1515. { item: groupIdA, type: GroupType.userGroup },
  1516. { item: externalGroupIdA, type: GroupType.externalUserGroup },
  1517. ]);
  1518. expect(pageTagRelation?.isPageTrashed).toBe(false);
  1519. });
  1520. test(`revert multiple pages: only target page should be reverted.
  1521. Non-existant middle page and leaf page with GRANT_RESTRICTED shoud not be reverted`, async() => {
  1522. const beforeRevertPath1 = '/trash/np_revert3';
  1523. const beforeRevertPath2 = '/trash/np_revert3/middle/np_revert4';
  1524. const trashedPage1 = await Page.findOne({ path: beforeRevertPath1, status: Page.STATUS_DELETED, grant: Page.GRANT_PUBLIC });
  1525. const trashedPage2 = await Page.findOne({ path: beforeRevertPath2, status: Page.STATUS_DELETED, grant: Page.GRANT_RESTRICTED });
  1526. const revision1 = await Revision.findOne({ pageId: trashedPage1._id });
  1527. const revision2 = await Revision.findOne({ pageId: trashedPage2._id });
  1528. expect(trashedPage1).toBeTruthy();
  1529. expect(trashedPage2).toBeTruthy();
  1530. expect(revision1).toBeTruthy();
  1531. expect(revision2).toBeTruthy();
  1532. await revertDeletedPage(trashedPage1, npDummyUser2, {}, true, {
  1533. ip: '::ffff:127.0.0.1',
  1534. endpoint: '/_api/v3/pages/revert',
  1535. });
  1536. const revertedPage = await Page.findOne({ path: '/np_revert3' });
  1537. const middlePage = await Page.findOne({ path: '/np_revert3/middle' });
  1538. const notRestrictedPage = await Page.findOne({ path: '/np_revert3/middle/np_revert4' });
  1539. // AR => After Revert
  1540. const trashedPage1AR = await Page.findOne({ path: beforeRevertPath1 });
  1541. const trashedPage2AR = await Page.findOne({ path: beforeRevertPath2 });
  1542. const revision1AR = await Revision.findOne({ pageId: revertedPage._id });
  1543. const revision2AR = await Revision.findOne({ pageId: trashedPage2AR._id });
  1544. expect(revertedPage).toBeTruthy();
  1545. expect(trashedPage2AR).toBeTruthy();
  1546. expect(revision1AR).toBeTruthy();
  1547. expect(revision2AR).toBeTruthy();
  1548. expect(trashedPage1AR).toBeNull();
  1549. expect(notRestrictedPage).toBeNull();
  1550. expect(middlePage).toBeNull();
  1551. expect(revertedPage.parent).toStrictEqual(rootPage._id);
  1552. expect(revertedPage.status).toBe(Page.STATUS_PUBLISHED);
  1553. expect(revertedPage.grant).toBe(Page.GRANT_PUBLIC);
  1554. });
  1555. test('revert multiple pages: target page, initially non-existant page and leaf page with GRANT_USER_GROUP shoud be reverted', async() => {
  1556. const user = await User.findOne({ _id: npDummyUser3 });
  1557. const beforeRevertPath1 = '/trash/np_revert5';
  1558. const beforeRevertPath2 = '/trash/np_revert5/middle/np_revert6';
  1559. const beforeRevertPath3 = '/trash/np_revert5/middle';
  1560. const trashedPage1 = await Page.findOne({ path: beforeRevertPath1, status: Page.STATUS_DELETED, grantedGroups: { $elemMatch: { item: groupIdA } } });
  1561. const trashedPage2 = await Page.findOne({ path: beforeRevertPath2, status: Page.STATUS_DELETED, grantedGroups: { $elemMatch: { item: groupIdB } } });
  1562. const nonExistantPage3 = await Page.findOne({ path: beforeRevertPath3 }); // not exist
  1563. const revision1 = await Revision.findOne({ pageId: trashedPage1._id });
  1564. const revision2 = await Revision.findOne({ pageId: trashedPage2._id });
  1565. expect(trashedPage1).toBeTruthy();
  1566. expect(trashedPage2).toBeTruthy();
  1567. expect(revision1).toBeTruthy();
  1568. expect(revision2).toBeTruthy();
  1569. expect(user).toBeTruthy();
  1570. expect(nonExistantPage3).toBeNull();
  1571. await revertDeletedPage(trashedPage1, user, {}, true, {
  1572. ip: '::ffff:127.0.0.1',
  1573. endpoint: '/_api/v3/pages/revert',
  1574. });
  1575. const revertedPage1 = await Page.findOne({ path: '/np_revert5' });
  1576. const newlyCreatedPage = await Page.findOne({ path: '/np_revert5/middle' });
  1577. const revertedPage2 = await Page.findOne({ path: '/np_revert5/middle/np_revert6' });
  1578. // // AR => After Revert
  1579. const trashedPage1AR = await Page.findOne({ path: beforeRevertPath1 });
  1580. const trashedPage2AR = await Page.findOne({ path: beforeRevertPath2 });
  1581. expect(revertedPage1).toBeTruthy();
  1582. expect(newlyCreatedPage).toBeTruthy();
  1583. expect(revertedPage2).toBeTruthy();
  1584. expect(trashedPage1AR).toBeNull();
  1585. expect(trashedPage2AR).toBeNull();
  1586. expect(newlyCreatedPage.isEmpty).toBe(true);
  1587. expect(revertedPage1.parent).toStrictEqual(rootPage._id);
  1588. expect(revertedPage2.parent).toStrictEqual(newlyCreatedPage._id);
  1589. expect(newlyCreatedPage.parent).toStrictEqual(revertedPage1._id);
  1590. expect(revertedPage1.status).toBe(Page.STATUS_PUBLISHED);
  1591. expect(revertedPage2.status).toBe(Page.STATUS_PUBLISHED);
  1592. expect(newlyCreatedPage.status).toBe(Page.STATUS_PUBLISHED);
  1593. expect(normalizeGrantedGroups(revertedPage1.grantedGroups)).toStrictEqual([
  1594. { item: groupIdA, type: GroupType.userGroup },
  1595. { item: externalGroupIdA, type: GroupType.externalUserGroup },
  1596. ]);
  1597. expect(normalizeGrantedGroups(revertedPage2.grantedGroups)).toStrictEqual([
  1598. { item: groupIdB, type: GroupType.userGroup },
  1599. { item: externalGroupIdB, type: GroupType.externalUserGroup },
  1600. ]);
  1601. expect(newlyCreatedPage.grant).toBe(Page.GRANT_PUBLIC);
  1602. });
  1603. });
  1604. });