page.test.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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(done) => {
  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. done();
  113. });
  114. describe('.isPublic', () => {
  115. describe('with a public page', () => {
  116. test('should return true', async() => {
  117. const page = await Page.findOne({ path: '/grant/public' });
  118. expect(page.isPublic()).toEqual(true);
  119. });
  120. });
  121. ['restricted', 'specified', 'owner'].forEach((grant) => {
  122. describe(`with a ${grant} page`, () => {
  123. test('should return false', async() => {
  124. const page = await Page.findOne({ path: `/grant/${grant}` });
  125. expect(page.isPublic()).toEqual(false);
  126. });
  127. });
  128. });
  129. });
  130. describe('.getDeletedPageName', () => {
  131. test('should return trash page name', () => {
  132. expect(Page.getDeletedPageName('/hoge')).toEqual('/trash/hoge');
  133. expect(Page.getDeletedPageName('hoge')).toEqual('/trash/hoge');
  134. });
  135. });
  136. describe('.getRevertDeletedPageName', () => {
  137. test('should return reverted trash page name', () => {
  138. expect(Page.getRevertDeletedPageName('/hoge')).toEqual('/hoge');
  139. expect(Page.getRevertDeletedPageName('/trash/hoge')).toEqual('/hoge');
  140. expect(Page.getRevertDeletedPageName('/trash/hoge/trash')).toEqual('/hoge/trash');
  141. });
  142. });
  143. describe('.isDeletableName', () => {
  144. test('should decide deletable or not', () => {
  145. expect(Page.isDeletableName('/hoge')).toBeTruthy();
  146. expect(Page.isDeletableName('/user/xxx')).toBeFalsy();
  147. expect(Page.isDeletableName('/user/xxx123')).toBeFalsy();
  148. expect(Page.isDeletableName('/user/xxx/')).toBeTruthy();
  149. expect(Page.isDeletableName('/user/xxx/hoge')).toBeTruthy();
  150. });
  151. });
  152. describe('.isAccessiblePageByViewer', () => {
  153. describe('with a granted page', () => {
  154. test('should return true with granted user', async() => {
  155. const user = await User.findOne({ email: 'anonymous0@example.com' });
  156. const page = await Page.findOne({ path: '/user/anonymous0/memo' });
  157. const bool = await Page.isAccessiblePageByViewer(page.id, user);
  158. expect(bool).toEqual(true);
  159. });
  160. test('should return false without user', async() => {
  161. const user = null;
  162. const page = await Page.findOne({ path: '/user/anonymous0/memo' });
  163. const bool = await Page.isAccessiblePageByViewer(page.id, user);
  164. expect(bool).toEqual(true);
  165. });
  166. });
  167. describe('with a public page', () => {
  168. test('should return true with user', async() => {
  169. const user = await User.findOne({ email: 'anonymous1@example.com' });
  170. const page = await Page.findOne({ path: '/grant/public' });
  171. const bool = await Page.isAccessiblePageByViewer(page.id, user);
  172. expect(bool).toEqual(true);
  173. });
  174. test('should return true with out', async() => {
  175. const user = null;
  176. const page = await Page.findOne({ path: '/grant/public' });
  177. const bool = await Page.isAccessiblePageByViewer(page.id, user);
  178. expect(bool).toEqual(true);
  179. });
  180. });
  181. describe('with a restricted page', () => {
  182. test('should return false with user who has no grant', async() => {
  183. const user = await User.findOne({ email: 'anonymous1@example.com' });
  184. const page = await Page.findOne({ path: '/grant/owner' });
  185. const bool = await Page.isAccessiblePageByViewer(page.id, user);
  186. expect(bool).toEqual(false);
  187. });
  188. test('should return false without user', async() => {
  189. const user = null;
  190. const page = await Page.findOne({ path: '/grant/owner' });
  191. const bool = await Page.isAccessiblePageByViewer(page.id, user);
  192. expect(bool).toEqual(false);
  193. });
  194. });
  195. });
  196. describe('.findPage', () => {
  197. describe('findByIdAndViewer', () => {
  198. test('should find page (public)', async() => {
  199. const expectedPage = await Page.findOne({ path: '/grant/public' });
  200. const page = await Page.findByIdAndViewer(expectedPage.id, testUser0);
  201. expect(page).not.toBeNull();
  202. expect(page.path).toEqual(expectedPage.path);
  203. });
  204. test('should find page (anyone knows link)', async() => {
  205. const expectedPage = await Page.findOne({ path: '/grant/restricted' });
  206. const page = await Page.findByIdAndViewer(expectedPage.id, testUser1);
  207. expect(page).not.toBeNull();
  208. expect(page.path).toEqual(expectedPage.path);
  209. });
  210. test('should find page (only me)', async() => {
  211. const expectedPage = await Page.findOne({ path: '/grant/owner' });
  212. const page = await Page.findByIdAndViewer(expectedPage.id, testUser0);
  213. expect(page).not.toBeNull();
  214. expect(page.path).toEqual(expectedPage.path);
  215. });
  216. test('should not be found by grant (only me)', async() => {
  217. const expectedPage = await Page.findOne({ path: '/grant/owner' });
  218. const page = await Page.findByIdAndViewer(expectedPage.id, testUser1);
  219. expect(page).toBeNull();
  220. });
  221. });
  222. describe('findByIdAndViewer granted userGroup', () => {
  223. test('should find page', async() => {
  224. const expectedPage = await Page.findOne({ path: '/grant/groupacl' });
  225. const page = await Page.findByIdAndViewer(expectedPage.id, testUser0);
  226. expect(page).not.toBeNull();
  227. expect(page.path).toEqual(expectedPage.path);
  228. });
  229. test('should not be found by grant', async() => {
  230. const expectedPage = await Page.findOne({ path: '/grant/groupacl' });
  231. const page = await Page.findByIdAndViewer(expectedPage.id, testUser2);
  232. expect(page).toBeNull();
  233. });
  234. });
  235. });
  236. describe('PageQueryBuilder.addConditionToListWithDescendants', () => {
  237. test('can retrieve descendants of /page', async() => {
  238. const builder = new PageQueryBuilder(Page.find());
  239. builder.addConditionToListWithDescendants('/page');
  240. const result = await builder.query.exec();
  241. // assert totalCount
  242. expect(result.length).toEqual(1);
  243. // assert paths
  244. const pagePaths = result.map((page) => { return page.path });
  245. expect(pagePaths).toContainEqual('/page/child/without/parents');
  246. });
  247. test('can retrieve descendants of /page1', async() => {
  248. const builder = new PageQueryBuilder(Page.find());
  249. builder.addConditionToListWithDescendants('/page1/');
  250. const result = await builder.query.exec();
  251. // assert totalCount
  252. expect(result.length).toEqual(2);
  253. // assert paths
  254. const pagePaths = result.map((page) => { return page.path });
  255. expect(pagePaths).toContainEqual('/page1');
  256. expect(pagePaths).toContainEqual('/page1/child1');
  257. });
  258. });
  259. describe('PageQueryBuilder.addConditionToListOnlyDescendants', () => {
  260. test('can retrieve only descendants of /page', async() => {
  261. const builder = new PageQueryBuilder(Page.find());
  262. builder.addConditionToListOnlyDescendants('/page');
  263. const result = await builder.query.exec();
  264. // assert totalCount
  265. expect(result.length).toEqual(1);
  266. // assert paths
  267. const pagePaths = result.map((page) => { return page.path });
  268. expect(pagePaths).toContainEqual('/page/child/without/parents');
  269. });
  270. test('can retrieve only descendants of /page1', async() => {
  271. const builder = new PageQueryBuilder(Page.find());
  272. builder.addConditionToListOnlyDescendants('/page1');
  273. const result = await builder.query.exec();
  274. // assert totalCount
  275. expect(result.length).toEqual(1);
  276. // assert paths
  277. const pagePaths = result.map((page) => { return page.path });
  278. expect(pagePaths).toContainEqual('/page1/child1');
  279. });
  280. });
  281. describe('PageQueryBuilder.addConditionToListByStartWith', () => {
  282. test('can retrieve pages which starts with /page', async() => {
  283. const builder = new PageQueryBuilder(Page.find());
  284. builder.addConditionToListByStartWith('/page');
  285. const result = await builder.query.exec();
  286. // assert totalCount
  287. expect(result.length).toEqual(4);
  288. // assert paths
  289. const pagePaths = result.map((page) => { return page.path });
  290. expect(pagePaths).toContainEqual('/page/child/without/parents');
  291. expect(pagePaths).toContainEqual('/page1');
  292. expect(pagePaths).toContainEqual('/page1/child1');
  293. expect(pagePaths).toContainEqual('/page2');
  294. });
  295. });
  296. describe('.findListWithDescendants', () => {
  297. test('can retrieve all pages with testUser0', async() => {
  298. const result = await Page.findListWithDescendants('/grant', testUser0);
  299. const { pages } = result;
  300. // assert totalCount
  301. expect(pages.length).toEqual(5);
  302. // assert paths
  303. const pagePaths = await pages.map((page) => { return page.path });
  304. expect(pagePaths).toContainEqual('/grant/groupacl');
  305. expect(pagePaths).toContainEqual('/grant/specified');
  306. expect(pagePaths).toContainEqual('/grant/owner');
  307. expect(pagePaths).toContainEqual('/grant/public');
  308. expect(pagePaths).toContainEqual('/grant');
  309. });
  310. test('can retrieve all pages with testUser1', async() => {
  311. const result = await Page.findListWithDescendants('/grant', testUser1);
  312. const { pages } = result;
  313. // assert totalCount
  314. expect(pages.length).toEqual(5);
  315. // assert paths
  316. const pagePaths = await pages.map((page) => { return page.path });
  317. expect(pagePaths).toContainEqual('/grant/groupacl');
  318. expect(pagePaths).toContainEqual('/grant/specified');
  319. expect(pagePaths).toContainEqual('/grant/owner');
  320. expect(pagePaths).toContainEqual('/grant/public');
  321. expect(pagePaths).toContainEqual('/grant');
  322. });
  323. test('can retrieve all pages with testUser2', async() => {
  324. const result = await Page.findListWithDescendants('/grant', testUser2);
  325. const { pages } = result;
  326. // assert totalCount
  327. expect(pages.length).toEqual(5);
  328. // assert paths
  329. const pagePaths = await pages.map((page) => { return page.path });
  330. expect(pagePaths).toContainEqual('/grant/groupacl');
  331. expect(pagePaths).toContainEqual('/grant/specified');
  332. expect(pagePaths).toContainEqual('/grant/owner');
  333. expect(pagePaths).toContainEqual('/grant/public');
  334. expect(pagePaths).toContainEqual('/grant');
  335. });
  336. test('can retrieve all pages without user', async() => {
  337. const result = await Page.findListWithDescendants('/grant', null);
  338. const { pages } = result;
  339. // assert totalCount
  340. expect(pages.length).toEqual(5);
  341. // assert paths
  342. const pagePaths = await pages.map((page) => { return page.path });
  343. expect(pagePaths).toContainEqual('/grant/groupacl');
  344. expect(pagePaths).toContainEqual('/grant/specified');
  345. expect(pagePaths).toContainEqual('/grant/owner');
  346. expect(pagePaths).toContainEqual('/grant/public');
  347. expect(pagePaths).toContainEqual('/grant');
  348. });
  349. });
  350. describe('.findManageableListWithDescendants', () => {
  351. test('can retrieve all pages with testUser0', async() => {
  352. const pages = await Page.findManageableListWithDescendants(parentPage, testUser0);
  353. // assert totalCount
  354. expect(pages.length).toEqual(5);
  355. // assert paths
  356. const pagePaths = await pages.map((page) => { return page.path });
  357. expect(pagePaths).toContainEqual('/grant/groupacl');
  358. expect(pagePaths).toContainEqual('/grant/specified');
  359. expect(pagePaths).toContainEqual('/grant/owner');
  360. expect(pagePaths).toContainEqual('/grant/public');
  361. expect(pagePaths).toContainEqual('/grant');
  362. });
  363. test('can retrieve group page and public page which starts with testUser1', async() => {
  364. const pages = await Page.findManageableListWithDescendants(parentPage, testUser1);
  365. // assert totalCount
  366. expect(pages.length).toEqual(3);
  367. // assert paths
  368. const pagePaths = await pages.map((page) => { return page.path });
  369. expect(pagePaths).toContainEqual('/grant/groupacl');
  370. expect(pagePaths).toContainEqual('/grant/public');
  371. expect(pagePaths).toContainEqual('/grant');
  372. });
  373. test('can retrieve only public page which starts with testUser2', async() => {
  374. const pages = await Page.findManageableListWithDescendants(parentPage, testUser2);
  375. // assert totalCount
  376. expect(pages.length).toEqual(2);
  377. // assert paths
  378. const pagePaths = await pages.map((page) => { return page.path });
  379. expect(pagePaths).toContainEqual('/grant/public');
  380. expect(pagePaths).toContainEqual('/grant');
  381. });
  382. test('can retrieve only public page which starts without user', async() => {
  383. const pages = await Page.findManageableListWithDescendants(parentPage, null);
  384. // assert totalCount
  385. expect(pages).toBeNull();
  386. });
  387. });
  388. });