page.test.js 15 KB

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