NotesService: Add unit tests for forbidden note alias

Signed-off-by: Philip Molares <philip.molares@udo.edu>
This commit is contained in:
Philip Molares 2021-02-20 22:23:52 +01:00
parent 9b25f401f7
commit 4034fa6495

View file

@ -26,6 +26,8 @@ import { NotesService } from './notes.service';
import { Repository } from 'typeorm';
import { Tag } from './tag.entity';
import {
AlreadyInDBError,
ForbiddenIdError,
NotInDBError,
PermissionsUpdateInconsistentError,
} from '../errors/errors';
@ -46,6 +48,7 @@ describe('NotesService', () => {
let revisionRepo: Repository<Revision>;
let userRepo: Repository<User>;
let groupRepo: Repository<Group>;
let forbiddenNoteId: string;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
@ -95,6 +98,8 @@ describe('NotesService', () => {
.useClass(Repository)
.compile();
const config = module.get<ConfigService>(ConfigService);
forbiddenNoteId = config.get('appConfig').forbiddenNoteIds[0];
service = module.get<NotesService>(NotesService);
noteRepo = module.get<Repository<Note>>(getRepositoryToken(Note));
revisionRepo = module.get<Repository<Revision>>(
@ -135,10 +140,10 @@ describe('NotesService', () => {
});
describe('createNote', () => {
describe('works', () => {
const user = User.create('hardcoded', 'Testy') as User;
const alias = 'alias';
const content = 'testContent';
describe('works', () => {
beforeEach(() => {
jest
.spyOn(noteRepo, 'save')
@ -195,6 +200,26 @@ describe('NotesService', () => {
expect(newNote.alias).toEqual(alias);
});
});
describe('fails:', () => {
it('alias is forbidden', async () => {
try {
await service.createNote(content, forbiddenNoteId);
} catch (e) {
expect(e).toBeInstanceOf(ForbiddenIdError);
}
});
it('alias is already used', async () => {
jest.spyOn(noteRepo, 'save').mockImplementationOnce(async () => {
throw new Error();
});
try {
await service.createNote(content, alias);
} catch (e) {
expect(e).toBeInstanceOf(AlreadyInDBError);
}
});
});
});
describe('getNoteContent', () => {
@ -252,7 +277,8 @@ describe('NotesService', () => {
const foundNote = await service.getNoteByIdOrAlias('noteThatExists');
expect(foundNote).toEqual(note);
});
it('fails: no note found', async () => {
describe('fails:', () => {
it('no note found', async () => {
jest.spyOn(noteRepo, 'findOne').mockResolvedValueOnce(undefined);
try {
await service.getNoteByIdOrAlias('noteThatDoesNoteExist');
@ -260,6 +286,15 @@ describe('NotesService', () => {
expect(e).toBeInstanceOf(NotInDBError);
}
});
it('id is forbidden', async () => {
jest.spyOn(noteRepo, 'findOne').mockResolvedValueOnce(undefined);
try {
await service.getNoteByIdOrAlias(forbiddenNoteId);
} catch (e) {
expect(e).toBeInstanceOf(ForbiddenIdError);
}
});
});
});
describe('deleteNote', () => {