page.test.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. const mongoose = require('mongoose');
  2. const { getInstance } = require('../setup-crowi');
  3. let testUser0;
  4. let testUser1;
  5. let testUser2;
  6. let testGroup0;
  7. let parentPage;
  8. describe('Page', () => {
  9. let Page;
  10. let PageQueryBuilder;
  11. let User;
  12. let UserGroup;
  13. let UserGroupRelation;
  14. beforeAll(async () => {
  15. crowi = await getInstance();
  16. User = mongoose.model('User');
  17. UserGroup = mongoose.model('UserGroup');
  18. UserGroupRelation = mongoose.model('UserGroupRelation');
  19. Page = mongoose.model('Page');
  20. PageQueryBuilder = Page.PageQueryBuilder;
  21. await User.insertMany([
  22. {
  23. name: 'Anon 0',
  24. username: 'anonymous0',
  25. email: 'anonymous0@example.com',
  26. },
  27. {
  28. name: 'Anon 1',
  29. username: 'anonymous1',
  30. email: 'anonymous1@example.com',
  31. },
  32. {
  33. name: 'Anon 2',
  34. username: 'anonymous2',
  35. email: 'anonymous2@example.com',
  36. },
  37. ]);
  38. await UserGroup.insertMany([
  39. { name: 'TestGroup0' },
  40. { name: 'TestGroup1' },
  41. ]);
  42. testUser0 = await User.findOne({ username: 'anonymous0' });
  43. testUser1 = await User.findOne({ username: 'anonymous1' });
  44. testUser2 = await User.findOne({ username: 'anonymous2' });
  45. testGroup0 = await UserGroup.findOne({ name: 'TestGroup0' });
  46. await UserGroupRelation.insertMany([
  47. {
  48. relatedGroup: testGroup0,
  49. relatedUser: testUser0,
  50. },
  51. {
  52. relatedGroup: testGroup0,
  53. relatedUser: testUser1,
  54. },
  55. ]);
  56. await Page.insertMany([
  57. {
  58. path: '/user/anonymous0/memo',
  59. grant: Page.GRANT_RESTRICTED,
  60. grantedUsers: [testUser0],
  61. creator: testUser0,
  62. },
  63. {
  64. path: '/grant',
  65. grant: Page.GRANT_PUBLIC,
  66. grantedUsers: [testUser0],
  67. creator: testUser0,
  68. },
  69. {
  70. path: '/grant/public',
  71. grant: Page.GRANT_PUBLIC,
  72. grantedUsers: [testUser0],
  73. creator: testUser0,
  74. },
  75. {
  76. path: '/grant/restricted',
  77. grant: Page.GRANT_RESTRICTED,
  78. grantedUsers: [testUser0],
  79. creator: testUser0,
  80. },
  81. {
  82. path: '/grant/specified',
  83. grant: Page.GRANT_SPECIFIED,
  84. grantedUsers: [testUser0],
  85. creator: testUser0,
  86. },
  87. {
  88. path: '/grant/owner',
  89. grant: Page.GRANT_OWNER,
  90. grantedUsers: [testUser0],
  91. creator: testUser0,
  92. },
  93. {
  94. path: '/page/child/without/parents',
  95. grant: Page.GRANT_PUBLIC,
  96. creator: testUser0,
  97. },
  98. {
  99. path: '/grant/groupacl',
  100. grant: Page.GRANT_USER_GROUP,
  101. grantedUsers: [],
  102. grantedGroups: [{ item: testGroup0, type: 'UserGroup' }],
  103. creator: testUser1,
  104. },
  105. {
  106. path: '/page1',
  107. grant: Page.GRANT_PUBLIC,
  108. creator: testUser0,
  109. },
  110. {
  111. path: '/page1/child1',
  112. grant: Page.GRANT_PUBLIC,
  113. creator: testUser0,
  114. },
  115. {
  116. path: '/page2',
  117. grant: Page.GRANT_PUBLIC,
  118. creator: testUser0,
  119. },
  120. ]);
  121. parentPage = await Page.findOne({ path: '/grant' });
  122. });
  123. describe('.isPublic', () => {
  124. describe('with a public page', () => {
  125. test('should return true', async () => {
  126. const page = await Page.findOne({ path: '/grant/public' });
  127. expect(page.isPublic()).toEqual(true);
  128. });
  129. });
  130. ['restricted', 'specified', 'owner'].forEach((grant) => {
  131. describe(`with a ${grant} page`, () => {
  132. test('should return false', async () => {
  133. const page = await Page.findOne({ path: `/grant/${grant}` });
  134. expect(page.isPublic()).toEqual(false);
  135. });
  136. });
  137. });
  138. });
  139. describe('.getDeletedPageName', () => {
  140. test('should return trash page name', () => {
  141. expect(Page.getDeletedPageName('/hoge')).toEqual('/trash/hoge');
  142. expect(Page.getDeletedPageName('hoge')).toEqual('/trash/hoge');
  143. });
  144. });
  145. describe('.getRevertDeletedPageName', () => {
  146. test('should return reverted trash page name', () => {
  147. expect(Page.getRevertDeletedPageName('/hoge')).toEqual('/hoge');
  148. expect(Page.getRevertDeletedPageName('/trash/hoge')).toEqual('/hoge');
  149. expect(Page.getRevertDeletedPageName('/trash/hoge/trash')).toEqual(
  150. '/hoge/trash',
  151. );
  152. });
  153. });
  154. describe('.isAccessiblePageByViewer', () => {
  155. describe('with a granted page', () => {
  156. test('should return true with granted user', async () => {
  157. const user = await User.findOne({ email: 'anonymous0@example.com' });
  158. const page = await Page.findOne({ path: '/user/anonymous0/memo' });
  159. const bool = await Page.isAccessiblePageByViewer(page.id, user);
  160. expect(bool).toEqual(true);
  161. });
  162. test('should return false without user', async () => {
  163. const user = null;
  164. const page = await Page.findOne({ path: '/user/anonymous0/memo' });
  165. const bool = await Page.isAccessiblePageByViewer(page.id, user);
  166. expect(bool).toEqual(true);
  167. });
  168. });
  169. describe('with a public page', () => {
  170. test('should return true with user', async () => {
  171. const user = await User.findOne({ email: 'anonymous1@example.com' });
  172. const page = await Page.findOne({ path: '/grant/public' });
  173. const bool = await Page.isAccessiblePageByViewer(page.id, user);
  174. expect(bool).toEqual(true);
  175. });
  176. test('should return true with out', async () => {
  177. const user = null;
  178. const page = await Page.findOne({ path: '/grant/public' });
  179. const bool = await Page.isAccessiblePageByViewer(page.id, user);
  180. expect(bool).toEqual(true);
  181. });
  182. });
  183. describe('with a restricted page', () => {
  184. test('should return false with user who has no grant', async () => {
  185. const user = await User.findOne({ email: 'anonymous1@example.com' });
  186. const page = await Page.findOne({ path: '/grant/owner' });
  187. const bool = await Page.isAccessiblePageByViewer(page.id, user);
  188. expect(bool).toEqual(false);
  189. });
  190. test('should return false without user', async () => {
  191. const user = null;
  192. const page = await Page.findOne({ path: '/grant/owner' });
  193. const bool = await Page.isAccessiblePageByViewer(page.id, user);
  194. expect(bool).toEqual(false);
  195. });
  196. });
  197. });
  198. describe('.findPage', () => {
  199. describe('findByIdAndViewer', () => {
  200. test('should find page (public)', async () => {
  201. const expectedPage = await Page.findOne({ path: '/grant/public' });
  202. const page = await Page.findByIdAndViewer(expectedPage.id, testUser0);
  203. expect(page).not.toBeNull();
  204. expect(page.path).toEqual(expectedPage.path);
  205. });
  206. test('should find page (anyone knows link)', async () => {
  207. const expectedPage = await Page.findOne({ path: '/grant/restricted' });
  208. const page = await Page.findByIdAndViewer(expectedPage.id, testUser1);
  209. expect(page).not.toBeNull();
  210. expect(page.path).toEqual(expectedPage.path);
  211. });
  212. test('should find page (only me)', async () => {
  213. const expectedPage = await Page.findOne({ path: '/grant/owner' });
  214. const page = await Page.findByIdAndViewer(expectedPage.id, testUser0);
  215. expect(page).not.toBeNull();
  216. expect(page.path).toEqual(expectedPage.path);
  217. });
  218. test('should not be found by grant (only me)', async () => {
  219. const expectedPage = await Page.findOne({ path: '/grant/owner' });
  220. const page = await Page.findByIdAndViewer(expectedPage.id, testUser1);
  221. expect(page).toBeNull();
  222. });
  223. });
  224. describe('findByIdAndViewer granted userGroup', () => {
  225. test('should find page', async () => {
  226. const expectedPage = await Page.findOne({ path: '/grant/groupacl' });
  227. const page = await Page.findByIdAndViewer(expectedPage.id, testUser0);
  228. expect(page).not.toBeNull();
  229. expect(page.path).toEqual(expectedPage.path);
  230. });
  231. test('should not be found by grant', async () => {
  232. const expectedPage = await Page.findOne({ path: '/grant/groupacl' });
  233. const page = await Page.findByIdAndViewer(expectedPage.id, testUser2);
  234. expect(page).toBeNull();
  235. });
  236. });
  237. });
  238. describe('PageQueryBuilder.addConditionToListWithDescendants', () => {
  239. test('can retrieve descendants of /page', async () => {
  240. const builder = new PageQueryBuilder(Page.find());
  241. builder.addConditionToListWithDescendants('/page');
  242. const result = await builder.query.exec();
  243. // assert totalCount
  244. expect(result.length).toEqual(1);
  245. // assert paths
  246. const pagePaths = result.map((page) => {
  247. return page.path;
  248. });
  249. expect(pagePaths).toContainEqual('/page/child/without/parents');
  250. });
  251. test('can retrieve descendants of /page1', async () => {
  252. const builder = new PageQueryBuilder(Page.find());
  253. builder.addConditionToListWithDescendants('/page1/');
  254. const result = await builder.query.exec();
  255. // assert totalCount
  256. expect(result.length).toEqual(2);
  257. // assert paths
  258. const pagePaths = result.map((page) => {
  259. return page.path;
  260. });
  261. expect(pagePaths).toContainEqual('/page1');
  262. expect(pagePaths).toContainEqual('/page1/child1');
  263. });
  264. });
  265. describe('PageQueryBuilder.addConditionToListOnlyDescendants', () => {
  266. test('can retrieve only descendants of /page', async () => {
  267. const builder = new PageQueryBuilder(Page.find());
  268. builder.addConditionToListOnlyDescendants('/page');
  269. const result = await builder.query.exec();
  270. // assert totalCount
  271. expect(result.length).toEqual(1);
  272. // assert paths
  273. const pagePaths = result.map((page) => {
  274. return page.path;
  275. });
  276. expect(pagePaths).toContainEqual('/page/child/without/parents');
  277. });
  278. test('can retrieve only descendants of /page1', async () => {
  279. const builder = new PageQueryBuilder(Page.find());
  280. builder.addConditionToListOnlyDescendants('/page1');
  281. const result = await builder.query.exec();
  282. // assert totalCount
  283. expect(result.length).toEqual(1);
  284. // assert paths
  285. const pagePaths = result.map((page) => {
  286. return page.path;
  287. });
  288. expect(pagePaths).toContainEqual('/page1/child1');
  289. });
  290. });
  291. describe('PageQueryBuilder.addConditionToListByStartWith', () => {
  292. test('can retrieve pages which starts with /page', async () => {
  293. const builder = new PageQueryBuilder(Page.find());
  294. builder.addConditionToListByStartWith('/page');
  295. const result = await builder.query.exec();
  296. // assert totalCount
  297. expect(result.length).toEqual(4);
  298. // assert paths
  299. const pagePaths = result.map((page) => {
  300. return page.path;
  301. });
  302. expect(pagePaths).toContainEqual('/page/child/without/parents');
  303. expect(pagePaths).toContainEqual('/page1');
  304. expect(pagePaths).toContainEqual('/page1/child1');
  305. expect(pagePaths).toContainEqual('/page2');
  306. });
  307. });
  308. describe('.findListWithDescendants', () => {
  309. test('can retrieve all pages with testUser0', async () => {
  310. const result = await Page.findListWithDescendants('/grant', testUser0);
  311. const { pages } = result;
  312. // assert totalCount
  313. expect(pages.length).toEqual(5);
  314. // assert paths
  315. const pagePaths = await pages.map((page) => {
  316. return page.path;
  317. });
  318. expect(pagePaths).toContainEqual('/grant/groupacl');
  319. expect(pagePaths).toContainEqual('/grant/specified');
  320. expect(pagePaths).toContainEqual('/grant/owner');
  321. expect(pagePaths).toContainEqual('/grant/public');
  322. expect(pagePaths).toContainEqual('/grant');
  323. });
  324. test('can retrieve all pages with testUser1', async () => {
  325. const result = await Page.findListWithDescendants('/grant', testUser1);
  326. const { pages } = result;
  327. // assert totalCount
  328. expect(pages.length).toEqual(5);
  329. // assert paths
  330. const pagePaths = await pages.map((page) => {
  331. return page.path;
  332. });
  333. expect(pagePaths).toContainEqual('/grant/groupacl');
  334. expect(pagePaths).toContainEqual('/grant/specified');
  335. expect(pagePaths).toContainEqual('/grant/owner');
  336. expect(pagePaths).toContainEqual('/grant/public');
  337. expect(pagePaths).toContainEqual('/grant');
  338. });
  339. test('can retrieve all pages with testUser2', async () => {
  340. const result = await Page.findListWithDescendants('/grant', testUser2);
  341. const { pages } = result;
  342. // assert totalCount
  343. expect(pages.length).toEqual(5);
  344. // assert paths
  345. const pagePaths = await pages.map((page) => {
  346. return page.path;
  347. });
  348. expect(pagePaths).toContainEqual('/grant/groupacl');
  349. expect(pagePaths).toContainEqual('/grant/specified');
  350. expect(pagePaths).toContainEqual('/grant/owner');
  351. expect(pagePaths).toContainEqual('/grant/public');
  352. expect(pagePaths).toContainEqual('/grant');
  353. });
  354. test('can retrieve all pages without user', async () => {
  355. const result = await Page.findListWithDescendants('/grant', null);
  356. const { pages } = result;
  357. // assert totalCount
  358. expect(pages.length).toEqual(5);
  359. // assert paths
  360. const pagePaths = await pages.map((page) => {
  361. return page.path;
  362. });
  363. expect(pagePaths).toContainEqual('/grant/groupacl');
  364. expect(pagePaths).toContainEqual('/grant/specified');
  365. expect(pagePaths).toContainEqual('/grant/owner');
  366. expect(pagePaths).toContainEqual('/grant/public');
  367. expect(pagePaths).toContainEqual('/grant');
  368. });
  369. });
  370. describe('.findManageableListWithDescendants', () => {
  371. test('can retrieve all pages with testUser0', async () => {
  372. const pages = await Page.findManageableListWithDescendants(
  373. parentPage,
  374. testUser0,
  375. );
  376. // assert totalCount
  377. expect(pages.length).toEqual(5);
  378. // assert paths
  379. const pagePaths = await pages.map((page) => {
  380. return page.path;
  381. });
  382. expect(pagePaths).toContainEqual('/grant/groupacl');
  383. expect(pagePaths).toContainEqual('/grant/specified');
  384. expect(pagePaths).toContainEqual('/grant/owner');
  385. expect(pagePaths).toContainEqual('/grant/public');
  386. expect(pagePaths).toContainEqual('/grant');
  387. });
  388. test('can retrieve group page and public page which starts with testUser1', async () => {
  389. const pages = await Page.findManageableListWithDescendants(
  390. parentPage,
  391. testUser1,
  392. );
  393. // assert totalCount
  394. expect(pages.length).toEqual(3);
  395. // assert paths
  396. const pagePaths = await pages.map((page) => {
  397. return page.path;
  398. });
  399. expect(pagePaths).toContainEqual('/grant/groupacl');
  400. expect(pagePaths).toContainEqual('/grant/public');
  401. expect(pagePaths).toContainEqual('/grant');
  402. });
  403. test('can retrieve only public page which starts with testUser2', async () => {
  404. const pages = await Page.findManageableListWithDescendants(
  405. parentPage,
  406. testUser2,
  407. );
  408. // assert totalCount
  409. expect(pages.length).toEqual(2);
  410. // assert paths
  411. const pagePaths = await pages.map((page) => {
  412. return page.path;
  413. });
  414. expect(pagePaths).toContainEqual('/grant/public');
  415. expect(pagePaths).toContainEqual('/grant');
  416. });
  417. test('can retrieve only public page which starts without user', async () => {
  418. const pages = await Page.findManageableListWithDescendants(
  419. parentPage,
  420. null,
  421. );
  422. // assert totalCount
  423. expect(pages).toBeNull();
  424. });
  425. });
  426. });