| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496 |
- const mongoose = require('mongoose');
- const { getInstance } = require('../setup-crowi');
- let testUser0;
- let testUser1;
- let testUser2;
- let testGroup0;
- let parentPage;
- describe('Page', () => {
- // biome-ignore lint/correctness/noUnusedVariables: ignore
- let crowi;
- let Page;
- let PageQueryBuilder;
- let User;
- let UserGroup;
- let UserGroupRelation;
- beforeAll(async () => {
- crowi = await getInstance();
- User = mongoose.model('User');
- UserGroup = mongoose.model('UserGroup');
- UserGroupRelation = mongoose.model('UserGroupRelation');
- Page = mongoose.model('Page');
- PageQueryBuilder = Page.PageQueryBuilder;
- await User.insertMany([
- {
- name: 'Anon 0',
- username: 'anonymous0',
- email: 'anonymous0@example.com',
- },
- {
- name: 'Anon 1',
- username: 'anonymous1',
- email: 'anonymous1@example.com',
- },
- {
- name: 'Anon 2',
- username: 'anonymous2',
- email: 'anonymous2@example.com',
- },
- ]);
- await UserGroup.insertMany([
- { name: 'TestGroup0' },
- { name: 'TestGroup1' },
- ]);
- testUser0 = await User.findOne({ username: 'anonymous0' });
- testUser1 = await User.findOne({ username: 'anonymous1' });
- testUser2 = await User.findOne({ username: 'anonymous2' });
- testGroup0 = await UserGroup.findOne({ name: 'TestGroup0' });
- await UserGroupRelation.insertMany([
- {
- relatedGroup: testGroup0,
- relatedUser: testUser0,
- },
- {
- relatedGroup: testGroup0,
- relatedUser: testUser1,
- },
- ]);
- await Page.insertMany([
- {
- path: '/user/anonymous0/memo',
- grant: Page.GRANT_RESTRICTED,
- grantedUsers: [testUser0],
- creator: testUser0,
- },
- {
- path: '/grant',
- grant: Page.GRANT_PUBLIC,
- grantedUsers: [testUser0],
- creator: testUser0,
- },
- {
- path: '/grant/public',
- grant: Page.GRANT_PUBLIC,
- grantedUsers: [testUser0],
- creator: testUser0,
- },
- {
- path: '/grant/restricted',
- grant: Page.GRANT_RESTRICTED,
- grantedUsers: [testUser0],
- creator: testUser0,
- },
- {
- path: '/grant/specified',
- grant: Page.GRANT_SPECIFIED,
- grantedUsers: [testUser0],
- creator: testUser0,
- },
- {
- path: '/grant/owner',
- grant: Page.GRANT_OWNER,
- grantedUsers: [testUser0],
- creator: testUser0,
- },
- {
- path: '/page/child/without/parents',
- grant: Page.GRANT_PUBLIC,
- creator: testUser0,
- },
- {
- path: '/grant/groupacl',
- grant: Page.GRANT_USER_GROUP,
- grantedUsers: [],
- grantedGroups: [{ item: testGroup0, type: 'UserGroup' }],
- creator: testUser1,
- },
- {
- path: '/page1',
- grant: Page.GRANT_PUBLIC,
- creator: testUser0,
- },
- {
- path: '/page1/child1',
- grant: Page.GRANT_PUBLIC,
- creator: testUser0,
- },
- {
- path: '/page2',
- grant: Page.GRANT_PUBLIC,
- creator: testUser0,
- },
- ]);
- parentPage = await Page.findOne({ path: '/grant' });
- });
- describe('.isPublic', () => {
- describe('with a public page', () => {
- test('should return true', async () => {
- const page = await Page.findOne({ path: '/grant/public' });
- expect(page.isPublic()).toEqual(true);
- });
- });
- ['restricted', 'specified', 'owner'].forEach((grant) => {
- describe(`with a ${grant} page`, () => {
- test('should return false', async () => {
- const page = await Page.findOne({ path: `/grant/${grant}` });
- expect(page.isPublic()).toEqual(false);
- });
- });
- });
- });
- describe('.getDeletedPageName', () => {
- test('should return trash page name', () => {
- expect(Page.getDeletedPageName('/hoge')).toEqual('/trash/hoge');
- expect(Page.getDeletedPageName('hoge')).toEqual('/trash/hoge');
- });
- });
- describe('.getRevertDeletedPageName', () => {
- test('should return reverted trash page name', () => {
- expect(Page.getRevertDeletedPageName('/hoge')).toEqual('/hoge');
- expect(Page.getRevertDeletedPageName('/trash/hoge')).toEqual('/hoge');
- expect(Page.getRevertDeletedPageName('/trash/hoge/trash')).toEqual(
- '/hoge/trash',
- );
- });
- });
- describe('.isAccessiblePageByViewer', () => {
- describe('with a granted page', () => {
- test('should return true with granted user', async () => {
- const user = await User.findOne({ email: 'anonymous0@example.com' });
- const page = await Page.findOne({ path: '/user/anonymous0/memo' });
- const bool = await Page.isAccessiblePageByViewer(page.id, user);
- expect(bool).toEqual(true);
- });
- test('should return false without user', async () => {
- const user = null;
- const page = await Page.findOne({ path: '/user/anonymous0/memo' });
- const bool = await Page.isAccessiblePageByViewer(page.id, user);
- expect(bool).toEqual(true);
- });
- });
- describe('with a public page', () => {
- test('should return true with user', async () => {
- const user = await User.findOne({ email: 'anonymous1@example.com' });
- const page = await Page.findOne({ path: '/grant/public' });
- const bool = await Page.isAccessiblePageByViewer(page.id, user);
- expect(bool).toEqual(true);
- });
- test('should return true with out', async () => {
- const user = null;
- const page = await Page.findOne({ path: '/grant/public' });
- const bool = await Page.isAccessiblePageByViewer(page.id, user);
- expect(bool).toEqual(true);
- });
- });
- describe('with a restricted page', () => {
- test('should return false with user who has no grant', async () => {
- const user = await User.findOne({ email: 'anonymous1@example.com' });
- const page = await Page.findOne({ path: '/grant/owner' });
- const bool = await Page.isAccessiblePageByViewer(page.id, user);
- expect(bool).toEqual(false);
- });
- test('should return false without user', async () => {
- const user = null;
- const page = await Page.findOne({ path: '/grant/owner' });
- const bool = await Page.isAccessiblePageByViewer(page.id, user);
- expect(bool).toEqual(false);
- });
- });
- });
- describe('.findPage', () => {
- describe('findByIdAndViewer', () => {
- test('should find page (public)', async () => {
- const expectedPage = await Page.findOne({ path: '/grant/public' });
- const page = await Page.findByIdAndViewer(expectedPage.id, testUser0);
- expect(page).not.toBeNull();
- expect(page.path).toEqual(expectedPage.path);
- });
- test('should find page (anyone knows link)', async () => {
- const expectedPage = await Page.findOne({ path: '/grant/restricted' });
- const page = await Page.findByIdAndViewer(expectedPage.id, testUser1);
- expect(page).not.toBeNull();
- expect(page.path).toEqual(expectedPage.path);
- });
- test('should find page (only me)', async () => {
- const expectedPage = await Page.findOne({ path: '/grant/owner' });
- const page = await Page.findByIdAndViewer(expectedPage.id, testUser0);
- expect(page).not.toBeNull();
- expect(page.path).toEqual(expectedPage.path);
- });
- test('should not be found by grant (only me)', async () => {
- const expectedPage = await Page.findOne({ path: '/grant/owner' });
- const page = await Page.findByIdAndViewer(expectedPage.id, testUser1);
- expect(page).toBeNull();
- });
- });
- describe('findByIdAndViewer granted userGroup', () => {
- test('should find page', async () => {
- const expectedPage = await Page.findOne({ path: '/grant/groupacl' });
- const page = await Page.findByIdAndViewer(expectedPage.id, testUser0);
- expect(page).not.toBeNull();
- expect(page.path).toEqual(expectedPage.path);
- });
- test('should not be found by grant', async () => {
- const expectedPage = await Page.findOne({ path: '/grant/groupacl' });
- const page = await Page.findByIdAndViewer(expectedPage.id, testUser2);
- expect(page).toBeNull();
- });
- });
- });
- describe('PageQueryBuilder.addConditionToListWithDescendants', () => {
- test('can retrieve descendants of /page', async () => {
- const builder = new PageQueryBuilder(Page.find());
- builder.addConditionToListWithDescendants('/page');
- const result = await builder.query.exec();
- // assert totalCount
- expect(result.length).toEqual(1);
- // assert paths
- const pagePaths = result.map((page) => {
- return page.path;
- });
- expect(pagePaths).toContainEqual('/page/child/without/parents');
- });
- test('can retrieve descendants of /page1', async () => {
- const builder = new PageQueryBuilder(Page.find());
- builder.addConditionToListWithDescendants('/page1/');
- const result = await builder.query.exec();
- // assert totalCount
- expect(result.length).toEqual(2);
- // assert paths
- const pagePaths = result.map((page) => {
- return page.path;
- });
- expect(pagePaths).toContainEqual('/page1');
- expect(pagePaths).toContainEqual('/page1/child1');
- });
- });
- describe('PageQueryBuilder.addConditionToListOnlyDescendants', () => {
- test('can retrieve only descendants of /page', async () => {
- const builder = new PageQueryBuilder(Page.find());
- builder.addConditionToListOnlyDescendants('/page');
- const result = await builder.query.exec();
- // assert totalCount
- expect(result.length).toEqual(1);
- // assert paths
- const pagePaths = result.map((page) => {
- return page.path;
- });
- expect(pagePaths).toContainEqual('/page/child/without/parents');
- });
- test('can retrieve only descendants of /page1', async () => {
- const builder = new PageQueryBuilder(Page.find());
- builder.addConditionToListOnlyDescendants('/page1');
- const result = await builder.query.exec();
- // assert totalCount
- expect(result.length).toEqual(1);
- // assert paths
- const pagePaths = result.map((page) => {
- return page.path;
- });
- expect(pagePaths).toContainEqual('/page1/child1');
- });
- });
- describe('PageQueryBuilder.addConditionToListByStartWith', () => {
- test('can retrieve pages which starts with /page', async () => {
- const builder = new PageQueryBuilder(Page.find());
- builder.addConditionToListByStartWith('/page');
- const result = await builder.query.exec();
- // assert totalCount
- expect(result.length).toEqual(4);
- // assert paths
- const pagePaths = result.map((page) => {
- return page.path;
- });
- expect(pagePaths).toContainEqual('/page/child/without/parents');
- expect(pagePaths).toContainEqual('/page1');
- expect(pagePaths).toContainEqual('/page1/child1');
- expect(pagePaths).toContainEqual('/page2');
- });
- });
- describe('.findListWithDescendants', () => {
- test('can retrieve all pages with testUser0', async () => {
- const result = await Page.findListWithDescendants('/grant', testUser0);
- const { pages } = result;
- // assert totalCount
- expect(pages.length).toEqual(5);
- // assert paths
- const pagePaths = await pages.map((page) => {
- return page.path;
- });
- expect(pagePaths).toContainEqual('/grant/groupacl');
- expect(pagePaths).toContainEqual('/grant/specified');
- expect(pagePaths).toContainEqual('/grant/owner');
- expect(pagePaths).toContainEqual('/grant/public');
- expect(pagePaths).toContainEqual('/grant');
- });
- test('can retrieve all pages with testUser1', async () => {
- const result = await Page.findListWithDescendants('/grant', testUser1);
- const { pages } = result;
- // assert totalCount
- expect(pages.length).toEqual(5);
- // assert paths
- const pagePaths = await pages.map((page) => {
- return page.path;
- });
- expect(pagePaths).toContainEqual('/grant/groupacl');
- expect(pagePaths).toContainEqual('/grant/specified');
- expect(pagePaths).toContainEqual('/grant/owner');
- expect(pagePaths).toContainEqual('/grant/public');
- expect(pagePaths).toContainEqual('/grant');
- });
- test('can retrieve all pages with testUser2', async () => {
- const result = await Page.findListWithDescendants('/grant', testUser2);
- const { pages } = result;
- // assert totalCount
- expect(pages.length).toEqual(5);
- // assert paths
- const pagePaths = await pages.map((page) => {
- return page.path;
- });
- expect(pagePaths).toContainEqual('/grant/groupacl');
- expect(pagePaths).toContainEqual('/grant/specified');
- expect(pagePaths).toContainEqual('/grant/owner');
- expect(pagePaths).toContainEqual('/grant/public');
- expect(pagePaths).toContainEqual('/grant');
- });
- test('can retrieve all pages without user', async () => {
- const result = await Page.findListWithDescendants('/grant', null);
- const { pages } = result;
- // assert totalCount
- expect(pages.length).toEqual(5);
- // assert paths
- const pagePaths = await pages.map((page) => {
- return page.path;
- });
- expect(pagePaths).toContainEqual('/grant/groupacl');
- expect(pagePaths).toContainEqual('/grant/specified');
- expect(pagePaths).toContainEqual('/grant/owner');
- expect(pagePaths).toContainEqual('/grant/public');
- expect(pagePaths).toContainEqual('/grant');
- });
- });
- describe('.findManageableListWithDescendants', () => {
- test('can retrieve all pages with testUser0', async () => {
- const pages = await Page.findManageableListWithDescendants(
- parentPage,
- testUser0,
- );
- // assert totalCount
- expect(pages.length).toEqual(5);
- // assert paths
- const pagePaths = await pages.map((page) => {
- return page.path;
- });
- expect(pagePaths).toContainEqual('/grant/groupacl');
- expect(pagePaths).toContainEqual('/grant/specified');
- expect(pagePaths).toContainEqual('/grant/owner');
- expect(pagePaths).toContainEqual('/grant/public');
- expect(pagePaths).toContainEqual('/grant');
- });
- test('can retrieve group page and public page which starts with testUser1', async () => {
- const pages = await Page.findManageableListWithDescendants(
- parentPage,
- testUser1,
- );
- // assert totalCount
- expect(pages.length).toEqual(3);
- // assert paths
- const pagePaths = await pages.map((page) => {
- return page.path;
- });
- expect(pagePaths).toContainEqual('/grant/groupacl');
- expect(pagePaths).toContainEqual('/grant/public');
- expect(pagePaths).toContainEqual('/grant');
- });
- test('can retrieve only public page which starts with testUser2', async () => {
- const pages = await Page.findManageableListWithDescendants(
- parentPage,
- testUser2,
- );
- // assert totalCount
- expect(pages.length).toEqual(2);
- // assert paths
- const pagePaths = await pages.map((page) => {
- return page.path;
- });
- expect(pagePaths).toContainEqual('/grant/public');
- expect(pagePaths).toContainEqual('/grant');
- });
- test('can retrieve only public page which starts without user', async () => {
- const pages = await Page.findManageableListWithDescendants(
- parentPage,
- null,
- );
- // assert totalCount
- expect(pages).toBeNull();
- });
- });
- });
|