page.test.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. const mongoose = require('mongoose');
  2. const { getInstance } = require('../setup-crowi');
  3. describe('Page', () => {
  4. // eslint-disable-next-line no-unused-vars
  5. let crowi;
  6. let Page;
  7. let User;
  8. let UserGroup;
  9. let UserGroupRelation;
  10. beforeAll(async(done) => {
  11. crowi = await getInstance();
  12. done();
  13. });
  14. beforeEach(async(done) => {
  15. User = mongoose.model('User');
  16. UserGroup = mongoose.model('UserGroup');
  17. UserGroupRelation = mongoose.model('UserGroupRelation');
  18. Page = mongoose.model('Page');
  19. // remove all
  20. await Promise.all([
  21. Page.remove({}),
  22. User.remove({}),
  23. UserGroup.remove({}),
  24. UserGroupRelation.remove({}),
  25. ]);
  26. await User.insertMany([
  27. { name: 'Anon 0', username: 'anonymous0', email: 'anonymous0@example.com' },
  28. { name: 'Anon 1', username: 'anonymous1', email: 'anonymous1@example.com' },
  29. { name: 'Anon 2', username: 'anonymous2', email: 'anonymous2@example.com' },
  30. ]);
  31. await UserGroup.insertMany([
  32. { name: 'TestGroup0' },
  33. { name: 'TestGroup1' },
  34. ]);
  35. const testUser0 = await User.findOne({ username: 'anonymous0' });
  36. const testUser1 = await User.findOne({ username: 'anonymous1' });
  37. const testGroup0 = await UserGroup.findOne({ name: 'TestGroup0' });
  38. await UserGroupRelation.insertMany([
  39. {
  40. relatedGroup: testGroup0,
  41. relatedUser: testUser0,
  42. },
  43. {
  44. relatedGroup: testGroup0,
  45. relatedUser: testUser1,
  46. },
  47. ]);
  48. await Page.insertMany([
  49. {
  50. path: '/user/anonymous0/memo',
  51. grant: Page.GRANT_RESTRICTED,
  52. grantedUsers: [testUser0],
  53. creator: testUser0,
  54. },
  55. {
  56. path: '/grant/public',
  57. grant: Page.GRANT_PUBLIC,
  58. grantedUsers: [testUser0],
  59. creator: testUser0,
  60. },
  61. {
  62. path: '/grant/restricted',
  63. grant: Page.GRANT_RESTRICTED,
  64. grantedUsers: [testUser0],
  65. creator: testUser0,
  66. },
  67. {
  68. path: '/grant/specified',
  69. grant: Page.GRANT_SPECIFIED,
  70. grantedUsers: [testUser0],
  71. creator: testUser0,
  72. },
  73. {
  74. path: '/grant/owner',
  75. grant: Page.GRANT_OWNER,
  76. grantedUsers: [testUser0],
  77. creator: testUser0,
  78. },
  79. {
  80. path: '/page/for/extended',
  81. grant: Page.GRANT_PUBLIC,
  82. creator: testUser0,
  83. extended: { hoge: 1 },
  84. },
  85. {
  86. path: '/grant/groupacl',
  87. grant: Page.GRANT_USER_GROUP,
  88. grantedUsers: [],
  89. grantedGroup: testGroup0,
  90. creator: testUser1,
  91. },
  92. {
  93. path: '/page1',
  94. grant: Page.GRANT_PUBLIC,
  95. creator: testUser0,
  96. },
  97. {
  98. path: '/page1/child1',
  99. grant: Page.GRANT_PUBLIC,
  100. creator: testUser0,
  101. },
  102. {
  103. path: '/page2',
  104. grant: Page.GRANT_PUBLIC,
  105. creator: testUser0,
  106. },
  107. ]);
  108. done();
  109. });
  110. describe('.isPublic', () => {
  111. describe('with a public page', () => {
  112. test('should return true', (done) => {
  113. Page.findOne({ path: '/grant/public' }, (err, page) => {
  114. expect(err).toBeNull();
  115. expect(page.isPublic()).toEqual(true);
  116. done();
  117. });
  118. });
  119. });
  120. ['restricted', 'specified', 'owner'].forEach((grant) => {
  121. describe(`with a ${grant} page`, () => {
  122. test('should return false', (done) => {
  123. Page.findOne({ path: `/grant/${grant}` }, (err, page) => {
  124. expect(err).toBeNull();
  125. expect(page.isPublic()).toEqual(false);
  126. done();
  127. });
  128. });
  129. });
  130. });
  131. });
  132. describe('.getDeletedPageName', () => {
  133. test('should return trash page name', () => {
  134. expect(Page.getDeletedPageName('/hoge')).toEqual('/trash/hoge');
  135. expect(Page.getDeletedPageName('hoge')).toEqual('/trash/hoge');
  136. });
  137. });
  138. describe('.getRevertDeletedPageName', () => {
  139. test('should return reverted trash page name', () => {
  140. expect(Page.getRevertDeletedPageName('/hoge')).toEqual('/hoge');
  141. expect(Page.getRevertDeletedPageName('/trash/hoge')).toEqual('/hoge');
  142. expect(Page.getRevertDeletedPageName('/trash/hoge/trash')).toEqual('/hoge/trash');
  143. });
  144. });
  145. describe('.isDeletableName', () => {
  146. test('should decide deletable or not', () => {
  147. expect(Page.isDeletableName('/hoge')).toBeTruthy();
  148. expect(Page.isDeletableName('/user/xxx')).toBeFalsy();
  149. expect(Page.isDeletableName('/user/xxx123')).toBeFalsy();
  150. expect(Page.isDeletableName('/user/xxx/')).toBeTruthy();
  151. expect(Page.isDeletableName('/user/xxx/hoge')).toBeTruthy();
  152. });
  153. });
  154. describe('.isCreatableName', () => {
  155. test('should decide creatable or not', () => {
  156. expect(Page.isCreatableName('/hoge')).toBeTruthy();
  157. // edge cases
  158. expect(Page.isCreatableName('/me')).toBeFalsy();
  159. expect(Page.isCreatableName('/me/')).toBeFalsy();
  160. expect(Page.isCreatableName('/me/x')).toBeFalsy();
  161. expect(Page.isCreatableName('/meeting')).toBeTruthy();
  162. expect(Page.isCreatableName('/meeting/x')).toBeTruthy();
  163. // end with "edit"
  164. expect(Page.isCreatableName('/meeting/edit')).toBeFalsy();
  165. // under score
  166. expect(Page.isCreatableName('/_')).toBeTruthy();
  167. expect(Page.isCreatableName('/_template')).toBeTruthy();
  168. expect(Page.isCreatableName('/__template')).toBeTruthy();
  169. expect(Page.isCreatableName('/_r/x')).toBeFalsy();
  170. expect(Page.isCreatableName('/_api')).toBeFalsy();
  171. expect(Page.isCreatableName('/_apix')).toBeFalsy();
  172. expect(Page.isCreatableName('/_api/x')).toBeFalsy();
  173. expect(Page.isCreatableName('/hoge/xx.md')).toBeFalsy();
  174. // start with https?
  175. expect(Page.isCreatableName('/http://demo.growi.org/hoge')).toBeFalsy();
  176. expect(Page.isCreatableName('/https://demo.growi.org/hoge')).toBeFalsy();
  177. expect(Page.isCreatableName('http://demo.growi.org/hoge')).toBeFalsy();
  178. expect(Page.isCreatableName('https://demo.growi.org/hoge')).toBeFalsy();
  179. expect(Page.isCreatableName('/ the / path / with / space')).toBeFalsy();
  180. const forbidden = ['installer', 'register', 'login', 'logout',
  181. 'admin', 'files', 'trash', 'paste', 'comments'];
  182. for (let i = 0; i < forbidden.length; i++) {
  183. const pn = forbidden[i];
  184. expect(Page.isCreatableName(`/${pn}`)).toBeFalsy();
  185. expect(Page.isCreatableName(`/${pn}/`)).toBeFalsy();
  186. expect(Page.isCreatableName(`/${pn}/abc`)).toBeFalsy();
  187. }
  188. });
  189. });
  190. describe('.isAccessiblePageByViewer', () => {
  191. describe('with a granted user', () => {
  192. test('should return true', async() => {
  193. const user = await User.findOne({ email: 'anonymous0@example.com' });
  194. const page = await Page.findOne({ path: '/user/anonymous0/memo' });
  195. const bool = await Page.isAccessiblePageByViewer(page.id, user);
  196. expect(bool).toEqual(true);
  197. });
  198. });
  199. describe('with a public page', () => {
  200. test('should return true', async() => {
  201. const user = await User.findOne({ email: 'anonymous1@example.com' });
  202. const page = await Page.findOne({ path: '/grant/public' });
  203. const bool = await Page.isAccessiblePageByViewer(page.id, user);
  204. expect(bool).toEqual(true);
  205. });
  206. });
  207. describe('with a restricted page and an user who has no grant', () => {
  208. test('should return false', async() => {
  209. const user = await User.findOne({ email: 'anonymous1@example.com' });
  210. const page = await Page.findOne({ path: '/grant/owner' });
  211. const bool = await Page.isAccessiblePageByViewer(page.id, user);
  212. expect(bool).toEqual(false);
  213. });
  214. });
  215. });
  216. describe('Extended field', () => {
  217. describe('Slack Channel.', () => {
  218. test('should be empty', (done) => {
  219. Page.findOne({ path: '/page/for/extended' }, (err, page) => {
  220. expect(page.extended.hoge).toEqual(1);
  221. expect(page.getSlackChannel()).toEqual('');
  222. done();
  223. });
  224. });
  225. test('set slack channel and should get it and should keep hoge ', async() => {
  226. let page = await Page.findOne({ path: '/page/for/extended' });
  227. await page.updateSlackChannel('slack-channel1');
  228. page = await Page.findOne({ path: '/page/for/extended' });
  229. expect(page.extended.hoge).toEqual(1);
  230. expect(page.getSlackChannel()).toEqual('slack-channel1');
  231. });
  232. });
  233. });
  234. // describe('.findPage', () => {
  235. // describe('findByIdAndViewer', () => {
  236. // test('should find page (public)', async() => {
  237. // const pageToFind = createdPages[1];
  238. // const grantedUser = createdUsers[0];
  239. // const page = await Page.findByIdAndViewer(pageToFind.id, grantedUser);
  240. // expect(page).to.be.not.null;
  241. // expect(page.path).to.equal(pageToFind.path);
  242. // });
  243. // test('should find page (anyone knows link)', async() => {
  244. // const pageToFind = createdPages[2];
  245. // const grantedUser = createdUsers[1];
  246. // const page = await Page.findByIdAndViewer(pageToFind.id, grantedUser);
  247. // expect(page).to.be.not.null;
  248. // expect(page.path).to.equal(pageToFind.path);
  249. // });
  250. // test('should find page (just me)', async() => {
  251. // const pageToFind = createdPages[4];
  252. // const grantedUser = createdUsers[0];
  253. // const page = await Page.findByIdAndViewer(pageToFind.id, grantedUser);
  254. // expect(page).to.be.not.null;
  255. // expect(page.path).to.equal(pageToFind.path);
  256. // });
  257. // test('should not be found by grant (just me)', async() => {
  258. // const pageToFind = createdPages[4];
  259. // const grantedUser = createdUsers[1];
  260. // const page = await Page.findByIdAndViewer(pageToFind.id, grantedUser);
  261. // expect(page).toBeNull();
  262. // });
  263. // });
  264. // describe('findByIdAndViewer granted userGroup', () => {
  265. // test('should find page', async() => {
  266. // const pageToFind = createdPages[6];
  267. // const grantedUser = createdUsers[0];
  268. // const page = await Page.findByIdAndViewer(pageToFind.id, grantedUser);
  269. // expect(page).to.be.not.null;
  270. // expect(page.path).to.equal(pageToFind.path);
  271. // });
  272. // test('should not be found by grant', async() => {
  273. // const pageToFind = createdPages[6];
  274. // const grantedUser = createdUsers[2];
  275. // const page = await Page.findByIdAndViewer(pageToFind.id, grantedUser);
  276. // expect(page).toBeNull();
  277. // });
  278. // });
  279. // });
  280. // describe('findListWithDescendants', () => {
  281. // test('should return only /page/', async() => {
  282. // const user = createdUsers[0];
  283. // const result = await Page.findListWithDescendants('/page/', user, { isRegExpEscapedFromPath: true });
  284. // // assert totalCount
  285. // expect(result.totalCount).to.equal(1);
  286. // // assert paths
  287. // const pagePaths = result.pages.map((page) => { return page.path });
  288. // expect(pagePaths).to.include.members(['/page/for/extended']);
  289. // });
  290. // test('should return only /page1/', async() => {
  291. // const user = createdUsers[0];
  292. // const result = await Page.findListWithDescendants('/page1/', user, { isRegExpEscapedFromPath: true });
  293. // // assert totalCount
  294. // expect(result.totalCount).to.equal(2);
  295. // // assert paths
  296. // const pagePaths = result.pages.map((page) => { return page.path });
  297. // expect(pagePaths).to.include.members(['/page1', '/page1/child1']);
  298. // });
  299. // });
  300. // describe('findListByStartWith', () => {
  301. // test('should return pages which starts with /page', async() => {
  302. // const user = createdUsers[0];
  303. // const result = await Page.findListByStartWith('/page', user, {});
  304. // // assert totalCount
  305. // expect(result.totalCount).to.equal(4);
  306. // // assert paths
  307. // const pagePaths = result.pages.map((page) => { return page.path });
  308. // expect(pagePaths).to.include.members(['/page/for/extended', '/page1', '/page1/child1', '/page2']);
  309. // });
  310. // test('should process with regexp', async() => {
  311. // const user = createdUsers[0];
  312. // const result = await Page.findListByStartWith('/page\\d{1}/', user, {});
  313. // // assert totalCount
  314. // expect(result.totalCount).to.equal(3);
  315. // // assert paths
  316. // const pagePaths = result.pages.map((page) => { return page.path });
  317. // expect(pagePaths).to.include.members(['/page1', '/page1/child1', '/page2']);
  318. // });
  319. // });
  320. });