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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779
  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. path: '/mc6_top',
  337. grant: Page.GRANT_USER_GROUP,
  338. creator: dummyUser1,
  339. lastUpdateUser: dummyUser1._id,
  340. isEmpty: false,
  341. parent: rootPage._id,
  342. descendantCount: 0,
  343. grantedGroups: [
  344. { item: groupIdIsolate, type: GroupType.userGroup },
  345. { item: groupIdB, type: GroupType.userGroup },
  346. ],
  347. },
  348. ]);
  349. /**
  350. * create
  351. * mc_ => model create
  352. * emp => empty => page with isEmpty: true
  353. * pub => public => GRANT_PUBLIC
  354. */
  355. const pageIdCreateBySystem1 = new mongoose.Types.ObjectId();
  356. const pageIdCreateBySystem2 = new mongoose.Types.ObjectId();
  357. const pageIdCreateBySystem3 = new mongoose.Types.ObjectId();
  358. await Page.insertMany([
  359. {
  360. _id: pageIdCreateBySystem1,
  361. path: '/mc4_top_by_system/mc1_emp_by_system',
  362. grant: Page.GRANT_PUBLIC,
  363. creator: dummyUser1,
  364. lastUpdateUser: dummyUser1._id,
  365. parent: rootPage._id,
  366. isEmpty: true,
  367. },
  368. {
  369. path: '/mc4_top_by_system/mc1_emp_by_system/mc2_pub_by_system',
  370. grant: Page.GRANT_PUBLIC,
  371. creator: dummyUser1,
  372. lastUpdateUser: dummyUser1._id,
  373. parent: pageIdCreateBySystem1,
  374. isEmpty: false,
  375. },
  376. {
  377. path: '/mc5_top_by_system/mc3_awl_by_system',
  378. grant: Page.GRANT_RESTRICTED,
  379. creator: dummyUser1,
  380. lastUpdateUser: dummyUser1._id,
  381. isEmpty: false,
  382. },
  383. {
  384. _id: pageIdCreateBySystem2,
  385. path: '/mc4_top_by_system',
  386. grant: Page.GRANT_PUBLIC,
  387. creator: dummyUser1,
  388. lastUpdateUser: dummyUser1._id,
  389. isEmpty: false,
  390. parent: rootPage._id,
  391. descendantCount: 1,
  392. },
  393. {
  394. _id: pageIdCreateBySystem3,
  395. path: '/mc5_top_by_system',
  396. grant: Page.GRANT_PUBLIC,
  397. creator: dummyUser1,
  398. lastUpdateUser: dummyUser1._id,
  399. isEmpty: false,
  400. parent: rootPage._id,
  401. descendantCount: 0,
  402. },
  403. ]);
  404. /*
  405. * Rename
  406. */
  407. await Page.insertMany([
  408. {
  409. _id: pageIdRename1,
  410. path: '/np_rename1_destination',
  411. grant: Page.GRANT_PUBLIC,
  412. creator: dummyUser1._id,
  413. lastUpdateUser: dummyUser1._id,
  414. parent: rootPage._id,
  415. },
  416. {
  417. _id: pageIdRename2,
  418. path: '/np_rename2',
  419. grant: Page.GRANT_USER_GROUP,
  420. grantedGroups: [{ item: groupIdB, type: GroupType.userGroup }, { item: externalGroupIdB, type: GroupType.externalUserGroup }],
  421. creator: npDummyUser2._id,
  422. lastUpdateUser: npDummyUser2._id,
  423. parent: rootPage._id,
  424. },
  425. {
  426. _id: pageIdRename3,
  427. path: '/np_rename2/np_rename3',
  428. grant: Page.GRANT_USER_GROUP,
  429. grantedGroups: [{ item: groupIdC, type: GroupType.userGroup }, { item: externalGroupIdC, type: GroupType.externalUserGroup }],
  430. creator: npDummyUser3._id,
  431. lastUpdateUser: npDummyUser3._id,
  432. parent: pageIdRename2._id,
  433. },
  434. {
  435. _id: pageIdRename4,
  436. path: '/np_rename4_destination',
  437. grant: Page.GRANT_USER_GROUP,
  438. grantedGroups: [{ item: groupIdIsolate, type: GroupType.userGroup }, { item: externalGroupIdIsolate, type: GroupType.externalUserGroup }],
  439. creator: npDummyUser3._id,
  440. lastUpdateUser: npDummyUser3._id,
  441. parent: rootPage._id,
  442. },
  443. {
  444. _id: pageIdRename5,
  445. path: '/np_rename5',
  446. grant: Page.GRANT_USER_GROUP,
  447. grantedGroups: [{ item: groupIdB, type: GroupType.userGroup }, { item: externalGroupIdB, type: GroupType.externalUserGroup }],
  448. creator: npDummyUser2._id,
  449. lastUpdateUser: npDummyUser2._id,
  450. parent: rootPage._id,
  451. },
  452. {
  453. _id: pageIdRename6,
  454. path: '/np_rename5/np_rename6',
  455. grant: Page.GRANT_USER_GROUP,
  456. grantedGroups: [{ item: groupIdB, type: GroupType.userGroup }, { item: externalGroupIdB, type: GroupType.externalUserGroup }],
  457. creator: npDummyUser2._id,
  458. lastUpdateUser: npDummyUser2._id,
  459. parent: pageIdRename5,
  460. },
  461. {
  462. _id: pageIdRename7,
  463. path: '/np_rename7_destination',
  464. grant: Page.GRANT_USER_GROUP,
  465. grantedGroups: [{ item: groupIdIsolate, type: GroupType.userGroup }, { item: externalGroupIdIsolate, type: GroupType.externalUserGroup }],
  466. creator: npDummyUser2._id,
  467. lastUpdateUser: npDummyUser2._id,
  468. parent: pageIdRename5,
  469. },
  470. {
  471. _id: pageIdRename8,
  472. path: '/np_rename8',
  473. grant: Page.GRANT_RESTRICTED,
  474. creator: dummyUser1._id,
  475. lastUpdateUser: dummyUser1._id,
  476. },
  477. {
  478. _id: pageIdRename9,
  479. path: '/np_rename8/np_rename9',
  480. grant: Page.GRANT_RESTRICTED,
  481. creator: dummyUser2._id,
  482. lastUpdateUser: dummyUser2._id,
  483. },
  484. ]);
  485. /*
  486. * Duplicate
  487. */
  488. await Page.insertMany([
  489. {
  490. _id: pageIdDuplicate1,
  491. path: '/np_duplicate1',
  492. grant: Page.GRANT_RESTRICTED,
  493. creator: dummyUser1._id,
  494. lastUpdateUser: dummyUser1._id,
  495. revision: revisionIdDuplicate1,
  496. },
  497. {
  498. _id: pageIdDuplicate2,
  499. path: '/np_duplicate2',
  500. grant: Page.GRANT_USER_GROUP,
  501. grantedGroups: [
  502. { item: groupIdA, type: GroupType.userGroup },
  503. { item: externalGroupIdA, type: GroupType.externalUserGroup },
  504. ],
  505. creator: npDummyUser1._id,
  506. lastUpdateUser: npDummyUser1._id,
  507. revision: revisionIdDuplicate2,
  508. parent: rootPage._id,
  509. },
  510. {
  511. _id: pageIdDuplicate3,
  512. path: '/np_duplicate2/np_duplicate3',
  513. grant: Page.GRANT_USER_GROUP,
  514. grantedGroups: [
  515. { item: groupIdB, type: GroupType.userGroup },
  516. { item: externalGroupIdB, type: GroupType.externalUserGroup },
  517. ],
  518. creator: npDummyUser2._id,
  519. lastUpdateUser: npDummyUser2._id,
  520. revision: revisionIdDuplicate3,
  521. parent: pageIdDuplicate2,
  522. },
  523. {
  524. _id: pageIdDuplicate4,
  525. path: '/np_duplicate4',
  526. grant: Page.GRANT_PUBLIC,
  527. creator: npDummyUser1._id,
  528. lastUpdateUser: npDummyUser1._id,
  529. revision: revisionIdDuplicate4,
  530. parent: rootPage._id,
  531. },
  532. {
  533. _id: pageIdDuplicate5,
  534. path: '/np_duplicate4/np_duplicate5',
  535. grant: Page.GRANT_RESTRICTED,
  536. creator: npDummyUser1._id,
  537. lastUpdateUser: npDummyUser1._id,
  538. revision: revisionIdDuplicate5,
  539. },
  540. {
  541. _id: pageIdDuplicate6,
  542. path: '/np_duplicate4/np_duplicate6',
  543. grant: Page.GRANT_PUBLIC,
  544. creator: npDummyUser1._id,
  545. lastUpdateUser: npDummyUser1._id,
  546. parent: pageIdDuplicate4,
  547. revision: revisionIdDuplicate6,
  548. },
  549. {
  550. _id: pageIdDuplicate7,
  551. path: '/np_duplicate7',
  552. grant: Page.GRANT_USER_GROUP,
  553. creator: npDummyUser1._id,
  554. lastUpdateUser: npDummyUser1._id,
  555. parent: rootPage._id,
  556. revision: revisionIdDuplicate7,
  557. grantedGroups: [
  558. { item: groupIdA, type: GroupType.userGroup },
  559. { item: externalGroupIdA, type: GroupType.externalUserGroup },
  560. { item: groupIdB, type: GroupType.userGroup },
  561. { item: externalGroupIdB, type: GroupType.externalUserGroup },
  562. ],
  563. },
  564. {
  565. _id: pageIdDuplicate8,
  566. path: '/np_duplicate7/np_duplicate8',
  567. grant: Page.GRANT_USER_GROUP,
  568. creator: npDummyUser3._id,
  569. lastUpdateUser: npDummyUser3._id,
  570. parent: pageIdDuplicate7,
  571. revision: revisionIdDuplicate8,
  572. grantedGroups: [
  573. { item: groupIdC, type: GroupType.userGroup },
  574. { item: externalGroupIdC, type: GroupType.externalUserGroup },
  575. ],
  576. },
  577. {
  578. _id: pageIdDuplicate9,
  579. path: '/np_duplicate7/np_duplicate9',
  580. grant: Page.GRANT_OWNER,
  581. creator: npDummyUser2._id,
  582. lastUpdateUser: npDummyUser2._id,
  583. parent: pageIdDuplicate7,
  584. revision: revisionIdDuplicate9,
  585. grantedUsers: [npDummyUser2._id],
  586. },
  587. ]);
  588. await Revision.insertMany([
  589. {
  590. _id: revisionIdDuplicate1,
  591. body: 'np_duplicate1',
  592. format: 'markdown',
  593. pageId: pageIdDuplicate1,
  594. author: npDummyUser1._id,
  595. },
  596. {
  597. _id: revisionIdDuplicate2,
  598. body: 'np_duplicate2',
  599. format: 'markdown',
  600. pageId: pageIdDuplicate2,
  601. author: npDummyUser2._id,
  602. },
  603. {
  604. _id: revisionIdDuplicate3,
  605. body: 'np_duplicate3',
  606. format: 'markdown',
  607. pageId: pageIdDuplicate3,
  608. author: npDummyUser2._id,
  609. },
  610. {
  611. _id: revisionIdDuplicate4,
  612. body: 'np_duplicate4',
  613. format: 'markdown',
  614. pageId: pageIdDuplicate4,
  615. author: npDummyUser2._id,
  616. },
  617. {
  618. _id: revisionIdDuplicate5,
  619. body: 'np_duplicate5',
  620. format: 'markdown',
  621. pageId: pageIdDuplicate5,
  622. author: npDummyUser2._id,
  623. },
  624. {
  625. _id: revisionIdDuplicate6,
  626. body: 'np_duplicate6',
  627. format: 'markdown',
  628. pageId: pageIdDuplicate6,
  629. author: npDummyUser1._id,
  630. },
  631. {
  632. _id: revisionIdDuplicate7,
  633. body: 'np_duplicate7',
  634. format: 'markdown',
  635. pageId: pageIdDuplicate7,
  636. author: npDummyUser1._id,
  637. },
  638. {
  639. _id: revisionIdDuplicate8,
  640. body: 'np_duplicate8',
  641. format: 'markdown',
  642. pageId: pageIdDuplicate8,
  643. author: npDummyUser3._id,
  644. },
  645. {
  646. _id: revisionIdDuplicate9,
  647. body: 'np_duplicate9',
  648. format: 'markdown',
  649. pageId: pageIdDuplicate9,
  650. author: npDummyUser2._id,
  651. },
  652. ]);
  653. /**
  654. * Delete
  655. */
  656. const pageIdDelete1 = new mongoose.Types.ObjectId();
  657. const pageIdDelete2 = new mongoose.Types.ObjectId();
  658. const pageIdDelete3 = new mongoose.Types.ObjectId();
  659. const pageIdDelete4 = new mongoose.Types.ObjectId();
  660. await Page.insertMany([
  661. {
  662. _id: pageIdDelete1,
  663. path: '/npdel1_awl',
  664. grant: Page.GRANT_RESTRICTED,
  665. status: Page.STATUS_PUBLISHED,
  666. isEmpty: false,
  667. },
  668. {
  669. _id: pageIdDelete2,
  670. path: '/npdel2_ug',
  671. grant: Page.GRANT_USER_GROUP,
  672. grantedGroups: [{ item: groupIdA, type: GroupType.userGroup }, { item: externalGroupIdA, type: GroupType.externalUserGroup }],
  673. status: Page.STATUS_PUBLISHED,
  674. isEmpty: false,
  675. parent: rootPage._id,
  676. descendantCount: 0,
  677. },
  678. {
  679. _id: pageIdDelete3,
  680. path: '/npdel3_top',
  681. grant: Page.GRANT_USER_GROUP,
  682. grantedGroups: [{ item: groupIdA, type: GroupType.userGroup }, { item: externalGroupIdA, type: GroupType.externalUserGroup }],
  683. status: Page.STATUS_PUBLISHED,
  684. isEmpty: false,
  685. parent: rootPage._id,
  686. descendantCount: 2,
  687. },
  688. {
  689. _id: pageIdDelete4,
  690. path: '/npdel3_top/npdel4_ug',
  691. grant: Page.GRANT_USER_GROUP,
  692. grantedGroups: [{ item: groupIdB, type: GroupType.userGroup }, { item: externalGroupIdB, type: GroupType.externalUserGroup }],
  693. status: Page.STATUS_PUBLISHED,
  694. isEmpty: false,
  695. parent: pageIdDelete3._id,
  696. descendantCount: 1,
  697. },
  698. {
  699. path: '/npdel3_top/npdel4_ug',
  700. grant: Page.GRANT_RESTRICTED,
  701. status: Page.STATUS_PUBLISHED,
  702. isEmpty: false,
  703. },
  704. {
  705. path: '/npdel3_top/npdel4_ug/npdel5_ug',
  706. grant: Page.GRANT_USER_GROUP,
  707. grantedGroups: [{ item: groupIdC, type: GroupType.userGroup }, { item: externalGroupIdC, type: GroupType.externalUserGroup }],
  708. status: Page.STATUS_PUBLISHED,
  709. isEmpty: false,
  710. parent: pageIdDelete4._id,
  711. descendantCount: 0,
  712. },
  713. ]);
  714. /**
  715. * Delete completely
  716. */
  717. const pageIdDeleteComp1 = new mongoose.Types.ObjectId();
  718. const pageIdDeleteComp2 = new mongoose.Types.ObjectId();
  719. await Page.insertMany([
  720. {
  721. path: '/npdc1_awl',
  722. grant: Page.GRANT_RESTRICTED,
  723. status: Page.STATUS_PUBLISHED,
  724. isEmpty: false,
  725. },
  726. {
  727. path: '/npdc2_ug',
  728. grant: Page.GRANT_USER_GROUP,
  729. grantedGroups: [{ item: groupIdA, type: GroupType.userGroup }, { item: externalGroupIdA, type: GroupType.externalUserGroup }],
  730. status: Page.STATUS_PUBLISHED,
  731. isEmpty: false,
  732. parent: rootPage._id,
  733. },
  734. {
  735. _id: pageIdDeleteComp1,
  736. path: '/npdc3_ug',
  737. grant: Page.GRANT_USER_GROUP,
  738. grantedGroups: [{ item: groupIdA, type: GroupType.userGroup }, { item: externalGroupIdA, type: GroupType.externalUserGroup }],
  739. status: Page.STATUS_PUBLISHED,
  740. isEmpty: false,
  741. parent: rootPage._id,
  742. },
  743. {
  744. _id: pageIdDeleteComp2,
  745. path: '/npdc3_ug/npdc4_ug',
  746. grant: Page.GRANT_USER_GROUP,
  747. grantedGroups: [{ item: groupIdB, type: GroupType.userGroup }, { item: externalGroupIdB, type: GroupType.externalUserGroup }],
  748. status: Page.STATUS_PUBLISHED,
  749. isEmpty: false,
  750. parent: pageIdDeleteComp1,
  751. },
  752. {
  753. path: '/npdc3_ug/npdc4_ug/npdc5_ug',
  754. grant: Page.GRANT_USER_GROUP,
  755. grantedGroups: [{ item: groupIdC, type: GroupType.userGroup }, { item: externalGroupIdC, type: GroupType.externalUserGroup }],
  756. status: Page.STATUS_PUBLISHED,
  757. isEmpty: false,
  758. parent: pageIdDeleteComp2,
  759. },
  760. {
  761. path: '/npdc3_ug/npdc4_ug',
  762. grant: Page.GRANT_RESTRICTED,
  763. status: Page.STATUS_PUBLISHED,
  764. isEmpty: false,
  765. },
  766. ]);
  767. /**
  768. * Revert
  769. */
  770. await Page.insertMany([
  771. {
  772. _id: pageIdRevert1,
  773. path: '/trash/np_revert1',
  774. grant: Page.GRANT_RESTRICTED,
  775. revision: revisionIdRevert1,
  776. status: Page.STATUS_DELETED,
  777. },
  778. {
  779. _id: pageIdRevert2,
  780. path: '/trash/np_revert2',
  781. grant: Page.GRANT_USER_GROUP,
  782. grantedGroups: [{ item: groupIdA, type: GroupType.userGroup }, { item: externalGroupIdA, type: GroupType.externalUserGroup }],
  783. revision: revisionIdRevert2,
  784. status: Page.STATUS_DELETED,
  785. },
  786. {
  787. _id: pageIdRevert3,
  788. path: '/trash/np_revert3',
  789. revision: revisionIdRevert3,
  790. status: Page.STATUS_DELETED,
  791. parent: rootPage._id,
  792. },
  793. {
  794. _id: pageIdRevert4,
  795. path: '/trash/np_revert3/middle/np_revert4',
  796. grant: Page.GRANT_RESTRICTED,
  797. revision: revisionIdRevert4,
  798. status: Page.STATUS_DELETED,
  799. },
  800. {
  801. _id: pageIdRevert5,
  802. path: '/trash/np_revert5',
  803. grant: Page.GRANT_USER_GROUP,
  804. grantedGroups: [{ item: groupIdA, type: GroupType.userGroup }, { item: externalGroupIdA, type: GroupType.externalUserGroup }],
  805. revision: revisionIdRevert5,
  806. status: Page.STATUS_DELETED,
  807. },
  808. {
  809. _id: pageIdRevert6,
  810. path: '/trash/np_revert5/middle/np_revert6',
  811. grant: Page.GRANT_USER_GROUP,
  812. grantedGroups: [{ item: groupIdB, type: GroupType.userGroup }, { item: externalGroupIdB, type: GroupType.externalUserGroup }],
  813. revision: revisionIdRevert6,
  814. status: Page.STATUS_DELETED,
  815. },
  816. ]);
  817. await Revision.insertMany([
  818. {
  819. _id: revisionIdRevert1,
  820. pageId: pageIdRevert1,
  821. body: 'np_revert1',
  822. format: 'markdown',
  823. author: dummyUser1._id,
  824. },
  825. {
  826. _id: revisionIdRevert2,
  827. pageId: pageIdRevert2,
  828. body: 'np_revert2',
  829. format: 'markdown',
  830. author: npDummyUser1,
  831. },
  832. {
  833. _id: revisionIdRevert3,
  834. pageId: pageIdRevert3,
  835. body: 'np_revert3',
  836. format: 'markdown',
  837. author: npDummyUser1,
  838. },
  839. {
  840. _id: revisionIdRevert4,
  841. pageId: pageIdRevert4,
  842. body: 'np_revert4',
  843. format: 'markdown',
  844. author: npDummyUser1,
  845. },
  846. {
  847. _id: revisionIdRevert5,
  848. pageId: pageIdRevert5,
  849. body: 'np_revert5',
  850. format: 'markdown',
  851. author: npDummyUser1,
  852. },
  853. {
  854. _id: revisionIdRevert6,
  855. pageId: pageIdRevert6,
  856. body: 'np_revert6',
  857. format: 'markdown',
  858. author: npDummyUser1,
  859. },
  860. ]);
  861. await Tag.insertMany([
  862. { _id: tagIdRevert1, name: 'np_revertTag1' },
  863. { _id: tagIdRevert2, name: 'np_revertTag2' },
  864. ]);
  865. await PageTagRelation.insertMany([
  866. {
  867. relatedPage: pageIdRevert1,
  868. relatedTag: tagIdRevert1,
  869. isPageTrashed: true,
  870. },
  871. {
  872. relatedPage: pageIdRevert2,
  873. relatedTag: tagIdRevert2,
  874. isPageTrashed: true,
  875. },
  876. ]);
  877. });
  878. describe('create', () => {
  879. describe('Creating a page using existing path', () => {
  880. test('with grant RESTRICTED should only create the page and change nothing else', async() => {
  881. const isGrantNormalizedSpy = jest.spyOn(crowi.pageGrantService, 'isGrantNormalized');
  882. const pathT = '/mc4_top';
  883. const path1 = '/mc4_top/mc1_emp';
  884. const path2 = '/mc4_top/mc1_emp/mc2_pub';
  885. const pageT = await Page.findOne({ path: pathT, descendantCount: 1 });
  886. const page1 = await Page.findOne({ path: path1, grant: Page.GRANT_PUBLIC });
  887. const page2 = await Page.findOne({ path: path2 });
  888. const page3 = await Page.findOne({ path: path1, grant: Page.GRANT_RESTRICTED });
  889. expect(pageT).toBeTruthy();
  890. expect(page1).toBeTruthy();
  891. expect(page2).toBeTruthy();
  892. expect(page3).toBeNull();
  893. // use existing path
  894. await create(path1, 'new body', dummyUser1, { grant: Page.GRANT_RESTRICTED });
  895. const _pageT = await Page.findOne({ path: pathT });
  896. const _page1 = await Page.findOne({ path: path1, grant: Page.GRANT_PUBLIC });
  897. const _page2 = await Page.findOne({ path: path2 });
  898. const _page3 = await Page.findOne({ path: path1, grant: Page.GRANT_RESTRICTED });
  899. expect(_pageT).toBeTruthy();
  900. expect(_page1).toBeTruthy();
  901. expect(_page2).toBeTruthy();
  902. expect(_page3).toBeTruthy();
  903. expect(_pageT.descendantCount).toBe(1);
  904. // isGrantNormalized is not called when GRANT RESTRICTED
  905. expect(isGrantNormalizedSpy).toBeCalledTimes(0);
  906. });
  907. });
  908. describe('Creating a page under a page with grant RESTRICTED', () => {
  909. test('will create a new empty page with the same path as the grant RESTRECTED page and become a parent', async() => {
  910. const isGrantNormalizedSpy = jest.spyOn(crowi.pageGrantService, 'isGrantNormalized');
  911. const pathT = '/mc5_top';
  912. const path1 = '/mc5_top/mc3_awl';
  913. const pathN = '/mc5_top/mc3_awl/mc4_pub'; // used to create
  914. const pageT = await Page.findOne({ path: pathT });
  915. const page1 = await Page.findOne({ path: path1, grant: Page.GRANT_RESTRICTED });
  916. const page2 = await Page.findOne({ path: path1, grant: Page.GRANT_PUBLIC });
  917. expect(pageT).toBeTruthy();
  918. expect(page1).toBeTruthy();
  919. expect(page2).toBeNull();
  920. await create(pathN, 'new body', dummyUser1, { grant: Page.GRANT_PUBLIC });
  921. const _pageT = await Page.findOne({ path: pathT });
  922. const _page1 = await Page.findOne({ path: path1, grant: Page.GRANT_RESTRICTED });
  923. const _page2 = await Page.findOne({ path: path1, grant: Page.GRANT_PUBLIC, isEmpty: true });
  924. const _pageN = await Page.findOne({ path: pathN, grant: Page.GRANT_PUBLIC }); // newly crated
  925. expect(_pageT).toBeTruthy();
  926. expect(_page1).toBeTruthy();
  927. expect(_page2).toBeTruthy();
  928. expect(_pageN).toBeTruthy();
  929. expect(_pageN.parent).toStrictEqual(_page2._id);
  930. expect(_pageT.descendantCount).toStrictEqual(1);
  931. // isGrantNormalized is called when GRANT PUBLIC
  932. expect(isGrantNormalizedSpy).toBeCalledTimes(1);
  933. });
  934. });
  935. describe('Creating a page under a page with grant USER_GROUP', () => {
  936. describe('When onlyInheritUserRelatedGrantedGroups is true', () => {
  937. test('Only user related groups should be inherited', async() => {
  938. const pathT = '/mc6_top';
  939. const pageT = await Page.findOne({ path: pathT });
  940. expect(pageT).toBeTruthy();
  941. const pathN = '/mc6_top/onlyRelatedGroupsInherited'; // path to create
  942. await create(pathN, 'new body', npDummyUser1, { grant: Page.GRANT_USER_GROUP, onlyInheritUserRelatedGrantedGroups: true });
  943. const _pageT = await Page.findOne({ path: pathT });
  944. const _pageN = await Page.findOne({ path: pathN, grant: Page.GRANT_USER_GROUP }); // newly crated
  945. expect(_pageT).toBeTruthy();
  946. expect(_pageN).toBeTruthy();
  947. expect(_pageN.parent).toStrictEqual(_pageT._id);
  948. expect(_pageT.descendantCount).toStrictEqual(1);
  949. expect(normalizeGrantedGroups(_pageN.grantedGroups)).toStrictEqual([
  950. { item: groupIdIsolate, type: GroupType.userGroup },
  951. ]);
  952. });
  953. });
  954. describe('When onlyInheritUserRelatedGrantedGroups is false', () => {
  955. test('All groups should be inherited', async() => {
  956. const pathT = '/mc6_top';
  957. const pageT = await Page.findOne({ path: pathT });
  958. expect(pageT).toBeTruthy();
  959. const pathN = '/mc6_top/allGroupsInherited'; // path to create
  960. await create(pathN, 'new body', npDummyUser1, { grant: Page.GRANT_USER_GROUP, onlyInheritUserRelatedGrantedGroups: false });
  961. const _pageT = await Page.findOne({ path: pathT });
  962. const _pageN = await Page.findOne({ path: pathN, grant: Page.GRANT_USER_GROUP }); // newly crated
  963. expect(_pageT).toBeTruthy();
  964. expect(_pageN).toBeTruthy();
  965. expect(_pageN.parent).toStrictEqual(_pageT._id);
  966. expect(_pageT.descendantCount).toStrictEqual(2);
  967. expect(normalizeGrantedGroups(_pageN.grantedGroups)).toStrictEqual([
  968. { item: groupIdIsolate, type: GroupType.userGroup },
  969. { item: groupIdB, type: GroupType.userGroup },
  970. ]);
  971. });
  972. });
  973. });
  974. });
  975. describe('create by system', () => {
  976. describe('Creating a page using existing path', () => {
  977. test('with grant RESTRICTED should only create the page and change nothing else', async() => {
  978. const isGrantNormalizedSpy = jest.spyOn(crowi.pageGrantService, 'isGrantNormalized');
  979. const pathT = '/mc4_top_by_system';
  980. const path1 = '/mc4_top_by_system/mc1_emp_by_system';
  981. const path2 = '/mc4_top_by_system/mc1_emp_by_system/mc2_pub_by_system';
  982. const pageT = await Page.findOne({ path: pathT, descendantCount: 1 });
  983. const page1 = await Page.findOne({ path: path1, grant: Page.GRANT_PUBLIC });
  984. const page2 = await Page.findOne({ path: path2 });
  985. const page3 = await Page.findOne({ path: path1, grant: Page.GRANT_RESTRICTED });
  986. expect(pageT).toBeTruthy();
  987. expect(page1).toBeTruthy();
  988. expect(page2).toBeTruthy();
  989. expect(page3).toBeNull();
  990. // use existing path
  991. await crowi.pageService.forceCreateBySystem(path1, 'new body', { grant: Page.GRANT_RESTRICTED });
  992. const _pageT = await Page.findOne({ path: pathT });
  993. const _page1 = await Page.findOne({ path: path1, grant: Page.GRANT_PUBLIC });
  994. const _page2 = await Page.findOne({ path: path2 });
  995. const _page3 = await Page.findOne({ path: path1, grant: Page.GRANT_RESTRICTED });
  996. expect(_pageT).toBeTruthy();
  997. expect(_page1).toBeTruthy();
  998. expect(_page2).toBeTruthy();
  999. expect(_page3).toBeTruthy();
  1000. expect(_pageT.descendantCount).toBe(1);
  1001. // isGrantNormalized is not called when create by ststem
  1002. expect(isGrantNormalizedSpy).toBeCalledTimes(0);
  1003. });
  1004. });
  1005. describe('Creating a page under a page with grant RESTRICTED', () => {
  1006. test('will create a new empty page with the same path as the grant RESTRECTED page and become a parent', async() => {
  1007. const isGrantNormalizedSpy = jest.spyOn(crowi.pageGrantService, 'isGrantNormalized');
  1008. const pathT = '/mc5_top_by_system';
  1009. const path1 = '/mc5_top_by_system/mc3_awl_by_system';
  1010. const pathN = '/mc5_top_by_system/mc3_awl_by_system/mc4_pub_by_system'; // used to create
  1011. const pageT = await Page.findOne({ path: pathT });
  1012. const page1 = await Page.findOne({ path: path1, grant: Page.GRANT_RESTRICTED });
  1013. const page2 = await Page.findOne({ path: path1, grant: Page.GRANT_PUBLIC });
  1014. expect(pageT).toBeTruthy();
  1015. expect(page1).toBeTruthy();
  1016. expect(page2).toBeNull();
  1017. await crowi.pageService.forceCreateBySystem(pathN, 'new body', { grant: Page.GRANT_PUBLIC });
  1018. const _pageT = await Page.findOne({ path: pathT });
  1019. const _page1 = await Page.findOne({ path: path1, grant: Page.GRANT_RESTRICTED });
  1020. const _page2 = await Page.findOne({ path: path1, grant: Page.GRANT_PUBLIC, isEmpty: true });
  1021. const _pageN = await Page.findOne({ path: pathN, grant: Page.GRANT_PUBLIC }); // newly crated
  1022. expect(_pageT).toBeTruthy();
  1023. expect(_page1).toBeTruthy();
  1024. expect(_page2).toBeTruthy();
  1025. expect(_pageN).toBeTruthy();
  1026. expect(_pageN.parent).toStrictEqual(_page2._id);
  1027. expect(_pageT.descendantCount).toStrictEqual(1);
  1028. // isGrantNormalized is not called when create by ststem
  1029. expect(isGrantNormalizedSpy).toBeCalledTimes(0);
  1030. });
  1031. });
  1032. });
  1033. describe('Rename', () => {
  1034. const renamePage = async(page, newPagePath, user, options, activityParameters?) => {
  1035. // mock return value
  1036. const mockedRenameSubOperation = jest.spyOn(crowi.pageService, 'renameSubOperation').mockReturnValue(null);
  1037. const renamedPage = await crowi.pageService.renamePage(page, newPagePath, user, options, activityParameters);
  1038. // retrieve the arguments passed when calling method renameSubOperation inside renamePage method
  1039. const argsForRenameSubOperation = mockedRenameSubOperation.mock.calls[0];
  1040. // restores the original implementation
  1041. mockedRenameSubOperation.mockRestore();
  1042. // rename descendants
  1043. if (page.grant !== Page.GRANT_RESTRICTED) {
  1044. await crowi.pageService.renameSubOperation(...argsForRenameSubOperation);
  1045. }
  1046. return renamedPage;
  1047. };
  1048. test('Should rename/move with descendants with grant normalized pages', async() => {
  1049. const _pathD = '/np_rename1_destination';
  1050. const _path2 = '/np_rename2';
  1051. const _path3 = '/np_rename2/np_rename3';
  1052. const _propertiesD = { grant: Page.GRANT_PUBLIC };
  1053. const _properties2 = { grant: Page.GRANT_USER_GROUP, grantedGroups: { $elemMatch: { item: groupIdB } } };
  1054. const _properties3 = { grant: Page.GRANT_USER_GROUP, grantedGroups: { $elemMatch: { item: groupIdC } } };
  1055. const _pageD = await Page.findOne({ path: _pathD, ..._propertiesD });
  1056. const _page2 = await Page.findOne({ path: _path2, ..._properties2 });
  1057. const _page3 = await Page.findOne({ path: _path3, ..._properties3, parent: _page2._id });
  1058. expect(_pageD).toBeTruthy();
  1059. expect(_page2).toBeTruthy();
  1060. expect(_page3).toBeTruthy();
  1061. const newPathForPage2 = '/np_rename1_destination/np_rename2';
  1062. const newPathForPage3 = '/np_rename1_destination/np_rename2/np_rename3';
  1063. await renamePage(_page2, newPathForPage2, npDummyUser2, {}, {
  1064. ip: '::ffff:127.0.0.1',
  1065. endpoint: '/_api/v3/pages/rename',
  1066. activityId: '62e291bc10e0ab61bd691794',
  1067. });
  1068. const pageD = await Page.findOne({ path: _pathD, ..._propertiesD });
  1069. const page2 = await Page.findOne({ path: _path2, ..._properties2 }); // not exist
  1070. const page3 = await Page.findOne({ path: _path3, ..._properties3, parent: _page2._id }); // not exist
  1071. const page2Renamed = await Page.findOne({ path: newPathForPage2 }); // renamed
  1072. const page3Renamed = await Page.findOne({ path: newPathForPage3 }); // renamed
  1073. expect(pageD).toBeTruthy();
  1074. expect(page2).toBeNull();
  1075. expect(page3).toBeNull();
  1076. expect(page2Renamed).toBeTruthy();
  1077. expect(page3Renamed).toBeTruthy();
  1078. expect(page2Renamed.parent).toStrictEqual(_pageD._id);
  1079. expect(page3Renamed.parent).toStrictEqual(page2Renamed._id);
  1080. expect(normalizeGrantedGroups(page2Renamed.grantedGroups)).toStrictEqual(normalizeGrantedGroups(_page2.grantedGroups));
  1081. expect(normalizeGrantedGroups(page3Renamed.grantedGroups)).toStrictEqual(normalizeGrantedGroups(_page3.grantedGroups));
  1082. expect(xssSpy).toHaveBeenCalled();
  1083. });
  1084. test('Should throw with NOT grant normalized pages', async() => {
  1085. const _pathD = '/np_rename4_destination';
  1086. const _path2 = '/np_rename5';
  1087. const _path3 = '/np_rename5/np_rename6';
  1088. const _propertiesD = { grant: Page.GRANT_USER_GROUP, grantedGroups: { $elemMatch: { item: groupIdIsolate } } };
  1089. const _properties2 = { grant: Page.GRANT_USER_GROUP, grantedGroups: { $elemMatch: { item: groupIdB } } };
  1090. const _properties3 = { grant: Page.GRANT_USER_GROUP, grantedGroups: { $elemMatch: { item: groupIdB } } };
  1091. const _pageD = await Page.findOne({ path: _pathD, ..._propertiesD });// isolate
  1092. const _page2 = await Page.findOne({ path: _path2, ..._properties2 });// groupIdB
  1093. const _page3 = await Page.findOne({ path: _path3, ..._properties3, parent: _page2 });// groupIdB
  1094. expect(_pageD).toBeTruthy();
  1095. expect(_page2).toBeTruthy();
  1096. expect(_page3).toBeTruthy();
  1097. const newPathForPage2 = '/np_rename4_destination/np_rename5';
  1098. const newPathForPage3 = '/np_rename4_destination/np_rename5/np_rename6';
  1099. let isThrown = false;
  1100. try {
  1101. await renamePage(_page2, newPathForPage2, dummyUser1, {}, {
  1102. ip: '::ffff:127.0.0.1',
  1103. endpoint: '/_api/v3/pages/rename',
  1104. activityId: '62e291bc10e0ab61bd691794',
  1105. });
  1106. }
  1107. catch (err) {
  1108. isThrown = true;
  1109. }
  1110. expect(isThrown).toBe(true);
  1111. const page2 = await Page.findOne({ path: _path2 }); // not renamed thus exist
  1112. const page3 = await Page.findOne({ path: _path3 }); // not renamed thus exist
  1113. const page2Renamed = await Page.findOne({ path: newPathForPage2 }); // not exist
  1114. const page3Renamed = await Page.findOne({ path: newPathForPage3 }); // not exist
  1115. expect(page2).toBeTruthy();
  1116. expect(page3).toBeTruthy();
  1117. expect(page2Renamed).toBeNull();
  1118. expect(page3Renamed).toBeNull();
  1119. });
  1120. test('Should rename/move multiple pages: child page with GRANT_RESTRICTED should NOT be renamed.', async() => {
  1121. const _pathD = '/np_rename7_destination';
  1122. const _path2 = '/np_rename8';
  1123. const _path3 = '/np_rename8/np_rename9';
  1124. const _pageD = await Page.findOne({ path: _pathD, grant: Page.GRANT_USER_GROUP, grantedGroups: { $elemMatch: { item: groupIdIsolate } } });
  1125. const _page2 = await Page.findOne({ path: _path2, grant: Page.GRANT_RESTRICTED });
  1126. const _page3 = await Page.findOne({ path: _path3, grant: Page.GRANT_RESTRICTED });
  1127. expect(_pageD).toBeTruthy();
  1128. expect(_page2).toBeTruthy();
  1129. expect(_page3).toBeTruthy();
  1130. const newPathForPage2 = '/np_rename7_destination/np_rename8';
  1131. const newpathForPage3 = '/np_rename7_destination/np_rename8/np_rename9';
  1132. await renamePage(_page2, newPathForPage2, npDummyUser1, { isRecursively: true }, {
  1133. ip: '::ffff:127.0.0.1',
  1134. endpoint: '/_api/v3/pages/rename',
  1135. activityId: '62e291bc10e0ab61bd691794',
  1136. });
  1137. const page2 = await Page.findOne({ path: _path2 }); // not exist
  1138. const page3 = await Page.findOne({ path: _path3 }); // not renamed thus exist
  1139. const page2Renamed = await Page.findOne({ path: newPathForPage2 }); // exist
  1140. const page3Renamed = await Page.findOne({ path: newpathForPage3 }); // not exist
  1141. expect(page2).toBeNull();
  1142. expect(page3).toBeTruthy();
  1143. expect(page2Renamed).toBeTruthy();
  1144. expect(page3Renamed).toBeNull();
  1145. expect(page2Renamed.parent).toBeNull();
  1146. expect(xssSpy).toHaveBeenCalled();
  1147. });
  1148. });
  1149. describe('Duplicate', () => {
  1150. const duplicate = async(page, newPagePath: string, user, isRecursively: boolean, onlyDuplicateUserRelatedResources: boolean) => {
  1151. // mock return value
  1152. const mockedDuplicateRecursivelyMainOperation = jest.spyOn(crowi.pageService, 'duplicateRecursivelyMainOperation').mockReturnValue(null);
  1153. const duplicatedPage = await crowi.pageService.duplicate(page, newPagePath, user, isRecursively, onlyDuplicateUserRelatedResources);
  1154. // retrieve the arguments passed when calling method duplicateRecursivelyMainOperation inside duplicate method
  1155. const argsForDuplicateRecursivelyMainOperation = mockedDuplicateRecursivelyMainOperation.mock.calls[0];
  1156. // restores the original implementation
  1157. mockedDuplicateRecursivelyMainOperation.mockRestore();
  1158. // duplicate descendants
  1159. if (page.grant !== Page.GRANT_RESTRICTED && isRecursively) {
  1160. await crowi.pageService.duplicateRecursivelyMainOperation(...argsForDuplicateRecursivelyMainOperation);
  1161. }
  1162. return duplicatedPage;
  1163. };
  1164. test('Duplicate single page with GRANT_RESTRICTED', async() => {
  1165. const _page = await Page.findOne({ path: '/np_duplicate1', grant: Page.GRANT_RESTRICTED }).populate({ path: 'revision', model: 'Revision' });
  1166. const _revision = _page.revision;
  1167. expect(_page).toBeTruthy();
  1168. expect(_revision).toBeTruthy();
  1169. const newPagePath = '/dup_np_duplicate1';
  1170. await duplicate(_page, newPagePath, npDummyUser1, false, false);
  1171. const duplicatedPage = await Page.findOne({ path: newPagePath });
  1172. const duplicatedRevision = await Revision.findOne({ pageId: duplicatedPage._id });
  1173. expect(xssSpy).toHaveBeenCalled();
  1174. expect(duplicatedPage).toBeTruthy();
  1175. expect(duplicatedPage._id).not.toStrictEqual(_page._id);
  1176. expect(duplicatedPage.grant).toBe(_page.grant);
  1177. expect(duplicatedPage.parent).toBeNull();
  1178. expect(duplicatedPage.parent).toStrictEqual(_page.parent);
  1179. expect(duplicatedPage.revision).toStrictEqual(duplicatedRevision._id);
  1180. expect(duplicatedRevision.body).toBe(_revision.body);
  1181. });
  1182. test('Should duplicate multiple pages with GRANT_USER_GROUP', async() => {
  1183. const _path1 = '/np_duplicate2';
  1184. const _path2 = '/np_duplicate2/np_duplicate3';
  1185. const _page1 = await Page.findOne({ path: _path1, parent: rootPage._id, grantedGroups: { $elemMatch: { item: groupIdA } } })
  1186. .populate({ path: 'revision', model: 'Revision' });
  1187. const _page2 = await Page.findOne({ path: _path2, parent: _page1._id, grantedGroups: { $elemMatch: { item: groupIdB } } })
  1188. .populate({ path: 'revision', model: 'Revision' });
  1189. const _revision1 = _page1.revision;
  1190. const _revision2 = _page2.revision;
  1191. expect(_page1).toBeTruthy();
  1192. expect(_page2).toBeTruthy();
  1193. expect(_revision1).toBeTruthy();
  1194. expect(_revision2).toBeTruthy();
  1195. const newPagePath = '/dup_np_duplicate2';
  1196. await duplicate(_page1, newPagePath, npDummyUser2, true, false);
  1197. const duplicatedPage1 = await Page.findOne({ path: newPagePath }).populate({ path: 'revision', model: 'Revision' });
  1198. const duplicatedPage2 = await Page.findOne({ path: '/dup_np_duplicate2/np_duplicate3' }).populate({ path: 'revision', model: 'Revision' });
  1199. const duplicatedRevision1 = duplicatedPage1.revision;
  1200. const duplicatedRevision2 = duplicatedPage2.revision;
  1201. expect(xssSpy).toHaveBeenCalled();
  1202. expect(duplicatedPage1).toBeTruthy();
  1203. expect(duplicatedPage2).toBeTruthy();
  1204. expect(duplicatedRevision1).toBeTruthy();
  1205. expect(duplicatedRevision2).toBeTruthy();
  1206. expect(normalizeGrantedGroups(duplicatedPage1.grantedGroups)).toStrictEqual([
  1207. { item: groupIdA, type: GroupType.userGroup },
  1208. { item: externalGroupIdA, type: GroupType.externalUserGroup },
  1209. ]);
  1210. expect(normalizeGrantedGroups(duplicatedPage2.grantedGroups)).toStrictEqual([
  1211. { item: groupIdB, type: GroupType.userGroup },
  1212. { item: externalGroupIdB, type: GroupType.externalUserGroup },
  1213. ]);
  1214. expect(duplicatedPage1.parent).toStrictEqual(_page1.parent);
  1215. expect(duplicatedPage2.parent).toStrictEqual(duplicatedPage1._id);
  1216. expect(duplicatedRevision1.body).toBe(_revision1.body);
  1217. expect(duplicatedRevision2.body).toBe(_revision2.body);
  1218. expect(duplicatedRevision1.pageId).toStrictEqual(duplicatedPage1._id);
  1219. expect(duplicatedRevision2.pageId).toStrictEqual(duplicatedPage2._id);
  1220. });
  1221. test('Should duplicate multiple pages. Page with GRANT_RESTRICTED should NOT be duplicated', async() => {
  1222. const _path1 = '/np_duplicate4';
  1223. const _path2 = '/np_duplicate4/np_duplicate5';
  1224. const _path3 = '/np_duplicate4/np_duplicate6';
  1225. const _page1 = await Page.findOne({ path: _path1, parent: rootPage._id, grant: Page.GRANT_PUBLIC })
  1226. .populate({ path: 'revision', model: 'Revision' });
  1227. const _page2 = await Page.findOne({ path: _path2, grant: Page.GRANT_RESTRICTED }).populate({ path: 'revision', model: 'Revision' });
  1228. const _page3 = await Page.findOne({ path: _path3, grant: Page.GRANT_PUBLIC }).populate({ path: 'revision', model: 'Revision' });
  1229. const baseRevision1 = _page1.revision;
  1230. const baseRevision2 = _page2.revision;
  1231. const baseRevision3 = _page3.revision;
  1232. expect(_page1).toBeTruthy();
  1233. expect(_page2).toBeTruthy();
  1234. expect(_page3).toBeTruthy();
  1235. expect(baseRevision1).toBeTruthy();
  1236. expect(baseRevision2).toBeTruthy();
  1237. const newPagePath = '/dup_np_duplicate4';
  1238. await duplicate(_page1, newPagePath, npDummyUser1, true, false);
  1239. const duplicatedPage1 = await Page.findOne({ path: newPagePath }).populate({ path: 'revision', model: 'Revision' });
  1240. const duplicatedPage2 = await Page.findOne({ path: '/dup_np_duplicate4/np_duplicate5' }).populate({ path: 'revision', model: 'Revision' });
  1241. const duplicatedPage3 = await Page.findOne({ path: '/dup_np_duplicate4/np_duplicate6' }).populate({ path: 'revision', model: 'Revision' });
  1242. const duplicatedRevision1 = duplicatedPage1.revision;
  1243. const duplicatedRevision3 = duplicatedPage3.revision;
  1244. expect(xssSpy).toHaveBeenCalled();
  1245. expect(duplicatedPage1).toBeTruthy();
  1246. expect(duplicatedPage2).toBeNull();
  1247. expect(duplicatedPage3).toBeTruthy();
  1248. expect(duplicatedRevision1).toBeTruthy();
  1249. expect(duplicatedRevision3).toBeTruthy();
  1250. expect(duplicatedPage1.grant).toStrictEqual(Page.GRANT_PUBLIC);
  1251. expect(duplicatedPage3.grant).toStrictEqual(Page.GRANT_PUBLIC);
  1252. expect(duplicatedPage1.parent).toStrictEqual(_page1.parent);
  1253. expect(duplicatedPage3.parent).toStrictEqual(duplicatedPage1._id);
  1254. expect(duplicatedRevision1.body).toBe(baseRevision1.body);
  1255. expect(duplicatedRevision3.body).toBe(baseRevision3.body);
  1256. expect(duplicatedRevision1.pageId).toStrictEqual(duplicatedPage1._id);
  1257. expect(duplicatedRevision3.pageId).toStrictEqual(duplicatedPage3._id);
  1258. });
  1259. test('Should duplicate only user related pages and granted groups when onlyDuplicateUserRelatedResources is true', async() => {
  1260. const _path1 = '/np_duplicate7';
  1261. const _path2 = '/np_duplicate7/np_duplicate8';
  1262. const _path3 = '/np_duplicate7/np_duplicate9';
  1263. const _page1 = await Page.findOne({ path: _path1, parent: rootPage._id })
  1264. .populate({ path: 'revision', model: 'Revision' });
  1265. const _page2 = await Page.findOne({ path: _path2, parent: _page1._id });
  1266. const _page3 = await Page.findOne({ path: _path3, parent: _page1._id });
  1267. const _revision1 = _page1.revision;
  1268. expect(_page1).toBeTruthy();
  1269. expect(_page2).toBeTruthy();
  1270. expect(_page3).toBeTruthy();
  1271. expect(_revision1).toBeTruthy();
  1272. const newPagePath = '/dup_np_duplicate7';
  1273. await duplicate(_page1, newPagePath, npDummyUser1, true, true);
  1274. const duplicatedPage1 = await Page.findOne({ path: newPagePath }).populate({ path: 'revision', model: 'Revision' });
  1275. const duplicatedPage2 = await Page.findOne({ path: '/dup_np_duplicate7/np_duplicate8' }).populate({ path: 'revision', model: 'Revision' });
  1276. const duplicatedPage3 = await Page.findOne({ path: '/dup_np_duplicate7/np_duplicate9' }).populate({ path: 'revision', model: 'Revision' });
  1277. const duplicatedRevision1 = duplicatedPage1.revision;
  1278. expect(xssSpy).toHaveBeenCalled();
  1279. expect(duplicatedPage1).toBeTruthy();
  1280. expect(duplicatedPage2).toBeFalsy();
  1281. expect(duplicatedPage3).toBeFalsy();
  1282. expect(duplicatedRevision1).toBeTruthy();
  1283. expect(normalizeGrantedGroups(duplicatedPage1.grantedGroups)).toStrictEqual([
  1284. { item: groupIdA, type: GroupType.userGroup },
  1285. { item: externalGroupIdA, type: GroupType.externalUserGroup },
  1286. ]);
  1287. expect(duplicatedPage1.parent).toStrictEqual(_page1.parent);
  1288. expect(duplicatedRevision1.body).toBe(_revision1.body);
  1289. expect(duplicatedRevision1.pageId).toStrictEqual(duplicatedPage1._id);
  1290. });
  1291. test('Should duplicate all pages and granted groups when onlyDuplicateUserRelatedResources is false', async() => {
  1292. const _path1 = '/np_duplicate7';
  1293. const _path2 = '/np_duplicate7/np_duplicate8';
  1294. const _path3 = '/np_duplicate7/np_duplicate9';
  1295. const _page1 = await Page.findOne({ path: _path1, parent: rootPage._id })
  1296. .populate({ path: 'revision', model: 'Revision' });
  1297. const _page2 = await Page.findOne({ path: _path2, parent: _page1._id })
  1298. .populate({ path: 'revision', model: 'Revision' });
  1299. const _page3 = await Page.findOne({ path: _path3, parent: _page1._id })
  1300. .populate({ path: 'revision', model: 'Revision' });
  1301. const _revision1 = _page1.revision;
  1302. const _revision2 = _page2.revision;
  1303. const _revision3 = _page3.revision;
  1304. expect(_page1).toBeTruthy();
  1305. expect(_page2).toBeTruthy();
  1306. expect(_page3).toBeTruthy();
  1307. expect(_revision1).toBeTruthy();
  1308. expect(_revision2).toBeTruthy();
  1309. expect(_revision3).toBeTruthy();
  1310. const newPagePath = '/dup2_np_duplicate7';
  1311. await duplicate(_page1, newPagePath, npDummyUser1, true, false);
  1312. const duplicatedPage1 = await Page.findOne({ path: newPagePath }).populate({ path: 'revision', model: 'Revision' });
  1313. const duplicatedPage2 = await Page.findOne({ path: '/dup2_np_duplicate7/np_duplicate8' }).populate({ path: 'revision', model: 'Revision' });
  1314. const duplicatedPage3 = await Page.findOne({ path: '/dup2_np_duplicate7/np_duplicate9' }).populate({ path: 'revision', model: 'Revision' });
  1315. const duplicatedRevision1 = duplicatedPage1.revision;
  1316. const duplicatedRevision2 = duplicatedPage2.revision;
  1317. const duplicatedRevision3 = duplicatedPage3.revision;
  1318. expect(xssSpy).toHaveBeenCalled();
  1319. expect(duplicatedPage1).toBeTruthy();
  1320. expect(duplicatedPage2).toBeTruthy();
  1321. expect(duplicatedPage3).toBeTruthy();
  1322. expect(duplicatedRevision1).toBeTruthy();
  1323. expect(duplicatedRevision2).toBeTruthy();
  1324. expect(duplicatedRevision3).toBeTruthy();
  1325. expect(normalizeGrantedGroups(duplicatedPage1.grantedGroups)).toStrictEqual([
  1326. { item: groupIdA, type: GroupType.userGroup },
  1327. { item: externalGroupIdA, type: GroupType.externalUserGroup },
  1328. { item: groupIdB, type: GroupType.userGroup },
  1329. { item: externalGroupIdB, type: GroupType.externalUserGroup },
  1330. ]);
  1331. expect(duplicatedPage1.parent).toStrictEqual(_page1.parent);
  1332. expect(duplicatedRevision1.body).toBe(_revision1.body);
  1333. expect(duplicatedRevision1.pageId).toStrictEqual(duplicatedPage1._id);
  1334. expect(normalizeGrantedGroups(duplicatedPage2.grantedGroups)).toStrictEqual([
  1335. { item: groupIdC, type: GroupType.userGroup },
  1336. { item: externalGroupIdC, type: GroupType.externalUserGroup },
  1337. ]);
  1338. expect(duplicatedPage2.parent).toStrictEqual(duplicatedPage1._id);
  1339. expect(duplicatedRevision2.body).toBe(_revision2.body);
  1340. expect(duplicatedRevision2.pageId).toStrictEqual(duplicatedPage2._id);
  1341. expect(duplicatedPage3.grantedUsers).toStrictEqual([npDummyUser2._id]);
  1342. expect(duplicatedPage3.parent).toStrictEqual(duplicatedPage1._id);
  1343. expect(duplicatedRevision3.body).toBe(_revision3.body);
  1344. expect(duplicatedRevision3.pageId).toStrictEqual(duplicatedPage3._id);
  1345. });
  1346. });
  1347. describe('Delete', () => {
  1348. const deletePage = async(page, user, options, isRecursively, activityParameters?) => {
  1349. const mockedDeleteRecursivelyMainOperation = jest.spyOn(crowi.pageService, 'deleteRecursivelyMainOperation').mockReturnValue(null);
  1350. const deletedPage = await crowi.pageService.deletePage(page, user, options, isRecursively, activityParameters);
  1351. const argsForDeleteRecursivelyMainOperation = mockedDeleteRecursivelyMainOperation.mock.calls[0];
  1352. mockedDeleteRecursivelyMainOperation.mockRestore();
  1353. if (isRecursively) {
  1354. await crowi.pageService.deleteRecursivelyMainOperation(...argsForDeleteRecursivelyMainOperation);
  1355. }
  1356. return deletedPage;
  1357. };
  1358. describe('Delete single page with grant RESTRICTED', () => {
  1359. test('should be able to delete', async() => {
  1360. const _pathT = '/npdel1_awl';
  1361. const _pageT = await Page.findOne({ path: _pathT, grant: Page.GRANT_RESTRICTED });
  1362. expect(_pageT).toBeTruthy();
  1363. const isRecursively = false;
  1364. await deletePage(_pageT, dummyUser1, {}, isRecursively, {
  1365. ip: '::ffff:127.0.0.1',
  1366. endpoint: '/_api/v3/pages/rename',
  1367. });
  1368. const pageT = await Page.findOne({ path: `/trash${_pathT}` });
  1369. const pageN = await Page.findOne({ path: _pathT }); // should not exist
  1370. expect(pageT).toBeTruthy();
  1371. expect(pageN).toBeNull();
  1372. expect(pageT.grant).toBe(Page.GRANT_RESTRICTED);
  1373. expect(pageT.status).toBe(Page.STATUS_DELETED);
  1374. });
  1375. });
  1376. describe('Delete single page with grant USER_GROUP', () => {
  1377. test('should be able to delete', async() => {
  1378. const _path = '/npdel2_ug';
  1379. const _page1 = await Page.findOne({ path: _path, grantedGroups: { $elemMatch: { item: groupIdA } } });
  1380. expect(_page1).toBeTruthy();
  1381. const isRecursively = false;
  1382. await deletePage(_page1, npDummyUser1, {}, isRecursively, {
  1383. ip: '::ffff:127.0.0.1',
  1384. endpoint: '/_api/v3/pages/rename',
  1385. });
  1386. const pageN = await Page.findOne({ path: _path, grantedGroups: { $elemMatch: { item: groupIdA } } });
  1387. const page1 = await Page.findOne({ path: `/trash${_path}`, grantedGroups: { $elemMatch: { item: groupIdA } } });
  1388. expect(pageN).toBeNull();
  1389. expect(page1).toBeTruthy();
  1390. expect(page1.status).toBe(Page.STATUS_DELETED);
  1391. expect(page1.descendantCount).toBe(0);
  1392. expect(page1.parent).toBeNull();
  1393. });
  1394. });
  1395. describe('Delete multiple pages with grant USER_GROUP', () => {
  1396. test('should be able to delete all descendants except page with GRANT_RESTRICTED', async() => {
  1397. const _pathT = '/npdel3_top';
  1398. const _path1 = '/npdel3_top/npdel4_ug';
  1399. const _path2 = '/npdel3_top/npdel4_ug/npdel5_ug';
  1400. const _pageT = await Page.findOne({ path: _pathT, grant: Page.GRANT_USER_GROUP, grantedGroups: { $elemMatch: { item: groupIdA } } }); // A
  1401. const _page1 = await Page.findOne({ path: _path1, grant: Page.GRANT_USER_GROUP, grantedGroups: { $elemMatch: { item: groupIdB } } }); // B
  1402. const _page2 = await Page.findOne({ path: _path2, grant: Page.GRANT_USER_GROUP, grantedGroups: { $elemMatch: { item: groupIdC } } }); // C
  1403. const _pageR = await Page.findOne({ path: _path1, grant: Page.GRANT_RESTRICTED }); // Restricted
  1404. expect(_pageT).toBeTruthy();
  1405. expect(_page1).toBeTruthy();
  1406. expect(_page2).toBeTruthy();
  1407. expect(_pageR).toBeTruthy();
  1408. const isRecursively = true;
  1409. await deletePage(_pageT, npDummyUser1, {}, isRecursively, {
  1410. ip: '::ffff:127.0.0.1',
  1411. endpoint: '/_api/v3/pages/rename',
  1412. });
  1413. const pageTNotExist = await Page.findOne({ path: _pathT, grant: Page.GRANT_USER_GROUP, grantedGroups: { $elemMatch: { item: groupIdA } } }); // A should not exist
  1414. const page1NotExist = await Page.findOne({ path: _path1, grant: Page.GRANT_USER_GROUP, grantedGroups: { $elemMatch: { item: groupIdB } } }); // B should not exist
  1415. const page2NotExist = await Page.findOne({ path: _path2, grant: Page.GRANT_USER_GROUP, grantedGroups: { $elemMatch: { item: groupIdC } } }); // C should not exist
  1416. const pageT = await Page.findOne({ path: `/trash${_pathT}`, grant: Page.GRANT_USER_GROUP, grantedGroups: { $elemMatch: { item: groupIdA } } }); // A
  1417. const page1 = await Page.findOne({ path: `/trash${_path1}`, grant: Page.GRANT_USER_GROUP, grantedGroups: { $elemMatch: { item: groupIdB } } }); // B
  1418. const page2 = await Page.findOne({ path: `/trash${_path2}`, grant: Page.GRANT_USER_GROUP, grantedGroups: { $elemMatch: { item: groupIdC } } }); // C
  1419. const pageR = await Page.findOne({ path: _path1, grant: Page.GRANT_RESTRICTED }); // Restricted
  1420. expect(page1NotExist).toBeNull();
  1421. expect(pageTNotExist).toBeNull();
  1422. expect(page2NotExist).toBeNull();
  1423. expect(pageT).toBeTruthy();
  1424. expect(page1).toBeTruthy();
  1425. expect(page2).toBeTruthy();
  1426. expect(pageR).toBeTruthy();
  1427. expect(pageT.status).toBe(Page.STATUS_DELETED);
  1428. expect(pageT.status).toBe(Page.STATUS_DELETED);
  1429. expect(page1.status).toBe(Page.STATUS_DELETED);
  1430. expect(page1.descendantCount).toBe(0);
  1431. expect(page2.descendantCount).toBe(0);
  1432. expect(page2.descendantCount).toBe(0);
  1433. expect(pageT.parent).toBeNull();
  1434. expect(page1.parent).toBeNull();
  1435. expect(page2.parent).toBeNull();
  1436. });
  1437. });
  1438. });
  1439. describe('Delete completely', () => {
  1440. const deleteCompletely = async(page, user, options = {}, isRecursively = false, preventEmitting = false, activityParameters?) => {
  1441. const mockedDeleteCompletelyRecursivelyMainOperation = jest.spyOn(crowi.pageService, 'deleteCompletelyRecursivelyMainOperation').mockReturnValue(null);
  1442. await crowi.pageService.deleteCompletely(page, user, options, isRecursively, preventEmitting, activityParameters);
  1443. const argsForDeleteCompletelyRecursivelyMainOperation = mockedDeleteCompletelyRecursivelyMainOperation.mock.calls[0];
  1444. mockedDeleteCompletelyRecursivelyMainOperation.mockRestore();
  1445. if (isRecursively) {
  1446. await crowi.pageService.deleteCompletelyRecursivelyMainOperation(...argsForDeleteCompletelyRecursivelyMainOperation);
  1447. }
  1448. return;
  1449. };
  1450. describe('Delete single page with grant RESTRICTED', () => {
  1451. test('should be able to delete completely', async() => {
  1452. const _path = '/npdc1_awl';
  1453. const _page = await Page.findOne({ path: _path, grant: Page.GRANT_RESTRICTED });
  1454. expect(_page).toBeTruthy();
  1455. await deleteCompletely(_page, dummyUser1, {}, false, false, {
  1456. ip: '::ffff:127.0.0.1',
  1457. endpoint: '/_api/v3/pages/rename',
  1458. });
  1459. const page = await Page.findOne({ path: _path, grant: Page.GRANT_RESTRICTED });
  1460. expect(page).toBeNull();
  1461. });
  1462. });
  1463. describe('Delete single page with grant USER_GROUP', () => {
  1464. test('should be able to delete completely', async() => {
  1465. const _path = '/npdc2_ug';
  1466. const _page = await Page.findOne({ path: _path, grant: Page.GRANT_USER_GROUP, grantedGroups: { $elemMatch: { item: groupIdA } } });
  1467. expect(_page).toBeTruthy();
  1468. await deleteCompletely(_page, npDummyUser1, {}, false, false, {
  1469. ip: '::ffff:127.0.0.1',
  1470. endpoint: '/_api/v3/pages/rename',
  1471. });
  1472. const page = await Page.findOne({ path: _path, grant: Page.GRANT_USER_GROUP, grantedGroups: { $elemMatch: { item: groupIdA } } });
  1473. expect(page).toBeNull();
  1474. });
  1475. });
  1476. describe('Delete multiple pages with grant USER_GROUP', () => {
  1477. test('should be able to delete all descendants completely except page with GRANT_RESTRICTED', async() => {
  1478. const _path1 = '/npdc3_ug';
  1479. const _path2 = '/npdc3_ug/npdc4_ug';
  1480. const _path3 = '/npdc3_ug/npdc4_ug/npdc5_ug';
  1481. const _page1 = await Page.findOne({ path: _path1, grant: Page.GRANT_USER_GROUP, grantedGroups: { $elemMatch: { item: groupIdA } } });
  1482. const _page2 = await Page.findOne({ path: _path2, grant: Page.GRANT_USER_GROUP, grantedGroups: { $elemMatch: { item: groupIdB } } });
  1483. const _page3 = await Page.findOne({ path: _path3, grant: Page.GRANT_USER_GROUP, grantedGroups: { $elemMatch: { item: groupIdC } } });
  1484. const _page4 = await Page.findOne({ path: _path2, grant: Page.GRANT_RESTRICTED });
  1485. expect(_page1).toBeTruthy();
  1486. expect(_page2).toBeTruthy();
  1487. expect(_page3).toBeTruthy();
  1488. expect(_page4).toBeTruthy();
  1489. await deleteCompletely(_page1, npDummyUser1, {}, true, false, {
  1490. ip: '::ffff:127.0.0.1',
  1491. endpoint: '/_api/v3/pages/rename',
  1492. });
  1493. const page1 = await Page.findOne({ path: _path1, grant: Page.GRANT_USER_GROUP, grantedGroups: { $elemMatch: { item: groupIdA } } });
  1494. const page2 = await Page.findOne({ path: _path2, grant: Page.GRANT_USER_GROUP, grantedGroups: { $elemMatch: { item: groupIdB } } });
  1495. const page3 = await Page.findOne({ path: _path3, grant: Page.GRANT_USER_GROUP, grantedGroups: { $elemMatch: { item: groupIdC } } });
  1496. const page4 = await Page.findOne({ path: _path2, grant: Page.GRANT_RESTRICTED });
  1497. expect(page1).toBeNull();
  1498. expect(page2).toBeNull();
  1499. expect(page3).toBeNull();
  1500. expect(page4).toBeTruthy();
  1501. });
  1502. });
  1503. });
  1504. describe('revert', () => {
  1505. const revertDeletedPage = async(page, user, options = {}, isRecursively = false, activityParameters?) => {
  1506. // mock return value
  1507. const mockedRevertRecursivelyMainOperation = jest.spyOn(crowi.pageService, 'revertRecursivelyMainOperation').mockReturnValue(null);
  1508. const revertedPage = await crowi.pageService.revertDeletedPage(page, user, options, isRecursively, activityParameters);
  1509. const argsForRecursivelyMainOperation = mockedRevertRecursivelyMainOperation.mock.calls[0];
  1510. // restores the original implementation
  1511. mockedRevertRecursivelyMainOperation.mockRestore();
  1512. if (isRecursively) {
  1513. await crowi.pageService.revertRecursivelyMainOperation(...argsForRecursivelyMainOperation);
  1514. }
  1515. return revertedPage;
  1516. };
  1517. test('should revert single deleted page with GRANT_RESTRICTED', async() => {
  1518. const trashedPage = await Page.findOne({ path: '/trash/np_revert1', status: Page.STATUS_DELETED, grant: Page.GRANT_RESTRICTED });
  1519. const revision = await Revision.findOne({ pageId: trashedPage._id });
  1520. const tag = await Tag.findOne({ name: 'np_revertTag1' });
  1521. const deletedPageTagRelation = await PageTagRelation.findOne({ relatedPage: trashedPage._id, relatedTag: tag?._id, isPageTrashed: true });
  1522. expect(trashedPage).toBeTruthy();
  1523. expect(revision).toBeTruthy();
  1524. expect(tag).toBeTruthy();
  1525. expect(deletedPageTagRelation).toBeTruthy();
  1526. await revertDeletedPage(trashedPage, dummyUser1, {}, false, {
  1527. ip: '::ffff:127.0.0.1',
  1528. endpoint: '/_api/v3/pages/rename',
  1529. });
  1530. const revertedPage = await Page.findOne({ path: '/np_revert1' });
  1531. const deltedPageBeforeRevert = await Page.findOne({ path: '/trash/np_revert1' });
  1532. const pageTagRelation = await PageTagRelation.findOne<IPageTagRelation>({ relatedPage: revertedPage._id, relatedTag: tag?._id });
  1533. expect(revertedPage).toBeTruthy();
  1534. expect(pageTagRelation).toBeTruthy();
  1535. expect(deltedPageBeforeRevert).toBeNull();
  1536. // page with GRANT_RESTRICTED does not have parent
  1537. expect(revertedPage.parent).toBeNull();
  1538. expect(revertedPage.status).toBe(Page.STATUS_PUBLISHED);
  1539. expect(revertedPage.grant).toBe(Page.GRANT_RESTRICTED);
  1540. expect(pageTagRelation?.isPageTrashed).toBe(false);
  1541. });
  1542. test('should revert single deleted page with GRANT_USER_GROUP', async() => {
  1543. const beforeRevertPath = '/trash/np_revert2';
  1544. const user1 = await User.findOne({ name: 'npUser1' });
  1545. const trashedPage = await Page.findOne({ path: beforeRevertPath, status: Page.STATUS_DELETED, grant: Page.GRANT_USER_GROUP });
  1546. const revision = await Revision.findOne({ pageId: trashedPage._id });
  1547. const tag = await Tag.findOne({ name: 'np_revertTag2' });
  1548. const deletedPageTagRelation = await PageTagRelation.findOne({ relatedPage: trashedPage._id, relatedTag: tag?._id, isPageTrashed: true });
  1549. expect(trashedPage).toBeTruthy();
  1550. expect(revision).toBeTruthy();
  1551. expect(tag).toBeTruthy();
  1552. expect(deletedPageTagRelation).toBeTruthy();
  1553. await revertDeletedPage(trashedPage, user1, {}, false, {
  1554. ip: '::ffff:127.0.0.1',
  1555. endpoint: '/_api/v3/pages/revert',
  1556. });
  1557. const revertedPage = await Page.findOne({ path: '/np_revert2' });
  1558. const trashedPageBR = await Page.findOne({ path: beforeRevertPath });
  1559. const pageTagRelation = await PageTagRelation.findOne<IPageTagRelation>({ relatedPage: revertedPage._id, relatedTag: tag?._id });
  1560. expect(revertedPage).toBeTruthy();
  1561. expect(pageTagRelation).toBeTruthy();
  1562. expect(trashedPageBR).toBeNull();
  1563. expect(revertedPage.parent).toStrictEqual(rootPage._id);
  1564. expect(revertedPage.status).toBe(Page.STATUS_PUBLISHED);
  1565. expect(revertedPage.grant).toBe(Page.GRANT_USER_GROUP);
  1566. expect(normalizeGrantedGroups(revertedPage.grantedGroups)).toStrictEqual([
  1567. { item: groupIdA, type: GroupType.userGroup },
  1568. { item: externalGroupIdA, type: GroupType.externalUserGroup },
  1569. ]);
  1570. expect(pageTagRelation?.isPageTrashed).toBe(false);
  1571. });
  1572. test(`revert multiple pages: only target page should be reverted.
  1573. Non-existant middle page and leaf page with GRANT_RESTRICTED shoud not be reverted`, async() => {
  1574. const beforeRevertPath1 = '/trash/np_revert3';
  1575. const beforeRevertPath2 = '/trash/np_revert3/middle/np_revert4';
  1576. const trashedPage1 = await Page.findOne({ path: beforeRevertPath1, status: Page.STATUS_DELETED, grant: Page.GRANT_PUBLIC });
  1577. const trashedPage2 = await Page.findOne({ path: beforeRevertPath2, status: Page.STATUS_DELETED, grant: Page.GRANT_RESTRICTED });
  1578. const revision1 = await Revision.findOne({ pageId: trashedPage1._id });
  1579. const revision2 = await Revision.findOne({ pageId: trashedPage2._id });
  1580. expect(trashedPage1).toBeTruthy();
  1581. expect(trashedPage2).toBeTruthy();
  1582. expect(revision1).toBeTruthy();
  1583. expect(revision2).toBeTruthy();
  1584. await revertDeletedPage(trashedPage1, npDummyUser2, {}, true, {
  1585. ip: '::ffff:127.0.0.1',
  1586. endpoint: '/_api/v3/pages/revert',
  1587. });
  1588. const revertedPage = await Page.findOne({ path: '/np_revert3' });
  1589. const middlePage = await Page.findOne({ path: '/np_revert3/middle' });
  1590. const notRestrictedPage = await Page.findOne({ path: '/np_revert3/middle/np_revert4' });
  1591. // AR => After Revert
  1592. const trashedPage1AR = await Page.findOne({ path: beforeRevertPath1 });
  1593. const trashedPage2AR = await Page.findOne({ path: beforeRevertPath2 });
  1594. const revision1AR = await Revision.findOne({ pageId: revertedPage._id });
  1595. const revision2AR = await Revision.findOne({ pageId: trashedPage2AR._id });
  1596. expect(revertedPage).toBeTruthy();
  1597. expect(trashedPage2AR).toBeTruthy();
  1598. expect(revision1AR).toBeTruthy();
  1599. expect(revision2AR).toBeTruthy();
  1600. expect(trashedPage1AR).toBeNull();
  1601. expect(notRestrictedPage).toBeNull();
  1602. expect(middlePage).toBeNull();
  1603. expect(revertedPage.parent).toStrictEqual(rootPage._id);
  1604. expect(revertedPage.status).toBe(Page.STATUS_PUBLISHED);
  1605. expect(revertedPage.grant).toBe(Page.GRANT_PUBLIC);
  1606. });
  1607. test('revert multiple pages: target page, initially non-existant page and leaf page with GRANT_USER_GROUP shoud be reverted', async() => {
  1608. const user = await User.findOne({ _id: npDummyUser3 });
  1609. const beforeRevertPath1 = '/trash/np_revert5';
  1610. const beforeRevertPath2 = '/trash/np_revert5/middle/np_revert6';
  1611. const beforeRevertPath3 = '/trash/np_revert5/middle';
  1612. const trashedPage1 = await Page.findOne({ path: beforeRevertPath1, status: Page.STATUS_DELETED, grantedGroups: { $elemMatch: { item: groupIdA } } });
  1613. const trashedPage2 = await Page.findOne({ path: beforeRevertPath2, status: Page.STATUS_DELETED, grantedGroups: { $elemMatch: { item: groupIdB } } });
  1614. const nonExistantPage3 = await Page.findOne({ path: beforeRevertPath3 }); // not exist
  1615. const revision1 = await Revision.findOne({ pageId: trashedPage1._id });
  1616. const revision2 = await Revision.findOne({ pageId: trashedPage2._id });
  1617. expect(trashedPage1).toBeTruthy();
  1618. expect(trashedPage2).toBeTruthy();
  1619. expect(revision1).toBeTruthy();
  1620. expect(revision2).toBeTruthy();
  1621. expect(user).toBeTruthy();
  1622. expect(nonExistantPage3).toBeNull();
  1623. await revertDeletedPage(trashedPage1, user, {}, true, {
  1624. ip: '::ffff:127.0.0.1',
  1625. endpoint: '/_api/v3/pages/revert',
  1626. });
  1627. const revertedPage1 = await Page.findOne({ path: '/np_revert5' });
  1628. const newlyCreatedPage = await Page.findOne({ path: '/np_revert5/middle' });
  1629. const revertedPage2 = await Page.findOne({ path: '/np_revert5/middle/np_revert6' });
  1630. // // AR => After Revert
  1631. const trashedPage1AR = await Page.findOne({ path: beforeRevertPath1 });
  1632. const trashedPage2AR = await Page.findOne({ path: beforeRevertPath2 });
  1633. expect(revertedPage1).toBeTruthy();
  1634. expect(newlyCreatedPage).toBeTruthy();
  1635. expect(revertedPage2).toBeTruthy();
  1636. expect(trashedPage1AR).toBeNull();
  1637. expect(trashedPage2AR).toBeNull();
  1638. expect(newlyCreatedPage.isEmpty).toBe(true);
  1639. expect(revertedPage1.parent).toStrictEqual(rootPage._id);
  1640. expect(revertedPage2.parent).toStrictEqual(newlyCreatedPage._id);
  1641. expect(newlyCreatedPage.parent).toStrictEqual(revertedPage1._id);
  1642. expect(revertedPage1.status).toBe(Page.STATUS_PUBLISHED);
  1643. expect(revertedPage2.status).toBe(Page.STATUS_PUBLISHED);
  1644. expect(newlyCreatedPage.status).toBe(Page.STATUS_PUBLISHED);
  1645. expect(normalizeGrantedGroups(revertedPage1.grantedGroups)).toStrictEqual([
  1646. { item: groupIdA, type: GroupType.userGroup },
  1647. { item: externalGroupIdA, type: GroupType.externalUserGroup },
  1648. ]);
  1649. expect(normalizeGrantedGroups(revertedPage2.grantedGroups)).toStrictEqual([
  1650. { item: groupIdB, type: GroupType.userGroup },
  1651. { item: externalGroupIdB, type: GroupType.externalUserGroup },
  1652. ]);
  1653. expect(newlyCreatedPage.grant).toBe(Page.GRANT_PUBLIC);
  1654. });
  1655. });
  1656. });