mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2024-11-29 07:54:28 -05:00
test: default permissions on newly created notes
Signed-off-by: Philip Molares <philip.molares@udo.edu> Signed-off-by: Tilman Vatteroth <git@tilmanvatteroth.de>
This commit is contained in:
parent
16c6f6e572
commit
f11f3b0931
1 changed files with 131 additions and 8 deletions
|
@ -10,10 +10,15 @@ import { DataSource, EntityManager, Repository } from 'typeorm';
|
||||||
|
|
||||||
import { AuthToken } from '../auth/auth-token.entity';
|
import { AuthToken } from '../auth/auth-token.entity';
|
||||||
import { Author } from '../authors/author.entity';
|
import { Author } from '../authors/author.entity';
|
||||||
|
import { DefaultAccessPermission } from '../config/default-access-permission.enum';
|
||||||
import appConfigMock from '../config/mock/app.config.mock';
|
import appConfigMock from '../config/mock/app.config.mock';
|
||||||
import authConfigMock from '../config/mock/auth.config.mock';
|
import authConfigMock from '../config/mock/auth.config.mock';
|
||||||
import databaseConfigMock from '../config/mock/database.config.mock';
|
import databaseConfigMock from '../config/mock/database.config.mock';
|
||||||
import noteConfigMock from '../config/mock/note.config.mock';
|
import {
|
||||||
|
createDefaultMockNoteConfig,
|
||||||
|
registerNoteConfig,
|
||||||
|
} from '../config/mock/note.config.mock';
|
||||||
|
import { NoteConfig } from '../config/note.config';
|
||||||
import {
|
import {
|
||||||
AlreadyInDBError,
|
AlreadyInDBError,
|
||||||
ForbiddenIdError,
|
ForbiddenIdError,
|
||||||
|
@ -21,6 +26,7 @@ import {
|
||||||
} from '../errors/errors';
|
} from '../errors/errors';
|
||||||
import { Group } from '../groups/group.entity';
|
import { Group } from '../groups/group.entity';
|
||||||
import { GroupsModule } from '../groups/groups.module';
|
import { GroupsModule } from '../groups/groups.module';
|
||||||
|
import { SpecialGroup } from '../groups/groups.special';
|
||||||
import { Identity } from '../identity/identity.entity';
|
import { Identity } from '../identity/identity.entity';
|
||||||
import { LoggerModule } from '../logger/logger.module';
|
import { LoggerModule } from '../logger/logger.module';
|
||||||
import { NoteGroupPermission } from '../permissions/note-group-permission.entity';
|
import { NoteGroupPermission } from '../permissions/note-group-permission.entity';
|
||||||
|
@ -40,10 +46,24 @@ import { Tag } from './tag.entity';
|
||||||
|
|
||||||
describe('NotesService', () => {
|
describe('NotesService', () => {
|
||||||
let service: NotesService;
|
let service: NotesService;
|
||||||
|
const noteMockConfig: NoteConfig = createDefaultMockNoteConfig();
|
||||||
let noteRepo: Repository<Note>;
|
let noteRepo: Repository<Note>;
|
||||||
let revisionRepo: Repository<Revision>;
|
let revisionRepo: Repository<Revision>;
|
||||||
let userRepo: Repository<User>;
|
let userRepo: Repository<User>;
|
||||||
|
let groupRepo: Repository<Group>;
|
||||||
let forbiddenNoteId: string;
|
let forbiddenNoteId: string;
|
||||||
|
let everyoneDefaultAccessPermission: string;
|
||||||
|
let loggedinDefaultAccessPermission: string;
|
||||||
|
const everyone = Group.create(
|
||||||
|
SpecialGroup.EVERYONE,
|
||||||
|
SpecialGroup.EVERYONE,
|
||||||
|
true,
|
||||||
|
);
|
||||||
|
const loggedin = Group.create(
|
||||||
|
SpecialGroup.LOGGED_IN,
|
||||||
|
SpecialGroup.LOGGED_IN,
|
||||||
|
true,
|
||||||
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a Note and a corresponding User and Group for testing.
|
* Creates a Note and a corresponding User and Group for testing.
|
||||||
|
@ -60,6 +80,10 @@ describe('NotesService', () => {
|
||||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
.mockImplementation(async (note: Note): Promise<Note> => note);
|
.mockImplementation(async (note: Note): Promise<Note> => note);
|
||||||
|
jest
|
||||||
|
.spyOn(groupRepo, 'findOne')
|
||||||
|
.mockResolvedValueOnce(everyone as Group)
|
||||||
|
.mockResolvedValueOnce(loggedin as Group);
|
||||||
const note = await service.createNote(content, null);
|
const note = await service.createNote(content, null);
|
||||||
const revisions = await note.revisions;
|
const revisions = await note.revisions;
|
||||||
revisions[0].edits = Promise.resolve([
|
revisions[0].edits = Promise.resolve([
|
||||||
|
@ -148,6 +172,16 @@ describe('NotesService', () => {
|
||||||
),
|
),
|
||||||
undefined,
|
undefined,
|
||||||
);
|
);
|
||||||
|
groupRepo = new Repository<Group>(
|
||||||
|
'',
|
||||||
|
new EntityManager(
|
||||||
|
new DataSource({
|
||||||
|
type: 'sqlite',
|
||||||
|
database: ':memory:',
|
||||||
|
}),
|
||||||
|
),
|
||||||
|
undefined,
|
||||||
|
);
|
||||||
const module: TestingModule = await Test.createTestingModule({
|
const module: TestingModule = await Test.createTestingModule({
|
||||||
providers: [
|
providers: [
|
||||||
NotesService,
|
NotesService,
|
||||||
|
@ -168,6 +202,10 @@ describe('NotesService', () => {
|
||||||
provide: getRepositoryToken(User),
|
provide: getRepositoryToken(User),
|
||||||
useValue: userRepo,
|
useValue: userRepo,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
provide: getRepositoryToken(Group),
|
||||||
|
useValue: groupRepo,
|
||||||
|
},
|
||||||
],
|
],
|
||||||
imports: [
|
imports: [
|
||||||
LoggerModule,
|
LoggerModule,
|
||||||
|
@ -181,7 +219,7 @@ describe('NotesService', () => {
|
||||||
appConfigMock,
|
appConfigMock,
|
||||||
databaseConfigMock,
|
databaseConfigMock,
|
||||||
authConfigMock,
|
authConfigMock,
|
||||||
noteConfigMock,
|
registerNoteConfig(noteMockConfig),
|
||||||
],
|
],
|
||||||
}),
|
}),
|
||||||
],
|
],
|
||||||
|
@ -207,7 +245,7 @@ describe('NotesService', () => {
|
||||||
.overrideProvider(getRepositoryToken(NoteUserPermission))
|
.overrideProvider(getRepositoryToken(NoteUserPermission))
|
||||||
.useValue({})
|
.useValue({})
|
||||||
.overrideProvider(getRepositoryToken(Group))
|
.overrideProvider(getRepositoryToken(Group))
|
||||||
.useClass(Repository)
|
.useValue(groupRepo)
|
||||||
.overrideProvider(getRepositoryToken(Session))
|
.overrideProvider(getRepositoryToken(Session))
|
||||||
.useValue({})
|
.useValue({})
|
||||||
.overrideProvider(getRepositoryToken(Author))
|
.overrideProvider(getRepositoryToken(Author))
|
||||||
|
@ -215,7 +253,12 @@ describe('NotesService', () => {
|
||||||
.compile();
|
.compile();
|
||||||
|
|
||||||
const config = module.get<ConfigService>(ConfigService);
|
const config = module.get<ConfigService>(ConfigService);
|
||||||
forbiddenNoteId = config.get('noteConfig').forbiddenNoteIds[0];
|
const noteConfig = config.get<NoteConfig>('noteConfig') as NoteConfig;
|
||||||
|
forbiddenNoteId = noteConfig.forbiddenNoteIds[0];
|
||||||
|
everyoneDefaultAccessPermission =
|
||||||
|
noteConfig.permissions.accessDefault.everyone;
|
||||||
|
loggedinDefaultAccessPermission =
|
||||||
|
noteConfig.permissions.accessDefault.loggedIn;
|
||||||
service = module.get<NotesService>(NotesService);
|
service = module.get<NotesService>(NotesService);
|
||||||
noteRepo = module.get<Repository<Note>>(getRepositoryToken(Note));
|
noteRepo = module.get<Repository<Note>>(getRepositoryToken(Note));
|
||||||
revisionRepo = module.get<Repository<Revision>>(
|
revisionRepo = module.get<Repository<Revision>>(
|
||||||
|
@ -297,6 +340,10 @@ describe('NotesService', () => {
|
||||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
.mockImplementation(async (note: Note): Promise<Note> => note);
|
.mockImplementation(async (note: Note): Promise<Note> => note);
|
||||||
|
jest
|
||||||
|
.spyOn(groupRepo, 'findOne')
|
||||||
|
.mockResolvedValueOnce(everyone as Group)
|
||||||
|
.mockResolvedValueOnce(loggedin as Group);
|
||||||
});
|
});
|
||||||
it('without alias, without owner', async () => {
|
it('without alias, without owner', async () => {
|
||||||
const newNote = await service.createNote(content, null);
|
const newNote = await service.createNote(content, null);
|
||||||
|
@ -305,7 +352,16 @@ describe('NotesService', () => {
|
||||||
expect(revisions[0].content).toEqual(content);
|
expect(revisions[0].content).toEqual(content);
|
||||||
expect(await newNote.historyEntries).toHaveLength(0);
|
expect(await newNote.historyEntries).toHaveLength(0);
|
||||||
expect(await newNote.userPermissions).toHaveLength(0);
|
expect(await newNote.userPermissions).toHaveLength(0);
|
||||||
expect(await newNote.groupPermissions).toHaveLength(0);
|
const groupPermissions = await newNote.groupPermissions;
|
||||||
|
expect(groupPermissions).toHaveLength(2);
|
||||||
|
expect(groupPermissions[0].canEdit).toEqual(
|
||||||
|
everyoneDefaultAccessPermission === DefaultAccessPermission.WRITE,
|
||||||
|
);
|
||||||
|
expect(groupPermissions[0].group.name).toEqual(SpecialGroup.EVERYONE);
|
||||||
|
expect(groupPermissions[1].canEdit).toEqual(
|
||||||
|
loggedinDefaultAccessPermission === DefaultAccessPermission.WRITE,
|
||||||
|
);
|
||||||
|
expect(groupPermissions[1].group.name).toEqual(SpecialGroup.LOGGED_IN);
|
||||||
expect(await newNote.tags).toHaveLength(0);
|
expect(await newNote.tags).toHaveLength(0);
|
||||||
expect(await newNote.owner).toBeNull();
|
expect(await newNote.owner).toBeNull();
|
||||||
expect(await newNote.aliases).toHaveLength(0);
|
expect(await newNote.aliases).toHaveLength(0);
|
||||||
|
@ -318,7 +374,16 @@ describe('NotesService', () => {
|
||||||
expect(await newNote.historyEntries).toHaveLength(1);
|
expect(await newNote.historyEntries).toHaveLength(1);
|
||||||
expect(await (await newNote.historyEntries)[0].user).toEqual(user);
|
expect(await (await newNote.historyEntries)[0].user).toEqual(user);
|
||||||
expect(await newNote.userPermissions).toHaveLength(0);
|
expect(await newNote.userPermissions).toHaveLength(0);
|
||||||
expect(await newNote.groupPermissions).toHaveLength(0);
|
const groupPermissions = await newNote.groupPermissions;
|
||||||
|
expect(groupPermissions).toHaveLength(2);
|
||||||
|
expect(groupPermissions[0].canEdit).toEqual(
|
||||||
|
everyoneDefaultAccessPermission === DefaultAccessPermission.WRITE,
|
||||||
|
);
|
||||||
|
expect(groupPermissions[0].group.name).toEqual(SpecialGroup.EVERYONE);
|
||||||
|
expect(groupPermissions[1].canEdit).toEqual(
|
||||||
|
loggedinDefaultAccessPermission === DefaultAccessPermission.WRITE,
|
||||||
|
);
|
||||||
|
expect(groupPermissions[1].group.name).toEqual(SpecialGroup.LOGGED_IN);
|
||||||
expect(await newNote.tags).toHaveLength(0);
|
expect(await newNote.tags).toHaveLength(0);
|
||||||
expect(await newNote.owner).toEqual(user);
|
expect(await newNote.owner).toEqual(user);
|
||||||
expect(await newNote.aliases).toHaveLength(0);
|
expect(await newNote.aliases).toHaveLength(0);
|
||||||
|
@ -330,7 +395,16 @@ describe('NotesService', () => {
|
||||||
expect(revisions[0].content).toEqual(content);
|
expect(revisions[0].content).toEqual(content);
|
||||||
expect(await newNote.historyEntries).toHaveLength(0);
|
expect(await newNote.historyEntries).toHaveLength(0);
|
||||||
expect(await newNote.userPermissions).toHaveLength(0);
|
expect(await newNote.userPermissions).toHaveLength(0);
|
||||||
expect(await newNote.groupPermissions).toHaveLength(0);
|
const groupPermissions = await newNote.groupPermissions;
|
||||||
|
expect(groupPermissions).toHaveLength(2);
|
||||||
|
expect(groupPermissions[0].canEdit).toEqual(
|
||||||
|
everyoneDefaultAccessPermission === DefaultAccessPermission.WRITE,
|
||||||
|
);
|
||||||
|
expect(groupPermissions[0].group.name).toEqual(SpecialGroup.EVERYONE);
|
||||||
|
expect(groupPermissions[1].canEdit).toEqual(
|
||||||
|
loggedinDefaultAccessPermission === DefaultAccessPermission.WRITE,
|
||||||
|
);
|
||||||
|
expect(groupPermissions[1].group.name).toEqual(SpecialGroup.LOGGED_IN);
|
||||||
expect(await newNote.tags).toHaveLength(0);
|
expect(await newNote.tags).toHaveLength(0);
|
||||||
expect(await newNote.owner).toBeNull();
|
expect(await newNote.owner).toBeNull();
|
||||||
expect(await newNote.aliases).toHaveLength(1);
|
expect(await newNote.aliases).toHaveLength(1);
|
||||||
|
@ -343,12 +417,53 @@ describe('NotesService', () => {
|
||||||
expect(await newNote.historyEntries).toHaveLength(1);
|
expect(await newNote.historyEntries).toHaveLength(1);
|
||||||
expect(await (await newNote.historyEntries)[0].user).toEqual(user);
|
expect(await (await newNote.historyEntries)[0].user).toEqual(user);
|
||||||
expect(await newNote.userPermissions).toHaveLength(0);
|
expect(await newNote.userPermissions).toHaveLength(0);
|
||||||
expect(await newNote.groupPermissions).toHaveLength(0);
|
const groupPermissions = await newNote.groupPermissions;
|
||||||
|
expect(groupPermissions).toHaveLength(2);
|
||||||
|
expect(groupPermissions[0].canEdit).toEqual(
|
||||||
|
everyoneDefaultAccessPermission === DefaultAccessPermission.WRITE,
|
||||||
|
);
|
||||||
|
expect(groupPermissions[0].group.name).toEqual(SpecialGroup.EVERYONE);
|
||||||
|
expect(groupPermissions[1].canEdit).toEqual(
|
||||||
|
loggedinDefaultAccessPermission === DefaultAccessPermission.WRITE,
|
||||||
|
);
|
||||||
|
expect(groupPermissions[1].group.name).toEqual(SpecialGroup.LOGGED_IN);
|
||||||
expect(await newNote.tags).toHaveLength(0);
|
expect(await newNote.tags).toHaveLength(0);
|
||||||
expect(await newNote.owner).toEqual(user);
|
expect(await newNote.owner).toEqual(user);
|
||||||
expect(await newNote.aliases).toHaveLength(1);
|
expect(await newNote.aliases).toHaveLength(1);
|
||||||
expect((await newNote.aliases)[0].name).toEqual(alias);
|
expect((await newNote.aliases)[0].name).toEqual(alias);
|
||||||
});
|
});
|
||||||
|
describe('with other', () => {
|
||||||
|
beforeEach(
|
||||||
|
() =>
|
||||||
|
(noteMockConfig.permissions.accessDefault.everyone =
|
||||||
|
DefaultAccessPermission.NONE),
|
||||||
|
);
|
||||||
|
it('default permissions', async () => {
|
||||||
|
jest.spyOn(groupRepo, 'findOne').mockReset();
|
||||||
|
jest
|
||||||
|
.spyOn(groupRepo, 'findOne')
|
||||||
|
.mockResolvedValueOnce(loggedin as Group);
|
||||||
|
const newNote = await service.createNote(content, user, alias);
|
||||||
|
const revisions = await newNote.revisions;
|
||||||
|
expect(revisions).toHaveLength(1);
|
||||||
|
expect(revisions[0].content).toEqual(content);
|
||||||
|
expect(await newNote.historyEntries).toHaveLength(1);
|
||||||
|
expect((await newNote.historyEntries)[0].user).toEqual(user);
|
||||||
|
expect(await newNote.userPermissions).toHaveLength(0);
|
||||||
|
const groupPermissions = await newNote.groupPermissions;
|
||||||
|
expect(groupPermissions).toHaveLength(1);
|
||||||
|
expect(groupPermissions[0].canEdit).toEqual(
|
||||||
|
loggedinDefaultAccessPermission === DefaultAccessPermission.WRITE,
|
||||||
|
);
|
||||||
|
expect(groupPermissions[0].group.name).toEqual(
|
||||||
|
SpecialGroup.LOGGED_IN,
|
||||||
|
);
|
||||||
|
expect(await newNote.tags).toHaveLength(0);
|
||||||
|
expect(await newNote.owner).toEqual(user);
|
||||||
|
expect(await newNote.aliases).toHaveLength(1);
|
||||||
|
expect((await newNote.aliases)[0].name).toEqual(alias);
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
describe('fails:', () => {
|
describe('fails:', () => {
|
||||||
it('alias is forbidden', async () => {
|
it('alias is forbidden', async () => {
|
||||||
|
@ -358,6 +473,10 @@ describe('NotesService', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('alias is already used', async () => {
|
it('alias is already used', async () => {
|
||||||
|
jest
|
||||||
|
.spyOn(groupRepo, 'findOne')
|
||||||
|
.mockResolvedValueOnce(everyone as Group)
|
||||||
|
.mockResolvedValueOnce(loggedin as Group);
|
||||||
jest.spyOn(noteRepo, 'save').mockImplementationOnce(async () => {
|
jest.spyOn(noteRepo, 'save').mockImplementationOnce(async () => {
|
||||||
throw new Error();
|
throw new Error();
|
||||||
});
|
});
|
||||||
|
@ -376,6 +495,10 @@ describe('NotesService', () => {
|
||||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
.mockImplementation(async (note: Note): Promise<Note> => note);
|
.mockImplementation(async (note: Note): Promise<Note> => note);
|
||||||
|
jest
|
||||||
|
.spyOn(groupRepo, 'findOne')
|
||||||
|
.mockResolvedValueOnce(everyone as Group)
|
||||||
|
.mockResolvedValueOnce(loggedin as Group);
|
||||||
const newNote = await service.createNote(content, null);
|
const newNote = await service.createNote(content, null);
|
||||||
const revisions = await newNote.revisions;
|
const revisions = await newNote.revisions;
|
||||||
jest.spyOn(revisionRepo, 'findOne').mockResolvedValueOnce(revisions[0]);
|
jest.spyOn(revisionRepo, 'findOne').mockResolvedValueOnce(revisions[0]);
|
||||||
|
|
Loading…
Reference in a new issue