mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2024-11-25 11:16:31 -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 { Author } from '../authors/author.entity';
|
||||
import { DefaultAccessPermission } from '../config/default-access-permission.enum';
|
||||
import appConfigMock from '../config/mock/app.config.mock';
|
||||
import authConfigMock from '../config/mock/auth.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 {
|
||||
AlreadyInDBError,
|
||||
ForbiddenIdError,
|
||||
|
@ -21,6 +26,7 @@ import {
|
|||
} from '../errors/errors';
|
||||
import { Group } from '../groups/group.entity';
|
||||
import { GroupsModule } from '../groups/groups.module';
|
||||
import { SpecialGroup } from '../groups/groups.special';
|
||||
import { Identity } from '../identity/identity.entity';
|
||||
import { LoggerModule } from '../logger/logger.module';
|
||||
import { NoteGroupPermission } from '../permissions/note-group-permission.entity';
|
||||
|
@ -40,10 +46,24 @@ import { Tag } from './tag.entity';
|
|||
|
||||
describe('NotesService', () => {
|
||||
let service: NotesService;
|
||||
const noteMockConfig: NoteConfig = createDefaultMockNoteConfig();
|
||||
let noteRepo: Repository<Note>;
|
||||
let revisionRepo: Repository<Revision>;
|
||||
let userRepo: Repository<User>;
|
||||
let groupRepo: Repository<Group>;
|
||||
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.
|
||||
|
@ -60,6 +80,10 @@ describe('NotesService', () => {
|
|||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-ignore
|
||||
.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 revisions = await note.revisions;
|
||||
revisions[0].edits = Promise.resolve([
|
||||
|
@ -148,6 +172,16 @@ describe('NotesService', () => {
|
|||
),
|
||||
undefined,
|
||||
);
|
||||
groupRepo = new Repository<Group>(
|
||||
'',
|
||||
new EntityManager(
|
||||
new DataSource({
|
||||
type: 'sqlite',
|
||||
database: ':memory:',
|
||||
}),
|
||||
),
|
||||
undefined,
|
||||
);
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
providers: [
|
||||
NotesService,
|
||||
|
@ -168,6 +202,10 @@ describe('NotesService', () => {
|
|||
provide: getRepositoryToken(User),
|
||||
useValue: userRepo,
|
||||
},
|
||||
{
|
||||
provide: getRepositoryToken(Group),
|
||||
useValue: groupRepo,
|
||||
},
|
||||
],
|
||||
imports: [
|
||||
LoggerModule,
|
||||
|
@ -181,7 +219,7 @@ describe('NotesService', () => {
|
|||
appConfigMock,
|
||||
databaseConfigMock,
|
||||
authConfigMock,
|
||||
noteConfigMock,
|
||||
registerNoteConfig(noteMockConfig),
|
||||
],
|
||||
}),
|
||||
],
|
||||
|
@ -207,7 +245,7 @@ describe('NotesService', () => {
|
|||
.overrideProvider(getRepositoryToken(NoteUserPermission))
|
||||
.useValue({})
|
||||
.overrideProvider(getRepositoryToken(Group))
|
||||
.useClass(Repository)
|
||||
.useValue(groupRepo)
|
||||
.overrideProvider(getRepositoryToken(Session))
|
||||
.useValue({})
|
||||
.overrideProvider(getRepositoryToken(Author))
|
||||
|
@ -215,7 +253,12 @@ describe('NotesService', () => {
|
|||
.compile();
|
||||
|
||||
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);
|
||||
noteRepo = module.get<Repository<Note>>(getRepositoryToken(Note));
|
||||
revisionRepo = module.get<Repository<Revision>>(
|
||||
|
@ -297,6 +340,10 @@ describe('NotesService', () => {
|
|||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-ignore
|
||||
.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 () => {
|
||||
const newNote = await service.createNote(content, null);
|
||||
|
@ -305,7 +352,16 @@ describe('NotesService', () => {
|
|||
expect(revisions[0].content).toEqual(content);
|
||||
expect(await newNote.historyEntries).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.owner).toBeNull();
|
||||
expect(await newNote.aliases).toHaveLength(0);
|
||||
|
@ -318,7 +374,16 @@ describe('NotesService', () => {
|
|||
expect(await newNote.historyEntries).toHaveLength(1);
|
||||
expect(await (await newNote.historyEntries)[0].user).toEqual(user);
|
||||
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.owner).toEqual(user);
|
||||
expect(await newNote.aliases).toHaveLength(0);
|
||||
|
@ -330,7 +395,16 @@ describe('NotesService', () => {
|
|||
expect(revisions[0].content).toEqual(content);
|
||||
expect(await newNote.historyEntries).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.owner).toBeNull();
|
||||
expect(await newNote.aliases).toHaveLength(1);
|
||||
|
@ -343,12 +417,53 @@ describe('NotesService', () => {
|
|||
expect(await newNote.historyEntries).toHaveLength(1);
|
||||
expect(await (await newNote.historyEntries)[0].user).toEqual(user);
|
||||
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.owner).toEqual(user);
|
||||
expect(await newNote.aliases).toHaveLength(1);
|
||||
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:', () => {
|
||||
it('alias is forbidden', async () => {
|
||||
|
@ -358,6 +473,10 @@ describe('NotesService', () => {
|
|||
});
|
||||
|
||||
it('alias is already used', async () => {
|
||||
jest
|
||||
.spyOn(groupRepo, 'findOne')
|
||||
.mockResolvedValueOnce(everyone as Group)
|
||||
.mockResolvedValueOnce(loggedin as Group);
|
||||
jest.spyOn(noteRepo, 'save').mockImplementationOnce(async () => {
|
||||
throw new Error();
|
||||
});
|
||||
|
@ -376,6 +495,10 @@ describe('NotesService', () => {
|
|||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-ignore
|
||||
.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 revisions = await newNote.revisions;
|
||||
jest.spyOn(revisionRepo, 'findOne').mockResolvedValueOnce(revisions[0]);
|
||||
|
|
Loading…
Reference in a new issue