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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378
  1. import {
  2. GroupType,
  3. type IGrantedGroup,
  4. type IPage,
  5. type IRevision,
  6. } from '@growi/core';
  7. import mongoose from 'mongoose';
  8. import { ExternalGroupProviderType } from '../../../src/features/external-user-group/interfaces/external-user-group';
  9. import ExternalUserGroup from '../../../src/features/external-user-group/server/models/external-user-group';
  10. import ExternalUserGroupRelation from '../../../src/features/external-user-group/server/models/external-user-group-relation';
  11. import type { IPageTagRelation } from '../../../src/interfaces/page-tag-relation';
  12. import type Crowi from '../../../src/server/crowi';
  13. import type { PageDocument, PageModel } from '../../../src/server/models/page';
  14. import PageTagRelation from '../../../src/server/models/page-tag-relation';
  15. import type {
  16. IRevisionDocument,
  17. IRevisionModel,
  18. } from '../../../src/server/models/revision';
  19. import Tag from '../../../src/server/models/tag';
  20. import UserGroup from '../../../src/server/models/user-group';
  21. import UserGroupRelation from '../../../src/server/models/user-group-relation';
  22. import { generalXssFilter } from '../../../src/services/general-xss-filter';
  23. import { getInstance } from '../setup-crowi';
  24. describe('PageService page operations with non-public pages', () => {
  25. // biome-ignore lint/suspicious/noImplicitAnyLet: ignore
  26. let dummyUser1;
  27. // biome-ignore lint/suspicious/noImplicitAnyLet: ignore
  28. let dummyUser2;
  29. // biome-ignore lint/suspicious/noImplicitAnyLet: ignore
  30. let npDummyUser1;
  31. // biome-ignore lint/suspicious/noImplicitAnyLet: ignore
  32. let npDummyUser2;
  33. // biome-ignore lint/suspicious/noImplicitAnyLet: ignore
  34. let npDummyUser3;
  35. let groupIdIsolate: mongoose.Types.ObjectId;
  36. let groupIdA: mongoose.Types.ObjectId;
  37. let groupIdB: mongoose.Types.ObjectId;
  38. let groupIdC: mongoose.Types.ObjectId;
  39. let externalGroupIdIsolate: mongoose.Types.ObjectId;
  40. let externalGroupIdA: mongoose.Types.ObjectId;
  41. let externalGroupIdB: mongoose.Types.ObjectId;
  42. let externalGroupIdC: mongoose.Types.ObjectId;
  43. let crowi: Crowi;
  44. let Page: PageModel;
  45. let Revision: IRevisionModel;
  46. // biome-ignore lint/suspicious/noImplicitAnyLet: ignore
  47. let User;
  48. let generalXssFilterProcessSpy: jest.SpyInstance;
  49. let rootPage: PageDocument;
  50. /**
  51. * Rename
  52. */
  53. const pageIdRename1 = new mongoose.Types.ObjectId();
  54. const pageIdRename2 = new mongoose.Types.ObjectId();
  55. const pageIdRename3 = new mongoose.Types.ObjectId();
  56. const pageIdRename4 = new mongoose.Types.ObjectId();
  57. const pageIdRename5 = new mongoose.Types.ObjectId();
  58. const pageIdRename6 = new mongoose.Types.ObjectId();
  59. const pageIdRename7 = new mongoose.Types.ObjectId();
  60. const pageIdRename8 = new mongoose.Types.ObjectId();
  61. const pageIdRename9 = new mongoose.Types.ObjectId();
  62. /**
  63. * Duplicate
  64. */
  65. // page id
  66. const pageIdDuplicate1 = new mongoose.Types.ObjectId();
  67. const pageIdDuplicate2 = new mongoose.Types.ObjectId();
  68. const pageIdDuplicate3 = new mongoose.Types.ObjectId();
  69. const pageIdDuplicate4 = new mongoose.Types.ObjectId();
  70. const pageIdDuplicate5 = new mongoose.Types.ObjectId();
  71. const pageIdDuplicate6 = new mongoose.Types.ObjectId();
  72. const pageIdDuplicate7 = new mongoose.Types.ObjectId();
  73. const pageIdDuplicate8 = new mongoose.Types.ObjectId();
  74. const pageIdDuplicate9 = new mongoose.Types.ObjectId();
  75. // revision id
  76. const revisionIdDuplicate1 = new mongoose.Types.ObjectId();
  77. const revisionIdDuplicate2 = new mongoose.Types.ObjectId();
  78. const revisionIdDuplicate3 = new mongoose.Types.ObjectId();
  79. const revisionIdDuplicate4 = new mongoose.Types.ObjectId();
  80. const revisionIdDuplicate5 = new mongoose.Types.ObjectId();
  81. const revisionIdDuplicate6 = new mongoose.Types.ObjectId();
  82. const revisionIdDuplicate7 = new mongoose.Types.ObjectId();
  83. const revisionIdDuplicate8 = new mongoose.Types.ObjectId();
  84. const revisionIdDuplicate9 = new mongoose.Types.ObjectId();
  85. /**
  86. * Revert
  87. */
  88. // page id
  89. const pageIdRevert1 = new mongoose.Types.ObjectId();
  90. const pageIdRevert2 = new mongoose.Types.ObjectId();
  91. const pageIdRevert3 = new mongoose.Types.ObjectId();
  92. const pageIdRevert4 = new mongoose.Types.ObjectId();
  93. const pageIdRevert5 = new mongoose.Types.ObjectId();
  94. const pageIdRevert6 = new mongoose.Types.ObjectId();
  95. // revision id
  96. const revisionIdRevert1 = new mongoose.Types.ObjectId();
  97. const revisionIdRevert2 = new mongoose.Types.ObjectId();
  98. const revisionIdRevert3 = new mongoose.Types.ObjectId();
  99. const revisionIdRevert4 = new mongoose.Types.ObjectId();
  100. const revisionIdRevert5 = new mongoose.Types.ObjectId();
  101. const revisionIdRevert6 = new mongoose.Types.ObjectId();
  102. // tag id
  103. const tagIdRevert1 = new mongoose.Types.ObjectId();
  104. const tagIdRevert2 = new mongoose.Types.ObjectId();
  105. const create = async (path, body, user, options = {}) => {
  106. const mockedCreateSubOperation = jest
  107. .spyOn(crowi.pageService, 'createSubOperation')
  108. .mockReturnValue(null);
  109. const createdPage = await crowi.pageService.create(
  110. path,
  111. body,
  112. user,
  113. options,
  114. );
  115. const argsForCreateSubOperation = mockedCreateSubOperation.mock.calls[0];
  116. mockedCreateSubOperation.mockRestore();
  117. await crowi.pageService.createSubOperation(
  118. ...(argsForCreateSubOperation as Parameters<
  119. typeof crowi.pageService.createSubOperation
  120. >),
  121. );
  122. return createdPage;
  123. };
  124. // normalize for result comparison
  125. const normalizeGrantedGroups = (
  126. grantedGroups: IGrantedGroup[] | undefined,
  127. ) => {
  128. return grantedGroups?.map((group) => {
  129. const itemId =
  130. typeof group.item === 'string' ? group.item : group.item._id;
  131. return { item: itemId, type: group.type };
  132. });
  133. };
  134. beforeAll(async () => {
  135. crowi = await getInstance();
  136. await crowi.configManager.updateConfig('app:isV5Compatible', true);
  137. User = mongoose.model('User');
  138. Page = mongoose.model<IPage, PageModel>('Page');
  139. Revision = mongoose.model<IRevision, IRevisionModel>('Revision');
  140. /*
  141. * Common
  142. */
  143. const npUserId1 = new mongoose.Types.ObjectId();
  144. const npUserId2 = new mongoose.Types.ObjectId();
  145. const npUserId3 = new mongoose.Types.ObjectId();
  146. await User.insertMany([
  147. {
  148. _id: npUserId1,
  149. name: 'npUser1',
  150. username: 'npUser1',
  151. email: 'npUser1@example.com',
  152. },
  153. {
  154. _id: npUserId2,
  155. name: 'npUser2',
  156. username: 'npUser2',
  157. email: 'npUser2@example.com',
  158. },
  159. {
  160. _id: npUserId3,
  161. name: 'npUser3',
  162. username: 'npUser3',
  163. email: 'npUser3@example.com',
  164. },
  165. ]);
  166. groupIdIsolate = new mongoose.Types.ObjectId();
  167. groupIdA = new mongoose.Types.ObjectId();
  168. groupIdB = new mongoose.Types.ObjectId();
  169. groupIdC = new mongoose.Types.ObjectId();
  170. await UserGroup.insertMany([
  171. {
  172. _id: groupIdIsolate,
  173. name: 'np_groupIsolate',
  174. },
  175. {
  176. _id: groupIdA,
  177. name: 'np_groupA',
  178. },
  179. {
  180. _id: groupIdB,
  181. name: 'np_groupB',
  182. parent: groupIdA,
  183. },
  184. {
  185. _id: groupIdC,
  186. name: 'np_groupC',
  187. parent: groupIdB,
  188. },
  189. ]);
  190. await UserGroupRelation.insertMany([
  191. {
  192. relatedGroup: groupIdIsolate,
  193. relatedUser: npUserId1,
  194. createdAt: new Date(),
  195. },
  196. {
  197. relatedGroup: groupIdIsolate,
  198. relatedUser: npUserId2,
  199. createdAt: new Date(),
  200. },
  201. {
  202. relatedGroup: groupIdA,
  203. relatedUser: npUserId1,
  204. createdAt: new Date(),
  205. },
  206. {
  207. relatedGroup: groupIdA,
  208. relatedUser: npUserId2,
  209. createdAt: new Date(),
  210. },
  211. {
  212. relatedGroup: groupIdA,
  213. relatedUser: npUserId3,
  214. createdAt: new Date(),
  215. },
  216. {
  217. relatedGroup: groupIdB,
  218. relatedUser: npUserId2,
  219. createdAt: new Date(),
  220. },
  221. {
  222. relatedGroup: groupIdB,
  223. relatedUser: npUserId3,
  224. createdAt: new Date(),
  225. },
  226. {
  227. relatedGroup: groupIdC,
  228. relatedUser: npUserId3,
  229. createdAt: new Date(),
  230. },
  231. ]);
  232. // Insert ExternalUserGroups with the same group structure as UserGroups
  233. // Use to test
  234. // - ExternalUserGroup
  235. // - Case of multiple grantedGroups for Page
  236. externalGroupIdIsolate = new mongoose.Types.ObjectId();
  237. externalGroupIdA = new mongoose.Types.ObjectId();
  238. externalGroupIdB = new mongoose.Types.ObjectId();
  239. externalGroupIdC = new mongoose.Types.ObjectId();
  240. await ExternalUserGroup.insertMany([
  241. {
  242. _id: externalGroupIdIsolate,
  243. name: 'np_externalGroupIsolate',
  244. externalId: 'np_externalGroupIsolate',
  245. provider: ExternalGroupProviderType.ldap,
  246. },
  247. {
  248. _id: externalGroupIdA,
  249. name: 'np_externalGroupA',
  250. externalId: 'np_externalGroupA',
  251. provider: ExternalGroupProviderType.ldap,
  252. },
  253. {
  254. _id: externalGroupIdB,
  255. name: 'np_externalGroupB',
  256. externalId: 'np_externalGroupB',
  257. parent: externalGroupIdA,
  258. provider: ExternalGroupProviderType.ldap,
  259. },
  260. {
  261. _id: externalGroupIdC,
  262. name: 'np_externalGroupC',
  263. externalId: 'np_externalGroupC',
  264. parent: externalGroupIdB,
  265. provider: ExternalGroupProviderType.ldap,
  266. },
  267. ]);
  268. await ExternalUserGroupRelation.insertMany([
  269. {
  270. relatedGroup: externalGroupIdIsolate,
  271. relatedUser: npUserId1,
  272. createdAt: new Date(),
  273. },
  274. {
  275. relatedGroup: externalGroupIdIsolate,
  276. relatedUser: npUserId2,
  277. createdAt: new Date(),
  278. },
  279. {
  280. relatedGroup: externalGroupIdA,
  281. relatedUser: npUserId1,
  282. createdAt: new Date(),
  283. },
  284. {
  285. relatedGroup: externalGroupIdA,
  286. relatedUser: npUserId2,
  287. createdAt: new Date(),
  288. },
  289. {
  290. relatedGroup: externalGroupIdA,
  291. relatedUser: npUserId3,
  292. createdAt: new Date(),
  293. },
  294. {
  295. relatedGroup: externalGroupIdB,
  296. relatedUser: npUserId2,
  297. createdAt: new Date(),
  298. },
  299. {
  300. relatedGroup: externalGroupIdB,
  301. relatedUser: npUserId3,
  302. createdAt: new Date(),
  303. },
  304. {
  305. relatedGroup: externalGroupIdC,
  306. relatedUser: npUserId3,
  307. createdAt: new Date(),
  308. },
  309. ]);
  310. generalXssFilterProcessSpy = jest.spyOn(generalXssFilter, 'process');
  311. dummyUser1 = await User.findOne({ username: 'v5DummyUser1' });
  312. dummyUser2 = await User.findOne({ username: 'v5DummyUser2' });
  313. npDummyUser1 = await User.findOne({ username: 'npUser1' });
  314. npDummyUser2 = await User.findOne({ username: 'npUser2' });
  315. npDummyUser3 = await User.findOne({ username: 'npUser3' });
  316. rootPage = (await Page.findOne({ path: '/' }))!;
  317. if (rootPage == null) {
  318. const pages = await Page.insertMany([
  319. { path: '/', grant: Page.GRANT_PUBLIC },
  320. ]);
  321. rootPage = pages[0];
  322. }
  323. /**
  324. * create
  325. * mc_ => model create
  326. * emp => empty => page with isEmpty: true
  327. * pub => public => GRANT_PUBLIC
  328. */
  329. const pageIdCreate1 = new mongoose.Types.ObjectId();
  330. const pageIdCreate2 = new mongoose.Types.ObjectId();
  331. const pageIdCreate3 = new mongoose.Types.ObjectId();
  332. await Page.insertMany([
  333. {
  334. _id: pageIdCreate1,
  335. path: '/mc4_top/mc1_emp',
  336. grant: Page.GRANT_PUBLIC,
  337. creator: dummyUser1,
  338. lastUpdateUser: dummyUser1._id,
  339. parent: rootPage._id,
  340. isEmpty: true,
  341. },
  342. {
  343. path: '/mc4_top/mc1_emp/mc2_pub',
  344. grant: Page.GRANT_PUBLIC,
  345. creator: dummyUser1,
  346. lastUpdateUser: dummyUser1._id,
  347. parent: pageIdCreate1,
  348. isEmpty: false,
  349. },
  350. {
  351. path: '/mc5_top/mc3_awl',
  352. grant: Page.GRANT_RESTRICTED,
  353. creator: dummyUser1,
  354. lastUpdateUser: dummyUser1._id,
  355. isEmpty: false,
  356. },
  357. {
  358. _id: pageIdCreate2,
  359. path: '/mc4_top',
  360. grant: Page.GRANT_PUBLIC,
  361. creator: dummyUser1,
  362. lastUpdateUser: dummyUser1._id,
  363. isEmpty: false,
  364. parent: rootPage._id,
  365. descendantCount: 1,
  366. },
  367. {
  368. _id: pageIdCreate3,
  369. path: '/mc5_top',
  370. grant: Page.GRANT_PUBLIC,
  371. creator: dummyUser1,
  372. lastUpdateUser: dummyUser1._id,
  373. isEmpty: false,
  374. parent: rootPage._id,
  375. descendantCount: 0,
  376. },
  377. {
  378. path: '/mc6_top',
  379. grant: Page.GRANT_USER_GROUP,
  380. creator: dummyUser1,
  381. lastUpdateUser: dummyUser1._id,
  382. isEmpty: false,
  383. parent: rootPage._id,
  384. descendantCount: 0,
  385. grantedGroups: [
  386. { item: groupIdIsolate, type: GroupType.userGroup },
  387. { item: groupIdB, type: GroupType.userGroup },
  388. ],
  389. },
  390. ]);
  391. /**
  392. * create
  393. * mc_ => model create
  394. * emp => empty => page with isEmpty: true
  395. * pub => public => GRANT_PUBLIC
  396. */
  397. const pageIdCreateBySystem1 = new mongoose.Types.ObjectId();
  398. const pageIdCreateBySystem2 = new mongoose.Types.ObjectId();
  399. const pageIdCreateBySystem3 = new mongoose.Types.ObjectId();
  400. await Page.insertMany([
  401. {
  402. _id: pageIdCreateBySystem1,
  403. path: '/mc4_top_by_system/mc1_emp_by_system',
  404. grant: Page.GRANT_PUBLIC,
  405. creator: dummyUser1,
  406. lastUpdateUser: dummyUser1._id,
  407. parent: rootPage._id,
  408. isEmpty: true,
  409. },
  410. {
  411. path: '/mc4_top_by_system/mc1_emp_by_system/mc2_pub_by_system',
  412. grant: Page.GRANT_PUBLIC,
  413. creator: dummyUser1,
  414. lastUpdateUser: dummyUser1._id,
  415. parent: pageIdCreateBySystem1,
  416. isEmpty: false,
  417. },
  418. {
  419. path: '/mc5_top_by_system/mc3_awl_by_system',
  420. grant: Page.GRANT_RESTRICTED,
  421. creator: dummyUser1,
  422. lastUpdateUser: dummyUser1._id,
  423. isEmpty: false,
  424. },
  425. {
  426. _id: pageIdCreateBySystem2,
  427. path: '/mc4_top_by_system',
  428. grant: Page.GRANT_PUBLIC,
  429. creator: dummyUser1,
  430. lastUpdateUser: dummyUser1._id,
  431. isEmpty: false,
  432. parent: rootPage._id,
  433. descendantCount: 1,
  434. },
  435. {
  436. _id: pageIdCreateBySystem3,
  437. path: '/mc5_top_by_system',
  438. grant: Page.GRANT_PUBLIC,
  439. creator: dummyUser1,
  440. lastUpdateUser: dummyUser1._id,
  441. isEmpty: false,
  442. parent: rootPage._id,
  443. descendantCount: 0,
  444. },
  445. ]);
  446. /*
  447. * Rename
  448. */
  449. await Page.insertMany([
  450. {
  451. _id: pageIdRename1,
  452. path: '/np_rename1_destination',
  453. grant: Page.GRANT_PUBLIC,
  454. creator: dummyUser1._id,
  455. lastUpdateUser: dummyUser1._id,
  456. parent: rootPage._id,
  457. },
  458. {
  459. _id: pageIdRename2,
  460. path: '/np_rename2',
  461. grant: Page.GRANT_USER_GROUP,
  462. grantedGroups: [
  463. { item: groupIdB, type: GroupType.userGroup },
  464. { item: externalGroupIdB, type: GroupType.externalUserGroup },
  465. ],
  466. creator: npDummyUser2._id,
  467. lastUpdateUser: npDummyUser2._id,
  468. parent: rootPage._id,
  469. },
  470. {
  471. _id: pageIdRename3,
  472. path: '/np_rename2/np_rename3',
  473. grant: Page.GRANT_USER_GROUP,
  474. grantedGroups: [
  475. { item: groupIdC, type: GroupType.userGroup },
  476. { item: externalGroupIdC, type: GroupType.externalUserGroup },
  477. ],
  478. creator: npDummyUser3._id,
  479. lastUpdateUser: npDummyUser3._id,
  480. parent: pageIdRename2._id,
  481. },
  482. {
  483. _id: pageIdRename4,
  484. path: '/np_rename4_destination',
  485. grant: Page.GRANT_USER_GROUP,
  486. grantedGroups: [
  487. { item: groupIdIsolate, type: GroupType.userGroup },
  488. { item: externalGroupIdIsolate, type: GroupType.externalUserGroup },
  489. ],
  490. creator: npDummyUser3._id,
  491. lastUpdateUser: npDummyUser3._id,
  492. parent: rootPage._id,
  493. },
  494. {
  495. _id: pageIdRename5,
  496. path: '/np_rename5',
  497. grant: Page.GRANT_USER_GROUP,
  498. grantedGroups: [
  499. { item: groupIdB, type: GroupType.userGroup },
  500. { item: externalGroupIdB, type: GroupType.externalUserGroup },
  501. ],
  502. creator: npDummyUser2._id,
  503. lastUpdateUser: npDummyUser2._id,
  504. parent: rootPage._id,
  505. },
  506. {
  507. _id: pageIdRename6,
  508. path: '/np_rename5/np_rename6',
  509. grant: Page.GRANT_USER_GROUP,
  510. grantedGroups: [
  511. { item: groupIdB, type: GroupType.userGroup },
  512. { item: externalGroupIdB, type: GroupType.externalUserGroup },
  513. ],
  514. creator: npDummyUser2._id,
  515. lastUpdateUser: npDummyUser2._id,
  516. parent: pageIdRename5,
  517. },
  518. {
  519. _id: pageIdRename7,
  520. path: '/np_rename7_destination',
  521. grant: Page.GRANT_USER_GROUP,
  522. grantedGroups: [
  523. { item: groupIdIsolate, type: GroupType.userGroup },
  524. { item: externalGroupIdIsolate, type: GroupType.externalUserGroup },
  525. ],
  526. creator: npDummyUser2._id,
  527. lastUpdateUser: npDummyUser2._id,
  528. parent: pageIdRename5,
  529. },
  530. {
  531. _id: pageIdRename8,
  532. path: '/np_rename8',
  533. grant: Page.GRANT_RESTRICTED,
  534. creator: dummyUser1._id,
  535. lastUpdateUser: dummyUser1._id,
  536. },
  537. {
  538. _id: pageIdRename9,
  539. path: '/np_rename8/np_rename9',
  540. grant: Page.GRANT_RESTRICTED,
  541. creator: dummyUser2._id,
  542. lastUpdateUser: dummyUser2._id,
  543. },
  544. ]);
  545. /*
  546. * Duplicate
  547. */
  548. await Page.insertMany([
  549. {
  550. _id: pageIdDuplicate1,
  551. path: '/np_duplicate1',
  552. grant: Page.GRANT_RESTRICTED,
  553. creator: dummyUser1._id,
  554. lastUpdateUser: dummyUser1._id,
  555. revision: revisionIdDuplicate1,
  556. },
  557. {
  558. _id: pageIdDuplicate2,
  559. path: '/np_duplicate2',
  560. grant: Page.GRANT_USER_GROUP,
  561. grantedGroups: [
  562. { item: groupIdA, type: GroupType.userGroup },
  563. { item: externalGroupIdA, type: GroupType.externalUserGroup },
  564. ],
  565. creator: npDummyUser1._id,
  566. lastUpdateUser: npDummyUser1._id,
  567. revision: revisionIdDuplicate2,
  568. parent: rootPage._id,
  569. },
  570. {
  571. _id: pageIdDuplicate3,
  572. path: '/np_duplicate2/np_duplicate3',
  573. grant: Page.GRANT_USER_GROUP,
  574. grantedGroups: [
  575. { item: groupIdB, type: GroupType.userGroup },
  576. { item: externalGroupIdB, type: GroupType.externalUserGroup },
  577. ],
  578. creator: npDummyUser2._id,
  579. lastUpdateUser: npDummyUser2._id,
  580. revision: revisionIdDuplicate3,
  581. parent: pageIdDuplicate2,
  582. },
  583. {
  584. _id: pageIdDuplicate4,
  585. path: '/np_duplicate4',
  586. grant: Page.GRANT_PUBLIC,
  587. creator: npDummyUser1._id,
  588. lastUpdateUser: npDummyUser1._id,
  589. revision: revisionIdDuplicate4,
  590. parent: rootPage._id,
  591. },
  592. {
  593. _id: pageIdDuplicate5,
  594. path: '/np_duplicate4/np_duplicate5',
  595. grant: Page.GRANT_RESTRICTED,
  596. creator: npDummyUser1._id,
  597. lastUpdateUser: npDummyUser1._id,
  598. revision: revisionIdDuplicate5,
  599. },
  600. {
  601. _id: pageIdDuplicate6,
  602. path: '/np_duplicate4/np_duplicate6',
  603. grant: Page.GRANT_PUBLIC,
  604. creator: npDummyUser1._id,
  605. lastUpdateUser: npDummyUser1._id,
  606. parent: pageIdDuplicate4,
  607. revision: revisionIdDuplicate6,
  608. },
  609. {
  610. _id: pageIdDuplicate7,
  611. path: '/np_duplicate7',
  612. grant: Page.GRANT_USER_GROUP,
  613. creator: npDummyUser1._id,
  614. lastUpdateUser: npDummyUser1._id,
  615. parent: rootPage._id,
  616. revision: revisionIdDuplicate7,
  617. grantedGroups: [
  618. { item: groupIdA, type: GroupType.userGroup },
  619. { item: externalGroupIdA, type: GroupType.externalUserGroup },
  620. { item: groupIdB, type: GroupType.userGroup },
  621. { item: externalGroupIdB, type: GroupType.externalUserGroup },
  622. ],
  623. },
  624. {
  625. _id: pageIdDuplicate8,
  626. path: '/np_duplicate7/np_duplicate8',
  627. grant: Page.GRANT_USER_GROUP,
  628. creator: npDummyUser3._id,
  629. lastUpdateUser: npDummyUser3._id,
  630. parent: pageIdDuplicate7,
  631. revision: revisionIdDuplicate8,
  632. grantedGroups: [
  633. { item: groupIdC, type: GroupType.userGroup },
  634. { item: externalGroupIdC, type: GroupType.externalUserGroup },
  635. ],
  636. },
  637. {
  638. _id: pageIdDuplicate9,
  639. path: '/np_duplicate7/np_duplicate9',
  640. grant: Page.GRANT_OWNER,
  641. creator: npDummyUser2._id,
  642. lastUpdateUser: npDummyUser2._id,
  643. parent: pageIdDuplicate7,
  644. revision: revisionIdDuplicate9,
  645. grantedUsers: [npDummyUser2._id],
  646. },
  647. ]);
  648. await Revision.insertMany([
  649. {
  650. _id: revisionIdDuplicate1,
  651. body: 'np_duplicate1',
  652. format: 'markdown',
  653. pageId: pageIdDuplicate1,
  654. author: npDummyUser1._id,
  655. },
  656. {
  657. _id: revisionIdDuplicate2,
  658. body: 'np_duplicate2',
  659. format: 'markdown',
  660. pageId: pageIdDuplicate2,
  661. author: npDummyUser2._id,
  662. },
  663. {
  664. _id: revisionIdDuplicate3,
  665. body: 'np_duplicate3',
  666. format: 'markdown',
  667. pageId: pageIdDuplicate3,
  668. author: npDummyUser2._id,
  669. },
  670. {
  671. _id: revisionIdDuplicate4,
  672. body: 'np_duplicate4',
  673. format: 'markdown',
  674. pageId: pageIdDuplicate4,
  675. author: npDummyUser2._id,
  676. },
  677. {
  678. _id: revisionIdDuplicate5,
  679. body: 'np_duplicate5',
  680. format: 'markdown',
  681. pageId: pageIdDuplicate5,
  682. author: npDummyUser2._id,
  683. },
  684. {
  685. _id: revisionIdDuplicate6,
  686. body: 'np_duplicate6',
  687. format: 'markdown',
  688. pageId: pageIdDuplicate6,
  689. author: npDummyUser1._id,
  690. },
  691. {
  692. _id: revisionIdDuplicate7,
  693. body: 'np_duplicate7',
  694. format: 'markdown',
  695. pageId: pageIdDuplicate7,
  696. author: npDummyUser1._id,
  697. },
  698. {
  699. _id: revisionIdDuplicate8,
  700. body: 'np_duplicate8',
  701. format: 'markdown',
  702. pageId: pageIdDuplicate8,
  703. author: npDummyUser3._id,
  704. },
  705. {
  706. _id: revisionIdDuplicate9,
  707. body: 'np_duplicate9',
  708. format: 'markdown',
  709. pageId: pageIdDuplicate9,
  710. author: npDummyUser2._id,
  711. },
  712. ]);
  713. /**
  714. * Delete
  715. */
  716. const pageIdDelete1 = new mongoose.Types.ObjectId();
  717. const pageIdDelete2 = new mongoose.Types.ObjectId();
  718. const pageIdDelete3 = new mongoose.Types.ObjectId();
  719. const pageIdDelete4 = new mongoose.Types.ObjectId();
  720. await Page.insertMany([
  721. {
  722. _id: pageIdDelete1,
  723. path: '/npdel1_awl',
  724. grant: Page.GRANT_RESTRICTED,
  725. status: Page.STATUS_PUBLISHED,
  726. isEmpty: false,
  727. },
  728. {
  729. _id: pageIdDelete2,
  730. path: '/npdel2_ug',
  731. grant: Page.GRANT_USER_GROUP,
  732. grantedGroups: [
  733. { item: groupIdA, type: GroupType.userGroup },
  734. { item: externalGroupIdA, type: GroupType.externalUserGroup },
  735. ],
  736. status: Page.STATUS_PUBLISHED,
  737. isEmpty: false,
  738. parent: rootPage._id,
  739. descendantCount: 0,
  740. },
  741. {
  742. _id: pageIdDelete3,
  743. path: '/npdel3_top',
  744. grant: Page.GRANT_USER_GROUP,
  745. grantedGroups: [
  746. { item: groupIdA, type: GroupType.userGroup },
  747. { item: externalGroupIdA, type: GroupType.externalUserGroup },
  748. ],
  749. status: Page.STATUS_PUBLISHED,
  750. isEmpty: false,
  751. parent: rootPage._id,
  752. descendantCount: 2,
  753. },
  754. {
  755. _id: pageIdDelete4,
  756. path: '/npdel3_top/npdel4_ug',
  757. grant: Page.GRANT_USER_GROUP,
  758. grantedGroups: [
  759. { item: groupIdB, type: GroupType.userGroup },
  760. { item: externalGroupIdB, type: GroupType.externalUserGroup },
  761. ],
  762. status: Page.STATUS_PUBLISHED,
  763. isEmpty: false,
  764. parent: pageIdDelete3._id,
  765. descendantCount: 1,
  766. },
  767. {
  768. path: '/npdel3_top/npdel4_ug',
  769. grant: Page.GRANT_RESTRICTED,
  770. status: Page.STATUS_PUBLISHED,
  771. isEmpty: false,
  772. },
  773. {
  774. path: '/npdel3_top/npdel4_ug/npdel5_ug',
  775. grant: Page.GRANT_USER_GROUP,
  776. grantedGroups: [
  777. { item: groupIdC, type: GroupType.userGroup },
  778. { item: externalGroupIdC, type: GroupType.externalUserGroup },
  779. ],
  780. status: Page.STATUS_PUBLISHED,
  781. isEmpty: false,
  782. parent: pageIdDelete4._id,
  783. descendantCount: 0,
  784. },
  785. ]);
  786. /**
  787. * Delete completely
  788. */
  789. const pageIdDeleteComp1 = new mongoose.Types.ObjectId();
  790. const pageIdDeleteComp2 = new mongoose.Types.ObjectId();
  791. await Page.insertMany([
  792. {
  793. path: '/npdc1_awl',
  794. grant: Page.GRANT_RESTRICTED,
  795. status: Page.STATUS_PUBLISHED,
  796. isEmpty: false,
  797. },
  798. {
  799. path: '/npdc2_ug',
  800. grant: Page.GRANT_USER_GROUP,
  801. grantedGroups: [
  802. { item: groupIdA, type: GroupType.userGroup },
  803. { item: externalGroupIdA, type: GroupType.externalUserGroup },
  804. ],
  805. status: Page.STATUS_PUBLISHED,
  806. isEmpty: false,
  807. parent: rootPage._id,
  808. },
  809. {
  810. _id: pageIdDeleteComp1,
  811. path: '/npdc3_ug',
  812. grant: Page.GRANT_USER_GROUP,
  813. grantedGroups: [
  814. { item: groupIdA, type: GroupType.userGroup },
  815. { item: externalGroupIdA, type: GroupType.externalUserGroup },
  816. ],
  817. status: Page.STATUS_PUBLISHED,
  818. isEmpty: false,
  819. parent: rootPage._id,
  820. },
  821. {
  822. _id: pageIdDeleteComp2,
  823. path: '/npdc3_ug/npdc4_ug',
  824. grant: Page.GRANT_USER_GROUP,
  825. grantedGroups: [
  826. { item: groupIdB, type: GroupType.userGroup },
  827. { item: externalGroupIdB, type: GroupType.externalUserGroup },
  828. ],
  829. status: Page.STATUS_PUBLISHED,
  830. isEmpty: false,
  831. parent: pageIdDeleteComp1,
  832. },
  833. {
  834. path: '/npdc3_ug/npdc4_ug/npdc5_ug',
  835. grant: Page.GRANT_USER_GROUP,
  836. grantedGroups: [
  837. { item: groupIdC, type: GroupType.userGroup },
  838. { item: externalGroupIdC, type: GroupType.externalUserGroup },
  839. ],
  840. status: Page.STATUS_PUBLISHED,
  841. isEmpty: false,
  842. parent: pageIdDeleteComp2,
  843. },
  844. {
  845. path: '/npdc3_ug/npdc4_ug',
  846. grant: Page.GRANT_RESTRICTED,
  847. status: Page.STATUS_PUBLISHED,
  848. isEmpty: false,
  849. },
  850. ]);
  851. /**
  852. * Revert
  853. */
  854. await Page.insertMany([
  855. {
  856. _id: pageIdRevert1,
  857. path: '/trash/np_revert1',
  858. grant: Page.GRANT_RESTRICTED,
  859. revision: revisionIdRevert1,
  860. status: Page.STATUS_DELETED,
  861. },
  862. {
  863. _id: pageIdRevert2,
  864. path: '/trash/np_revert2',
  865. grant: Page.GRANT_USER_GROUP,
  866. grantedGroups: [
  867. { item: groupIdA, type: GroupType.userGroup },
  868. { item: externalGroupIdA, type: GroupType.externalUserGroup },
  869. ],
  870. revision: revisionIdRevert2,
  871. status: Page.STATUS_DELETED,
  872. },
  873. {
  874. _id: pageIdRevert3,
  875. path: '/trash/np_revert3',
  876. revision: revisionIdRevert3,
  877. status: Page.STATUS_DELETED,
  878. parent: rootPage._id,
  879. },
  880. {
  881. _id: pageIdRevert4,
  882. path: '/trash/np_revert3/middle/np_revert4',
  883. grant: Page.GRANT_RESTRICTED,
  884. revision: revisionIdRevert4,
  885. status: Page.STATUS_DELETED,
  886. },
  887. {
  888. _id: pageIdRevert5,
  889. path: '/trash/np_revert5',
  890. grant: Page.GRANT_USER_GROUP,
  891. grantedGroups: [
  892. { item: groupIdA, type: GroupType.userGroup },
  893. { item: externalGroupIdA, type: GroupType.externalUserGroup },
  894. ],
  895. revision: revisionIdRevert5,
  896. status: Page.STATUS_DELETED,
  897. },
  898. {
  899. _id: pageIdRevert6,
  900. path: '/trash/np_revert5/middle/np_revert6',
  901. grant: Page.GRANT_USER_GROUP,
  902. grantedGroups: [
  903. { item: groupIdB, type: GroupType.userGroup },
  904. { item: externalGroupIdB, type: GroupType.externalUserGroup },
  905. ],
  906. revision: revisionIdRevert6,
  907. status: Page.STATUS_DELETED,
  908. },
  909. ]);
  910. await Revision.insertMany([
  911. {
  912. _id: revisionIdRevert1,
  913. pageId: pageIdRevert1,
  914. body: 'np_revert1',
  915. format: 'markdown',
  916. author: dummyUser1._id,
  917. },
  918. {
  919. _id: revisionIdRevert2,
  920. pageId: pageIdRevert2,
  921. body: 'np_revert2',
  922. format: 'markdown',
  923. author: npDummyUser1,
  924. },
  925. {
  926. _id: revisionIdRevert3,
  927. pageId: pageIdRevert3,
  928. body: 'np_revert3',
  929. format: 'markdown',
  930. author: npDummyUser1,
  931. },
  932. {
  933. _id: revisionIdRevert4,
  934. pageId: pageIdRevert4,
  935. body: 'np_revert4',
  936. format: 'markdown',
  937. author: npDummyUser1,
  938. },
  939. {
  940. _id: revisionIdRevert5,
  941. pageId: pageIdRevert5,
  942. body: 'np_revert5',
  943. format: 'markdown',
  944. author: npDummyUser1,
  945. },
  946. {
  947. _id: revisionIdRevert6,
  948. pageId: pageIdRevert6,
  949. body: 'np_revert6',
  950. format: 'markdown',
  951. author: npDummyUser1,
  952. },
  953. ]);
  954. await Tag.insertMany([
  955. { _id: tagIdRevert1, name: 'np_revertTag1' },
  956. { _id: tagIdRevert2, name: 'np_revertTag2' },
  957. ]);
  958. await PageTagRelation.insertMany([
  959. {
  960. relatedPage: pageIdRevert1,
  961. relatedTag: tagIdRevert1,
  962. isPageTrashed: true,
  963. },
  964. {
  965. relatedPage: pageIdRevert2,
  966. relatedTag: tagIdRevert2,
  967. isPageTrashed: true,
  968. },
  969. ]);
  970. });
  971. describe('create', () => {
  972. describe('Creating a page using existing path', () => {
  973. test('with grant RESTRICTED should only create the page and change nothing else', async () => {
  974. const isGrantNormalizedSpy = jest.spyOn(
  975. crowi.pageGrantService,
  976. 'isGrantNormalized',
  977. );
  978. const pathT = '/mc4_top';
  979. const path1 = '/mc4_top/mc1_emp';
  980. const path2 = '/mc4_top/mc1_emp/mc2_pub';
  981. const pageT = await Page.findOne({ path: pathT, descendantCount: 1 });
  982. const page1 = await Page.findOne({
  983. path: path1,
  984. grant: Page.GRANT_PUBLIC,
  985. });
  986. const page2 = await Page.findOne({ path: path2 });
  987. const page3 = await Page.findOne({
  988. path: path1,
  989. grant: Page.GRANT_RESTRICTED,
  990. });
  991. expect(pageT).toBeTruthy();
  992. expect(page1).toBeTruthy();
  993. expect(page2).toBeTruthy();
  994. expect(page3).toBeNull();
  995. // use existing path
  996. await create(path1, 'new body', dummyUser1, {
  997. grant: Page.GRANT_RESTRICTED,
  998. });
  999. const _pageT = await Page.findOne({ path: pathT });
  1000. const _page1 = await Page.findOne({
  1001. path: path1,
  1002. grant: Page.GRANT_PUBLIC,
  1003. });
  1004. const _page2 = await Page.findOne({ path: path2 });
  1005. const _page3 = await Page.findOne({
  1006. path: path1,
  1007. grant: Page.GRANT_RESTRICTED,
  1008. });
  1009. expect(_pageT).toBeTruthy();
  1010. expect(_page1).toBeTruthy();
  1011. expect(_page2).toBeTruthy();
  1012. expect(_page3).toBeTruthy();
  1013. expect(_pageT?.descendantCount).toBe(1);
  1014. // isGrantNormalized is not called when GRANT RESTRICTED
  1015. expect(isGrantNormalizedSpy).toBeCalledTimes(0);
  1016. });
  1017. });
  1018. describe('Creating a page under a page with grant RESTRICTED', () => {
  1019. test('will create a new empty page with the same path as the grant RESTRECTED page and become a parent', async () => {
  1020. const isGrantNormalizedSpy = jest.spyOn(
  1021. crowi.pageGrantService,
  1022. 'isGrantNormalized',
  1023. );
  1024. const pathT = '/mc5_top';
  1025. const path1 = '/mc5_top/mc3_awl';
  1026. const pathN = '/mc5_top/mc3_awl/mc4_pub'; // used to create
  1027. const pageT = await Page.findOne({ path: pathT });
  1028. const page1 = await Page.findOne({
  1029. path: path1,
  1030. grant: Page.GRANT_RESTRICTED,
  1031. });
  1032. const page2 = await Page.findOne({
  1033. path: path1,
  1034. grant: Page.GRANT_PUBLIC,
  1035. });
  1036. expect(pageT).toBeTruthy();
  1037. expect(page1).toBeTruthy();
  1038. expect(page2).toBeNull();
  1039. await create(pathN, 'new body', dummyUser1, {
  1040. grant: Page.GRANT_PUBLIC,
  1041. });
  1042. const _pageT = await Page.findOne({ path: pathT });
  1043. const _page1 = await Page.findOne({
  1044. path: path1,
  1045. grant: Page.GRANT_RESTRICTED,
  1046. });
  1047. const _page2 = await Page.findOne({
  1048. path: path1,
  1049. grant: Page.GRANT_PUBLIC,
  1050. isEmpty: true,
  1051. });
  1052. const _pageN = await Page.findOne({
  1053. path: pathN,
  1054. grant: Page.GRANT_PUBLIC,
  1055. }); // newly crated
  1056. expect(_pageT).toBeTruthy();
  1057. expect(_page1).toBeTruthy();
  1058. expect(_page2).toBeTruthy();
  1059. expect(_pageN).toBeTruthy();
  1060. expect(_pageN?.parent).toStrictEqual(_page2?._id);
  1061. expect(_pageT?.descendantCount).toStrictEqual(1);
  1062. // isGrantNormalized is called when GRANT PUBLIC
  1063. expect(isGrantNormalizedSpy).toBeCalledTimes(1);
  1064. });
  1065. });
  1066. describe('Creating a page under a page with grant USER_GROUP', () => {
  1067. describe('When onlyInheritUserRelatedGrantedGroups is true', () => {
  1068. test('Only user related groups should be inherited', async () => {
  1069. const pathT = '/mc6_top';
  1070. const pageT = await Page.findOne({ path: pathT });
  1071. expect(pageT).toBeTruthy();
  1072. const pathN = '/mc6_top/onlyRelatedGroupsInherited'; // path to create
  1073. await create(pathN, 'new body', npDummyUser1, {
  1074. grant: Page.GRANT_USER_GROUP,
  1075. onlyInheritUserRelatedGrantedGroups: true,
  1076. });
  1077. const _pageT = await Page.findOne({ path: pathT });
  1078. const _pageN = await Page.findOne({
  1079. path: pathN,
  1080. grant: Page.GRANT_USER_GROUP,
  1081. }); // newly crated
  1082. expect(_pageT).toBeTruthy();
  1083. expect(_pageN).toBeTruthy();
  1084. expect(_pageN?.parent).toStrictEqual(_pageT?._id);
  1085. expect(_pageT?.descendantCount).toStrictEqual(1);
  1086. expect(normalizeGrantedGroups(_pageN?.grantedGroups)).toStrictEqual([
  1087. { item: groupIdIsolate, type: GroupType.userGroup },
  1088. ]);
  1089. });
  1090. });
  1091. describe('When onlyInheritUserRelatedGrantedGroups is false', () => {
  1092. test('All groups should be inherited', async () => {
  1093. const pathT = '/mc6_top';
  1094. const pageT = await Page.findOne({ path: pathT });
  1095. expect(pageT).toBeTruthy();
  1096. const pathN = '/mc6_top/allGroupsInherited'; // path to create
  1097. await create(pathN, 'new body', npDummyUser1, {
  1098. grant: Page.GRANT_USER_GROUP,
  1099. onlyInheritUserRelatedGrantedGroups: false,
  1100. });
  1101. const _pageT = await Page.findOne({ path: pathT });
  1102. const _pageN = await Page.findOne({
  1103. path: pathN,
  1104. grant: Page.GRANT_USER_GROUP,
  1105. }); // newly crated
  1106. expect(_pageT).toBeTruthy();
  1107. expect(_pageN).toBeTruthy();
  1108. expect(_pageN?.parent).toStrictEqual(_pageT?._id);
  1109. expect(_pageT?.descendantCount).toStrictEqual(2);
  1110. expect(normalizeGrantedGroups(_pageN?.grantedGroups)).toStrictEqual([
  1111. { item: groupIdIsolate, type: GroupType.userGroup },
  1112. { item: groupIdB, type: GroupType.userGroup },
  1113. ]);
  1114. });
  1115. });
  1116. });
  1117. });
  1118. describe('create by system', () => {
  1119. describe('Creating a page using existing path', () => {
  1120. test('with grant RESTRICTED should only create the page and change nothing else', async () => {
  1121. const isGrantNormalizedSpy = jest.spyOn(
  1122. crowi.pageGrantService,
  1123. 'isGrantNormalized',
  1124. );
  1125. const pathT = '/mc4_top_by_system';
  1126. const path1 = '/mc4_top_by_system/mc1_emp_by_system';
  1127. const path2 = '/mc4_top_by_system/mc1_emp_by_system/mc2_pub_by_system';
  1128. const pageT = await Page.findOne({ path: pathT, descendantCount: 1 });
  1129. const page1 = await Page.findOne({
  1130. path: path1,
  1131. grant: Page.GRANT_PUBLIC,
  1132. });
  1133. const page2 = await Page.findOne({ path: path2 });
  1134. const page3 = await Page.findOne({
  1135. path: path1,
  1136. grant: Page.GRANT_RESTRICTED,
  1137. });
  1138. expect(pageT).toBeTruthy();
  1139. expect(page1).toBeTruthy();
  1140. expect(page2).toBeTruthy();
  1141. expect(page3).toBeNull();
  1142. // use existing path
  1143. await crowi.pageService.forceCreateBySystem(path1, 'new body', {
  1144. grant: Page.GRANT_RESTRICTED,
  1145. });
  1146. const _pageT = await Page.findOne({ path: pathT });
  1147. const _page1 = await Page.findOne({
  1148. path: path1,
  1149. grant: Page.GRANT_PUBLIC,
  1150. });
  1151. const _page2 = await Page.findOne({ path: path2 });
  1152. const _page3 = await Page.findOne({
  1153. path: path1,
  1154. grant: Page.GRANT_RESTRICTED,
  1155. });
  1156. expect(_pageT).toBeTruthy();
  1157. expect(_page1).toBeTruthy();
  1158. expect(_page2).toBeTruthy();
  1159. expect(_page3).toBeTruthy();
  1160. expect(_pageT?.descendantCount).toBe(1);
  1161. // isGrantNormalized is not called when create by ststem
  1162. expect(isGrantNormalizedSpy).toBeCalledTimes(0);
  1163. });
  1164. });
  1165. describe('Creating a page under a page with grant RESTRICTED', () => {
  1166. test('will create a new empty page with the same path as the grant RESTRECTED page and become a parent', async () => {
  1167. const isGrantNormalizedSpy = jest.spyOn(
  1168. crowi.pageGrantService,
  1169. 'isGrantNormalized',
  1170. );
  1171. const pathT = '/mc5_top_by_system';
  1172. const path1 = '/mc5_top_by_system/mc3_awl_by_system';
  1173. const pathN = '/mc5_top_by_system/mc3_awl_by_system/mc4_pub_by_system'; // used to create
  1174. const pageT = await Page.findOne({ path: pathT });
  1175. const page1 = await Page.findOne({
  1176. path: path1,
  1177. grant: Page.GRANT_RESTRICTED,
  1178. });
  1179. const page2 = await Page.findOne({
  1180. path: path1,
  1181. grant: Page.GRANT_PUBLIC,
  1182. });
  1183. expect(pageT).toBeTruthy();
  1184. expect(page1).toBeTruthy();
  1185. expect(page2).toBeNull();
  1186. await crowi.pageService.forceCreateBySystem(pathN, 'new body', {
  1187. grant: Page.GRANT_PUBLIC,
  1188. });
  1189. const _pageT = await Page.findOne({ path: pathT });
  1190. const _page1 = await Page.findOne({
  1191. path: path1,
  1192. grant: Page.GRANT_RESTRICTED,
  1193. });
  1194. const _page2 = await Page.findOne({
  1195. path: path1,
  1196. grant: Page.GRANT_PUBLIC,
  1197. isEmpty: true,
  1198. });
  1199. const _pageN = await Page.findOne({
  1200. path: pathN,
  1201. grant: Page.GRANT_PUBLIC,
  1202. }); // newly crated
  1203. expect(_pageT).toBeTruthy();
  1204. expect(_page1).toBeTruthy();
  1205. expect(_page2).toBeTruthy();
  1206. expect(_pageN).toBeTruthy();
  1207. expect(_pageN?.parent).toStrictEqual(_page2?._id);
  1208. expect(_pageT?.descendantCount).toStrictEqual(1);
  1209. // isGrantNormalized is not called when create by ststem
  1210. expect(isGrantNormalizedSpy).toBeCalledTimes(0);
  1211. });
  1212. });
  1213. });
  1214. describe('Rename', () => {
  1215. const renamePage = async (
  1216. page,
  1217. newPagePath,
  1218. user,
  1219. options,
  1220. activityParameters?,
  1221. ) => {
  1222. // mock return value
  1223. const mockedRenameSubOperation = jest
  1224. .spyOn(crowi.pageService, 'renameSubOperation')
  1225. .mockReturnValue(null);
  1226. const renamedPage = await crowi.pageService.renamePage(
  1227. page,
  1228. newPagePath,
  1229. user,
  1230. options,
  1231. activityParameters,
  1232. );
  1233. // retrieve the arguments passed when calling method renameSubOperation inside renamePage method
  1234. const argsForRenameSubOperation = mockedRenameSubOperation.mock.calls[0];
  1235. // restores the original implementation
  1236. mockedRenameSubOperation.mockRestore();
  1237. // rename descendants
  1238. if (page.grant !== Page.GRANT_RESTRICTED) {
  1239. await crowi.pageService.renameSubOperation(
  1240. ...(argsForRenameSubOperation as Parameters<
  1241. typeof crowi.pageService.renameSubOperation
  1242. >),
  1243. );
  1244. }
  1245. return renamedPage;
  1246. };
  1247. test('Should rename/move with descendants with grant normalized pages', async () => {
  1248. const _pathD = '/np_rename1_destination';
  1249. const _path2 = '/np_rename2';
  1250. const _path3 = '/np_rename2/np_rename3';
  1251. const _propertiesD = { grant: Page.GRANT_PUBLIC };
  1252. const _properties2 = {
  1253. grant: Page.GRANT_USER_GROUP,
  1254. grantedGroups: { $elemMatch: { item: groupIdB } },
  1255. };
  1256. const _properties3 = {
  1257. grant: Page.GRANT_USER_GROUP,
  1258. grantedGroups: { $elemMatch: { item: groupIdC } },
  1259. };
  1260. const _pageD = await Page.findOne({ path: _pathD, ..._propertiesD });
  1261. const _page2 = await Page.findOne({ path: _path2, ..._properties2 });
  1262. const _page3 = await Page.findOne({
  1263. path: _path3,
  1264. ..._properties3,
  1265. parent: _page2?._id,
  1266. });
  1267. expect(_pageD).toBeTruthy();
  1268. expect(_page2).toBeTruthy();
  1269. expect(_page3).toBeTruthy();
  1270. const newPathForPage2 = '/np_rename1_destination/np_rename2';
  1271. const newPathForPage3 = '/np_rename1_destination/np_rename2/np_rename3';
  1272. await renamePage(
  1273. _page2,
  1274. newPathForPage2,
  1275. npDummyUser2,
  1276. {},
  1277. {
  1278. ip: '::ffff:127.0.0.1',
  1279. endpoint: '/_api/v3/pages/rename',
  1280. activityId: '62e291bc10e0ab61bd691794',
  1281. },
  1282. );
  1283. const pageD = await Page.findOne({ path: _pathD, ..._propertiesD });
  1284. const page2 = await Page.findOne({ path: _path2, ..._properties2 }); // not exist
  1285. const page3 = await Page.findOne({
  1286. path: _path3,
  1287. ..._properties3,
  1288. parent: _page2?._id,
  1289. }); // not exist
  1290. const page2Renamed = await Page.findOne({ path: newPathForPage2 }); // renamed
  1291. const page3Renamed = await Page.findOne({ path: newPathForPage3 }); // renamed
  1292. expect(pageD).toBeTruthy();
  1293. expect(page2).toBeNull();
  1294. expect(page3).toBeNull();
  1295. expect(page2Renamed).toBeTruthy();
  1296. expect(page3Renamed).toBeTruthy();
  1297. expect(page2Renamed?.parent).toStrictEqual(_pageD?._id);
  1298. expect(page3Renamed?.parent).toStrictEqual(page2Renamed?._id);
  1299. expect(normalizeGrantedGroups(page2Renamed?.grantedGroups)).toStrictEqual(
  1300. normalizeGrantedGroups(_page2?.grantedGroups),
  1301. );
  1302. expect(normalizeGrantedGroups(page3Renamed?.grantedGroups)).toStrictEqual(
  1303. normalizeGrantedGroups(_page3?.grantedGroups),
  1304. );
  1305. expect(generalXssFilterProcessSpy).toHaveBeenCalled();
  1306. });
  1307. test('Should throw with NOT grant normalized pages', async () => {
  1308. const _pathD = '/np_rename4_destination';
  1309. const _path2 = '/np_rename5';
  1310. const _path3 = '/np_rename5/np_rename6';
  1311. const _propertiesD = {
  1312. grant: Page.GRANT_USER_GROUP,
  1313. grantedGroups: { $elemMatch: { item: groupIdIsolate } },
  1314. };
  1315. const _properties2 = {
  1316. grant: Page.GRANT_USER_GROUP,
  1317. grantedGroups: { $elemMatch: { item: groupIdB } },
  1318. };
  1319. const _properties3 = {
  1320. grant: Page.GRANT_USER_GROUP,
  1321. grantedGroups: { $elemMatch: { item: groupIdB } },
  1322. };
  1323. const _pageD = await Page.findOne({ path: _pathD, ..._propertiesD }); // isolate
  1324. const _page2 = await Page.findOne({ path: _path2, ..._properties2 }); // groupIdB
  1325. const _page3 = await Page.findOne({
  1326. path: _path3,
  1327. ..._properties3,
  1328. parent: _page2,
  1329. }); // groupIdB
  1330. expect(_pageD).toBeTruthy();
  1331. expect(_page2).toBeTruthy();
  1332. expect(_page3).toBeTruthy();
  1333. const newPathForPage2 = '/np_rename4_destination/np_rename5';
  1334. const newPathForPage3 = '/np_rename4_destination/np_rename5/np_rename6';
  1335. let isThrown = false;
  1336. try {
  1337. await renamePage(
  1338. _page2,
  1339. newPathForPage2,
  1340. dummyUser1,
  1341. {},
  1342. {
  1343. ip: '::ffff:127.0.0.1',
  1344. endpoint: '/_api/v3/pages/rename',
  1345. activityId: '62e291bc10e0ab61bd691794',
  1346. },
  1347. );
  1348. } catch (err) {
  1349. isThrown = true;
  1350. }
  1351. expect(isThrown).toBe(true);
  1352. const page2 = await Page.findOne({ path: _path2 }); // not renamed thus exist
  1353. const page3 = await Page.findOne({ path: _path3 }); // not renamed thus exist
  1354. const page2Renamed = await Page.findOne({ path: newPathForPage2 }); // not exist
  1355. const page3Renamed = await Page.findOne({ path: newPathForPage3 }); // not exist
  1356. expect(page2).toBeTruthy();
  1357. expect(page3).toBeTruthy();
  1358. expect(page2Renamed).toBeNull();
  1359. expect(page3Renamed).toBeNull();
  1360. });
  1361. test('Should rename/move multiple pages: child page with GRANT_RESTRICTED should NOT be renamed.', async () => {
  1362. const _pathD = '/np_rename7_destination';
  1363. const _path2 = '/np_rename8';
  1364. const _path3 = '/np_rename8/np_rename9';
  1365. const _pageD = await Page.findOne({
  1366. path: _pathD,
  1367. grant: Page.GRANT_USER_GROUP,
  1368. grantedGroups: { $elemMatch: { item: groupIdIsolate } },
  1369. });
  1370. const _page2 = await Page.findOne({
  1371. path: _path2,
  1372. grant: Page.GRANT_RESTRICTED,
  1373. });
  1374. const _page3 = await Page.findOne({
  1375. path: _path3,
  1376. grant: Page.GRANT_RESTRICTED,
  1377. });
  1378. expect(_pageD).toBeTruthy();
  1379. expect(_page2).toBeTruthy();
  1380. expect(_page3).toBeTruthy();
  1381. const newPathForPage2 = '/np_rename7_destination/np_rename8';
  1382. const newpathForPage3 = '/np_rename7_destination/np_rename8/np_rename9';
  1383. await renamePage(
  1384. _page2,
  1385. newPathForPage2,
  1386. npDummyUser1,
  1387. { isRecursively: true },
  1388. {
  1389. ip: '::ffff:127.0.0.1',
  1390. endpoint: '/_api/v3/pages/rename',
  1391. activityId: '62e291bc10e0ab61bd691794',
  1392. },
  1393. );
  1394. const page2 = await Page.findOne({ path: _path2 }); // not exist
  1395. const page3 = await Page.findOne({ path: _path3 }); // not renamed thus exist
  1396. const page2Renamed = await Page.findOne({ path: newPathForPage2 }); // exist
  1397. const page3Renamed = await Page.findOne({ path: newpathForPage3 }); // not exist
  1398. expect(page2).toBeNull();
  1399. expect(page3).toBeTruthy();
  1400. expect(page2Renamed).toBeTruthy();
  1401. expect(page3Renamed).toBeNull();
  1402. expect(page2Renamed?.parent).toBeNull();
  1403. expect(generalXssFilterProcessSpy).toHaveBeenCalled();
  1404. });
  1405. });
  1406. describe('Duplicate', () => {
  1407. const duplicate = async (
  1408. page,
  1409. newPagePath: string,
  1410. user,
  1411. isRecursively: boolean,
  1412. onlyDuplicateUserRelatedResources: boolean,
  1413. ) => {
  1414. // mock return value
  1415. const mockedDuplicateRecursivelyMainOperation = jest
  1416. .spyOn(crowi.pageService, 'duplicateRecursivelyMainOperation')
  1417. .mockReturnValue(null);
  1418. const duplicatedPage = await crowi.pageService.duplicate(
  1419. page,
  1420. newPagePath,
  1421. user,
  1422. isRecursively,
  1423. onlyDuplicateUserRelatedResources,
  1424. );
  1425. // retrieve the arguments passed when calling method duplicateRecursivelyMainOperation inside duplicate method
  1426. const argsForDuplicateRecursivelyMainOperation =
  1427. mockedDuplicateRecursivelyMainOperation.mock.calls[0];
  1428. // restores the original implementation
  1429. mockedDuplicateRecursivelyMainOperation.mockRestore();
  1430. // duplicate descendants
  1431. if (page.grant !== Page.GRANT_RESTRICTED && isRecursively) {
  1432. await crowi.pageService.duplicateRecursivelyMainOperation(
  1433. ...(argsForDuplicateRecursivelyMainOperation as Parameters<
  1434. typeof crowi.pageService.duplicateRecursivelyMainOperation
  1435. >),
  1436. );
  1437. }
  1438. return duplicatedPage;
  1439. };
  1440. test('Duplicate single page with GRANT_RESTRICTED', async () => {
  1441. const _page = await Page.findOne({
  1442. path: '/np_duplicate1',
  1443. grant: Page.GRANT_RESTRICTED,
  1444. }).populate<{ revision: IRevisionDocument }>({
  1445. path: 'revision',
  1446. model: 'Revision',
  1447. });
  1448. const _revision = _page?.revision;
  1449. expect(_page).toBeTruthy();
  1450. expect(_revision).toBeTruthy();
  1451. const newPagePath = '/dup_np_duplicate1';
  1452. await duplicate(_page, newPagePath, npDummyUser1, false, false);
  1453. const duplicatedPage = await Page.findOne({ path: newPagePath });
  1454. const duplicatedRevision = await Revision.findOne({
  1455. pageId: duplicatedPage?._id,
  1456. });
  1457. expect(generalXssFilterProcessSpy).toHaveBeenCalled();
  1458. expect(duplicatedPage).toBeTruthy();
  1459. expect(duplicatedPage?._id).not.toStrictEqual(_page?._id);
  1460. expect(duplicatedPage?.grant).toBe(_page?.grant);
  1461. expect(duplicatedPage?.parent).toBeNull();
  1462. expect(duplicatedPage?.parent).toStrictEqual(_page?.parent);
  1463. expect(duplicatedPage?.revision).toStrictEqual(duplicatedRevision?._id);
  1464. expect(duplicatedRevision?.body).toBe(_revision?.body);
  1465. });
  1466. test('Should duplicate multiple pages with GRANT_USER_GROUP', async () => {
  1467. const _path1 = '/np_duplicate2';
  1468. const _path2 = '/np_duplicate2/np_duplicate3';
  1469. const _page1 = await Page.findOne({
  1470. path: _path1,
  1471. parent: rootPage._id,
  1472. grantedGroups: { $elemMatch: { item: groupIdA } },
  1473. }).populate<{ revision: IRevisionDocument }>({
  1474. path: 'revision',
  1475. model: 'Revision',
  1476. });
  1477. const _page2 = await Page.findOne({
  1478. path: _path2,
  1479. parent: _page1?._id,
  1480. grantedGroups: { $elemMatch: { item: groupIdB } },
  1481. }).populate<{ revision: IRevisionDocument }>({
  1482. path: 'revision',
  1483. model: 'Revision',
  1484. });
  1485. const _revision1 = _page1?.revision;
  1486. const _revision2 = _page2?.revision;
  1487. expect(_page1).toBeTruthy();
  1488. expect(_page2).toBeTruthy();
  1489. expect(_revision1).toBeTruthy();
  1490. expect(_revision2).toBeTruthy();
  1491. const newPagePath = '/dup_np_duplicate2';
  1492. await duplicate(_page1, newPagePath, npDummyUser2, true, false);
  1493. const duplicatedPage1 = await Page.findOne({
  1494. path: newPagePath,
  1495. }).populate<{ revision: IRevisionDocument }>({
  1496. path: 'revision',
  1497. model: 'Revision',
  1498. });
  1499. const duplicatedPage2 = await Page.findOne({
  1500. path: '/dup_np_duplicate2/np_duplicate3',
  1501. }).populate<{ revision: IRevisionDocument }>({
  1502. path: 'revision',
  1503. model: 'Revision',
  1504. });
  1505. const duplicatedRevision1 = duplicatedPage1?.revision;
  1506. const duplicatedRevision2 = duplicatedPage2?.revision;
  1507. expect(generalXssFilterProcessSpy).toHaveBeenCalled();
  1508. expect(duplicatedPage1).toBeTruthy();
  1509. expect(duplicatedPage2).toBeTruthy();
  1510. expect(duplicatedRevision1).toBeTruthy();
  1511. expect(duplicatedRevision2).toBeTruthy();
  1512. expect(
  1513. normalizeGrantedGroups(duplicatedPage1?.grantedGroups),
  1514. ).toStrictEqual([
  1515. { item: groupIdA, type: GroupType.userGroup },
  1516. { item: externalGroupIdA, type: GroupType.externalUserGroup },
  1517. ]);
  1518. expect(
  1519. normalizeGrantedGroups(duplicatedPage2?.grantedGroups),
  1520. ).toStrictEqual([
  1521. { item: groupIdB, type: GroupType.userGroup },
  1522. { item: externalGroupIdB, type: GroupType.externalUserGroup },
  1523. ]);
  1524. expect(duplicatedPage1?.parent).toStrictEqual(_page1?.parent);
  1525. expect(duplicatedPage2?.parent).toStrictEqual(duplicatedPage1?._id);
  1526. expect(duplicatedRevision1?.body).toBe(_revision1?.body);
  1527. expect(duplicatedRevision2?.body).toBe(_revision2?.body);
  1528. expect(duplicatedRevision1?.pageId).toStrictEqual(duplicatedPage1?._id);
  1529. expect(duplicatedRevision2?.pageId).toStrictEqual(duplicatedPage2?._id);
  1530. });
  1531. test('Should duplicate multiple pages. Page with GRANT_RESTRICTED should NOT be duplicated', async () => {
  1532. const _path1 = '/np_duplicate4';
  1533. const _path2 = '/np_duplicate4/np_duplicate5';
  1534. const _path3 = '/np_duplicate4/np_duplicate6';
  1535. const _page1 = await Page.findOne({
  1536. path: _path1,
  1537. parent: rootPage._id,
  1538. grant: Page.GRANT_PUBLIC,
  1539. }).populate<{ revision: IRevisionDocument }>({
  1540. path: 'revision',
  1541. model: 'Revision',
  1542. });
  1543. const _page2 = await Page.findOne({
  1544. path: _path2,
  1545. grant: Page.GRANT_RESTRICTED,
  1546. }).populate<{ revision: IRevisionDocument }>({
  1547. path: 'revision',
  1548. model: 'Revision',
  1549. });
  1550. const _page3 = await Page.findOne({
  1551. path: _path3,
  1552. grant: Page.GRANT_PUBLIC,
  1553. }).populate<{ revision: IRevisionDocument }>({
  1554. path: 'revision',
  1555. model: 'Revision',
  1556. });
  1557. const baseRevision1 = _page1?.revision;
  1558. const baseRevision2 = _page2?.revision;
  1559. const baseRevision3 = _page3?.revision;
  1560. expect(_page1).toBeTruthy();
  1561. expect(_page2).toBeTruthy();
  1562. expect(_page3).toBeTruthy();
  1563. expect(baseRevision1).toBeTruthy();
  1564. expect(baseRevision2).toBeTruthy();
  1565. const newPagePath = '/dup_np_duplicate4';
  1566. await duplicate(_page1, newPagePath, npDummyUser1, true, false);
  1567. const duplicatedPage1 = await Page.findOne({
  1568. path: newPagePath,
  1569. }).populate<{ revision: IRevisionDocument }>({
  1570. path: 'revision',
  1571. model: 'Revision',
  1572. });
  1573. const duplicatedPage2 = await Page.findOne({
  1574. path: '/dup_np_duplicate4/np_duplicate5',
  1575. }).populate<{ revision: IRevisionDocument }>({
  1576. path: 'revision',
  1577. model: 'Revision',
  1578. });
  1579. const duplicatedPage3 = await Page.findOne({
  1580. path: '/dup_np_duplicate4/np_duplicate6',
  1581. }).populate<{ revision: IRevisionDocument }>({
  1582. path: 'revision',
  1583. model: 'Revision',
  1584. });
  1585. const duplicatedRevision1 = duplicatedPage1?.revision;
  1586. const duplicatedRevision3 = duplicatedPage3?.revision;
  1587. expect(generalXssFilterProcessSpy).toHaveBeenCalled();
  1588. expect(duplicatedPage1).toBeTruthy();
  1589. expect(duplicatedPage2).toBeNull();
  1590. expect(duplicatedPage3).toBeTruthy();
  1591. expect(duplicatedRevision1).toBeTruthy();
  1592. expect(duplicatedRevision3).toBeTruthy();
  1593. expect(duplicatedPage1?.grant).toStrictEqual(Page.GRANT_PUBLIC);
  1594. expect(duplicatedPage3?.grant).toStrictEqual(Page.GRANT_PUBLIC);
  1595. expect(duplicatedPage1?.parent).toStrictEqual(_page1?.parent);
  1596. expect(duplicatedPage3?.parent).toStrictEqual(duplicatedPage1?._id);
  1597. expect(duplicatedRevision1?.body).toBe(baseRevision1?.body);
  1598. expect(duplicatedRevision3?.body).toBe(baseRevision3?.body);
  1599. expect(duplicatedRevision1?.pageId).toStrictEqual(duplicatedPage1?._id);
  1600. expect(duplicatedRevision3?.pageId).toStrictEqual(duplicatedPage3?._id);
  1601. });
  1602. test('Should duplicate only user related pages and granted groups when onlyDuplicateUserRelatedResources is true', async () => {
  1603. const _path1 = '/np_duplicate7';
  1604. const _path2 = '/np_duplicate7/np_duplicate8';
  1605. const _path3 = '/np_duplicate7/np_duplicate9';
  1606. const _page1 = await Page.findOne({
  1607. path: _path1,
  1608. parent: rootPage._id,
  1609. }).populate<{ revision: IRevisionDocument }>({
  1610. path: 'revision',
  1611. model: 'Revision',
  1612. });
  1613. const _page2 = await Page.findOne({ path: _path2, parent: _page1?._id });
  1614. const _page3 = await Page.findOne({ path: _path3, parent: _page1?._id });
  1615. const _revision1 = _page1?.revision;
  1616. expect(_page1).toBeTruthy();
  1617. expect(_page2).toBeTruthy();
  1618. expect(_page3).toBeTruthy();
  1619. expect(_revision1).toBeTruthy();
  1620. const newPagePath = '/dup_np_duplicate7';
  1621. await duplicate(_page1, newPagePath, npDummyUser1, true, true);
  1622. const duplicatedPage1 = await Page.findOne({
  1623. path: newPagePath,
  1624. }).populate<{ revision: IRevisionDocument }>({
  1625. path: 'revision',
  1626. model: 'Revision',
  1627. });
  1628. const duplicatedPage2 = await Page.findOne({
  1629. path: '/dup_np_duplicate7/np_duplicate8',
  1630. }).populate<{ revision: IRevisionDocument }>({
  1631. path: 'revision',
  1632. model: 'Revision',
  1633. });
  1634. const duplicatedPage3 = await Page.findOne({
  1635. path: '/dup_np_duplicate7/np_duplicate9',
  1636. }).populate<{ revision: IRevisionDocument }>({
  1637. path: 'revision',
  1638. model: 'Revision',
  1639. });
  1640. const duplicatedRevision1 = duplicatedPage1?.revision;
  1641. expect(generalXssFilterProcessSpy).toHaveBeenCalled();
  1642. expect(duplicatedPage1).toBeTruthy();
  1643. expect(duplicatedPage2).toBeFalsy();
  1644. expect(duplicatedPage3).toBeFalsy();
  1645. expect(duplicatedRevision1).toBeTruthy();
  1646. expect(
  1647. normalizeGrantedGroups(duplicatedPage1?.grantedGroups),
  1648. ).toStrictEqual([
  1649. { item: groupIdA, type: GroupType.userGroup },
  1650. { item: externalGroupIdA, type: GroupType.externalUserGroup },
  1651. ]);
  1652. expect(duplicatedPage1?.parent).toStrictEqual(_page1?.parent);
  1653. expect(duplicatedRevision1?.body).toBe(_revision1?.body);
  1654. expect(duplicatedRevision1?.pageId).toStrictEqual(duplicatedPage1?._id);
  1655. });
  1656. test('Should duplicate all pages and granted groups when onlyDuplicateUserRelatedResources is false', async () => {
  1657. const _path1 = '/np_duplicate7';
  1658. const _path2 = '/np_duplicate7/np_duplicate8';
  1659. const _path3 = '/np_duplicate7/np_duplicate9';
  1660. const _page1 = await Page.findOne({
  1661. path: _path1,
  1662. parent: rootPage._id,
  1663. }).populate<{ revision: IRevisionDocument }>({
  1664. path: 'revision',
  1665. model: 'Revision',
  1666. });
  1667. const _page2 = await Page.findOne({
  1668. path: _path2,
  1669. parent: _page1?._id,
  1670. }).populate<{ revision: IRevisionDocument }>({
  1671. path: 'revision',
  1672. model: 'Revision',
  1673. });
  1674. const _page3 = await Page.findOne({
  1675. path: _path3,
  1676. parent: _page1?._id,
  1677. }).populate<{ revision: IRevisionDocument }>({
  1678. path: 'revision',
  1679. model: 'Revision',
  1680. });
  1681. const _revision1 = _page1?.revision;
  1682. const _revision2 = _page2?.revision;
  1683. const _revision3 = _page3?.revision;
  1684. expect(_page1).toBeTruthy();
  1685. expect(_page2).toBeTruthy();
  1686. expect(_page3).toBeTruthy();
  1687. expect(_revision1).toBeTruthy();
  1688. expect(_revision2).toBeTruthy();
  1689. expect(_revision3).toBeTruthy();
  1690. const newPagePath = '/dup2_np_duplicate7';
  1691. await duplicate(_page1, newPagePath, npDummyUser1, true, false);
  1692. const duplicatedPage1 = await Page.findOne({
  1693. path: newPagePath,
  1694. }).populate<{ revision: IRevisionDocument }>({
  1695. path: 'revision',
  1696. model: 'Revision',
  1697. });
  1698. const duplicatedPage2 = await Page.findOne({
  1699. path: '/dup2_np_duplicate7/np_duplicate8',
  1700. }).populate<{ revision: IRevisionDocument }>({
  1701. path: 'revision',
  1702. model: 'Revision',
  1703. });
  1704. const duplicatedPage3 = await Page.findOne({
  1705. path: '/dup2_np_duplicate7/np_duplicate9',
  1706. }).populate<{ revision: IRevisionDocument }>({
  1707. path: 'revision',
  1708. model: 'Revision',
  1709. });
  1710. const duplicatedRevision1 = duplicatedPage1?.revision;
  1711. const duplicatedRevision2 = duplicatedPage2?.revision;
  1712. const duplicatedRevision3 = duplicatedPage3?.revision;
  1713. expect(generalXssFilterProcessSpy).toHaveBeenCalled();
  1714. expect(duplicatedPage1).toBeTruthy();
  1715. expect(duplicatedPage2).toBeTruthy();
  1716. expect(duplicatedPage3).toBeTruthy();
  1717. expect(duplicatedRevision1).toBeTruthy();
  1718. expect(duplicatedRevision2).toBeTruthy();
  1719. expect(duplicatedRevision3).toBeTruthy();
  1720. expect(
  1721. normalizeGrantedGroups(duplicatedPage1?.grantedGroups),
  1722. ).toStrictEqual([
  1723. { item: groupIdA, type: GroupType.userGroup },
  1724. { item: externalGroupIdA, type: GroupType.externalUserGroup },
  1725. { item: groupIdB, type: GroupType.userGroup },
  1726. { item: externalGroupIdB, type: GroupType.externalUserGroup },
  1727. ]);
  1728. expect(duplicatedPage1?.parent).toStrictEqual(_page1?.parent);
  1729. expect(duplicatedRevision1?.body).toBe(_revision1?.body);
  1730. expect(duplicatedRevision1?.pageId).toStrictEqual(duplicatedPage1?._id);
  1731. expect(
  1732. normalizeGrantedGroups(duplicatedPage2?.grantedGroups),
  1733. ).toStrictEqual([
  1734. { item: groupIdC, type: GroupType.userGroup },
  1735. { item: externalGroupIdC, type: GroupType.externalUserGroup },
  1736. ]);
  1737. expect(duplicatedPage2?.parent).toStrictEqual(duplicatedPage1?._id);
  1738. expect(duplicatedRevision2?.body).toBe(_revision2?.body);
  1739. expect(duplicatedRevision2?.pageId).toStrictEqual(duplicatedPage2?._id);
  1740. expect(duplicatedPage3?.grantedUsers).toStrictEqual([npDummyUser2?._id]);
  1741. expect(duplicatedPage3?.parent).toStrictEqual(duplicatedPage1?._id);
  1742. expect(duplicatedRevision3?.body).toBe(_revision3?.body);
  1743. expect(duplicatedRevision3?.pageId).toStrictEqual(duplicatedPage3?._id);
  1744. });
  1745. });
  1746. describe('Delete', () => {
  1747. const deletePage = async (
  1748. page,
  1749. user,
  1750. options,
  1751. isRecursively,
  1752. activityParameters?,
  1753. ) => {
  1754. const mockedDeleteRecursivelyMainOperation = jest
  1755. .spyOn(crowi.pageService, 'deleteRecursivelyMainOperation')
  1756. .mockReturnValue(null);
  1757. const deletedPage = await crowi.pageService.deletePage(
  1758. page,
  1759. user,
  1760. options,
  1761. isRecursively,
  1762. activityParameters,
  1763. );
  1764. const argsForDeleteRecursivelyMainOperation =
  1765. mockedDeleteRecursivelyMainOperation.mock.calls[0];
  1766. mockedDeleteRecursivelyMainOperation.mockRestore();
  1767. if (isRecursively) {
  1768. await crowi.pageService.deleteRecursivelyMainOperation(
  1769. ...(argsForDeleteRecursivelyMainOperation as Parameters<
  1770. typeof crowi.pageService.deleteRecursivelyMainOperation
  1771. >),
  1772. );
  1773. }
  1774. return deletedPage;
  1775. };
  1776. describe('Delete single page with grant RESTRICTED', () => {
  1777. test('should be able to delete', async () => {
  1778. const _pathT = '/npdel1_awl';
  1779. const _pageT = await Page.findOne({
  1780. path: _pathT,
  1781. grant: Page.GRANT_RESTRICTED,
  1782. });
  1783. expect(_pageT).toBeTruthy();
  1784. const isRecursively = false;
  1785. await deletePage(_pageT, dummyUser1, {}, isRecursively, {
  1786. ip: '::ffff:127.0.0.1',
  1787. endpoint: '/_api/v3/pages/rename',
  1788. });
  1789. const pageT = await Page.findOne({ path: `/trash${_pathT}` });
  1790. const pageN = await Page.findOne({ path: _pathT }); // should not exist
  1791. expect(pageT).toBeTruthy();
  1792. expect(pageN).toBeNull();
  1793. expect(pageT?.grant).toBe(Page.GRANT_RESTRICTED);
  1794. expect(pageT?.status).toBe(Page.STATUS_DELETED);
  1795. });
  1796. });
  1797. describe('Delete single page with grant USER_GROUP', () => {
  1798. test('should be able to delete', async () => {
  1799. const _path = '/npdel2_ug';
  1800. const _page1 = await Page.findOne({
  1801. path: _path,
  1802. grantedGroups: { $elemMatch: { item: groupIdA } },
  1803. });
  1804. expect(_page1).toBeTruthy();
  1805. const isRecursively = false;
  1806. await deletePage(_page1, npDummyUser1, {}, isRecursively, {
  1807. ip: '::ffff:127.0.0.1',
  1808. endpoint: '/_api/v3/pages/rename',
  1809. });
  1810. const pageN = await Page.findOne({
  1811. path: _path,
  1812. grantedGroups: { $elemMatch: { item: groupIdA } },
  1813. });
  1814. const page1 = await Page.findOne({
  1815. path: `/trash${_path}`,
  1816. grantedGroups: { $elemMatch: { item: groupIdA } },
  1817. });
  1818. expect(pageN).toBeNull();
  1819. expect(page1).toBeTruthy();
  1820. expect(page1?.status).toBe(Page.STATUS_DELETED);
  1821. expect(page1?.descendantCount).toBe(0);
  1822. expect(page1?.parent).toBeNull();
  1823. });
  1824. });
  1825. describe('Delete multiple pages with grant USER_GROUP', () => {
  1826. test('should be able to delete all descendants except page with GRANT_RESTRICTED', async () => {
  1827. const _pathT = '/npdel3_top';
  1828. const _path1 = '/npdel3_top/npdel4_ug';
  1829. const _path2 = '/npdel3_top/npdel4_ug/npdel5_ug';
  1830. const _pageT = await Page.findOne({
  1831. path: _pathT,
  1832. grant: Page.GRANT_USER_GROUP,
  1833. grantedGroups: { $elemMatch: { item: groupIdA } },
  1834. }); // A
  1835. const _page1 = await Page.findOne({
  1836. path: _path1,
  1837. grant: Page.GRANT_USER_GROUP,
  1838. grantedGroups: { $elemMatch: { item: groupIdB } },
  1839. }); // B
  1840. const _page2 = await Page.findOne({
  1841. path: _path2,
  1842. grant: Page.GRANT_USER_GROUP,
  1843. grantedGroups: { $elemMatch: { item: groupIdC } },
  1844. }); // C
  1845. const _pageR = await Page.findOne({
  1846. path: _path1,
  1847. grant: Page.GRANT_RESTRICTED,
  1848. }); // Restricted
  1849. expect(_pageT).toBeTruthy();
  1850. expect(_page1).toBeTruthy();
  1851. expect(_page2).toBeTruthy();
  1852. expect(_pageR).toBeTruthy();
  1853. const isRecursively = true;
  1854. await deletePage(_pageT, npDummyUser1, {}, isRecursively, {
  1855. ip: '::ffff:127.0.0.1',
  1856. endpoint: '/_api/v3/pages/rename',
  1857. });
  1858. const pageTNotExist = await Page.findOne({
  1859. path: _pathT,
  1860. grant: Page.GRANT_USER_GROUP,
  1861. grantedGroups: { $elemMatch: { item: groupIdA } },
  1862. }); // A should not exist
  1863. const page1NotExist = await Page.findOne({
  1864. path: _path1,
  1865. grant: Page.GRANT_USER_GROUP,
  1866. grantedGroups: { $elemMatch: { item: groupIdB } },
  1867. }); // B should not exist
  1868. const page2NotExist = await Page.findOne({
  1869. path: _path2,
  1870. grant: Page.GRANT_USER_GROUP,
  1871. grantedGroups: { $elemMatch: { item: groupIdC } },
  1872. }); // C should not exist
  1873. const pageT = await Page.findOne({
  1874. path: `/trash${_pathT}`,
  1875. grant: Page.GRANT_USER_GROUP,
  1876. grantedGroups: { $elemMatch: { item: groupIdA } },
  1877. }); // A
  1878. const page1 = await Page.findOne({
  1879. path: `/trash${_path1}`,
  1880. grant: Page.GRANT_USER_GROUP,
  1881. grantedGroups: { $elemMatch: { item: groupIdB } },
  1882. }); // B
  1883. const page2 = await Page.findOne({
  1884. path: `/trash${_path2}`,
  1885. grant: Page.GRANT_USER_GROUP,
  1886. grantedGroups: { $elemMatch: { item: groupIdC } },
  1887. }); // C
  1888. const pageR = await Page.findOne({
  1889. path: _path1,
  1890. grant: Page.GRANT_RESTRICTED,
  1891. }); // Restricted
  1892. expect(page1NotExist).toBeNull();
  1893. expect(pageTNotExist).toBeNull();
  1894. expect(page2NotExist).toBeNull();
  1895. expect(pageT).toBeTruthy();
  1896. expect(page1).toBeTruthy();
  1897. expect(page2).toBeTruthy();
  1898. expect(pageR).toBeTruthy();
  1899. expect(pageT?.status).toBe(Page.STATUS_DELETED);
  1900. expect(pageT?.status).toBe(Page.STATUS_DELETED);
  1901. expect(page1?.status).toBe(Page.STATUS_DELETED);
  1902. expect(page1?.descendantCount).toBe(0);
  1903. expect(page2?.descendantCount).toBe(0);
  1904. expect(page2?.descendantCount).toBe(0);
  1905. expect(pageT?.parent).toBeNull();
  1906. expect(page1?.parent).toBeNull();
  1907. expect(page2?.parent).toBeNull();
  1908. });
  1909. });
  1910. });
  1911. describe('Delete completely', () => {
  1912. const deleteCompletely = async (
  1913. page,
  1914. user,
  1915. options = {},
  1916. isRecursively = false,
  1917. preventEmitting = false,
  1918. activityParameters?,
  1919. ) => {
  1920. const mockedDeleteCompletelyRecursivelyMainOperation = jest
  1921. .spyOn(crowi.pageService, 'deleteCompletelyRecursivelyMainOperation')
  1922. .mockReturnValue(null);
  1923. await crowi.pageService.deleteCompletely(
  1924. page,
  1925. user,
  1926. options,
  1927. isRecursively,
  1928. preventEmitting,
  1929. activityParameters,
  1930. );
  1931. const argsForDeleteCompletelyRecursivelyMainOperation =
  1932. mockedDeleteCompletelyRecursivelyMainOperation.mock.calls[0];
  1933. mockedDeleteCompletelyRecursivelyMainOperation.mockRestore();
  1934. if (isRecursively) {
  1935. await crowi.pageService.deleteCompletelyRecursivelyMainOperation(
  1936. ...(argsForDeleteCompletelyRecursivelyMainOperation as Parameters<
  1937. typeof crowi.pageService.deleteCompletelyRecursivelyMainOperation
  1938. >),
  1939. );
  1940. }
  1941. return;
  1942. };
  1943. describe('Delete single page with grant RESTRICTED', () => {
  1944. test('should be able to delete completely', async () => {
  1945. const _path = '/npdc1_awl';
  1946. const _page = await Page.findOne({
  1947. path: _path,
  1948. grant: Page.GRANT_RESTRICTED,
  1949. });
  1950. expect(_page).toBeTruthy();
  1951. await deleteCompletely(_page, dummyUser1, {}, false, false, {
  1952. ip: '::ffff:127.0.0.1',
  1953. endpoint: '/_api/v3/pages/rename',
  1954. });
  1955. const page = await Page.findOne({
  1956. path: _path,
  1957. grant: Page.GRANT_RESTRICTED,
  1958. });
  1959. expect(page).toBeNull();
  1960. });
  1961. });
  1962. describe('Delete single page with grant USER_GROUP', () => {
  1963. test('should be able to delete completely', async () => {
  1964. const _path = '/npdc2_ug';
  1965. const _page = await Page.findOne({
  1966. path: _path,
  1967. grant: Page.GRANT_USER_GROUP,
  1968. grantedGroups: { $elemMatch: { item: groupIdA } },
  1969. });
  1970. expect(_page).toBeTruthy();
  1971. await deleteCompletely(_page, npDummyUser1, {}, false, false, {
  1972. ip: '::ffff:127.0.0.1',
  1973. endpoint: '/_api/v3/pages/rename',
  1974. });
  1975. const page = await Page.findOne({
  1976. path: _path,
  1977. grant: Page.GRANT_USER_GROUP,
  1978. grantedGroups: { $elemMatch: { item: groupIdA } },
  1979. });
  1980. expect(page).toBeNull();
  1981. });
  1982. });
  1983. describe('Delete multiple pages with grant USER_GROUP', () => {
  1984. test('should be able to delete all descendants completely except page with GRANT_RESTRICTED', async () => {
  1985. const _path1 = '/npdc3_ug';
  1986. const _path2 = '/npdc3_ug/npdc4_ug';
  1987. const _path3 = '/npdc3_ug/npdc4_ug/npdc5_ug';
  1988. const _page1 = await Page.findOne({
  1989. path: _path1,
  1990. grant: Page.GRANT_USER_GROUP,
  1991. grantedGroups: { $elemMatch: { item: groupIdA } },
  1992. });
  1993. const _page2 = await Page.findOne({
  1994. path: _path2,
  1995. grant: Page.GRANT_USER_GROUP,
  1996. grantedGroups: { $elemMatch: { item: groupIdB } },
  1997. });
  1998. const _page3 = await Page.findOne({
  1999. path: _path3,
  2000. grant: Page.GRANT_USER_GROUP,
  2001. grantedGroups: { $elemMatch: { item: groupIdC } },
  2002. });
  2003. const _page4 = await Page.findOne({
  2004. path: _path2,
  2005. grant: Page.GRANT_RESTRICTED,
  2006. });
  2007. expect(_page1).toBeTruthy();
  2008. expect(_page2).toBeTruthy();
  2009. expect(_page3).toBeTruthy();
  2010. expect(_page4).toBeTruthy();
  2011. await deleteCompletely(_page1, npDummyUser1, {}, true, false, {
  2012. ip: '::ffff:127.0.0.1',
  2013. endpoint: '/_api/v3/pages/rename',
  2014. });
  2015. const page1 = await Page.findOne({
  2016. path: _path1,
  2017. grant: Page.GRANT_USER_GROUP,
  2018. grantedGroups: { $elemMatch: { item: groupIdA } },
  2019. });
  2020. const page2 = await Page.findOne({
  2021. path: _path2,
  2022. grant: Page.GRANT_USER_GROUP,
  2023. grantedGroups: { $elemMatch: { item: groupIdB } },
  2024. });
  2025. const page3 = await Page.findOne({
  2026. path: _path3,
  2027. grant: Page.GRANT_USER_GROUP,
  2028. grantedGroups: { $elemMatch: { item: groupIdC } },
  2029. });
  2030. const page4 = await Page.findOne({
  2031. path: _path2,
  2032. grant: Page.GRANT_RESTRICTED,
  2033. });
  2034. expect(page1).toBeNull();
  2035. expect(page2).toBeNull();
  2036. expect(page3).toBeNull();
  2037. expect(page4).toBeTruthy();
  2038. });
  2039. });
  2040. });
  2041. describe('revert', () => {
  2042. const revertDeletedPage = async (
  2043. page,
  2044. user,
  2045. options = {},
  2046. isRecursively = false,
  2047. activityParameters?,
  2048. ) => {
  2049. // mock return value
  2050. const mockedRevertRecursivelyMainOperation = jest
  2051. .spyOn(crowi.pageService, 'revertRecursivelyMainOperation')
  2052. .mockReturnValue(null);
  2053. const revertedPage = await crowi.pageService.revertDeletedPage(
  2054. page,
  2055. user,
  2056. options,
  2057. isRecursively,
  2058. activityParameters,
  2059. );
  2060. const argsForRecursivelyMainOperation =
  2061. mockedRevertRecursivelyMainOperation.mock.calls[0];
  2062. // restores the original implementation
  2063. mockedRevertRecursivelyMainOperation.mockRestore();
  2064. if (isRecursively) {
  2065. await crowi.pageService.revertRecursivelyMainOperation(
  2066. ...(argsForRecursivelyMainOperation as Parameters<
  2067. typeof crowi.pageService.revertRecursivelyMainOperation
  2068. >),
  2069. );
  2070. }
  2071. return revertedPage;
  2072. };
  2073. test('should revert single deleted page with GRANT_RESTRICTED', async () => {
  2074. const trashedPage = await Page.findOne({
  2075. path: '/trash/np_revert1',
  2076. status: Page.STATUS_DELETED,
  2077. grant: Page.GRANT_RESTRICTED,
  2078. });
  2079. const revision = await Revision.findOne({ pageId: trashedPage?._id });
  2080. const tag = await Tag.findOne({ name: 'np_revertTag1' });
  2081. const deletedPageTagRelation = await PageTagRelation.findOne({
  2082. relatedPage: trashedPage?._id,
  2083. relatedTag: tag?._id,
  2084. isPageTrashed: true,
  2085. });
  2086. expect(trashedPage).toBeTruthy();
  2087. expect(revision).toBeTruthy();
  2088. expect(tag).toBeTruthy();
  2089. expect(deletedPageTagRelation).toBeTruthy();
  2090. await revertDeletedPage(trashedPage, dummyUser1, {}, false, {
  2091. ip: '::ffff:127.0.0.1',
  2092. endpoint: '/_api/v3/pages/rename',
  2093. });
  2094. const revertedPage = await Page.findOne({ path: '/np_revert1' });
  2095. const deltedPageBeforeRevert = await Page.findOne({
  2096. path: '/trash/np_revert1',
  2097. });
  2098. const pageTagRelation = await PageTagRelation.findOne<IPageTagRelation>({
  2099. relatedPage: revertedPage?._id,
  2100. relatedTag: tag?._id,
  2101. });
  2102. expect(revertedPage).toBeTruthy();
  2103. expect(pageTagRelation).toBeTruthy();
  2104. expect(deltedPageBeforeRevert).toBeNull();
  2105. // page with GRANT_RESTRICTED does not have parent
  2106. expect(revertedPage?.parent).toBeNull();
  2107. expect(revertedPage?.status).toBe(Page.STATUS_PUBLISHED);
  2108. expect(revertedPage?.grant).toBe(Page.GRANT_RESTRICTED);
  2109. expect(pageTagRelation?.isPageTrashed).toBe(false);
  2110. });
  2111. test('should revert single deleted page with GRANT_USER_GROUP', async () => {
  2112. const beforeRevertPath = '/trash/np_revert2';
  2113. const user1 = await User.findOne({ name: 'npUser1' });
  2114. const trashedPage = await Page.findOne({
  2115. path: beforeRevertPath,
  2116. status: Page.STATUS_DELETED,
  2117. grant: Page.GRANT_USER_GROUP,
  2118. });
  2119. const revision = await Revision.findOne({ pageId: trashedPage?._id });
  2120. const tag = await Tag.findOne({ name: 'np_revertTag2' });
  2121. const deletedPageTagRelation = await PageTagRelation.findOne({
  2122. relatedPage: trashedPage?._id,
  2123. relatedTag: tag?._id,
  2124. isPageTrashed: true,
  2125. });
  2126. expect(trashedPage).toBeTruthy();
  2127. expect(revision).toBeTruthy();
  2128. expect(tag).toBeTruthy();
  2129. expect(deletedPageTagRelation).toBeTruthy();
  2130. await revertDeletedPage(trashedPage, user1, {}, false, {
  2131. ip: '::ffff:127.0.0.1',
  2132. endpoint: '/_api/v3/pages/revert',
  2133. });
  2134. const revertedPage = await Page.findOne({ path: '/np_revert2' });
  2135. const trashedPageBR = await Page.findOne({ path: beforeRevertPath });
  2136. const pageTagRelation = await PageTagRelation.findOne<IPageTagRelation>({
  2137. relatedPage: revertedPage?._id,
  2138. relatedTag: tag?._id,
  2139. });
  2140. expect(revertedPage).toBeTruthy();
  2141. expect(pageTagRelation).toBeTruthy();
  2142. expect(trashedPageBR).toBeNull();
  2143. expect(revertedPage?.parent).toStrictEqual(rootPage._id);
  2144. expect(revertedPage?.status).toBe(Page.STATUS_PUBLISHED);
  2145. expect(revertedPage?.grant).toBe(Page.GRANT_USER_GROUP);
  2146. expect(normalizeGrantedGroups(revertedPage?.grantedGroups)).toStrictEqual(
  2147. [
  2148. { item: groupIdA, type: GroupType.userGroup },
  2149. { item: externalGroupIdA, type: GroupType.externalUserGroup },
  2150. ],
  2151. );
  2152. expect(pageTagRelation?.isPageTrashed).toBe(false);
  2153. });
  2154. test(`revert multiple pages: only target page should be reverted.
  2155. Non-existant middle page and leaf page with GRANT_RESTRICTED shoud not be reverted`, async () => {
  2156. const beforeRevertPath1 = '/trash/np_revert3';
  2157. const beforeRevertPath2 = '/trash/np_revert3/middle/np_revert4';
  2158. const trashedPage1 = await Page.findOne({
  2159. path: beforeRevertPath1,
  2160. status: Page.STATUS_DELETED,
  2161. grant: Page.GRANT_PUBLIC,
  2162. });
  2163. const trashedPage2 = await Page.findOne({
  2164. path: beforeRevertPath2,
  2165. status: Page.STATUS_DELETED,
  2166. grant: Page.GRANT_RESTRICTED,
  2167. });
  2168. const revision1 = await Revision.findOne({ pageId: trashedPage1?._id });
  2169. const revision2 = await Revision.findOne({ pageId: trashedPage2?._id });
  2170. expect(trashedPage1).toBeTruthy();
  2171. expect(trashedPage2).toBeTruthy();
  2172. expect(revision1).toBeTruthy();
  2173. expect(revision2).toBeTruthy();
  2174. await revertDeletedPage(trashedPage1, npDummyUser2, {}, true, {
  2175. ip: '::ffff:127.0.0.1',
  2176. endpoint: '/_api/v3/pages/revert',
  2177. });
  2178. const revertedPage = await Page.findOne({ path: '/np_revert3' });
  2179. const middlePage = await Page.findOne({ path: '/np_revert3/middle' });
  2180. const notRestrictedPage = await Page.findOne({
  2181. path: '/np_revert3/middle/np_revert4',
  2182. });
  2183. // AR => After Revert
  2184. const trashedPage1AR = await Page.findOne({ path: beforeRevertPath1 });
  2185. const trashedPage2AR = await Page.findOne({ path: beforeRevertPath2 });
  2186. const revision1AR = await Revision.findOne({ pageId: revertedPage?._id });
  2187. const revision2AR = await Revision.findOne({
  2188. pageId: trashedPage2AR?._id,
  2189. });
  2190. expect(revertedPage).toBeTruthy();
  2191. expect(trashedPage2AR).toBeTruthy();
  2192. expect(revision1AR).toBeTruthy();
  2193. expect(revision2AR).toBeTruthy();
  2194. expect(trashedPage1AR).toBeNull();
  2195. expect(notRestrictedPage).toBeNull();
  2196. expect(middlePage).toBeNull();
  2197. expect(revertedPage?.parent).toStrictEqual(rootPage._id);
  2198. expect(revertedPage?.status).toBe(Page.STATUS_PUBLISHED);
  2199. expect(revertedPage?.grant).toBe(Page.GRANT_PUBLIC);
  2200. });
  2201. test('revert multiple pages: target page, initially non-existant page and leaf page with GRANT_USER_GROUP shoud be reverted', async () => {
  2202. const user = await User.findOne({ _id: npDummyUser3 });
  2203. const beforeRevertPath1 = '/trash/np_revert5';
  2204. const beforeRevertPath2 = '/trash/np_revert5/middle/np_revert6';
  2205. const beforeRevertPath3 = '/trash/np_revert5/middle';
  2206. const trashedPage1 = await Page.findOne({
  2207. path: beforeRevertPath1,
  2208. status: Page.STATUS_DELETED,
  2209. grantedGroups: { $elemMatch: { item: groupIdA } },
  2210. });
  2211. const trashedPage2 = await Page.findOne({
  2212. path: beforeRevertPath2,
  2213. status: Page.STATUS_DELETED,
  2214. grantedGroups: { $elemMatch: { item: groupIdB } },
  2215. });
  2216. const nonExistantPage3 = await Page.findOne({ path: beforeRevertPath3 }); // not exist
  2217. const revision1 = await Revision.findOne({ pageId: trashedPage1?._id });
  2218. const revision2 = await Revision.findOne({ pageId: trashedPage2?._id });
  2219. expect(trashedPage1).toBeTruthy();
  2220. expect(trashedPage2).toBeTruthy();
  2221. expect(revision1).toBeTruthy();
  2222. expect(revision2).toBeTruthy();
  2223. expect(user).toBeTruthy();
  2224. expect(nonExistantPage3).toBeNull();
  2225. await revertDeletedPage(trashedPage1, user, {}, true, {
  2226. ip: '::ffff:127.0.0.1',
  2227. endpoint: '/_api/v3/pages/revert',
  2228. });
  2229. const revertedPage1 = await Page.findOne({ path: '/np_revert5' });
  2230. const newlyCreatedPage = await Page.findOne({
  2231. path: '/np_revert5/middle',
  2232. });
  2233. const revertedPage2 = await Page.findOne({
  2234. path: '/np_revert5/middle/np_revert6',
  2235. });
  2236. // // AR => After Revert
  2237. const trashedPage1AR = await Page.findOne({ path: beforeRevertPath1 });
  2238. const trashedPage2AR = await Page.findOne({ path: beforeRevertPath2 });
  2239. expect(revertedPage1).toBeTruthy();
  2240. expect(newlyCreatedPage).toBeTruthy();
  2241. expect(revertedPage2).toBeTruthy();
  2242. expect(trashedPage1AR).toBeNull();
  2243. expect(trashedPage2AR).toBeNull();
  2244. expect(newlyCreatedPage?.isEmpty).toBe(true);
  2245. expect(revertedPage1?.parent).toStrictEqual(rootPage._id);
  2246. expect(revertedPage2?.parent).toStrictEqual(newlyCreatedPage?._id);
  2247. expect(newlyCreatedPage?.parent).toStrictEqual(revertedPage1?._id);
  2248. expect(revertedPage1?.status).toBe(Page.STATUS_PUBLISHED);
  2249. expect(revertedPage2?.status).toBe(Page.STATUS_PUBLISHED);
  2250. expect(newlyCreatedPage?.status).toBe(Page.STATUS_PUBLISHED);
  2251. expect(
  2252. normalizeGrantedGroups(revertedPage1?.grantedGroups),
  2253. ).toStrictEqual([
  2254. { item: groupIdA, type: GroupType.userGroup },
  2255. { item: externalGroupIdA, type: GroupType.externalUserGroup },
  2256. ]);
  2257. expect(
  2258. normalizeGrantedGroups(revertedPage2?.grantedGroups),
  2259. ).toStrictEqual([
  2260. { item: groupIdB, type: GroupType.userGroup },
  2261. { item: externalGroupIdB, type: GroupType.externalUserGroup },
  2262. ]);
  2263. expect(newlyCreatedPage?.grant).toBe(Page.GRANT_PUBLIC);
  2264. });
  2265. });
  2266. });